core.c 37 KB

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