core.c 37 KB

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