core.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "hci: %s: " fmt, __func__
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/nfc.h>
  10. #include <net/nfc/nfc.h>
  11. #include <net/nfc/hci.h>
  12. #include <net/nfc/llc.h>
  13. #include "hci.h"
  14. /* Largest headroom needed for outgoing HCI commands */
  15. #define HCI_CMDS_HEADROOM 1
  16. int nfc_hci_result_to_errno(u8 result)
  17. {
  18. switch (result) {
  19. case NFC_HCI_ANY_OK:
  20. return 0;
  21. case NFC_HCI_ANY_E_REG_PAR_UNKNOWN:
  22. return -EOPNOTSUPP;
  23. case NFC_HCI_ANY_E_TIMEOUT:
  24. return -ETIME;
  25. default:
  26. return -1;
  27. }
  28. }
  29. EXPORT_SYMBOL(nfc_hci_result_to_errno);
  30. void nfc_hci_reset_pipes(struct nfc_hci_dev *hdev)
  31. {
  32. int i = 0;
  33. for (i = 0; i < NFC_HCI_MAX_PIPES; i++) {
  34. hdev->pipes[i].gate = NFC_HCI_INVALID_GATE;
  35. hdev->pipes[i].dest_host = NFC_HCI_INVALID_HOST;
  36. }
  37. memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
  38. }
  39. EXPORT_SYMBOL(nfc_hci_reset_pipes);
  40. void nfc_hci_reset_pipes_per_host(struct nfc_hci_dev *hdev, u8 host)
  41. {
  42. int i = 0;
  43. for (i = 0; i < NFC_HCI_MAX_PIPES; i++) {
  44. if (hdev->pipes[i].dest_host != host)
  45. continue;
  46. hdev->pipes[i].gate = NFC_HCI_INVALID_GATE;
  47. hdev->pipes[i].dest_host = NFC_HCI_INVALID_HOST;
  48. }
  49. }
  50. EXPORT_SYMBOL(nfc_hci_reset_pipes_per_host);
  51. static void nfc_hci_msg_tx_work(struct work_struct *work)
  52. {
  53. struct nfc_hci_dev *hdev = container_of(work, struct nfc_hci_dev,
  54. msg_tx_work);
  55. struct hci_msg *msg;
  56. struct sk_buff *skb;
  57. int r = 0;
  58. mutex_lock(&hdev->msg_tx_mutex);
  59. if (hdev->shutting_down)
  60. goto exit;
  61. if (hdev->cmd_pending_msg) {
  62. if (timer_pending(&hdev->cmd_timer) == 0) {
  63. if (hdev->cmd_pending_msg->cb)
  64. hdev->cmd_pending_msg->cb(hdev->
  65. cmd_pending_msg->
  66. cb_context,
  67. NULL,
  68. -ETIME);
  69. kfree(hdev->cmd_pending_msg);
  70. hdev->cmd_pending_msg = NULL;
  71. } else {
  72. goto exit;
  73. }
  74. }
  75. next_msg:
  76. if (list_empty(&hdev->msg_tx_queue))
  77. goto exit;
  78. msg = list_first_entry(&hdev->msg_tx_queue, struct hci_msg, msg_l);
  79. list_del(&msg->msg_l);
  80. pr_debug("msg_tx_queue has a cmd to send\n");
  81. while ((skb = skb_dequeue(&msg->msg_frags)) != NULL) {
  82. r = nfc_llc_xmit_from_hci(hdev->llc, skb);
  83. if (r < 0) {
  84. kfree_skb(skb);
  85. skb_queue_purge(&msg->msg_frags);
  86. if (msg->cb)
  87. msg->cb(msg->cb_context, NULL, r);
  88. kfree(msg);
  89. break;
  90. }
  91. }
  92. if (r)
  93. goto next_msg;
  94. if (msg->wait_response == false) {
  95. kfree(msg);
  96. goto next_msg;
  97. }
  98. hdev->cmd_pending_msg = msg;
  99. mod_timer(&hdev->cmd_timer, jiffies +
  100. msecs_to_jiffies(hdev->cmd_pending_msg->completion_delay));
  101. exit:
  102. mutex_unlock(&hdev->msg_tx_mutex);
  103. }
  104. static void nfc_hci_msg_rx_work(struct work_struct *work)
  105. {
  106. struct nfc_hci_dev *hdev = container_of(work, struct nfc_hci_dev,
  107. msg_rx_work);
  108. struct sk_buff *skb;
  109. struct hcp_message *message;
  110. u8 pipe;
  111. u8 type;
  112. u8 instruction;
  113. while ((skb = skb_dequeue(&hdev->msg_rx_queue)) != NULL) {
  114. pipe = skb->data[0];
  115. skb_pull(skb, NFC_HCI_HCP_PACKET_HEADER_LEN);
  116. message = (struct hcp_message *)skb->data;
  117. type = HCP_MSG_GET_TYPE(message->header);
  118. instruction = HCP_MSG_GET_CMD(message->header);
  119. skb_pull(skb, NFC_HCI_HCP_MESSAGE_HEADER_LEN);
  120. nfc_hci_hcp_message_rx(hdev, pipe, type, instruction, skb);
  121. }
  122. }
  123. static void __nfc_hci_cmd_completion(struct nfc_hci_dev *hdev, int err,
  124. struct sk_buff *skb)
  125. {
  126. del_timer_sync(&hdev->cmd_timer);
  127. if (hdev->cmd_pending_msg->cb)
  128. hdev->cmd_pending_msg->cb(hdev->cmd_pending_msg->cb_context,
  129. skb, err);
  130. else
  131. kfree_skb(skb);
  132. kfree(hdev->cmd_pending_msg);
  133. hdev->cmd_pending_msg = NULL;
  134. schedule_work(&hdev->msg_tx_work);
  135. }
  136. void nfc_hci_resp_received(struct nfc_hci_dev *hdev, u8 result,
  137. struct sk_buff *skb)
  138. {
  139. mutex_lock(&hdev->msg_tx_mutex);
  140. if (hdev->cmd_pending_msg == NULL) {
  141. kfree_skb(skb);
  142. goto exit;
  143. }
  144. __nfc_hci_cmd_completion(hdev, nfc_hci_result_to_errno(result), skb);
  145. exit:
  146. mutex_unlock(&hdev->msg_tx_mutex);
  147. }
  148. void nfc_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
  149. struct sk_buff *skb)
  150. {
  151. u8 status = NFC_HCI_ANY_OK;
  152. struct hci_create_pipe_resp *create_info;
  153. struct hci_delete_pipe_noti *delete_info;
  154. struct hci_all_pipe_cleared_noti *cleared_info;
  155. u8 gate;
  156. pr_debug("from pipe %x cmd %x\n", pipe, cmd);
  157. if (pipe >= NFC_HCI_MAX_PIPES) {
  158. status = NFC_HCI_ANY_E_NOK;
  159. goto exit;
  160. }
  161. gate = hdev->pipes[pipe].gate;
  162. switch (cmd) {
  163. case NFC_HCI_ADM_NOTIFY_PIPE_CREATED:
  164. if (skb->len != 5) {
  165. status = NFC_HCI_ANY_E_NOK;
  166. goto exit;
  167. }
  168. create_info = (struct hci_create_pipe_resp *)skb->data;
  169. if (create_info->pipe >= NFC_HCI_MAX_PIPES) {
  170. status = NFC_HCI_ANY_E_NOK;
  171. goto exit;
  172. }
  173. /* Save the new created pipe and bind with local gate,
  174. * the description for skb->data[3] is destination gate id
  175. * but since we received this cmd from host controller, we
  176. * are the destination and it is our local gate
  177. */
  178. hdev->gate2pipe[create_info->dest_gate] = create_info->pipe;
  179. hdev->pipes[create_info->pipe].gate = create_info->dest_gate;
  180. hdev->pipes[create_info->pipe].dest_host =
  181. create_info->src_host;
  182. break;
  183. case NFC_HCI_ANY_OPEN_PIPE:
  184. if (gate == NFC_HCI_INVALID_GATE) {
  185. status = NFC_HCI_ANY_E_NOK;
  186. goto exit;
  187. }
  188. break;
  189. case NFC_HCI_ADM_NOTIFY_PIPE_DELETED:
  190. if (skb->len != 1) {
  191. status = NFC_HCI_ANY_E_NOK;
  192. goto exit;
  193. }
  194. delete_info = (struct hci_delete_pipe_noti *)skb->data;
  195. if (delete_info->pipe >= NFC_HCI_MAX_PIPES) {
  196. status = NFC_HCI_ANY_E_NOK;
  197. goto exit;
  198. }
  199. hdev->pipes[delete_info->pipe].gate = NFC_HCI_INVALID_GATE;
  200. hdev->pipes[delete_info->pipe].dest_host = NFC_HCI_INVALID_HOST;
  201. break;
  202. case NFC_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED:
  203. if (skb->len != 1) {
  204. status = NFC_HCI_ANY_E_NOK;
  205. goto exit;
  206. }
  207. cleared_info = (struct hci_all_pipe_cleared_noti *)skb->data;
  208. nfc_hci_reset_pipes_per_host(hdev, cleared_info->host);
  209. break;
  210. default:
  211. pr_info("Discarded unknown cmd %x to gate %x\n", cmd, gate);
  212. break;
  213. }
  214. if (hdev->ops->cmd_received)
  215. hdev->ops->cmd_received(hdev, pipe, cmd, skb);
  216. exit:
  217. nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE,
  218. status, NULL, 0, NULL, NULL, 0);
  219. kfree_skb(skb);
  220. }
  221. u32 nfc_hci_sak_to_protocol(u8 sak)
  222. {
  223. switch (NFC_HCI_TYPE_A_SEL_PROT(sak)) {
  224. case NFC_HCI_TYPE_A_SEL_PROT_MIFARE:
  225. return NFC_PROTO_MIFARE_MASK;
  226. case NFC_HCI_TYPE_A_SEL_PROT_ISO14443:
  227. return NFC_PROTO_ISO14443_MASK;
  228. case NFC_HCI_TYPE_A_SEL_PROT_DEP:
  229. return NFC_PROTO_NFC_DEP_MASK;
  230. case NFC_HCI_TYPE_A_SEL_PROT_ISO14443_DEP:
  231. return NFC_PROTO_ISO14443_MASK | NFC_PROTO_NFC_DEP_MASK;
  232. default:
  233. return 0xffffffff;
  234. }
  235. }
  236. EXPORT_SYMBOL(nfc_hci_sak_to_protocol);
  237. int nfc_hci_target_discovered(struct nfc_hci_dev *hdev, u8 gate)
  238. {
  239. struct nfc_target *targets;
  240. struct sk_buff *atqa_skb = NULL;
  241. struct sk_buff *sak_skb = NULL;
  242. struct sk_buff *uid_skb = NULL;
  243. int r;
  244. pr_debug("from gate %d\n", gate);
  245. targets = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
  246. if (targets == NULL)
  247. return -ENOMEM;
  248. switch (gate) {
  249. case NFC_HCI_RF_READER_A_GATE:
  250. r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
  251. NFC_HCI_RF_READER_A_ATQA, &atqa_skb);
  252. if (r < 0)
  253. goto exit;
  254. r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
  255. NFC_HCI_RF_READER_A_SAK, &sak_skb);
  256. if (r < 0)
  257. goto exit;
  258. if (atqa_skb->len != 2 || sak_skb->len != 1) {
  259. r = -EPROTO;
  260. goto exit;
  261. }
  262. targets->supported_protocols =
  263. nfc_hci_sak_to_protocol(sak_skb->data[0]);
  264. if (targets->supported_protocols == 0xffffffff) {
  265. r = -EPROTO;
  266. goto exit;
  267. }
  268. targets->sens_res = be16_to_cpu(*(__be16 *)atqa_skb->data);
  269. targets->sel_res = sak_skb->data[0];
  270. r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
  271. NFC_HCI_RF_READER_A_UID, &uid_skb);
  272. if (r < 0)
  273. goto exit;
  274. if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
  275. r = -EPROTO;
  276. goto exit;
  277. }
  278. memcpy(targets->nfcid1, uid_skb->data, uid_skb->len);
  279. targets->nfcid1_len = uid_skb->len;
  280. if (hdev->ops->complete_target_discovered) {
  281. r = hdev->ops->complete_target_discovered(hdev, gate,
  282. targets);
  283. if (r < 0)
  284. goto exit;
  285. }
  286. break;
  287. case NFC_HCI_RF_READER_B_GATE:
  288. targets->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
  289. break;
  290. default:
  291. if (hdev->ops->target_from_gate)
  292. r = hdev->ops->target_from_gate(hdev, gate, targets);
  293. else
  294. r = -EPROTO;
  295. if (r < 0)
  296. goto exit;
  297. if (hdev->ops->complete_target_discovered) {
  298. r = hdev->ops->complete_target_discovered(hdev, gate,
  299. targets);
  300. if (r < 0)
  301. goto exit;
  302. }
  303. break;
  304. }
  305. /* if driver set the new gate, we will skip the old one */
  306. if (targets->hci_reader_gate == 0x00)
  307. targets->hci_reader_gate = gate;
  308. r = nfc_targets_found(hdev->ndev, targets, 1);
  309. exit:
  310. kfree(targets);
  311. kfree_skb(atqa_skb);
  312. kfree_skb(sak_skb);
  313. kfree_skb(uid_skb);
  314. return r;
  315. }
  316. EXPORT_SYMBOL(nfc_hci_target_discovered);
  317. void nfc_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
  318. struct sk_buff *skb)
  319. {
  320. int r = 0;
  321. u8 gate;
  322. if (pipe >= NFC_HCI_MAX_PIPES) {
  323. pr_err("Discarded event %x to invalid pipe %x\n", event, pipe);
  324. goto exit;
  325. }
  326. gate = hdev->pipes[pipe].gate;
  327. if (gate == NFC_HCI_INVALID_GATE) {
  328. pr_err("Discarded event %x to unopened pipe %x\n", event, pipe);
  329. goto exit;
  330. }
  331. if (hdev->ops->event_received) {
  332. r = hdev->ops->event_received(hdev, pipe, event, skb);
  333. if (r <= 0)
  334. goto exit_noskb;
  335. }
  336. switch (event) {
  337. case NFC_HCI_EVT_TARGET_DISCOVERED:
  338. if (skb->len < 1) { /* no status data? */
  339. r = -EPROTO;
  340. goto exit;
  341. }
  342. if (skb->data[0] == 3) {
  343. /* TODO: Multiple targets in field, none activated
  344. * poll is supposedly stopped, but there is no
  345. * single target to activate, so nothing to report
  346. * up.
  347. * if we need to restart poll, we must save the
  348. * protocols from the initial poll and reuse here.
  349. */
  350. }
  351. if (skb->data[0] != 0) {
  352. r = -EPROTO;
  353. goto exit;
  354. }
  355. r = nfc_hci_target_discovered(hdev, gate);
  356. break;
  357. default:
  358. pr_info("Discarded unknown event %x to gate %x\n", event, gate);
  359. r = -EINVAL;
  360. break;
  361. }
  362. exit:
  363. kfree_skb(skb);
  364. exit_noskb:
  365. if (r)
  366. nfc_hci_driver_failure(hdev, r);
  367. }
  368. static void nfc_hci_cmd_timeout(struct timer_list *t)
  369. {
  370. struct nfc_hci_dev *hdev = from_timer(hdev, t, cmd_timer);
  371. schedule_work(&hdev->msg_tx_work);
  372. }
  373. static int hci_dev_connect_gates(struct nfc_hci_dev *hdev, u8 gate_count,
  374. struct nfc_hci_gate *gates)
  375. {
  376. int r;
  377. while (gate_count--) {
  378. r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
  379. gates->gate, gates->pipe);
  380. if (r < 0)
  381. return r;
  382. gates++;
  383. }
  384. return 0;
  385. }
  386. static int hci_dev_session_init(struct nfc_hci_dev *hdev)
  387. {
  388. struct sk_buff *skb = NULL;
  389. int r;
  390. if (hdev->init_data.gates[0].gate != NFC_HCI_ADMIN_GATE)
  391. return -EPROTO;
  392. r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
  393. hdev->init_data.gates[0].gate,
  394. hdev->init_data.gates[0].pipe);
  395. if (r < 0)
  396. goto exit;
  397. r = nfc_hci_get_param(hdev, NFC_HCI_ADMIN_GATE,
  398. NFC_HCI_ADMIN_SESSION_IDENTITY, &skb);
  399. if (r < 0)
  400. goto disconnect_all;
  401. if (skb->len && skb->len == strlen(hdev->init_data.session_id) &&
  402. (memcmp(hdev->init_data.session_id, skb->data,
  403. skb->len) == 0) && hdev->ops->load_session) {
  404. /* Restore gate<->pipe table from some proprietary location. */
  405. r = hdev->ops->load_session(hdev);
  406. if (r < 0)
  407. goto disconnect_all;
  408. } else {
  409. r = nfc_hci_disconnect_all_gates(hdev);
  410. if (r < 0)
  411. goto exit;
  412. r = hci_dev_connect_gates(hdev, hdev->init_data.gate_count,
  413. hdev->init_data.gates);
  414. if (r < 0)
  415. goto disconnect_all;
  416. r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
  417. NFC_HCI_ADMIN_SESSION_IDENTITY,
  418. hdev->init_data.session_id,
  419. strlen(hdev->init_data.session_id));
  420. }
  421. if (r == 0)
  422. goto exit;
  423. disconnect_all:
  424. nfc_hci_disconnect_all_gates(hdev);
  425. exit:
  426. kfree_skb(skb);
  427. return r;
  428. }
  429. static int hci_dev_version(struct nfc_hci_dev *hdev)
  430. {
  431. int r;
  432. struct sk_buff *skb;
  433. r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
  434. NFC_HCI_ID_MGMT_VERSION_SW, &skb);
  435. if (r == -EOPNOTSUPP) {
  436. pr_info("Software/Hardware info not available\n");
  437. return 0;
  438. }
  439. if (r < 0)
  440. return r;
  441. if (skb->len != 3) {
  442. kfree_skb(skb);
  443. return -EINVAL;
  444. }
  445. hdev->sw_romlib = (skb->data[0] & 0xf0) >> 4;
  446. hdev->sw_patch = skb->data[0] & 0x0f;
  447. hdev->sw_flashlib_major = skb->data[1];
  448. hdev->sw_flashlib_minor = skb->data[2];
  449. kfree_skb(skb);
  450. r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
  451. NFC_HCI_ID_MGMT_VERSION_HW, &skb);
  452. if (r < 0)
  453. return r;
  454. if (skb->len != 3) {
  455. kfree_skb(skb);
  456. return -EINVAL;
  457. }
  458. hdev->hw_derivative = (skb->data[0] & 0xe0) >> 5;
  459. hdev->hw_version = skb->data[0] & 0x1f;
  460. hdev->hw_mpw = (skb->data[1] & 0xc0) >> 6;
  461. hdev->hw_software = skb->data[1] & 0x3f;
  462. hdev->hw_bsid = skb->data[2];
  463. kfree_skb(skb);
  464. pr_info("SOFTWARE INFO:\n");
  465. pr_info("RomLib : %d\n", hdev->sw_romlib);
  466. pr_info("Patch : %d\n", hdev->sw_patch);
  467. pr_info("FlashLib Major : %d\n", hdev->sw_flashlib_major);
  468. pr_info("FlashLib Minor : %d\n", hdev->sw_flashlib_minor);
  469. pr_info("HARDWARE INFO:\n");
  470. pr_info("Derivative : %d\n", hdev->hw_derivative);
  471. pr_info("HW Version : %d\n", hdev->hw_version);
  472. pr_info("#MPW : %d\n", hdev->hw_mpw);
  473. pr_info("Software : %d\n", hdev->hw_software);
  474. pr_info("BSID Version : %d\n", hdev->hw_bsid);
  475. return 0;
  476. }
  477. static int hci_dev_up(struct nfc_dev *nfc_dev)
  478. {
  479. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  480. int r = 0;
  481. if (hdev->ops->open) {
  482. r = hdev->ops->open(hdev);
  483. if (r < 0)
  484. return r;
  485. }
  486. r = nfc_llc_start(hdev->llc);
  487. if (r < 0)
  488. goto exit_close;
  489. r = hci_dev_session_init(hdev);
  490. if (r < 0)
  491. goto exit_llc;
  492. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  493. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  494. if (r < 0)
  495. goto exit_llc;
  496. if (hdev->ops->hci_ready) {
  497. r = hdev->ops->hci_ready(hdev);
  498. if (r < 0)
  499. goto exit_llc;
  500. }
  501. r = hci_dev_version(hdev);
  502. if (r < 0)
  503. goto exit_llc;
  504. return 0;
  505. exit_llc:
  506. nfc_llc_stop(hdev->llc);
  507. exit_close:
  508. if (hdev->ops->close)
  509. hdev->ops->close(hdev);
  510. return r;
  511. }
  512. static int hci_dev_down(struct nfc_dev *nfc_dev)
  513. {
  514. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  515. nfc_llc_stop(hdev->llc);
  516. if (hdev->ops->close)
  517. hdev->ops->close(hdev);
  518. nfc_hci_reset_pipes(hdev);
  519. return 0;
  520. }
  521. static int hci_start_poll(struct nfc_dev *nfc_dev,
  522. u32 im_protocols, u32 tm_protocols)
  523. {
  524. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  525. if (hdev->ops->start_poll)
  526. return hdev->ops->start_poll(hdev, im_protocols, tm_protocols);
  527. else
  528. return nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  529. NFC_HCI_EVT_READER_REQUESTED,
  530. NULL, 0);
  531. }
  532. static void hci_stop_poll(struct nfc_dev *nfc_dev)
  533. {
  534. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  535. if (hdev->ops->stop_poll)
  536. hdev->ops->stop_poll(hdev);
  537. else
  538. nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  539. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  540. }
  541. static int hci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
  542. __u8 comm_mode, __u8 *gb, size_t gb_len)
  543. {
  544. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  545. if (!hdev->ops->dep_link_up)
  546. return 0;
  547. return hdev->ops->dep_link_up(hdev, target, comm_mode,
  548. gb, gb_len);
  549. }
  550. static int hci_dep_link_down(struct nfc_dev *nfc_dev)
  551. {
  552. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  553. if (!hdev->ops->dep_link_down)
  554. return 0;
  555. return hdev->ops->dep_link_down(hdev);
  556. }
  557. static int hci_activate_target(struct nfc_dev *nfc_dev,
  558. struct nfc_target *target, u32 protocol)
  559. {
  560. return 0;
  561. }
  562. static void hci_deactivate_target(struct nfc_dev *nfc_dev,
  563. struct nfc_target *target,
  564. u8 mode)
  565. {
  566. }
  567. #define HCI_CB_TYPE_TRANSCEIVE 1
  568. static void hci_transceive_cb(void *context, struct sk_buff *skb, int err)
  569. {
  570. struct nfc_hci_dev *hdev = context;
  571. switch (hdev->async_cb_type) {
  572. case HCI_CB_TYPE_TRANSCEIVE:
  573. /*
  574. * TODO: Check RF Error indicator to make sure data is valid.
  575. * It seems that HCI cmd can complete without error, but data
  576. * can be invalid if an RF error occured? Ignore for now.
  577. */
  578. if (err == 0)
  579. skb_trim(skb, skb->len - 1); /* RF Err ind */
  580. hdev->async_cb(hdev->async_cb_context, skb, err);
  581. break;
  582. default:
  583. if (err == 0)
  584. kfree_skb(skb);
  585. break;
  586. }
  587. }
  588. static int hci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
  589. struct sk_buff *skb, data_exchange_cb_t cb,
  590. void *cb_context)
  591. {
  592. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  593. int r;
  594. pr_debug("target_idx=%d\n", target->idx);
  595. switch (target->hci_reader_gate) {
  596. case NFC_HCI_RF_READER_A_GATE:
  597. case NFC_HCI_RF_READER_B_GATE:
  598. if (hdev->ops->im_transceive) {
  599. r = hdev->ops->im_transceive(hdev, target, skb, cb,
  600. cb_context);
  601. if (r <= 0) /* handled */
  602. break;
  603. }
  604. *(u8 *)skb_push(skb, 1) = 0; /* CTR, see spec:10.2.2.1 */
  605. hdev->async_cb_type = HCI_CB_TYPE_TRANSCEIVE;
  606. hdev->async_cb = cb;
  607. hdev->async_cb_context = cb_context;
  608. r = nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  609. NFC_HCI_WR_XCHG_DATA, skb->data,
  610. skb->len, hci_transceive_cb, hdev);
  611. break;
  612. default:
  613. if (hdev->ops->im_transceive) {
  614. r = hdev->ops->im_transceive(hdev, target, skb, cb,
  615. cb_context);
  616. if (r == 1)
  617. r = -ENOTSUPP;
  618. } else {
  619. r = -ENOTSUPP;
  620. }
  621. break;
  622. }
  623. kfree_skb(skb);
  624. return r;
  625. }
  626. static int hci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
  627. {
  628. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  629. if (!hdev->ops->tm_send) {
  630. kfree_skb(skb);
  631. return -ENOTSUPP;
  632. }
  633. return hdev->ops->tm_send(hdev, skb);
  634. }
  635. static int hci_check_presence(struct nfc_dev *nfc_dev,
  636. struct nfc_target *target)
  637. {
  638. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  639. if (!hdev->ops->check_presence)
  640. return 0;
  641. return hdev->ops->check_presence(hdev, target);
  642. }
  643. static int hci_discover_se(struct nfc_dev *nfc_dev)
  644. {
  645. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  646. if (hdev->ops->discover_se)
  647. return hdev->ops->discover_se(hdev);
  648. return 0;
  649. }
  650. static int hci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  651. {
  652. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  653. if (hdev->ops->enable_se)
  654. return hdev->ops->enable_se(hdev, se_idx);
  655. return 0;
  656. }
  657. static int hci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  658. {
  659. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  660. if (hdev->ops->disable_se)
  661. return hdev->ops->disable_se(hdev, se_idx);
  662. return 0;
  663. }
  664. static int hci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
  665. u8 *apdu, size_t apdu_length,
  666. se_io_cb_t cb, void *cb_context)
  667. {
  668. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  669. if (hdev->ops->se_io)
  670. return hdev->ops->se_io(hdev, se_idx, apdu,
  671. apdu_length, cb, cb_context);
  672. return 0;
  673. }
  674. static void nfc_hci_failure(struct nfc_hci_dev *hdev, int err)
  675. {
  676. mutex_lock(&hdev->msg_tx_mutex);
  677. if (hdev->cmd_pending_msg == NULL) {
  678. nfc_driver_failure(hdev->ndev, err);
  679. goto exit;
  680. }
  681. __nfc_hci_cmd_completion(hdev, err, NULL);
  682. exit:
  683. mutex_unlock(&hdev->msg_tx_mutex);
  684. }
  685. static void nfc_hci_llc_failure(struct nfc_hci_dev *hdev, int err)
  686. {
  687. nfc_hci_failure(hdev, err);
  688. }
  689. static void nfc_hci_recv_from_llc(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  690. {
  691. struct hcp_packet *packet;
  692. u8 type;
  693. u8 instruction;
  694. struct sk_buff *hcp_skb;
  695. u8 pipe;
  696. struct sk_buff *frag_skb;
  697. int msg_len;
  698. packet = (struct hcp_packet *)skb->data;
  699. if ((packet->header & ~NFC_HCI_FRAGMENT) == 0) {
  700. skb_queue_tail(&hdev->rx_hcp_frags, skb);
  701. return;
  702. }
  703. /* it's the last fragment. Does it need re-aggregation? */
  704. if (skb_queue_len(&hdev->rx_hcp_frags)) {
  705. pipe = packet->header & NFC_HCI_FRAGMENT;
  706. skb_queue_tail(&hdev->rx_hcp_frags, skb);
  707. msg_len = 0;
  708. skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
  709. msg_len += (frag_skb->len -
  710. NFC_HCI_HCP_PACKET_HEADER_LEN);
  711. }
  712. hcp_skb = nfc_alloc_recv_skb(NFC_HCI_HCP_PACKET_HEADER_LEN +
  713. msg_len, GFP_KERNEL);
  714. if (hcp_skb == NULL) {
  715. nfc_hci_failure(hdev, -ENOMEM);
  716. return;
  717. }
  718. skb_put_u8(hcp_skb, pipe);
  719. skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
  720. msg_len = frag_skb->len - NFC_HCI_HCP_PACKET_HEADER_LEN;
  721. skb_put_data(hcp_skb,
  722. frag_skb->data + NFC_HCI_HCP_PACKET_HEADER_LEN,
  723. msg_len);
  724. }
  725. skb_queue_purge(&hdev->rx_hcp_frags);
  726. } else {
  727. packet->header &= NFC_HCI_FRAGMENT;
  728. hcp_skb = skb;
  729. }
  730. /* if this is a response, dispatch immediately to
  731. * unblock waiting cmd context. Otherwise, enqueue to dispatch
  732. * in separate context where handler can also execute command.
  733. */
  734. packet = (struct hcp_packet *)hcp_skb->data;
  735. type = HCP_MSG_GET_TYPE(packet->message.header);
  736. if (type == NFC_HCI_HCP_RESPONSE) {
  737. pipe = packet->header;
  738. instruction = HCP_MSG_GET_CMD(packet->message.header);
  739. skb_pull(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN +
  740. NFC_HCI_HCP_MESSAGE_HEADER_LEN);
  741. nfc_hci_hcp_message_rx(hdev, pipe, type, instruction, hcp_skb);
  742. } else {
  743. skb_queue_tail(&hdev->msg_rx_queue, hcp_skb);
  744. schedule_work(&hdev->msg_rx_work);
  745. }
  746. }
  747. static int hci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
  748. {
  749. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  750. if (!hdev->ops->fw_download)
  751. return -ENOTSUPP;
  752. return hdev->ops->fw_download(hdev, firmware_name);
  753. }
  754. static struct nfc_ops hci_nfc_ops = {
  755. .dev_up = hci_dev_up,
  756. .dev_down = hci_dev_down,
  757. .start_poll = hci_start_poll,
  758. .stop_poll = hci_stop_poll,
  759. .dep_link_up = hci_dep_link_up,
  760. .dep_link_down = hci_dep_link_down,
  761. .activate_target = hci_activate_target,
  762. .deactivate_target = hci_deactivate_target,
  763. .im_transceive = hci_transceive,
  764. .tm_send = hci_tm_send,
  765. .check_presence = hci_check_presence,
  766. .fw_download = hci_fw_download,
  767. .discover_se = hci_discover_se,
  768. .enable_se = hci_enable_se,
  769. .disable_se = hci_disable_se,
  770. .se_io = hci_se_io,
  771. };
  772. struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
  773. struct nfc_hci_init_data *init_data,
  774. unsigned long quirks,
  775. u32 protocols,
  776. const char *llc_name,
  777. int tx_headroom,
  778. int tx_tailroom,
  779. int max_link_payload)
  780. {
  781. struct nfc_hci_dev *hdev;
  782. if (ops->xmit == NULL)
  783. return NULL;
  784. if (protocols == 0)
  785. return NULL;
  786. hdev = kzalloc(sizeof(struct nfc_hci_dev), GFP_KERNEL);
  787. if (hdev == NULL)
  788. return NULL;
  789. hdev->llc = nfc_llc_allocate(llc_name, hdev, ops->xmit,
  790. nfc_hci_recv_from_llc, tx_headroom,
  791. tx_tailroom, nfc_hci_llc_failure);
  792. if (hdev->llc == NULL) {
  793. kfree(hdev);
  794. return NULL;
  795. }
  796. hdev->ndev = nfc_allocate_device(&hci_nfc_ops, protocols,
  797. tx_headroom + HCI_CMDS_HEADROOM,
  798. tx_tailroom);
  799. if (!hdev->ndev) {
  800. nfc_llc_free(hdev->llc);
  801. kfree(hdev);
  802. return NULL;
  803. }
  804. hdev->ops = ops;
  805. hdev->max_data_link_payload = max_link_payload;
  806. hdev->init_data = *init_data;
  807. nfc_set_drvdata(hdev->ndev, hdev);
  808. nfc_hci_reset_pipes(hdev);
  809. hdev->quirks = quirks;
  810. return hdev;
  811. }
  812. EXPORT_SYMBOL(nfc_hci_allocate_device);
  813. void nfc_hci_free_device(struct nfc_hci_dev *hdev)
  814. {
  815. nfc_free_device(hdev->ndev);
  816. nfc_llc_free(hdev->llc);
  817. kfree(hdev);
  818. }
  819. EXPORT_SYMBOL(nfc_hci_free_device);
  820. int nfc_hci_register_device(struct nfc_hci_dev *hdev)
  821. {
  822. mutex_init(&hdev->msg_tx_mutex);
  823. INIT_LIST_HEAD(&hdev->msg_tx_queue);
  824. INIT_WORK(&hdev->msg_tx_work, nfc_hci_msg_tx_work);
  825. timer_setup(&hdev->cmd_timer, nfc_hci_cmd_timeout, 0);
  826. skb_queue_head_init(&hdev->rx_hcp_frags);
  827. INIT_WORK(&hdev->msg_rx_work, nfc_hci_msg_rx_work);
  828. skb_queue_head_init(&hdev->msg_rx_queue);
  829. return nfc_register_device(hdev->ndev);
  830. }
  831. EXPORT_SYMBOL(nfc_hci_register_device);
  832. void nfc_hci_unregister_device(struct nfc_hci_dev *hdev)
  833. {
  834. struct hci_msg *msg, *n;
  835. mutex_lock(&hdev->msg_tx_mutex);
  836. if (hdev->cmd_pending_msg) {
  837. if (hdev->cmd_pending_msg->cb)
  838. hdev->cmd_pending_msg->cb(
  839. hdev->cmd_pending_msg->cb_context,
  840. NULL, -ESHUTDOWN);
  841. kfree(hdev->cmd_pending_msg);
  842. hdev->cmd_pending_msg = NULL;
  843. }
  844. hdev->shutting_down = true;
  845. mutex_unlock(&hdev->msg_tx_mutex);
  846. del_timer_sync(&hdev->cmd_timer);
  847. cancel_work_sync(&hdev->msg_tx_work);
  848. cancel_work_sync(&hdev->msg_rx_work);
  849. nfc_unregister_device(hdev->ndev);
  850. skb_queue_purge(&hdev->rx_hcp_frags);
  851. skb_queue_purge(&hdev->msg_rx_queue);
  852. list_for_each_entry_safe(msg, n, &hdev->msg_tx_queue, msg_l) {
  853. list_del(&msg->msg_l);
  854. skb_queue_purge(&msg->msg_frags);
  855. kfree(msg);
  856. }
  857. }
  858. EXPORT_SYMBOL(nfc_hci_unregister_device);
  859. void nfc_hci_set_clientdata(struct nfc_hci_dev *hdev, void *clientdata)
  860. {
  861. hdev->clientdata = clientdata;
  862. }
  863. EXPORT_SYMBOL(nfc_hci_set_clientdata);
  864. void *nfc_hci_get_clientdata(struct nfc_hci_dev *hdev)
  865. {
  866. return hdev->clientdata;
  867. }
  868. EXPORT_SYMBOL(nfc_hci_get_clientdata);
  869. void nfc_hci_driver_failure(struct nfc_hci_dev *hdev, int err)
  870. {
  871. nfc_hci_failure(hdev, err);
  872. }
  873. EXPORT_SYMBOL(nfc_hci_driver_failure);
  874. void nfc_hci_recv_frame(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  875. {
  876. nfc_llc_rcv_from_drv(hdev->llc, skb);
  877. }
  878. EXPORT_SYMBOL(nfc_hci_recv_frame);
  879. static int __init nfc_hci_init(void)
  880. {
  881. return nfc_llc_init();
  882. }
  883. static void __exit nfc_hci_exit(void)
  884. {
  885. nfc_llc_exit();
  886. }
  887. subsys_initcall(nfc_hci_init);
  888. module_exit(nfc_hci_exit);
  889. MODULE_LICENSE("GPL");
  890. MODULE_DESCRIPTION("NFC HCI Core");