fdp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. u8 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, (unsigned long) max_size,
  181. len);
  182. skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + payload_size),
  183. GFP_KERNEL);
  184. if (!skb) {
  185. fdp_nci_set_data_pkt_counter(ndev, NULL, 0);
  186. return -ENOMEM;
  187. }
  188. skb_reserve(skb, NCI_CTRL_HDR_SIZE);
  189. memcpy(skb_put(skb, payload_size), fw->data + (fw->size - len),
  190. payload_size);
  191. rc = nci_send_data(ndev, conn_id, skb);
  192. if (rc) {
  193. fdp_nci_set_data_pkt_counter(ndev, NULL, 0);
  194. return rc;
  195. }
  196. len -= payload_size;
  197. }
  198. return rc;
  199. }
  200. static int fdp_nci_open(struct nci_dev *ndev)
  201. {
  202. int r;
  203. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  204. struct device *dev = &info->phy->i2c_dev->dev;
  205. dev_dbg(dev, "%s\n", __func__);
  206. r = info->phy_ops->enable(info->phy);
  207. return r;
  208. }
  209. static int fdp_nci_close(struct nci_dev *ndev)
  210. {
  211. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  212. struct device *dev = &info->phy->i2c_dev->dev;
  213. dev_dbg(dev, "%s\n", __func__);
  214. return 0;
  215. }
  216. static int fdp_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
  217. {
  218. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  219. struct device *dev = &info->phy->i2c_dev->dev;
  220. dev_dbg(dev, "%s\n", __func__);
  221. if (atomic_dec_and_test(&info->data_pkt_counter))
  222. info->data_pkt_counter_cb(ndev);
  223. return info->phy_ops->write(info->phy, skb);
  224. }
  225. int fdp_nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
  226. {
  227. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  228. struct device *dev = &info->phy->i2c_dev->dev;
  229. dev_dbg(dev, "%s\n", __func__);
  230. return nci_recv_frame(ndev, skb);
  231. }
  232. EXPORT_SYMBOL(fdp_nci_recv_frame);
  233. static int fdp_nci_request_firmware(struct nci_dev *ndev)
  234. {
  235. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  236. struct device *dev = &info->phy->i2c_dev->dev;
  237. u8 *data;
  238. int r;
  239. r = request_firmware(&info->ram_patch, FDP_RAM_PATCH_NAME, dev);
  240. if (r < 0) {
  241. nfc_err(dev, "RAM patch request error\n");
  242. goto error;
  243. }
  244. data = (u8 *) info->ram_patch->data;
  245. info->ram_patch_version =
  246. data[FDP_FW_HEADER_SIZE] |
  247. (data[FDP_FW_HEADER_SIZE + 1] << 8) |
  248. (data[FDP_FW_HEADER_SIZE + 2] << 16) |
  249. (data[FDP_FW_HEADER_SIZE + 3] << 24);
  250. dev_dbg(dev, "RAM patch version: %d, size: %d\n",
  251. info->ram_patch_version, (int) info->ram_patch->size);
  252. r = request_firmware(&info->otp_patch, FDP_OTP_PATCH_NAME, dev);
  253. if (r < 0) {
  254. nfc_err(dev, "OTP patch request error\n");
  255. goto out;
  256. }
  257. data = (u8 *) info->otp_patch->data;
  258. info->otp_patch_version =
  259. data[FDP_FW_HEADER_SIZE] |
  260. (data[FDP_FW_HEADER_SIZE + 1] << 8) |
  261. (data[FDP_FW_HEADER_SIZE+2] << 16) |
  262. (data[FDP_FW_HEADER_SIZE+3] << 24);
  263. dev_dbg(dev, "OTP patch version: %d, size: %d\n",
  264. info->otp_patch_version, (int) info->otp_patch->size);
  265. out:
  266. return 0;
  267. error:
  268. return r;
  269. }
  270. static void fdp_nci_release_firmware(struct nci_dev *ndev)
  271. {
  272. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  273. if (info->otp_patch) {
  274. release_firmware(info->otp_patch);
  275. info->otp_patch = NULL;
  276. }
  277. if (info->ram_patch) {
  278. release_firmware(info->ram_patch);
  279. info->ram_patch = NULL;
  280. }
  281. }
  282. static int fdp_nci_patch_otp(struct nci_dev *ndev)
  283. {
  284. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  285. struct device *dev = &info->phy->i2c_dev->dev;
  286. int conn_id;
  287. int r = 0;
  288. if (info->otp_version >= info->otp_patch_version)
  289. goto out;
  290. info->setup_patch_sent = 0;
  291. info->setup_reset_ntf = 0;
  292. info->setup_patch_ntf = 0;
  293. /* Patch init request */
  294. r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_OTP);
  295. if (r)
  296. goto out;
  297. /* Patch data connection creation */
  298. conn_id = fdp_nci_create_conn(ndev);
  299. if (conn_id < 0) {
  300. r = conn_id;
  301. goto out;
  302. }
  303. /* Send the patch over the data connection */
  304. r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_OTP);
  305. if (r)
  306. goto out;
  307. /* Wait for all the packets to be send over i2c */
  308. wait_event_interruptible(info->setup_wq,
  309. info->setup_patch_sent == 1);
  310. /* make sure that the NFCC processed the last data packet */
  311. msleep(FDP_FW_UPDATE_SLEEP);
  312. /* Close the data connection */
  313. r = nci_core_conn_close(info->ndev, conn_id);
  314. if (r)
  315. goto out;
  316. /* Patch finish message */
  317. if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
  318. nfc_err(dev, "OTP patch error 0x%x\n", r);
  319. r = -EINVAL;
  320. goto out;
  321. }
  322. /* If the patch notification didn't arrive yet, wait for it */
  323. wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
  324. /* Check if the patching was successful */
  325. r = info->setup_patch_status;
  326. if (r) {
  327. nfc_err(dev, "OTP patch error 0x%x\n", r);
  328. r = -EINVAL;
  329. goto out;
  330. }
  331. /*
  332. * We need to wait for the reset notification before we
  333. * can continue
  334. */
  335. wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
  336. out:
  337. return r;
  338. }
  339. static int fdp_nci_patch_ram(struct nci_dev *ndev)
  340. {
  341. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  342. struct device *dev = &info->phy->i2c_dev->dev;
  343. int conn_id;
  344. int r = 0;
  345. if (info->ram_version >= info->ram_patch_version)
  346. goto out;
  347. info->setup_patch_sent = 0;
  348. info->setup_reset_ntf = 0;
  349. info->setup_patch_ntf = 0;
  350. /* Patch init request */
  351. r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_RAM);
  352. if (r)
  353. goto out;
  354. /* Patch data connection creation */
  355. conn_id = fdp_nci_create_conn(ndev);
  356. if (conn_id < 0) {
  357. r = conn_id;
  358. goto out;
  359. }
  360. /* Send the patch over the data connection */
  361. r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_RAM);
  362. if (r)
  363. goto out;
  364. /* Wait for all the packets to be send over i2c */
  365. wait_event_interruptible(info->setup_wq,
  366. info->setup_patch_sent == 1);
  367. /* make sure that the NFCC processed the last data packet */
  368. msleep(FDP_FW_UPDATE_SLEEP);
  369. /* Close the data connection */
  370. r = nci_core_conn_close(info->ndev, conn_id);
  371. if (r)
  372. goto out;
  373. /* Patch finish message */
  374. if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
  375. nfc_err(dev, "RAM patch error 0x%x\n", r);
  376. r = -EINVAL;
  377. goto out;
  378. }
  379. /* If the patch notification didn't arrive yet, wait for it */
  380. wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
  381. /* Check if the patching was successful */
  382. r = info->setup_patch_status;
  383. if (r) {
  384. nfc_err(dev, "RAM patch error 0x%x\n", r);
  385. r = -EINVAL;
  386. goto out;
  387. }
  388. /*
  389. * We need to wait for the reset notification before we
  390. * can continue
  391. */
  392. wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
  393. out:
  394. return r;
  395. }
  396. static int fdp_nci_setup(struct nci_dev *ndev)
  397. {
  398. /* Format: total length followed by an NCI packet */
  399. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  400. struct device *dev = &info->phy->i2c_dev->dev;
  401. int r;
  402. u8 patched = 0;
  403. dev_dbg(dev, "%s\n", __func__);
  404. r = nci_core_init(ndev);
  405. if (r)
  406. goto error;
  407. /* Get RAM and OTP version */
  408. r = fdp_nci_get_versions(ndev);
  409. if (r)
  410. goto error;
  411. /* Load firmware from disk */
  412. r = fdp_nci_request_firmware(ndev);
  413. if (r)
  414. goto error;
  415. /* Update OTP */
  416. if (info->otp_version < info->otp_patch_version) {
  417. r = fdp_nci_patch_otp(ndev);
  418. if (r)
  419. goto error;
  420. patched = 1;
  421. }
  422. /* Update RAM */
  423. if (info->ram_version < info->ram_patch_version) {
  424. r = fdp_nci_patch_ram(ndev);
  425. if (r)
  426. goto error;
  427. patched = 1;
  428. }
  429. /* Release the firmware buffers */
  430. fdp_nci_release_firmware(ndev);
  431. /* If a patch was applied the new version is checked */
  432. if (patched) {
  433. r = nci_core_init(ndev);
  434. if (r)
  435. goto error;
  436. r = fdp_nci_get_versions(ndev);
  437. if (r)
  438. goto error;
  439. if (info->otp_version != info->otp_patch_version ||
  440. info->ram_version != info->ram_patch_version) {
  441. nfc_err(dev, "Firmware update failed");
  442. r = -EINVAL;
  443. goto error;
  444. }
  445. }
  446. /*
  447. * We initialized the devices but the NFC subsystem expects
  448. * it to not be initialized.
  449. */
  450. return nci_core_reset(ndev);
  451. error:
  452. fdp_nci_release_firmware(ndev);
  453. nfc_err(dev, "Setup error %d\n", r);
  454. return r;
  455. }
  456. static int fdp_nci_post_setup(struct nci_dev *ndev)
  457. {
  458. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  459. struct device *dev = &info->phy->i2c_dev->dev;
  460. int r;
  461. /* Check if the device has VSC */
  462. if (info->fw_vsc_cfg && info->fw_vsc_cfg[0]) {
  463. /* Set the vendor specific configuration */
  464. r = fdp_nci_set_production_data(ndev, info->fw_vsc_cfg[3],
  465. &info->fw_vsc_cfg[4]);
  466. if (r) {
  467. nfc_err(dev, "Vendor specific config set error %d\n",
  468. r);
  469. return r;
  470. }
  471. }
  472. /* Set clock type and frequency */
  473. r = fdp_nci_set_clock(ndev, info->clock_type, info->clock_freq);
  474. if (r) {
  475. nfc_err(dev, "Clock set error %d\n", r);
  476. return r;
  477. }
  478. /*
  479. * In order to apply the VSC FDP needs a reset
  480. */
  481. r = nci_core_reset(ndev);
  482. if (r)
  483. return r;
  484. /**
  485. * The nci core was initialized when post setup was called
  486. * so we leave it like that
  487. */
  488. return nci_core_init(ndev);
  489. }
  490. static int fdp_nci_core_reset_ntf_packet(struct nci_dev *ndev,
  491. struct sk_buff *skb)
  492. {
  493. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  494. struct device *dev = &info->phy->i2c_dev->dev;
  495. dev_dbg(dev, "%s\n", __func__);
  496. info->setup_reset_ntf = 1;
  497. wake_up(&info->setup_wq);
  498. return 0;
  499. }
  500. static int fdp_nci_prop_patch_ntf_packet(struct nci_dev *ndev,
  501. struct sk_buff *skb)
  502. {
  503. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  504. struct device *dev = &info->phy->i2c_dev->dev;
  505. dev_dbg(dev, "%s\n", __func__);
  506. info->setup_patch_ntf = 1;
  507. info->setup_patch_status = skb->data[0];
  508. wake_up(&info->setup_wq);
  509. return 0;
  510. }
  511. static int fdp_nci_prop_patch_rsp_packet(struct nci_dev *ndev,
  512. struct sk_buff *skb)
  513. {
  514. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  515. struct device *dev = &info->phy->i2c_dev->dev;
  516. u8 status = skb->data[0];
  517. dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
  518. nci_req_complete(ndev, status);
  519. return 0;
  520. }
  521. static int fdp_nci_prop_set_production_data_rsp_packet(struct nci_dev *ndev,
  522. struct sk_buff *skb)
  523. {
  524. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  525. struct device *dev = &info->phy->i2c_dev->dev;
  526. u8 status = skb->data[0];
  527. dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
  528. nci_req_complete(ndev, status);
  529. return 0;
  530. }
  531. static int fdp_nci_core_get_config_rsp_packet(struct nci_dev *ndev,
  532. struct sk_buff *skb)
  533. {
  534. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  535. struct device *dev = &info->phy->i2c_dev->dev;
  536. struct nci_core_get_config_rsp *rsp = (void *) skb->data;
  537. u8 i, *p;
  538. if (rsp->status == NCI_STATUS_OK) {
  539. p = rsp->data;
  540. for (i = 0; i < 4; i++) {
  541. switch (*p++) {
  542. case NCI_PARAM_ID_FW_RAM_VERSION:
  543. p++;
  544. info->ram_version = le32_to_cpup((__le32 *) p);
  545. p += 4;
  546. break;
  547. case NCI_PARAM_ID_FW_OTP_VERSION:
  548. p++;
  549. info->otp_version = le32_to_cpup((__le32 *) p);
  550. p += 4;
  551. break;
  552. case NCI_PARAM_ID_OTP_LIMITED_VERSION:
  553. p++;
  554. info->otp_version = le32_to_cpup((__le32 *) p);
  555. p += 4;
  556. break;
  557. case NCI_PARAM_ID_KEY_INDEX_ID:
  558. p++;
  559. info->key_index = *p++;
  560. }
  561. }
  562. }
  563. dev_dbg(dev, "OTP version %d\n", info->otp_version);
  564. dev_dbg(dev, "RAM version %d\n", info->ram_version);
  565. dev_dbg(dev, "key index %d\n", info->key_index);
  566. dev_dbg(dev, "%s: status 0x%x\n", __func__, rsp->status);
  567. nci_req_complete(ndev, rsp->status);
  568. return 0;
  569. }
  570. static struct nci_driver_ops fdp_core_ops[] = {
  571. {
  572. .opcode = NCI_OP_CORE_GET_CONFIG_RSP,
  573. .rsp = fdp_nci_core_get_config_rsp_packet,
  574. },
  575. {
  576. .opcode = NCI_OP_CORE_RESET_NTF,
  577. .ntf = fdp_nci_core_reset_ntf_packet,
  578. },
  579. };
  580. static struct nci_driver_ops fdp_prop_ops[] = {
  581. {
  582. .opcode = nci_opcode_pack(NCI_GID_PROP, NCI_OP_PROP_PATCH_OID),
  583. .rsp = fdp_nci_prop_patch_rsp_packet,
  584. .ntf = fdp_nci_prop_patch_ntf_packet,
  585. },
  586. {
  587. .opcode = nci_opcode_pack(NCI_GID_PROP,
  588. NCI_OP_PROP_SET_PDATA_OID),
  589. .rsp = fdp_nci_prop_set_production_data_rsp_packet,
  590. },
  591. };
  592. struct nci_ops nci_ops = {
  593. .open = fdp_nci_open,
  594. .close = fdp_nci_close,
  595. .send = fdp_nci_send,
  596. .setup = fdp_nci_setup,
  597. .post_setup = fdp_nci_post_setup,
  598. .prop_ops = fdp_prop_ops,
  599. .n_prop_ops = ARRAY_SIZE(fdp_prop_ops),
  600. .core_ops = fdp_core_ops,
  601. .n_core_ops = ARRAY_SIZE(fdp_core_ops),
  602. };
  603. int fdp_nci_probe(struct fdp_i2c_phy *phy, struct nfc_phy_ops *phy_ops,
  604. struct nci_dev **ndevp, int tx_headroom,
  605. int tx_tailroom, u8 clock_type, u32 clock_freq,
  606. u8 *fw_vsc_cfg)
  607. {
  608. struct device *dev = &phy->i2c_dev->dev;
  609. struct fdp_nci_info *info;
  610. struct nci_dev *ndev;
  611. u32 protocols;
  612. int r;
  613. info = kzalloc(sizeof(struct fdp_nci_info), GFP_KERNEL);
  614. if (!info) {
  615. r = -ENOMEM;
  616. goto err_info_alloc;
  617. }
  618. info->phy = phy;
  619. info->phy_ops = phy_ops;
  620. info->clock_type = clock_type;
  621. info->clock_freq = clock_freq;
  622. info->fw_vsc_cfg = fw_vsc_cfg;
  623. init_waitqueue_head(&info->setup_wq);
  624. protocols = NFC_PROTO_JEWEL_MASK |
  625. NFC_PROTO_MIFARE_MASK |
  626. NFC_PROTO_FELICA_MASK |
  627. NFC_PROTO_ISO14443_MASK |
  628. NFC_PROTO_ISO14443_B_MASK |
  629. NFC_PROTO_NFC_DEP_MASK |
  630. NFC_PROTO_ISO15693_MASK;
  631. ndev = nci_allocate_device(&nci_ops, protocols, tx_headroom,
  632. tx_tailroom);
  633. if (!ndev) {
  634. nfc_err(dev, "Cannot allocate nfc ndev\n");
  635. r = -ENOMEM;
  636. goto err_alloc_ndev;
  637. }
  638. r = nci_register_device(ndev);
  639. if (r)
  640. goto err_regdev;
  641. *ndevp = ndev;
  642. info->ndev = ndev;
  643. nci_set_drvdata(ndev, info);
  644. return 0;
  645. err_regdev:
  646. nci_free_device(ndev);
  647. err_alloc_ndev:
  648. kfree(info);
  649. err_info_alloc:
  650. return r;
  651. }
  652. EXPORT_SYMBOL(fdp_nci_probe);
  653. void fdp_nci_remove(struct nci_dev *ndev)
  654. {
  655. struct fdp_nci_info *info = nci_get_drvdata(ndev);
  656. struct device *dev = &info->phy->i2c_dev->dev;
  657. dev_dbg(dev, "%s\n", __func__);
  658. nci_unregister_device(ndev);
  659. nci_free_device(ndev);
  660. kfree(info);
  661. }
  662. EXPORT_SYMBOL(fdp_nci_remove);
  663. MODULE_LICENSE("GPL");
  664. MODULE_DESCRIPTION("NFC NCI driver for Intel Fields Peak NFC controller");
  665. MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");