core.c 37 KB

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