core.c 23 KB

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