hci.c 20 KB

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