core.c 25 KB

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