hci.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. * The NFC Controller Interface is the communication protocol between an
  3. * NFC Controller (NFCC) and a Device Host (DH).
  4. * This is the HCI over NCI implementation, as specified in the 10.2
  5. * section of the NCI 1.1 specification.
  6. *
  7. * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include <linux/skbuff.h>
  23. #include "../nfc.h"
  24. #include <net/nfc/nci.h>
  25. #include <net/nfc/nci_core.h>
  26. #include <linux/nfc.h>
  27. struct nci_data {
  28. u8 conn_id;
  29. u8 pipe;
  30. u8 cmd;
  31. const u8 *data;
  32. u32 data_len;
  33. } __packed;
  34. struct nci_hci_create_pipe_params {
  35. u8 src_gate;
  36. u8 dest_host;
  37. u8 dest_gate;
  38. } __packed;
  39. struct nci_hci_create_pipe_resp {
  40. u8 src_host;
  41. u8 src_gate;
  42. u8 dest_host;
  43. u8 dest_gate;
  44. u8 pipe;
  45. } __packed;
  46. struct nci_hci_delete_pipe_noti {
  47. u8 pipe;
  48. } __packed;
  49. struct nci_hci_all_pipe_cleared_noti {
  50. u8 host;
  51. } __packed;
  52. struct nci_hcp_message {
  53. u8 header; /* type -cmd,evt,rsp- + instruction */
  54. u8 data[];
  55. } __packed;
  56. struct nci_hcp_packet {
  57. u8 header; /* cbit+pipe */
  58. struct nci_hcp_message message;
  59. } __packed;
  60. #define NCI_HCI_ANY_SET_PARAMETER 0x01
  61. #define NCI_HCI_ANY_GET_PARAMETER 0x02
  62. #define NCI_HCI_ANY_CLOSE_PIPE 0x04
  63. #define NCI_HFP_NO_CHAINING 0x80
  64. #define NCI_NFCEE_ID_HCI 0x80
  65. #define NCI_EVT_HOT_PLUG 0x03
  66. #define NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY 0x01
  67. /* HCP headers */
  68. #define NCI_HCI_HCP_PACKET_HEADER_LEN 1
  69. #define NCI_HCI_HCP_MESSAGE_HEADER_LEN 1
  70. #define NCI_HCI_HCP_HEADER_LEN 2
  71. /* HCP types */
  72. #define NCI_HCI_HCP_COMMAND 0x00
  73. #define NCI_HCI_HCP_EVENT 0x01
  74. #define NCI_HCI_HCP_RESPONSE 0x02
  75. #define NCI_HCI_ADM_NOTIFY_PIPE_CREATED 0x12
  76. #define NCI_HCI_ADM_NOTIFY_PIPE_DELETED 0x13
  77. #define NCI_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED 0x15
  78. #define NCI_HCI_FRAGMENT 0x7f
  79. #define NCI_HCP_HEADER(type, instr) ((((type) & 0x03) << 6) |\
  80. ((instr) & 0x3f))
  81. #define NCI_HCP_MSG_GET_TYPE(header) ((header & 0xc0) >> 6)
  82. #define NCI_HCP_MSG_GET_CMD(header) (header & 0x3f)
  83. #define NCI_HCP_MSG_GET_PIPE(header) (header & 0x7f)
  84. /* HCI core */
  85. static void nci_hci_reset_pipes(struct nci_hci_dev *hdev)
  86. {
  87. int i;
  88. for (i = 0; i < NCI_HCI_MAX_PIPES; i++) {
  89. hdev->pipes[i].gate = NCI_HCI_INVALID_GATE;
  90. hdev->pipes[i].host = NCI_HCI_INVALID_HOST;
  91. }
  92. memset(hdev->gate2pipe, NCI_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
  93. }
  94. static void nci_hci_reset_pipes_per_host(struct nci_dev *ndev, u8 host)
  95. {
  96. int i;
  97. for (i = 0; i < NCI_HCI_MAX_PIPES; i++) {
  98. if (ndev->hci_dev->pipes[i].host == host) {
  99. ndev->hci_dev->pipes[i].gate = NCI_HCI_INVALID_GATE;
  100. ndev->hci_dev->pipes[i].host = NCI_HCI_INVALID_HOST;
  101. }
  102. }
  103. }
  104. /* Fragment HCI data over NCI packet.
  105. * NFC Forum NCI 10.2.2 Data Exchange:
  106. * The payload of the Data Packets sent on the Logical Connection SHALL be
  107. * valid HCP packets, as defined within [ETSI_102622]. Each Data Packet SHALL
  108. * contain a single HCP packet. NCI Segmentation and Reassembly SHALL NOT be
  109. * applied to Data Messages in either direction. The HCI fragmentation mechanism
  110. * is used if required.
  111. */
  112. static int nci_hci_send_data(struct nci_dev *ndev, u8 pipe,
  113. const u8 data_type, const u8 *data,
  114. size_t data_len)
  115. {
  116. struct nci_conn_info *conn_info;
  117. struct sk_buff *skb;
  118. int len, i, r;
  119. u8 cb = pipe;
  120. conn_info = ndev->hci_dev->conn_info;
  121. if (!conn_info)
  122. return -EPROTO;
  123. skb = nci_skb_alloc(ndev, 2 + conn_info->max_pkt_payload_len +
  124. NCI_DATA_HDR_SIZE, GFP_KERNEL);
  125. if (!skb)
  126. return -ENOMEM;
  127. skb_reserve(skb, 2 + NCI_DATA_HDR_SIZE);
  128. *skb_push(skb, 1) = data_type;
  129. i = 0;
  130. len = conn_info->max_pkt_payload_len;
  131. do {
  132. /* If last packet add NCI_HFP_NO_CHAINING */
  133. if (i + conn_info->max_pkt_payload_len -
  134. (skb->len + 1) >= data_len) {
  135. cb |= NCI_HFP_NO_CHAINING;
  136. len = data_len - i;
  137. } else {
  138. len = conn_info->max_pkt_payload_len - skb->len - 1;
  139. }
  140. *skb_push(skb, 1) = cb;
  141. if (len > 0)
  142. memcpy(skb_put(skb, len), data + i, len);
  143. r = nci_send_data(ndev, conn_info->conn_id, skb);
  144. if (r < 0)
  145. return r;
  146. i += len;
  147. if (i < data_len) {
  148. skb_trim(skb, 0);
  149. skb_pull(skb, len);
  150. }
  151. } while (i < data_len);
  152. return i;
  153. }
  154. static void nci_hci_send_data_req(struct nci_dev *ndev, unsigned long opt)
  155. {
  156. struct nci_data *data = (struct nci_data *)opt;
  157. nci_hci_send_data(ndev, data->pipe, data->cmd,
  158. data->data, data->data_len);
  159. }
  160. int nci_hci_send_event(struct nci_dev *ndev, u8 gate, u8 event,
  161. const u8 *param, size_t param_len)
  162. {
  163. u8 pipe = ndev->hci_dev->gate2pipe[gate];
  164. if (pipe == NCI_HCI_INVALID_PIPE)
  165. return -EADDRNOTAVAIL;
  166. return nci_hci_send_data(ndev, pipe,
  167. NCI_HCP_HEADER(NCI_HCI_HCP_EVENT, event),
  168. param, param_len);
  169. }
  170. EXPORT_SYMBOL(nci_hci_send_event);
  171. int nci_hci_send_cmd(struct nci_dev *ndev, u8 gate, u8 cmd,
  172. const u8 *param, size_t param_len,
  173. struct sk_buff **skb)
  174. {
  175. struct nci_conn_info *conn_info;
  176. struct nci_data data;
  177. int r;
  178. u8 pipe = ndev->hci_dev->gate2pipe[gate];
  179. if (pipe == NCI_HCI_INVALID_PIPE)
  180. return -EADDRNOTAVAIL;
  181. conn_info = ndev->hci_dev->conn_info;
  182. if (!conn_info)
  183. return -EPROTO;
  184. data.conn_id = conn_info->conn_id;
  185. data.pipe = pipe;
  186. data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND, cmd);
  187. data.data = param;
  188. data.data_len = param_len;
  189. r = nci_request(ndev, nci_hci_send_data_req, (unsigned long)&data,
  190. msecs_to_jiffies(NCI_DATA_TIMEOUT));
  191. if (r == NCI_STATUS_OK)
  192. *skb = conn_info->rx_skb;
  193. return r;
  194. }
  195. EXPORT_SYMBOL(nci_hci_send_cmd);
  196. static void nci_hci_event_received(struct nci_dev *ndev, u8 pipe,
  197. u8 event, struct sk_buff *skb)
  198. {
  199. if (ndev->ops->hci_event_received)
  200. ndev->ops->hci_event_received(ndev, pipe, event, skb);
  201. }
  202. static void nci_hci_cmd_received(struct nci_dev *ndev, u8 pipe,
  203. u8 cmd, struct sk_buff *skb)
  204. {
  205. u8 gate = ndev->hci_dev->pipes[pipe].gate;
  206. u8 status = NCI_HCI_ANY_OK | ~NCI_HCI_FRAGMENT;
  207. u8 dest_gate, new_pipe;
  208. struct nci_hci_create_pipe_resp *create_info;
  209. struct nci_hci_delete_pipe_noti *delete_info;
  210. struct nci_hci_all_pipe_cleared_noti *cleared_info;
  211. pr_debug("from gate %x pipe %x cmd %x\n", gate, pipe, cmd);
  212. switch (cmd) {
  213. case NCI_HCI_ADM_NOTIFY_PIPE_CREATED:
  214. if (skb->len != 5) {
  215. status = NCI_HCI_ANY_E_NOK;
  216. goto exit;
  217. }
  218. create_info = (struct nci_hci_create_pipe_resp *)skb->data;
  219. dest_gate = create_info->dest_gate;
  220. new_pipe = create_info->pipe;
  221. /* Save the new created pipe and bind with local gate,
  222. * the description for skb->data[3] is destination gate id
  223. * but since we received this cmd from host controller, we
  224. * are the destination and it is our local gate
  225. */
  226. ndev->hci_dev->gate2pipe[dest_gate] = new_pipe;
  227. ndev->hci_dev->pipes[new_pipe].gate = dest_gate;
  228. ndev->hci_dev->pipes[new_pipe].host =
  229. create_info->src_host;
  230. break;
  231. case NCI_HCI_ANY_OPEN_PIPE:
  232. /* If the pipe is not created report an error */
  233. if (gate == NCI_HCI_INVALID_GATE) {
  234. status = NCI_HCI_ANY_E_NOK;
  235. goto exit;
  236. }
  237. break;
  238. case NCI_HCI_ADM_NOTIFY_PIPE_DELETED:
  239. if (skb->len != 1) {
  240. status = NCI_HCI_ANY_E_NOK;
  241. goto exit;
  242. }
  243. delete_info = (struct nci_hci_delete_pipe_noti *)skb->data;
  244. ndev->hci_dev->pipes[delete_info->pipe].gate =
  245. NCI_HCI_INVALID_GATE;
  246. ndev->hci_dev->pipes[delete_info->pipe].host =
  247. NCI_HCI_INVALID_HOST;
  248. break;
  249. case NCI_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED:
  250. if (skb->len != 1) {
  251. status = NCI_HCI_ANY_E_NOK;
  252. goto exit;
  253. }
  254. cleared_info =
  255. (struct nci_hci_all_pipe_cleared_noti *)skb->data;
  256. nci_hci_reset_pipes_per_host(ndev, cleared_info->host);
  257. break;
  258. default:
  259. pr_debug("Discarded unknown cmd %x to gate %x\n", cmd, gate);
  260. break;
  261. }
  262. if (ndev->ops->hci_cmd_received)
  263. ndev->ops->hci_cmd_received(ndev, pipe, cmd, skb);
  264. exit:
  265. nci_hci_send_data(ndev, pipe, status, NULL, 0);
  266. kfree_skb(skb);
  267. }
  268. static void nci_hci_resp_received(struct nci_dev *ndev, u8 pipe,
  269. u8 result, struct sk_buff *skb)
  270. {
  271. struct nci_conn_info *conn_info;
  272. u8 status = result;
  273. if (result != NCI_HCI_ANY_OK)
  274. goto exit;
  275. conn_info = ndev->hci_dev->conn_info;
  276. if (!conn_info) {
  277. status = NCI_STATUS_REJECTED;
  278. goto exit;
  279. }
  280. conn_info->rx_skb = skb;
  281. exit:
  282. nci_req_complete(ndev, status);
  283. }
  284. /* Receive hcp message for pipe, with type and cmd.
  285. * skb contains optional message data only.
  286. */
  287. static void nci_hci_hcp_message_rx(struct nci_dev *ndev, u8 pipe,
  288. u8 type, u8 instruction, struct sk_buff *skb)
  289. {
  290. switch (type) {
  291. case NCI_HCI_HCP_RESPONSE:
  292. nci_hci_resp_received(ndev, pipe, instruction, skb);
  293. break;
  294. case NCI_HCI_HCP_COMMAND:
  295. nci_hci_cmd_received(ndev, pipe, instruction, skb);
  296. break;
  297. case NCI_HCI_HCP_EVENT:
  298. nci_hci_event_received(ndev, pipe, instruction, skb);
  299. break;
  300. default:
  301. pr_err("UNKNOWN MSG Type %d, instruction=%d\n",
  302. type, instruction);
  303. kfree_skb(skb);
  304. break;
  305. }
  306. nci_req_complete(ndev, 0);
  307. }
  308. static void nci_hci_msg_rx_work(struct work_struct *work)
  309. {
  310. struct nci_hci_dev *hdev =
  311. container_of(work, struct nci_hci_dev, msg_rx_work);
  312. struct sk_buff *skb;
  313. struct nci_hcp_message *message;
  314. u8 pipe, type, instruction;
  315. while ((skb = skb_dequeue(&hdev->msg_rx_queue)) != NULL) {
  316. pipe = skb->data[0];
  317. skb_pull(skb, NCI_HCI_HCP_PACKET_HEADER_LEN);
  318. message = (struct nci_hcp_message *)skb->data;
  319. type = NCI_HCP_MSG_GET_TYPE(message->header);
  320. instruction = NCI_HCP_MSG_GET_CMD(message->header);
  321. skb_pull(skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
  322. nci_hci_hcp_message_rx(hdev->ndev, pipe,
  323. type, instruction, skb);
  324. }
  325. }
  326. void nci_hci_data_received_cb(void *context,
  327. struct sk_buff *skb, int err)
  328. {
  329. struct nci_dev *ndev = (struct nci_dev *)context;
  330. struct nci_hcp_packet *packet;
  331. u8 pipe, type, instruction;
  332. struct sk_buff *hcp_skb;
  333. struct sk_buff *frag_skb;
  334. int msg_len;
  335. pr_debug("\n");
  336. if (err) {
  337. nci_req_complete(ndev, err);
  338. return;
  339. }
  340. packet = (struct nci_hcp_packet *)skb->data;
  341. if ((packet->header & ~NCI_HCI_FRAGMENT) == 0) {
  342. skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb);
  343. return;
  344. }
  345. /* it's the last fragment. Does it need re-aggregation? */
  346. if (skb_queue_len(&ndev->hci_dev->rx_hcp_frags)) {
  347. pipe = packet->header & NCI_HCI_FRAGMENT;
  348. skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb);
  349. msg_len = 0;
  350. skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) {
  351. msg_len += (frag_skb->len -
  352. NCI_HCI_HCP_PACKET_HEADER_LEN);
  353. }
  354. hcp_skb = nfc_alloc_recv_skb(NCI_HCI_HCP_PACKET_HEADER_LEN +
  355. msg_len, GFP_KERNEL);
  356. if (!hcp_skb) {
  357. nci_req_complete(ndev, -ENOMEM);
  358. return;
  359. }
  360. *skb_put(hcp_skb, NCI_HCI_HCP_PACKET_HEADER_LEN) = pipe;
  361. skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) {
  362. msg_len = frag_skb->len - NCI_HCI_HCP_PACKET_HEADER_LEN;
  363. memcpy(skb_put(hcp_skb, msg_len), frag_skb->data +
  364. NCI_HCI_HCP_PACKET_HEADER_LEN, msg_len);
  365. }
  366. skb_queue_purge(&ndev->hci_dev->rx_hcp_frags);
  367. } else {
  368. packet->header &= NCI_HCI_FRAGMENT;
  369. hcp_skb = skb;
  370. }
  371. /* if this is a response, dispatch immediately to
  372. * unblock waiting cmd context. Otherwise, enqueue to dispatch
  373. * in separate context where handler can also execute command.
  374. */
  375. packet = (struct nci_hcp_packet *)hcp_skb->data;
  376. type = NCI_HCP_MSG_GET_TYPE(packet->message.header);
  377. if (type == NCI_HCI_HCP_RESPONSE) {
  378. pipe = packet->header;
  379. instruction = NCI_HCP_MSG_GET_CMD(packet->message.header);
  380. skb_pull(hcp_skb, NCI_HCI_HCP_PACKET_HEADER_LEN +
  381. NCI_HCI_HCP_MESSAGE_HEADER_LEN);
  382. nci_hci_hcp_message_rx(ndev, pipe, type, instruction, hcp_skb);
  383. } else {
  384. skb_queue_tail(&ndev->hci_dev->msg_rx_queue, hcp_skb);
  385. schedule_work(&ndev->hci_dev->msg_rx_work);
  386. }
  387. }
  388. int nci_hci_open_pipe(struct nci_dev *ndev, u8 pipe)
  389. {
  390. struct nci_data data;
  391. struct nci_conn_info *conn_info;
  392. conn_info = ndev->hci_dev->conn_info;
  393. if (!conn_info)
  394. return -EPROTO;
  395. data.conn_id = conn_info->conn_id;
  396. data.pipe = pipe;
  397. data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
  398. NCI_HCI_ANY_OPEN_PIPE);
  399. data.data = NULL;
  400. data.data_len = 0;
  401. return nci_request(ndev, nci_hci_send_data_req,
  402. (unsigned long)&data,
  403. msecs_to_jiffies(NCI_DATA_TIMEOUT));
  404. }
  405. EXPORT_SYMBOL(nci_hci_open_pipe);
  406. int nci_hci_set_param(struct nci_dev *ndev, u8 gate, u8 idx,
  407. const u8 *param, size_t param_len)
  408. {
  409. struct nci_conn_info *conn_info;
  410. struct nci_data data;
  411. int r;
  412. u8 *tmp;
  413. u8 pipe = ndev->hci_dev->gate2pipe[gate];
  414. pr_debug("idx=%d to gate %d\n", idx, gate);
  415. if (pipe == NCI_HCI_INVALID_PIPE)
  416. return -EADDRNOTAVAIL;
  417. conn_info = ndev->hci_dev->conn_info;
  418. if (!conn_info)
  419. return -EPROTO;
  420. tmp = kmalloc(1 + param_len, GFP_KERNEL);
  421. if (!tmp)
  422. return -ENOMEM;
  423. *tmp = idx;
  424. memcpy(tmp + 1, param, param_len);
  425. data.conn_id = conn_info->conn_id;
  426. data.pipe = pipe;
  427. data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
  428. NCI_HCI_ANY_SET_PARAMETER);
  429. data.data = tmp;
  430. data.data_len = param_len + 1;
  431. r = nci_request(ndev, nci_hci_send_data_req,
  432. (unsigned long)&data,
  433. msecs_to_jiffies(NCI_DATA_TIMEOUT));
  434. kfree(tmp);
  435. return r;
  436. }
  437. EXPORT_SYMBOL(nci_hci_set_param);
  438. int nci_hci_get_param(struct nci_dev *ndev, u8 gate, u8 idx,
  439. struct sk_buff **skb)
  440. {
  441. struct nci_conn_info *conn_info;
  442. struct nci_data data;
  443. int r;
  444. u8 pipe = ndev->hci_dev->gate2pipe[gate];
  445. pr_debug("idx=%d to gate %d\n", idx, gate);
  446. if (pipe == NCI_HCI_INVALID_PIPE)
  447. return -EADDRNOTAVAIL;
  448. conn_info = ndev->hci_dev->conn_info;
  449. if (!conn_info)
  450. return -EPROTO;
  451. data.conn_id = conn_info->conn_id;
  452. data.pipe = pipe;
  453. data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
  454. NCI_HCI_ANY_GET_PARAMETER);
  455. data.data = &idx;
  456. data.data_len = 1;
  457. r = nci_request(ndev, nci_hci_send_data_req, (unsigned long)&data,
  458. msecs_to_jiffies(NCI_DATA_TIMEOUT));
  459. if (r == NCI_STATUS_OK)
  460. *skb = conn_info->rx_skb;
  461. return r;
  462. }
  463. EXPORT_SYMBOL(nci_hci_get_param);
  464. int nci_hci_connect_gate(struct nci_dev *ndev,
  465. u8 dest_host, u8 dest_gate, u8 pipe)
  466. {
  467. int r;
  468. if (pipe == NCI_HCI_DO_NOT_OPEN_PIPE)
  469. return 0;
  470. if (ndev->hci_dev->gate2pipe[dest_gate] != NCI_HCI_INVALID_PIPE)
  471. return -EADDRINUSE;
  472. if (pipe != NCI_HCI_INVALID_PIPE)
  473. goto open_pipe;
  474. switch (dest_gate) {
  475. case NCI_HCI_LINK_MGMT_GATE:
  476. pipe = NCI_HCI_LINK_MGMT_PIPE;
  477. break;
  478. case NCI_HCI_ADMIN_GATE:
  479. pipe = NCI_HCI_ADMIN_PIPE;
  480. break;
  481. }
  482. open_pipe:
  483. r = nci_hci_open_pipe(ndev, pipe);
  484. if (r < 0)
  485. return r;
  486. ndev->hci_dev->pipes[pipe].gate = dest_gate;
  487. ndev->hci_dev->pipes[pipe].host = dest_host;
  488. ndev->hci_dev->gate2pipe[dest_gate] = pipe;
  489. return 0;
  490. }
  491. EXPORT_SYMBOL(nci_hci_connect_gate);
  492. static int nci_hci_dev_connect_gates(struct nci_dev *ndev,
  493. u8 gate_count,
  494. struct nci_hci_gate *gates)
  495. {
  496. int r;
  497. while (gate_count--) {
  498. r = nci_hci_connect_gate(ndev, gates->dest_host,
  499. gates->gate, gates->pipe);
  500. if (r < 0)
  501. return r;
  502. gates++;
  503. }
  504. return 0;
  505. }
  506. int nci_hci_dev_session_init(struct nci_dev *ndev)
  507. {
  508. struct nci_conn_info *conn_info;
  509. struct sk_buff *skb;
  510. int r;
  511. ndev->hci_dev->count_pipes = 0;
  512. ndev->hci_dev->expected_pipes = 0;
  513. conn_info = ndev->hci_dev->conn_info;
  514. if (!conn_info)
  515. return -EPROTO;
  516. conn_info->data_exchange_cb = nci_hci_data_received_cb;
  517. conn_info->data_exchange_cb_context = ndev;
  518. nci_hci_reset_pipes(ndev->hci_dev);
  519. if (ndev->hci_dev->init_data.gates[0].gate != NCI_HCI_ADMIN_GATE)
  520. return -EPROTO;
  521. r = nci_hci_connect_gate(ndev,
  522. ndev->hci_dev->init_data.gates[0].dest_host,
  523. ndev->hci_dev->init_data.gates[0].gate,
  524. ndev->hci_dev->init_data.gates[0].pipe);
  525. if (r < 0)
  526. return r;
  527. r = nci_hci_get_param(ndev, NCI_HCI_ADMIN_GATE,
  528. NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY, &skb);
  529. if (r < 0)
  530. return r;
  531. if (skb->len &&
  532. skb->len == strlen(ndev->hci_dev->init_data.session_id) &&
  533. !memcmp(ndev->hci_dev->init_data.session_id, skb->data, skb->len) &&
  534. ndev->ops->hci_load_session) {
  535. /* Restore gate<->pipe table from some proprietary location. */
  536. r = ndev->ops->hci_load_session(ndev);
  537. } else {
  538. r = nci_hci_dev_connect_gates(ndev,
  539. ndev->hci_dev->init_data.gate_count,
  540. ndev->hci_dev->init_data.gates);
  541. if (r < 0)
  542. goto exit;
  543. r = nci_hci_set_param(ndev, NCI_HCI_ADMIN_GATE,
  544. NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY,
  545. ndev->hci_dev->init_data.session_id,
  546. strlen(ndev->hci_dev->init_data.session_id));
  547. }
  548. exit:
  549. kfree_skb(skb);
  550. return r;
  551. }
  552. EXPORT_SYMBOL(nci_hci_dev_session_init);
  553. struct nci_hci_dev *nci_hci_allocate(struct nci_dev *ndev)
  554. {
  555. struct nci_hci_dev *hdev;
  556. hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
  557. if (!hdev)
  558. return NULL;
  559. skb_queue_head_init(&hdev->rx_hcp_frags);
  560. INIT_WORK(&hdev->msg_rx_work, nci_hci_msg_rx_work);
  561. skb_queue_head_init(&hdev->msg_rx_queue);
  562. hdev->ndev = ndev;
  563. return hdev;
  564. }