core.c 25 KB

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