digital_core.c 18 KB

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