core.c 23 KB

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