xref: /openbmc/linux/net/nfc/nci/hci.c (revision 360823a09426347ea8f232b0b0b5156d0aed0302)
1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
211f54f22SChristophe Ricard /*
311f54f22SChristophe Ricard  *  The NFC Controller Interface is the communication protocol between an
411f54f22SChristophe Ricard  *  NFC Controller (NFCC) and a Device Host (DH).
511f54f22SChristophe Ricard  *  This is the HCI over NCI implementation, as specified in the 10.2
611f54f22SChristophe Ricard  *  section of the NCI 1.1 specification.
711f54f22SChristophe Ricard  *
811f54f22SChristophe Ricard  *  Copyright (C) 2014  STMicroelectronics SAS. All rights reserved.
911f54f22SChristophe Ricard  */
1011f54f22SChristophe Ricard 
1111f54f22SChristophe Ricard #include <linux/skbuff.h>
1211f54f22SChristophe Ricard 
1311f54f22SChristophe Ricard #include "../nfc.h"
1411f54f22SChristophe Ricard #include <net/nfc/nci.h>
1511f54f22SChristophe Ricard #include <net/nfc/nci_core.h>
1611f54f22SChristophe Ricard #include <linux/nfc.h>
177e8cdc97SDmitry Vyukov #include <linux/kcov.h>
1811f54f22SChristophe Ricard 
1911f54f22SChristophe Ricard struct nci_data {
2011f54f22SChristophe Ricard 	u8 conn_id;
2111f54f22SChristophe Ricard 	u8 pipe;
2211f54f22SChristophe Ricard 	u8 cmd;
2311f54f22SChristophe Ricard 	const u8 *data;
2411f54f22SChristophe Ricard 	u32 data_len;
2511f54f22SChristophe Ricard } __packed;
2611f54f22SChristophe Ricard 
2711f54f22SChristophe Ricard struct nci_hci_create_pipe_params {
2811f54f22SChristophe Ricard 	u8 src_gate;
2911f54f22SChristophe Ricard 	u8 dest_host;
3011f54f22SChristophe Ricard 	u8 dest_gate;
3111f54f22SChristophe Ricard } __packed;
3211f54f22SChristophe Ricard 
3311f54f22SChristophe Ricard struct nci_hci_create_pipe_resp {
3411f54f22SChristophe Ricard 	u8 src_host;
3511f54f22SChristophe Ricard 	u8 src_gate;
3611f54f22SChristophe Ricard 	u8 dest_host;
3711f54f22SChristophe Ricard 	u8 dest_gate;
3811f54f22SChristophe Ricard 	u8 pipe;
3911f54f22SChristophe Ricard } __packed;
4011f54f22SChristophe Ricard 
4111f54f22SChristophe Ricard struct nci_hci_delete_pipe_noti {
4211f54f22SChristophe Ricard 	u8 pipe;
4311f54f22SChristophe Ricard } __packed;
4411f54f22SChristophe Ricard 
4511f54f22SChristophe Ricard struct nci_hci_all_pipe_cleared_noti {
4611f54f22SChristophe Ricard 	u8 host;
4711f54f22SChristophe Ricard } __packed;
4811f54f22SChristophe Ricard 
4911f54f22SChristophe Ricard struct nci_hcp_message {
5011f54f22SChristophe Ricard 	u8 header;      /* type -cmd,evt,rsp- + instruction */
5111f54f22SChristophe Ricard 	u8 data[];
5211f54f22SChristophe Ricard } __packed;
5311f54f22SChristophe Ricard 
5411f54f22SChristophe Ricard struct nci_hcp_packet {
5511f54f22SChristophe Ricard 	u8 header;      /* cbit+pipe */
5611f54f22SChristophe Ricard 	struct nci_hcp_message message;
5711f54f22SChristophe Ricard } __packed;
5811f54f22SChristophe Ricard 
5911f54f22SChristophe Ricard #define NCI_HCI_ANY_SET_PARAMETER  0x01
6011f54f22SChristophe Ricard #define NCI_HCI_ANY_GET_PARAMETER  0x02
6111f54f22SChristophe Ricard #define NCI_HCI_ANY_CLOSE_PIPE     0x04
62fa6fbadeSChristophe Ricard #define NCI_HCI_ADM_CLEAR_ALL_PIPE 0x14
6311f54f22SChristophe Ricard 
6411f54f22SChristophe Ricard #define NCI_HFP_NO_CHAINING        0x80
6511f54f22SChristophe Ricard 
6611f54f22SChristophe Ricard #define NCI_NFCEE_ID_HCI                0x80
6711f54f22SChristophe Ricard 
6811f54f22SChristophe Ricard #define NCI_EVT_HOT_PLUG           0x03
6911f54f22SChristophe Ricard 
7011f54f22SChristophe Ricard #define NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY       0x01
71a1b0b941SChristophe Ricard #define NCI_HCI_ADM_CREATE_PIPE			0x10
72a1b0b941SChristophe Ricard #define NCI_HCI_ADM_DELETE_PIPE			0x11
7311f54f22SChristophe Ricard 
7411f54f22SChristophe Ricard /* HCP headers */
7511f54f22SChristophe Ricard #define NCI_HCI_HCP_PACKET_HEADER_LEN      1
7611f54f22SChristophe Ricard #define NCI_HCI_HCP_MESSAGE_HEADER_LEN     1
7711f54f22SChristophe Ricard #define NCI_HCI_HCP_HEADER_LEN             2
7811f54f22SChristophe Ricard 
7911f54f22SChristophe Ricard /* HCP types */
8011f54f22SChristophe Ricard #define NCI_HCI_HCP_COMMAND        0x00
8111f54f22SChristophe Ricard #define NCI_HCI_HCP_EVENT          0x01
8211f54f22SChristophe Ricard #define NCI_HCI_HCP_RESPONSE       0x02
8311f54f22SChristophe Ricard 
8411f54f22SChristophe Ricard #define NCI_HCI_ADM_NOTIFY_PIPE_CREATED     0x12
8511f54f22SChristophe Ricard #define NCI_HCI_ADM_NOTIFY_PIPE_DELETED     0x13
8611f54f22SChristophe Ricard #define NCI_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED 0x15
8711f54f22SChristophe Ricard 
8811f54f22SChristophe Ricard #define NCI_HCI_FRAGMENT           0x7f
8911f54f22SChristophe Ricard #define NCI_HCP_HEADER(type, instr) ((((type) & 0x03) << 6) |\
9011f54f22SChristophe Ricard 				      ((instr) & 0x3f))
9111f54f22SChristophe Ricard 
9211f54f22SChristophe Ricard #define NCI_HCP_MSG_GET_TYPE(header) ((header & 0xc0) >> 6)
9311f54f22SChristophe Ricard #define NCI_HCP_MSG_GET_CMD(header)  (header & 0x3f)
9411f54f22SChristophe Ricard #define NCI_HCP_MSG_GET_PIPE(header) (header & 0x7f)
9511f54f22SChristophe Ricard 
nci_hci_result_to_errno(u8 result)96d8cd37edSChristophe Ricard static int nci_hci_result_to_errno(u8 result)
97d8cd37edSChristophe Ricard {
98d8cd37edSChristophe Ricard 	switch (result) {
99d8cd37edSChristophe Ricard 	case NCI_HCI_ANY_OK:
100d8cd37edSChristophe Ricard 		return 0;
101d8cd37edSChristophe Ricard 	case NCI_HCI_ANY_E_REG_PAR_UNKNOWN:
102d8cd37edSChristophe Ricard 		return -EOPNOTSUPP;
103d8cd37edSChristophe Ricard 	case NCI_HCI_ANY_E_TIMEOUT:
104d8cd37edSChristophe Ricard 		return -ETIME;
105d8cd37edSChristophe Ricard 	default:
106d8cd37edSChristophe Ricard 		return -1;
107d8cd37edSChristophe Ricard 	}
108d8cd37edSChristophe Ricard }
109d8cd37edSChristophe Ricard 
11011f54f22SChristophe Ricard /* HCI core */
nci_hci_reset_pipes(struct nci_hci_dev * hdev)11111f54f22SChristophe Ricard static void nci_hci_reset_pipes(struct nci_hci_dev *hdev)
11211f54f22SChristophe Ricard {
11311f54f22SChristophe Ricard 	int i;
11411f54f22SChristophe Ricard 
11511f54f22SChristophe Ricard 	for (i = 0; i < NCI_HCI_MAX_PIPES; i++) {
11611f54f22SChristophe Ricard 		hdev->pipes[i].gate = NCI_HCI_INVALID_GATE;
11711f54f22SChristophe Ricard 		hdev->pipes[i].host = NCI_HCI_INVALID_HOST;
11811f54f22SChristophe Ricard 	}
11911f54f22SChristophe Ricard 	memset(hdev->gate2pipe, NCI_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
12011f54f22SChristophe Ricard }
12111f54f22SChristophe Ricard 
nci_hci_reset_pipes_per_host(struct nci_dev * ndev,u8 host)12211f54f22SChristophe Ricard static void nci_hci_reset_pipes_per_host(struct nci_dev *ndev, u8 host)
12311f54f22SChristophe Ricard {
12411f54f22SChristophe Ricard 	int i;
12511f54f22SChristophe Ricard 
12611f54f22SChristophe Ricard 	for (i = 0; i < NCI_HCI_MAX_PIPES; i++) {
12711f54f22SChristophe Ricard 		if (ndev->hci_dev->pipes[i].host == host) {
12811f54f22SChristophe Ricard 			ndev->hci_dev->pipes[i].gate = NCI_HCI_INVALID_GATE;
12911f54f22SChristophe Ricard 			ndev->hci_dev->pipes[i].host = NCI_HCI_INVALID_HOST;
13011f54f22SChristophe Ricard 		}
13111f54f22SChristophe Ricard 	}
13211f54f22SChristophe Ricard }
13311f54f22SChristophe Ricard 
13411f54f22SChristophe Ricard /* Fragment HCI data over NCI packet.
13511f54f22SChristophe Ricard  * NFC Forum NCI 10.2.2 Data Exchange:
13611f54f22SChristophe Ricard  * The payload of the Data Packets sent on the Logical Connection SHALL be
13711f54f22SChristophe Ricard  * valid HCP packets, as defined within [ETSI_102622]. Each Data Packet SHALL
13811f54f22SChristophe Ricard  * contain a single HCP packet. NCI Segmentation and Reassembly SHALL NOT be
13911f54f22SChristophe Ricard  * applied to Data Messages in either direction. The HCI fragmentation mechanism
14011f54f22SChristophe Ricard  * is used if required.
14111f54f22SChristophe Ricard  */
nci_hci_send_data(struct nci_dev * ndev,u8 pipe,const u8 data_type,const u8 * data,size_t data_len)14211f54f22SChristophe Ricard static int nci_hci_send_data(struct nci_dev *ndev, u8 pipe,
14311f54f22SChristophe Ricard 			     const u8 data_type, const u8 *data,
14411f54f22SChristophe Ricard 			     size_t data_len)
14511f54f22SChristophe Ricard {
146ddecf555SKrzysztof Kozlowski 	const struct nci_conn_info *conn_info;
14711f54f22SChristophe Ricard 	struct sk_buff *skb;
14811f54f22SChristophe Ricard 	int len, i, r;
14911f54f22SChristophe Ricard 	u8 cb = pipe;
15011f54f22SChristophe Ricard 
15111f54f22SChristophe Ricard 	conn_info = ndev->hci_dev->conn_info;
15211f54f22SChristophe Ricard 	if (!conn_info)
15311f54f22SChristophe Ricard 		return -EPROTO;
15411f54f22SChristophe Ricard 
155500c4ef0SChristophe Ricard 	i = 0;
156500c4ef0SChristophe Ricard 	skb = nci_skb_alloc(ndev, conn_info->max_pkt_payload_len +
15723dd4581SDuoming Zhou 			    NCI_DATA_HDR_SIZE, GFP_ATOMIC);
15811f54f22SChristophe Ricard 	if (!skb)
15911f54f22SChristophe Ricard 		return -ENOMEM;
16011f54f22SChristophe Ricard 
161500c4ef0SChristophe Ricard 	skb_reserve(skb, NCI_DATA_HDR_SIZE + 2);
162d58ff351SJohannes Berg 	*(u8 *)skb_push(skb, 1) = data_type;
16311f54f22SChristophe Ricard 
164500c4ef0SChristophe Ricard 	do {
16511f54f22SChristophe Ricard 		/* If last packet add NCI_HFP_NO_CHAINING */
16611f54f22SChristophe Ricard 		if (i + conn_info->max_pkt_payload_len -
16711f54f22SChristophe Ricard 		    (skb->len + 1) >= data_len) {
16811f54f22SChristophe Ricard 			cb |= NCI_HFP_NO_CHAINING;
16911f54f22SChristophe Ricard 			len = data_len - i;
17011f54f22SChristophe Ricard 		} else {
17111f54f22SChristophe Ricard 			len = conn_info->max_pkt_payload_len - skb->len - 1;
17211f54f22SChristophe Ricard 		}
17311f54f22SChristophe Ricard 
174d58ff351SJohannes Berg 		*(u8 *)skb_push(skb, 1) = cb;
17511f54f22SChristophe Ricard 
17611f54f22SChristophe Ricard 		if (len > 0)
17759ae1d12SJohannes Berg 			skb_put_data(skb, data + i, len);
17811f54f22SChristophe Ricard 
17911f54f22SChristophe Ricard 		r = nci_send_data(ndev, conn_info->conn_id, skb);
18011f54f22SChristophe Ricard 		if (r < 0)
18111f54f22SChristophe Ricard 			return r;
18211f54f22SChristophe Ricard 
18311f54f22SChristophe Ricard 		i += len;
184500c4ef0SChristophe Ricard 
18511f54f22SChristophe Ricard 		if (i < data_len) {
186500c4ef0SChristophe Ricard 			skb = nci_skb_alloc(ndev,
187500c4ef0SChristophe Ricard 					    conn_info->max_pkt_payload_len +
18823dd4581SDuoming Zhou 					    NCI_DATA_HDR_SIZE, GFP_ATOMIC);
189500c4ef0SChristophe Ricard 			if (!skb)
190500c4ef0SChristophe Ricard 				return -ENOMEM;
191500c4ef0SChristophe Ricard 
192500c4ef0SChristophe Ricard 			skb_reserve(skb, NCI_DATA_HDR_SIZE + 1);
19311f54f22SChristophe Ricard 		}
19411f54f22SChristophe Ricard 	} while (i < data_len);
19511f54f22SChristophe Ricard 
19611f54f22SChristophe Ricard 	return i;
19711f54f22SChristophe Ricard }
19811f54f22SChristophe Ricard 
nci_hci_send_data_req(struct nci_dev * ndev,const void * opt)19935d7a6f1SKrzysztof Kozlowski static void nci_hci_send_data_req(struct nci_dev *ndev, const void *opt)
20011f54f22SChristophe Ricard {
20135d7a6f1SKrzysztof Kozlowski 	const struct nci_data *data = opt;
20211f54f22SChristophe Ricard 
20311f54f22SChristophe Ricard 	nci_hci_send_data(ndev, data->pipe, data->cmd,
20411f54f22SChristophe Ricard 			  data->data, data->data_len);
20511f54f22SChristophe Ricard }
20611f54f22SChristophe Ricard 
nci_hci_send_event(struct nci_dev * ndev,u8 gate,u8 event,const u8 * param,size_t param_len)20711f54f22SChristophe Ricard int nci_hci_send_event(struct nci_dev *ndev, u8 gate, u8 event,
20811f54f22SChristophe Ricard 		       const u8 *param, size_t param_len)
20911f54f22SChristophe Ricard {
21011f54f22SChristophe Ricard 	u8 pipe = ndev->hci_dev->gate2pipe[gate];
21111f54f22SChristophe Ricard 
21211f54f22SChristophe Ricard 	if (pipe == NCI_HCI_INVALID_PIPE)
21311f54f22SChristophe Ricard 		return -EADDRNOTAVAIL;
21411f54f22SChristophe Ricard 
21511f54f22SChristophe Ricard 	return nci_hci_send_data(ndev, pipe,
21611f54f22SChristophe Ricard 			NCI_HCP_HEADER(NCI_HCI_HCP_EVENT, event),
21711f54f22SChristophe Ricard 			param, param_len);
21811f54f22SChristophe Ricard }
21911f54f22SChristophe Ricard EXPORT_SYMBOL(nci_hci_send_event);
22011f54f22SChristophe Ricard 
nci_hci_send_cmd(struct nci_dev * ndev,u8 gate,u8 cmd,const u8 * param,size_t param_len,struct sk_buff ** skb)22111f54f22SChristophe Ricard int nci_hci_send_cmd(struct nci_dev *ndev, u8 gate, u8 cmd,
22211f54f22SChristophe Ricard 		     const u8 *param, size_t param_len,
22311f54f22SChristophe Ricard 		     struct sk_buff **skb)
22411f54f22SChristophe Ricard {
225ddecf555SKrzysztof Kozlowski 	const struct nci_hcp_message *message;
226ddecf555SKrzysztof Kozlowski 	const struct nci_conn_info *conn_info;
22711f54f22SChristophe Ricard 	struct nci_data data;
22811f54f22SChristophe Ricard 	int r;
22911f54f22SChristophe Ricard 	u8 pipe = ndev->hci_dev->gate2pipe[gate];
23011f54f22SChristophe Ricard 
23111f54f22SChristophe Ricard 	if (pipe == NCI_HCI_INVALID_PIPE)
23211f54f22SChristophe Ricard 		return -EADDRNOTAVAIL;
23311f54f22SChristophe Ricard 
23411f54f22SChristophe Ricard 	conn_info = ndev->hci_dev->conn_info;
23511f54f22SChristophe Ricard 	if (!conn_info)
23611f54f22SChristophe Ricard 		return -EPROTO;
23711f54f22SChristophe Ricard 
23811f54f22SChristophe Ricard 	data.conn_id = conn_info->conn_id;
23911f54f22SChristophe Ricard 	data.pipe = pipe;
24011f54f22SChristophe Ricard 	data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND, cmd);
24111f54f22SChristophe Ricard 	data.data = param;
24211f54f22SChristophe Ricard 	data.data_len = param_len;
24311f54f22SChristophe Ricard 
24435d7a6f1SKrzysztof Kozlowski 	r = nci_request(ndev, nci_hci_send_data_req, &data,
24511f54f22SChristophe Ricard 			msecs_to_jiffies(NCI_DATA_TIMEOUT));
246d8cd37edSChristophe Ricard 	if (r == NCI_STATUS_OK) {
247d8cd37edSChristophe Ricard 		message = (struct nci_hcp_message *)conn_info->rx_skb->data;
248d8cd37edSChristophe Ricard 		r = nci_hci_result_to_errno(
249d8cd37edSChristophe Ricard 			NCI_HCP_MSG_GET_CMD(message->header));
250d8cd37edSChristophe Ricard 		skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
25111f54f22SChristophe Ricard 
252d8cd37edSChristophe Ricard 		if (!r && skb)
25311f54f22SChristophe Ricard 			*skb = conn_info->rx_skb;
254d8cd37edSChristophe Ricard 	}
25511f54f22SChristophe Ricard 
25611f54f22SChristophe Ricard 	return r;
25711f54f22SChristophe Ricard }
25811f54f22SChristophe Ricard EXPORT_SYMBOL(nci_hci_send_cmd);
25911f54f22SChristophe Ricard 
nci_hci_clear_all_pipes(struct nci_dev * ndev)260fa6fbadeSChristophe Ricard int nci_hci_clear_all_pipes(struct nci_dev *ndev)
261fa6fbadeSChristophe Ricard {
262fa6fbadeSChristophe Ricard 	int r;
263fa6fbadeSChristophe Ricard 
264fa6fbadeSChristophe Ricard 	r = nci_hci_send_cmd(ndev, NCI_HCI_ADMIN_GATE,
265fa6fbadeSChristophe Ricard 			     NCI_HCI_ADM_CLEAR_ALL_PIPE, NULL, 0, NULL);
266fa6fbadeSChristophe Ricard 	if (r < 0)
267fa6fbadeSChristophe Ricard 		return r;
268fa6fbadeSChristophe Ricard 
269fa6fbadeSChristophe Ricard 	nci_hci_reset_pipes(ndev->hci_dev);
270fa6fbadeSChristophe Ricard 	return r;
271fa6fbadeSChristophe Ricard }
272fa6fbadeSChristophe Ricard EXPORT_SYMBOL(nci_hci_clear_all_pipes);
273fa6fbadeSChristophe Ricard 
nci_hci_event_received(struct nci_dev * ndev,u8 pipe,u8 event,struct sk_buff * skb)27411f54f22SChristophe Ricard static void nci_hci_event_received(struct nci_dev *ndev, u8 pipe,
27511f54f22SChristophe Ricard 				   u8 event, struct sk_buff *skb)
27611f54f22SChristophe Ricard {
27711f54f22SChristophe Ricard 	if (ndev->ops->hci_event_received)
27811f54f22SChristophe Ricard 		ndev->ops->hci_event_received(ndev, pipe, event, skb);
27911f54f22SChristophe Ricard }
28011f54f22SChristophe Ricard 
nci_hci_cmd_received(struct nci_dev * ndev,u8 pipe,u8 cmd,struct sk_buff * skb)28111f54f22SChristophe Ricard static void nci_hci_cmd_received(struct nci_dev *ndev, u8 pipe,
28211f54f22SChristophe Ricard 				 u8 cmd, struct sk_buff *skb)
28311f54f22SChristophe Ricard {
28411f54f22SChristophe Ricard 	u8 gate = ndev->hci_dev->pipes[pipe].gate;
28511f54f22SChristophe Ricard 	u8 status = NCI_HCI_ANY_OK | ~NCI_HCI_FRAGMENT;
28611f54f22SChristophe Ricard 	u8 dest_gate, new_pipe;
28711f54f22SChristophe Ricard 	struct nci_hci_create_pipe_resp *create_info;
28811f54f22SChristophe Ricard 	struct nci_hci_delete_pipe_noti *delete_info;
28911f54f22SChristophe Ricard 	struct nci_hci_all_pipe_cleared_noti *cleared_info;
29011f54f22SChristophe Ricard 
29111f54f22SChristophe Ricard 	pr_debug("from gate %x pipe %x cmd %x\n", gate, pipe, cmd);
29211f54f22SChristophe Ricard 
29311f54f22SChristophe Ricard 	switch (cmd) {
29411f54f22SChristophe Ricard 	case NCI_HCI_ADM_NOTIFY_PIPE_CREATED:
29511f54f22SChristophe Ricard 		if (skb->len != 5) {
29611f54f22SChristophe Ricard 			status = NCI_HCI_ANY_E_NOK;
29711f54f22SChristophe Ricard 			goto exit;
29811f54f22SChristophe Ricard 		}
29911f54f22SChristophe Ricard 		create_info = (struct nci_hci_create_pipe_resp *)skb->data;
30011f54f22SChristophe Ricard 		dest_gate = create_info->dest_gate;
30111f54f22SChristophe Ricard 		new_pipe = create_info->pipe;
302d7ee81adSDan Carpenter 		if (new_pipe >= NCI_HCI_MAX_PIPES) {
303d7ee81adSDan Carpenter 			status = NCI_HCI_ANY_E_NOK;
304d7ee81adSDan Carpenter 			goto exit;
305d7ee81adSDan Carpenter 		}
30611f54f22SChristophe Ricard 
30711f54f22SChristophe Ricard 		/* Save the new created pipe and bind with local gate,
30811f54f22SChristophe Ricard 		 * the description for skb->data[3] is destination gate id
30911f54f22SChristophe Ricard 		 * but since we received this cmd from host controller, we
31011f54f22SChristophe Ricard 		 * are the destination and it is our local gate
31111f54f22SChristophe Ricard 		 */
31211f54f22SChristophe Ricard 		ndev->hci_dev->gate2pipe[dest_gate] = new_pipe;
31311f54f22SChristophe Ricard 		ndev->hci_dev->pipes[new_pipe].gate = dest_gate;
31411f54f22SChristophe Ricard 		ndev->hci_dev->pipes[new_pipe].host =
31511f54f22SChristophe Ricard 						create_info->src_host;
31611f54f22SChristophe Ricard 		break;
31711f54f22SChristophe Ricard 	case NCI_HCI_ANY_OPEN_PIPE:
31811f54f22SChristophe Ricard 		/* If the pipe is not created report an error */
31911f54f22SChristophe Ricard 		if (gate == NCI_HCI_INVALID_GATE) {
32011f54f22SChristophe Ricard 			status = NCI_HCI_ANY_E_NOK;
32111f54f22SChristophe Ricard 			goto exit;
32211f54f22SChristophe Ricard 		}
32311f54f22SChristophe Ricard 		break;
32411f54f22SChristophe Ricard 	case NCI_HCI_ADM_NOTIFY_PIPE_DELETED:
32511f54f22SChristophe Ricard 		if (skb->len != 1) {
32611f54f22SChristophe Ricard 			status = NCI_HCI_ANY_E_NOK;
32711f54f22SChristophe Ricard 			goto exit;
32811f54f22SChristophe Ricard 		}
32911f54f22SChristophe Ricard 		delete_info = (struct nci_hci_delete_pipe_noti *)skb->data;
330d7ee81adSDan Carpenter 		if (delete_info->pipe >= NCI_HCI_MAX_PIPES) {
331d7ee81adSDan Carpenter 			status = NCI_HCI_ANY_E_NOK;
332d7ee81adSDan Carpenter 			goto exit;
333d7ee81adSDan Carpenter 		}
33411f54f22SChristophe Ricard 
33511f54f22SChristophe Ricard 		ndev->hci_dev->pipes[delete_info->pipe].gate =
33611f54f22SChristophe Ricard 						NCI_HCI_INVALID_GATE;
33711f54f22SChristophe Ricard 		ndev->hci_dev->pipes[delete_info->pipe].host =
33811f54f22SChristophe Ricard 						NCI_HCI_INVALID_HOST;
33911f54f22SChristophe Ricard 		break;
34011f54f22SChristophe Ricard 	case NCI_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED:
34111f54f22SChristophe Ricard 		if (skb->len != 1) {
34211f54f22SChristophe Ricard 			status = NCI_HCI_ANY_E_NOK;
34311f54f22SChristophe Ricard 			goto exit;
34411f54f22SChristophe Ricard 		}
34511f54f22SChristophe Ricard 
34611f54f22SChristophe Ricard 		cleared_info =
34711f54f22SChristophe Ricard 			(struct nci_hci_all_pipe_cleared_noti *)skb->data;
34811f54f22SChristophe Ricard 		nci_hci_reset_pipes_per_host(ndev, cleared_info->host);
34911f54f22SChristophe Ricard 		break;
35011f54f22SChristophe Ricard 	default:
35111f54f22SChristophe Ricard 		pr_debug("Discarded unknown cmd %x to gate %x\n", cmd, gate);
35211f54f22SChristophe Ricard 		break;
35311f54f22SChristophe Ricard 	}
35411f54f22SChristophe Ricard 
35511f54f22SChristophe Ricard 	if (ndev->ops->hci_cmd_received)
35611f54f22SChristophe Ricard 		ndev->ops->hci_cmd_received(ndev, pipe, cmd, skb);
35711f54f22SChristophe Ricard 
35811f54f22SChristophe Ricard exit:
35911f54f22SChristophe Ricard 	nci_hci_send_data(ndev, pipe, status, NULL, 0);
36011f54f22SChristophe Ricard 
36111f54f22SChristophe Ricard 	kfree_skb(skb);
36211f54f22SChristophe Ricard }
36311f54f22SChristophe Ricard 
nci_hci_resp_received(struct nci_dev * ndev,u8 pipe,struct sk_buff * skb)36411f54f22SChristophe Ricard static void nci_hci_resp_received(struct nci_dev *ndev, u8 pipe,
36549b1cabfSAlex Shi 				  struct sk_buff *skb)
36611f54f22SChristophe Ricard {
36711f54f22SChristophe Ricard 	struct nci_conn_info *conn_info;
36811f54f22SChristophe Ricard 
36911f54f22SChristophe Ricard 	conn_info = ndev->hci_dev->conn_info;
37049b1cabfSAlex Shi 	if (!conn_info)
37111f54f22SChristophe Ricard 		goto exit;
37211f54f22SChristophe Ricard 
37311f54f22SChristophe Ricard 	conn_info->rx_skb = skb;
37411f54f22SChristophe Ricard 
37511f54f22SChristophe Ricard exit:
376d8cd37edSChristophe Ricard 	nci_req_complete(ndev, NCI_STATUS_OK);
37711f54f22SChristophe Ricard }
37811f54f22SChristophe Ricard 
37911f54f22SChristophe Ricard /* Receive hcp message for pipe, with type and cmd.
38011f54f22SChristophe Ricard  * skb contains optional message data only.
38111f54f22SChristophe Ricard  */
nci_hci_hcp_message_rx(struct nci_dev * ndev,u8 pipe,u8 type,u8 instruction,struct sk_buff * skb)38211f54f22SChristophe Ricard static void nci_hci_hcp_message_rx(struct nci_dev *ndev, u8 pipe,
38311f54f22SChristophe Ricard 				   u8 type, u8 instruction, struct sk_buff *skb)
38411f54f22SChristophe Ricard {
38511f54f22SChristophe Ricard 	switch (type) {
38611f54f22SChristophe Ricard 	case NCI_HCI_HCP_RESPONSE:
38749b1cabfSAlex Shi 		nci_hci_resp_received(ndev, pipe, skb);
38811f54f22SChristophe Ricard 		break;
38911f54f22SChristophe Ricard 	case NCI_HCI_HCP_COMMAND:
39011f54f22SChristophe Ricard 		nci_hci_cmd_received(ndev, pipe, instruction, skb);
39111f54f22SChristophe Ricard 		break;
39211f54f22SChristophe Ricard 	case NCI_HCI_HCP_EVENT:
39311f54f22SChristophe Ricard 		nci_hci_event_received(ndev, pipe, instruction, skb);
39411f54f22SChristophe Ricard 		break;
39511f54f22SChristophe Ricard 	default:
39611f54f22SChristophe Ricard 		pr_err("UNKNOWN MSG Type %d, instruction=%d\n",
39711f54f22SChristophe Ricard 		       type, instruction);
39811f54f22SChristophe Ricard 		kfree_skb(skb);
39911f54f22SChristophe Ricard 		break;
40011f54f22SChristophe Ricard 	}
40111f54f22SChristophe Ricard 
402064d0047SChristophe Ricard 	nci_req_complete(ndev, NCI_STATUS_OK);
40311f54f22SChristophe Ricard }
40411f54f22SChristophe Ricard 
nci_hci_msg_rx_work(struct work_struct * work)40511f54f22SChristophe Ricard static void nci_hci_msg_rx_work(struct work_struct *work)
40611f54f22SChristophe Ricard {
40711f54f22SChristophe Ricard 	struct nci_hci_dev *hdev =
40811f54f22SChristophe Ricard 		container_of(work, struct nci_hci_dev, msg_rx_work);
40911f54f22SChristophe Ricard 	struct sk_buff *skb;
410ddecf555SKrzysztof Kozlowski 	const struct nci_hcp_message *message;
41111f54f22SChristophe Ricard 	u8 pipe, type, instruction;
41211f54f22SChristophe Ricard 
4137e8cdc97SDmitry Vyukov 	for (; (skb = skb_dequeue(&hdev->msg_rx_queue)); kcov_remote_stop()) {
4147e8cdc97SDmitry Vyukov 		kcov_remote_start_common(skb_get_kcov_handle(skb));
415e65917b6SChristophe Ricard 		pipe = NCI_HCP_MSG_GET_PIPE(skb->data[0]);
41611f54f22SChristophe Ricard 		skb_pull(skb, NCI_HCI_HCP_PACKET_HEADER_LEN);
41711f54f22SChristophe Ricard 		message = (struct nci_hcp_message *)skb->data;
41811f54f22SChristophe Ricard 		type = NCI_HCP_MSG_GET_TYPE(message->header);
41911f54f22SChristophe Ricard 		instruction = NCI_HCP_MSG_GET_CMD(message->header);
42011f54f22SChristophe Ricard 		skb_pull(skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
42111f54f22SChristophe Ricard 
42211f54f22SChristophe Ricard 		nci_hci_hcp_message_rx(hdev->ndev, pipe,
42311f54f22SChristophe Ricard 				       type, instruction, skb);
42411f54f22SChristophe Ricard 	}
42511f54f22SChristophe Ricard }
42611f54f22SChristophe Ricard 
nci_hci_data_received_cb(void * context,struct sk_buff * skb,int err)42711f54f22SChristophe Ricard void nci_hci_data_received_cb(void *context,
42811f54f22SChristophe Ricard 			      struct sk_buff *skb, int err)
42911f54f22SChristophe Ricard {
43011f54f22SChristophe Ricard 	struct nci_dev *ndev = (struct nci_dev *)context;
43111f54f22SChristophe Ricard 	struct nci_hcp_packet *packet;
432d8cd37edSChristophe Ricard 	u8 pipe, type;
43311f54f22SChristophe Ricard 	struct sk_buff *hcp_skb;
43411f54f22SChristophe Ricard 	struct sk_buff *frag_skb;
43511f54f22SChristophe Ricard 	int msg_len;
43611f54f22SChristophe Ricard 
43711f54f22SChristophe Ricard 	if (err) {
43811f54f22SChristophe Ricard 		nci_req_complete(ndev, err);
43911f54f22SChristophe Ricard 		return;
44011f54f22SChristophe Ricard 	}
44111f54f22SChristophe Ricard 
44211f54f22SChristophe Ricard 	packet = (struct nci_hcp_packet *)skb->data;
44311f54f22SChristophe Ricard 	if ((packet->header & ~NCI_HCI_FRAGMENT) == 0) {
44411f54f22SChristophe Ricard 		skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb);
44511f54f22SChristophe Ricard 		return;
44611f54f22SChristophe Ricard 	}
44711f54f22SChristophe Ricard 
44811f54f22SChristophe Ricard 	/* it's the last fragment. Does it need re-aggregation? */
44911f54f22SChristophe Ricard 	if (skb_queue_len(&ndev->hci_dev->rx_hcp_frags)) {
450e65917b6SChristophe Ricard 		pipe = NCI_HCP_MSG_GET_PIPE(packet->header);
45111f54f22SChristophe Ricard 		skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb);
45211f54f22SChristophe Ricard 
45311f54f22SChristophe Ricard 		msg_len = 0;
45411f54f22SChristophe Ricard 		skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) {
45511f54f22SChristophe Ricard 			msg_len += (frag_skb->len -
45611f54f22SChristophe Ricard 				    NCI_HCI_HCP_PACKET_HEADER_LEN);
45711f54f22SChristophe Ricard 		}
45811f54f22SChristophe Ricard 
45911f54f22SChristophe Ricard 		hcp_skb = nfc_alloc_recv_skb(NCI_HCI_HCP_PACKET_HEADER_LEN +
46011f54f22SChristophe Ricard 					     msg_len, GFP_KERNEL);
46111f54f22SChristophe Ricard 		if (!hcp_skb) {
46211f54f22SChristophe Ricard 			nci_req_complete(ndev, -ENOMEM);
46311f54f22SChristophe Ricard 			return;
46411f54f22SChristophe Ricard 		}
46511f54f22SChristophe Ricard 
466634fef61SJohannes Berg 		skb_put_u8(hcp_skb, pipe);
46711f54f22SChristophe Ricard 
46811f54f22SChristophe Ricard 		skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) {
46911f54f22SChristophe Ricard 			msg_len = frag_skb->len - NCI_HCI_HCP_PACKET_HEADER_LEN;
47059ae1d12SJohannes Berg 			skb_put_data(hcp_skb,
47159ae1d12SJohannes Berg 				     frag_skb->data + NCI_HCI_HCP_PACKET_HEADER_LEN,
47259ae1d12SJohannes Berg 				     msg_len);
47311f54f22SChristophe Ricard 		}
47411f54f22SChristophe Ricard 
47511f54f22SChristophe Ricard 		skb_queue_purge(&ndev->hci_dev->rx_hcp_frags);
47611f54f22SChristophe Ricard 	} else {
47711f54f22SChristophe Ricard 		packet->header &= NCI_HCI_FRAGMENT;
47811f54f22SChristophe Ricard 		hcp_skb = skb;
47911f54f22SChristophe Ricard 	}
48011f54f22SChristophe Ricard 
48111f54f22SChristophe Ricard 	/* if this is a response, dispatch immediately to
48211f54f22SChristophe Ricard 	 * unblock waiting cmd context. Otherwise, enqueue to dispatch
48311f54f22SChristophe Ricard 	 * in separate context where handler can also execute command.
48411f54f22SChristophe Ricard 	 */
48511f54f22SChristophe Ricard 	packet = (struct nci_hcp_packet *)hcp_skb->data;
48611f54f22SChristophe Ricard 	type = NCI_HCP_MSG_GET_TYPE(packet->message.header);
48711f54f22SChristophe Ricard 	if (type == NCI_HCI_HCP_RESPONSE) {
488d8cd37edSChristophe Ricard 		pipe = NCI_HCP_MSG_GET_PIPE(packet->header);
489d8cd37edSChristophe Ricard 		skb_pull(hcp_skb, NCI_HCI_HCP_PACKET_HEADER_LEN);
490d8cd37edSChristophe Ricard 		nci_hci_hcp_message_rx(ndev, pipe, type,
491d8cd37edSChristophe Ricard 				       NCI_STATUS_OK, hcp_skb);
49211f54f22SChristophe Ricard 	} else {
49311f54f22SChristophe Ricard 		skb_queue_tail(&ndev->hci_dev->msg_rx_queue, hcp_skb);
49411f54f22SChristophe Ricard 		schedule_work(&ndev->hci_dev->msg_rx_work);
49511f54f22SChristophe Ricard 	}
49611f54f22SChristophe Ricard }
49711f54f22SChristophe Ricard 
nci_hci_open_pipe(struct nci_dev * ndev,u8 pipe)49811f54f22SChristophe Ricard int nci_hci_open_pipe(struct nci_dev *ndev, u8 pipe)
49911f54f22SChristophe Ricard {
50011f54f22SChristophe Ricard 	struct nci_data data;
501ddecf555SKrzysztof Kozlowski 	const struct nci_conn_info *conn_info;
50211f54f22SChristophe Ricard 
50311f54f22SChristophe Ricard 	conn_info = ndev->hci_dev->conn_info;
50411f54f22SChristophe Ricard 	if (!conn_info)
50511f54f22SChristophe Ricard 		return -EPROTO;
50611f54f22SChristophe Ricard 
50711f54f22SChristophe Ricard 	data.conn_id = conn_info->conn_id;
50811f54f22SChristophe Ricard 	data.pipe = pipe;
50911f54f22SChristophe Ricard 	data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
51011f54f22SChristophe Ricard 				       NCI_HCI_ANY_OPEN_PIPE);
51111f54f22SChristophe Ricard 	data.data = NULL;
51211f54f22SChristophe Ricard 	data.data_len = 0;
51311f54f22SChristophe Ricard 
51435d7a6f1SKrzysztof Kozlowski 	return nci_request(ndev, nci_hci_send_data_req, &data,
51511f54f22SChristophe Ricard 			   msecs_to_jiffies(NCI_DATA_TIMEOUT));
51611f54f22SChristophe Ricard }
51711f54f22SChristophe Ricard EXPORT_SYMBOL(nci_hci_open_pipe);
51811f54f22SChristophe Ricard 
nci_hci_create_pipe(struct nci_dev * ndev,u8 dest_host,u8 dest_gate,int * result)519a1b0b941SChristophe Ricard static u8 nci_hci_create_pipe(struct nci_dev *ndev, u8 dest_host,
520a1b0b941SChristophe Ricard 			      u8 dest_gate, int *result)
521a1b0b941SChristophe Ricard {
522a1b0b941SChristophe Ricard 	u8 pipe;
523a1b0b941SChristophe Ricard 	struct sk_buff *skb;
524a1b0b941SChristophe Ricard 	struct nci_hci_create_pipe_params params;
525ddecf555SKrzysztof Kozlowski 	const struct nci_hci_create_pipe_resp *resp;
526a1b0b941SChristophe Ricard 
527a1b0b941SChristophe Ricard 	pr_debug("gate=%d\n", dest_gate);
528a1b0b941SChristophe Ricard 
529a1b0b941SChristophe Ricard 	params.src_gate = NCI_HCI_ADMIN_GATE;
530a1b0b941SChristophe Ricard 	params.dest_host = dest_host;
531a1b0b941SChristophe Ricard 	params.dest_gate = dest_gate;
532a1b0b941SChristophe Ricard 
533a1b0b941SChristophe Ricard 	*result = nci_hci_send_cmd(ndev, NCI_HCI_ADMIN_GATE,
534a1b0b941SChristophe Ricard 				   NCI_HCI_ADM_CREATE_PIPE,
535a1b0b941SChristophe Ricard 				   (u8 *)&params, sizeof(params), &skb);
536a1b0b941SChristophe Ricard 	if (*result < 0)
537a1b0b941SChristophe Ricard 		return NCI_HCI_INVALID_PIPE;
538a1b0b941SChristophe Ricard 
539a1b0b941SChristophe Ricard 	resp = (struct nci_hci_create_pipe_resp *)skb->data;
540a1b0b941SChristophe Ricard 	pipe = resp->pipe;
541a1b0b941SChristophe Ricard 	kfree_skb(skb);
542a1b0b941SChristophe Ricard 
543a1b0b941SChristophe Ricard 	pr_debug("pipe created=%d\n", pipe);
544a1b0b941SChristophe Ricard 
545*172cdfc3SDan Carpenter 	if (pipe >= NCI_HCI_MAX_PIPES)
546*172cdfc3SDan Carpenter 		pipe = NCI_HCI_INVALID_PIPE;
547a1b0b941SChristophe Ricard 	return pipe;
548a1b0b941SChristophe Ricard }
549a1b0b941SChristophe Ricard 
nci_hci_delete_pipe(struct nci_dev * ndev,u8 pipe)550a1b0b941SChristophe Ricard static int nci_hci_delete_pipe(struct nci_dev *ndev, u8 pipe)
551a1b0b941SChristophe Ricard {
552a1b0b941SChristophe Ricard 	return nci_hci_send_cmd(ndev, NCI_HCI_ADMIN_GATE,
553a1b0b941SChristophe Ricard 				NCI_HCI_ADM_DELETE_PIPE, &pipe, 1, NULL);
554a1b0b941SChristophe Ricard }
555a1b0b941SChristophe Ricard 
nci_hci_set_param(struct nci_dev * ndev,u8 gate,u8 idx,const u8 * param,size_t param_len)55611f54f22SChristophe Ricard int nci_hci_set_param(struct nci_dev *ndev, u8 gate, u8 idx,
55711f54f22SChristophe Ricard 		      const u8 *param, size_t param_len)
55811f54f22SChristophe Ricard {
559ddecf555SKrzysztof Kozlowski 	const struct nci_hcp_message *message;
560ddecf555SKrzysztof Kozlowski 	const struct nci_conn_info *conn_info;
56111f54f22SChristophe Ricard 	struct nci_data data;
56211f54f22SChristophe Ricard 	int r;
56311f54f22SChristophe Ricard 	u8 *tmp;
56411f54f22SChristophe Ricard 	u8 pipe = ndev->hci_dev->gate2pipe[gate];
56511f54f22SChristophe Ricard 
56611f54f22SChristophe Ricard 	pr_debug("idx=%d to gate %d\n", idx, gate);
56711f54f22SChristophe Ricard 
56811f54f22SChristophe Ricard 	if (pipe == NCI_HCI_INVALID_PIPE)
56911f54f22SChristophe Ricard 		return -EADDRNOTAVAIL;
57011f54f22SChristophe Ricard 
57111f54f22SChristophe Ricard 	conn_info = ndev->hci_dev->conn_info;
57211f54f22SChristophe Ricard 	if (!conn_info)
57311f54f22SChristophe Ricard 		return -EPROTO;
57411f54f22SChristophe Ricard 
57511f54f22SChristophe Ricard 	tmp = kmalloc(1 + param_len, GFP_KERNEL);
57611f54f22SChristophe Ricard 	if (!tmp)
57711f54f22SChristophe Ricard 		return -ENOMEM;
57811f54f22SChristophe Ricard 
57911f54f22SChristophe Ricard 	*tmp = idx;
58011f54f22SChristophe Ricard 	memcpy(tmp + 1, param, param_len);
58111f54f22SChristophe Ricard 
58211f54f22SChristophe Ricard 	data.conn_id = conn_info->conn_id;
58311f54f22SChristophe Ricard 	data.pipe = pipe;
58411f54f22SChristophe Ricard 	data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
58511f54f22SChristophe Ricard 				       NCI_HCI_ANY_SET_PARAMETER);
58611f54f22SChristophe Ricard 	data.data = tmp;
58711f54f22SChristophe Ricard 	data.data_len = param_len + 1;
58811f54f22SChristophe Ricard 
58935d7a6f1SKrzysztof Kozlowski 	r = nci_request(ndev, nci_hci_send_data_req, &data,
59011f54f22SChristophe Ricard 			msecs_to_jiffies(NCI_DATA_TIMEOUT));
591d8cd37edSChristophe Ricard 	if (r == NCI_STATUS_OK) {
592d8cd37edSChristophe Ricard 		message = (struct nci_hcp_message *)conn_info->rx_skb->data;
593d8cd37edSChristophe Ricard 		r = nci_hci_result_to_errno(
594d8cd37edSChristophe Ricard 			NCI_HCP_MSG_GET_CMD(message->header));
595d8cd37edSChristophe Ricard 		skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
596d8cd37edSChristophe Ricard 	}
59711f54f22SChristophe Ricard 
59811f54f22SChristophe Ricard 	kfree(tmp);
59911f54f22SChristophe Ricard 	return r;
60011f54f22SChristophe Ricard }
60111f54f22SChristophe Ricard EXPORT_SYMBOL(nci_hci_set_param);
60211f54f22SChristophe Ricard 
nci_hci_get_param(struct nci_dev * ndev,u8 gate,u8 idx,struct sk_buff ** skb)60311f54f22SChristophe Ricard int nci_hci_get_param(struct nci_dev *ndev, u8 gate, u8 idx,
60411f54f22SChristophe Ricard 		      struct sk_buff **skb)
60511f54f22SChristophe Ricard {
606ddecf555SKrzysztof Kozlowski 	const struct nci_hcp_message *message;
607ddecf555SKrzysztof Kozlowski 	const struct nci_conn_info *conn_info;
60811f54f22SChristophe Ricard 	struct nci_data data;
60911f54f22SChristophe Ricard 	int r;
61011f54f22SChristophe Ricard 	u8 pipe = ndev->hci_dev->gate2pipe[gate];
61111f54f22SChristophe Ricard 
61211f54f22SChristophe Ricard 	pr_debug("idx=%d to gate %d\n", idx, gate);
61311f54f22SChristophe Ricard 
61411f54f22SChristophe Ricard 	if (pipe == NCI_HCI_INVALID_PIPE)
61511f54f22SChristophe Ricard 		return -EADDRNOTAVAIL;
61611f54f22SChristophe Ricard 
61711f54f22SChristophe Ricard 	conn_info = ndev->hci_dev->conn_info;
61811f54f22SChristophe Ricard 	if (!conn_info)
61911f54f22SChristophe Ricard 		return -EPROTO;
62011f54f22SChristophe Ricard 
62111f54f22SChristophe Ricard 	data.conn_id = conn_info->conn_id;
62211f54f22SChristophe Ricard 	data.pipe = pipe;
62311f54f22SChristophe Ricard 	data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
62411f54f22SChristophe Ricard 				  NCI_HCI_ANY_GET_PARAMETER);
62511f54f22SChristophe Ricard 	data.data = &idx;
62611f54f22SChristophe Ricard 	data.data_len = 1;
62711f54f22SChristophe Ricard 
62835d7a6f1SKrzysztof Kozlowski 	r = nci_request(ndev, nci_hci_send_data_req, &data,
62911f54f22SChristophe Ricard 			msecs_to_jiffies(NCI_DATA_TIMEOUT));
63011f54f22SChristophe Ricard 
631d8cd37edSChristophe Ricard 	if (r == NCI_STATUS_OK) {
632d8cd37edSChristophe Ricard 		message = (struct nci_hcp_message *)conn_info->rx_skb->data;
633d8cd37edSChristophe Ricard 		r = nci_hci_result_to_errno(
634d8cd37edSChristophe Ricard 			NCI_HCP_MSG_GET_CMD(message->header));
635d8cd37edSChristophe Ricard 		skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
636d8cd37edSChristophe Ricard 
637d8cd37edSChristophe Ricard 		if (!r && skb)
63811f54f22SChristophe Ricard 			*skb = conn_info->rx_skb;
639d8cd37edSChristophe Ricard 	}
64011f54f22SChristophe Ricard 
64111f54f22SChristophe Ricard 	return r;
64211f54f22SChristophe Ricard }
64311f54f22SChristophe Ricard EXPORT_SYMBOL(nci_hci_get_param);
64411f54f22SChristophe Ricard 
nci_hci_connect_gate(struct nci_dev * ndev,u8 dest_host,u8 dest_gate,u8 pipe)64511f54f22SChristophe Ricard int nci_hci_connect_gate(struct nci_dev *ndev,
64611f54f22SChristophe Ricard 			 u8 dest_host, u8 dest_gate, u8 pipe)
64711f54f22SChristophe Ricard {
648a1b0b941SChristophe Ricard 	bool pipe_created = false;
64911f54f22SChristophe Ricard 	int r;
65011f54f22SChristophe Ricard 
65111f54f22SChristophe Ricard 	if (pipe == NCI_HCI_DO_NOT_OPEN_PIPE)
65211f54f22SChristophe Ricard 		return 0;
65311f54f22SChristophe Ricard 
65411f54f22SChristophe Ricard 	if (ndev->hci_dev->gate2pipe[dest_gate] != NCI_HCI_INVALID_PIPE)
65511f54f22SChristophe Ricard 		return -EADDRINUSE;
65611f54f22SChristophe Ricard 
65711f54f22SChristophe Ricard 	if (pipe != NCI_HCI_INVALID_PIPE)
65811f54f22SChristophe Ricard 		goto open_pipe;
65911f54f22SChristophe Ricard 
66011f54f22SChristophe Ricard 	switch (dest_gate) {
66111f54f22SChristophe Ricard 	case NCI_HCI_LINK_MGMT_GATE:
66211f54f22SChristophe Ricard 		pipe = NCI_HCI_LINK_MGMT_PIPE;
66311f54f22SChristophe Ricard 	break;
66411f54f22SChristophe Ricard 	case NCI_HCI_ADMIN_GATE:
66511f54f22SChristophe Ricard 		pipe = NCI_HCI_ADMIN_PIPE;
66611f54f22SChristophe Ricard 	break;
667a1b0b941SChristophe Ricard 	default:
668a1b0b941SChristophe Ricard 		pipe = nci_hci_create_pipe(ndev, dest_host, dest_gate, &r);
6692a84193fSChristophe Ricard 		if (pipe == NCI_HCI_INVALID_PIPE)
670a1b0b941SChristophe Ricard 			return r;
671a1b0b941SChristophe Ricard 		pipe_created = true;
672a1b0b941SChristophe Ricard 		break;
67311f54f22SChristophe Ricard 	}
67411f54f22SChristophe Ricard 
67511f54f22SChristophe Ricard open_pipe:
67611f54f22SChristophe Ricard 	r = nci_hci_open_pipe(ndev, pipe);
677a1b0b941SChristophe Ricard 	if (r < 0) {
678a1b0b941SChristophe Ricard 		if (pipe_created) {
679a1b0b941SChristophe Ricard 			if (nci_hci_delete_pipe(ndev, pipe) < 0) {
680a1b0b941SChristophe Ricard 				/* TODO: Cannot clean by deleting pipe...
681a1b0b941SChristophe Ricard 				 * -> inconsistent state
682a1b0b941SChristophe Ricard 				 */
683a1b0b941SChristophe Ricard 			}
684a1b0b941SChristophe Ricard 		}
68511f54f22SChristophe Ricard 		return r;
686a1b0b941SChristophe Ricard 	}
68711f54f22SChristophe Ricard 
68811f54f22SChristophe Ricard 	ndev->hci_dev->pipes[pipe].gate = dest_gate;
68911f54f22SChristophe Ricard 	ndev->hci_dev->pipes[pipe].host = dest_host;
69011f54f22SChristophe Ricard 	ndev->hci_dev->gate2pipe[dest_gate] = pipe;
69111f54f22SChristophe Ricard 
69211f54f22SChristophe Ricard 	return 0;
69311f54f22SChristophe Ricard }
69411f54f22SChristophe Ricard EXPORT_SYMBOL(nci_hci_connect_gate);
69511f54f22SChristophe Ricard 
nci_hci_dev_connect_gates(struct nci_dev * ndev,u8 gate_count,const struct nci_hci_gate * gates)69611f54f22SChristophe Ricard static int nci_hci_dev_connect_gates(struct nci_dev *ndev,
69711f54f22SChristophe Ricard 				     u8 gate_count,
698ddecf555SKrzysztof Kozlowski 				     const struct nci_hci_gate *gates)
69911f54f22SChristophe Ricard {
70011f54f22SChristophe Ricard 	int r;
70111f54f22SChristophe Ricard 
70211f54f22SChristophe Ricard 	while (gate_count--) {
70311f54f22SChristophe Ricard 		r = nci_hci_connect_gate(ndev, gates->dest_host,
70411f54f22SChristophe Ricard 					 gates->gate, gates->pipe);
70511f54f22SChristophe Ricard 		if (r < 0)
70611f54f22SChristophe Ricard 			return r;
70711f54f22SChristophe Ricard 		gates++;
70811f54f22SChristophe Ricard 	}
70911f54f22SChristophe Ricard 
71011f54f22SChristophe Ricard 	return 0;
71111f54f22SChristophe Ricard }
71211f54f22SChristophe Ricard 
nci_hci_dev_session_init(struct nci_dev * ndev)71311f54f22SChristophe Ricard int nci_hci_dev_session_init(struct nci_dev *ndev)
71411f54f22SChristophe Ricard {
71515d4a8daSChristophe Ricard 	struct nci_conn_info *conn_info;
71611f54f22SChristophe Ricard 	struct sk_buff *skb;
71711f54f22SChristophe Ricard 	int r;
71811f54f22SChristophe Ricard 
71911f54f22SChristophe Ricard 	ndev->hci_dev->count_pipes = 0;
72011f54f22SChristophe Ricard 	ndev->hci_dev->expected_pipes = 0;
72111f54f22SChristophe Ricard 
72215d4a8daSChristophe Ricard 	conn_info = ndev->hci_dev->conn_info;
72315d4a8daSChristophe Ricard 	if (!conn_info)
72415d4a8daSChristophe Ricard 		return -EPROTO;
72515d4a8daSChristophe Ricard 
72615d4a8daSChristophe Ricard 	conn_info->data_exchange_cb = nci_hci_data_received_cb;
72715d4a8daSChristophe Ricard 	conn_info->data_exchange_cb_context = ndev;
72815d4a8daSChristophe Ricard 
72911f54f22SChristophe Ricard 	nci_hci_reset_pipes(ndev->hci_dev);
73011f54f22SChristophe Ricard 
73111f54f22SChristophe Ricard 	if (ndev->hci_dev->init_data.gates[0].gate != NCI_HCI_ADMIN_GATE)
73211f54f22SChristophe Ricard 		return -EPROTO;
73311f54f22SChristophe Ricard 
73411f54f22SChristophe Ricard 	r = nci_hci_connect_gate(ndev,
73511f54f22SChristophe Ricard 				 ndev->hci_dev->init_data.gates[0].dest_host,
73611f54f22SChristophe Ricard 				 ndev->hci_dev->init_data.gates[0].gate,
73711f54f22SChristophe Ricard 				 ndev->hci_dev->init_data.gates[0].pipe);
73811f54f22SChristophe Ricard 	if (r < 0)
7392622e2a0SJoe Perches 		return r;
74011f54f22SChristophe Ricard 
74111f54f22SChristophe Ricard 	r = nci_hci_get_param(ndev, NCI_HCI_ADMIN_GATE,
74211f54f22SChristophe Ricard 			      NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY, &skb);
74311f54f22SChristophe Ricard 	if (r < 0)
7442622e2a0SJoe Perches 		return r;
74511f54f22SChristophe Ricard 
74611f54f22SChristophe Ricard 	if (skb->len &&
74711f54f22SChristophe Ricard 	    skb->len == strlen(ndev->hci_dev->init_data.session_id) &&
7482622e2a0SJoe Perches 	    !memcmp(ndev->hci_dev->init_data.session_id, skb->data, skb->len) &&
74911f54f22SChristophe Ricard 	    ndev->ops->hci_load_session) {
75011f54f22SChristophe Ricard 		/* Restore gate<->pipe table from some proprietary location. */
75111f54f22SChristophe Ricard 		r = ndev->ops->hci_load_session(ndev);
75211f54f22SChristophe Ricard 	} else {
7538a49943fSChristophe Ricard 		r = nci_hci_clear_all_pipes(ndev);
7548a49943fSChristophe Ricard 		if (r < 0)
7558a49943fSChristophe Ricard 			goto exit;
7568a49943fSChristophe Ricard 
75711f54f22SChristophe Ricard 		r = nci_hci_dev_connect_gates(ndev,
75811f54f22SChristophe Ricard 					      ndev->hci_dev->init_data.gate_count,
75911f54f22SChristophe Ricard 					      ndev->hci_dev->init_data.gates);
76011f54f22SChristophe Ricard 		if (r < 0)
76111f54f22SChristophe Ricard 			goto exit;
76211f54f22SChristophe Ricard 
76311f54f22SChristophe Ricard 		r = nci_hci_set_param(ndev, NCI_HCI_ADMIN_GATE,
76411f54f22SChristophe Ricard 				      NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY,
76511f54f22SChristophe Ricard 				      ndev->hci_dev->init_data.session_id,
76611f54f22SChristophe Ricard 				      strlen(ndev->hci_dev->init_data.session_id));
76711f54f22SChristophe Ricard 	}
76811f54f22SChristophe Ricard 
76911f54f22SChristophe Ricard exit:
77011f54f22SChristophe Ricard 	kfree_skb(skb);
77111f54f22SChristophe Ricard 
77211f54f22SChristophe Ricard 	return r;
77311f54f22SChristophe Ricard }
77411f54f22SChristophe Ricard EXPORT_SYMBOL(nci_hci_dev_session_init);
77511f54f22SChristophe Ricard 
nci_hci_allocate(struct nci_dev * ndev)77611f54f22SChristophe Ricard struct nci_hci_dev *nci_hci_allocate(struct nci_dev *ndev)
77711f54f22SChristophe Ricard {
77811f54f22SChristophe Ricard 	struct nci_hci_dev *hdev;
77911f54f22SChristophe Ricard 
78011f54f22SChristophe Ricard 	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
78111f54f22SChristophe Ricard 	if (!hdev)
78211f54f22SChristophe Ricard 		return NULL;
78311f54f22SChristophe Ricard 
78411f54f22SChristophe Ricard 	skb_queue_head_init(&hdev->rx_hcp_frags);
78511f54f22SChristophe Ricard 	INIT_WORK(&hdev->msg_rx_work, nci_hci_msg_rx_work);
78611f54f22SChristophe Ricard 	skb_queue_head_init(&hdev->msg_rx_queue);
78711f54f22SChristophe Ricard 	hdev->ndev = ndev;
78811f54f22SChristophe Ricard 
78911f54f22SChristophe Ricard 	return hdev;
79011f54f22SChristophe Ricard }
791e0652f8bSDongliang Mu 
nci_hci_deallocate(struct nci_dev * ndev)792e0652f8bSDongliang Mu void nci_hci_deallocate(struct nci_dev *ndev)
793e0652f8bSDongliang Mu {
794e0652f8bSDongliang Mu 	kfree(ndev->hci_dev);
795e0652f8bSDongliang Mu }
796