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