core.c 38 KB

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