core.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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,
  450. (unsigned long)&cmd,
  451. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  452. }
  453. EXPORT_SYMBOL(nci_nfcee_mode_set);
  454. static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt)
  455. {
  456. struct core_conn_create_data *data =
  457. (struct core_conn_create_data *)opt;
  458. nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
  459. }
  460. int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
  461. u8 number_destination_params,
  462. size_t params_len,
  463. struct core_conn_create_dest_spec_params *params)
  464. {
  465. int r;
  466. struct nci_core_conn_create_cmd *cmd;
  467. struct core_conn_create_data data;
  468. data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
  469. cmd = kzalloc(data.length, GFP_KERNEL);
  470. if (!cmd)
  471. return -ENOMEM;
  472. cmd->destination_type = destination_type;
  473. cmd->number_destination_params = number_destination_params;
  474. memcpy(cmd->params, params, params_len);
  475. data.cmd = cmd;
  476. ndev->cur_id = params->value[DEST_SPEC_PARAMS_ID_INDEX];
  477. r = __nci_request(ndev, nci_core_conn_create_req,
  478. (unsigned long)&data,
  479. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  480. kfree(cmd);
  481. return r;
  482. }
  483. EXPORT_SYMBOL(nci_core_conn_create);
  484. static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
  485. {
  486. __u8 conn_id = opt;
  487. nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
  488. }
  489. int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
  490. {
  491. return __nci_request(ndev, nci_core_conn_close_req, conn_id,
  492. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  493. }
  494. EXPORT_SYMBOL(nci_core_conn_close);
  495. static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
  496. {
  497. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  498. struct nci_set_config_param param;
  499. int rc;
  500. param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
  501. if ((param.val == NULL) || (param.len == 0))
  502. return 0;
  503. if (param.len > NFC_MAX_GT_LEN)
  504. return -EINVAL;
  505. param.id = NCI_PN_ATR_REQ_GEN_BYTES;
  506. rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
  507. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  508. if (rc)
  509. return rc;
  510. param.id = NCI_LN_ATR_RES_GEN_BYTES;
  511. return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
  512. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  513. }
  514. static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
  515. {
  516. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  517. int rc;
  518. __u8 val;
  519. val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
  520. rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
  521. if (rc)
  522. return rc;
  523. val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
  524. rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
  525. if (rc)
  526. return rc;
  527. val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
  528. return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
  529. }
  530. static int nci_start_poll(struct nfc_dev *nfc_dev,
  531. __u32 im_protocols, __u32 tm_protocols)
  532. {
  533. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  534. struct nci_rf_discover_param param;
  535. int rc;
  536. if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
  537. (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
  538. pr_err("unable to start poll, since poll is already active\n");
  539. return -EBUSY;
  540. }
  541. if (ndev->target_active_prot) {
  542. pr_err("there is an active target\n");
  543. return -EBUSY;
  544. }
  545. if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
  546. (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
  547. pr_debug("target active or w4 select, implicitly deactivate\n");
  548. rc = nci_request(ndev, nci_rf_deactivate_req,
  549. NCI_DEACTIVATE_TYPE_IDLE_MODE,
  550. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  551. if (rc)
  552. return -EBUSY;
  553. }
  554. if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
  555. rc = nci_set_local_general_bytes(nfc_dev);
  556. if (rc) {
  557. pr_err("failed to set local general bytes\n");
  558. return rc;
  559. }
  560. }
  561. if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
  562. rc = nci_set_listen_parameters(nfc_dev);
  563. if (rc)
  564. pr_err("failed to set listen parameters\n");
  565. }
  566. param.im_protocols = im_protocols;
  567. param.tm_protocols = tm_protocols;
  568. rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
  569. msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
  570. if (!rc)
  571. ndev->poll_prots = im_protocols;
  572. return rc;
  573. }
  574. static void nci_stop_poll(struct nfc_dev *nfc_dev)
  575. {
  576. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  577. if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
  578. (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
  579. pr_err("unable to stop poll, since poll is not active\n");
  580. return;
  581. }
  582. nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
  583. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  584. }
  585. static int nci_activate_target(struct nfc_dev *nfc_dev,
  586. struct nfc_target *target, __u32 protocol)
  587. {
  588. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  589. struct nci_rf_discover_select_param param;
  590. struct nfc_target *nci_target = NULL;
  591. int i;
  592. int rc = 0;
  593. pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
  594. if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
  595. (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
  596. pr_err("there is no available target to activate\n");
  597. return -EINVAL;
  598. }
  599. if (ndev->target_active_prot) {
  600. pr_err("there is already an active target\n");
  601. return -EBUSY;
  602. }
  603. for (i = 0; i < ndev->n_targets; i++) {
  604. if (ndev->targets[i].idx == target->idx) {
  605. nci_target = &ndev->targets[i];
  606. break;
  607. }
  608. }
  609. if (!nci_target) {
  610. pr_err("unable to find the selected target\n");
  611. return -EINVAL;
  612. }
  613. if (!(nci_target->supported_protocols & (1 << protocol))) {
  614. pr_err("target does not support the requested protocol 0x%x\n",
  615. protocol);
  616. return -EINVAL;
  617. }
  618. if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
  619. param.rf_discovery_id = nci_target->logical_idx;
  620. if (protocol == NFC_PROTO_JEWEL)
  621. param.rf_protocol = NCI_RF_PROTOCOL_T1T;
  622. else if (protocol == NFC_PROTO_MIFARE)
  623. param.rf_protocol = NCI_RF_PROTOCOL_T2T;
  624. else if (protocol == NFC_PROTO_FELICA)
  625. param.rf_protocol = NCI_RF_PROTOCOL_T3T;
  626. else if (protocol == NFC_PROTO_ISO14443 ||
  627. protocol == NFC_PROTO_ISO14443_B)
  628. param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
  629. else
  630. param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
  631. rc = nci_request(ndev, nci_rf_discover_select_req,
  632. (unsigned long)&param,
  633. msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
  634. }
  635. if (!rc)
  636. ndev->target_active_prot = protocol;
  637. return rc;
  638. }
  639. static void nci_deactivate_target(struct nfc_dev *nfc_dev,
  640. struct nfc_target *target)
  641. {
  642. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  643. pr_debug("entry\n");
  644. if (!ndev->target_active_prot) {
  645. pr_err("unable to deactivate target, no active target\n");
  646. return;
  647. }
  648. ndev->target_active_prot = 0;
  649. if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
  650. nci_request(ndev, nci_rf_deactivate_req,
  651. NCI_DEACTIVATE_TYPE_IDLE_MODE,
  652. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  653. }
  654. }
  655. static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
  656. __u8 comm_mode, __u8 *gb, size_t gb_len)
  657. {
  658. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  659. int rc;
  660. pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
  661. rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
  662. if (rc)
  663. return rc;
  664. rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
  665. ndev->remote_gb_len);
  666. if (!rc)
  667. rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
  668. NFC_RF_INITIATOR);
  669. return rc;
  670. }
  671. static int nci_dep_link_down(struct nfc_dev *nfc_dev)
  672. {
  673. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  674. int rc;
  675. pr_debug("entry\n");
  676. if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
  677. nci_deactivate_target(nfc_dev, NULL);
  678. } else {
  679. if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
  680. atomic_read(&ndev->state) == NCI_DISCOVERY) {
  681. nci_request(ndev, nci_rf_deactivate_req, 0,
  682. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  683. }
  684. rc = nfc_tm_deactivated(nfc_dev);
  685. if (rc)
  686. pr_err("error when signaling tm deactivation\n");
  687. }
  688. return 0;
  689. }
  690. static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
  691. struct sk_buff *skb,
  692. data_exchange_cb_t cb, void *cb_context)
  693. {
  694. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  695. int rc;
  696. struct nci_conn_info *conn_info;
  697. conn_info = ndev->rf_conn_info;
  698. if (!conn_info)
  699. return -EPROTO;
  700. pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
  701. if (!ndev->target_active_prot) {
  702. pr_err("unable to exchange data, no active target\n");
  703. return -EINVAL;
  704. }
  705. if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  706. return -EBUSY;
  707. /* store cb and context to be used on receiving data */
  708. conn_info->data_exchange_cb = cb;
  709. conn_info->data_exchange_cb_context = cb_context;
  710. rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
  711. if (rc)
  712. clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
  713. return rc;
  714. }
  715. static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
  716. {
  717. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  718. int rc;
  719. rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
  720. if (rc)
  721. pr_err("unable to send data\n");
  722. return rc;
  723. }
  724. static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  725. {
  726. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  727. if (ndev->ops->enable_se)
  728. return ndev->ops->enable_se(ndev, se_idx);
  729. return 0;
  730. }
  731. static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  732. {
  733. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  734. if (ndev->ops->disable_se)
  735. return ndev->ops->disable_se(ndev, se_idx);
  736. return 0;
  737. }
  738. static int nci_discover_se(struct nfc_dev *nfc_dev)
  739. {
  740. int r;
  741. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  742. if (ndev->ops->discover_se) {
  743. r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
  744. if (r != NCI_STATUS_OK)
  745. return -EPROTO;
  746. return ndev->ops->discover_se(ndev);
  747. }
  748. return 0;
  749. }
  750. static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
  751. u8 *apdu, size_t apdu_length,
  752. se_io_cb_t cb, void *cb_context)
  753. {
  754. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  755. if (ndev->ops->se_io)
  756. return ndev->ops->se_io(ndev, se_idx, apdu,
  757. apdu_length, cb, cb_context);
  758. return 0;
  759. }
  760. static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
  761. {
  762. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  763. if (!ndev->ops->fw_download)
  764. return -ENOTSUPP;
  765. return ndev->ops->fw_download(ndev, firmware_name);
  766. }
  767. static struct nfc_ops nci_nfc_ops = {
  768. .dev_up = nci_dev_up,
  769. .dev_down = nci_dev_down,
  770. .start_poll = nci_start_poll,
  771. .stop_poll = nci_stop_poll,
  772. .dep_link_up = nci_dep_link_up,
  773. .dep_link_down = nci_dep_link_down,
  774. .activate_target = nci_activate_target,
  775. .deactivate_target = nci_deactivate_target,
  776. .im_transceive = nci_transceive,
  777. .tm_send = nci_tm_send,
  778. .enable_se = nci_enable_se,
  779. .disable_se = nci_disable_se,
  780. .discover_se = nci_discover_se,
  781. .se_io = nci_se_io,
  782. .fw_download = nci_fw_download,
  783. };
  784. /* ---- Interface to NCI drivers ---- */
  785. /**
  786. * nci_allocate_device - allocate a new nci device
  787. *
  788. * @ops: device operations
  789. * @supported_protocols: NFC protocols supported by the device
  790. */
  791. struct nci_dev *nci_allocate_device(struct nci_ops *ops,
  792. __u32 supported_protocols,
  793. int tx_headroom, int tx_tailroom)
  794. {
  795. struct nci_dev *ndev;
  796. pr_debug("supported_protocols 0x%x\n", supported_protocols);
  797. if (!ops->open || !ops->close || !ops->send)
  798. return NULL;
  799. if (!supported_protocols)
  800. return NULL;
  801. ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
  802. if (!ndev)
  803. return NULL;
  804. ndev->ops = ops;
  805. if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
  806. pr_err("Too many proprietary commands: %zd\n",
  807. ops->n_prop_ops);
  808. ops->prop_ops = NULL;
  809. ops->n_prop_ops = 0;
  810. }
  811. ndev->tx_headroom = tx_headroom;
  812. ndev->tx_tailroom = tx_tailroom;
  813. init_completion(&ndev->req_completion);
  814. ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
  815. supported_protocols,
  816. tx_headroom + NCI_DATA_HDR_SIZE,
  817. tx_tailroom);
  818. if (!ndev->nfc_dev)
  819. goto free_nci;
  820. ndev->hci_dev = nci_hci_allocate(ndev);
  821. if (!ndev->hci_dev)
  822. goto free_nfc;
  823. nfc_set_drvdata(ndev->nfc_dev, ndev);
  824. return ndev;
  825. free_nfc:
  826. kfree(ndev->nfc_dev);
  827. free_nci:
  828. kfree(ndev);
  829. return NULL;
  830. }
  831. EXPORT_SYMBOL(nci_allocate_device);
  832. /**
  833. * nci_free_device - deallocate nci device
  834. *
  835. * @ndev: The nci device to deallocate
  836. */
  837. void nci_free_device(struct nci_dev *ndev)
  838. {
  839. nfc_free_device(ndev->nfc_dev);
  840. kfree(ndev);
  841. }
  842. EXPORT_SYMBOL(nci_free_device);
  843. /**
  844. * nci_register_device - register a nci device in the nfc subsystem
  845. *
  846. * @dev: The nci device to register
  847. */
  848. int nci_register_device(struct nci_dev *ndev)
  849. {
  850. int rc;
  851. struct device *dev = &ndev->nfc_dev->dev;
  852. char name[32];
  853. ndev->flags = 0;
  854. INIT_WORK(&ndev->cmd_work, nci_cmd_work);
  855. snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
  856. ndev->cmd_wq = create_singlethread_workqueue(name);
  857. if (!ndev->cmd_wq) {
  858. rc = -ENOMEM;
  859. goto exit;
  860. }
  861. INIT_WORK(&ndev->rx_work, nci_rx_work);
  862. snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
  863. ndev->rx_wq = create_singlethread_workqueue(name);
  864. if (!ndev->rx_wq) {
  865. rc = -ENOMEM;
  866. goto destroy_cmd_wq_exit;
  867. }
  868. INIT_WORK(&ndev->tx_work, nci_tx_work);
  869. snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
  870. ndev->tx_wq = create_singlethread_workqueue(name);
  871. if (!ndev->tx_wq) {
  872. rc = -ENOMEM;
  873. goto destroy_rx_wq_exit;
  874. }
  875. skb_queue_head_init(&ndev->cmd_q);
  876. skb_queue_head_init(&ndev->rx_q);
  877. skb_queue_head_init(&ndev->tx_q);
  878. setup_timer(&ndev->cmd_timer, nci_cmd_timer,
  879. (unsigned long) ndev);
  880. setup_timer(&ndev->data_timer, nci_data_timer,
  881. (unsigned long) ndev);
  882. mutex_init(&ndev->req_lock);
  883. INIT_LIST_HEAD(&ndev->conn_info_list);
  884. rc = nfc_register_device(ndev->nfc_dev);
  885. if (rc)
  886. goto destroy_rx_wq_exit;
  887. goto exit;
  888. destroy_rx_wq_exit:
  889. destroy_workqueue(ndev->rx_wq);
  890. destroy_cmd_wq_exit:
  891. destroy_workqueue(ndev->cmd_wq);
  892. exit:
  893. return rc;
  894. }
  895. EXPORT_SYMBOL(nci_register_device);
  896. /**
  897. * nci_unregister_device - unregister a nci device in the nfc subsystem
  898. *
  899. * @dev: The nci device to unregister
  900. */
  901. void nci_unregister_device(struct nci_dev *ndev)
  902. {
  903. struct nci_conn_info *conn_info, *n;
  904. nci_close_device(ndev);
  905. destroy_workqueue(ndev->cmd_wq);
  906. destroy_workqueue(ndev->rx_wq);
  907. destroy_workqueue(ndev->tx_wq);
  908. list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
  909. list_del(&conn_info->list);
  910. /* conn_info is allocated with devm_kzalloc */
  911. }
  912. nfc_unregister_device(ndev->nfc_dev);
  913. }
  914. EXPORT_SYMBOL(nci_unregister_device);
  915. /**
  916. * nci_recv_frame - receive frame from NCI drivers
  917. *
  918. * @ndev: The nci device
  919. * @skb: The sk_buff to receive
  920. */
  921. int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
  922. {
  923. pr_debug("len %d\n", skb->len);
  924. if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
  925. !test_bit(NCI_INIT, &ndev->flags))) {
  926. kfree_skb(skb);
  927. return -ENXIO;
  928. }
  929. /* Queue frame for rx worker thread */
  930. skb_queue_tail(&ndev->rx_q, skb);
  931. queue_work(ndev->rx_wq, &ndev->rx_work);
  932. return 0;
  933. }
  934. EXPORT_SYMBOL(nci_recv_frame);
  935. static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
  936. {
  937. pr_debug("len %d\n", skb->len);
  938. if (!ndev) {
  939. kfree_skb(skb);
  940. return -ENODEV;
  941. }
  942. /* Get rid of skb owner, prior to sending to the driver. */
  943. skb_orphan(skb);
  944. /* Send copy to sniffer */
  945. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  946. RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
  947. return ndev->ops->send(ndev, skb);
  948. }
  949. /* Send NCI command */
  950. int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
  951. {
  952. struct nci_ctrl_hdr *hdr;
  953. struct sk_buff *skb;
  954. pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
  955. skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
  956. if (!skb) {
  957. pr_err("no memory for command\n");
  958. return -ENOMEM;
  959. }
  960. hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
  961. hdr->gid = nci_opcode_gid(opcode);
  962. hdr->oid = nci_opcode_oid(opcode);
  963. hdr->plen = plen;
  964. nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
  965. nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
  966. if (plen)
  967. memcpy(skb_put(skb, plen), payload, plen);
  968. skb_queue_tail(&ndev->cmd_q, skb);
  969. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  970. return 0;
  971. }
  972. /* Proprietary commands API */
  973. static struct nci_prop_ops *prop_cmd_lookup(struct nci_dev *ndev,
  974. __u16 opcode)
  975. {
  976. size_t i;
  977. struct nci_prop_ops *prop_op;
  978. if (!ndev->ops->prop_ops || !ndev->ops->n_prop_ops)
  979. return NULL;
  980. for (i = 0; i < ndev->ops->n_prop_ops; i++) {
  981. prop_op = &ndev->ops->prop_ops[i];
  982. if (prop_op->opcode == opcode)
  983. return prop_op;
  984. }
  985. return NULL;
  986. }
  987. int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
  988. struct sk_buff *skb)
  989. {
  990. struct nci_prop_ops *prop_op;
  991. prop_op = prop_cmd_lookup(ndev, rsp_opcode);
  992. if (!prop_op || !prop_op->rsp)
  993. return -ENOTSUPP;
  994. return prop_op->rsp(ndev, skb);
  995. }
  996. int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
  997. struct sk_buff *skb)
  998. {
  999. struct nci_prop_ops *prop_op;
  1000. prop_op = prop_cmd_lookup(ndev, ntf_opcode);
  1001. if (!prop_op || !prop_op->ntf)
  1002. return -ENOTSUPP;
  1003. return prop_op->ntf(ndev, skb);
  1004. }
  1005. /* ---- NCI TX Data worker thread ---- */
  1006. static void nci_tx_work(struct work_struct *work)
  1007. {
  1008. struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
  1009. struct nci_conn_info *conn_info;
  1010. struct sk_buff *skb;
  1011. conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
  1012. if (!conn_info)
  1013. return;
  1014. pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
  1015. /* Send queued tx data */
  1016. while (atomic_read(&conn_info->credits_cnt)) {
  1017. skb = skb_dequeue(&ndev->tx_q);
  1018. if (!skb)
  1019. return;
  1020. /* Check if data flow control is used */
  1021. if (atomic_read(&conn_info->credits_cnt) !=
  1022. NCI_DATA_FLOW_CONTROL_NOT_USED)
  1023. atomic_dec(&conn_info->credits_cnt);
  1024. pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
  1025. nci_pbf(skb->data),
  1026. nci_conn_id(skb->data),
  1027. nci_plen(skb->data));
  1028. nci_send_frame(ndev, skb);
  1029. mod_timer(&ndev->data_timer,
  1030. jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
  1031. }
  1032. }
  1033. /* ----- NCI RX worker thread (data & control) ----- */
  1034. static void nci_rx_work(struct work_struct *work)
  1035. {
  1036. struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
  1037. struct sk_buff *skb;
  1038. while ((skb = skb_dequeue(&ndev->rx_q))) {
  1039. /* Send copy to sniffer */
  1040. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  1041. RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
  1042. /* Process frame */
  1043. switch (nci_mt(skb->data)) {
  1044. case NCI_MT_RSP_PKT:
  1045. nci_rsp_packet(ndev, skb);
  1046. break;
  1047. case NCI_MT_NTF_PKT:
  1048. nci_ntf_packet(ndev, skb);
  1049. break;
  1050. case NCI_MT_DATA_PKT:
  1051. nci_rx_data_packet(ndev, skb);
  1052. break;
  1053. default:
  1054. pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
  1055. kfree_skb(skb);
  1056. break;
  1057. }
  1058. }
  1059. /* check if a data exchange timout has occurred */
  1060. if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
  1061. /* complete the data exchange transaction, if exists */
  1062. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  1063. nci_data_exchange_complete(ndev, NULL,
  1064. ndev->cur_conn_id,
  1065. -ETIMEDOUT);
  1066. clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  1067. }
  1068. }
  1069. /* ----- NCI TX CMD worker thread ----- */
  1070. static void nci_cmd_work(struct work_struct *work)
  1071. {
  1072. struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
  1073. struct sk_buff *skb;
  1074. pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
  1075. /* Send queued command */
  1076. if (atomic_read(&ndev->cmd_cnt)) {
  1077. skb = skb_dequeue(&ndev->cmd_q);
  1078. if (!skb)
  1079. return;
  1080. atomic_dec(&ndev->cmd_cnt);
  1081. pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
  1082. nci_pbf(skb->data),
  1083. nci_opcode_gid(nci_opcode(skb->data)),
  1084. nci_opcode_oid(nci_opcode(skb->data)),
  1085. nci_plen(skb->data));
  1086. nci_send_frame(ndev, skb);
  1087. mod_timer(&ndev->cmd_timer,
  1088. jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
  1089. }
  1090. }
  1091. MODULE_LICENSE("GPL");