core.c 19 KB

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