core.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  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. timer_delete_sync(&ndev->cmd_timer);
  457. timer_delete_sync(&ndev->data_timer);
  458. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  459. nci_data_exchange_complete(ndev, NULL,
  460. ndev->cur_conn_id,
  461. -ENODEV);
  462. mutex_unlock(&ndev->req_lock);
  463. return 0;
  464. }
  465. /* Drop RX and TX queues */
  466. skb_queue_purge(&ndev->rx_q);
  467. skb_queue_purge(&ndev->tx_q);
  468. /* Flush TX wq, RX wq flush can't be under the lock */
  469. flush_workqueue(ndev->tx_wq);
  470. /* Reset device */
  471. skb_queue_purge(&ndev->cmd_q);
  472. atomic_set(&ndev->cmd_cnt, 1);
  473. set_bit(NCI_INIT, &ndev->flags);
  474. __nci_request(ndev, nci_reset_req, (void *)0,
  475. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  476. /* After this point our queues are empty
  477. * rx work may be running but will see that NCI_UP was cleared
  478. */
  479. ndev->ops->close(ndev);
  480. clear_bit(NCI_INIT, &ndev->flags);
  481. /* Flush cmd and tx wq */
  482. flush_workqueue(ndev->cmd_wq);
  483. timer_delete_sync(&ndev->cmd_timer);
  484. timer_delete_sync(&ndev->data_timer);
  485. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  486. nci_data_exchange_complete(ndev, NULL, ndev->cur_conn_id,
  487. -ENODEV);
  488. /* Clear flags except NCI_UNREG */
  489. ndev->flags &= BIT(NCI_UNREG);
  490. mutex_unlock(&ndev->req_lock);
  491. /* rx_work may take req_lock via nci_deactivate_target */
  492. flush_workqueue(ndev->rx_wq);
  493. return 0;
  494. }
  495. /* NCI command timer function */
  496. static void nci_cmd_timer(struct timer_list *t)
  497. {
  498. struct nci_dev *ndev = timer_container_of(ndev, t, cmd_timer);
  499. atomic_set(&ndev->cmd_cnt, 1);
  500. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  501. }
  502. /* NCI data exchange timer function */
  503. static void nci_data_timer(struct timer_list *t)
  504. {
  505. struct nci_dev *ndev = timer_container_of(ndev, t, data_timer);
  506. set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  507. queue_work(ndev->rx_wq, &ndev->rx_work);
  508. }
  509. static int nci_dev_up(struct nfc_dev *nfc_dev)
  510. {
  511. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  512. return nci_open_device(ndev);
  513. }
  514. static int nci_dev_down(struct nfc_dev *nfc_dev)
  515. {
  516. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  517. return nci_close_device(ndev);
  518. }
  519. int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, const __u8 *val)
  520. {
  521. struct nci_set_config_param param;
  522. if (!val || !len)
  523. return 0;
  524. param.id = id;
  525. param.len = len;
  526. param.val = val;
  527. return __nci_request(ndev, nci_set_config_req, &param,
  528. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  529. }
  530. EXPORT_SYMBOL(nci_set_config);
  531. static void nci_nfcee_discover_req(struct nci_dev *ndev, const void *opt)
  532. {
  533. struct nci_nfcee_discover_cmd cmd;
  534. __u8 action = (unsigned long)opt;
  535. cmd.discovery_action = action;
  536. nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
  537. }
  538. int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
  539. {
  540. unsigned long opt = action;
  541. return __nci_request(ndev, nci_nfcee_discover_req, (void *)opt,
  542. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  543. }
  544. EXPORT_SYMBOL(nci_nfcee_discover);
  545. static void nci_nfcee_mode_set_req(struct nci_dev *ndev, const void *opt)
  546. {
  547. const struct nci_nfcee_mode_set_cmd *cmd = opt;
  548. nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
  549. sizeof(struct nci_nfcee_mode_set_cmd), cmd);
  550. }
  551. int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
  552. {
  553. struct nci_nfcee_mode_set_cmd cmd;
  554. cmd.nfcee_id = nfcee_id;
  555. cmd.nfcee_mode = nfcee_mode;
  556. return __nci_request(ndev, nci_nfcee_mode_set_req, &cmd,
  557. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  558. }
  559. EXPORT_SYMBOL(nci_nfcee_mode_set);
  560. static void nci_core_conn_create_req(struct nci_dev *ndev, const void *opt)
  561. {
  562. const struct core_conn_create_data *data = opt;
  563. nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
  564. }
  565. int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
  566. u8 number_destination_params,
  567. size_t params_len,
  568. const struct core_conn_create_dest_spec_params *params)
  569. {
  570. int r;
  571. struct nci_core_conn_create_cmd *cmd;
  572. struct core_conn_create_data data;
  573. data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
  574. cmd = kzalloc(data.length, GFP_KERNEL);
  575. if (!cmd)
  576. return -ENOMEM;
  577. cmd->destination_type = destination_type;
  578. cmd->number_destination_params = number_destination_params;
  579. data.cmd = cmd;
  580. if (params) {
  581. memcpy(cmd->params, params, params_len);
  582. if (params->length > 0)
  583. memcpy(&ndev->cur_params,
  584. &params->value[DEST_SPEC_PARAMS_ID_INDEX],
  585. sizeof(struct dest_spec_params));
  586. else
  587. ndev->cur_params.id = 0;
  588. } else {
  589. ndev->cur_params.id = 0;
  590. }
  591. ndev->cur_dest_type = destination_type;
  592. r = __nci_request(ndev, nci_core_conn_create_req, &data,
  593. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  594. kfree(cmd);
  595. return r;
  596. }
  597. EXPORT_SYMBOL(nci_core_conn_create);
  598. static void nci_core_conn_close_req(struct nci_dev *ndev, const void *opt)
  599. {
  600. __u8 conn_id = (unsigned long)opt;
  601. nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
  602. }
  603. int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
  604. {
  605. unsigned long opt = conn_id;
  606. ndev->cur_conn_id = conn_id;
  607. return __nci_request(ndev, nci_core_conn_close_req, (void *)opt,
  608. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  609. }
  610. EXPORT_SYMBOL(nci_core_conn_close);
  611. static void nci_set_target_ats(struct nfc_target *target, struct nci_dev *ndev)
  612. {
  613. if (ndev->target_ats_len > 0) {
  614. target->ats_len = ndev->target_ats_len;
  615. memcpy(target->ats, ndev->target_ats, target->ats_len);
  616. }
  617. }
  618. static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
  619. {
  620. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  621. struct nci_set_config_param param;
  622. int rc;
  623. param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
  624. if ((param.val == NULL) || (param.len == 0))
  625. return 0;
  626. if (param.len > NFC_MAX_GT_LEN)
  627. return -EINVAL;
  628. param.id = NCI_PN_ATR_REQ_GEN_BYTES;
  629. rc = nci_request(ndev, nci_set_config_req, &param,
  630. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  631. if (rc)
  632. return rc;
  633. param.id = NCI_LN_ATR_RES_GEN_BYTES;
  634. return nci_request(ndev, nci_set_config_req, &param,
  635. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  636. }
  637. static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
  638. {
  639. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  640. int rc;
  641. __u8 val;
  642. val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
  643. rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
  644. if (rc)
  645. return rc;
  646. val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
  647. rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
  648. if (rc)
  649. return rc;
  650. val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
  651. return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
  652. }
  653. static int nci_start_poll(struct nfc_dev *nfc_dev,
  654. __u32 im_protocols, __u32 tm_protocols)
  655. {
  656. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  657. struct nci_rf_discover_param param;
  658. int rc;
  659. if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
  660. (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
  661. pr_err("unable to start poll, since poll is already active\n");
  662. return -EBUSY;
  663. }
  664. if (ndev->target_active_prot) {
  665. pr_err("there is an active target\n");
  666. return -EBUSY;
  667. }
  668. if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
  669. (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
  670. pr_debug("target active or w4 select, implicitly deactivate\n");
  671. rc = nci_request(ndev, nci_rf_deactivate_req,
  672. (void *)NCI_DEACTIVATE_TYPE_IDLE_MODE,
  673. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  674. if (rc)
  675. return -EBUSY;
  676. }
  677. if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
  678. rc = nci_set_local_general_bytes(nfc_dev);
  679. if (rc) {
  680. pr_err("failed to set local general bytes\n");
  681. return rc;
  682. }
  683. }
  684. if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
  685. rc = nci_set_listen_parameters(nfc_dev);
  686. if (rc)
  687. pr_err("failed to set listen parameters\n");
  688. }
  689. param.im_protocols = im_protocols;
  690. param.tm_protocols = tm_protocols;
  691. rc = nci_request(ndev, nci_rf_discover_req, &param,
  692. msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
  693. if (!rc)
  694. ndev->poll_prots = im_protocols;
  695. return rc;
  696. }
  697. static void nci_stop_poll(struct nfc_dev *nfc_dev)
  698. {
  699. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  700. if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
  701. (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
  702. pr_err("unable to stop poll, since poll is not active\n");
  703. return;
  704. }
  705. nci_request(ndev, nci_rf_deactivate_req,
  706. (void *)NCI_DEACTIVATE_TYPE_IDLE_MODE,
  707. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  708. }
  709. static int nci_activate_target(struct nfc_dev *nfc_dev,
  710. struct nfc_target *target, __u32 protocol)
  711. {
  712. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  713. struct nci_rf_discover_select_param param;
  714. const struct nfc_target *nci_target = NULL;
  715. int i;
  716. int rc = 0;
  717. pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
  718. if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
  719. (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
  720. pr_err("there is no available target to activate\n");
  721. return -EINVAL;
  722. }
  723. if (ndev->target_active_prot) {
  724. pr_err("there is already an active target\n");
  725. return -EBUSY;
  726. }
  727. for (i = 0; i < ndev->n_targets; i++) {
  728. if (ndev->targets[i].idx == target->idx) {
  729. nci_target = &ndev->targets[i];
  730. break;
  731. }
  732. }
  733. if (!nci_target) {
  734. pr_err("unable to find the selected target\n");
  735. return -EINVAL;
  736. }
  737. if (protocol >= NFC_PROTO_MAX) {
  738. pr_err("the requested nfc protocol is invalid\n");
  739. return -EINVAL;
  740. }
  741. if (!(nci_target->supported_protocols & (1 << protocol))) {
  742. pr_err("target does not support the requested protocol 0x%x\n",
  743. protocol);
  744. return -EINVAL;
  745. }
  746. if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
  747. param.rf_discovery_id = nci_target->logical_idx;
  748. if (protocol == NFC_PROTO_JEWEL)
  749. param.rf_protocol = NCI_RF_PROTOCOL_T1T;
  750. else if (protocol == NFC_PROTO_MIFARE)
  751. param.rf_protocol = NCI_RF_PROTOCOL_T2T;
  752. else if (protocol == NFC_PROTO_FELICA)
  753. param.rf_protocol = NCI_RF_PROTOCOL_T3T;
  754. else if (protocol == NFC_PROTO_ISO14443 ||
  755. protocol == NFC_PROTO_ISO14443_B)
  756. param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
  757. else
  758. param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
  759. rc = nci_request(ndev, nci_rf_discover_select_req, &param,
  760. msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
  761. }
  762. if (!rc) {
  763. ndev->target_active_prot = protocol;
  764. if (protocol == NFC_PROTO_ISO14443)
  765. nci_set_target_ats(target, ndev);
  766. }
  767. return rc;
  768. }
  769. static void nci_deactivate_target(struct nfc_dev *nfc_dev,
  770. struct nfc_target *target,
  771. __u8 mode)
  772. {
  773. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  774. unsigned long nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
  775. if (!ndev->target_active_prot) {
  776. pr_err("unable to deactivate target, no active target\n");
  777. return;
  778. }
  779. ndev->target_active_prot = 0;
  780. switch (mode) {
  781. case NFC_TARGET_MODE_SLEEP:
  782. nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
  783. break;
  784. }
  785. if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
  786. nci_request(ndev, nci_rf_deactivate_req, (void *)nci_mode,
  787. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  788. }
  789. }
  790. static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
  791. __u8 comm_mode, __u8 *gb, size_t gb_len)
  792. {
  793. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  794. int rc;
  795. pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
  796. rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
  797. if (rc)
  798. return rc;
  799. rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
  800. ndev->remote_gb_len);
  801. if (!rc)
  802. rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
  803. NFC_RF_INITIATOR);
  804. return rc;
  805. }
  806. static int nci_dep_link_down(struct nfc_dev *nfc_dev)
  807. {
  808. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  809. int rc;
  810. if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
  811. nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
  812. } else {
  813. if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
  814. atomic_read(&ndev->state) == NCI_DISCOVERY) {
  815. nci_request(ndev, nci_rf_deactivate_req, (void *)0,
  816. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  817. }
  818. rc = nfc_tm_deactivated(nfc_dev);
  819. if (rc)
  820. pr_err("error when signaling tm deactivation\n");
  821. }
  822. return 0;
  823. }
  824. static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
  825. struct sk_buff *skb,
  826. data_exchange_cb_t cb, void *cb_context)
  827. {
  828. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  829. int rc;
  830. struct nci_conn_info *conn_info;
  831. conn_info = ndev->rf_conn_info;
  832. if (!conn_info) {
  833. kfree_skb(skb);
  834. return -EPROTO;
  835. }
  836. pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
  837. if (!ndev->target_active_prot) {
  838. pr_err("unable to exchange data, no active target\n");
  839. kfree_skb(skb);
  840. return -EINVAL;
  841. }
  842. if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags)) {
  843. kfree_skb(skb);
  844. return -EBUSY;
  845. }
  846. /* store cb and context to be used on receiving data */
  847. conn_info->data_exchange_cb = cb;
  848. conn_info->data_exchange_cb_context = cb_context;
  849. rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
  850. if (rc)
  851. clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
  852. return rc;
  853. }
  854. static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
  855. {
  856. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  857. int rc;
  858. rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
  859. if (rc)
  860. pr_err("unable to send data\n");
  861. return rc;
  862. }
  863. static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  864. {
  865. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  866. if (ndev->ops->enable_se)
  867. return ndev->ops->enable_se(ndev, se_idx);
  868. return 0;
  869. }
  870. static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  871. {
  872. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  873. if (ndev->ops->disable_se)
  874. return ndev->ops->disable_se(ndev, se_idx);
  875. return 0;
  876. }
  877. static int nci_discover_se(struct nfc_dev *nfc_dev)
  878. {
  879. int r;
  880. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  881. if (ndev->ops->discover_se) {
  882. r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
  883. if (r != NCI_STATUS_OK)
  884. return -EPROTO;
  885. return ndev->ops->discover_se(ndev);
  886. }
  887. return 0;
  888. }
  889. static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
  890. u8 *apdu, size_t apdu_length,
  891. se_io_cb_t cb, void *cb_context)
  892. {
  893. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  894. if (ndev->ops->se_io)
  895. return ndev->ops->se_io(ndev, se_idx, apdu,
  896. apdu_length, cb, cb_context);
  897. return 0;
  898. }
  899. static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
  900. {
  901. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  902. if (!ndev->ops->fw_download)
  903. return -ENOTSUPP;
  904. return ndev->ops->fw_download(ndev, firmware_name);
  905. }
  906. static const struct nfc_ops nci_nfc_ops = {
  907. .dev_up = nci_dev_up,
  908. .dev_down = nci_dev_down,
  909. .start_poll = nci_start_poll,
  910. .stop_poll = nci_stop_poll,
  911. .dep_link_up = nci_dep_link_up,
  912. .dep_link_down = nci_dep_link_down,
  913. .activate_target = nci_activate_target,
  914. .deactivate_target = nci_deactivate_target,
  915. .im_transceive = nci_transceive,
  916. .tm_send = nci_tm_send,
  917. .enable_se = nci_enable_se,
  918. .disable_se = nci_disable_se,
  919. .discover_se = nci_discover_se,
  920. .se_io = nci_se_io,
  921. .fw_download = nci_fw_download,
  922. };
  923. /* ---- Interface to NCI drivers ---- */
  924. /**
  925. * nci_allocate_device - allocate a new nci device
  926. *
  927. * @ops: device operations
  928. * @supported_protocols: NFC protocols supported by the device
  929. * @tx_headroom: Reserved space at beginning of skb
  930. * @tx_tailroom: Reserved space at end of skb
  931. */
  932. struct nci_dev *nci_allocate_device(const struct nci_ops *ops,
  933. __u32 supported_protocols,
  934. int tx_headroom, int tx_tailroom)
  935. {
  936. struct nci_dev *ndev;
  937. pr_debug("supported_protocols 0x%x\n", supported_protocols);
  938. if (!ops->open || !ops->close || !ops->send)
  939. return NULL;
  940. if (!supported_protocols)
  941. return NULL;
  942. ndev = kzalloc_obj(struct nci_dev);
  943. if (!ndev)
  944. return NULL;
  945. ndev->ops = ops;
  946. if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
  947. pr_err("Too many proprietary commands: %zd\n",
  948. ops->n_prop_ops);
  949. goto free_nci;
  950. }
  951. ndev->tx_headroom = tx_headroom;
  952. ndev->tx_tailroom = tx_tailroom;
  953. init_completion(&ndev->req_completion);
  954. ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
  955. supported_protocols,
  956. tx_headroom + NCI_DATA_HDR_SIZE,
  957. tx_tailroom);
  958. if (!ndev->nfc_dev)
  959. goto free_nci;
  960. ndev->hci_dev = nci_hci_allocate(ndev);
  961. if (!ndev->hci_dev)
  962. goto free_nfc;
  963. nfc_set_drvdata(ndev->nfc_dev, ndev);
  964. return ndev;
  965. free_nfc:
  966. nfc_free_device(ndev->nfc_dev);
  967. free_nci:
  968. kfree(ndev);
  969. return NULL;
  970. }
  971. EXPORT_SYMBOL(nci_allocate_device);
  972. /**
  973. * nci_free_device - deallocate nci device
  974. *
  975. * @ndev: The nci device to deallocate
  976. */
  977. void nci_free_device(struct nci_dev *ndev)
  978. {
  979. nfc_free_device(ndev->nfc_dev);
  980. nci_hci_deallocate(ndev);
  981. /* drop partial rx data packet if present */
  982. if (ndev->rx_data_reassembly)
  983. kfree_skb(ndev->rx_data_reassembly);
  984. kfree(ndev);
  985. }
  986. EXPORT_SYMBOL(nci_free_device);
  987. /**
  988. * nci_register_device - register a nci device in the nfc subsystem
  989. *
  990. * @ndev: The nci device to register
  991. */
  992. int nci_register_device(struct nci_dev *ndev)
  993. {
  994. int rc;
  995. struct device *dev = &ndev->nfc_dev->dev;
  996. char name[32];
  997. ndev->flags = 0;
  998. INIT_WORK(&ndev->cmd_work, nci_cmd_work);
  999. snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
  1000. ndev->cmd_wq = create_singlethread_workqueue(name);
  1001. if (!ndev->cmd_wq) {
  1002. rc = -ENOMEM;
  1003. goto exit;
  1004. }
  1005. INIT_WORK(&ndev->rx_work, nci_rx_work);
  1006. snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
  1007. ndev->rx_wq = create_singlethread_workqueue(name);
  1008. if (!ndev->rx_wq) {
  1009. rc = -ENOMEM;
  1010. goto destroy_cmd_wq_exit;
  1011. }
  1012. INIT_WORK(&ndev->tx_work, nci_tx_work);
  1013. snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
  1014. ndev->tx_wq = create_singlethread_workqueue(name);
  1015. if (!ndev->tx_wq) {
  1016. rc = -ENOMEM;
  1017. goto destroy_rx_wq_exit;
  1018. }
  1019. skb_queue_head_init(&ndev->cmd_q);
  1020. skb_queue_head_init(&ndev->rx_q);
  1021. skb_queue_head_init(&ndev->tx_q);
  1022. timer_setup(&ndev->cmd_timer, nci_cmd_timer, 0);
  1023. timer_setup(&ndev->data_timer, nci_data_timer, 0);
  1024. mutex_init(&ndev->req_lock);
  1025. INIT_LIST_HEAD(&ndev->conn_info_list);
  1026. rc = nfc_register_device(ndev->nfc_dev);
  1027. if (rc)
  1028. goto destroy_tx_wq_exit;
  1029. goto exit;
  1030. destroy_tx_wq_exit:
  1031. destroy_workqueue(ndev->tx_wq);
  1032. destroy_rx_wq_exit:
  1033. destroy_workqueue(ndev->rx_wq);
  1034. destroy_cmd_wq_exit:
  1035. destroy_workqueue(ndev->cmd_wq);
  1036. exit:
  1037. return rc;
  1038. }
  1039. EXPORT_SYMBOL(nci_register_device);
  1040. /**
  1041. * nci_unregister_device - unregister a nci device in the nfc subsystem
  1042. *
  1043. * @ndev: The nci device to unregister
  1044. */
  1045. void nci_unregister_device(struct nci_dev *ndev)
  1046. {
  1047. struct nci_conn_info *conn_info, *n;
  1048. nfc_unregister_rfkill(ndev->nfc_dev);
  1049. /* This set_bit is not protected with specialized barrier,
  1050. * However, it is fine because the mutex_lock(&ndev->req_lock);
  1051. * in nci_close_device() will help to emit one.
  1052. */
  1053. set_bit(NCI_UNREG, &ndev->flags);
  1054. nci_close_device(ndev);
  1055. destroy_workqueue(ndev->cmd_wq);
  1056. destroy_workqueue(ndev->rx_wq);
  1057. destroy_workqueue(ndev->tx_wq);
  1058. list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
  1059. list_del(&conn_info->list);
  1060. /* conn_info is allocated with devm_kzalloc */
  1061. }
  1062. nfc_remove_device(ndev->nfc_dev);
  1063. }
  1064. EXPORT_SYMBOL(nci_unregister_device);
  1065. /**
  1066. * nci_recv_frame - receive frame from NCI drivers
  1067. *
  1068. * @ndev: The nci device
  1069. * @skb: The sk_buff to receive
  1070. */
  1071. int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
  1072. {
  1073. pr_debug("len %d\n", skb->len);
  1074. if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
  1075. !test_bit(NCI_INIT, &ndev->flags))) {
  1076. kfree_skb(skb);
  1077. return -ENXIO;
  1078. }
  1079. /* Queue frame for rx worker thread */
  1080. skb_queue_tail(&ndev->rx_q, skb);
  1081. queue_work(ndev->rx_wq, &ndev->rx_work);
  1082. return 0;
  1083. }
  1084. EXPORT_SYMBOL(nci_recv_frame);
  1085. int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
  1086. {
  1087. pr_debug("len %d\n", skb->len);
  1088. if (!ndev) {
  1089. kfree_skb(skb);
  1090. return -ENODEV;
  1091. }
  1092. /* Get rid of skb owner, prior to sending to the driver. */
  1093. skb_orphan(skb);
  1094. /* Send copy to sniffer */
  1095. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  1096. RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
  1097. return ndev->ops->send(ndev, skb);
  1098. }
  1099. EXPORT_SYMBOL(nci_send_frame);
  1100. /* Send NCI command */
  1101. int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, const void *payload)
  1102. {
  1103. struct nci_ctrl_hdr *hdr;
  1104. struct sk_buff *skb;
  1105. pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
  1106. skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
  1107. if (!skb) {
  1108. pr_err("no memory for command\n");
  1109. return -ENOMEM;
  1110. }
  1111. hdr = skb_put(skb, NCI_CTRL_HDR_SIZE);
  1112. hdr->gid = nci_opcode_gid(opcode);
  1113. hdr->oid = nci_opcode_oid(opcode);
  1114. hdr->plen = plen;
  1115. nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
  1116. nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
  1117. if (plen)
  1118. skb_put_data(skb, payload, plen);
  1119. skb_queue_tail(&ndev->cmd_q, skb);
  1120. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  1121. return 0;
  1122. }
  1123. EXPORT_SYMBOL(nci_send_cmd);
  1124. /* Proprietary commands API */
  1125. static const struct nci_driver_ops *ops_cmd_lookup(const struct nci_driver_ops *ops,
  1126. size_t n_ops,
  1127. __u16 opcode)
  1128. {
  1129. size_t i;
  1130. const struct nci_driver_ops *op;
  1131. if (!ops || !n_ops)
  1132. return NULL;
  1133. for (i = 0; i < n_ops; i++) {
  1134. op = &ops[i];
  1135. if (op->opcode == opcode)
  1136. return op;
  1137. }
  1138. return NULL;
  1139. }
  1140. static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
  1141. struct sk_buff *skb, const struct nci_driver_ops *ops,
  1142. size_t n_ops)
  1143. {
  1144. const struct nci_driver_ops *op;
  1145. op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
  1146. if (!op || !op->rsp)
  1147. return -ENOTSUPP;
  1148. return op->rsp(ndev, skb);
  1149. }
  1150. static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
  1151. struct sk_buff *skb, const struct nci_driver_ops *ops,
  1152. size_t n_ops)
  1153. {
  1154. const struct nci_driver_ops *op;
  1155. op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
  1156. if (!op || !op->ntf)
  1157. return -ENOTSUPP;
  1158. return op->ntf(ndev, skb);
  1159. }
  1160. int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
  1161. struct sk_buff *skb)
  1162. {
  1163. return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
  1164. ndev->ops->n_prop_ops);
  1165. }
  1166. int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
  1167. struct sk_buff *skb)
  1168. {
  1169. return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
  1170. ndev->ops->n_prop_ops);
  1171. }
  1172. int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
  1173. struct sk_buff *skb)
  1174. {
  1175. return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
  1176. ndev->ops->n_core_ops);
  1177. }
  1178. int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
  1179. struct sk_buff *skb)
  1180. {
  1181. return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
  1182. ndev->ops->n_core_ops);
  1183. }
  1184. static bool nci_valid_size(struct sk_buff *skb)
  1185. {
  1186. BUILD_BUG_ON(NCI_CTRL_HDR_SIZE != NCI_DATA_HDR_SIZE);
  1187. unsigned int hdr_size = NCI_CTRL_HDR_SIZE;
  1188. if (skb->len < hdr_size ||
  1189. skb->len < hdr_size + nci_plen(skb->data)) {
  1190. return false;
  1191. }
  1192. if (!nci_plen(skb->data)) {
  1193. /* Allow zero length in proprietary notifications (0x20 - 0x3F). */
  1194. if (nci_opcode_oid(nci_opcode(skb->data)) >= 0x20 &&
  1195. nci_mt(skb->data) == NCI_MT_NTF_PKT)
  1196. return true;
  1197. /* Disallow zero length otherwise. */
  1198. return false;
  1199. }
  1200. return true;
  1201. }
  1202. /* ---- NCI TX Data worker thread ---- */
  1203. static void nci_tx_work(struct work_struct *work)
  1204. {
  1205. struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
  1206. struct nci_conn_info *conn_info;
  1207. struct sk_buff *skb;
  1208. conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
  1209. if (!conn_info)
  1210. return;
  1211. pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
  1212. /* Send queued tx data */
  1213. while (atomic_read(&conn_info->credits_cnt)) {
  1214. skb = skb_dequeue(&ndev->tx_q);
  1215. if (!skb)
  1216. return;
  1217. kcov_remote_start_common(skb_get_kcov_handle(skb));
  1218. /* Check if data flow control is used */
  1219. if (atomic_read(&conn_info->credits_cnt) !=
  1220. NCI_DATA_FLOW_CONTROL_NOT_USED)
  1221. atomic_dec(&conn_info->credits_cnt);
  1222. pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
  1223. nci_pbf(skb->data),
  1224. nci_conn_id(skb->data),
  1225. nci_plen(skb->data));
  1226. nci_send_frame(ndev, skb);
  1227. mod_timer(&ndev->data_timer,
  1228. jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
  1229. kcov_remote_stop();
  1230. }
  1231. }
  1232. /* ----- NCI RX worker thread (data & control) ----- */
  1233. static void nci_rx_work(struct work_struct *work)
  1234. {
  1235. struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
  1236. struct sk_buff *skb;
  1237. for (; (skb = skb_dequeue(&ndev->rx_q)); kcov_remote_stop()) {
  1238. kcov_remote_start_common(skb_get_kcov_handle(skb));
  1239. /* Send copy to sniffer */
  1240. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  1241. RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
  1242. if (!nci_valid_size(skb)) {
  1243. kfree_skb(skb);
  1244. continue;
  1245. }
  1246. /* Process frame */
  1247. switch (nci_mt(skb->data)) {
  1248. case NCI_MT_RSP_PKT:
  1249. nci_rsp_packet(ndev, skb);
  1250. break;
  1251. case NCI_MT_NTF_PKT:
  1252. nci_ntf_packet(ndev, skb);
  1253. break;
  1254. case NCI_MT_DATA_PKT:
  1255. nci_rx_data_packet(ndev, skb);
  1256. break;
  1257. default:
  1258. pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
  1259. kfree_skb(skb);
  1260. break;
  1261. }
  1262. }
  1263. /* check if a data exchange timeout has occurred */
  1264. if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
  1265. /* complete the data exchange transaction, if exists */
  1266. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  1267. nci_data_exchange_complete(ndev, NULL,
  1268. ndev->cur_conn_id,
  1269. -ETIMEDOUT);
  1270. clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  1271. }
  1272. }
  1273. /* ----- NCI TX CMD worker thread ----- */
  1274. static void nci_cmd_work(struct work_struct *work)
  1275. {
  1276. struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
  1277. struct sk_buff *skb;
  1278. pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
  1279. /* Send queued command */
  1280. if (atomic_read(&ndev->cmd_cnt)) {
  1281. skb = skb_dequeue(&ndev->cmd_q);
  1282. if (!skb)
  1283. return;
  1284. kcov_remote_start_common(skb_get_kcov_handle(skb));
  1285. atomic_dec(&ndev->cmd_cnt);
  1286. pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
  1287. nci_pbf(skb->data),
  1288. nci_opcode_gid(nci_opcode(skb->data)),
  1289. nci_opcode_oid(nci_opcode(skb->data)),
  1290. nci_plen(skb->data));
  1291. nci_send_frame(ndev, skb);
  1292. mod_timer(&ndev->cmd_timer,
  1293. jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
  1294. kcov_remote_stop();
  1295. }
  1296. }
  1297. MODULE_DESCRIPTION("NFC Controller Interface");
  1298. MODULE_LICENSE("GPL");