hci.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. const 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_ATOMIC);
  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. /* If last packet add NCI_HFP_NO_CHAINING */
  136. if (i + conn_info->max_pkt_payload_len -
  137. (skb->len + 1) >= data_len) {
  138. cb |= NCI_HFP_NO_CHAINING;
  139. len = data_len - i;
  140. } else {
  141. len = conn_info->max_pkt_payload_len - skb->len - 1;
  142. }
  143. *(u8 *)skb_push(skb, 1) = cb;
  144. if (len > 0)
  145. skb_put_data(skb, data + i, len);
  146. r = nci_send_data(ndev, conn_info->conn_id, skb);
  147. if (r < 0)
  148. return r;
  149. i += len;
  150. if (i < data_len) {
  151. skb = nci_skb_alloc(ndev,
  152. conn_info->max_pkt_payload_len +
  153. NCI_DATA_HDR_SIZE, GFP_ATOMIC);
  154. if (!skb)
  155. return -ENOMEM;
  156. skb_reserve(skb, NCI_DATA_HDR_SIZE + 1);
  157. }
  158. } while (i < data_len);
  159. return i;
  160. }
  161. static void nci_hci_send_data_req(struct nci_dev *ndev, const void *opt)
  162. {
  163. const struct nci_data *data = opt;
  164. nci_hci_send_data(ndev, data->pipe, data->cmd,
  165. data->data, data->data_len);
  166. }
  167. int nci_hci_send_event(struct nci_dev *ndev, u8 gate, u8 event,
  168. const u8 *param, size_t param_len)
  169. {
  170. u8 pipe = ndev->hci_dev->gate2pipe[gate];
  171. if (pipe == NCI_HCI_INVALID_PIPE)
  172. return -EADDRNOTAVAIL;
  173. return nci_hci_send_data(ndev, pipe,
  174. NCI_HCP_HEADER(NCI_HCI_HCP_EVENT, event),
  175. param, param_len);
  176. }
  177. EXPORT_SYMBOL(nci_hci_send_event);
  178. int nci_hci_send_cmd(struct nci_dev *ndev, u8 gate, u8 cmd,
  179. const u8 *param, size_t param_len,
  180. struct sk_buff **skb)
  181. {
  182. const struct nci_hcp_message *message;
  183. const struct nci_conn_info *conn_info;
  184. struct nci_data data;
  185. int r;
  186. u8 pipe = ndev->hci_dev->gate2pipe[gate];
  187. if (pipe == NCI_HCI_INVALID_PIPE)
  188. return -EADDRNOTAVAIL;
  189. conn_info = ndev->hci_dev->conn_info;
  190. if (!conn_info)
  191. return -EPROTO;
  192. data.conn_id = conn_info->conn_id;
  193. data.pipe = pipe;
  194. data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND, cmd);
  195. data.data = param;
  196. data.data_len = param_len;
  197. r = nci_request(ndev, nci_hci_send_data_req, &data,
  198. msecs_to_jiffies(NCI_DATA_TIMEOUT));
  199. if (r == NCI_STATUS_OK) {
  200. message = (struct nci_hcp_message *)conn_info->rx_skb->data;
  201. r = nci_hci_result_to_errno(
  202. NCI_HCP_MSG_GET_CMD(message->header));
  203. skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
  204. if (!r && skb)
  205. *skb = conn_info->rx_skb;
  206. }
  207. return r;
  208. }
  209. EXPORT_SYMBOL(nci_hci_send_cmd);
  210. int nci_hci_clear_all_pipes(struct nci_dev *ndev)
  211. {
  212. int r;
  213. r = nci_hci_send_cmd(ndev, NCI_HCI_ADMIN_GATE,
  214. NCI_HCI_ADM_CLEAR_ALL_PIPE, NULL, 0, NULL);
  215. if (r < 0)
  216. return r;
  217. nci_hci_reset_pipes(ndev->hci_dev);
  218. return r;
  219. }
  220. EXPORT_SYMBOL(nci_hci_clear_all_pipes);
  221. static void nci_hci_event_received(struct nci_dev *ndev, u8 pipe,
  222. u8 event, struct sk_buff *skb)
  223. {
  224. if (ndev->ops->hci_event_received)
  225. ndev->ops->hci_event_received(ndev, pipe, event, skb);
  226. }
  227. static void nci_hci_cmd_received(struct nci_dev *ndev, u8 pipe,
  228. u8 cmd, struct sk_buff *skb)
  229. {
  230. u8 gate = ndev->hci_dev->pipes[pipe].gate;
  231. u8 status = NCI_HCI_ANY_OK | ~NCI_HCI_FRAGMENT;
  232. u8 dest_gate, new_pipe;
  233. struct nci_hci_create_pipe_resp *create_info;
  234. struct nci_hci_delete_pipe_noti *delete_info;
  235. struct nci_hci_all_pipe_cleared_noti *cleared_info;
  236. pr_debug("from gate %x pipe %x cmd %x\n", gate, pipe, cmd);
  237. switch (cmd) {
  238. case NCI_HCI_ADM_NOTIFY_PIPE_CREATED:
  239. if (skb->len != 5) {
  240. status = NCI_HCI_ANY_E_NOK;
  241. goto exit;
  242. }
  243. create_info = (struct nci_hci_create_pipe_resp *)skb->data;
  244. dest_gate = create_info->dest_gate;
  245. new_pipe = create_info->pipe;
  246. if (new_pipe >= NCI_HCI_MAX_PIPES) {
  247. status = NCI_HCI_ANY_E_NOK;
  248. goto exit;
  249. }
  250. /* Save the new created pipe and bind with local gate,
  251. * the description for skb->data[3] is destination gate id
  252. * but since we received this cmd from host controller, we
  253. * are the destination and it is our local gate
  254. */
  255. ndev->hci_dev->gate2pipe[dest_gate] = new_pipe;
  256. ndev->hci_dev->pipes[new_pipe].gate = dest_gate;
  257. ndev->hci_dev->pipes[new_pipe].host =
  258. create_info->src_host;
  259. break;
  260. case NCI_HCI_ANY_OPEN_PIPE:
  261. /* If the pipe is not created report an error */
  262. if (gate == NCI_HCI_INVALID_GATE) {
  263. status = NCI_HCI_ANY_E_NOK;
  264. goto exit;
  265. }
  266. break;
  267. case NCI_HCI_ADM_NOTIFY_PIPE_DELETED:
  268. if (skb->len != 1) {
  269. status = NCI_HCI_ANY_E_NOK;
  270. goto exit;
  271. }
  272. delete_info = (struct nci_hci_delete_pipe_noti *)skb->data;
  273. if (delete_info->pipe >= NCI_HCI_MAX_PIPES) {
  274. status = NCI_HCI_ANY_E_NOK;
  275. goto exit;
  276. }
  277. ndev->hci_dev->pipes[delete_info->pipe].gate =
  278. NCI_HCI_INVALID_GATE;
  279. ndev->hci_dev->pipes[delete_info->pipe].host =
  280. NCI_HCI_INVALID_HOST;
  281. break;
  282. case NCI_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED:
  283. if (skb->len != 1) {
  284. status = NCI_HCI_ANY_E_NOK;
  285. goto exit;
  286. }
  287. cleared_info =
  288. (struct nci_hci_all_pipe_cleared_noti *)skb->data;
  289. nci_hci_reset_pipes_per_host(ndev, cleared_info->host);
  290. break;
  291. default:
  292. pr_debug("Discarded unknown cmd %x to gate %x\n", cmd, gate);
  293. break;
  294. }
  295. if (ndev->ops->hci_cmd_received)
  296. ndev->ops->hci_cmd_received(ndev, pipe, cmd, skb);
  297. exit:
  298. nci_hci_send_data(ndev, pipe, status, NULL, 0);
  299. kfree_skb(skb);
  300. }
  301. static void nci_hci_resp_received(struct nci_dev *ndev, u8 pipe,
  302. struct sk_buff *skb)
  303. {
  304. struct nci_conn_info *conn_info;
  305. conn_info = ndev->hci_dev->conn_info;
  306. if (!conn_info)
  307. goto exit;
  308. conn_info->rx_skb = skb;
  309. exit:
  310. nci_req_complete(ndev, NCI_STATUS_OK);
  311. }
  312. /* Receive hcp message for pipe, with type and cmd.
  313. * skb contains optional message data only.
  314. */
  315. static void nci_hci_hcp_message_rx(struct nci_dev *ndev, u8 pipe,
  316. u8 type, u8 instruction, struct sk_buff *skb)
  317. {
  318. switch (type) {
  319. case NCI_HCI_HCP_RESPONSE:
  320. nci_hci_resp_received(ndev, pipe, skb);
  321. break;
  322. case NCI_HCI_HCP_COMMAND:
  323. nci_hci_cmd_received(ndev, pipe, instruction, skb);
  324. break;
  325. case NCI_HCI_HCP_EVENT:
  326. nci_hci_event_received(ndev, pipe, instruction, skb);
  327. break;
  328. default:
  329. pr_err("UNKNOWN MSG Type %d, instruction=%d\n",
  330. type, instruction);
  331. kfree_skb(skb);
  332. break;
  333. }
  334. nci_req_complete(ndev, NCI_STATUS_OK);
  335. }
  336. static void nci_hci_msg_rx_work(struct work_struct *work)
  337. {
  338. struct nci_hci_dev *hdev =
  339. container_of(work, struct nci_hci_dev, msg_rx_work);
  340. struct sk_buff *skb;
  341. const struct nci_hcp_message *message;
  342. u8 pipe, type, instruction;
  343. while ((skb = skb_dequeue(&hdev->msg_rx_queue)) != NULL) {
  344. pipe = NCI_HCP_MSG_GET_PIPE(skb->data[0]);
  345. skb_pull(skb, NCI_HCI_HCP_PACKET_HEADER_LEN);
  346. message = (struct nci_hcp_message *)skb->data;
  347. type = NCI_HCP_MSG_GET_TYPE(message->header);
  348. instruction = NCI_HCP_MSG_GET_CMD(message->header);
  349. skb_pull(skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
  350. nci_hci_hcp_message_rx(hdev->ndev, pipe,
  351. type, instruction, skb);
  352. }
  353. }
  354. void nci_hci_data_received_cb(void *context,
  355. struct sk_buff *skb, int err)
  356. {
  357. struct nci_dev *ndev = (struct nci_dev *)context;
  358. struct nci_hcp_packet *packet;
  359. u8 pipe, type;
  360. struct sk_buff *hcp_skb;
  361. struct sk_buff *frag_skb;
  362. int msg_len;
  363. if (err) {
  364. nci_req_complete(ndev, err);
  365. return;
  366. }
  367. packet = (struct nci_hcp_packet *)skb->data;
  368. if ((packet->header & ~NCI_HCI_FRAGMENT) == 0) {
  369. skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb);
  370. return;
  371. }
  372. /* it's the last fragment. Does it need re-aggregation? */
  373. if (skb_queue_len(&ndev->hci_dev->rx_hcp_frags)) {
  374. pipe = NCI_HCP_MSG_GET_PIPE(packet->header);
  375. skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb);
  376. msg_len = 0;
  377. skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) {
  378. msg_len += (frag_skb->len -
  379. NCI_HCI_HCP_PACKET_HEADER_LEN);
  380. }
  381. hcp_skb = nfc_alloc_recv_skb(NCI_HCI_HCP_PACKET_HEADER_LEN +
  382. msg_len, GFP_KERNEL);
  383. if (!hcp_skb) {
  384. nci_req_complete(ndev, -ENOMEM);
  385. return;
  386. }
  387. skb_put_u8(hcp_skb, pipe);
  388. skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) {
  389. msg_len = frag_skb->len - NCI_HCI_HCP_PACKET_HEADER_LEN;
  390. skb_put_data(hcp_skb,
  391. frag_skb->data + NCI_HCI_HCP_PACKET_HEADER_LEN,
  392. msg_len);
  393. }
  394. skb_queue_purge(&ndev->hci_dev->rx_hcp_frags);
  395. } else {
  396. packet->header &= NCI_HCI_FRAGMENT;
  397. hcp_skb = skb;
  398. }
  399. /* if this is a response, dispatch immediately to
  400. * unblock waiting cmd context. Otherwise, enqueue to dispatch
  401. * in separate context where handler can also execute command.
  402. */
  403. packet = (struct nci_hcp_packet *)hcp_skb->data;
  404. type = NCI_HCP_MSG_GET_TYPE(packet->message.header);
  405. if (type == NCI_HCI_HCP_RESPONSE) {
  406. pipe = NCI_HCP_MSG_GET_PIPE(packet->header);
  407. skb_pull(hcp_skb, NCI_HCI_HCP_PACKET_HEADER_LEN);
  408. nci_hci_hcp_message_rx(ndev, pipe, type,
  409. NCI_STATUS_OK, hcp_skb);
  410. } else {
  411. skb_queue_tail(&ndev->hci_dev->msg_rx_queue, hcp_skb);
  412. schedule_work(&ndev->hci_dev->msg_rx_work);
  413. }
  414. }
  415. int nci_hci_open_pipe(struct nci_dev *ndev, u8 pipe)
  416. {
  417. struct nci_data data;
  418. const struct nci_conn_info *conn_info;
  419. conn_info = ndev->hci_dev->conn_info;
  420. if (!conn_info)
  421. return -EPROTO;
  422. data.conn_id = conn_info->conn_id;
  423. data.pipe = pipe;
  424. data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
  425. NCI_HCI_ANY_OPEN_PIPE);
  426. data.data = NULL;
  427. data.data_len = 0;
  428. return nci_request(ndev, nci_hci_send_data_req, &data,
  429. msecs_to_jiffies(NCI_DATA_TIMEOUT));
  430. }
  431. EXPORT_SYMBOL(nci_hci_open_pipe);
  432. static u8 nci_hci_create_pipe(struct nci_dev *ndev, u8 dest_host,
  433. u8 dest_gate, int *result)
  434. {
  435. u8 pipe;
  436. struct sk_buff *skb;
  437. struct nci_hci_create_pipe_params params;
  438. const struct nci_hci_create_pipe_resp *resp;
  439. pr_debug("gate=%d\n", dest_gate);
  440. params.src_gate = NCI_HCI_ADMIN_GATE;
  441. params.dest_host = dest_host;
  442. params.dest_gate = dest_gate;
  443. *result = nci_hci_send_cmd(ndev, NCI_HCI_ADMIN_GATE,
  444. NCI_HCI_ADM_CREATE_PIPE,
  445. (u8 *)&params, sizeof(params), &skb);
  446. if (*result < 0)
  447. return NCI_HCI_INVALID_PIPE;
  448. resp = (struct nci_hci_create_pipe_resp *)skb->data;
  449. pipe = resp->pipe;
  450. kfree_skb(skb);
  451. pr_debug("pipe created=%d\n", pipe);
  452. return pipe;
  453. }
  454. static int nci_hci_delete_pipe(struct nci_dev *ndev, u8 pipe)
  455. {
  456. return nci_hci_send_cmd(ndev, NCI_HCI_ADMIN_GATE,
  457. NCI_HCI_ADM_DELETE_PIPE, &pipe, 1, NULL);
  458. }
  459. int nci_hci_set_param(struct nci_dev *ndev, u8 gate, u8 idx,
  460. const u8 *param, size_t param_len)
  461. {
  462. const struct nci_hcp_message *message;
  463. const struct nci_conn_info *conn_info;
  464. struct nci_data data;
  465. int r;
  466. u8 *tmp;
  467. u8 pipe = ndev->hci_dev->gate2pipe[gate];
  468. pr_debug("idx=%d to gate %d\n", idx, gate);
  469. if (pipe == NCI_HCI_INVALID_PIPE)
  470. return -EADDRNOTAVAIL;
  471. conn_info = ndev->hci_dev->conn_info;
  472. if (!conn_info)
  473. return -EPROTO;
  474. tmp = kmalloc(1 + param_len, GFP_KERNEL);
  475. if (!tmp)
  476. return -ENOMEM;
  477. *tmp = idx;
  478. memcpy(tmp + 1, param, param_len);
  479. data.conn_id = conn_info->conn_id;
  480. data.pipe = pipe;
  481. data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
  482. NCI_HCI_ANY_SET_PARAMETER);
  483. data.data = tmp;
  484. data.data_len = param_len + 1;
  485. r = nci_request(ndev, nci_hci_send_data_req, &data,
  486. msecs_to_jiffies(NCI_DATA_TIMEOUT));
  487. if (r == NCI_STATUS_OK) {
  488. message = (struct nci_hcp_message *)conn_info->rx_skb->data;
  489. r = nci_hci_result_to_errno(
  490. NCI_HCP_MSG_GET_CMD(message->header));
  491. skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
  492. }
  493. kfree(tmp);
  494. return r;
  495. }
  496. EXPORT_SYMBOL(nci_hci_set_param);
  497. int nci_hci_get_param(struct nci_dev *ndev, u8 gate, u8 idx,
  498. struct sk_buff **skb)
  499. {
  500. const struct nci_hcp_message *message;
  501. const struct nci_conn_info *conn_info;
  502. struct nci_data data;
  503. int r;
  504. u8 pipe = ndev->hci_dev->gate2pipe[gate];
  505. pr_debug("idx=%d to gate %d\n", idx, gate);
  506. if (pipe == NCI_HCI_INVALID_PIPE)
  507. return -EADDRNOTAVAIL;
  508. conn_info = ndev->hci_dev->conn_info;
  509. if (!conn_info)
  510. return -EPROTO;
  511. data.conn_id = conn_info->conn_id;
  512. data.pipe = pipe;
  513. data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
  514. NCI_HCI_ANY_GET_PARAMETER);
  515. data.data = &idx;
  516. data.data_len = 1;
  517. r = nci_request(ndev, nci_hci_send_data_req, &data,
  518. msecs_to_jiffies(NCI_DATA_TIMEOUT));
  519. if (r == NCI_STATUS_OK) {
  520. message = (struct nci_hcp_message *)conn_info->rx_skb->data;
  521. r = nci_hci_result_to_errno(
  522. NCI_HCP_MSG_GET_CMD(message->header));
  523. skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
  524. if (!r && skb)
  525. *skb = conn_info->rx_skb;
  526. }
  527. return r;
  528. }
  529. EXPORT_SYMBOL(nci_hci_get_param);
  530. int nci_hci_connect_gate(struct nci_dev *ndev,
  531. u8 dest_host, u8 dest_gate, u8 pipe)
  532. {
  533. bool pipe_created = false;
  534. int r;
  535. if (pipe == NCI_HCI_DO_NOT_OPEN_PIPE)
  536. return 0;
  537. if (ndev->hci_dev->gate2pipe[dest_gate] != NCI_HCI_INVALID_PIPE)
  538. return -EADDRINUSE;
  539. if (pipe != NCI_HCI_INVALID_PIPE)
  540. goto open_pipe;
  541. switch (dest_gate) {
  542. case NCI_HCI_LINK_MGMT_GATE:
  543. pipe = NCI_HCI_LINK_MGMT_PIPE;
  544. break;
  545. case NCI_HCI_ADMIN_GATE:
  546. pipe = NCI_HCI_ADMIN_PIPE;
  547. break;
  548. default:
  549. pipe = nci_hci_create_pipe(ndev, dest_host, dest_gate, &r);
  550. if (pipe == NCI_HCI_INVALID_PIPE)
  551. return r;
  552. pipe_created = true;
  553. break;
  554. }
  555. open_pipe:
  556. r = nci_hci_open_pipe(ndev, pipe);
  557. if (r < 0) {
  558. if (pipe_created) {
  559. if (nci_hci_delete_pipe(ndev, pipe) < 0) {
  560. /* TODO: Cannot clean by deleting pipe...
  561. * -> inconsistent state
  562. */
  563. }
  564. }
  565. return r;
  566. }
  567. ndev->hci_dev->pipes[pipe].gate = dest_gate;
  568. ndev->hci_dev->pipes[pipe].host = dest_host;
  569. ndev->hci_dev->gate2pipe[dest_gate] = pipe;
  570. return 0;
  571. }
  572. EXPORT_SYMBOL(nci_hci_connect_gate);
  573. static int nci_hci_dev_connect_gates(struct nci_dev *ndev,
  574. u8 gate_count,
  575. const struct nci_hci_gate *gates)
  576. {
  577. int r;
  578. while (gate_count--) {
  579. r = nci_hci_connect_gate(ndev, gates->dest_host,
  580. gates->gate, gates->pipe);
  581. if (r < 0)
  582. return r;
  583. gates++;
  584. }
  585. return 0;
  586. }
  587. int nci_hci_dev_session_init(struct nci_dev *ndev)
  588. {
  589. struct nci_conn_info *conn_info;
  590. struct sk_buff *skb;
  591. int r;
  592. ndev->hci_dev->count_pipes = 0;
  593. ndev->hci_dev->expected_pipes = 0;
  594. conn_info = ndev->hci_dev->conn_info;
  595. if (!conn_info)
  596. return -EPROTO;
  597. conn_info->data_exchange_cb = nci_hci_data_received_cb;
  598. conn_info->data_exchange_cb_context = ndev;
  599. nci_hci_reset_pipes(ndev->hci_dev);
  600. if (ndev->hci_dev->init_data.gates[0].gate != NCI_HCI_ADMIN_GATE)
  601. return -EPROTO;
  602. r = nci_hci_connect_gate(ndev,
  603. ndev->hci_dev->init_data.gates[0].dest_host,
  604. ndev->hci_dev->init_data.gates[0].gate,
  605. ndev->hci_dev->init_data.gates[0].pipe);
  606. if (r < 0)
  607. return r;
  608. r = nci_hci_get_param(ndev, NCI_HCI_ADMIN_GATE,
  609. NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY, &skb);
  610. if (r < 0)
  611. return r;
  612. if (skb->len &&
  613. skb->len == strlen(ndev->hci_dev->init_data.session_id) &&
  614. !memcmp(ndev->hci_dev->init_data.session_id, skb->data, skb->len) &&
  615. ndev->ops->hci_load_session) {
  616. /* Restore gate<->pipe table from some proprietary location. */
  617. r = ndev->ops->hci_load_session(ndev);
  618. } else {
  619. r = nci_hci_clear_all_pipes(ndev);
  620. if (r < 0)
  621. goto exit;
  622. r = nci_hci_dev_connect_gates(ndev,
  623. ndev->hci_dev->init_data.gate_count,
  624. ndev->hci_dev->init_data.gates);
  625. if (r < 0)
  626. goto exit;
  627. r = nci_hci_set_param(ndev, NCI_HCI_ADMIN_GATE,
  628. NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY,
  629. ndev->hci_dev->init_data.session_id,
  630. strlen(ndev->hci_dev->init_data.session_id));
  631. }
  632. exit:
  633. kfree_skb(skb);
  634. return r;
  635. }
  636. EXPORT_SYMBOL(nci_hci_dev_session_init);
  637. struct nci_hci_dev *nci_hci_allocate(struct nci_dev *ndev)
  638. {
  639. struct nci_hci_dev *hdev;
  640. hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
  641. if (!hdev)
  642. return NULL;
  643. skb_queue_head_init(&hdev->rx_hcp_frags);
  644. INIT_WORK(&hdev->msg_rx_work, nci_hci_msg_rx_work);
  645. skb_queue_head_init(&hdev->msg_rx_queue);
  646. hdev->ndev = ndev;
  647. return hdev;
  648. }
  649. void nci_hci_deallocate(struct nci_dev *ndev)
  650. {
  651. kfree(ndev->hci_dev);
  652. }