core.c 33 KB

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