core.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. *
  7. * Written by Ilan Elias <ilane@ti.com>
  8. *
  9. * Acknowledgements:
  10. * This file is based on hci_core.c, which was written
  11. * by Maxim Krasnyansky.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2
  15. * as published by the Free Software Foundation
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  27. #include <linux/module.h>
  28. #include <linux/types.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/completion.h>
  31. #include <linux/export.h>
  32. #include <linux/sched.h>
  33. #include <linux/bitops.h>
  34. #include <linux/skbuff.h>
  35. #include "../nfc.h"
  36. #include <net/nfc/nci.h>
  37. #include <net/nfc/nci_core.h>
  38. #include <linux/nfc.h>
  39. static void nci_cmd_work(struct work_struct *work);
  40. static void nci_rx_work(struct work_struct *work);
  41. static void nci_tx_work(struct work_struct *work);
  42. /* ---- NCI requests ---- */
  43. void nci_req_complete(struct nci_dev *ndev, int result)
  44. {
  45. if (ndev->req_status == NCI_REQ_PEND) {
  46. ndev->req_result = result;
  47. ndev->req_status = NCI_REQ_DONE;
  48. complete(&ndev->req_completion);
  49. }
  50. }
  51. static void nci_req_cancel(struct nci_dev *ndev, int err)
  52. {
  53. if (ndev->req_status == NCI_REQ_PEND) {
  54. ndev->req_result = err;
  55. ndev->req_status = NCI_REQ_CANCELED;
  56. complete(&ndev->req_completion);
  57. }
  58. }
  59. /* Execute request and wait for completion. */
  60. static int __nci_request(struct nci_dev *ndev,
  61. void (*req)(struct nci_dev *ndev, unsigned long opt),
  62. unsigned long opt, __u32 timeout)
  63. {
  64. int rc = 0;
  65. long completion_rc;
  66. ndev->req_status = NCI_REQ_PEND;
  67. reinit_completion(&ndev->req_completion);
  68. req(ndev, opt);
  69. completion_rc =
  70. wait_for_completion_interruptible_timeout(&ndev->req_completion,
  71. timeout);
  72. pr_debug("wait_for_completion return %ld\n", completion_rc);
  73. if (completion_rc > 0) {
  74. switch (ndev->req_status) {
  75. case NCI_REQ_DONE:
  76. rc = nci_to_errno(ndev->req_result);
  77. break;
  78. case NCI_REQ_CANCELED:
  79. rc = -ndev->req_result;
  80. break;
  81. default:
  82. rc = -ETIMEDOUT;
  83. break;
  84. }
  85. } else {
  86. pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
  87. completion_rc);
  88. rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
  89. }
  90. ndev->req_status = ndev->req_result = 0;
  91. return rc;
  92. }
  93. static inline int nci_request(struct nci_dev *ndev,
  94. void (*req)(struct nci_dev *ndev,
  95. unsigned long opt),
  96. unsigned long opt, __u32 timeout)
  97. {
  98. int rc;
  99. if (!test_bit(NCI_UP, &ndev->flags))
  100. return -ENETDOWN;
  101. /* Serialize all requests */
  102. mutex_lock(&ndev->req_lock);
  103. rc = __nci_request(ndev, req, opt, timeout);
  104. mutex_unlock(&ndev->req_lock);
  105. return rc;
  106. }
  107. static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
  108. {
  109. struct nci_core_reset_cmd cmd;
  110. cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
  111. nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
  112. }
  113. static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
  114. {
  115. nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
  116. }
  117. static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
  118. {
  119. struct nci_rf_disc_map_cmd cmd;
  120. struct disc_map_config *cfg = cmd.mapping_configs;
  121. __u8 *num = &cmd.num_mapping_configs;
  122. int i;
  123. /* set rf mapping configurations */
  124. *num = 0;
  125. /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
  126. for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
  127. if (ndev->supported_rf_interfaces[i] ==
  128. NCI_RF_INTERFACE_ISO_DEP) {
  129. cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
  130. cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
  131. NCI_DISC_MAP_MODE_LISTEN;
  132. cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
  133. (*num)++;
  134. } else if (ndev->supported_rf_interfaces[i] ==
  135. NCI_RF_INTERFACE_NFC_DEP) {
  136. cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
  137. cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
  138. NCI_DISC_MAP_MODE_LISTEN;
  139. cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
  140. (*num)++;
  141. }
  142. if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
  143. break;
  144. }
  145. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
  146. (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
  147. }
  148. struct nci_set_config_param {
  149. __u8 id;
  150. size_t len;
  151. __u8 *val;
  152. };
  153. static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
  154. {
  155. struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
  156. struct nci_core_set_config_cmd cmd;
  157. BUG_ON(param->len > NCI_MAX_PARAM_LEN);
  158. cmd.num_params = 1;
  159. cmd.param.id = param->id;
  160. cmd.param.len = param->len;
  161. memcpy(cmd.param.val, param->val, param->len);
  162. nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
  163. }
  164. static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
  165. {
  166. struct nci_rf_disc_cmd cmd;
  167. __u32 protocols = opt;
  168. cmd.num_disc_configs = 0;
  169. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  170. (protocols & NFC_PROTO_JEWEL_MASK ||
  171. protocols & NFC_PROTO_MIFARE_MASK ||
  172. protocols & NFC_PROTO_ISO14443_MASK ||
  173. protocols & NFC_PROTO_NFC_DEP_MASK)) {
  174. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  175. NCI_NFC_A_PASSIVE_POLL_MODE;
  176. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  177. cmd.num_disc_configs++;
  178. }
  179. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  180. (protocols & NFC_PROTO_ISO14443_B_MASK)) {
  181. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  182. NCI_NFC_B_PASSIVE_POLL_MODE;
  183. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  184. cmd.num_disc_configs++;
  185. }
  186. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  187. (protocols & NFC_PROTO_FELICA_MASK ||
  188. protocols & NFC_PROTO_NFC_DEP_MASK)) {
  189. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  190. NCI_NFC_F_PASSIVE_POLL_MODE;
  191. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  192. cmd.num_disc_configs++;
  193. }
  194. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  195. (protocols & NFC_PROTO_ISO15693_MASK)) {
  196. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  197. NCI_NFC_V_PASSIVE_POLL_MODE;
  198. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  199. cmd.num_disc_configs++;
  200. }
  201. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
  202. (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
  203. &cmd);
  204. }
  205. struct nci_rf_discover_select_param {
  206. __u8 rf_discovery_id;
  207. __u8 rf_protocol;
  208. };
  209. static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
  210. {
  211. struct nci_rf_discover_select_param *param =
  212. (struct nci_rf_discover_select_param *)opt;
  213. struct nci_rf_discover_select_cmd cmd;
  214. cmd.rf_discovery_id = param->rf_discovery_id;
  215. cmd.rf_protocol = param->rf_protocol;
  216. switch (cmd.rf_protocol) {
  217. case NCI_RF_PROTOCOL_ISO_DEP:
  218. cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
  219. break;
  220. case NCI_RF_PROTOCOL_NFC_DEP:
  221. cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
  222. break;
  223. default:
  224. cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
  225. break;
  226. }
  227. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
  228. sizeof(struct nci_rf_discover_select_cmd), &cmd);
  229. }
  230. static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
  231. {
  232. struct nci_rf_deactivate_cmd cmd;
  233. cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE;
  234. nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
  235. sizeof(struct nci_rf_deactivate_cmd), &cmd);
  236. }
  237. static int nci_open_device(struct nci_dev *ndev)
  238. {
  239. int rc = 0;
  240. mutex_lock(&ndev->req_lock);
  241. if (test_bit(NCI_UP, &ndev->flags)) {
  242. rc = -EALREADY;
  243. goto done;
  244. }
  245. if (ndev->ops->open(ndev)) {
  246. rc = -EIO;
  247. goto done;
  248. }
  249. atomic_set(&ndev->cmd_cnt, 1);
  250. set_bit(NCI_INIT, &ndev->flags);
  251. rc = __nci_request(ndev, nci_reset_req, 0,
  252. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  253. if (ndev->ops->setup)
  254. ndev->ops->setup(ndev);
  255. if (!rc) {
  256. rc = __nci_request(ndev, nci_init_req, 0,
  257. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  258. }
  259. if (!rc) {
  260. rc = __nci_request(ndev, nci_init_complete_req, 0,
  261. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  262. }
  263. clear_bit(NCI_INIT, &ndev->flags);
  264. if (!rc) {
  265. set_bit(NCI_UP, &ndev->flags);
  266. nci_clear_target_list(ndev);
  267. atomic_set(&ndev->state, NCI_IDLE);
  268. } else {
  269. /* Init failed, cleanup */
  270. skb_queue_purge(&ndev->cmd_q);
  271. skb_queue_purge(&ndev->rx_q);
  272. skb_queue_purge(&ndev->tx_q);
  273. ndev->ops->close(ndev);
  274. ndev->flags = 0;
  275. }
  276. done:
  277. mutex_unlock(&ndev->req_lock);
  278. return rc;
  279. }
  280. static int nci_close_device(struct nci_dev *ndev)
  281. {
  282. nci_req_cancel(ndev, ENODEV);
  283. mutex_lock(&ndev->req_lock);
  284. if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
  285. del_timer_sync(&ndev->cmd_timer);
  286. del_timer_sync(&ndev->data_timer);
  287. mutex_unlock(&ndev->req_lock);
  288. return 0;
  289. }
  290. /* Drop RX and TX queues */
  291. skb_queue_purge(&ndev->rx_q);
  292. skb_queue_purge(&ndev->tx_q);
  293. /* Flush RX and TX wq */
  294. flush_workqueue(ndev->rx_wq);
  295. flush_workqueue(ndev->tx_wq);
  296. /* Reset device */
  297. skb_queue_purge(&ndev->cmd_q);
  298. atomic_set(&ndev->cmd_cnt, 1);
  299. set_bit(NCI_INIT, &ndev->flags);
  300. __nci_request(ndev, nci_reset_req, 0,
  301. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  302. clear_bit(NCI_INIT, &ndev->flags);
  303. del_timer_sync(&ndev->cmd_timer);
  304. /* Flush cmd wq */
  305. flush_workqueue(ndev->cmd_wq);
  306. /* After this point our queues are empty
  307. * and no works are scheduled. */
  308. ndev->ops->close(ndev);
  309. /* Clear flags */
  310. ndev->flags = 0;
  311. mutex_unlock(&ndev->req_lock);
  312. return 0;
  313. }
  314. /* NCI command timer function */
  315. static void nci_cmd_timer(unsigned long arg)
  316. {
  317. struct nci_dev *ndev = (void *) arg;
  318. atomic_set(&ndev->cmd_cnt, 1);
  319. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  320. }
  321. /* NCI data exchange timer function */
  322. static void nci_data_timer(unsigned long arg)
  323. {
  324. struct nci_dev *ndev = (void *) arg;
  325. set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  326. queue_work(ndev->rx_wq, &ndev->rx_work);
  327. }
  328. static int nci_dev_up(struct nfc_dev *nfc_dev)
  329. {
  330. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  331. return nci_open_device(ndev);
  332. }
  333. static int nci_dev_down(struct nfc_dev *nfc_dev)
  334. {
  335. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  336. return nci_close_device(ndev);
  337. }
  338. int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
  339. {
  340. struct nci_set_config_param param;
  341. if (!val || !len)
  342. return 0;
  343. param.id = id;
  344. param.len = len;
  345. param.val = val;
  346. return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
  347. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  348. }
  349. EXPORT_SYMBOL(nci_set_config);
  350. static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
  351. {
  352. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  353. struct nci_set_config_param param;
  354. param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
  355. if ((param.val == NULL) || (param.len == 0))
  356. return 0;
  357. if (param.len > NFC_MAX_GT_LEN)
  358. return -EINVAL;
  359. param.id = NCI_PN_ATR_REQ_GEN_BYTES;
  360. return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
  361. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  362. }
  363. static int nci_start_poll(struct nfc_dev *nfc_dev,
  364. __u32 im_protocols, __u32 tm_protocols)
  365. {
  366. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  367. int rc;
  368. if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
  369. (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
  370. pr_err("unable to start poll, since poll is already active\n");
  371. return -EBUSY;
  372. }
  373. if (ndev->target_active_prot) {
  374. pr_err("there is an active target\n");
  375. return -EBUSY;
  376. }
  377. if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
  378. (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
  379. pr_debug("target active or w4 select, implicitly deactivate\n");
  380. rc = nci_request(ndev, nci_rf_deactivate_req, 0,
  381. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  382. if (rc)
  383. return -EBUSY;
  384. }
  385. if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
  386. rc = nci_set_local_general_bytes(nfc_dev);
  387. if (rc) {
  388. pr_err("failed to set local general bytes\n");
  389. return rc;
  390. }
  391. }
  392. rc = nci_request(ndev, nci_rf_discover_req, im_protocols,
  393. msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
  394. if (!rc)
  395. ndev->poll_prots = im_protocols;
  396. return rc;
  397. }
  398. static void nci_stop_poll(struct nfc_dev *nfc_dev)
  399. {
  400. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  401. if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
  402. (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
  403. pr_err("unable to stop poll, since poll is not active\n");
  404. return;
  405. }
  406. nci_request(ndev, nci_rf_deactivate_req, 0,
  407. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  408. }
  409. static int nci_activate_target(struct nfc_dev *nfc_dev,
  410. struct nfc_target *target, __u32 protocol)
  411. {
  412. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  413. struct nci_rf_discover_select_param param;
  414. struct nfc_target *nci_target = NULL;
  415. int i;
  416. int rc = 0;
  417. pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
  418. if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
  419. (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
  420. pr_err("there is no available target to activate\n");
  421. return -EINVAL;
  422. }
  423. if (ndev->target_active_prot) {
  424. pr_err("there is already an active target\n");
  425. return -EBUSY;
  426. }
  427. for (i = 0; i < ndev->n_targets; i++) {
  428. if (ndev->targets[i].idx == target->idx) {
  429. nci_target = &ndev->targets[i];
  430. break;
  431. }
  432. }
  433. if (!nci_target) {
  434. pr_err("unable to find the selected target\n");
  435. return -EINVAL;
  436. }
  437. if (!(nci_target->supported_protocols & (1 << protocol))) {
  438. pr_err("target does not support the requested protocol 0x%x\n",
  439. protocol);
  440. return -EINVAL;
  441. }
  442. if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
  443. param.rf_discovery_id = nci_target->logical_idx;
  444. if (protocol == NFC_PROTO_JEWEL)
  445. param.rf_protocol = NCI_RF_PROTOCOL_T1T;
  446. else if (protocol == NFC_PROTO_MIFARE)
  447. param.rf_protocol = NCI_RF_PROTOCOL_T2T;
  448. else if (protocol == NFC_PROTO_FELICA)
  449. param.rf_protocol = NCI_RF_PROTOCOL_T3T;
  450. else if (protocol == NFC_PROTO_ISO14443 ||
  451. protocol == NFC_PROTO_ISO14443_B)
  452. param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
  453. else
  454. param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
  455. rc = nci_request(ndev, nci_rf_discover_select_req,
  456. (unsigned long)&param,
  457. msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
  458. }
  459. if (!rc)
  460. ndev->target_active_prot = protocol;
  461. return rc;
  462. }
  463. static void nci_deactivate_target(struct nfc_dev *nfc_dev,
  464. struct nfc_target *target)
  465. {
  466. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  467. pr_debug("entry\n");
  468. if (!ndev->target_active_prot) {
  469. pr_err("unable to deactivate target, no active target\n");
  470. return;
  471. }
  472. ndev->target_active_prot = 0;
  473. if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
  474. nci_request(ndev, nci_rf_deactivate_req, 0,
  475. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  476. }
  477. }
  478. static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
  479. __u8 comm_mode, __u8 *gb, size_t gb_len)
  480. {
  481. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  482. int rc;
  483. pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
  484. rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
  485. if (rc)
  486. return rc;
  487. rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
  488. ndev->remote_gb_len);
  489. if (!rc)
  490. rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
  491. NFC_RF_INITIATOR);
  492. return rc;
  493. }
  494. static int nci_dep_link_down(struct nfc_dev *nfc_dev)
  495. {
  496. pr_debug("entry\n");
  497. nci_deactivate_target(nfc_dev, NULL);
  498. return 0;
  499. }
  500. static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
  501. struct sk_buff *skb,
  502. data_exchange_cb_t cb, void *cb_context)
  503. {
  504. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  505. int rc;
  506. pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
  507. if (!ndev->target_active_prot) {
  508. pr_err("unable to exchange data, no active target\n");
  509. return -EINVAL;
  510. }
  511. if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  512. return -EBUSY;
  513. /* store cb and context to be used on receiving data */
  514. ndev->data_exchange_cb = cb;
  515. ndev->data_exchange_cb_context = cb_context;
  516. rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
  517. if (rc)
  518. clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
  519. return rc;
  520. }
  521. static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  522. {
  523. return 0;
  524. }
  525. static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  526. {
  527. return 0;
  528. }
  529. static int nci_discover_se(struct nfc_dev *nfc_dev)
  530. {
  531. return 0;
  532. }
  533. static struct nfc_ops nci_nfc_ops = {
  534. .dev_up = nci_dev_up,
  535. .dev_down = nci_dev_down,
  536. .start_poll = nci_start_poll,
  537. .stop_poll = nci_stop_poll,
  538. .dep_link_up = nci_dep_link_up,
  539. .dep_link_down = nci_dep_link_down,
  540. .activate_target = nci_activate_target,
  541. .deactivate_target = nci_deactivate_target,
  542. .im_transceive = nci_transceive,
  543. .enable_se = nci_enable_se,
  544. .disable_se = nci_disable_se,
  545. .discover_se = nci_discover_se,
  546. };
  547. /* ---- Interface to NCI drivers ---- */
  548. /**
  549. * nci_allocate_device - allocate a new nci device
  550. *
  551. * @ops: device operations
  552. * @supported_protocols: NFC protocols supported by the device
  553. */
  554. struct nci_dev *nci_allocate_device(struct nci_ops *ops,
  555. __u32 supported_protocols,
  556. int tx_headroom, int tx_tailroom)
  557. {
  558. struct nci_dev *ndev;
  559. pr_debug("supported_protocols 0x%x\n", supported_protocols);
  560. if (!ops->open || !ops->close || !ops->send)
  561. return NULL;
  562. if (!supported_protocols)
  563. return NULL;
  564. ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
  565. if (!ndev)
  566. return NULL;
  567. ndev->ops = ops;
  568. ndev->tx_headroom = tx_headroom;
  569. ndev->tx_tailroom = tx_tailroom;
  570. init_completion(&ndev->req_completion);
  571. ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
  572. supported_protocols,
  573. tx_headroom + NCI_DATA_HDR_SIZE,
  574. tx_tailroom);
  575. if (!ndev->nfc_dev)
  576. goto free_exit;
  577. nfc_set_drvdata(ndev->nfc_dev, ndev);
  578. return ndev;
  579. free_exit:
  580. kfree(ndev);
  581. return NULL;
  582. }
  583. EXPORT_SYMBOL(nci_allocate_device);
  584. /**
  585. * nci_free_device - deallocate nci device
  586. *
  587. * @ndev: The nci device to deallocate
  588. */
  589. void nci_free_device(struct nci_dev *ndev)
  590. {
  591. nfc_free_device(ndev->nfc_dev);
  592. kfree(ndev);
  593. }
  594. EXPORT_SYMBOL(nci_free_device);
  595. /**
  596. * nci_register_device - register a nci device in the nfc subsystem
  597. *
  598. * @dev: The nci device to register
  599. */
  600. int nci_register_device(struct nci_dev *ndev)
  601. {
  602. int rc;
  603. struct device *dev = &ndev->nfc_dev->dev;
  604. char name[32];
  605. ndev->flags = 0;
  606. INIT_WORK(&ndev->cmd_work, nci_cmd_work);
  607. snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
  608. ndev->cmd_wq = create_singlethread_workqueue(name);
  609. if (!ndev->cmd_wq) {
  610. rc = -ENOMEM;
  611. goto exit;
  612. }
  613. INIT_WORK(&ndev->rx_work, nci_rx_work);
  614. snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
  615. ndev->rx_wq = create_singlethread_workqueue(name);
  616. if (!ndev->rx_wq) {
  617. rc = -ENOMEM;
  618. goto destroy_cmd_wq_exit;
  619. }
  620. INIT_WORK(&ndev->tx_work, nci_tx_work);
  621. snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
  622. ndev->tx_wq = create_singlethread_workqueue(name);
  623. if (!ndev->tx_wq) {
  624. rc = -ENOMEM;
  625. goto destroy_rx_wq_exit;
  626. }
  627. skb_queue_head_init(&ndev->cmd_q);
  628. skb_queue_head_init(&ndev->rx_q);
  629. skb_queue_head_init(&ndev->tx_q);
  630. setup_timer(&ndev->cmd_timer, nci_cmd_timer,
  631. (unsigned long) ndev);
  632. setup_timer(&ndev->data_timer, nci_data_timer,
  633. (unsigned long) ndev);
  634. mutex_init(&ndev->req_lock);
  635. rc = nfc_register_device(ndev->nfc_dev);
  636. if (rc)
  637. goto destroy_rx_wq_exit;
  638. goto exit;
  639. destroy_rx_wq_exit:
  640. destroy_workqueue(ndev->rx_wq);
  641. destroy_cmd_wq_exit:
  642. destroy_workqueue(ndev->cmd_wq);
  643. exit:
  644. return rc;
  645. }
  646. EXPORT_SYMBOL(nci_register_device);
  647. /**
  648. * nci_unregister_device - unregister a nci device in the nfc subsystem
  649. *
  650. * @dev: The nci device to unregister
  651. */
  652. void nci_unregister_device(struct nci_dev *ndev)
  653. {
  654. nci_close_device(ndev);
  655. destroy_workqueue(ndev->cmd_wq);
  656. destroy_workqueue(ndev->rx_wq);
  657. destroy_workqueue(ndev->tx_wq);
  658. nfc_unregister_device(ndev->nfc_dev);
  659. }
  660. EXPORT_SYMBOL(nci_unregister_device);
  661. /**
  662. * nci_recv_frame - receive frame from NCI drivers
  663. *
  664. * @ndev: The nci device
  665. * @skb: The sk_buff to receive
  666. */
  667. int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
  668. {
  669. pr_debug("len %d\n", skb->len);
  670. if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
  671. !test_bit(NCI_INIT, &ndev->flags))) {
  672. kfree_skb(skb);
  673. return -ENXIO;
  674. }
  675. /* Queue frame for rx worker thread */
  676. skb_queue_tail(&ndev->rx_q, skb);
  677. queue_work(ndev->rx_wq, &ndev->rx_work);
  678. return 0;
  679. }
  680. EXPORT_SYMBOL(nci_recv_frame);
  681. static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
  682. {
  683. pr_debug("len %d\n", skb->len);
  684. if (!ndev) {
  685. kfree_skb(skb);
  686. return -ENODEV;
  687. }
  688. /* Get rid of skb owner, prior to sending to the driver. */
  689. skb_orphan(skb);
  690. /* Send copy to sniffer */
  691. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  692. RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
  693. return ndev->ops->send(ndev, skb);
  694. }
  695. /* Send NCI command */
  696. int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
  697. {
  698. struct nci_ctrl_hdr *hdr;
  699. struct sk_buff *skb;
  700. pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
  701. skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
  702. if (!skb) {
  703. pr_err("no memory for command\n");
  704. return -ENOMEM;
  705. }
  706. hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
  707. hdr->gid = nci_opcode_gid(opcode);
  708. hdr->oid = nci_opcode_oid(opcode);
  709. hdr->plen = plen;
  710. nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
  711. nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
  712. if (plen)
  713. memcpy(skb_put(skb, plen), payload, plen);
  714. skb_queue_tail(&ndev->cmd_q, skb);
  715. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  716. return 0;
  717. }
  718. /* ---- NCI TX Data worker thread ---- */
  719. static void nci_tx_work(struct work_struct *work)
  720. {
  721. struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
  722. struct sk_buff *skb;
  723. pr_debug("credits_cnt %d\n", atomic_read(&ndev->credits_cnt));
  724. /* Send queued tx data */
  725. while (atomic_read(&ndev->credits_cnt)) {
  726. skb = skb_dequeue(&ndev->tx_q);
  727. if (!skb)
  728. return;
  729. /* Check if data flow control is used */
  730. if (atomic_read(&ndev->credits_cnt) !=
  731. NCI_DATA_FLOW_CONTROL_NOT_USED)
  732. atomic_dec(&ndev->credits_cnt);
  733. pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
  734. nci_pbf(skb->data),
  735. nci_conn_id(skb->data),
  736. nci_plen(skb->data));
  737. nci_send_frame(ndev, skb);
  738. mod_timer(&ndev->data_timer,
  739. jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
  740. }
  741. }
  742. /* ----- NCI RX worker thread (data & control) ----- */
  743. static void nci_rx_work(struct work_struct *work)
  744. {
  745. struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
  746. struct sk_buff *skb;
  747. while ((skb = skb_dequeue(&ndev->rx_q))) {
  748. /* Send copy to sniffer */
  749. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  750. RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
  751. /* Process frame */
  752. switch (nci_mt(skb->data)) {
  753. case NCI_MT_RSP_PKT:
  754. nci_rsp_packet(ndev, skb);
  755. break;
  756. case NCI_MT_NTF_PKT:
  757. nci_ntf_packet(ndev, skb);
  758. break;
  759. case NCI_MT_DATA_PKT:
  760. nci_rx_data_packet(ndev, skb);
  761. break;
  762. default:
  763. pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
  764. kfree_skb(skb);
  765. break;
  766. }
  767. }
  768. /* check if a data exchange timout has occurred */
  769. if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
  770. /* complete the data exchange transaction, if exists */
  771. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  772. nci_data_exchange_complete(ndev, NULL, -ETIMEDOUT);
  773. clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  774. }
  775. }
  776. /* ----- NCI TX CMD worker thread ----- */
  777. static void nci_cmd_work(struct work_struct *work)
  778. {
  779. struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
  780. struct sk_buff *skb;
  781. pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
  782. /* Send queued command */
  783. if (atomic_read(&ndev->cmd_cnt)) {
  784. skb = skb_dequeue(&ndev->cmd_q);
  785. if (!skb)
  786. return;
  787. atomic_dec(&ndev->cmd_cnt);
  788. pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
  789. nci_pbf(skb->data),
  790. nci_opcode_gid(nci_opcode(skb->data)),
  791. nci_opcode_oid(nci_opcode(skb->data)),
  792. nci_plen(skb->data));
  793. nci_send_frame(ndev, skb);
  794. mod_timer(&ndev->cmd_timer,
  795. jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
  796. }
  797. }
  798. MODULE_LICENSE("GPL");