digital_core.c 19 KB

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