core.c 19 KB

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