core.c 33 KB

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