fdp.c 19 KB

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