digital_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. #include <linux/module.h>
  16. #include "digital.h"
  17. #define DIGITAL_PROTO_NFCA_RF_TECH \
  18. (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK)
  19. struct digital_cmd {
  20. struct list_head queue;
  21. u8 type;
  22. u8 pending;
  23. u16 timeout;
  24. struct sk_buff *req;
  25. struct sk_buff *resp;
  26. nfc_digital_cmd_complete_t cmd_cb;
  27. void *cb_context;
  28. };
  29. struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
  30. unsigned int len)
  31. {
  32. struct sk_buff *skb;
  33. skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
  34. GFP_KERNEL);
  35. if (skb)
  36. skb_reserve(skb, ddev->tx_headroom);
  37. return skb;
  38. }
  39. static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
  40. {
  41. ddev->ops->switch_rf(ddev, on);
  42. }
  43. static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
  44. {
  45. ddev->ops->abort_cmd(ddev);
  46. }
  47. static void digital_wq_cmd_complete(struct work_struct *work)
  48. {
  49. struct digital_cmd *cmd;
  50. struct nfc_digital_dev *ddev = container_of(work,
  51. struct nfc_digital_dev,
  52. cmd_complete_work);
  53. mutex_lock(&ddev->cmd_lock);
  54. cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
  55. queue);
  56. if (!cmd) {
  57. mutex_unlock(&ddev->cmd_lock);
  58. return;
  59. }
  60. list_del(&cmd->queue);
  61. mutex_unlock(&ddev->cmd_lock);
  62. if (!IS_ERR(cmd->resp))
  63. print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
  64. cmd->resp->data, cmd->resp->len, false);
  65. cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
  66. kfree(cmd);
  67. schedule_work(&ddev->cmd_work);
  68. }
  69. static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
  70. void *arg, struct sk_buff *resp)
  71. {
  72. struct digital_cmd *cmd = arg;
  73. cmd->resp = resp;
  74. schedule_work(&ddev->cmd_complete_work);
  75. }
  76. static void digital_wq_cmd(struct work_struct *work)
  77. {
  78. int rc;
  79. struct digital_cmd *cmd;
  80. struct nfc_digital_dev *ddev = container_of(work,
  81. struct nfc_digital_dev,
  82. cmd_work);
  83. mutex_lock(&ddev->cmd_lock);
  84. cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
  85. queue);
  86. if (!cmd || cmd->pending) {
  87. mutex_unlock(&ddev->cmd_lock);
  88. return;
  89. }
  90. mutex_unlock(&ddev->cmd_lock);
  91. if (cmd->req)
  92. print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
  93. cmd->req->data, cmd->req->len, false);
  94. switch (cmd->type) {
  95. case DIGITAL_CMD_IN_SEND:
  96. rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
  97. digital_send_cmd_complete, cmd);
  98. break;
  99. default:
  100. PR_ERR("Unknown cmd type %d", cmd->type);
  101. return;
  102. }
  103. if (!rc)
  104. return;
  105. PR_ERR("in_send_command returned err %d", rc);
  106. mutex_lock(&ddev->cmd_lock);
  107. list_del(&cmd->queue);
  108. mutex_unlock(&ddev->cmd_lock);
  109. kfree_skb(cmd->req);
  110. kfree(cmd);
  111. schedule_work(&ddev->cmd_work);
  112. }
  113. int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
  114. struct sk_buff *skb, u16 timeout,
  115. nfc_digital_cmd_complete_t cmd_cb, void *cb_context)
  116. {
  117. struct digital_cmd *cmd;
  118. cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL);
  119. if (!cmd)
  120. return -ENOMEM;
  121. cmd->type = cmd_type;
  122. cmd->timeout = timeout;
  123. cmd->req = skb;
  124. cmd->cmd_cb = cmd_cb;
  125. cmd->cb_context = cb_context;
  126. INIT_LIST_HEAD(&cmd->queue);
  127. mutex_lock(&ddev->cmd_lock);
  128. list_add_tail(&cmd->queue, &ddev->cmd_queue);
  129. mutex_unlock(&ddev->cmd_lock);
  130. schedule_work(&ddev->cmd_work);
  131. return 0;
  132. }
  133. int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
  134. {
  135. int rc;
  136. rc = ddev->ops->in_configure_hw(ddev, type, param);
  137. if (rc)
  138. PR_ERR("in_configure_hw failed: %d", rc);
  139. return rc;
  140. }
  141. void digital_poll_next_tech(struct nfc_digital_dev *ddev)
  142. {
  143. digital_switch_rf(ddev, 0);
  144. mutex_lock(&ddev->poll_lock);
  145. if (!ddev->poll_tech_count) {
  146. mutex_unlock(&ddev->poll_lock);
  147. return;
  148. }
  149. ddev->poll_tech_index = (ddev->poll_tech_index + 1) %
  150. ddev->poll_tech_count;
  151. mutex_unlock(&ddev->poll_lock);
  152. schedule_work(&ddev->poll_work);
  153. }
  154. static void digital_wq_poll(struct work_struct *work)
  155. {
  156. int rc;
  157. struct digital_poll_tech *poll_tech;
  158. struct nfc_digital_dev *ddev = container_of(work,
  159. struct nfc_digital_dev,
  160. poll_work);
  161. mutex_lock(&ddev->poll_lock);
  162. if (!ddev->poll_tech_count) {
  163. mutex_unlock(&ddev->poll_lock);
  164. return;
  165. }
  166. poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
  167. mutex_unlock(&ddev->poll_lock);
  168. rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
  169. if (rc)
  170. digital_poll_next_tech(ddev);
  171. }
  172. static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
  173. digital_poll_t poll_func)
  174. {
  175. struct digital_poll_tech *poll_tech;
  176. if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
  177. return;
  178. poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
  179. poll_tech->rf_tech = rf_tech;
  180. poll_tech->poll_func = poll_func;
  181. }
  182. /**
  183. * start_poll operation
  184. *
  185. * For every supported protocol, the corresponding polling function is added
  186. * to the table of polling technologies (ddev->poll_techs[]) using
  187. * digital_add_poll_tech().
  188. * When a polling function fails (by timeout or protocol error) the next one is
  189. * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
  190. */
  191. static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
  192. __u32 tm_protocols)
  193. {
  194. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  195. u32 matching_im_protocols, matching_tm_protocols;
  196. PR_DBG("protocols: im 0x%x, tm 0x%x, supported 0x%x", im_protocols,
  197. tm_protocols, ddev->protocols);
  198. matching_im_protocols = ddev->protocols & im_protocols;
  199. matching_tm_protocols = ddev->protocols & tm_protocols;
  200. if (!matching_im_protocols && !matching_tm_protocols) {
  201. PR_ERR("No known protocol");
  202. return -EINVAL;
  203. }
  204. if (ddev->poll_tech_count) {
  205. PR_ERR("Already polling");
  206. return -EBUSY;
  207. }
  208. if (ddev->curr_protocol) {
  209. PR_ERR("A target is already active");
  210. return -EBUSY;
  211. }
  212. ddev->poll_tech_count = 0;
  213. ddev->poll_tech_index = 0;
  214. if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
  215. digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
  216. digital_in_send_sens_req);
  217. if (!ddev->poll_tech_count) {
  218. PR_ERR("Unsupported protocols: im=0x%x, tm=0x%x",
  219. matching_im_protocols, matching_tm_protocols);
  220. return -EINVAL;
  221. }
  222. schedule_work(&ddev->poll_work);
  223. return 0;
  224. }
  225. static void digital_stop_poll(struct nfc_dev *nfc_dev)
  226. {
  227. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  228. mutex_lock(&ddev->poll_lock);
  229. if (!ddev->poll_tech_count) {
  230. PR_ERR("Polling operation was not running");
  231. mutex_unlock(&ddev->poll_lock);
  232. return;
  233. }
  234. ddev->poll_tech_count = 0;
  235. mutex_unlock(&ddev->poll_lock);
  236. cancel_work_sync(&ddev->poll_work);
  237. digital_abort_cmd(ddev);
  238. }
  239. static int digital_dev_up(struct nfc_dev *nfc_dev)
  240. {
  241. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  242. digital_switch_rf(ddev, 1);
  243. return 0;
  244. }
  245. static int digital_dev_down(struct nfc_dev *nfc_dev)
  246. {
  247. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  248. digital_switch_rf(ddev, 0);
  249. return 0;
  250. }
  251. static int digital_dep_link_up(struct nfc_dev *nfc_dev,
  252. struct nfc_target *target,
  253. __u8 comm_mode, __u8 *gb, size_t gb_len)
  254. {
  255. return -EOPNOTSUPP;
  256. }
  257. static int digital_dep_link_down(struct nfc_dev *nfc_dev)
  258. {
  259. return -EOPNOTSUPP;
  260. }
  261. static int digital_activate_target(struct nfc_dev *nfc_dev,
  262. struct nfc_target *target, __u32 protocol)
  263. {
  264. return 0;
  265. }
  266. static void digital_deactivate_target(struct nfc_dev *nfc_dev,
  267. struct nfc_target *target)
  268. {
  269. struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
  270. ddev->curr_protocol = 0;
  271. }
  272. static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
  273. {
  274. return -EOPNOTSUPP;
  275. }
  276. static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
  277. struct sk_buff *skb, data_exchange_cb_t cb,
  278. void *cb_context)
  279. {
  280. return -EOPNOTSUPP;
  281. }
  282. static struct nfc_ops digital_nfc_ops = {
  283. .dev_up = digital_dev_up,
  284. .dev_down = digital_dev_down,
  285. .start_poll = digital_start_poll,
  286. .stop_poll = digital_stop_poll,
  287. .dep_link_up = digital_dep_link_up,
  288. .dep_link_down = digital_dep_link_down,
  289. .activate_target = digital_activate_target,
  290. .deactivate_target = digital_deactivate_target,
  291. .tm_send = digital_tg_send,
  292. .im_transceive = digital_in_send,
  293. };
  294. struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
  295. __u32 supported_protocols,
  296. __u32 driver_capabilities,
  297. int tx_headroom, int tx_tailroom)
  298. {
  299. struct nfc_digital_dev *ddev;
  300. if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
  301. !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
  302. !ops->switch_rf)
  303. return NULL;
  304. ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL);
  305. if (!ddev) {
  306. PR_ERR("kzalloc failed");
  307. return NULL;
  308. }
  309. ddev->driver_capabilities = driver_capabilities;
  310. ddev->ops = ops;
  311. mutex_init(&ddev->cmd_lock);
  312. INIT_LIST_HEAD(&ddev->cmd_queue);
  313. INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
  314. INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
  315. mutex_init(&ddev->poll_lock);
  316. INIT_WORK(&ddev->poll_work, digital_wq_poll);
  317. if (supported_protocols & NFC_PROTO_JEWEL_MASK)
  318. ddev->protocols |= NFC_PROTO_JEWEL_MASK;
  319. if (supported_protocols & NFC_PROTO_MIFARE_MASK)
  320. ddev->protocols |= NFC_PROTO_MIFARE_MASK;
  321. ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
  322. ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
  323. ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
  324. ddev->tx_headroom,
  325. ddev->tx_tailroom);
  326. if (!ddev->nfc_dev) {
  327. PR_ERR("nfc_allocate_device failed");
  328. goto free_dev;
  329. }
  330. nfc_set_drvdata(ddev->nfc_dev, ddev);
  331. return ddev;
  332. free_dev:
  333. kfree(ddev);
  334. return NULL;
  335. }
  336. EXPORT_SYMBOL(nfc_digital_allocate_device);
  337. void nfc_digital_free_device(struct nfc_digital_dev *ddev)
  338. {
  339. nfc_free_device(ddev->nfc_dev);
  340. kfree(ddev);
  341. }
  342. EXPORT_SYMBOL(nfc_digital_free_device);
  343. int nfc_digital_register_device(struct nfc_digital_dev *ddev)
  344. {
  345. return nfc_register_device(ddev->nfc_dev);
  346. }
  347. EXPORT_SYMBOL(nfc_digital_register_device);
  348. void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
  349. {
  350. struct digital_cmd *cmd, *n;
  351. nfc_unregister_device(ddev->nfc_dev);
  352. mutex_lock(&ddev->poll_lock);
  353. ddev->poll_tech_count = 0;
  354. mutex_unlock(&ddev->poll_lock);
  355. cancel_work_sync(&ddev->poll_work);
  356. cancel_work_sync(&ddev->cmd_work);
  357. cancel_work_sync(&ddev->cmd_complete_work);
  358. list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
  359. list_del(&cmd->queue);
  360. kfree(cmd);
  361. }
  362. }
  363. EXPORT_SYMBOL(nfc_digital_unregister_device);
  364. MODULE_LICENSE("GPL");