core.c 20 KB

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