fdp.c 19 KB

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