core.c 35 KB

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