digital_core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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. case DIGITAL_CMD_TG_LISTEN_MD:
  152. rc = ddev->ops->tg_listen_md(ddev, cmd->timeout,
  153. digital_send_cmd_complete, cmd);
  154. break;
  155. default:
  156. pr_err("Unknown cmd type %d\n", cmd->type);
  157. return;
  158. }
  159. if (!rc)
  160. return;
  161. pr_err("in_send_command returned err %d\n", rc);
  162. mutex_lock(&ddev->cmd_lock);
  163. list_del(&cmd->queue);
  164. mutex_unlock(&ddev->cmd_lock);
  165. kfree_skb(cmd->req);
  166. kfree(cmd->mdaa_params);
  167. kfree(cmd);
  168. schedule_work(&ddev->cmd_work);
  169. }
  170. int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
  171. struct sk_buff *skb, struct digital_tg_mdaa_params *params,
  172. u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
  173. void *cb_context)
  174. {
  175. struct digital_cmd *cmd;
  176. cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL);
  177. if (!cmd)
  178. return -ENOMEM;
  179. cmd->type = cmd_type;
  180. cmd->timeout = timeout;
  181. cmd->req = skb;
  182. cmd->mdaa_params = params;
  183. cmd->cmd_cb = cmd_cb;
  184. cmd->cb_context = cb_context;
  185. INIT_LIST_HEAD(&cmd->queue);
  186. mutex_lock(&ddev->cmd_lock);
  187. list_add_tail(&cmd->queue, &ddev->cmd_queue);
  188. mutex_unlock(&ddev->cmd_lock);
  189. schedule_work(&ddev->cmd_work);
  190. return 0;
  191. }
  192. int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
  193. {
  194. int rc;
  195. rc = ddev->ops->in_configure_hw(ddev, type, param);
  196. if (rc)
  197. pr_err("in_configure_hw failed: %d\n", rc);
  198. return rc;
  199. }
  200. int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
  201. {
  202. int rc;
  203. rc = ddev->ops->tg_configure_hw(ddev, type, param);
  204. if (rc)
  205. pr_err("tg_configure_hw failed: %d\n", rc);
  206. return rc;
  207. }
  208. static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
  209. {
  210. struct digital_tg_mdaa_params *params;
  211. params = kzalloc(sizeof(struct digital_tg_mdaa_params), GFP_KERNEL);
  212. if (!params)
  213. return -ENOMEM;
  214. params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
  215. get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
  216. params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
  217. params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
  218. params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
  219. get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
  220. params->sc = DIGITAL_SENSF_FELICA_SC;
  221. return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
  222. 500, digital_tg_recv_atr_req, NULL);
  223. }
  224. static int digital_tg_listen_md(struct nfc_digital_dev *ddev, u8 rf_tech)
  225. {
  226. return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MD, NULL, NULL, 500,
  227. digital_tg_recv_md_req, NULL);
  228. }
  229. int digital_target_found(struct nfc_digital_dev *ddev,
  230. struct nfc_target *target, u8 protocol)
  231. {
  232. int rc;
  233. u8 framing;
  234. u8 rf_tech;
  235. u8 poll_tech_count;
  236. int (*check_crc)(struct sk_buff *skb);
  237. void (*add_crc)(struct sk_buff *skb);
  238. rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
  239. switch (protocol) {
  240. case NFC_PROTO_JEWEL:
  241. framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
  242. check_crc = digital_skb_check_crc_b;
  243. add_crc = digital_skb_add_crc_b;
  244. break;
  245. case NFC_PROTO_MIFARE:
  246. framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
  247. check_crc = digital_skb_check_crc_a;
  248. add_crc = digital_skb_add_crc_a;
  249. break;
  250. case NFC_PROTO_FELICA:
  251. framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
  252. check_crc = digital_skb_check_crc_f;
  253. add_crc = digital_skb_add_crc_f;
  254. break;
  255. case NFC_PROTO_NFC_DEP:
  256. if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
  257. framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
  258. check_crc = digital_skb_check_crc_a;
  259. add_crc = digital_skb_add_crc_a;
  260. } else {
  261. framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
  262. check_crc = digital_skb_check_crc_f;
  263. add_crc = digital_skb_add_crc_f;
  264. }
  265. break;
  266. case NFC_PROTO_ISO15693:
  267. framing = NFC_DIGITAL_FRAMING_ISO15693_T5T;
  268. check_crc = digital_skb_check_crc_b;
  269. add_crc = digital_skb_add_crc_b;
  270. break;
  271. case NFC_PROTO_ISO14443:
  272. framing = NFC_DIGITAL_FRAMING_NFCA_T4T;
  273. check_crc = digital_skb_check_crc_a;
  274. add_crc = digital_skb_add_crc_a;
  275. break;
  276. case NFC_PROTO_ISO14443_B:
  277. framing = NFC_DIGITAL_FRAMING_NFCB_T4T;
  278. check_crc = digital_skb_check_crc_b;
  279. add_crc = digital_skb_add_crc_b;
  280. break;
  281. default:
  282. pr_err("Invalid protocol %d\n", protocol);
  283. return -EINVAL;
  284. }
  285. pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
  286. ddev->curr_rf_tech = rf_tech;
  287. if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
  288. ddev->skb_add_crc = digital_skb_add_crc_none;
  289. ddev->skb_check_crc = digital_skb_check_crc_none;
  290. } else {
  291. ddev->skb_add_crc = add_crc;
  292. ddev->skb_check_crc = check_crc;
  293. }
  294. rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
  295. if (rc)
  296. return rc;
  297. target->supported_protocols = (1 << protocol);
  298. poll_tech_count = ddev->poll_tech_count;
  299. ddev->poll_tech_count = 0;
  300. rc = nfc_targets_found(ddev->nfc_dev, target, 1);
  301. if (rc) {
  302. ddev->poll_tech_count = poll_tech_count;
  303. return rc;
  304. }
  305. return 0;
  306. }
  307. void digital_poll_next_tech(struct nfc_digital_dev *ddev)
  308. {
  309. u8 rand_mod;
  310. digital_switch_rf(ddev, 0);
  311. mutex_lock(&ddev->poll_lock);
  312. if (!ddev->poll_tech_count) {
  313. mutex_unlock(&ddev->poll_lock);
  314. return;
  315. }
  316. get_random_bytes(&rand_mod, sizeof(rand_mod));
  317. ddev->poll_tech_index = rand_mod % ddev->poll_tech_count;
  318. mutex_unlock(&ddev->poll_lock);
  319. schedule_work(&ddev->poll_work);
  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);
  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_work(&ddev->poll_work);
  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_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. ddev->curr_protocol = 0;
  461. return 0;
  462. }
  463. static int digital_activate_target(struct nfc_dev *nfc_dev,
  464. struct nfc_target *target, __u32 protocol)
  465. {
  466. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  467. if (ddev->poll_tech_count) {
  468. pr_err("Can't activate a target while polling\n");
  469. return -EBUSY;
  470. }
  471. if (ddev->curr_protocol) {
  472. pr_err("A target is already active\n");
  473. return -EBUSY;
  474. }
  475. ddev->curr_protocol = protocol;
  476. return 0;
  477. }
  478. static void digital_deactivate_target(struct nfc_dev *nfc_dev,
  479. struct nfc_target *target)
  480. {
  481. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  482. if (!ddev->curr_protocol) {
  483. pr_err("No active target\n");
  484. return;
  485. }
  486. ddev->curr_protocol = 0;
  487. }
  488. static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
  489. {
  490. struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
  491. return digital_tg_send_dep_res(ddev, skb);
  492. }
  493. static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
  494. struct sk_buff *resp)
  495. {
  496. struct digital_data_exch *data_exch = arg;
  497. int rc;
  498. if (IS_ERR(resp)) {
  499. rc = PTR_ERR(resp);
  500. resp = NULL;
  501. goto done;
  502. }
  503. if (ddev->curr_protocol == NFC_PROTO_MIFARE) {
  504. rc = digital_in_recv_mifare_res(resp);
  505. /* crc check is done in digital_in_recv_mifare_res() */
  506. goto done;
  507. }
  508. if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
  509. (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
  510. rc = digital_in_iso_dep_pull_sod(ddev, resp);
  511. if (rc)
  512. goto done;
  513. }
  514. rc = ddev->skb_check_crc(resp);
  515. done:
  516. if (rc) {
  517. kfree_skb(resp);
  518. resp = NULL;
  519. }
  520. data_exch->cb(data_exch->cb_context, resp, rc);
  521. kfree(data_exch);
  522. }
  523. static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
  524. struct sk_buff *skb, data_exchange_cb_t cb,
  525. void *cb_context)
  526. {
  527. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  528. struct digital_data_exch *data_exch;
  529. int rc;
  530. data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL);
  531. if (!data_exch) {
  532. pr_err("Failed to allocate data_exch struct\n");
  533. return -ENOMEM;
  534. }
  535. data_exch->cb = cb;
  536. data_exch->cb_context = cb_context;
  537. if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) {
  538. rc = digital_in_send_dep_req(ddev, target, skb, data_exch);
  539. goto exit;
  540. }
  541. if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
  542. (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
  543. rc = digital_in_iso_dep_push_sod(ddev, skb);
  544. if (rc)
  545. goto exit;
  546. }
  547. ddev->skb_add_crc(skb);
  548. rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
  549. data_exch);
  550. exit:
  551. if (rc)
  552. kfree(data_exch);
  553. return rc;
  554. }
  555. static struct nfc_ops digital_nfc_ops = {
  556. .dev_up = digital_dev_up,
  557. .dev_down = digital_dev_down,
  558. .start_poll = digital_start_poll,
  559. .stop_poll = digital_stop_poll,
  560. .dep_link_up = digital_dep_link_up,
  561. .dep_link_down = digital_dep_link_down,
  562. .activate_target = digital_activate_target,
  563. .deactivate_target = digital_deactivate_target,
  564. .tm_send = digital_tg_send,
  565. .im_transceive = digital_in_send,
  566. };
  567. struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
  568. __u32 supported_protocols,
  569. __u32 driver_capabilities,
  570. int tx_headroom, int tx_tailroom)
  571. {
  572. struct nfc_digital_dev *ddev;
  573. if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
  574. !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
  575. !ops->switch_rf || (ops->tg_listen_md && !ops->tg_get_rf_tech))
  576. return NULL;
  577. ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL);
  578. if (!ddev)
  579. return NULL;
  580. ddev->driver_capabilities = driver_capabilities;
  581. ddev->ops = ops;
  582. mutex_init(&ddev->cmd_lock);
  583. INIT_LIST_HEAD(&ddev->cmd_queue);
  584. INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
  585. INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
  586. mutex_init(&ddev->poll_lock);
  587. INIT_WORK(&ddev->poll_work, digital_wq_poll);
  588. if (supported_protocols & NFC_PROTO_JEWEL_MASK)
  589. ddev->protocols |= NFC_PROTO_JEWEL_MASK;
  590. if (supported_protocols & NFC_PROTO_MIFARE_MASK)
  591. ddev->protocols |= NFC_PROTO_MIFARE_MASK;
  592. if (supported_protocols & NFC_PROTO_FELICA_MASK)
  593. ddev->protocols |= NFC_PROTO_FELICA_MASK;
  594. if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
  595. ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
  596. if (supported_protocols & NFC_PROTO_ISO15693_MASK)
  597. ddev->protocols |= NFC_PROTO_ISO15693_MASK;
  598. if (supported_protocols & NFC_PROTO_ISO14443_MASK)
  599. ddev->protocols |= NFC_PROTO_ISO14443_MASK;
  600. if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
  601. ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
  602. ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
  603. ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
  604. ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
  605. ddev->tx_headroom,
  606. ddev->tx_tailroom);
  607. if (!ddev->nfc_dev) {
  608. pr_err("nfc_allocate_device failed\n");
  609. goto free_dev;
  610. }
  611. nfc_set_drvdata(ddev->nfc_dev, ddev);
  612. return ddev;
  613. free_dev:
  614. kfree(ddev);
  615. return NULL;
  616. }
  617. EXPORT_SYMBOL(nfc_digital_allocate_device);
  618. void nfc_digital_free_device(struct nfc_digital_dev *ddev)
  619. {
  620. nfc_free_device(ddev->nfc_dev);
  621. kfree(ddev);
  622. }
  623. EXPORT_SYMBOL(nfc_digital_free_device);
  624. int nfc_digital_register_device(struct nfc_digital_dev *ddev)
  625. {
  626. return nfc_register_device(ddev->nfc_dev);
  627. }
  628. EXPORT_SYMBOL(nfc_digital_register_device);
  629. void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
  630. {
  631. struct digital_cmd *cmd, *n;
  632. nfc_unregister_device(ddev->nfc_dev);
  633. mutex_lock(&ddev->poll_lock);
  634. ddev->poll_tech_count = 0;
  635. mutex_unlock(&ddev->poll_lock);
  636. cancel_work_sync(&ddev->poll_work);
  637. cancel_work_sync(&ddev->cmd_work);
  638. cancel_work_sync(&ddev->cmd_complete_work);
  639. list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
  640. list_del(&cmd->queue);
  641. kfree(cmd->mdaa_params);
  642. kfree(cmd);
  643. }
  644. }
  645. EXPORT_SYMBOL(nfc_digital_unregister_device);
  646. MODULE_LICENSE("GPL");