core.c 25 KB

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