hci.c 19 KB

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