core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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 int nfc_hci_result_to_errno(u8 result)
  30. {
  31. switch (result) {
  32. case NFC_HCI_ANY_OK:
  33. return 0;
  34. case NFC_HCI_ANY_E_TIMEOUT:
  35. return -ETIME;
  36. default:
  37. return -1;
  38. }
  39. }
  40. static void nfc_hci_msg_tx_work(struct work_struct *work)
  41. {
  42. struct nfc_hci_dev *hdev = container_of(work, struct nfc_hci_dev,
  43. msg_tx_work);
  44. struct hci_msg *msg;
  45. struct sk_buff *skb;
  46. int r = 0;
  47. mutex_lock(&hdev->msg_tx_mutex);
  48. if (hdev->cmd_pending_msg) {
  49. if (timer_pending(&hdev->cmd_timer) == 0) {
  50. if (hdev->cmd_pending_msg->cb)
  51. hdev->cmd_pending_msg->cb(hdev,
  52. -ETIME,
  53. NULL,
  54. hdev->
  55. cmd_pending_msg->
  56. cb_context);
  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 = hdev->ops->xmit(hdev, skb);
  70. if (r < 0) {
  71. kfree_skb(skb);
  72. skb_queue_purge(&msg->msg_frags);
  73. if (msg->cb)
  74. msg->cb(hdev, r, NULL, msg->cb_context);
  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, err, skb,
  116. hdev->cmd_pending_msg->cb_context);
  117. else
  118. kfree_skb(skb);
  119. kfree(hdev->cmd_pending_msg);
  120. hdev->cmd_pending_msg = NULL;
  121. queue_work(hdev->msg_tx_wq, &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. int r;
  161. pr_debug("from gate %d\n", gate);
  162. targets = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
  163. if (targets == NULL)
  164. return -ENOMEM;
  165. switch (gate) {
  166. case NFC_HCI_RF_READER_A_GATE:
  167. r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
  168. NFC_HCI_RF_READER_A_ATQA, &atqa_skb);
  169. if (r < 0)
  170. goto exit;
  171. r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
  172. NFC_HCI_RF_READER_A_SAK, &sak_skb);
  173. if (r < 0)
  174. goto exit;
  175. if (atqa_skb->len != 2 || sak_skb->len != 1) {
  176. r = -EPROTO;
  177. goto exit;
  178. }
  179. targets->supported_protocols =
  180. nfc_hci_sak_to_protocol(sak_skb->data[0]);
  181. if (targets->supported_protocols == 0xffffffff) {
  182. r = -EPROTO;
  183. goto exit;
  184. }
  185. targets->sens_res = be16_to_cpu(*(u16 *)atqa_skb->data);
  186. targets->sel_res = sak_skb->data[0];
  187. if (hdev->ops->complete_target_discovered) {
  188. r = hdev->ops->complete_target_discovered(hdev, gate,
  189. targets);
  190. if (r < 0)
  191. goto exit;
  192. }
  193. break;
  194. case NFC_HCI_RF_READER_B_GATE:
  195. targets->supported_protocols = NFC_PROTO_ISO14443_MASK;
  196. break;
  197. default:
  198. if (hdev->ops->target_from_gate)
  199. r = hdev->ops->target_from_gate(hdev, gate, targets);
  200. else
  201. r = -EPROTO;
  202. if (r < 0)
  203. goto exit;
  204. if (hdev->ops->complete_target_discovered) {
  205. r = hdev->ops->complete_target_discovered(hdev, gate,
  206. targets);
  207. if (r < 0)
  208. goto exit;
  209. }
  210. break;
  211. }
  212. targets->hci_reader_gate = gate;
  213. r = nfc_targets_found(hdev->ndev, targets, 1);
  214. exit:
  215. kfree(targets);
  216. kfree_skb(atqa_skb);
  217. kfree_skb(sak_skb);
  218. return r;
  219. }
  220. void nfc_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
  221. struct sk_buff *skb)
  222. {
  223. int r = 0;
  224. switch (event) {
  225. case NFC_HCI_EVT_TARGET_DISCOVERED:
  226. if (skb->len < 1) { /* no status data? */
  227. r = -EPROTO;
  228. goto exit;
  229. }
  230. if (skb->data[0] == 3) {
  231. /* TODO: Multiple targets in field, none activated
  232. * poll is supposedly stopped, but there is no
  233. * single target to activate, so nothing to report
  234. * up.
  235. * if we need to restart poll, we must save the
  236. * protocols from the initial poll and reuse here.
  237. */
  238. }
  239. if (skb->data[0] != 0) {
  240. r = -EPROTO;
  241. goto exit;
  242. }
  243. r = nfc_hci_target_discovered(hdev,
  244. nfc_hci_pipe2gate(hdev, pipe));
  245. break;
  246. default:
  247. /* TODO: Unknown events are hardware specific
  248. * pass them to the driver (needs a new hci_ops) */
  249. break;
  250. }
  251. exit:
  252. kfree_skb(skb);
  253. if (r) {
  254. /* TODO: There was an error dispatching the event,
  255. * how to propagate up to nfc core?
  256. */
  257. }
  258. }
  259. static void nfc_hci_cmd_timeout(unsigned long data)
  260. {
  261. struct nfc_hci_dev *hdev = (struct nfc_hci_dev *)data;
  262. queue_work(hdev->msg_tx_wq, &hdev->msg_tx_work);
  263. }
  264. static int hci_dev_connect_gates(struct nfc_hci_dev *hdev, u8 gate_count,
  265. struct nfc_hci_gate *gates)
  266. {
  267. int r;
  268. while (gate_count--) {
  269. r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
  270. gates->gate, gates->pipe);
  271. if (r < 0)
  272. return r;
  273. gates++;
  274. }
  275. return 0;
  276. }
  277. static int hci_dev_session_init(struct nfc_hci_dev *hdev)
  278. {
  279. struct sk_buff *skb = NULL;
  280. int r;
  281. if (hdev->init_data.gates[0].gate != NFC_HCI_ADMIN_GATE)
  282. return -EPROTO;
  283. r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
  284. hdev->init_data.gates[0].gate,
  285. hdev->init_data.gates[0].pipe);
  286. if (r < 0)
  287. goto exit;
  288. r = nfc_hci_get_param(hdev, NFC_HCI_ADMIN_GATE,
  289. NFC_HCI_ADMIN_SESSION_IDENTITY, &skb);
  290. if (r < 0)
  291. goto disconnect_all;
  292. if (skb->len && skb->len == strlen(hdev->init_data.session_id))
  293. if (memcmp(hdev->init_data.session_id, skb->data,
  294. skb->len) == 0) {
  295. /* TODO ELa: restore gate<->pipe table from
  296. * some TBD location.
  297. * note: it doesn't seem possible to get the chip
  298. * currently open gate/pipe table.
  299. * It is only possible to obtain the supported
  300. * gate list.
  301. */
  302. /* goto exit
  303. * For now, always do a full initialization */
  304. }
  305. r = nfc_hci_disconnect_all_gates(hdev);
  306. if (r < 0)
  307. goto exit;
  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,
  408. u32 im_protocols, u32 tm_protocols)
  409. {
  410. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  411. if (hdev->ops->start_poll)
  412. return hdev->ops->start_poll(hdev, im_protocols, tm_protocols);
  413. else
  414. return nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  415. NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
  416. }
  417. static void hci_stop_poll(struct nfc_dev *nfc_dev)
  418. {
  419. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  420. nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  421. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  422. }
  423. static int hci_activate_target(struct nfc_dev *nfc_dev,
  424. struct nfc_target *target, u32 protocol)
  425. {
  426. return 0;
  427. }
  428. static void hci_deactivate_target(struct nfc_dev *nfc_dev,
  429. struct nfc_target *target)
  430. {
  431. }
  432. static int hci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
  433. struct sk_buff *skb, data_exchange_cb_t cb,
  434. void *cb_context)
  435. {
  436. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  437. int r;
  438. struct sk_buff *res_skb = NULL;
  439. pr_debug("target_idx=%d\n", target->idx);
  440. switch (target->hci_reader_gate) {
  441. case NFC_HCI_RF_READER_A_GATE:
  442. case NFC_HCI_RF_READER_B_GATE:
  443. if (hdev->ops->data_exchange) {
  444. r = hdev->ops->data_exchange(hdev, target, skb,
  445. &res_skb);
  446. if (r <= 0) /* handled */
  447. break;
  448. }
  449. *skb_push(skb, 1) = 0; /* CTR, see spec:10.2.2.1 */
  450. r = nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  451. NFC_HCI_WR_XCHG_DATA,
  452. skb->data, skb->len, &res_skb);
  453. /*
  454. * TODO: Check RF Error indicator to make sure data is valid.
  455. * It seems that HCI cmd can complete without error, but data
  456. * can be invalid if an RF error occured? Ignore for now.
  457. */
  458. if (r == 0)
  459. skb_trim(res_skb, res_skb->len - 1); /* RF Err ind */
  460. break;
  461. default:
  462. if (hdev->ops->data_exchange) {
  463. r = hdev->ops->data_exchange(hdev, target, skb,
  464. &res_skb);
  465. if (r == 1)
  466. r = -ENOTSUPP;
  467. }
  468. else
  469. r = -ENOTSUPP;
  470. }
  471. kfree_skb(skb);
  472. cb(cb_context, res_skb, r);
  473. return 0;
  474. }
  475. static int hci_check_presence(struct nfc_dev *nfc_dev,
  476. struct nfc_target *target)
  477. {
  478. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  479. if (hdev->ops->check_presence)
  480. return hdev->ops->check_presence(hdev, target);
  481. return 0;
  482. }
  483. static struct nfc_ops hci_nfc_ops = {
  484. .dev_up = hci_dev_up,
  485. .dev_down = hci_dev_down,
  486. .start_poll = hci_start_poll,
  487. .stop_poll = hci_stop_poll,
  488. .activate_target = hci_activate_target,
  489. .deactivate_target = hci_deactivate_target,
  490. .im_transceive = hci_transceive,
  491. .check_presence = hci_check_presence,
  492. };
  493. struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
  494. struct nfc_hci_init_data *init_data,
  495. u32 protocols,
  496. int tx_headroom,
  497. int tx_tailroom,
  498. int max_link_payload)
  499. {
  500. struct nfc_hci_dev *hdev;
  501. if (ops->xmit == NULL)
  502. return NULL;
  503. if (protocols == 0)
  504. return NULL;
  505. hdev = kzalloc(sizeof(struct nfc_hci_dev), GFP_KERNEL);
  506. if (hdev == NULL)
  507. return NULL;
  508. hdev->ndev = nfc_allocate_device(&hci_nfc_ops, protocols,
  509. tx_headroom + HCI_CMDS_HEADROOM,
  510. tx_tailroom);
  511. if (!hdev->ndev) {
  512. kfree(hdev);
  513. return NULL;
  514. }
  515. hdev->ops = ops;
  516. hdev->max_data_link_payload = max_link_payload;
  517. hdev->init_data = *init_data;
  518. nfc_set_drvdata(hdev->ndev, hdev);
  519. memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
  520. return hdev;
  521. }
  522. EXPORT_SYMBOL(nfc_hci_allocate_device);
  523. void nfc_hci_free_device(struct nfc_hci_dev *hdev)
  524. {
  525. nfc_free_device(hdev->ndev);
  526. kfree(hdev);
  527. }
  528. EXPORT_SYMBOL(nfc_hci_free_device);
  529. int nfc_hci_register_device(struct nfc_hci_dev *hdev)
  530. {
  531. struct device *dev = &hdev->ndev->dev;
  532. const char *devname = dev_name(dev);
  533. char name[32];
  534. int r = 0;
  535. mutex_init(&hdev->msg_tx_mutex);
  536. INIT_LIST_HEAD(&hdev->msg_tx_queue);
  537. INIT_WORK(&hdev->msg_tx_work, nfc_hci_msg_tx_work);
  538. snprintf(name, sizeof(name), "%s_hci_msg_tx_wq", devname);
  539. hdev->msg_tx_wq = alloc_workqueue(name, WQ_NON_REENTRANT | WQ_UNBOUND |
  540. WQ_MEM_RECLAIM, 1);
  541. if (hdev->msg_tx_wq == NULL) {
  542. r = -ENOMEM;
  543. goto exit;
  544. }
  545. init_timer(&hdev->cmd_timer);
  546. hdev->cmd_timer.data = (unsigned long)hdev;
  547. hdev->cmd_timer.function = nfc_hci_cmd_timeout;
  548. skb_queue_head_init(&hdev->rx_hcp_frags);
  549. INIT_WORK(&hdev->msg_rx_work, nfc_hci_msg_rx_work);
  550. snprintf(name, sizeof(name), "%s_hci_msg_rx_wq", devname);
  551. hdev->msg_rx_wq = alloc_workqueue(name, WQ_NON_REENTRANT | WQ_UNBOUND |
  552. WQ_MEM_RECLAIM, 1);
  553. if (hdev->msg_rx_wq == NULL) {
  554. r = -ENOMEM;
  555. goto exit;
  556. }
  557. skb_queue_head_init(&hdev->msg_rx_queue);
  558. r = nfc_register_device(hdev->ndev);
  559. exit:
  560. if (r < 0) {
  561. if (hdev->msg_tx_wq)
  562. destroy_workqueue(hdev->msg_tx_wq);
  563. if (hdev->msg_rx_wq)
  564. destroy_workqueue(hdev->msg_rx_wq);
  565. }
  566. return r;
  567. }
  568. EXPORT_SYMBOL(nfc_hci_register_device);
  569. void nfc_hci_unregister_device(struct nfc_hci_dev *hdev)
  570. {
  571. struct hci_msg *msg;
  572. skb_queue_purge(&hdev->rx_hcp_frags);
  573. skb_queue_purge(&hdev->msg_rx_queue);
  574. while ((msg = list_first_entry(&hdev->msg_tx_queue, struct hci_msg,
  575. msg_l)) != NULL) {
  576. list_del(&msg->msg_l);
  577. skb_queue_purge(&msg->msg_frags);
  578. kfree(msg);
  579. }
  580. del_timer_sync(&hdev->cmd_timer);
  581. nfc_unregister_device(hdev->ndev);
  582. destroy_workqueue(hdev->msg_tx_wq);
  583. destroy_workqueue(hdev->msg_rx_wq);
  584. }
  585. EXPORT_SYMBOL(nfc_hci_unregister_device);
  586. void nfc_hci_set_clientdata(struct nfc_hci_dev *hdev, void *clientdata)
  587. {
  588. hdev->clientdata = clientdata;
  589. }
  590. EXPORT_SYMBOL(nfc_hci_set_clientdata);
  591. void *nfc_hci_get_clientdata(struct nfc_hci_dev *hdev)
  592. {
  593. return hdev->clientdata;
  594. }
  595. EXPORT_SYMBOL(nfc_hci_get_clientdata);
  596. static void nfc_hci_failure(struct nfc_hci_dev *hdev, int err)
  597. {
  598. mutex_lock(&hdev->msg_tx_mutex);
  599. if (hdev->cmd_pending_msg == NULL) {
  600. nfc_driver_failure(hdev->ndev, err);
  601. goto exit;
  602. }
  603. __nfc_hci_cmd_completion(hdev, err, NULL);
  604. exit:
  605. mutex_unlock(&hdev->msg_tx_mutex);
  606. }
  607. void nfc_hci_driver_failure(struct nfc_hci_dev *hdev, int err)
  608. {
  609. nfc_hci_failure(hdev, err);
  610. }
  611. EXPORT_SYMBOL(nfc_hci_driver_failure);
  612. void nfc_hci_recv_frame(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  613. {
  614. struct hcp_packet *packet;
  615. u8 type;
  616. u8 instruction;
  617. struct sk_buff *hcp_skb;
  618. u8 pipe;
  619. struct sk_buff *frag_skb;
  620. int msg_len;
  621. packet = (struct hcp_packet *)skb->data;
  622. if ((packet->header & ~NFC_HCI_FRAGMENT) == 0) {
  623. skb_queue_tail(&hdev->rx_hcp_frags, skb);
  624. return;
  625. }
  626. /* it's the last fragment. Does it need re-aggregation? */
  627. if (skb_queue_len(&hdev->rx_hcp_frags)) {
  628. pipe = packet->header & NFC_HCI_FRAGMENT;
  629. skb_queue_tail(&hdev->rx_hcp_frags, skb);
  630. msg_len = 0;
  631. skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
  632. msg_len += (frag_skb->len -
  633. NFC_HCI_HCP_PACKET_HEADER_LEN);
  634. }
  635. hcp_skb = nfc_alloc_recv_skb(NFC_HCI_HCP_PACKET_HEADER_LEN +
  636. msg_len, GFP_KERNEL);
  637. if (hcp_skb == NULL) {
  638. nfc_hci_failure(hdev, -ENOMEM);
  639. return;
  640. }
  641. *skb_put(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN) = pipe;
  642. skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
  643. msg_len = frag_skb->len - NFC_HCI_HCP_PACKET_HEADER_LEN;
  644. memcpy(skb_put(hcp_skb, msg_len),
  645. frag_skb->data + NFC_HCI_HCP_PACKET_HEADER_LEN,
  646. msg_len);
  647. }
  648. skb_queue_purge(&hdev->rx_hcp_frags);
  649. } else {
  650. packet->header &= NFC_HCI_FRAGMENT;
  651. hcp_skb = skb;
  652. }
  653. /* if this is a response, dispatch immediately to
  654. * unblock waiting cmd context. Otherwise, enqueue to dispatch
  655. * in separate context where handler can also execute command.
  656. */
  657. packet = (struct hcp_packet *)hcp_skb->data;
  658. type = HCP_MSG_GET_TYPE(packet->message.header);
  659. if (type == NFC_HCI_HCP_RESPONSE) {
  660. pipe = packet->header;
  661. instruction = HCP_MSG_GET_CMD(packet->message.header);
  662. skb_pull(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN +
  663. NFC_HCI_HCP_MESSAGE_HEADER_LEN);
  664. nfc_hci_hcp_message_rx(hdev, pipe, type, instruction, hcp_skb);
  665. } else {
  666. skb_queue_tail(&hdev->msg_rx_queue, hcp_skb);
  667. queue_work(hdev->msg_rx_wq, &hdev->msg_rx_work);
  668. }
  669. }
  670. EXPORT_SYMBOL(nfc_hci_recv_frame);
  671. MODULE_LICENSE("GPL");