core.c 24 KB

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