fdp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* -------------------------------------------------------------------------
  3. * Copyright (C) 2014-2016, Intel Corporation
  4. *
  5. * -------------------------------------------------------------------------
  6. */
  7. #include <linux/module.h>
  8. #include <linux/nfc.h>
  9. #include <linux/i2c.h>
  10. #include <linux/delay.h>
  11. #include <linux/firmware.h>
  12. #include <net/nfc/nci_core.h>
  13. #include "fdp.h"
  14. #define FDP_OTP_PATCH_NAME "otp.bin"
  15. #define FDP_RAM_PATCH_NAME "ram.bin"
  16. #define FDP_FW_HEADER_SIZE 576
  17. #define FDP_FW_UPDATE_SLEEP 1000
  18. #define NCI_GET_VERSION_TIMEOUT 8000
  19. #define NCI_PATCH_REQUEST_TIMEOUT 8000
  20. #define FDP_PATCH_CONN_DEST 0xC2
  21. #define FDP_PATCH_CONN_PARAM_TYPE 0xA0
  22. #define NCI_PATCH_TYPE_RAM 0x00
  23. #define NCI_PATCH_TYPE_OTP 0x01
  24. #define NCI_PATCH_TYPE_EOT 0xFF
  25. #define NCI_PARAM_ID_FW_RAM_VERSION 0xA0
  26. #define NCI_PARAM_ID_FW_OTP_VERSION 0xA1
  27. #define NCI_PARAM_ID_OTP_LIMITED_VERSION 0xC5
  28. #define NCI_PARAM_ID_KEY_INDEX_ID 0xC6
  29. #define NCI_GID_PROP 0x0F
  30. #define NCI_OP_PROP_PATCH_OID 0x08
  31. #define NCI_OP_PROP_SET_PDATA_OID 0x23
  32. struct fdp_nci_info {
  33. const struct nfc_phy_ops *phy_ops;
  34. struct fdp_i2c_phy *phy;
  35. struct nci_dev *ndev;
  36. const struct firmware *otp_patch;
  37. const struct firmware *ram_patch;
  38. u32 otp_patch_version;
  39. u32 ram_patch_version;
  40. u32 otp_version;
  41. u32 ram_version;
  42. u32 limited_otp_version;
  43. u8 key_index;
  44. const u8 *fw_vsc_cfg;
  45. u8 clock_type;
  46. u32 clock_freq;
  47. atomic_t data_pkt_counter;
  48. void (*data_pkt_counter_cb)(struct nci_dev *ndev);
  49. u8 setup_patch_sent;
  50. u8 setup_patch_ntf;
  51. u8 setup_patch_status;
  52. u8 setup_reset_ntf;
  53. wait_queue_head_t setup_wq;
  54. };
  55. static const u8 nci_core_get_config_otp_ram_version[5] = {
  56. 0x04,
  57. NCI_PARAM_ID_FW_RAM_VERSION,
  58. NCI_PARAM_ID_FW_OTP_VERSION,
  59. NCI_PARAM_ID_OTP_LIMITED_VERSION,
  60. NCI_PARAM_ID_KEY_INDEX_ID
  61. };
  62. struct nci_core_get_config_rsp {
  63. u8 status;
  64. u8 count;
  65. u8 data[];
  66. };
  67. static int fdp_nci_create_conn(struct nci_dev *ndev)
  68. {
  69. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  70. struct core_conn_create_dest_spec_params param;
  71. int r;
  72. /* proprietary destination specific paramerer without value */
  73. param.type = FDP_PATCH_CONN_PARAM_TYPE;
  74. param.length = 0x00;
  75. r = nci_core_conn_create(info->ndev, FDP_PATCH_CONN_DEST, 1,
  76. sizeof(param), &param);
  77. if (r)
  78. return r;
  79. return nci_get_conn_info_by_dest_type_params(ndev,
  80. FDP_PATCH_CONN_DEST, NULL);
  81. }
  82. static inline int fdp_nci_get_versions(struct nci_dev *ndev)
  83. {
  84. return nci_core_cmd(ndev, NCI_OP_CORE_GET_CONFIG_CMD,
  85. sizeof(nci_core_get_config_otp_ram_version),
  86. (__u8 *) &nci_core_get_config_otp_ram_version);
  87. }
  88. static inline int fdp_nci_patch_cmd(struct nci_dev *ndev, u8 type)
  89. {
  90. return nci_prop_cmd(ndev, NCI_OP_PROP_PATCH_OID, sizeof(type), &type);
  91. }
  92. static inline int fdp_nci_set_production_data(struct nci_dev *ndev, u8 len,
  93. const char *data)
  94. {
  95. return nci_prop_cmd(ndev, NCI_OP_PROP_SET_PDATA_OID, len, data);
  96. }
  97. static int fdp_nci_set_clock(struct nci_dev *ndev, u8 clock_type,
  98. u32 clock_freq)
  99. {
  100. u32 fc = 13560;
  101. u32 nd, num, delta;
  102. char data[9];
  103. nd = (24 * fc) / clock_freq;
  104. delta = 24 * fc - nd * clock_freq;
  105. num = (32768 * delta) / clock_freq;
  106. data[0] = 0x00;
  107. data[1] = 0x00;
  108. data[2] = 0x00;
  109. data[3] = 0x10;
  110. data[4] = 0x04;
  111. data[5] = num & 0xFF;
  112. data[6] = (num >> 8) & 0xff;
  113. data[7] = nd;
  114. data[8] = clock_type;
  115. return fdp_nci_set_production_data(ndev, 9, data);
  116. }
  117. static void fdp_nci_send_patch_cb(struct nci_dev *ndev)
  118. {
  119. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  120. info->setup_patch_sent = 1;
  121. wake_up(&info->setup_wq);
  122. }
  123. /*
  124. * Register a packet sent counter and a callback
  125. *
  126. * We have no other way of knowing when all firmware packets were sent out
  127. * on the i2c bus. We need to know that in order to close the connection and
  128. * send the patch end message.
  129. */
  130. static void fdp_nci_set_data_pkt_counter(struct nci_dev *ndev,
  131. void (*cb)(struct nci_dev *ndev), int count)
  132. {
  133. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  134. struct device *dev = &info->phy->i2c_dev->dev;
  135. dev_dbg(dev, "NCI data pkt counter %d\n", count);
  136. atomic_set(&info->data_pkt_counter, count);
  137. info->data_pkt_counter_cb = cb;
  138. }
  139. /*
  140. * The device is expecting a stream of packets. All packets need to
  141. * have the PBF flag set to 0x0 (last packet) even if the firmware
  142. * file is segmented and there are multiple packets. If we give the
  143. * whole firmware to nci_send_data it will segment it and it will set
  144. * the PBF flag to 0x01 so we need to do the segmentation here.
  145. *
  146. * The firmware will be analyzed and applied when we send NCI_OP_PROP_PATCH_CMD
  147. * command with NCI_PATCH_TYPE_EOT parameter. The device will send a
  148. * NFCC_PATCH_NTF packet and a NCI_OP_CORE_RESET_NTF packet.
  149. */
  150. static int fdp_nci_send_patch(struct nci_dev *ndev, u8 conn_id, u8 type)
  151. {
  152. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  153. const struct firmware *fw;
  154. struct sk_buff *skb;
  155. unsigned long len;
  156. int max_size, payload_size;
  157. int rc = 0;
  158. if ((type == NCI_PATCH_TYPE_OTP && !info->otp_patch) ||
  159. (type == NCI_PATCH_TYPE_RAM && !info->ram_patch))
  160. return -EINVAL;
  161. if (type == NCI_PATCH_TYPE_OTP)
  162. fw = info->otp_patch;
  163. else
  164. fw = info->ram_patch;
  165. max_size = nci_conn_max_data_pkt_payload_size(ndev, conn_id);
  166. if (max_size <= 0)
  167. return -EINVAL;
  168. len = fw->size;
  169. fdp_nci_set_data_pkt_counter(ndev, fdp_nci_send_patch_cb,
  170. DIV_ROUND_UP(fw->size, max_size));
  171. while (len) {
  172. payload_size = min_t(unsigned long, max_size, len);
  173. skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + payload_size),
  174. GFP_KERNEL);
  175. if (!skb) {
  176. fdp_nci_set_data_pkt_counter(ndev, NULL, 0);
  177. return -ENOMEM;
  178. }
  179. skb_reserve(skb, NCI_CTRL_HDR_SIZE);
  180. skb_put_data(skb, fw->data + (fw->size - len), payload_size);
  181. rc = nci_send_data(ndev, conn_id, skb);
  182. if (rc) {
  183. fdp_nci_set_data_pkt_counter(ndev, NULL, 0);
  184. return rc;
  185. }
  186. len -= payload_size;
  187. }
  188. return rc;
  189. }
  190. static int fdp_nci_open(struct nci_dev *ndev)
  191. {
  192. const struct fdp_nci_info *info = nci_get_drvdata(ndev);
  193. return info->phy_ops->enable(info->phy);
  194. }
  195. static int fdp_nci_close(struct nci_dev *ndev)
  196. {
  197. return 0;
  198. }
  199. static int fdp_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
  200. {
  201. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  202. int ret;
  203. if (atomic_dec_and_test(&info->data_pkt_counter))
  204. info->data_pkt_counter_cb(ndev);
  205. ret = info->phy_ops->write(info->phy, skb);
  206. if (ret < 0) {
  207. kfree_skb(skb);
  208. return ret;
  209. }
  210. consume_skb(skb);
  211. return 0;
  212. }
  213. static int fdp_nci_request_firmware(struct nci_dev *ndev)
  214. {
  215. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  216. struct device *dev = &info->phy->i2c_dev->dev;
  217. const u8 *data;
  218. int r;
  219. r = request_firmware(&info->ram_patch, FDP_RAM_PATCH_NAME, dev);
  220. if (r < 0) {
  221. nfc_err(dev, "RAM patch request error\n");
  222. return r;
  223. }
  224. data = info->ram_patch->data;
  225. info->ram_patch_version =
  226. data[FDP_FW_HEADER_SIZE] |
  227. (data[FDP_FW_HEADER_SIZE + 1] << 8) |
  228. (data[FDP_FW_HEADER_SIZE + 2] << 16) |
  229. (data[FDP_FW_HEADER_SIZE + 3] << 24);
  230. dev_dbg(dev, "RAM patch version: %d, size: %zu\n",
  231. info->ram_patch_version, info->ram_patch->size);
  232. r = request_firmware(&info->otp_patch, FDP_OTP_PATCH_NAME, dev);
  233. if (r < 0) {
  234. nfc_err(dev, "OTP patch request error\n");
  235. return 0;
  236. }
  237. data = (u8 *) info->otp_patch->data;
  238. info->otp_patch_version =
  239. data[FDP_FW_HEADER_SIZE] |
  240. (data[FDP_FW_HEADER_SIZE + 1] << 8) |
  241. (data[FDP_FW_HEADER_SIZE+2] << 16) |
  242. (data[FDP_FW_HEADER_SIZE+3] << 24);
  243. dev_dbg(dev, "OTP patch version: %d, size: %zu\n",
  244. info->otp_patch_version, info->otp_patch->size);
  245. return 0;
  246. }
  247. static void fdp_nci_release_firmware(struct nci_dev *ndev)
  248. {
  249. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  250. if (info->otp_patch) {
  251. release_firmware(info->otp_patch);
  252. info->otp_patch = NULL;
  253. }
  254. if (info->ram_patch) {
  255. release_firmware(info->ram_patch);
  256. info->ram_patch = NULL;
  257. }
  258. }
  259. static int fdp_nci_patch_otp(struct nci_dev *ndev)
  260. {
  261. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  262. struct device *dev = &info->phy->i2c_dev->dev;
  263. int conn_id;
  264. int r = 0;
  265. if (info->otp_version >= info->otp_patch_version)
  266. return r;
  267. info->setup_patch_sent = 0;
  268. info->setup_reset_ntf = 0;
  269. info->setup_patch_ntf = 0;
  270. /* Patch init request */
  271. r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_OTP);
  272. if (r)
  273. return r;
  274. /* Patch data connection creation */
  275. conn_id = fdp_nci_create_conn(ndev);
  276. if (conn_id < 0)
  277. return conn_id;
  278. /* Send the patch over the data connection */
  279. r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_OTP);
  280. if (r)
  281. return r;
  282. /* Wait for all the packets to be send over i2c */
  283. wait_event_interruptible(info->setup_wq,
  284. info->setup_patch_sent == 1);
  285. /* make sure that the NFCC processed the last data packet */
  286. msleep(FDP_FW_UPDATE_SLEEP);
  287. /* Close the data connection */
  288. r = nci_core_conn_close(info->ndev, conn_id);
  289. if (r)
  290. return r;
  291. /* Patch finish message */
  292. if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
  293. nfc_err(dev, "OTP patch error 0x%x\n", r);
  294. return -EINVAL;
  295. }
  296. /* If the patch notification didn't arrive yet, wait for it */
  297. wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
  298. /* Check if the patching was successful */
  299. r = info->setup_patch_status;
  300. if (r) {
  301. nfc_err(dev, "OTP patch error 0x%x\n", r);
  302. return -EINVAL;
  303. }
  304. /*
  305. * We need to wait for the reset notification before we
  306. * can continue
  307. */
  308. wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
  309. return r;
  310. }
  311. static int fdp_nci_patch_ram(struct nci_dev *ndev)
  312. {
  313. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  314. struct device *dev = &info->phy->i2c_dev->dev;
  315. int conn_id;
  316. int r = 0;
  317. if (info->ram_version >= info->ram_patch_version)
  318. return r;
  319. info->setup_patch_sent = 0;
  320. info->setup_reset_ntf = 0;
  321. info->setup_patch_ntf = 0;
  322. /* Patch init request */
  323. r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_RAM);
  324. if (r)
  325. return r;
  326. /* Patch data connection creation */
  327. conn_id = fdp_nci_create_conn(ndev);
  328. if (conn_id < 0)
  329. return conn_id;
  330. /* Send the patch over the data connection */
  331. r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_RAM);
  332. if (r)
  333. return r;
  334. /* Wait for all the packets to be send over i2c */
  335. wait_event_interruptible(info->setup_wq,
  336. info->setup_patch_sent == 1);
  337. /* make sure that the NFCC processed the last data packet */
  338. msleep(FDP_FW_UPDATE_SLEEP);
  339. /* Close the data connection */
  340. r = nci_core_conn_close(info->ndev, conn_id);
  341. if (r)
  342. return r;
  343. /* Patch finish message */
  344. if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
  345. nfc_err(dev, "RAM patch error 0x%x\n", r);
  346. return -EINVAL;
  347. }
  348. /* If the patch notification didn't arrive yet, wait for it */
  349. wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
  350. /* Check if the patching was successful */
  351. r = info->setup_patch_status;
  352. if (r) {
  353. nfc_err(dev, "RAM patch error 0x%x\n", r);
  354. return -EINVAL;
  355. }
  356. /*
  357. * We need to wait for the reset notification before we
  358. * can continue
  359. */
  360. wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
  361. return r;
  362. }
  363. static int fdp_nci_setup(struct nci_dev *ndev)
  364. {
  365. /* Format: total length followed by an NCI packet */
  366. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  367. struct device *dev = &info->phy->i2c_dev->dev;
  368. int r;
  369. u8 patched = 0;
  370. r = nci_core_init(ndev);
  371. if (r)
  372. goto error;
  373. /* Get RAM and OTP version */
  374. r = fdp_nci_get_versions(ndev);
  375. if (r)
  376. goto error;
  377. /* Load firmware from disk */
  378. r = fdp_nci_request_firmware(ndev);
  379. if (r)
  380. goto error;
  381. /* Update OTP */
  382. if (info->otp_version < info->otp_patch_version) {
  383. r = fdp_nci_patch_otp(ndev);
  384. if (r)
  385. goto error;
  386. patched = 1;
  387. }
  388. /* Update RAM */
  389. if (info->ram_version < info->ram_patch_version) {
  390. r = fdp_nci_patch_ram(ndev);
  391. if (r)
  392. goto error;
  393. patched = 1;
  394. }
  395. /* Release the firmware buffers */
  396. fdp_nci_release_firmware(ndev);
  397. /* If a patch was applied the new version is checked */
  398. if (patched) {
  399. r = nci_core_init(ndev);
  400. if (r)
  401. goto error;
  402. r = fdp_nci_get_versions(ndev);
  403. if (r)
  404. goto error;
  405. if (info->otp_version != info->otp_patch_version ||
  406. info->ram_version != info->ram_patch_version) {
  407. nfc_err(dev, "Firmware update failed");
  408. r = -EINVAL;
  409. goto error;
  410. }
  411. }
  412. /*
  413. * We initialized the devices but the NFC subsystem expects
  414. * it to not be initialized.
  415. */
  416. return nci_core_reset(ndev);
  417. error:
  418. fdp_nci_release_firmware(ndev);
  419. nfc_err(dev, "Setup error %d\n", r);
  420. return r;
  421. }
  422. static int fdp_nci_post_setup(struct nci_dev *ndev)
  423. {
  424. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  425. struct device *dev = &info->phy->i2c_dev->dev;
  426. int r;
  427. /* Check if the device has VSC */
  428. if (info->fw_vsc_cfg && info->fw_vsc_cfg[0]) {
  429. /* Set the vendor specific configuration */
  430. r = fdp_nci_set_production_data(ndev, info->fw_vsc_cfg[3],
  431. &info->fw_vsc_cfg[4]);
  432. if (r) {
  433. nfc_err(dev, "Vendor specific config set error %d\n",
  434. r);
  435. return r;
  436. }
  437. }
  438. /* Set clock type and frequency */
  439. r = fdp_nci_set_clock(ndev, info->clock_type, info->clock_freq);
  440. if (r) {
  441. nfc_err(dev, "Clock set error %d\n", r);
  442. return r;
  443. }
  444. /*
  445. * In order to apply the VSC FDP needs a reset
  446. */
  447. r = nci_core_reset(ndev);
  448. if (r)
  449. return r;
  450. /**
  451. * The nci core was initialized when post setup was called
  452. * so we leave it like that
  453. */
  454. return nci_core_init(ndev);
  455. }
  456. static int fdp_nci_core_reset_ntf_packet(struct nci_dev *ndev,
  457. struct sk_buff *skb)
  458. {
  459. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  460. info->setup_reset_ntf = 1;
  461. wake_up(&info->setup_wq);
  462. return 0;
  463. }
  464. static int fdp_nci_prop_patch_ntf_packet(struct nci_dev *ndev,
  465. struct sk_buff *skb)
  466. {
  467. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  468. info->setup_patch_ntf = 1;
  469. info->setup_patch_status = skb->data[0];
  470. wake_up(&info->setup_wq);
  471. return 0;
  472. }
  473. static int fdp_nci_prop_patch_rsp_packet(struct nci_dev *ndev,
  474. struct sk_buff *skb)
  475. {
  476. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  477. struct device *dev = &info->phy->i2c_dev->dev;
  478. u8 status = skb->data[0];
  479. dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
  480. nci_req_complete(ndev, status);
  481. return 0;
  482. }
  483. static int fdp_nci_prop_set_production_data_rsp_packet(struct nci_dev *ndev,
  484. struct sk_buff *skb)
  485. {
  486. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  487. struct device *dev = &info->phy->i2c_dev->dev;
  488. u8 status = skb->data[0];
  489. dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
  490. nci_req_complete(ndev, status);
  491. return 0;
  492. }
  493. static int fdp_nci_core_get_config_rsp_packet(struct nci_dev *ndev,
  494. struct sk_buff *skb)
  495. {
  496. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  497. struct device *dev = &info->phy->i2c_dev->dev;
  498. const struct nci_core_get_config_rsp *rsp = (void *) skb->data;
  499. unsigned int i;
  500. const u8 *p;
  501. if (rsp->status == NCI_STATUS_OK) {
  502. p = rsp->data;
  503. for (i = 0; i < 4; i++) {
  504. switch (*p++) {
  505. case NCI_PARAM_ID_FW_RAM_VERSION:
  506. p++;
  507. info->ram_version = le32_to_cpup((__le32 *) p);
  508. p += 4;
  509. break;
  510. case NCI_PARAM_ID_FW_OTP_VERSION:
  511. p++;
  512. info->otp_version = le32_to_cpup((__le32 *) p);
  513. p += 4;
  514. break;
  515. case NCI_PARAM_ID_OTP_LIMITED_VERSION:
  516. p++;
  517. info->otp_version = le32_to_cpup((__le32 *) p);
  518. p += 4;
  519. break;
  520. case NCI_PARAM_ID_KEY_INDEX_ID:
  521. p++;
  522. info->key_index = *p++;
  523. }
  524. }
  525. }
  526. dev_dbg(dev, "OTP version %d\n", info->otp_version);
  527. dev_dbg(dev, "RAM version %d\n", info->ram_version);
  528. dev_dbg(dev, "key index %d\n", info->key_index);
  529. dev_dbg(dev, "%s: status 0x%x\n", __func__, rsp->status);
  530. nci_req_complete(ndev, rsp->status);
  531. return 0;
  532. }
  533. static const struct nci_driver_ops fdp_core_ops[] = {
  534. {
  535. .opcode = NCI_OP_CORE_GET_CONFIG_RSP,
  536. .rsp = fdp_nci_core_get_config_rsp_packet,
  537. },
  538. {
  539. .opcode = NCI_OP_CORE_RESET_NTF,
  540. .ntf = fdp_nci_core_reset_ntf_packet,
  541. },
  542. };
  543. static const struct nci_driver_ops fdp_prop_ops[] = {
  544. {
  545. .opcode = nci_opcode_pack(NCI_GID_PROP, NCI_OP_PROP_PATCH_OID),
  546. .rsp = fdp_nci_prop_patch_rsp_packet,
  547. .ntf = fdp_nci_prop_patch_ntf_packet,
  548. },
  549. {
  550. .opcode = nci_opcode_pack(NCI_GID_PROP,
  551. NCI_OP_PROP_SET_PDATA_OID),
  552. .rsp = fdp_nci_prop_set_production_data_rsp_packet,
  553. },
  554. };
  555. static const struct nci_ops nci_ops = {
  556. .open = fdp_nci_open,
  557. .close = fdp_nci_close,
  558. .send = fdp_nci_send,
  559. .setup = fdp_nci_setup,
  560. .post_setup = fdp_nci_post_setup,
  561. .prop_ops = fdp_prop_ops,
  562. .n_prop_ops = ARRAY_SIZE(fdp_prop_ops),
  563. .core_ops = fdp_core_ops,
  564. .n_core_ops = ARRAY_SIZE(fdp_core_ops),
  565. };
  566. int fdp_nci_probe(struct fdp_i2c_phy *phy, const struct nfc_phy_ops *phy_ops,
  567. struct nci_dev **ndevp, int tx_headroom,
  568. int tx_tailroom, u8 clock_type, u32 clock_freq,
  569. const u8 *fw_vsc_cfg)
  570. {
  571. struct device *dev = &phy->i2c_dev->dev;
  572. struct fdp_nci_info *info;
  573. struct nci_dev *ndev;
  574. u32 protocols;
  575. int r;
  576. info = devm_kzalloc(dev, sizeof(struct fdp_nci_info), GFP_KERNEL);
  577. if (!info)
  578. return -ENOMEM;
  579. info->phy = phy;
  580. info->phy_ops = phy_ops;
  581. info->clock_type = clock_type;
  582. info->clock_freq = clock_freq;
  583. info->fw_vsc_cfg = fw_vsc_cfg;
  584. init_waitqueue_head(&info->setup_wq);
  585. protocols = NFC_PROTO_JEWEL_MASK |
  586. NFC_PROTO_MIFARE_MASK |
  587. NFC_PROTO_FELICA_MASK |
  588. NFC_PROTO_ISO14443_MASK |
  589. NFC_PROTO_ISO14443_B_MASK |
  590. NFC_PROTO_NFC_DEP_MASK |
  591. NFC_PROTO_ISO15693_MASK;
  592. BUILD_BUG_ON(ARRAY_SIZE(fdp_prop_ops) > NCI_MAX_PROPRIETARY_CMD);
  593. ndev = nci_allocate_device(&nci_ops, protocols, tx_headroom,
  594. tx_tailroom);
  595. if (!ndev) {
  596. nfc_err(dev, "Cannot allocate nfc ndev\n");
  597. return -ENOMEM;
  598. }
  599. r = nci_register_device(ndev);
  600. if (r)
  601. goto err_regdev;
  602. *ndevp = ndev;
  603. info->ndev = ndev;
  604. nci_set_drvdata(ndev, info);
  605. return 0;
  606. err_regdev:
  607. nci_free_device(ndev);
  608. return r;
  609. }
  610. EXPORT_SYMBOL(fdp_nci_probe);
  611. void fdp_nci_remove(struct nci_dev *ndev)
  612. {
  613. nci_unregister_device(ndev);
  614. nci_free_device(ndev);
  615. }
  616. EXPORT_SYMBOL(fdp_nci_remove);
  617. MODULE_LICENSE("GPL");
  618. MODULE_DESCRIPTION("NFC NCI driver for Intel Fields Peak NFC controller");
  619. MODULE_AUTHOR("Robert Dolca <[email protected]>");