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