core.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #define pr_fmt(fmt) "hci: %s: " fmt, __func__
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/nfc.h>
  22. #include <net/nfc/nfc.h>
  23. #include <net/nfc/hci.h>
  24. #include <net/nfc/llc.h>
  25. #include "hci.h"
  26. /* Largest headroom needed for outgoing HCI commands */
  27. #define HCI_CMDS_HEADROOM 1
  28. int nfc_hci_result_to_errno(u8 result)
  29. {
  30. switch (result) {
  31. case NFC_HCI_ANY_OK:
  32. return 0;
  33. case NFC_HCI_ANY_E_REG_PAR_UNKNOWN:
  34. return -EOPNOTSUPP;
  35. case NFC_HCI_ANY_E_TIMEOUT:
  36. return -ETIME;
  37. default:
  38. return -1;
  39. }
  40. }
  41. EXPORT_SYMBOL(nfc_hci_result_to_errno);
  42. void nfc_hci_reset_pipes(struct nfc_hci_dev *hdev)
  43. {
  44. int i = 0;
  45. for (i = 0; i < NFC_HCI_MAX_PIPES; i++) {
  46. hdev->pipes[i].gate = NFC_HCI_INVALID_GATE;
  47. hdev->pipes[i].dest_host = NFC_HCI_INVALID_HOST;
  48. }
  49. memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
  50. }
  51. EXPORT_SYMBOL(nfc_hci_reset_pipes);
  52. void nfc_hci_reset_pipes_per_host(struct nfc_hci_dev *hdev, u8 host)
  53. {
  54. int i = 0;
  55. for (i = 0; i < NFC_HCI_MAX_PIPES; i++) {
  56. if (hdev->pipes[i].dest_host != host)
  57. continue;
  58. hdev->pipes[i].gate = NFC_HCI_INVALID_GATE;
  59. hdev->pipes[i].dest_host = NFC_HCI_INVALID_HOST;
  60. }
  61. }
  62. EXPORT_SYMBOL(nfc_hci_reset_pipes_per_host);
  63. static void nfc_hci_msg_tx_work(struct work_struct *work)
  64. {
  65. struct nfc_hci_dev *hdev = container_of(work, struct nfc_hci_dev,
  66. msg_tx_work);
  67. struct hci_msg *msg;
  68. struct sk_buff *skb;
  69. int r = 0;
  70. mutex_lock(&hdev->msg_tx_mutex);
  71. if (hdev->shutting_down)
  72. goto exit;
  73. if (hdev->cmd_pending_msg) {
  74. if (timer_pending(&hdev->cmd_timer) == 0) {
  75. if (hdev->cmd_pending_msg->cb)
  76. hdev->cmd_pending_msg->cb(hdev->
  77. cmd_pending_msg->
  78. cb_context,
  79. NULL,
  80. -ETIME);
  81. kfree(hdev->cmd_pending_msg);
  82. hdev->cmd_pending_msg = NULL;
  83. } else {
  84. goto exit;
  85. }
  86. }
  87. next_msg:
  88. if (list_empty(&hdev->msg_tx_queue))
  89. goto exit;
  90. msg = list_first_entry(&hdev->msg_tx_queue, struct hci_msg, msg_l);
  91. list_del(&msg->msg_l);
  92. pr_debug("msg_tx_queue has a cmd to send\n");
  93. while ((skb = skb_dequeue(&msg->msg_frags)) != NULL) {
  94. r = nfc_llc_xmit_from_hci(hdev->llc, skb);
  95. if (r < 0) {
  96. kfree_skb(skb);
  97. skb_queue_purge(&msg->msg_frags);
  98. if (msg->cb)
  99. msg->cb(msg->cb_context, NULL, r);
  100. kfree(msg);
  101. break;
  102. }
  103. }
  104. if (r)
  105. goto next_msg;
  106. if (msg->wait_response == false) {
  107. kfree(msg);
  108. goto next_msg;
  109. }
  110. hdev->cmd_pending_msg = msg;
  111. mod_timer(&hdev->cmd_timer, jiffies +
  112. msecs_to_jiffies(hdev->cmd_pending_msg->completion_delay));
  113. exit:
  114. mutex_unlock(&hdev->msg_tx_mutex);
  115. }
  116. static void nfc_hci_msg_rx_work(struct work_struct *work)
  117. {
  118. struct nfc_hci_dev *hdev = container_of(work, struct nfc_hci_dev,
  119. msg_rx_work);
  120. struct sk_buff *skb;
  121. struct hcp_message *message;
  122. u8 pipe;
  123. u8 type;
  124. u8 instruction;
  125. while ((skb = skb_dequeue(&hdev->msg_rx_queue)) != NULL) {
  126. pipe = skb->data[0];
  127. skb_pull(skb, NFC_HCI_HCP_PACKET_HEADER_LEN);
  128. message = (struct hcp_message *)skb->data;
  129. type = HCP_MSG_GET_TYPE(message->header);
  130. instruction = HCP_MSG_GET_CMD(message->header);
  131. skb_pull(skb, NFC_HCI_HCP_MESSAGE_HEADER_LEN);
  132. nfc_hci_hcp_message_rx(hdev, pipe, type, instruction, skb);
  133. }
  134. }
  135. static void __nfc_hci_cmd_completion(struct nfc_hci_dev *hdev, int err,
  136. struct sk_buff *skb)
  137. {
  138. del_timer_sync(&hdev->cmd_timer);
  139. if (hdev->cmd_pending_msg->cb)
  140. hdev->cmd_pending_msg->cb(hdev->cmd_pending_msg->cb_context,
  141. skb, err);
  142. else
  143. kfree_skb(skb);
  144. kfree(hdev->cmd_pending_msg);
  145. hdev->cmd_pending_msg = NULL;
  146. schedule_work(&hdev->msg_tx_work);
  147. }
  148. void nfc_hci_resp_received(struct nfc_hci_dev *hdev, u8 result,
  149. struct sk_buff *skb)
  150. {
  151. mutex_lock(&hdev->msg_tx_mutex);
  152. if (hdev->cmd_pending_msg == NULL) {
  153. kfree_skb(skb);
  154. goto exit;
  155. }
  156. __nfc_hci_cmd_completion(hdev, nfc_hci_result_to_errno(result), skb);
  157. exit:
  158. mutex_unlock(&hdev->msg_tx_mutex);
  159. }
  160. void nfc_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
  161. struct sk_buff *skb)
  162. {
  163. u8 gate = hdev->pipes[pipe].gate;
  164. u8 status = NFC_HCI_ANY_OK;
  165. struct hci_create_pipe_resp *create_info;
  166. struct hci_delete_pipe_noti *delete_info;
  167. struct hci_all_pipe_cleared_noti *cleared_info;
  168. pr_debug("from gate %x pipe %x cmd %x\n", gate, pipe, cmd);
  169. switch (cmd) {
  170. case NFC_HCI_ADM_NOTIFY_PIPE_CREATED:
  171. if (skb->len != 5) {
  172. status = NFC_HCI_ANY_E_NOK;
  173. goto exit;
  174. }
  175. create_info = (struct hci_create_pipe_resp *)skb->data;
  176. if (create_info->pipe >= NFC_HCI_MAX_PIPES) {
  177. status = NFC_HCI_ANY_E_NOK;
  178. goto exit;
  179. }
  180. /* Save the new created pipe and bind with local gate,
  181. * the description for skb->data[3] is destination gate id
  182. * but since we received this cmd from host controller, we
  183. * are the destination and it is our local gate
  184. */
  185. hdev->gate2pipe[create_info->dest_gate] = create_info->pipe;
  186. hdev->pipes[create_info->pipe].gate = create_info->dest_gate;
  187. hdev->pipes[create_info->pipe].dest_host =
  188. create_info->src_host;
  189. break;
  190. case NFC_HCI_ANY_OPEN_PIPE:
  191. if (gate == NFC_HCI_INVALID_GATE) {
  192. status = NFC_HCI_ANY_E_NOK;
  193. goto exit;
  194. }
  195. break;
  196. case NFC_HCI_ADM_NOTIFY_PIPE_DELETED:
  197. if (skb->len != 1) {
  198. status = NFC_HCI_ANY_E_NOK;
  199. goto exit;
  200. }
  201. delete_info = (struct hci_delete_pipe_noti *)skb->data;
  202. if (delete_info->pipe >= NFC_HCI_MAX_PIPES) {
  203. status = NFC_HCI_ANY_E_NOK;
  204. goto exit;
  205. }
  206. hdev->pipes[delete_info->pipe].gate = NFC_HCI_INVALID_GATE;
  207. hdev->pipes[delete_info->pipe].dest_host = NFC_HCI_INVALID_HOST;
  208. break;
  209. case NFC_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED:
  210. if (skb->len != 1) {
  211. status = NFC_HCI_ANY_E_NOK;
  212. goto exit;
  213. }
  214. cleared_info = (struct hci_all_pipe_cleared_noti *)skb->data;
  215. nfc_hci_reset_pipes_per_host(hdev, cleared_info->host);
  216. break;
  217. default:
  218. pr_info("Discarded unknown cmd %x to gate %x\n", cmd, gate);
  219. break;
  220. }
  221. if (hdev->ops->cmd_received)
  222. hdev->ops->cmd_received(hdev, pipe, cmd, skb);
  223. exit:
  224. nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE,
  225. status, NULL, 0, NULL, NULL, 0);
  226. kfree_skb(skb);
  227. }
  228. u32 nfc_hci_sak_to_protocol(u8 sak)
  229. {
  230. switch (NFC_HCI_TYPE_A_SEL_PROT(sak)) {
  231. case NFC_HCI_TYPE_A_SEL_PROT_MIFARE:
  232. return NFC_PROTO_MIFARE_MASK;
  233. case NFC_HCI_TYPE_A_SEL_PROT_ISO14443:
  234. return NFC_PROTO_ISO14443_MASK;
  235. case NFC_HCI_TYPE_A_SEL_PROT_DEP:
  236. return NFC_PROTO_NFC_DEP_MASK;
  237. case NFC_HCI_TYPE_A_SEL_PROT_ISO14443_DEP:
  238. return NFC_PROTO_ISO14443_MASK | NFC_PROTO_NFC_DEP_MASK;
  239. default:
  240. return 0xffffffff;
  241. }
  242. }
  243. EXPORT_SYMBOL(nfc_hci_sak_to_protocol);
  244. int nfc_hci_target_discovered(struct nfc_hci_dev *hdev, u8 gate)
  245. {
  246. struct nfc_target *targets;
  247. struct sk_buff *atqa_skb = NULL;
  248. struct sk_buff *sak_skb = NULL;
  249. struct sk_buff *uid_skb = NULL;
  250. int r;
  251. pr_debug("from gate %d\n", gate);
  252. targets = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
  253. if (targets == NULL)
  254. return -ENOMEM;
  255. switch (gate) {
  256. case NFC_HCI_RF_READER_A_GATE:
  257. r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
  258. NFC_HCI_RF_READER_A_ATQA, &atqa_skb);
  259. if (r < 0)
  260. goto exit;
  261. r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
  262. NFC_HCI_RF_READER_A_SAK, &sak_skb);
  263. if (r < 0)
  264. goto exit;
  265. if (atqa_skb->len != 2 || sak_skb->len != 1) {
  266. r = -EPROTO;
  267. goto exit;
  268. }
  269. targets->supported_protocols =
  270. nfc_hci_sak_to_protocol(sak_skb->data[0]);
  271. if (targets->supported_protocols == 0xffffffff) {
  272. r = -EPROTO;
  273. goto exit;
  274. }
  275. targets->sens_res = be16_to_cpu(*(__be16 *)atqa_skb->data);
  276. targets->sel_res = sak_skb->data[0];
  277. r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
  278. NFC_HCI_RF_READER_A_UID, &uid_skb);
  279. if (r < 0)
  280. goto exit;
  281. if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
  282. r = -EPROTO;
  283. goto exit;
  284. }
  285. memcpy(targets->nfcid1, uid_skb->data, uid_skb->len);
  286. targets->nfcid1_len = uid_skb->len;
  287. if (hdev->ops->complete_target_discovered) {
  288. r = hdev->ops->complete_target_discovered(hdev, gate,
  289. targets);
  290. if (r < 0)
  291. goto exit;
  292. }
  293. break;
  294. case NFC_HCI_RF_READER_B_GATE:
  295. targets->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
  296. break;
  297. default:
  298. if (hdev->ops->target_from_gate)
  299. r = hdev->ops->target_from_gate(hdev, gate, targets);
  300. else
  301. r = -EPROTO;
  302. if (r < 0)
  303. goto exit;
  304. if (hdev->ops->complete_target_discovered) {
  305. r = hdev->ops->complete_target_discovered(hdev, gate,
  306. targets);
  307. if (r < 0)
  308. goto exit;
  309. }
  310. break;
  311. }
  312. /* if driver set the new gate, we will skip the old one */
  313. if (targets->hci_reader_gate == 0x00)
  314. targets->hci_reader_gate = gate;
  315. r = nfc_targets_found(hdev->ndev, targets, 1);
  316. exit:
  317. kfree(targets);
  318. kfree_skb(atqa_skb);
  319. kfree_skb(sak_skb);
  320. kfree_skb(uid_skb);
  321. return r;
  322. }
  323. EXPORT_SYMBOL(nfc_hci_target_discovered);
  324. void nfc_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
  325. struct sk_buff *skb)
  326. {
  327. int r = 0;
  328. u8 gate = hdev->pipes[pipe].gate;
  329. if (gate == NFC_HCI_INVALID_GATE) {
  330. pr_err("Discarded event %x to unopened pipe %x\n", event, pipe);
  331. goto exit;
  332. }
  333. if (hdev->ops->event_received) {
  334. r = hdev->ops->event_received(hdev, pipe, event, skb);
  335. if (r <= 0)
  336. goto exit_noskb;
  337. }
  338. switch (event) {
  339. case NFC_HCI_EVT_TARGET_DISCOVERED:
  340. if (skb->len < 1) { /* no status data? */
  341. r = -EPROTO;
  342. goto exit;
  343. }
  344. if (skb->data[0] == 3) {
  345. /* TODO: Multiple targets in field, none activated
  346. * poll is supposedly stopped, but there is no
  347. * single target to activate, so nothing to report
  348. * up.
  349. * if we need to restart poll, we must save the
  350. * protocols from the initial poll and reuse here.
  351. */
  352. }
  353. if (skb->data[0] != 0) {
  354. r = -EPROTO;
  355. goto exit;
  356. }
  357. r = nfc_hci_target_discovered(hdev, gate);
  358. break;
  359. default:
  360. pr_info("Discarded unknown event %x to gate %x\n", event, gate);
  361. r = -EINVAL;
  362. break;
  363. }
  364. exit:
  365. kfree_skb(skb);
  366. exit_noskb:
  367. if (r)
  368. nfc_hci_driver_failure(hdev, r);
  369. }
  370. static void nfc_hci_cmd_timeout(unsigned long data)
  371. {
  372. struct nfc_hci_dev *hdev = (struct nfc_hci_dev *)data;
  373. schedule_work(&hdev->msg_tx_work);
  374. }
  375. static int hci_dev_connect_gates(struct nfc_hci_dev *hdev, u8 gate_count,
  376. struct nfc_hci_gate *gates)
  377. {
  378. int r;
  379. while (gate_count--) {
  380. r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
  381. gates->gate, gates->pipe);
  382. if (r < 0)
  383. return r;
  384. gates++;
  385. }
  386. return 0;
  387. }
  388. static int hci_dev_session_init(struct nfc_hci_dev *hdev)
  389. {
  390. struct sk_buff *skb = NULL;
  391. int r;
  392. if (hdev->init_data.gates[0].gate != NFC_HCI_ADMIN_GATE)
  393. return -EPROTO;
  394. r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
  395. hdev->init_data.gates[0].gate,
  396. hdev->init_data.gates[0].pipe);
  397. if (r < 0)
  398. goto exit;
  399. r = nfc_hci_get_param(hdev, NFC_HCI_ADMIN_GATE,
  400. NFC_HCI_ADMIN_SESSION_IDENTITY, &skb);
  401. if (r < 0)
  402. goto disconnect_all;
  403. if (skb->len && skb->len == strlen(hdev->init_data.session_id) &&
  404. (memcmp(hdev->init_data.session_id, skb->data,
  405. skb->len) == 0) && hdev->ops->load_session) {
  406. /* Restore gate<->pipe table from some proprietary location. */
  407. r = hdev->ops->load_session(hdev);
  408. if (r < 0)
  409. goto disconnect_all;
  410. } else {
  411. r = nfc_hci_disconnect_all_gates(hdev);
  412. if (r < 0)
  413. goto exit;
  414. r = hci_dev_connect_gates(hdev, hdev->init_data.gate_count,
  415. hdev->init_data.gates);
  416. if (r < 0)
  417. goto disconnect_all;
  418. r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
  419. NFC_HCI_ADMIN_SESSION_IDENTITY,
  420. hdev->init_data.session_id,
  421. strlen(hdev->init_data.session_id));
  422. }
  423. if (r == 0)
  424. goto exit;
  425. disconnect_all:
  426. nfc_hci_disconnect_all_gates(hdev);
  427. exit:
  428. kfree_skb(skb);
  429. return r;
  430. }
  431. static int hci_dev_version(struct nfc_hci_dev *hdev)
  432. {
  433. int r;
  434. struct sk_buff *skb;
  435. r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
  436. NFC_HCI_ID_MGMT_VERSION_SW, &skb);
  437. if (r == -EOPNOTSUPP) {
  438. pr_info("Software/Hardware info not available\n");
  439. return 0;
  440. }
  441. if (r < 0)
  442. return r;
  443. if (skb->len != 3) {
  444. kfree_skb(skb);
  445. return -EINVAL;
  446. }
  447. hdev->sw_romlib = (skb->data[0] & 0xf0) >> 4;
  448. hdev->sw_patch = skb->data[0] & 0x0f;
  449. hdev->sw_flashlib_major = skb->data[1];
  450. hdev->sw_flashlib_minor = skb->data[2];
  451. kfree_skb(skb);
  452. r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
  453. NFC_HCI_ID_MGMT_VERSION_HW, &skb);
  454. if (r < 0)
  455. return r;
  456. if (skb->len != 3) {
  457. kfree_skb(skb);
  458. return -EINVAL;
  459. }
  460. hdev->hw_derivative = (skb->data[0] & 0xe0) >> 5;
  461. hdev->hw_version = skb->data[0] & 0x1f;
  462. hdev->hw_mpw = (skb->data[1] & 0xc0) >> 6;
  463. hdev->hw_software = skb->data[1] & 0x3f;
  464. hdev->hw_bsid = skb->data[2];
  465. kfree_skb(skb);
  466. pr_info("SOFTWARE INFO:\n");
  467. pr_info("RomLib : %d\n", hdev->sw_romlib);
  468. pr_info("Patch : %d\n", hdev->sw_patch);
  469. pr_info("FlashLib Major : %d\n", hdev->sw_flashlib_major);
  470. pr_info("FlashLib Minor : %d\n", hdev->sw_flashlib_minor);
  471. pr_info("HARDWARE INFO:\n");
  472. pr_info("Derivative : %d\n", hdev->hw_derivative);
  473. pr_info("HW Version : %d\n", hdev->hw_version);
  474. pr_info("#MPW : %d\n", hdev->hw_mpw);
  475. pr_info("Software : %d\n", hdev->hw_software);
  476. pr_info("BSID Version : %d\n", hdev->hw_bsid);
  477. return 0;
  478. }
  479. static int hci_dev_up(struct nfc_dev *nfc_dev)
  480. {
  481. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  482. int r = 0;
  483. if (hdev->ops->open) {
  484. r = hdev->ops->open(hdev);
  485. if (r < 0)
  486. return r;
  487. }
  488. r = nfc_llc_start(hdev->llc);
  489. if (r < 0)
  490. goto exit_close;
  491. r = hci_dev_session_init(hdev);
  492. if (r < 0)
  493. goto exit_llc;
  494. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  495. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  496. if (r < 0)
  497. goto exit_llc;
  498. if (hdev->ops->hci_ready) {
  499. r = hdev->ops->hci_ready(hdev);
  500. if (r < 0)
  501. goto exit_llc;
  502. }
  503. r = hci_dev_version(hdev);
  504. if (r < 0)
  505. goto exit_llc;
  506. return 0;
  507. exit_llc:
  508. nfc_llc_stop(hdev->llc);
  509. exit_close:
  510. if (hdev->ops->close)
  511. hdev->ops->close(hdev);
  512. return r;
  513. }
  514. static int hci_dev_down(struct nfc_dev *nfc_dev)
  515. {
  516. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  517. nfc_llc_stop(hdev->llc);
  518. if (hdev->ops->close)
  519. hdev->ops->close(hdev);
  520. nfc_hci_reset_pipes(hdev);
  521. return 0;
  522. }
  523. static int hci_start_poll(struct nfc_dev *nfc_dev,
  524. u32 im_protocols, u32 tm_protocols)
  525. {
  526. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  527. if (hdev->ops->start_poll)
  528. return hdev->ops->start_poll(hdev, im_protocols, tm_protocols);
  529. else
  530. return nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  531. NFC_HCI_EVT_READER_REQUESTED,
  532. NULL, 0);
  533. }
  534. static void hci_stop_poll(struct nfc_dev *nfc_dev)
  535. {
  536. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  537. if (hdev->ops->stop_poll)
  538. hdev->ops->stop_poll(hdev);
  539. else
  540. nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  541. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  542. }
  543. static int hci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
  544. __u8 comm_mode, __u8 *gb, size_t gb_len)
  545. {
  546. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  547. if (!hdev->ops->dep_link_up)
  548. return 0;
  549. return hdev->ops->dep_link_up(hdev, target, comm_mode,
  550. gb, gb_len);
  551. }
  552. static int hci_dep_link_down(struct nfc_dev *nfc_dev)
  553. {
  554. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  555. if (!hdev->ops->dep_link_down)
  556. return 0;
  557. return hdev->ops->dep_link_down(hdev);
  558. }
  559. static int hci_activate_target(struct nfc_dev *nfc_dev,
  560. struct nfc_target *target, u32 protocol)
  561. {
  562. return 0;
  563. }
  564. static void hci_deactivate_target(struct nfc_dev *nfc_dev,
  565. struct nfc_target *target,
  566. u8 mode)
  567. {
  568. }
  569. #define HCI_CB_TYPE_TRANSCEIVE 1
  570. static void hci_transceive_cb(void *context, struct sk_buff *skb, int err)
  571. {
  572. struct nfc_hci_dev *hdev = context;
  573. switch (hdev->async_cb_type) {
  574. case HCI_CB_TYPE_TRANSCEIVE:
  575. /*
  576. * TODO: Check RF Error indicator to make sure data is valid.
  577. * It seems that HCI cmd can complete without error, but data
  578. * can be invalid if an RF error occured? Ignore for now.
  579. */
  580. if (err == 0)
  581. skb_trim(skb, skb->len - 1); /* RF Err ind */
  582. hdev->async_cb(hdev->async_cb_context, skb, err);
  583. break;
  584. default:
  585. if (err == 0)
  586. kfree_skb(skb);
  587. break;
  588. }
  589. }
  590. static int hci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
  591. struct sk_buff *skb, data_exchange_cb_t cb,
  592. void *cb_context)
  593. {
  594. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  595. int r;
  596. pr_debug("target_idx=%d\n", target->idx);
  597. switch (target->hci_reader_gate) {
  598. case NFC_HCI_RF_READER_A_GATE:
  599. case NFC_HCI_RF_READER_B_GATE:
  600. if (hdev->ops->im_transceive) {
  601. r = hdev->ops->im_transceive(hdev, target, skb, cb,
  602. cb_context);
  603. if (r <= 0) /* handled */
  604. break;
  605. }
  606. *skb_push(skb, 1) = 0; /* CTR, see spec:10.2.2.1 */
  607. hdev->async_cb_type = HCI_CB_TYPE_TRANSCEIVE;
  608. hdev->async_cb = cb;
  609. hdev->async_cb_context = cb_context;
  610. r = nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  611. NFC_HCI_WR_XCHG_DATA, skb->data,
  612. skb->len, hci_transceive_cb, hdev);
  613. break;
  614. default:
  615. if (hdev->ops->im_transceive) {
  616. r = hdev->ops->im_transceive(hdev, target, skb, cb,
  617. cb_context);
  618. if (r == 1)
  619. r = -ENOTSUPP;
  620. } else {
  621. r = -ENOTSUPP;
  622. }
  623. break;
  624. }
  625. kfree_skb(skb);
  626. return r;
  627. }
  628. static int hci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
  629. {
  630. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  631. if (!hdev->ops->tm_send) {
  632. kfree_skb(skb);
  633. return -ENOTSUPP;
  634. }
  635. return hdev->ops->tm_send(hdev, skb);
  636. }
  637. static int hci_check_presence(struct nfc_dev *nfc_dev,
  638. struct nfc_target *target)
  639. {
  640. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  641. if (!hdev->ops->check_presence)
  642. return 0;
  643. return hdev->ops->check_presence(hdev, target);
  644. }
  645. static int hci_discover_se(struct nfc_dev *nfc_dev)
  646. {
  647. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  648. if (hdev->ops->discover_se)
  649. return hdev->ops->discover_se(hdev);
  650. return 0;
  651. }
  652. static int hci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  653. {
  654. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  655. if (hdev->ops->enable_se)
  656. return hdev->ops->enable_se(hdev, se_idx);
  657. return 0;
  658. }
  659. static int hci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  660. {
  661. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  662. if (hdev->ops->disable_se)
  663. return hdev->ops->disable_se(hdev, se_idx);
  664. return 0;
  665. }
  666. static int hci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
  667. u8 *apdu, size_t apdu_length,
  668. se_io_cb_t cb, void *cb_context)
  669. {
  670. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  671. if (hdev->ops->se_io)
  672. return hdev->ops->se_io(hdev, se_idx, apdu,
  673. apdu_length, cb, cb_context);
  674. return 0;
  675. }
  676. static void nfc_hci_failure(struct nfc_hci_dev *hdev, int err)
  677. {
  678. mutex_lock(&hdev->msg_tx_mutex);
  679. if (hdev->cmd_pending_msg == NULL) {
  680. nfc_driver_failure(hdev->ndev, err);
  681. goto exit;
  682. }
  683. __nfc_hci_cmd_completion(hdev, err, NULL);
  684. exit:
  685. mutex_unlock(&hdev->msg_tx_mutex);
  686. }
  687. static void nfc_hci_llc_failure(struct nfc_hci_dev *hdev, int err)
  688. {
  689. nfc_hci_failure(hdev, err);
  690. }
  691. static void nfc_hci_recv_from_llc(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  692. {
  693. struct hcp_packet *packet;
  694. u8 type;
  695. u8 instruction;
  696. struct sk_buff *hcp_skb;
  697. u8 pipe;
  698. struct sk_buff *frag_skb;
  699. int msg_len;
  700. packet = (struct hcp_packet *)skb->data;
  701. if ((packet->header & ~NFC_HCI_FRAGMENT) == 0) {
  702. skb_queue_tail(&hdev->rx_hcp_frags, skb);
  703. return;
  704. }
  705. /* it's the last fragment. Does it need re-aggregation? */
  706. if (skb_queue_len(&hdev->rx_hcp_frags)) {
  707. pipe = packet->header & NFC_HCI_FRAGMENT;
  708. skb_queue_tail(&hdev->rx_hcp_frags, skb);
  709. msg_len = 0;
  710. skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
  711. msg_len += (frag_skb->len -
  712. NFC_HCI_HCP_PACKET_HEADER_LEN);
  713. }
  714. hcp_skb = nfc_alloc_recv_skb(NFC_HCI_HCP_PACKET_HEADER_LEN +
  715. msg_len, GFP_KERNEL);
  716. if (hcp_skb == NULL) {
  717. nfc_hci_failure(hdev, -ENOMEM);
  718. return;
  719. }
  720. *skb_put(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN) = pipe;
  721. skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
  722. msg_len = frag_skb->len - NFC_HCI_HCP_PACKET_HEADER_LEN;
  723. memcpy(skb_put(hcp_skb, msg_len),
  724. frag_skb->data + NFC_HCI_HCP_PACKET_HEADER_LEN,
  725. msg_len);
  726. }
  727. skb_queue_purge(&hdev->rx_hcp_frags);
  728. } else {
  729. packet->header &= NFC_HCI_FRAGMENT;
  730. hcp_skb = skb;
  731. }
  732. /* if this is a response, dispatch immediately to
  733. * unblock waiting cmd context. Otherwise, enqueue to dispatch
  734. * in separate context where handler can also execute command.
  735. */
  736. packet = (struct hcp_packet *)hcp_skb->data;
  737. type = HCP_MSG_GET_TYPE(packet->message.header);
  738. if (type == NFC_HCI_HCP_RESPONSE) {
  739. pipe = packet->header;
  740. instruction = HCP_MSG_GET_CMD(packet->message.header);
  741. skb_pull(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN +
  742. NFC_HCI_HCP_MESSAGE_HEADER_LEN);
  743. nfc_hci_hcp_message_rx(hdev, pipe, type, instruction, hcp_skb);
  744. } else {
  745. skb_queue_tail(&hdev->msg_rx_queue, hcp_skb);
  746. schedule_work(&hdev->msg_rx_work);
  747. }
  748. }
  749. static int hci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
  750. {
  751. struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
  752. if (!hdev->ops->fw_download)
  753. return -ENOTSUPP;
  754. return hdev->ops->fw_download(hdev, firmware_name);
  755. }
  756. static struct nfc_ops hci_nfc_ops = {
  757. .dev_up = hci_dev_up,
  758. .dev_down = hci_dev_down,
  759. .start_poll = hci_start_poll,
  760. .stop_poll = hci_stop_poll,
  761. .dep_link_up = hci_dep_link_up,
  762. .dep_link_down = hci_dep_link_down,
  763. .activate_target = hci_activate_target,
  764. .deactivate_target = hci_deactivate_target,
  765. .im_transceive = hci_transceive,
  766. .tm_send = hci_tm_send,
  767. .check_presence = hci_check_presence,
  768. .fw_download = hci_fw_download,
  769. .discover_se = hci_discover_se,
  770. .enable_se = hci_enable_se,
  771. .disable_se = hci_disable_se,
  772. .se_io = hci_se_io,
  773. };
  774. struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
  775. struct nfc_hci_init_data *init_data,
  776. unsigned long quirks,
  777. u32 protocols,
  778. const char *llc_name,
  779. int tx_headroom,
  780. int tx_tailroom,
  781. int max_link_payload)
  782. {
  783. struct nfc_hci_dev *hdev;
  784. if (ops->xmit == NULL)
  785. return NULL;
  786. if (protocols == 0)
  787. return NULL;
  788. hdev = kzalloc(sizeof(struct nfc_hci_dev), GFP_KERNEL);
  789. if (hdev == NULL)
  790. return NULL;
  791. hdev->llc = nfc_llc_allocate(llc_name, hdev, ops->xmit,
  792. nfc_hci_recv_from_llc, tx_headroom,
  793. tx_tailroom, nfc_hci_llc_failure);
  794. if (hdev->llc == NULL) {
  795. kfree(hdev);
  796. return NULL;
  797. }
  798. hdev->ndev = nfc_allocate_device(&hci_nfc_ops, protocols,
  799. tx_headroom + HCI_CMDS_HEADROOM,
  800. tx_tailroom);
  801. if (!hdev->ndev) {
  802. nfc_llc_free(hdev->llc);
  803. kfree(hdev);
  804. return NULL;
  805. }
  806. hdev->ops = ops;
  807. hdev->max_data_link_payload = max_link_payload;
  808. hdev->init_data = *init_data;
  809. nfc_set_drvdata(hdev->ndev, hdev);
  810. nfc_hci_reset_pipes(hdev);
  811. hdev->quirks = quirks;
  812. return hdev;
  813. }
  814. EXPORT_SYMBOL(nfc_hci_allocate_device);
  815. void nfc_hci_free_device(struct nfc_hci_dev *hdev)
  816. {
  817. nfc_free_device(hdev->ndev);
  818. nfc_llc_free(hdev->llc);
  819. kfree(hdev);
  820. }
  821. EXPORT_SYMBOL(nfc_hci_free_device);
  822. int nfc_hci_register_device(struct nfc_hci_dev *hdev)
  823. {
  824. mutex_init(&hdev->msg_tx_mutex);
  825. INIT_LIST_HEAD(&hdev->msg_tx_queue);
  826. INIT_WORK(&hdev->msg_tx_work, nfc_hci_msg_tx_work);
  827. init_timer(&hdev->cmd_timer);
  828. hdev->cmd_timer.data = (unsigned long)hdev;
  829. hdev->cmd_timer.function = nfc_hci_cmd_timeout;
  830. skb_queue_head_init(&hdev->rx_hcp_frags);
  831. INIT_WORK(&hdev->msg_rx_work, nfc_hci_msg_rx_work);
  832. skb_queue_head_init(&hdev->msg_rx_queue);
  833. return nfc_register_device(hdev->ndev);
  834. }
  835. EXPORT_SYMBOL(nfc_hci_register_device);
  836. void nfc_hci_unregister_device(struct nfc_hci_dev *hdev)
  837. {
  838. struct hci_msg *msg, *n;
  839. mutex_lock(&hdev->msg_tx_mutex);
  840. if (hdev->cmd_pending_msg) {
  841. if (hdev->cmd_pending_msg->cb)
  842. hdev->cmd_pending_msg->cb(
  843. hdev->cmd_pending_msg->cb_context,
  844. NULL, -ESHUTDOWN);
  845. kfree(hdev->cmd_pending_msg);
  846. hdev->cmd_pending_msg = NULL;
  847. }
  848. hdev->shutting_down = true;
  849. mutex_unlock(&hdev->msg_tx_mutex);
  850. del_timer_sync(&hdev->cmd_timer);
  851. cancel_work_sync(&hdev->msg_tx_work);
  852. cancel_work_sync(&hdev->msg_rx_work);
  853. nfc_unregister_device(hdev->ndev);
  854. skb_queue_purge(&hdev->rx_hcp_frags);
  855. skb_queue_purge(&hdev->msg_rx_queue);
  856. list_for_each_entry_safe(msg, n, &hdev->msg_tx_queue, msg_l) {
  857. list_del(&msg->msg_l);
  858. skb_queue_purge(&msg->msg_frags);
  859. kfree(msg);
  860. }
  861. }
  862. EXPORT_SYMBOL(nfc_hci_unregister_device);
  863. void nfc_hci_set_clientdata(struct nfc_hci_dev *hdev, void *clientdata)
  864. {
  865. hdev->clientdata = clientdata;
  866. }
  867. EXPORT_SYMBOL(nfc_hci_set_clientdata);
  868. void *nfc_hci_get_clientdata(struct nfc_hci_dev *hdev)
  869. {
  870. return hdev->clientdata;
  871. }
  872. EXPORT_SYMBOL(nfc_hci_get_clientdata);
  873. void nfc_hci_driver_failure(struct nfc_hci_dev *hdev, int err)
  874. {
  875. nfc_hci_failure(hdev, err);
  876. }
  877. EXPORT_SYMBOL(nfc_hci_driver_failure);
  878. void nfc_hci_recv_frame(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  879. {
  880. nfc_llc_rcv_from_drv(hdev->llc, skb);
  881. }
  882. EXPORT_SYMBOL(nfc_hci_recv_frame);
  883. static int __init nfc_hci_init(void)
  884. {
  885. return nfc_llc_init();
  886. }
  887. static void __exit nfc_hci_exit(void)
  888. {
  889. nfc_llc_exit();
  890. }
  891. subsys_initcall(nfc_hci_init);
  892. module_exit(nfc_hci_exit);
  893. MODULE_LICENSE("GPL");
  894. MODULE_DESCRIPTION("NFC HCI Core");