digital_core.c 20 KB

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