core.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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 int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
  717. {
  718. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  719. if (!ndev->ops->fw_download)
  720. return -ENOTSUPP;
  721. return ndev->ops->fw_download(ndev, firmware_name);
  722. }
  723. static struct nfc_ops nci_nfc_ops = {
  724. .dev_up = nci_dev_up,
  725. .dev_down = nci_dev_down,
  726. .start_poll = nci_start_poll,
  727. .stop_poll = nci_stop_poll,
  728. .dep_link_up = nci_dep_link_up,
  729. .dep_link_down = nci_dep_link_down,
  730. .activate_target = nci_activate_target,
  731. .deactivate_target = nci_deactivate_target,
  732. .im_transceive = nci_transceive,
  733. .tm_send = nci_tm_send,
  734. .enable_se = nci_enable_se,
  735. .disable_se = nci_disable_se,
  736. .discover_se = nci_discover_se,
  737. .se_io = nci_se_io,
  738. .fw_download = nci_fw_download,
  739. };
  740. /* ---- Interface to NCI drivers ---- */
  741. /**
  742. * nci_allocate_device - allocate a new nci device
  743. *
  744. * @ops: device operations
  745. * @supported_protocols: NFC protocols supported by the device
  746. */
  747. struct nci_dev *nci_allocate_device(struct nci_ops *ops,
  748. __u32 supported_protocols,
  749. int tx_headroom, int tx_tailroom)
  750. {
  751. struct nci_dev *ndev;
  752. pr_debug("supported_protocols 0x%x\n", supported_protocols);
  753. if (!ops->open || !ops->close || !ops->send)
  754. return NULL;
  755. if (!supported_protocols)
  756. return NULL;
  757. ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
  758. if (!ndev)
  759. return NULL;
  760. ndev->ops = ops;
  761. ndev->tx_headroom = tx_headroom;
  762. ndev->tx_tailroom = tx_tailroom;
  763. init_completion(&ndev->req_completion);
  764. ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
  765. supported_protocols,
  766. tx_headroom + NCI_DATA_HDR_SIZE,
  767. tx_tailroom);
  768. if (!ndev->nfc_dev)
  769. goto free_nci;
  770. ndev->hci_dev = nci_hci_allocate(ndev);
  771. if (!ndev->hci_dev)
  772. goto free_nfc;
  773. nfc_set_drvdata(ndev->nfc_dev, ndev);
  774. return ndev;
  775. free_nfc:
  776. kfree(ndev->nfc_dev);
  777. free_nci:
  778. kfree(ndev);
  779. return NULL;
  780. }
  781. EXPORT_SYMBOL(nci_allocate_device);
  782. /**
  783. * nci_free_device - deallocate nci device
  784. *
  785. * @ndev: The nci device to deallocate
  786. */
  787. void nci_free_device(struct nci_dev *ndev)
  788. {
  789. nfc_free_device(ndev->nfc_dev);
  790. kfree(ndev);
  791. }
  792. EXPORT_SYMBOL(nci_free_device);
  793. /**
  794. * nci_register_device - register a nci device in the nfc subsystem
  795. *
  796. * @dev: The nci device to register
  797. */
  798. int nci_register_device(struct nci_dev *ndev)
  799. {
  800. int rc;
  801. struct device *dev = &ndev->nfc_dev->dev;
  802. char name[32];
  803. ndev->flags = 0;
  804. INIT_WORK(&ndev->cmd_work, nci_cmd_work);
  805. snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
  806. ndev->cmd_wq = create_singlethread_workqueue(name);
  807. if (!ndev->cmd_wq) {
  808. rc = -ENOMEM;
  809. goto exit;
  810. }
  811. INIT_WORK(&ndev->rx_work, nci_rx_work);
  812. snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
  813. ndev->rx_wq = create_singlethread_workqueue(name);
  814. if (!ndev->rx_wq) {
  815. rc = -ENOMEM;
  816. goto destroy_cmd_wq_exit;
  817. }
  818. INIT_WORK(&ndev->tx_work, nci_tx_work);
  819. snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
  820. ndev->tx_wq = create_singlethread_workqueue(name);
  821. if (!ndev->tx_wq) {
  822. rc = -ENOMEM;
  823. goto destroy_rx_wq_exit;
  824. }
  825. skb_queue_head_init(&ndev->cmd_q);
  826. skb_queue_head_init(&ndev->rx_q);
  827. skb_queue_head_init(&ndev->tx_q);
  828. setup_timer(&ndev->cmd_timer, nci_cmd_timer,
  829. (unsigned long) ndev);
  830. setup_timer(&ndev->data_timer, nci_data_timer,
  831. (unsigned long) ndev);
  832. mutex_init(&ndev->req_lock);
  833. INIT_LIST_HEAD(&ndev->conn_info_list);
  834. rc = nfc_register_device(ndev->nfc_dev);
  835. if (rc)
  836. goto destroy_rx_wq_exit;
  837. goto exit;
  838. destroy_rx_wq_exit:
  839. destroy_workqueue(ndev->rx_wq);
  840. destroy_cmd_wq_exit:
  841. destroy_workqueue(ndev->cmd_wq);
  842. exit:
  843. return rc;
  844. }
  845. EXPORT_SYMBOL(nci_register_device);
  846. /**
  847. * nci_unregister_device - unregister a nci device in the nfc subsystem
  848. *
  849. * @dev: The nci device to unregister
  850. */
  851. void nci_unregister_device(struct nci_dev *ndev)
  852. {
  853. struct nci_conn_info *conn_info, *n;
  854. nci_close_device(ndev);
  855. destroy_workqueue(ndev->cmd_wq);
  856. destroy_workqueue(ndev->rx_wq);
  857. destroy_workqueue(ndev->tx_wq);
  858. list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
  859. list_del(&conn_info->list);
  860. /* conn_info is allocated with devm_kzalloc */
  861. }
  862. nfc_unregister_device(ndev->nfc_dev);
  863. }
  864. EXPORT_SYMBOL(nci_unregister_device);
  865. /**
  866. * nci_recv_frame - receive frame from NCI drivers
  867. *
  868. * @ndev: The nci device
  869. * @skb: The sk_buff to receive
  870. */
  871. int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
  872. {
  873. pr_debug("len %d\n", skb->len);
  874. if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
  875. !test_bit(NCI_INIT, &ndev->flags))) {
  876. kfree_skb(skb);
  877. return -ENXIO;
  878. }
  879. /* Queue frame for rx worker thread */
  880. skb_queue_tail(&ndev->rx_q, skb);
  881. queue_work(ndev->rx_wq, &ndev->rx_work);
  882. return 0;
  883. }
  884. EXPORT_SYMBOL(nci_recv_frame);
  885. static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
  886. {
  887. pr_debug("len %d\n", skb->len);
  888. if (!ndev) {
  889. kfree_skb(skb);
  890. return -ENODEV;
  891. }
  892. /* Get rid of skb owner, prior to sending to the driver. */
  893. skb_orphan(skb);
  894. /* Send copy to sniffer */
  895. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  896. RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
  897. return ndev->ops->send(ndev, skb);
  898. }
  899. /* Send NCI command */
  900. int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
  901. {
  902. struct nci_ctrl_hdr *hdr;
  903. struct sk_buff *skb;
  904. pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
  905. skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
  906. if (!skb) {
  907. pr_err("no memory for command\n");
  908. return -ENOMEM;
  909. }
  910. hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
  911. hdr->gid = nci_opcode_gid(opcode);
  912. hdr->oid = nci_opcode_oid(opcode);
  913. hdr->plen = plen;
  914. nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
  915. nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
  916. if (plen)
  917. memcpy(skb_put(skb, plen), payload, plen);
  918. skb_queue_tail(&ndev->cmd_q, skb);
  919. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  920. return 0;
  921. }
  922. /* ---- NCI TX Data worker thread ---- */
  923. static void nci_tx_work(struct work_struct *work)
  924. {
  925. struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
  926. struct nci_conn_info *conn_info;
  927. struct sk_buff *skb;
  928. conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
  929. if (!conn_info)
  930. return;
  931. pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
  932. /* Send queued tx data */
  933. while (atomic_read(&conn_info->credits_cnt)) {
  934. skb = skb_dequeue(&ndev->tx_q);
  935. if (!skb)
  936. return;
  937. /* Check if data flow control is used */
  938. if (atomic_read(&conn_info->credits_cnt) !=
  939. NCI_DATA_FLOW_CONTROL_NOT_USED)
  940. atomic_dec(&conn_info->credits_cnt);
  941. pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
  942. nci_pbf(skb->data),
  943. nci_conn_id(skb->data),
  944. nci_plen(skb->data));
  945. nci_send_frame(ndev, skb);
  946. mod_timer(&ndev->data_timer,
  947. jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
  948. }
  949. }
  950. /* ----- NCI RX worker thread (data & control) ----- */
  951. static void nci_rx_work(struct work_struct *work)
  952. {
  953. struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
  954. struct sk_buff *skb;
  955. while ((skb = skb_dequeue(&ndev->rx_q))) {
  956. /* Send copy to sniffer */
  957. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  958. RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
  959. /* Process frame */
  960. switch (nci_mt(skb->data)) {
  961. case NCI_MT_RSP_PKT:
  962. nci_rsp_packet(ndev, skb);
  963. break;
  964. case NCI_MT_NTF_PKT:
  965. nci_ntf_packet(ndev, skb);
  966. break;
  967. case NCI_MT_DATA_PKT:
  968. nci_rx_data_packet(ndev, skb);
  969. break;
  970. default:
  971. pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
  972. kfree_skb(skb);
  973. break;
  974. }
  975. }
  976. /* check if a data exchange timout has occurred */
  977. if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
  978. /* complete the data exchange transaction, if exists */
  979. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  980. nci_data_exchange_complete(ndev, NULL,
  981. ndev->cur_conn_id,
  982. -ETIMEDOUT);
  983. clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  984. }
  985. }
  986. /* ----- NCI TX CMD worker thread ----- */
  987. static void nci_cmd_work(struct work_struct *work)
  988. {
  989. struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
  990. struct sk_buff *skb;
  991. pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
  992. /* Send queued command */
  993. if (atomic_read(&ndev->cmd_cnt)) {
  994. skb = skb_dequeue(&ndev->cmd_q);
  995. if (!skb)
  996. return;
  997. atomic_dec(&ndev->cmd_cnt);
  998. pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
  999. nci_pbf(skb->data),
  1000. nci_opcode_gid(nci_opcode(skb->data)),
  1001. nci_opcode_oid(nci_opcode(skb->data)),
  1002. nci_plen(skb->data));
  1003. nci_send_frame(ndev, skb);
  1004. mod_timer(&ndev->cmd_timer,
  1005. jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
  1006. }
  1007. }
  1008. MODULE_LICENSE("GPL");