core.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /*
  2. * The NFC Controller Interface is the communication protocol between an
  3. * NFC Controller (NFCC) and a Device Host (DH).
  4. *
  5. * Copyright (C) 2011 Texas Instruments, Inc.
  6. * Copyright (C) 2014 Marvell International Ltd.
  7. *
  8. * Written by Ilan Elias <ilane@ti.com>
  9. *
  10. * Acknowledgements:
  11. * This file is based on hci_core.c, which was written
  12. * by Maxim Krasnyansky.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2
  16. * as published by the Free Software Foundation
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  28. #include <linux/module.h>
  29. #include <linux/types.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/completion.h>
  32. #include <linux/export.h>
  33. #include <linux/sched.h>
  34. #include <linux/bitops.h>
  35. #include <linux/skbuff.h>
  36. #include "../nfc.h"
  37. #include <net/nfc/nci.h>
  38. #include <net/nfc/nci_core.h>
  39. #include <linux/nfc.h>
  40. struct core_conn_create_data {
  41. int length;
  42. struct nci_core_conn_create_cmd *cmd;
  43. };
  44. static void nci_cmd_work(struct work_struct *work);
  45. static void nci_rx_work(struct work_struct *work);
  46. static void nci_tx_work(struct work_struct *work);
  47. struct nci_conn_info *nci_get_conn_info_by_conn_id(struct nci_dev *ndev,
  48. int conn_id)
  49. {
  50. struct nci_conn_info *conn_info;
  51. list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
  52. if (conn_info->conn_id == conn_id)
  53. return conn_info;
  54. }
  55. return NULL;
  56. }
  57. /* ---- NCI requests ---- */
  58. void nci_req_complete(struct nci_dev *ndev, int result)
  59. {
  60. if (ndev->req_status == NCI_REQ_PEND) {
  61. ndev->req_result = result;
  62. ndev->req_status = NCI_REQ_DONE;
  63. complete(&ndev->req_completion);
  64. }
  65. }
  66. static void nci_req_cancel(struct nci_dev *ndev, int err)
  67. {
  68. if (ndev->req_status == NCI_REQ_PEND) {
  69. ndev->req_result = err;
  70. ndev->req_status = NCI_REQ_CANCELED;
  71. complete(&ndev->req_completion);
  72. }
  73. }
  74. /* Execute request and wait for completion. */
  75. static int __nci_request(struct nci_dev *ndev,
  76. void (*req)(struct nci_dev *ndev, unsigned long opt),
  77. unsigned long opt, __u32 timeout)
  78. {
  79. int rc = 0;
  80. long completion_rc;
  81. ndev->req_status = NCI_REQ_PEND;
  82. reinit_completion(&ndev->req_completion);
  83. req(ndev, opt);
  84. completion_rc =
  85. wait_for_completion_interruptible_timeout(&ndev->req_completion,
  86. timeout);
  87. pr_debug("wait_for_completion return %ld\n", completion_rc);
  88. if (completion_rc > 0) {
  89. switch (ndev->req_status) {
  90. case NCI_REQ_DONE:
  91. rc = nci_to_errno(ndev->req_result);
  92. break;
  93. case NCI_REQ_CANCELED:
  94. rc = -ndev->req_result;
  95. break;
  96. default:
  97. rc = -ETIMEDOUT;
  98. break;
  99. }
  100. } else {
  101. pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
  102. completion_rc);
  103. rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
  104. }
  105. ndev->req_status = ndev->req_result = 0;
  106. return rc;
  107. }
  108. inline int nci_request(struct nci_dev *ndev,
  109. void (*req)(struct nci_dev *ndev,
  110. unsigned long opt),
  111. unsigned long opt, __u32 timeout)
  112. {
  113. int rc;
  114. if (!test_bit(NCI_UP, &ndev->flags))
  115. return -ENETDOWN;
  116. /* Serialize all requests */
  117. mutex_lock(&ndev->req_lock);
  118. rc = __nci_request(ndev, req, opt, timeout);
  119. mutex_unlock(&ndev->req_lock);
  120. return rc;
  121. }
  122. static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
  123. {
  124. struct nci_core_reset_cmd cmd;
  125. cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
  126. nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
  127. }
  128. static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
  129. {
  130. nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
  131. }
  132. static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
  133. {
  134. struct nci_rf_disc_map_cmd cmd;
  135. struct disc_map_config *cfg = cmd.mapping_configs;
  136. __u8 *num = &cmd.num_mapping_configs;
  137. int i;
  138. /* set rf mapping configurations */
  139. *num = 0;
  140. /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
  141. for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
  142. if (ndev->supported_rf_interfaces[i] ==
  143. NCI_RF_INTERFACE_ISO_DEP) {
  144. cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
  145. cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
  146. NCI_DISC_MAP_MODE_LISTEN;
  147. cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
  148. (*num)++;
  149. } else if (ndev->supported_rf_interfaces[i] ==
  150. NCI_RF_INTERFACE_NFC_DEP) {
  151. cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
  152. cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
  153. NCI_DISC_MAP_MODE_LISTEN;
  154. cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
  155. (*num)++;
  156. }
  157. if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
  158. break;
  159. }
  160. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
  161. (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
  162. }
  163. struct nci_set_config_param {
  164. __u8 id;
  165. size_t len;
  166. __u8 *val;
  167. };
  168. static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
  169. {
  170. struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
  171. struct nci_core_set_config_cmd cmd;
  172. BUG_ON(param->len > NCI_MAX_PARAM_LEN);
  173. cmd.num_params = 1;
  174. cmd.param.id = param->id;
  175. cmd.param.len = param->len;
  176. memcpy(cmd.param.val, param->val, param->len);
  177. nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
  178. }
  179. struct nci_rf_discover_param {
  180. __u32 im_protocols;
  181. __u32 tm_protocols;
  182. };
  183. static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
  184. {
  185. struct nci_rf_discover_param *param =
  186. (struct nci_rf_discover_param *)opt;
  187. struct nci_rf_disc_cmd cmd;
  188. cmd.num_disc_configs = 0;
  189. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  190. (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
  191. param->im_protocols & NFC_PROTO_MIFARE_MASK ||
  192. param->im_protocols & NFC_PROTO_ISO14443_MASK ||
  193. param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
  194. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  195. NCI_NFC_A_PASSIVE_POLL_MODE;
  196. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  197. cmd.num_disc_configs++;
  198. }
  199. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  200. (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
  201. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  202. NCI_NFC_B_PASSIVE_POLL_MODE;
  203. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  204. cmd.num_disc_configs++;
  205. }
  206. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  207. (param->im_protocols & NFC_PROTO_FELICA_MASK ||
  208. param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
  209. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  210. NCI_NFC_F_PASSIVE_POLL_MODE;
  211. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  212. cmd.num_disc_configs++;
  213. }
  214. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  215. (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
  216. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  217. NCI_NFC_V_PASSIVE_POLL_MODE;
  218. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  219. cmd.num_disc_configs++;
  220. }
  221. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
  222. (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
  223. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  224. NCI_NFC_A_PASSIVE_LISTEN_MODE;
  225. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  226. cmd.num_disc_configs++;
  227. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  228. NCI_NFC_F_PASSIVE_LISTEN_MODE;
  229. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  230. cmd.num_disc_configs++;
  231. }
  232. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
  233. (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
  234. &cmd);
  235. }
  236. struct nci_rf_discover_select_param {
  237. __u8 rf_discovery_id;
  238. __u8 rf_protocol;
  239. };
  240. static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
  241. {
  242. struct nci_rf_discover_select_param *param =
  243. (struct nci_rf_discover_select_param *)opt;
  244. struct nci_rf_discover_select_cmd cmd;
  245. cmd.rf_discovery_id = param->rf_discovery_id;
  246. cmd.rf_protocol = param->rf_protocol;
  247. switch (cmd.rf_protocol) {
  248. case NCI_RF_PROTOCOL_ISO_DEP:
  249. cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
  250. break;
  251. case NCI_RF_PROTOCOL_NFC_DEP:
  252. cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
  253. break;
  254. default:
  255. cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
  256. break;
  257. }
  258. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
  259. sizeof(struct nci_rf_discover_select_cmd), &cmd);
  260. }
  261. static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
  262. {
  263. struct nci_rf_deactivate_cmd cmd;
  264. cmd.type = opt;
  265. nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
  266. sizeof(struct nci_rf_deactivate_cmd), &cmd);
  267. }
  268. static int nci_open_device(struct nci_dev *ndev)
  269. {
  270. int rc = 0;
  271. mutex_lock(&ndev->req_lock);
  272. if (test_bit(NCI_UP, &ndev->flags)) {
  273. rc = -EALREADY;
  274. goto done;
  275. }
  276. if (ndev->ops->open(ndev)) {
  277. rc = -EIO;
  278. goto done;
  279. }
  280. atomic_set(&ndev->cmd_cnt, 1);
  281. set_bit(NCI_INIT, &ndev->flags);
  282. rc = __nci_request(ndev, nci_reset_req, 0,
  283. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  284. if (ndev->ops->setup)
  285. ndev->ops->setup(ndev);
  286. if (!rc) {
  287. rc = __nci_request(ndev, nci_init_req, 0,
  288. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  289. }
  290. if (!rc) {
  291. rc = __nci_request(ndev, nci_init_complete_req, 0,
  292. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  293. }
  294. clear_bit(NCI_INIT, &ndev->flags);
  295. if (!rc) {
  296. set_bit(NCI_UP, &ndev->flags);
  297. nci_clear_target_list(ndev);
  298. atomic_set(&ndev->state, NCI_IDLE);
  299. } else {
  300. /* Init failed, cleanup */
  301. skb_queue_purge(&ndev->cmd_q);
  302. skb_queue_purge(&ndev->rx_q);
  303. skb_queue_purge(&ndev->tx_q);
  304. ndev->ops->close(ndev);
  305. ndev->flags = 0;
  306. }
  307. done:
  308. mutex_unlock(&ndev->req_lock);
  309. return rc;
  310. }
  311. static int nci_close_device(struct nci_dev *ndev)
  312. {
  313. nci_req_cancel(ndev, ENODEV);
  314. mutex_lock(&ndev->req_lock);
  315. if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
  316. del_timer_sync(&ndev->cmd_timer);
  317. del_timer_sync(&ndev->data_timer);
  318. mutex_unlock(&ndev->req_lock);
  319. return 0;
  320. }
  321. /* Drop RX and TX queues */
  322. skb_queue_purge(&ndev->rx_q);
  323. skb_queue_purge(&ndev->tx_q);
  324. /* Flush RX and TX wq */
  325. flush_workqueue(ndev->rx_wq);
  326. flush_workqueue(ndev->tx_wq);
  327. /* Reset device */
  328. skb_queue_purge(&ndev->cmd_q);
  329. atomic_set(&ndev->cmd_cnt, 1);
  330. set_bit(NCI_INIT, &ndev->flags);
  331. __nci_request(ndev, nci_reset_req, 0,
  332. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  333. clear_bit(NCI_INIT, &ndev->flags);
  334. del_timer_sync(&ndev->cmd_timer);
  335. /* Flush cmd wq */
  336. flush_workqueue(ndev->cmd_wq);
  337. /* After this point our queues are empty
  338. * and no works are scheduled. */
  339. ndev->ops->close(ndev);
  340. /* Clear flags */
  341. ndev->flags = 0;
  342. mutex_unlock(&ndev->req_lock);
  343. return 0;
  344. }
  345. /* NCI command timer function */
  346. static void nci_cmd_timer(unsigned long arg)
  347. {
  348. struct nci_dev *ndev = (void *) arg;
  349. atomic_set(&ndev->cmd_cnt, 1);
  350. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  351. }
  352. /* NCI data exchange timer function */
  353. static void nci_data_timer(unsigned long arg)
  354. {
  355. struct nci_dev *ndev = (void *) arg;
  356. set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  357. queue_work(ndev->rx_wq, &ndev->rx_work);
  358. }
  359. static int nci_dev_up(struct nfc_dev *nfc_dev)
  360. {
  361. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  362. return nci_open_device(ndev);
  363. }
  364. static int nci_dev_down(struct nfc_dev *nfc_dev)
  365. {
  366. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  367. return nci_close_device(ndev);
  368. }
  369. int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
  370. {
  371. struct nci_set_config_param param;
  372. if (!val || !len)
  373. return 0;
  374. param.id = id;
  375. param.len = len;
  376. param.val = val;
  377. return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
  378. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  379. }
  380. EXPORT_SYMBOL(nci_set_config);
  381. static void nci_nfcee_discover_req(struct nci_dev *ndev, unsigned long opt)
  382. {
  383. struct nci_nfcee_discover_cmd cmd;
  384. __u8 action = opt;
  385. cmd.discovery_action = action;
  386. nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
  387. }
  388. int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
  389. {
  390. return nci_request(ndev, nci_nfcee_discover_req, action,
  391. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  392. }
  393. EXPORT_SYMBOL(nci_nfcee_discover);
  394. static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt)
  395. {
  396. struct nci_nfcee_mode_set_cmd *cmd =
  397. (struct nci_nfcee_mode_set_cmd *)opt;
  398. nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
  399. sizeof(struct nci_nfcee_mode_set_cmd), cmd);
  400. }
  401. int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
  402. {
  403. struct nci_nfcee_mode_set_cmd cmd;
  404. cmd.nfcee_id = nfcee_id;
  405. cmd.nfcee_mode = nfcee_mode;
  406. return nci_request(ndev, nci_nfcee_mode_set_req, (unsigned long)&cmd,
  407. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  408. }
  409. EXPORT_SYMBOL(nci_nfcee_mode_set);
  410. static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt)
  411. {
  412. struct core_conn_create_data *data =
  413. (struct core_conn_create_data *)opt;
  414. nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
  415. }
  416. int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
  417. u8 number_destination_params,
  418. size_t params_len,
  419. struct core_conn_create_dest_spec_params *params)
  420. {
  421. int r;
  422. struct nci_core_conn_create_cmd *cmd;
  423. struct core_conn_create_data data;
  424. data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
  425. cmd = kzalloc(data.length, GFP_KERNEL);
  426. if (!cmd)
  427. return -ENOMEM;
  428. cmd->destination_type = destination_type;
  429. cmd->number_destination_params = number_destination_params;
  430. memcpy(cmd->params, params, params_len);
  431. data.cmd = cmd;
  432. ndev->cur_id = params->value[DEST_SPEC_PARAMS_ID_INDEX];
  433. r = __nci_request(ndev, nci_core_conn_create_req,
  434. (unsigned long)&data,
  435. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  436. kfree(cmd);
  437. return r;
  438. }
  439. EXPORT_SYMBOL(nci_core_conn_create);
  440. static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
  441. {
  442. __u8 conn_id = opt;
  443. nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
  444. }
  445. int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
  446. {
  447. return nci_request(ndev, nci_core_conn_close_req, conn_id,
  448. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  449. }
  450. EXPORT_SYMBOL(nci_core_conn_close);
  451. static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
  452. {
  453. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  454. struct nci_set_config_param param;
  455. int rc;
  456. param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
  457. if ((param.val == NULL) || (param.len == 0))
  458. return 0;
  459. if (param.len > NFC_MAX_GT_LEN)
  460. return -EINVAL;
  461. param.id = NCI_PN_ATR_REQ_GEN_BYTES;
  462. rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
  463. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  464. if (rc)
  465. return rc;
  466. param.id = NCI_LN_ATR_RES_GEN_BYTES;
  467. return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
  468. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  469. }
  470. static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
  471. {
  472. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  473. int rc;
  474. __u8 val;
  475. val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
  476. rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
  477. if (rc)
  478. return rc;
  479. val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
  480. rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
  481. if (rc)
  482. return rc;
  483. val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
  484. return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
  485. }
  486. static int nci_start_poll(struct nfc_dev *nfc_dev,
  487. __u32 im_protocols, __u32 tm_protocols)
  488. {
  489. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  490. struct nci_rf_discover_param param;
  491. int rc;
  492. if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
  493. (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
  494. pr_err("unable to start poll, since poll is already active\n");
  495. return -EBUSY;
  496. }
  497. if (ndev->target_active_prot) {
  498. pr_err("there is an active target\n");
  499. return -EBUSY;
  500. }
  501. if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
  502. (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
  503. pr_debug("target active or w4 select, implicitly deactivate\n");
  504. rc = nci_request(ndev, nci_rf_deactivate_req,
  505. NCI_DEACTIVATE_TYPE_IDLE_MODE,
  506. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  507. if (rc)
  508. return -EBUSY;
  509. }
  510. if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
  511. rc = nci_set_local_general_bytes(nfc_dev);
  512. if (rc) {
  513. pr_err("failed to set local general bytes\n");
  514. return rc;
  515. }
  516. }
  517. if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
  518. rc = nci_set_listen_parameters(nfc_dev);
  519. if (rc)
  520. pr_err("failed to set listen parameters\n");
  521. }
  522. param.im_protocols = im_protocols;
  523. param.tm_protocols = tm_protocols;
  524. rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
  525. msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
  526. if (!rc)
  527. ndev->poll_prots = im_protocols;
  528. return rc;
  529. }
  530. static void nci_stop_poll(struct nfc_dev *nfc_dev)
  531. {
  532. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  533. if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
  534. (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
  535. pr_err("unable to stop poll, since poll is not active\n");
  536. return;
  537. }
  538. nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
  539. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  540. }
  541. static int nci_activate_target(struct nfc_dev *nfc_dev,
  542. struct nfc_target *target, __u32 protocol)
  543. {
  544. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  545. struct nci_rf_discover_select_param param;
  546. struct nfc_target *nci_target = NULL;
  547. int i;
  548. int rc = 0;
  549. pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
  550. if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
  551. (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
  552. pr_err("there is no available target to activate\n");
  553. return -EINVAL;
  554. }
  555. if (ndev->target_active_prot) {
  556. pr_err("there is already an active target\n");
  557. return -EBUSY;
  558. }
  559. for (i = 0; i < ndev->n_targets; i++) {
  560. if (ndev->targets[i].idx == target->idx) {
  561. nci_target = &ndev->targets[i];
  562. break;
  563. }
  564. }
  565. if (!nci_target) {
  566. pr_err("unable to find the selected target\n");
  567. return -EINVAL;
  568. }
  569. if (!(nci_target->supported_protocols & (1 << protocol))) {
  570. pr_err("target does not support the requested protocol 0x%x\n",
  571. protocol);
  572. return -EINVAL;
  573. }
  574. if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
  575. param.rf_discovery_id = nci_target->logical_idx;
  576. if (protocol == NFC_PROTO_JEWEL)
  577. param.rf_protocol = NCI_RF_PROTOCOL_T1T;
  578. else if (protocol == NFC_PROTO_MIFARE)
  579. param.rf_protocol = NCI_RF_PROTOCOL_T2T;
  580. else if (protocol == NFC_PROTO_FELICA)
  581. param.rf_protocol = NCI_RF_PROTOCOL_T3T;
  582. else if (protocol == NFC_PROTO_ISO14443 ||
  583. protocol == NFC_PROTO_ISO14443_B)
  584. param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
  585. else
  586. param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
  587. rc = nci_request(ndev, nci_rf_discover_select_req,
  588. (unsigned long)&param,
  589. msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
  590. }
  591. if (!rc)
  592. ndev->target_active_prot = protocol;
  593. return rc;
  594. }
  595. static void nci_deactivate_target(struct nfc_dev *nfc_dev,
  596. struct nfc_target *target)
  597. {
  598. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  599. pr_debug("entry\n");
  600. if (!ndev->target_active_prot) {
  601. pr_err("unable to deactivate target, no active target\n");
  602. return;
  603. }
  604. ndev->target_active_prot = 0;
  605. if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
  606. nci_request(ndev, nci_rf_deactivate_req,
  607. NCI_DEACTIVATE_TYPE_SLEEP_MODE,
  608. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  609. }
  610. }
  611. static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
  612. __u8 comm_mode, __u8 *gb, size_t gb_len)
  613. {
  614. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  615. int rc;
  616. pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
  617. rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
  618. if (rc)
  619. return rc;
  620. rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
  621. ndev->remote_gb_len);
  622. if (!rc)
  623. rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
  624. NFC_RF_INITIATOR);
  625. return rc;
  626. }
  627. static int nci_dep_link_down(struct nfc_dev *nfc_dev)
  628. {
  629. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  630. int rc;
  631. pr_debug("entry\n");
  632. if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
  633. nci_deactivate_target(nfc_dev, NULL);
  634. } else {
  635. if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
  636. atomic_read(&ndev->state) == NCI_DISCOVERY) {
  637. nci_request(ndev, nci_rf_deactivate_req, 0,
  638. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  639. }
  640. rc = nfc_tm_deactivated(nfc_dev);
  641. if (rc)
  642. pr_err("error when signaling tm deactivation\n");
  643. }
  644. return 0;
  645. }
  646. static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
  647. struct sk_buff *skb,
  648. data_exchange_cb_t cb, void *cb_context)
  649. {
  650. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  651. int rc;
  652. struct nci_conn_info *conn_info;
  653. conn_info = ndev->rf_conn_info;
  654. if (!conn_info)
  655. return -EPROTO;
  656. pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
  657. if (!ndev->target_active_prot) {
  658. pr_err("unable to exchange data, no active target\n");
  659. return -EINVAL;
  660. }
  661. if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  662. return -EBUSY;
  663. /* store cb and context to be used on receiving data */
  664. conn_info->data_exchange_cb = cb;
  665. conn_info->data_exchange_cb_context = cb_context;
  666. rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
  667. if (rc)
  668. clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
  669. return rc;
  670. }
  671. static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
  672. {
  673. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  674. int rc;
  675. rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
  676. if (rc)
  677. pr_err("unable to send data\n");
  678. return rc;
  679. }
  680. static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  681. {
  682. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  683. if (ndev->ops->enable_se)
  684. return ndev->ops->enable_se(ndev, se_idx);
  685. return 0;
  686. }
  687. static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  688. {
  689. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  690. if (ndev->ops->disable_se)
  691. return ndev->ops->disable_se(ndev, se_idx);
  692. return 0;
  693. }
  694. static int nci_discover_se(struct nfc_dev *nfc_dev)
  695. {
  696. int r;
  697. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  698. if (ndev->ops->discover_se) {
  699. r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
  700. if (r != NCI_STATUS_OK)
  701. return -EPROTO;
  702. return ndev->ops->discover_se(ndev);
  703. }
  704. return 0;
  705. }
  706. static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
  707. u8 *apdu, size_t apdu_length,
  708. se_io_cb_t cb, void *cb_context)
  709. {
  710. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  711. if (ndev->ops->se_io)
  712. return ndev->ops->se_io(ndev, se_idx, apdu,
  713. apdu_length, cb, cb_context);
  714. return 0;
  715. }
  716. static struct nfc_ops nci_nfc_ops = {
  717. .dev_up = nci_dev_up,
  718. .dev_down = nci_dev_down,
  719. .start_poll = nci_start_poll,
  720. .stop_poll = nci_stop_poll,
  721. .dep_link_up = nci_dep_link_up,
  722. .dep_link_down = nci_dep_link_down,
  723. .activate_target = nci_activate_target,
  724. .deactivate_target = nci_deactivate_target,
  725. .im_transceive = nci_transceive,
  726. .tm_send = nci_tm_send,
  727. .enable_se = nci_enable_se,
  728. .disable_se = nci_disable_se,
  729. .discover_se = nci_discover_se,
  730. .se_io = nci_se_io,
  731. };
  732. /* ---- Interface to NCI drivers ---- */
  733. /**
  734. * nci_allocate_device - allocate a new nci device
  735. *
  736. * @ops: device operations
  737. * @supported_protocols: NFC protocols supported by the device
  738. */
  739. struct nci_dev *nci_allocate_device(struct nci_ops *ops,
  740. __u32 supported_protocols,
  741. int tx_headroom, int tx_tailroom)
  742. {
  743. struct nci_dev *ndev;
  744. pr_debug("supported_protocols 0x%x\n", supported_protocols);
  745. if (!ops->open || !ops->close || !ops->send)
  746. return NULL;
  747. if (!supported_protocols)
  748. return NULL;
  749. ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
  750. if (!ndev)
  751. return NULL;
  752. ndev->ops = ops;
  753. ndev->tx_headroom = tx_headroom;
  754. ndev->tx_tailroom = tx_tailroom;
  755. init_completion(&ndev->req_completion);
  756. ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
  757. supported_protocols,
  758. tx_headroom + NCI_DATA_HDR_SIZE,
  759. tx_tailroom);
  760. if (!ndev->nfc_dev)
  761. goto free_nci;
  762. ndev->hci_dev = nci_hci_allocate(ndev);
  763. if (!ndev->hci_dev)
  764. goto free_nfc;
  765. nfc_set_drvdata(ndev->nfc_dev, ndev);
  766. return ndev;
  767. free_nfc:
  768. kfree(ndev->nfc_dev);
  769. free_nci:
  770. kfree(ndev);
  771. return NULL;
  772. }
  773. EXPORT_SYMBOL(nci_allocate_device);
  774. /**
  775. * nci_free_device - deallocate nci device
  776. *
  777. * @ndev: The nci device to deallocate
  778. */
  779. void nci_free_device(struct nci_dev *ndev)
  780. {
  781. nfc_free_device(ndev->nfc_dev);
  782. kfree(ndev);
  783. }
  784. EXPORT_SYMBOL(nci_free_device);
  785. /**
  786. * nci_register_device - register a nci device in the nfc subsystem
  787. *
  788. * @dev: The nci device to register
  789. */
  790. int nci_register_device(struct nci_dev *ndev)
  791. {
  792. int rc;
  793. struct device *dev = &ndev->nfc_dev->dev;
  794. char name[32];
  795. ndev->flags = 0;
  796. INIT_WORK(&ndev->cmd_work, nci_cmd_work);
  797. snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
  798. ndev->cmd_wq = create_singlethread_workqueue(name);
  799. if (!ndev->cmd_wq) {
  800. rc = -ENOMEM;
  801. goto exit;
  802. }
  803. INIT_WORK(&ndev->rx_work, nci_rx_work);
  804. snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
  805. ndev->rx_wq = create_singlethread_workqueue(name);
  806. if (!ndev->rx_wq) {
  807. rc = -ENOMEM;
  808. goto destroy_cmd_wq_exit;
  809. }
  810. INIT_WORK(&ndev->tx_work, nci_tx_work);
  811. snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
  812. ndev->tx_wq = create_singlethread_workqueue(name);
  813. if (!ndev->tx_wq) {
  814. rc = -ENOMEM;
  815. goto destroy_rx_wq_exit;
  816. }
  817. skb_queue_head_init(&ndev->cmd_q);
  818. skb_queue_head_init(&ndev->rx_q);
  819. skb_queue_head_init(&ndev->tx_q);
  820. setup_timer(&ndev->cmd_timer, nci_cmd_timer,
  821. (unsigned long) ndev);
  822. setup_timer(&ndev->data_timer, nci_data_timer,
  823. (unsigned long) ndev);
  824. mutex_init(&ndev->req_lock);
  825. INIT_LIST_HEAD(&ndev->conn_info_list);
  826. rc = nfc_register_device(ndev->nfc_dev);
  827. if (rc)
  828. goto destroy_rx_wq_exit;
  829. goto exit;
  830. destroy_rx_wq_exit:
  831. destroy_workqueue(ndev->rx_wq);
  832. destroy_cmd_wq_exit:
  833. destroy_workqueue(ndev->cmd_wq);
  834. exit:
  835. return rc;
  836. }
  837. EXPORT_SYMBOL(nci_register_device);
  838. /**
  839. * nci_unregister_device - unregister a nci device in the nfc subsystem
  840. *
  841. * @dev: The nci device to unregister
  842. */
  843. void nci_unregister_device(struct nci_dev *ndev)
  844. {
  845. struct nci_conn_info *conn_info, *n;
  846. nci_close_device(ndev);
  847. destroy_workqueue(ndev->cmd_wq);
  848. destroy_workqueue(ndev->rx_wq);
  849. destroy_workqueue(ndev->tx_wq);
  850. list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
  851. list_del(&conn_info->list);
  852. /* conn_info is allocated with devm_kzalloc */
  853. }
  854. nfc_unregister_device(ndev->nfc_dev);
  855. }
  856. EXPORT_SYMBOL(nci_unregister_device);
  857. /**
  858. * nci_recv_frame - receive frame from NCI drivers
  859. *
  860. * @ndev: The nci device
  861. * @skb: The sk_buff to receive
  862. */
  863. int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
  864. {
  865. pr_debug("len %d\n", skb->len);
  866. if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
  867. !test_bit(NCI_INIT, &ndev->flags))) {
  868. kfree_skb(skb);
  869. return -ENXIO;
  870. }
  871. /* Queue frame for rx worker thread */
  872. skb_queue_tail(&ndev->rx_q, skb);
  873. queue_work(ndev->rx_wq, &ndev->rx_work);
  874. return 0;
  875. }
  876. EXPORT_SYMBOL(nci_recv_frame);
  877. static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
  878. {
  879. pr_debug("len %d\n", skb->len);
  880. if (!ndev) {
  881. kfree_skb(skb);
  882. return -ENODEV;
  883. }
  884. /* Get rid of skb owner, prior to sending to the driver. */
  885. skb_orphan(skb);
  886. /* Send copy to sniffer */
  887. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  888. RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
  889. return ndev->ops->send(ndev, skb);
  890. }
  891. /* Send NCI command */
  892. int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
  893. {
  894. struct nci_ctrl_hdr *hdr;
  895. struct sk_buff *skb;
  896. pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
  897. skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
  898. if (!skb) {
  899. pr_err("no memory for command\n");
  900. return -ENOMEM;
  901. }
  902. hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
  903. hdr->gid = nci_opcode_gid(opcode);
  904. hdr->oid = nci_opcode_oid(opcode);
  905. hdr->plen = plen;
  906. nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
  907. nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
  908. if (plen)
  909. memcpy(skb_put(skb, plen), payload, plen);
  910. skb_queue_tail(&ndev->cmd_q, skb);
  911. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  912. return 0;
  913. }
  914. /* ---- NCI TX Data worker thread ---- */
  915. static void nci_tx_work(struct work_struct *work)
  916. {
  917. struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
  918. struct nci_conn_info *conn_info;
  919. struct sk_buff *skb;
  920. conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
  921. if (!conn_info)
  922. return;
  923. pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
  924. /* Send queued tx data */
  925. while (atomic_read(&conn_info->credits_cnt)) {
  926. skb = skb_dequeue(&ndev->tx_q);
  927. if (!skb)
  928. return;
  929. /* Check if data flow control is used */
  930. if (atomic_read(&conn_info->credits_cnt) !=
  931. NCI_DATA_FLOW_CONTROL_NOT_USED)
  932. atomic_dec(&conn_info->credits_cnt);
  933. pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
  934. nci_pbf(skb->data),
  935. nci_conn_id(skb->data),
  936. nci_plen(skb->data));
  937. nci_send_frame(ndev, skb);
  938. mod_timer(&ndev->data_timer,
  939. jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
  940. }
  941. }
  942. /* ----- NCI RX worker thread (data & control) ----- */
  943. static void nci_rx_work(struct work_struct *work)
  944. {
  945. struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
  946. struct sk_buff *skb;
  947. while ((skb = skb_dequeue(&ndev->rx_q))) {
  948. /* Send copy to sniffer */
  949. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  950. RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
  951. /* Process frame */
  952. switch (nci_mt(skb->data)) {
  953. case NCI_MT_RSP_PKT:
  954. nci_rsp_packet(ndev, skb);
  955. break;
  956. case NCI_MT_NTF_PKT:
  957. nci_ntf_packet(ndev, skb);
  958. break;
  959. case NCI_MT_DATA_PKT:
  960. nci_rx_data_packet(ndev, skb);
  961. break;
  962. default:
  963. pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
  964. kfree_skb(skb);
  965. break;
  966. }
  967. }
  968. /* check if a data exchange timout has occurred */
  969. if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
  970. /* complete the data exchange transaction, if exists */
  971. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  972. nci_data_exchange_complete(ndev, NULL,
  973. ndev->cur_conn_id,
  974. -ETIMEDOUT);
  975. clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  976. }
  977. }
  978. /* ----- NCI TX CMD worker thread ----- */
  979. static void nci_cmd_work(struct work_struct *work)
  980. {
  981. struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
  982. struct sk_buff *skb;
  983. pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
  984. /* Send queued command */
  985. if (atomic_read(&ndev->cmd_cnt)) {
  986. skb = skb_dequeue(&ndev->cmd_q);
  987. if (!skb)
  988. return;
  989. atomic_dec(&ndev->cmd_cnt);
  990. pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
  991. nci_pbf(skb->data),
  992. nci_opcode_gid(nci_opcode(skb->data)),
  993. nci_opcode_oid(nci_opcode(skb->data)),
  994. nci_plen(skb->data));
  995. nci_send_frame(ndev, skb);
  996. mod_timer(&ndev->cmd_timer,
  997. jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
  998. }
  999. }
  1000. MODULE_LICENSE("GPL");