digital_core.c 20 KB

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