digital_core.c 20 KB

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