xref: /openbmc/linux/net/nfc/nci/hci.c (revision 96ac6d43)
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 
30 struct nci_data {
31 	u8              conn_id;
32 	u8              pipe;
33 	u8              cmd;
34 	const u8        *data;
35 	u32             data_len;
36 } __packed;
37 
38 struct nci_hci_create_pipe_params {
39 	u8 src_gate;
40 	u8 dest_host;
41 	u8 dest_gate;
42 } __packed;
43 
44 struct 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 
52 struct nci_hci_delete_pipe_noti {
53 	u8 pipe;
54 } __packed;
55 
56 struct nci_hci_all_pipe_cleared_noti {
57 	u8 host;
58 } __packed;
59 
60 struct nci_hcp_message {
61 	u8 header;      /* type -cmd,evt,rsp- + instruction */
62 	u8 data[];
63 } __packed;
64 
65 struct 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 
107 static 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 */
122 static 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 
133 static 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  */
153 static 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 	*(u8 *)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 		*(u8 *)skb_push(skb, 1) = cb;
188 
189 		if (len > 0)
190 			skb_put_data(skb, 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 
212 static 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 
220 int 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 }
232 EXPORT_SYMBOL(nci_hci_send_event);
233 
234 int 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 }
271 EXPORT_SYMBOL(nci_hci_send_cmd);
272 
273 int 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 }
285 EXPORT_SYMBOL(nci_hci_clear_all_pipes);
286 
287 static 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 
294 static 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 		if (new_pipe >= NCI_HCI_MAX_PIPES) {
316 			status = NCI_HCI_ANY_E_NOK;
317 			goto exit;
318 		}
319 
320 		/* Save the new created pipe and bind with local gate,
321 		 * the description for skb->data[3] is destination gate id
322 		 * but since we received this cmd from host controller, we
323 		 * are the destination and it is our local gate
324 		 */
325 		ndev->hci_dev->gate2pipe[dest_gate] = new_pipe;
326 		ndev->hci_dev->pipes[new_pipe].gate = dest_gate;
327 		ndev->hci_dev->pipes[new_pipe].host =
328 						create_info->src_host;
329 		break;
330 	case NCI_HCI_ANY_OPEN_PIPE:
331 		/* If the pipe is not created report an error */
332 		if (gate == NCI_HCI_INVALID_GATE) {
333 			status = NCI_HCI_ANY_E_NOK;
334 			goto exit;
335 		}
336 		break;
337 	case NCI_HCI_ADM_NOTIFY_PIPE_DELETED:
338 		if (skb->len != 1) {
339 			status = NCI_HCI_ANY_E_NOK;
340 			goto exit;
341 		}
342 		delete_info = (struct nci_hci_delete_pipe_noti *)skb->data;
343 		if (delete_info->pipe >= NCI_HCI_MAX_PIPES) {
344 			status = NCI_HCI_ANY_E_NOK;
345 			goto exit;
346 		}
347 
348 		ndev->hci_dev->pipes[delete_info->pipe].gate =
349 						NCI_HCI_INVALID_GATE;
350 		ndev->hci_dev->pipes[delete_info->pipe].host =
351 						NCI_HCI_INVALID_HOST;
352 		break;
353 	case NCI_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED:
354 		if (skb->len != 1) {
355 			status = NCI_HCI_ANY_E_NOK;
356 			goto exit;
357 		}
358 
359 		cleared_info =
360 			(struct nci_hci_all_pipe_cleared_noti *)skb->data;
361 		nci_hci_reset_pipes_per_host(ndev, cleared_info->host);
362 		break;
363 	default:
364 		pr_debug("Discarded unknown cmd %x to gate %x\n", cmd, gate);
365 		break;
366 	}
367 
368 	if (ndev->ops->hci_cmd_received)
369 		ndev->ops->hci_cmd_received(ndev, pipe, cmd, skb);
370 
371 exit:
372 	nci_hci_send_data(ndev, pipe, status, NULL, 0);
373 
374 	kfree_skb(skb);
375 }
376 
377 static void nci_hci_resp_received(struct nci_dev *ndev, u8 pipe,
378 				  u8 result, struct sk_buff *skb)
379 {
380 	struct nci_conn_info    *conn_info;
381 	u8 status = result;
382 
383 	conn_info = ndev->hci_dev->conn_info;
384 	if (!conn_info) {
385 		status = NCI_STATUS_REJECTED;
386 		goto exit;
387 	}
388 
389 	conn_info->rx_skb = skb;
390 
391 exit:
392 	nci_req_complete(ndev, NCI_STATUS_OK);
393 }
394 
395 /* Receive hcp message for pipe, with type and cmd.
396  * skb contains optional message data only.
397  */
398 static void nci_hci_hcp_message_rx(struct nci_dev *ndev, u8 pipe,
399 				   u8 type, u8 instruction, struct sk_buff *skb)
400 {
401 	switch (type) {
402 	case NCI_HCI_HCP_RESPONSE:
403 		nci_hci_resp_received(ndev, pipe, instruction, skb);
404 		break;
405 	case NCI_HCI_HCP_COMMAND:
406 		nci_hci_cmd_received(ndev, pipe, instruction, skb);
407 		break;
408 	case NCI_HCI_HCP_EVENT:
409 		nci_hci_event_received(ndev, pipe, instruction, skb);
410 		break;
411 	default:
412 		pr_err("UNKNOWN MSG Type %d, instruction=%d\n",
413 		       type, instruction);
414 		kfree_skb(skb);
415 		break;
416 	}
417 
418 	nci_req_complete(ndev, NCI_STATUS_OK);
419 }
420 
421 static void nci_hci_msg_rx_work(struct work_struct *work)
422 {
423 	struct nci_hci_dev *hdev =
424 		container_of(work, struct nci_hci_dev, msg_rx_work);
425 	struct sk_buff *skb;
426 	struct nci_hcp_message *message;
427 	u8 pipe, type, instruction;
428 
429 	while ((skb = skb_dequeue(&hdev->msg_rx_queue)) != NULL) {
430 		pipe = NCI_HCP_MSG_GET_PIPE(skb->data[0]);
431 		skb_pull(skb, NCI_HCI_HCP_PACKET_HEADER_LEN);
432 		message = (struct nci_hcp_message *)skb->data;
433 		type = NCI_HCP_MSG_GET_TYPE(message->header);
434 		instruction = NCI_HCP_MSG_GET_CMD(message->header);
435 		skb_pull(skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
436 
437 		nci_hci_hcp_message_rx(hdev->ndev, pipe,
438 				       type, instruction, skb);
439 	}
440 }
441 
442 void nci_hci_data_received_cb(void *context,
443 			      struct sk_buff *skb, int err)
444 {
445 	struct nci_dev *ndev = (struct nci_dev *)context;
446 	struct nci_hcp_packet *packet;
447 	u8 pipe, type;
448 	struct sk_buff *hcp_skb;
449 	struct sk_buff *frag_skb;
450 	int msg_len;
451 
452 	pr_debug("\n");
453 
454 	if (err) {
455 		nci_req_complete(ndev, err);
456 		return;
457 	}
458 
459 	packet = (struct nci_hcp_packet *)skb->data;
460 	if ((packet->header & ~NCI_HCI_FRAGMENT) == 0) {
461 		skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb);
462 		return;
463 	}
464 
465 	/* it's the last fragment. Does it need re-aggregation? */
466 	if (skb_queue_len(&ndev->hci_dev->rx_hcp_frags)) {
467 		pipe = NCI_HCP_MSG_GET_PIPE(packet->header);
468 		skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb);
469 
470 		msg_len = 0;
471 		skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) {
472 			msg_len += (frag_skb->len -
473 				    NCI_HCI_HCP_PACKET_HEADER_LEN);
474 		}
475 
476 		hcp_skb = nfc_alloc_recv_skb(NCI_HCI_HCP_PACKET_HEADER_LEN +
477 					     msg_len, GFP_KERNEL);
478 		if (!hcp_skb) {
479 			nci_req_complete(ndev, -ENOMEM);
480 			return;
481 		}
482 
483 		skb_put_u8(hcp_skb, pipe);
484 
485 		skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) {
486 			msg_len = frag_skb->len - NCI_HCI_HCP_PACKET_HEADER_LEN;
487 			skb_put_data(hcp_skb,
488 				     frag_skb->data + NCI_HCI_HCP_PACKET_HEADER_LEN,
489 				     msg_len);
490 		}
491 
492 		skb_queue_purge(&ndev->hci_dev->rx_hcp_frags);
493 	} else {
494 		packet->header &= NCI_HCI_FRAGMENT;
495 		hcp_skb = skb;
496 	}
497 
498 	/* if this is a response, dispatch immediately to
499 	 * unblock waiting cmd context. Otherwise, enqueue to dispatch
500 	 * in separate context where handler can also execute command.
501 	 */
502 	packet = (struct nci_hcp_packet *)hcp_skb->data;
503 	type = NCI_HCP_MSG_GET_TYPE(packet->message.header);
504 	if (type == NCI_HCI_HCP_RESPONSE) {
505 		pipe = NCI_HCP_MSG_GET_PIPE(packet->header);
506 		skb_pull(hcp_skb, NCI_HCI_HCP_PACKET_HEADER_LEN);
507 		nci_hci_hcp_message_rx(ndev, pipe, type,
508 				       NCI_STATUS_OK, hcp_skb);
509 	} else {
510 		skb_queue_tail(&ndev->hci_dev->msg_rx_queue, hcp_skb);
511 		schedule_work(&ndev->hci_dev->msg_rx_work);
512 	}
513 }
514 
515 int nci_hci_open_pipe(struct nci_dev *ndev, u8 pipe)
516 {
517 	struct nci_data data;
518 	struct nci_conn_info    *conn_info;
519 
520 	conn_info = ndev->hci_dev->conn_info;
521 	if (!conn_info)
522 		return -EPROTO;
523 
524 	data.conn_id = conn_info->conn_id;
525 	data.pipe = pipe;
526 	data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
527 				       NCI_HCI_ANY_OPEN_PIPE);
528 	data.data = NULL;
529 	data.data_len = 0;
530 
531 	return nci_request(ndev, nci_hci_send_data_req,
532 			(unsigned long)&data,
533 			msecs_to_jiffies(NCI_DATA_TIMEOUT));
534 }
535 EXPORT_SYMBOL(nci_hci_open_pipe);
536 
537 static u8 nci_hci_create_pipe(struct nci_dev *ndev, u8 dest_host,
538 			      u8 dest_gate, int *result)
539 {
540 	u8 pipe;
541 	struct sk_buff *skb;
542 	struct nci_hci_create_pipe_params params;
543 	struct nci_hci_create_pipe_resp *resp;
544 
545 	pr_debug("gate=%d\n", dest_gate);
546 
547 	params.src_gate = NCI_HCI_ADMIN_GATE;
548 	params.dest_host = dest_host;
549 	params.dest_gate = dest_gate;
550 
551 	*result = nci_hci_send_cmd(ndev, NCI_HCI_ADMIN_GATE,
552 				   NCI_HCI_ADM_CREATE_PIPE,
553 				   (u8 *)&params, sizeof(params), &skb);
554 	if (*result < 0)
555 		return NCI_HCI_INVALID_PIPE;
556 
557 	resp = (struct nci_hci_create_pipe_resp *)skb->data;
558 	pipe = resp->pipe;
559 	kfree_skb(skb);
560 
561 	pr_debug("pipe created=%d\n", pipe);
562 
563 	return pipe;
564 }
565 
566 static int nci_hci_delete_pipe(struct nci_dev *ndev, u8 pipe)
567 {
568 	pr_debug("\n");
569 
570 	return nci_hci_send_cmd(ndev, NCI_HCI_ADMIN_GATE,
571 				NCI_HCI_ADM_DELETE_PIPE, &pipe, 1, NULL);
572 }
573 
574 int nci_hci_set_param(struct nci_dev *ndev, u8 gate, u8 idx,
575 		      const u8 *param, size_t param_len)
576 {
577 	struct nci_hcp_message *message;
578 	struct nci_conn_info *conn_info;
579 	struct nci_data data;
580 	int r;
581 	u8 *tmp;
582 	u8 pipe = ndev->hci_dev->gate2pipe[gate];
583 
584 	pr_debug("idx=%d to gate %d\n", idx, gate);
585 
586 	if (pipe == NCI_HCI_INVALID_PIPE)
587 		return -EADDRNOTAVAIL;
588 
589 	conn_info = ndev->hci_dev->conn_info;
590 	if (!conn_info)
591 		return -EPROTO;
592 
593 	tmp = kmalloc(1 + param_len, GFP_KERNEL);
594 	if (!tmp)
595 		return -ENOMEM;
596 
597 	*tmp = idx;
598 	memcpy(tmp + 1, param, param_len);
599 
600 	data.conn_id = conn_info->conn_id;
601 	data.pipe = pipe;
602 	data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
603 				       NCI_HCI_ANY_SET_PARAMETER);
604 	data.data = tmp;
605 	data.data_len = param_len + 1;
606 
607 	r = nci_request(ndev, nci_hci_send_data_req,
608 			(unsigned long)&data,
609 			msecs_to_jiffies(NCI_DATA_TIMEOUT));
610 	if (r == NCI_STATUS_OK) {
611 		message = (struct nci_hcp_message *)conn_info->rx_skb->data;
612 		r = nci_hci_result_to_errno(
613 			NCI_HCP_MSG_GET_CMD(message->header));
614 		skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
615 	}
616 
617 	kfree(tmp);
618 	return r;
619 }
620 EXPORT_SYMBOL(nci_hci_set_param);
621 
622 int nci_hci_get_param(struct nci_dev *ndev, u8 gate, u8 idx,
623 		      struct sk_buff **skb)
624 {
625 	struct nci_hcp_message *message;
626 	struct nci_conn_info    *conn_info;
627 	struct nci_data data;
628 	int r;
629 	u8 pipe = ndev->hci_dev->gate2pipe[gate];
630 
631 	pr_debug("idx=%d to gate %d\n", idx, gate);
632 
633 	if (pipe == NCI_HCI_INVALID_PIPE)
634 		return -EADDRNOTAVAIL;
635 
636 	conn_info = ndev->hci_dev->conn_info;
637 	if (!conn_info)
638 		return -EPROTO;
639 
640 	data.conn_id = conn_info->conn_id;
641 	data.pipe = pipe;
642 	data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
643 				  NCI_HCI_ANY_GET_PARAMETER);
644 	data.data = &idx;
645 	data.data_len = 1;
646 
647 	r = nci_request(ndev, nci_hci_send_data_req, (unsigned long)&data,
648 			msecs_to_jiffies(NCI_DATA_TIMEOUT));
649 
650 	if (r == NCI_STATUS_OK) {
651 		message = (struct nci_hcp_message *)conn_info->rx_skb->data;
652 		r = nci_hci_result_to_errno(
653 			NCI_HCP_MSG_GET_CMD(message->header));
654 		skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
655 
656 		if (!r && skb)
657 			*skb = conn_info->rx_skb;
658 	}
659 
660 	return r;
661 }
662 EXPORT_SYMBOL(nci_hci_get_param);
663 
664 int nci_hci_connect_gate(struct nci_dev *ndev,
665 			 u8 dest_host, u8 dest_gate, u8 pipe)
666 {
667 	bool pipe_created = false;
668 	int r;
669 
670 	if (pipe == NCI_HCI_DO_NOT_OPEN_PIPE)
671 		return 0;
672 
673 	if (ndev->hci_dev->gate2pipe[dest_gate] != NCI_HCI_INVALID_PIPE)
674 		return -EADDRINUSE;
675 
676 	if (pipe != NCI_HCI_INVALID_PIPE)
677 		goto open_pipe;
678 
679 	switch (dest_gate) {
680 	case NCI_HCI_LINK_MGMT_GATE:
681 		pipe = NCI_HCI_LINK_MGMT_PIPE;
682 	break;
683 	case NCI_HCI_ADMIN_GATE:
684 		pipe = NCI_HCI_ADMIN_PIPE;
685 	break;
686 	default:
687 		pipe = nci_hci_create_pipe(ndev, dest_host, dest_gate, &r);
688 		if (pipe == NCI_HCI_INVALID_PIPE)
689 			return r;
690 		pipe_created = true;
691 		break;
692 	}
693 
694 open_pipe:
695 	r = nci_hci_open_pipe(ndev, pipe);
696 	if (r < 0) {
697 		if (pipe_created) {
698 			if (nci_hci_delete_pipe(ndev, pipe) < 0) {
699 				/* TODO: Cannot clean by deleting pipe...
700 				 * -> inconsistent state
701 				 */
702 			}
703 		}
704 		return r;
705 	}
706 
707 	ndev->hci_dev->pipes[pipe].gate = dest_gate;
708 	ndev->hci_dev->pipes[pipe].host = dest_host;
709 	ndev->hci_dev->gate2pipe[dest_gate] = pipe;
710 
711 	return 0;
712 }
713 EXPORT_SYMBOL(nci_hci_connect_gate);
714 
715 static int nci_hci_dev_connect_gates(struct nci_dev *ndev,
716 				     u8 gate_count,
717 				     struct nci_hci_gate *gates)
718 {
719 	int r;
720 
721 	while (gate_count--) {
722 		r = nci_hci_connect_gate(ndev, gates->dest_host,
723 					 gates->gate, gates->pipe);
724 		if (r < 0)
725 			return r;
726 		gates++;
727 	}
728 
729 	return 0;
730 }
731 
732 int nci_hci_dev_session_init(struct nci_dev *ndev)
733 {
734 	struct nci_conn_info    *conn_info;
735 	struct sk_buff *skb;
736 	int r;
737 
738 	ndev->hci_dev->count_pipes = 0;
739 	ndev->hci_dev->expected_pipes = 0;
740 
741 	conn_info = ndev->hci_dev->conn_info;
742 	if (!conn_info)
743 		return -EPROTO;
744 
745 	conn_info->data_exchange_cb = nci_hci_data_received_cb;
746 	conn_info->data_exchange_cb_context = ndev;
747 
748 	nci_hci_reset_pipes(ndev->hci_dev);
749 
750 	if (ndev->hci_dev->init_data.gates[0].gate != NCI_HCI_ADMIN_GATE)
751 		return -EPROTO;
752 
753 	r = nci_hci_connect_gate(ndev,
754 				 ndev->hci_dev->init_data.gates[0].dest_host,
755 				 ndev->hci_dev->init_data.gates[0].gate,
756 				 ndev->hci_dev->init_data.gates[0].pipe);
757 	if (r < 0)
758 		return r;
759 
760 	r = nci_hci_get_param(ndev, NCI_HCI_ADMIN_GATE,
761 			      NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY, &skb);
762 	if (r < 0)
763 		return r;
764 
765 	if (skb->len &&
766 	    skb->len == strlen(ndev->hci_dev->init_data.session_id) &&
767 	    !memcmp(ndev->hci_dev->init_data.session_id, skb->data, skb->len) &&
768 	    ndev->ops->hci_load_session) {
769 		/* Restore gate<->pipe table from some proprietary location. */
770 		r = ndev->ops->hci_load_session(ndev);
771 	} else {
772 		r = nci_hci_clear_all_pipes(ndev);
773 		if (r < 0)
774 			goto exit;
775 
776 		r = nci_hci_dev_connect_gates(ndev,
777 					      ndev->hci_dev->init_data.gate_count,
778 					      ndev->hci_dev->init_data.gates);
779 		if (r < 0)
780 			goto exit;
781 
782 		r = nci_hci_set_param(ndev, NCI_HCI_ADMIN_GATE,
783 				      NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY,
784 				      ndev->hci_dev->init_data.session_id,
785 				      strlen(ndev->hci_dev->init_data.session_id));
786 	}
787 
788 exit:
789 	kfree_skb(skb);
790 
791 	return r;
792 }
793 EXPORT_SYMBOL(nci_hci_dev_session_init);
794 
795 struct nci_hci_dev *nci_hci_allocate(struct nci_dev *ndev)
796 {
797 	struct nci_hci_dev *hdev;
798 
799 	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
800 	if (!hdev)
801 		return NULL;
802 
803 	skb_queue_head_init(&hdev->rx_hcp_frags);
804 	INIT_WORK(&hdev->msg_rx_work, nci_hci_msg_rx_work);
805 	skb_queue_head_init(&hdev->msg_rx_queue);
806 	hdev->ndev = ndev;
807 
808 	return hdev;
809 }
810