core.c 21 KB

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