digital_core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * NFC Digital Protocol stack
  3. * Copyright (c) 2013, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #define pr_fmt(fmt) "digital: %s: " fmt, __func__
  16. #include <linux/module.h>
  17. #include "digital.h"
  18. #define DIGITAL_PROTO_NFCA_RF_TECH \
  19. (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | NFC_PROTO_NFC_DEP_MASK)
  20. #define DIGITAL_PROTO_NFCB_RF_TECH NFC_PROTO_ISO14443_B_MASK
  21. #define DIGITAL_PROTO_NFCF_RF_TECH \
  22. (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
  23. #define DIGITAL_PROTO_ISO15693_RF_TECH NFC_PROTO_ISO15693_MASK
  24. struct digital_cmd {
  25. struct list_head queue;
  26. u8 type;
  27. u8 pending;
  28. u16 timeout;
  29. struct sk_buff *req;
  30. struct sk_buff *resp;
  31. struct digital_tg_mdaa_params *mdaa_params;
  32. nfc_digital_cmd_complete_t cmd_cb;
  33. void *cb_context;
  34. };
  35. struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
  36. unsigned int len)
  37. {
  38. struct sk_buff *skb;
  39. skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
  40. GFP_KERNEL);
  41. if (skb)
  42. skb_reserve(skb, ddev->tx_headroom);
  43. return skb;
  44. }
  45. void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
  46. u8 bitwise_inv, u8 msb_first)
  47. {
  48. u16 crc;
  49. crc = crc_func(init, skb->data, skb->len);
  50. if (bitwise_inv)
  51. crc = ~crc;
  52. if (msb_first)
  53. crc = __fswab16(crc);
  54. *skb_put(skb, 1) = crc & 0xFF;
  55. *skb_put(skb, 1) = (crc >> 8) & 0xFF;
  56. }
  57. int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
  58. u16 crc_init, u8 bitwise_inv, u8 msb_first)
  59. {
  60. int rc;
  61. u16 crc;
  62. if (skb->len <= 2)
  63. return -EIO;
  64. crc = crc_func(crc_init, skb->data, skb->len - 2);
  65. if (bitwise_inv)
  66. crc = ~crc;
  67. if (msb_first)
  68. crc = __swab16(crc);
  69. rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
  70. (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
  71. if (rc)
  72. return -EIO;
  73. skb_trim(skb, skb->len - 2);
  74. return 0;
  75. }
  76. static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
  77. {
  78. ddev->ops->switch_rf(ddev, on);
  79. }
  80. static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
  81. {
  82. ddev->ops->abort_cmd(ddev);
  83. }
  84. static void digital_wq_cmd_complete(struct work_struct *work)
  85. {
  86. struct digital_cmd *cmd;
  87. struct nfc_digital_dev *ddev = container_of(work,
  88. struct nfc_digital_dev,
  89. cmd_complete_work);
  90. mutex_lock(&ddev->cmd_lock);
  91. cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
  92. queue);
  93. if (!cmd) {
  94. mutex_unlock(&ddev->cmd_lock);
  95. return;
  96. }
  97. list_del(&cmd->queue);
  98. mutex_unlock(&ddev->cmd_lock);
  99. if (!IS_ERR(cmd->resp))
  100. print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
  101. cmd->resp->data, cmd->resp->len, false);
  102. cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
  103. kfree(cmd->mdaa_params);
  104. kfree(cmd);
  105. schedule_work(&ddev->cmd_work);
  106. }
  107. static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
  108. void *arg, struct sk_buff *resp)
  109. {
  110. struct digital_cmd *cmd = arg;
  111. cmd->resp = resp;
  112. schedule_work(&ddev->cmd_complete_work);
  113. }
  114. static void digital_wq_cmd(struct work_struct *work)
  115. {
  116. int rc;
  117. struct digital_cmd *cmd;
  118. struct digital_tg_mdaa_params *params;
  119. struct nfc_digital_dev *ddev = container_of(work,
  120. struct nfc_digital_dev,
  121. cmd_work);
  122. mutex_lock(&ddev->cmd_lock);
  123. cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
  124. queue);
  125. if (!cmd || cmd->pending) {
  126. mutex_unlock(&ddev->cmd_lock);
  127. return;
  128. }
  129. mutex_unlock(&ddev->cmd_lock);
  130. if (cmd->req)
  131. print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
  132. cmd->req->data, cmd->req->len, false);
  133. switch (cmd->type) {
  134. case DIGITAL_CMD_IN_SEND:
  135. rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
  136. digital_send_cmd_complete, cmd);
  137. break;
  138. case DIGITAL_CMD_TG_SEND:
  139. rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout,
  140. digital_send_cmd_complete, cmd);
  141. break;
  142. case DIGITAL_CMD_TG_LISTEN:
  143. rc = ddev->ops->tg_listen(ddev, cmd->timeout,
  144. digital_send_cmd_complete, cmd);
  145. break;
  146. case DIGITAL_CMD_TG_LISTEN_MDAA:
  147. params = cmd->mdaa_params;
  148. rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout,
  149. digital_send_cmd_complete, cmd);
  150. break;
  151. default:
  152. pr_err("Unknown cmd type %d\n", cmd->type);
  153. return;
  154. }
  155. if (!rc)
  156. return;
  157. pr_err("in_send_command returned err %d\n", rc);
  158. mutex_lock(&ddev->cmd_lock);
  159. list_del(&cmd->queue);
  160. mutex_unlock(&ddev->cmd_lock);
  161. kfree_skb(cmd->req);
  162. kfree(cmd->mdaa_params);
  163. kfree(cmd);
  164. schedule_work(&ddev->cmd_work);
  165. }
  166. int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
  167. struct sk_buff *skb, struct digital_tg_mdaa_params *params,
  168. u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
  169. void *cb_context)
  170. {
  171. struct digital_cmd *cmd;
  172. cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL);
  173. if (!cmd)
  174. return -ENOMEM;
  175. cmd->type = cmd_type;
  176. cmd->timeout = timeout;
  177. cmd->req = skb;
  178. cmd->mdaa_params = params;
  179. cmd->cmd_cb = cmd_cb;
  180. cmd->cb_context = cb_context;
  181. INIT_LIST_HEAD(&cmd->queue);
  182. mutex_lock(&ddev->cmd_lock);
  183. list_add_tail(&cmd->queue, &ddev->cmd_queue);
  184. mutex_unlock(&ddev->cmd_lock);
  185. schedule_work(&ddev->cmd_work);
  186. return 0;
  187. }
  188. int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
  189. {
  190. int rc;
  191. rc = ddev->ops->in_configure_hw(ddev, type, param);
  192. if (rc)
  193. pr_err("in_configure_hw failed: %d\n", rc);
  194. return rc;
  195. }
  196. int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
  197. {
  198. int rc;
  199. rc = ddev->ops->tg_configure_hw(ddev, type, param);
  200. if (rc)
  201. pr_err("tg_configure_hw failed: %d\n", rc);
  202. return rc;
  203. }
  204. static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
  205. {
  206. struct digital_tg_mdaa_params *params;
  207. params = kzalloc(sizeof(struct digital_tg_mdaa_params), GFP_KERNEL);
  208. if (!params)
  209. return -ENOMEM;
  210. params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
  211. get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
  212. params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
  213. params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
  214. params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
  215. get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
  216. params->sc = DIGITAL_SENSF_FELICA_SC;
  217. return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
  218. 500, digital_tg_recv_atr_req, NULL);
  219. }
  220. int digital_target_found(struct nfc_digital_dev *ddev,
  221. struct nfc_target *target, u8 protocol)
  222. {
  223. int rc;
  224. u8 framing;
  225. u8 rf_tech;
  226. int (*check_crc)(struct sk_buff *skb);
  227. void (*add_crc)(struct sk_buff *skb);
  228. rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
  229. switch (protocol) {
  230. case NFC_PROTO_JEWEL:
  231. framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
  232. check_crc = digital_skb_check_crc_b;
  233. add_crc = digital_skb_add_crc_b;
  234. break;
  235. case NFC_PROTO_MIFARE:
  236. framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
  237. check_crc = digital_skb_check_crc_a;
  238. add_crc = digital_skb_add_crc_a;
  239. break;
  240. case NFC_PROTO_FELICA:
  241. framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
  242. check_crc = digital_skb_check_crc_f;
  243. add_crc = digital_skb_add_crc_f;
  244. break;
  245. case NFC_PROTO_NFC_DEP:
  246. if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
  247. framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
  248. check_crc = digital_skb_check_crc_a;
  249. add_crc = digital_skb_add_crc_a;
  250. } else {
  251. framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
  252. check_crc = digital_skb_check_crc_f;
  253. add_crc = digital_skb_add_crc_f;
  254. }
  255. break;
  256. case NFC_PROTO_ISO15693:
  257. framing = NFC_DIGITAL_FRAMING_ISO15693_T5T;
  258. check_crc = digital_skb_check_crc_b;
  259. add_crc = digital_skb_add_crc_b;
  260. break;
  261. case NFC_PROTO_ISO14443:
  262. framing = NFC_DIGITAL_FRAMING_NFCA_T4T;
  263. check_crc = digital_skb_check_crc_a;
  264. add_crc = digital_skb_add_crc_a;
  265. break;
  266. case NFC_PROTO_ISO14443_B:
  267. framing = NFC_DIGITAL_FRAMING_NFCB_T4T;
  268. check_crc = digital_skb_check_crc_b;
  269. add_crc = digital_skb_add_crc_b;
  270. break;
  271. default:
  272. pr_err("Invalid protocol %d\n", protocol);
  273. return -EINVAL;
  274. }
  275. pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
  276. ddev->curr_rf_tech = rf_tech;
  277. if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
  278. ddev->skb_add_crc = digital_skb_add_crc_none;
  279. ddev->skb_check_crc = digital_skb_check_crc_none;
  280. } else {
  281. ddev->skb_add_crc = add_crc;
  282. ddev->skb_check_crc = check_crc;
  283. }
  284. rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
  285. if (rc)
  286. return rc;
  287. target->supported_protocols = (1 << protocol);
  288. rc = nfc_targets_found(ddev->nfc_dev, target, 1);
  289. if (rc)
  290. return rc;
  291. ddev->poll_tech_count = 0;
  292. return 0;
  293. }
  294. void digital_poll_next_tech(struct nfc_digital_dev *ddev)
  295. {
  296. u8 rand_mod;
  297. digital_switch_rf(ddev, 0);
  298. mutex_lock(&ddev->poll_lock);
  299. if (!ddev->poll_tech_count) {
  300. mutex_unlock(&ddev->poll_lock);
  301. return;
  302. }
  303. get_random_bytes(&rand_mod, sizeof(rand_mod));
  304. ddev->poll_tech_index = rand_mod % ddev->poll_tech_count;
  305. mutex_unlock(&ddev->poll_lock);
  306. schedule_work(&ddev->poll_work);
  307. }
  308. static void digital_wq_poll(struct work_struct *work)
  309. {
  310. int rc;
  311. struct digital_poll_tech *poll_tech;
  312. struct nfc_digital_dev *ddev = container_of(work,
  313. struct nfc_digital_dev,
  314. poll_work);
  315. mutex_lock(&ddev->poll_lock);
  316. if (!ddev->poll_tech_count) {
  317. mutex_unlock(&ddev->poll_lock);
  318. return;
  319. }
  320. poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
  321. mutex_unlock(&ddev->poll_lock);
  322. rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
  323. if (rc)
  324. digital_poll_next_tech(ddev);
  325. }
  326. static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
  327. digital_poll_t poll_func)
  328. {
  329. struct digital_poll_tech *poll_tech;
  330. if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
  331. return;
  332. poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
  333. poll_tech->rf_tech = rf_tech;
  334. poll_tech->poll_func = poll_func;
  335. }
  336. /**
  337. * start_poll operation
  338. *
  339. * For every supported protocol, the corresponding polling function is added
  340. * to the table of polling technologies (ddev->poll_techs[]) using
  341. * digital_add_poll_tech().
  342. * When a polling function fails (by timeout or protocol error) the next one is
  343. * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
  344. */
  345. static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
  346. __u32 tm_protocols)
  347. {
  348. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  349. u32 matching_im_protocols, matching_tm_protocols;
  350. pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
  351. tm_protocols, ddev->protocols);
  352. matching_im_protocols = ddev->protocols & im_protocols;
  353. matching_tm_protocols = ddev->protocols & tm_protocols;
  354. if (!matching_im_protocols && !matching_tm_protocols) {
  355. pr_err("Unknown protocol\n");
  356. return -EINVAL;
  357. }
  358. if (ddev->poll_tech_count) {
  359. pr_err("Already polling\n");
  360. return -EBUSY;
  361. }
  362. if (ddev->curr_protocol) {
  363. pr_err("A target is already active\n");
  364. return -EBUSY;
  365. }
  366. ddev->poll_tech_count = 0;
  367. ddev->poll_tech_index = 0;
  368. if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
  369. digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
  370. digital_in_send_sens_req);
  371. if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH)
  372. digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B,
  373. digital_in_send_sensb_req);
  374. if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
  375. digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
  376. digital_in_send_sensf_req);
  377. digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
  378. digital_in_send_sensf_req);
  379. }
  380. if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH)
  381. digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693,
  382. digital_in_send_iso15693_inv_req);
  383. if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
  384. if (ddev->ops->tg_listen_mdaa) {
  385. digital_add_poll_tech(ddev, 0,
  386. digital_tg_listen_mdaa);
  387. } else {
  388. digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
  389. digital_tg_listen_nfca);
  390. digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
  391. digital_tg_listen_nfcf);
  392. digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
  393. digital_tg_listen_nfcf);
  394. }
  395. }
  396. if (!ddev->poll_tech_count) {
  397. pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n",
  398. matching_im_protocols, matching_tm_protocols);
  399. return -EINVAL;
  400. }
  401. schedule_work(&ddev->poll_work);
  402. return 0;
  403. }
  404. static void digital_stop_poll(struct nfc_dev *nfc_dev)
  405. {
  406. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  407. mutex_lock(&ddev->poll_lock);
  408. if (!ddev->poll_tech_count) {
  409. pr_err("Polling operation was not running\n");
  410. mutex_unlock(&ddev->poll_lock);
  411. return;
  412. }
  413. ddev->poll_tech_count = 0;
  414. mutex_unlock(&ddev->poll_lock);
  415. cancel_work_sync(&ddev->poll_work);
  416. digital_abort_cmd(ddev);
  417. }
  418. static int digital_dev_up(struct nfc_dev *nfc_dev)
  419. {
  420. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  421. digital_switch_rf(ddev, 1);
  422. return 0;
  423. }
  424. static int digital_dev_down(struct nfc_dev *nfc_dev)
  425. {
  426. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  427. digital_switch_rf(ddev, 0);
  428. return 0;
  429. }
  430. static int digital_dep_link_up(struct nfc_dev *nfc_dev,
  431. struct nfc_target *target,
  432. __u8 comm_mode, __u8 *gb, size_t gb_len)
  433. {
  434. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  435. int rc;
  436. rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
  437. if (!rc)
  438. ddev->curr_protocol = NFC_PROTO_NFC_DEP;
  439. return rc;
  440. }
  441. static int digital_dep_link_down(struct nfc_dev *nfc_dev)
  442. {
  443. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  444. ddev->curr_protocol = 0;
  445. return 0;
  446. }
  447. static int digital_activate_target(struct nfc_dev *nfc_dev,
  448. struct nfc_target *target, __u32 protocol)
  449. {
  450. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  451. if (ddev->poll_tech_count) {
  452. pr_err("Can't activate a target while polling\n");
  453. return -EBUSY;
  454. }
  455. if (ddev->curr_protocol) {
  456. pr_err("A target is already active\n");
  457. return -EBUSY;
  458. }
  459. ddev->curr_protocol = protocol;
  460. return 0;
  461. }
  462. static void digital_deactivate_target(struct nfc_dev *nfc_dev,
  463. struct nfc_target *target)
  464. {
  465. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  466. if (!ddev->curr_protocol) {
  467. pr_err("No active target\n");
  468. return;
  469. }
  470. ddev->curr_protocol = 0;
  471. }
  472. static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
  473. {
  474. struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
  475. return digital_tg_send_dep_res(ddev, skb);
  476. }
  477. static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
  478. struct sk_buff *resp)
  479. {
  480. struct digital_data_exch *data_exch = arg;
  481. int rc;
  482. if (IS_ERR(resp)) {
  483. rc = PTR_ERR(resp);
  484. resp = NULL;
  485. goto done;
  486. }
  487. if (ddev->curr_protocol == NFC_PROTO_MIFARE) {
  488. rc = digital_in_recv_mifare_res(resp);
  489. /* crc check is done in digital_in_recv_mifare_res() */
  490. goto done;
  491. }
  492. if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
  493. (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
  494. rc = digital_in_iso_dep_pull_sod(ddev, resp);
  495. if (rc)
  496. goto done;
  497. }
  498. rc = ddev->skb_check_crc(resp);
  499. done:
  500. if (rc) {
  501. kfree_skb(resp);
  502. resp = NULL;
  503. }
  504. data_exch->cb(data_exch->cb_context, resp, rc);
  505. kfree(data_exch);
  506. }
  507. static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
  508. struct sk_buff *skb, data_exchange_cb_t cb,
  509. void *cb_context)
  510. {
  511. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  512. struct digital_data_exch *data_exch;
  513. int rc;
  514. data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL);
  515. if (!data_exch) {
  516. pr_err("Failed to allocate data_exch struct\n");
  517. return -ENOMEM;
  518. }
  519. data_exch->cb = cb;
  520. data_exch->cb_context = cb_context;
  521. if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) {
  522. rc = digital_in_send_dep_req(ddev, target, skb, data_exch);
  523. goto exit;
  524. }
  525. if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
  526. (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
  527. rc = digital_in_iso_dep_push_sod(ddev, skb);
  528. if (rc)
  529. goto exit;
  530. }
  531. ddev->skb_add_crc(skb);
  532. rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
  533. data_exch);
  534. exit:
  535. if (rc)
  536. kfree(data_exch);
  537. return rc;
  538. }
  539. static struct nfc_ops digital_nfc_ops = {
  540. .dev_up = digital_dev_up,
  541. .dev_down = digital_dev_down,
  542. .start_poll = digital_start_poll,
  543. .stop_poll = digital_stop_poll,
  544. .dep_link_up = digital_dep_link_up,
  545. .dep_link_down = digital_dep_link_down,
  546. .activate_target = digital_activate_target,
  547. .deactivate_target = digital_deactivate_target,
  548. .tm_send = digital_tg_send,
  549. .im_transceive = digital_in_send,
  550. };
  551. struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
  552. __u32 supported_protocols,
  553. __u32 driver_capabilities,
  554. int tx_headroom, int tx_tailroom)
  555. {
  556. struct nfc_digital_dev *ddev;
  557. if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
  558. !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
  559. !ops->switch_rf)
  560. return NULL;
  561. ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL);
  562. if (!ddev)
  563. return NULL;
  564. ddev->driver_capabilities = driver_capabilities;
  565. ddev->ops = ops;
  566. mutex_init(&ddev->cmd_lock);
  567. INIT_LIST_HEAD(&ddev->cmd_queue);
  568. INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
  569. INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
  570. mutex_init(&ddev->poll_lock);
  571. INIT_WORK(&ddev->poll_work, digital_wq_poll);
  572. if (supported_protocols & NFC_PROTO_JEWEL_MASK)
  573. ddev->protocols |= NFC_PROTO_JEWEL_MASK;
  574. if (supported_protocols & NFC_PROTO_MIFARE_MASK)
  575. ddev->protocols |= NFC_PROTO_MIFARE_MASK;
  576. if (supported_protocols & NFC_PROTO_FELICA_MASK)
  577. ddev->protocols |= NFC_PROTO_FELICA_MASK;
  578. if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
  579. ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
  580. if (supported_protocols & NFC_PROTO_ISO15693_MASK)
  581. ddev->protocols |= NFC_PROTO_ISO15693_MASK;
  582. if (supported_protocols & NFC_PROTO_ISO14443_MASK)
  583. ddev->protocols |= NFC_PROTO_ISO14443_MASK;
  584. if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
  585. ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
  586. ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
  587. ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
  588. ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
  589. ddev->tx_headroom,
  590. ddev->tx_tailroom);
  591. if (!ddev->nfc_dev) {
  592. pr_err("nfc_allocate_device failed\n");
  593. goto free_dev;
  594. }
  595. nfc_set_drvdata(ddev->nfc_dev, ddev);
  596. return ddev;
  597. free_dev:
  598. kfree(ddev);
  599. return NULL;
  600. }
  601. EXPORT_SYMBOL(nfc_digital_allocate_device);
  602. void nfc_digital_free_device(struct nfc_digital_dev *ddev)
  603. {
  604. nfc_free_device(ddev->nfc_dev);
  605. kfree(ddev);
  606. }
  607. EXPORT_SYMBOL(nfc_digital_free_device);
  608. int nfc_digital_register_device(struct nfc_digital_dev *ddev)
  609. {
  610. return nfc_register_device(ddev->nfc_dev);
  611. }
  612. EXPORT_SYMBOL(nfc_digital_register_device);
  613. void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
  614. {
  615. struct digital_cmd *cmd, *n;
  616. nfc_unregister_device(ddev->nfc_dev);
  617. mutex_lock(&ddev->poll_lock);
  618. ddev->poll_tech_count = 0;
  619. mutex_unlock(&ddev->poll_lock);
  620. cancel_work_sync(&ddev->poll_work);
  621. cancel_work_sync(&ddev->cmd_work);
  622. cancel_work_sync(&ddev->cmd_complete_work);
  623. list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
  624. list_del(&cmd->queue);
  625. kfree(cmd->mdaa_params);
  626. kfree(cmd);
  627. }
  628. }
  629. EXPORT_SYMBOL(nfc_digital_unregister_device);
  630. MODULE_LICENSE("GPL");