1b06ebda0SMatthew Dillon /*
2b06ebda0SMatthew Dillon  * ng_hci_cmds.c
3b06ebda0SMatthew Dillon  */
4b06ebda0SMatthew Dillon 
5b06ebda0SMatthew Dillon /*-
6b06ebda0SMatthew Dillon  * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
7b06ebda0SMatthew Dillon  * All rights reserved.
8b06ebda0SMatthew Dillon  *
9b06ebda0SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
10b06ebda0SMatthew Dillon  * modification, are permitted provided that the following conditions
11b06ebda0SMatthew Dillon  * are met:
12b06ebda0SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
13b06ebda0SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
14b06ebda0SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
15b06ebda0SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
16b06ebda0SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
17b06ebda0SMatthew Dillon  *
18b06ebda0SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19b06ebda0SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20b06ebda0SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21b06ebda0SMatthew Dillon  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22b06ebda0SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23b06ebda0SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24b06ebda0SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25b06ebda0SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26b06ebda0SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27b06ebda0SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28b06ebda0SMatthew Dillon  * SUCH DAMAGE.
29b06ebda0SMatthew Dillon  *
30b06ebda0SMatthew Dillon  * $Id: ng_hci_cmds.c,v 1.4 2003/09/08 18:57:51 max Exp $
31b06ebda0SMatthew Dillon  * $FreeBSD: src/sys/netgraph/bluetooth/hci/ng_hci_cmds.c,v 1.7 2005/01/07 01:45:43 imp Exp $
32b06ebda0SMatthew Dillon  */
33b06ebda0SMatthew Dillon 
34b06ebda0SMatthew Dillon #include <sys/param.h>
35b06ebda0SMatthew Dillon #include <sys/systm.h>
36b06ebda0SMatthew Dillon #include <sys/kernel.h>
37b06ebda0SMatthew Dillon #include <sys/endian.h>
38b06ebda0SMatthew Dillon #include <sys/malloc.h>
39b06ebda0SMatthew Dillon #include <sys/mbuf.h>
40b06ebda0SMatthew Dillon #include <sys/queue.h>
41e85b99abSSascha Wildner #include <netgraph7/ng_message.h>
42e85b99abSSascha Wildner #include <netgraph7/netgraph.h>
43e85b99abSSascha Wildner #include <netgraph7/bluetooth/include/ng_bluetooth.h>
44e85b99abSSascha Wildner #include <netgraph7/bluetooth/include/ng_hci.h>
45e85b99abSSascha Wildner #include <netgraph7/bluetooth/hci/ng_hci_var.h>
46e85b99abSSascha Wildner #include <netgraph7/bluetooth/hci/ng_hci_cmds.h>
47e85b99abSSascha Wildner #include <netgraph7/bluetooth/hci/ng_hci_evnt.h>
48e85b99abSSascha Wildner #include <netgraph7/bluetooth/hci/ng_hci_ulpi.h>
49e85b99abSSascha Wildner #include <netgraph7/bluetooth/hci/ng_hci_misc.h>
50b06ebda0SMatthew Dillon 
51b06ebda0SMatthew Dillon /******************************************************************************
52b06ebda0SMatthew Dillon  ******************************************************************************
53b06ebda0SMatthew Dillon  **                     HCI commands processing module
54b06ebda0SMatthew Dillon  ******************************************************************************
55b06ebda0SMatthew Dillon  ******************************************************************************/
56b06ebda0SMatthew Dillon 
57b06ebda0SMatthew Dillon #undef	min
58b06ebda0SMatthew Dillon #define	min(a, b)	((a) < (b))? (a) : (b)
59b06ebda0SMatthew Dillon 
60b06ebda0SMatthew Dillon static int  complete_command (ng_hci_unit_p, int, struct mbuf **);
61b06ebda0SMatthew Dillon 
62b06ebda0SMatthew Dillon static int process_link_control_params
63b06ebda0SMatthew Dillon 	(ng_hci_unit_p, u_int16_t, struct mbuf *, struct mbuf *);
64b06ebda0SMatthew Dillon static int process_link_policy_params
65b06ebda0SMatthew Dillon 	(ng_hci_unit_p, u_int16_t, struct mbuf *, struct mbuf *);
66b06ebda0SMatthew Dillon static int process_hc_baseband_params
67b06ebda0SMatthew Dillon 	(ng_hci_unit_p, u_int16_t, struct mbuf *, struct mbuf *);
68b06ebda0SMatthew Dillon static int process_info_params
69b06ebda0SMatthew Dillon 	(ng_hci_unit_p, u_int16_t, struct mbuf *, struct mbuf *);
70b06ebda0SMatthew Dillon static int process_status_params
71b06ebda0SMatthew Dillon 	(ng_hci_unit_p, u_int16_t, struct mbuf *, struct mbuf *);
72b06ebda0SMatthew Dillon static int process_testing_params
73b06ebda0SMatthew Dillon 	(ng_hci_unit_p, u_int16_t, struct mbuf *, struct mbuf *);
74b06ebda0SMatthew Dillon 
75b06ebda0SMatthew Dillon static int process_link_control_status
76b06ebda0SMatthew Dillon 	(ng_hci_unit_p, ng_hci_command_status_ep *, struct mbuf *);
77b06ebda0SMatthew Dillon static int process_link_policy_status
78b06ebda0SMatthew Dillon 	(ng_hci_unit_p, ng_hci_command_status_ep *, struct mbuf *);
79b06ebda0SMatthew Dillon 
80b06ebda0SMatthew Dillon /*
81b06ebda0SMatthew Dillon  * Send HCI command to the driver.
82b06ebda0SMatthew Dillon  */
83b06ebda0SMatthew Dillon 
84b06ebda0SMatthew Dillon int
ng_hci_send_command(ng_hci_unit_p unit)85b06ebda0SMatthew Dillon ng_hci_send_command(ng_hci_unit_p unit)
86b06ebda0SMatthew Dillon {
87b06ebda0SMatthew Dillon 	struct mbuf	*m0 = NULL, *m = NULL;
88b06ebda0SMatthew Dillon 	int		 free, error = 0;
89b06ebda0SMatthew Dillon 
90b06ebda0SMatthew Dillon 	/* Check if other command is pending */
91b06ebda0SMatthew Dillon 	if (unit->state & NG_HCI_UNIT_COMMAND_PENDING)
92b06ebda0SMatthew Dillon 		return (0);
93b06ebda0SMatthew Dillon 
94b06ebda0SMatthew Dillon 	/* Check if unit can accept our command */
95b06ebda0SMatthew Dillon 	NG_HCI_BUFF_CMD_GET(unit->buffer, free);
96b06ebda0SMatthew Dillon 	if (free == 0)
97b06ebda0SMatthew Dillon 		return (0);
98b06ebda0SMatthew Dillon 
99b06ebda0SMatthew Dillon 	/* Check if driver hook is still ok */
100b06ebda0SMatthew Dillon 	if (unit->drv == NULL || NG_HOOK_NOT_VALID(unit->drv)) {
101b06ebda0SMatthew Dillon 		NG_HCI_WARN(
102b06ebda0SMatthew Dillon "%s: %s - hook \"%s\" is not connected or valid\n",
103b06ebda0SMatthew Dillon 			__func__, NG_NODE_NAME(unit->node), NG_HCI_HOOK_DRV);
104b06ebda0SMatthew Dillon 
105b06ebda0SMatthew Dillon 		NG_BT_MBUFQ_DRAIN(&unit->cmdq);
106b06ebda0SMatthew Dillon 
107b06ebda0SMatthew Dillon 		return (ENOTCONN);
108b06ebda0SMatthew Dillon 	}
109b06ebda0SMatthew Dillon 
110b06ebda0SMatthew Dillon 	/*
111b06ebda0SMatthew Dillon 	 * Get first command from queue, give it to RAW hook then
112b06ebda0SMatthew Dillon 	 * make copy of it and send it to the driver
113b06ebda0SMatthew Dillon 	 */
114b06ebda0SMatthew Dillon 
115b06ebda0SMatthew Dillon 	m0 = NG_BT_MBUFQ_FIRST(&unit->cmdq);
116b06ebda0SMatthew Dillon 	if (m0 == NULL)
117b06ebda0SMatthew Dillon 		return (0);
118b06ebda0SMatthew Dillon 
119b06ebda0SMatthew Dillon 	ng_hci_mtap(unit, m0);
120b06ebda0SMatthew Dillon 
121b5523eacSSascha Wildner 	m = m_dup(m0, M_NOWAIT);
122b06ebda0SMatthew Dillon 	if (m != NULL)
123b06ebda0SMatthew Dillon 		NG_SEND_DATA_ONLY(error, unit->drv, m);
124b06ebda0SMatthew Dillon 	else
125b06ebda0SMatthew Dillon 		error = ENOBUFS;
126b06ebda0SMatthew Dillon 
127b06ebda0SMatthew Dillon 	if (error != 0)
128b06ebda0SMatthew Dillon 		NG_HCI_ERR(
129b06ebda0SMatthew Dillon "%s: %s - could not send HCI command, error=%d\n",
130b06ebda0SMatthew Dillon 			__func__, NG_NODE_NAME(unit->node), error);
131b06ebda0SMatthew Dillon 
132b06ebda0SMatthew Dillon 	/*
133b06ebda0SMatthew Dillon 	 * Even if we were not able to send command we still pretend
134b06ebda0SMatthew Dillon 	 * that everything is OK and let timeout handle that.
135b06ebda0SMatthew Dillon 	 */
136b06ebda0SMatthew Dillon 
137b06ebda0SMatthew Dillon 	NG_HCI_BUFF_CMD_USE(unit->buffer, 1);
138b06ebda0SMatthew Dillon 	NG_HCI_STAT_CMD_SENT(unit->stat);
139b06ebda0SMatthew Dillon 	NG_HCI_STAT_BYTES_SENT(unit->stat, m0->m_pkthdr.len);
140b06ebda0SMatthew Dillon 
141b06ebda0SMatthew Dillon 	/*
142b06ebda0SMatthew Dillon 	 * Note: ng_hci_command_timeout() will set
143b06ebda0SMatthew Dillon 	 * NG_HCI_UNIT_COMMAND_PENDING flag
144b06ebda0SMatthew Dillon 	 */
145b06ebda0SMatthew Dillon 
146b06ebda0SMatthew Dillon 	ng_hci_command_timeout(unit);
147b06ebda0SMatthew Dillon 
148b06ebda0SMatthew Dillon 	return (0);
149b06ebda0SMatthew Dillon } /* ng_hci_send_command */
150b06ebda0SMatthew Dillon 
151b06ebda0SMatthew Dillon /*
152b06ebda0SMatthew Dillon  * Process HCI Command_Compete event. Complete HCI command, and do post
153b06ebda0SMatthew Dillon  * processing on the command parameters (cp) and command return parameters
154b06ebda0SMatthew Dillon  * (e) if required (for example adjust state).
155b06ebda0SMatthew Dillon  */
156b06ebda0SMatthew Dillon 
157b06ebda0SMatthew Dillon int
ng_hci_process_command_complete(ng_hci_unit_p unit,struct mbuf * e)158b06ebda0SMatthew Dillon ng_hci_process_command_complete(ng_hci_unit_p unit, struct mbuf *e)
159b06ebda0SMatthew Dillon {
160b06ebda0SMatthew Dillon 	ng_hci_command_compl_ep		*ep = NULL;
161b06ebda0SMatthew Dillon 	struct mbuf			*cp = NULL;
162b06ebda0SMatthew Dillon 	int				 error = 0;
163b06ebda0SMatthew Dillon 
164b06ebda0SMatthew Dillon 	/* Get event packet and update command buffer info */
165b06ebda0SMatthew Dillon 	NG_HCI_M_PULLUP(e, sizeof(*ep));
166b06ebda0SMatthew Dillon 	if (e == NULL)
167b06ebda0SMatthew Dillon 		return (ENOBUFS); /* XXX this is bad */
168b06ebda0SMatthew Dillon 
169b06ebda0SMatthew Dillon 	ep = mtod(e, ng_hci_command_compl_ep *);
170b06ebda0SMatthew Dillon         NG_HCI_BUFF_CMD_SET(unit->buffer, ep->num_cmd_pkts);
171b06ebda0SMatthew Dillon 
172b06ebda0SMatthew Dillon 	/* Check for special NOOP command */
173b06ebda0SMatthew Dillon 	if (ep->opcode == 0x0000) {
174b06ebda0SMatthew Dillon 		NG_FREE_M(e);
175b06ebda0SMatthew Dillon 		goto out;
176b06ebda0SMatthew Dillon 	}
177b06ebda0SMatthew Dillon 
178b06ebda0SMatthew Dillon 	/* Try to match first command item in the queue */
179b06ebda0SMatthew Dillon 	error = complete_command(unit, ep->opcode, &cp);
180b06ebda0SMatthew Dillon 	if (error != 0) {
181b06ebda0SMatthew Dillon 		NG_FREE_M(e);
182b06ebda0SMatthew Dillon 		goto out;
183b06ebda0SMatthew Dillon 	}
184b06ebda0SMatthew Dillon 
185b06ebda0SMatthew Dillon 	/*
186b06ebda0SMatthew Dillon 	 * Perform post processing on command parameters and return parameters
187b06ebda0SMatthew Dillon 	 * do it only if status is OK (status == 0). Status is the first byte
188b06ebda0SMatthew Dillon 	 * of any command return parameters.
189b06ebda0SMatthew Dillon 	 */
190b06ebda0SMatthew Dillon 
191b06ebda0SMatthew Dillon 	ep->opcode = le16toh(ep->opcode);
192b06ebda0SMatthew Dillon 	m_adj(e, sizeof(*ep));
193b06ebda0SMatthew Dillon 
194b06ebda0SMatthew Dillon 	if (*mtod(e, u_int8_t *) == 0) { /* XXX m_pullup here? */
195b06ebda0SMatthew Dillon 		switch (NG_HCI_OGF(ep->opcode)) {
196b06ebda0SMatthew Dillon 		case NG_HCI_OGF_LINK_CONTROL:
197b06ebda0SMatthew Dillon 			error = process_link_control_params(unit,
198b06ebda0SMatthew Dillon 					NG_HCI_OCF(ep->opcode), cp, e);
199b06ebda0SMatthew Dillon 			break;
200b06ebda0SMatthew Dillon 
201b06ebda0SMatthew Dillon 		case NG_HCI_OGF_LINK_POLICY:
202b06ebda0SMatthew Dillon 			error = process_link_policy_params(unit,
203b06ebda0SMatthew Dillon 					NG_HCI_OCF(ep->opcode), cp, e);
204b06ebda0SMatthew Dillon 			break;
205b06ebda0SMatthew Dillon 
206b06ebda0SMatthew Dillon 		case NG_HCI_OGF_HC_BASEBAND:
207b06ebda0SMatthew Dillon 			error = process_hc_baseband_params(unit,
208b06ebda0SMatthew Dillon 					NG_HCI_OCF(ep->opcode), cp, e);
209b06ebda0SMatthew Dillon 			break;
210b06ebda0SMatthew Dillon 
211b06ebda0SMatthew Dillon 		case NG_HCI_OGF_INFO:
212b06ebda0SMatthew Dillon 			error = process_info_params(unit,
213b06ebda0SMatthew Dillon 					NG_HCI_OCF(ep->opcode), cp, e);
214b06ebda0SMatthew Dillon 			break;
215b06ebda0SMatthew Dillon 
216b06ebda0SMatthew Dillon 		case NG_HCI_OGF_STATUS:
217b06ebda0SMatthew Dillon 			error = process_status_params(unit,
218b06ebda0SMatthew Dillon 					NG_HCI_OCF(ep->opcode), cp, e);
219b06ebda0SMatthew Dillon 			break;
220b06ebda0SMatthew Dillon 
221b06ebda0SMatthew Dillon 		case NG_HCI_OGF_TESTING:
222b06ebda0SMatthew Dillon 			error = process_testing_params(unit,
223b06ebda0SMatthew Dillon 					NG_HCI_OCF(ep->opcode), cp, e);
224b06ebda0SMatthew Dillon 			break;
225b06ebda0SMatthew Dillon 
226b06ebda0SMatthew Dillon 		case NG_HCI_OGF_BT_LOGO:
227b06ebda0SMatthew Dillon 		case NG_HCI_OGF_VENDOR:
228b06ebda0SMatthew Dillon 			NG_FREE_M(cp);
229b06ebda0SMatthew Dillon 			NG_FREE_M(e);
230b06ebda0SMatthew Dillon 			break;
231b06ebda0SMatthew Dillon 
232b06ebda0SMatthew Dillon 		default:
233b06ebda0SMatthew Dillon 			NG_FREE_M(cp);
234b06ebda0SMatthew Dillon 			NG_FREE_M(e);
235b06ebda0SMatthew Dillon 			error = EINVAL;
236b06ebda0SMatthew Dillon 			break;
237b06ebda0SMatthew Dillon 		}
238b06ebda0SMatthew Dillon 	} else {
239b06ebda0SMatthew Dillon 		NG_HCI_ERR(
240b06ebda0SMatthew Dillon "%s: %s - HCI command failed, OGF=%#x, OCF=%#x, status=%#x\n",
241b06ebda0SMatthew Dillon 			__func__, NG_NODE_NAME(unit->node),
242b06ebda0SMatthew Dillon 			NG_HCI_OGF(ep->opcode), NG_HCI_OCF(ep->opcode),
243b06ebda0SMatthew Dillon 			*mtod(e, u_int8_t *));
244b06ebda0SMatthew Dillon 
245b06ebda0SMatthew Dillon 		NG_FREE_M(cp);
246b06ebda0SMatthew Dillon 		NG_FREE_M(e);
247b06ebda0SMatthew Dillon 	}
248b06ebda0SMatthew Dillon out:
249b06ebda0SMatthew Dillon 	ng_hci_send_command(unit);
250b06ebda0SMatthew Dillon 
251b06ebda0SMatthew Dillon 	return (error);
252b06ebda0SMatthew Dillon } /* ng_hci_process_command_complete */
253b06ebda0SMatthew Dillon 
254b06ebda0SMatthew Dillon /*
255b06ebda0SMatthew Dillon  * Process HCI Command_Status event. Check the status (mst) and do post
256b06ebda0SMatthew Dillon  * processing (if required).
257b06ebda0SMatthew Dillon  */
258b06ebda0SMatthew Dillon 
259b06ebda0SMatthew Dillon int
ng_hci_process_command_status(ng_hci_unit_p unit,struct mbuf * e)260b06ebda0SMatthew Dillon ng_hci_process_command_status(ng_hci_unit_p unit, struct mbuf *e)
261b06ebda0SMatthew Dillon {
262b06ebda0SMatthew Dillon 	ng_hci_command_status_ep	*ep = NULL;
263b06ebda0SMatthew Dillon 	struct mbuf			*cp = NULL;
264b06ebda0SMatthew Dillon 	int				 error = 0;
265b06ebda0SMatthew Dillon 
266b06ebda0SMatthew Dillon 	/* Update command buffer info */
267b06ebda0SMatthew Dillon 	NG_HCI_M_PULLUP(e, sizeof(*ep));
268b06ebda0SMatthew Dillon 	if (e == NULL)
269b06ebda0SMatthew Dillon 		return (ENOBUFS); /* XXX this is bad */
270b06ebda0SMatthew Dillon 
271b06ebda0SMatthew Dillon 	ep = mtod(e, ng_hci_command_status_ep *);
272b06ebda0SMatthew Dillon 	NG_HCI_BUFF_CMD_SET(unit->buffer, ep->num_cmd_pkts);
273b06ebda0SMatthew Dillon 
274b06ebda0SMatthew Dillon 	/* Check for special NOOP command */
275b06ebda0SMatthew Dillon 	if (ep->opcode == 0x0000)
276b06ebda0SMatthew Dillon 		goto out;
277b06ebda0SMatthew Dillon 
278b06ebda0SMatthew Dillon 	/* Try to match first command item in the queue */
279b06ebda0SMatthew Dillon 	error = complete_command(unit, ep->opcode, &cp);
280b06ebda0SMatthew Dillon         if (error != 0)
281b06ebda0SMatthew Dillon 		goto out;
282b06ebda0SMatthew Dillon 
283b06ebda0SMatthew Dillon 	/*
284b06ebda0SMatthew Dillon 	 * Perform post processing on HCI Command_Status event
285b06ebda0SMatthew Dillon 	 */
286b06ebda0SMatthew Dillon 
287b06ebda0SMatthew Dillon 	ep->opcode = le16toh(ep->opcode);
288b06ebda0SMatthew Dillon 
289b06ebda0SMatthew Dillon 	switch (NG_HCI_OGF(ep->opcode)) {
290b06ebda0SMatthew Dillon 	case NG_HCI_OGF_LINK_CONTROL:
291b06ebda0SMatthew Dillon 		error = process_link_control_status(unit, ep, cp);
292b06ebda0SMatthew Dillon 		break;
293b06ebda0SMatthew Dillon 
294b06ebda0SMatthew Dillon 	case NG_HCI_OGF_LINK_POLICY:
295b06ebda0SMatthew Dillon 		error = process_link_policy_status(unit, ep, cp);
296b06ebda0SMatthew Dillon 		break;
297b06ebda0SMatthew Dillon 
298b06ebda0SMatthew Dillon 	case NG_HCI_OGF_BT_LOGO:
299b06ebda0SMatthew Dillon 	case NG_HCI_OGF_VENDOR:
300b06ebda0SMatthew Dillon 		NG_FREE_M(cp);
301b06ebda0SMatthew Dillon 		break;
302b06ebda0SMatthew Dillon 
303b06ebda0SMatthew Dillon 	case NG_HCI_OGF_HC_BASEBAND:
304b06ebda0SMatthew Dillon 	case NG_HCI_OGF_INFO:
305b06ebda0SMatthew Dillon 	case NG_HCI_OGF_STATUS:
306b06ebda0SMatthew Dillon 	case NG_HCI_OGF_TESTING:
307b06ebda0SMatthew Dillon 	default:
308b06ebda0SMatthew Dillon 		NG_FREE_M(cp);
309b06ebda0SMatthew Dillon 		error = EINVAL;
310b06ebda0SMatthew Dillon 		break;
311b06ebda0SMatthew Dillon 	}
312b06ebda0SMatthew Dillon out:
313b06ebda0SMatthew Dillon 	NG_FREE_M(e);
314b06ebda0SMatthew Dillon 	ng_hci_send_command(unit);
315b06ebda0SMatthew Dillon 
316b06ebda0SMatthew Dillon 	return (error);
317b06ebda0SMatthew Dillon } /* ng_hci_process_command_status */
318b06ebda0SMatthew Dillon 
319b06ebda0SMatthew Dillon /*
320b06ebda0SMatthew Dillon  * Complete queued HCI command.
321b06ebda0SMatthew Dillon  */
322b06ebda0SMatthew Dillon 
323b06ebda0SMatthew Dillon static int
complete_command(ng_hci_unit_p unit,int opcode,struct mbuf ** cp)324b06ebda0SMatthew Dillon complete_command(ng_hci_unit_p unit, int opcode, struct mbuf **cp)
325b06ebda0SMatthew Dillon {
326b06ebda0SMatthew Dillon 	struct mbuf	*m = NULL;
327b06ebda0SMatthew Dillon 
328b06ebda0SMatthew Dillon 	/* Check unit state */
329b06ebda0SMatthew Dillon 	if (!(unit->state & NG_HCI_UNIT_COMMAND_PENDING)) {
330b06ebda0SMatthew Dillon 		NG_HCI_ALERT(
331b06ebda0SMatthew Dillon "%s: %s - no pending command, state=%#x\n",
332b06ebda0SMatthew Dillon 			__func__, NG_NODE_NAME(unit->node), unit->state);
333b06ebda0SMatthew Dillon 
334b06ebda0SMatthew Dillon 		return (EINVAL);
335b06ebda0SMatthew Dillon 	}
336b06ebda0SMatthew Dillon 
337b06ebda0SMatthew Dillon 	/* Get first command in the queue */
338b06ebda0SMatthew Dillon 	m = NG_BT_MBUFQ_FIRST(&unit->cmdq);
339b06ebda0SMatthew Dillon 	if (m == NULL) {
340b06ebda0SMatthew Dillon 		NG_HCI_ALERT(
341b06ebda0SMatthew Dillon "%s: %s - empty command queue?!\n", __func__, NG_NODE_NAME(unit->node));
342b06ebda0SMatthew Dillon 
343b06ebda0SMatthew Dillon 		return (EINVAL);
344b06ebda0SMatthew Dillon 	}
345b06ebda0SMatthew Dillon 
346b06ebda0SMatthew Dillon 	/*
347b06ebda0SMatthew Dillon 	 * Match command opcode, if does not match - do nothing and
348b06ebda0SMatthew Dillon 	 * let timeout handle that.
349b06ebda0SMatthew Dillon 	 */
350b06ebda0SMatthew Dillon 
351b06ebda0SMatthew Dillon 	if (mtod(m, ng_hci_cmd_pkt_t *)->opcode != opcode) {
352b06ebda0SMatthew Dillon 		NG_HCI_ALERT(
353b06ebda0SMatthew Dillon "%s: %s - command queue is out of sync\n", __func__, NG_NODE_NAME(unit->node));
354b06ebda0SMatthew Dillon 
355b06ebda0SMatthew Dillon 		return (EINVAL);
356b06ebda0SMatthew Dillon 	}
357b06ebda0SMatthew Dillon 
358b06ebda0SMatthew Dillon 	/*
359b06ebda0SMatthew Dillon 	 * Now we can remove command timeout, dequeue completed command
360b06ebda0SMatthew Dillon 	 * and return command parameters. ng_hci_command_untimeout will
361b06ebda0SMatthew Dillon 	 * drop NG_HCI_UNIT_COMMAND_PENDING flag.
362b06ebda0SMatthew Dillon 	 * Note: if ng_hci_command_untimeout() fails (returns non-zero)
363b06ebda0SMatthew Dillon 	 * then timeout aready happened and timeout message went info node
364b06ebda0SMatthew Dillon 	 * queue. In this case we ignore command completion and pretend
365b06ebda0SMatthew Dillon 	 * there is a timeout.
366b06ebda0SMatthew Dillon 	 */
367b06ebda0SMatthew Dillon 
368b06ebda0SMatthew Dillon 	if (ng_hci_command_untimeout(unit) != 0)
369b06ebda0SMatthew Dillon 		return (ETIMEDOUT);
370b06ebda0SMatthew Dillon 
371b06ebda0SMatthew Dillon 	NG_BT_MBUFQ_DEQUEUE(&unit->cmdq, *cp);
372b06ebda0SMatthew Dillon 	m_adj(*cp, sizeof(ng_hci_cmd_pkt_t));
373b06ebda0SMatthew Dillon 
374b06ebda0SMatthew Dillon 	return (0);
375b06ebda0SMatthew Dillon } /* complete_command */
376b06ebda0SMatthew Dillon 
377b06ebda0SMatthew Dillon /*
378b06ebda0SMatthew Dillon  * Process HCI command timeout
379b06ebda0SMatthew Dillon  */
380b06ebda0SMatthew Dillon 
381b06ebda0SMatthew Dillon void
ng_hci_process_command_timeout(node_p node,hook_p hook,void * arg1,int arg2)382b06ebda0SMatthew Dillon ng_hci_process_command_timeout(node_p node, hook_p hook, void *arg1, int arg2)
383b06ebda0SMatthew Dillon {
384b06ebda0SMatthew Dillon 	ng_hci_unit_p	 unit = NULL;
385b06ebda0SMatthew Dillon 	struct mbuf	*m = NULL;
386b06ebda0SMatthew Dillon 	u_int16_t	 opcode;
387b06ebda0SMatthew Dillon 
388b06ebda0SMatthew Dillon 	if (NG_NODE_NOT_VALID(node)) {
389a62226e4SSascha Wildner 		kprintf("%s: Netgraph node is not valid\n", __func__);
390b06ebda0SMatthew Dillon 		return;
391b06ebda0SMatthew Dillon 	}
392b06ebda0SMatthew Dillon 
393b06ebda0SMatthew Dillon 	unit = (ng_hci_unit_p) NG_NODE_PRIVATE(node);
394b06ebda0SMatthew Dillon 
395b06ebda0SMatthew Dillon 	if (unit->state & NG_HCI_UNIT_COMMAND_PENDING) {
396b06ebda0SMatthew Dillon 		unit->state &= ~NG_HCI_UNIT_COMMAND_PENDING;
397b06ebda0SMatthew Dillon 
398b06ebda0SMatthew Dillon 		NG_BT_MBUFQ_DEQUEUE(&unit->cmdq, m);
399b06ebda0SMatthew Dillon 		if (m == NULL) {
400b06ebda0SMatthew Dillon 			NG_HCI_ALERT(
401b06ebda0SMatthew Dillon "%s: %s - command queue is out of sync!\n", __func__, NG_NODE_NAME(unit->node));
402b06ebda0SMatthew Dillon 
403b06ebda0SMatthew Dillon 			return;
404b06ebda0SMatthew Dillon 		}
405b06ebda0SMatthew Dillon 
406b06ebda0SMatthew Dillon 		opcode = le16toh(mtod(m, ng_hci_cmd_pkt_t *)->opcode);
407b06ebda0SMatthew Dillon 		NG_FREE_M(m);
408b06ebda0SMatthew Dillon 
409b06ebda0SMatthew Dillon 		NG_HCI_ERR(
410b06ebda0SMatthew Dillon "%s: %s - unable to complete HCI command OGF=%#x, OCF=%#x. Timeout\n",
411b06ebda0SMatthew Dillon 			__func__, NG_NODE_NAME(unit->node), NG_HCI_OGF(opcode),
412b06ebda0SMatthew Dillon 			NG_HCI_OCF(opcode));
413b06ebda0SMatthew Dillon 
414b06ebda0SMatthew Dillon 		/* Try to send more commands */
415b06ebda0SMatthew Dillon  		NG_HCI_BUFF_CMD_SET(unit->buffer, 1);
416b06ebda0SMatthew Dillon 		ng_hci_send_command(unit);
417b06ebda0SMatthew Dillon 	} else
418b06ebda0SMatthew Dillon 		NG_HCI_ALERT(
419b06ebda0SMatthew Dillon "%s: %s - no pending command\n", __func__, NG_NODE_NAME(unit->node));
420b06ebda0SMatthew Dillon } /* ng_hci_process_command_timeout */
421b06ebda0SMatthew Dillon 
422b06ebda0SMatthew Dillon /*
423b06ebda0SMatthew Dillon  * Process link command return parameters
424b06ebda0SMatthew Dillon  */
425b06ebda0SMatthew Dillon 
426b06ebda0SMatthew Dillon static int
process_link_control_params(ng_hci_unit_p unit,u_int16_t ocf,struct mbuf * mcp,struct mbuf * mrp)427b06ebda0SMatthew Dillon process_link_control_params(ng_hci_unit_p unit, u_int16_t ocf,
428b06ebda0SMatthew Dillon 		struct mbuf *mcp, struct mbuf *mrp)
429b06ebda0SMatthew Dillon {
430b06ebda0SMatthew Dillon 	int	error  = 0;
431b06ebda0SMatthew Dillon 
432b06ebda0SMatthew Dillon 	switch (ocf) {
433b06ebda0SMatthew Dillon 	case NG_HCI_OCF_INQUIRY_CANCEL:
434b06ebda0SMatthew Dillon 	case NG_HCI_OCF_PERIODIC_INQUIRY:
435b06ebda0SMatthew Dillon 	case NG_HCI_OCF_EXIT_PERIODIC_INQUIRY:
436b06ebda0SMatthew Dillon 	case NG_HCI_OCF_LINK_KEY_REP:
437b06ebda0SMatthew Dillon 	case NG_HCI_OCF_LINK_KEY_NEG_REP:
438b06ebda0SMatthew Dillon 	case NG_HCI_OCF_PIN_CODE_REP:
439b06ebda0SMatthew Dillon 	case NG_HCI_OCF_PIN_CODE_NEG_REP:
440b06ebda0SMatthew Dillon 		/* These do not need post processing */
441b06ebda0SMatthew Dillon 		break;
442b06ebda0SMatthew Dillon 
443b06ebda0SMatthew Dillon 	case NG_HCI_OCF_INQUIRY:
444b06ebda0SMatthew Dillon 	case NG_HCI_OCF_CREATE_CON:
445b06ebda0SMatthew Dillon 	case NG_HCI_OCF_DISCON:
446b06ebda0SMatthew Dillon 	case NG_HCI_OCF_ADD_SCO_CON:
447b06ebda0SMatthew Dillon 	case NG_HCI_OCF_ACCEPT_CON:
448b06ebda0SMatthew Dillon 	case NG_HCI_OCF_REJECT_CON:
449b06ebda0SMatthew Dillon 	case NG_HCI_OCF_CHANGE_CON_PKT_TYPE:
450b06ebda0SMatthew Dillon 	case NG_HCI_OCF_AUTH_REQ:
451b06ebda0SMatthew Dillon 	case NG_HCI_OCF_SET_CON_ENCRYPTION:
452b06ebda0SMatthew Dillon 	case NG_HCI_OCF_CHANGE_CON_LINK_KEY:
453b06ebda0SMatthew Dillon 	case NG_HCI_OCF_MASTER_LINK_KEY:
454b06ebda0SMatthew Dillon 	case NG_HCI_OCF_REMOTE_NAME_REQ:
455b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_REMOTE_FEATURES:
456b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_REMOTE_VER_INFO:
457b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_CLOCK_OFFSET:
458b06ebda0SMatthew Dillon 	default:
459b06ebda0SMatthew Dillon 
460b06ebda0SMatthew Dillon 		/*
461b06ebda0SMatthew Dillon 		 * None of these command was supposed to generate
462b06ebda0SMatthew Dillon 		 * Command_Complete event. Instead Command_Status event
463b06ebda0SMatthew Dillon 		 * should have been generated and then appropriate event
464b06ebda0SMatthew Dillon 		 * should have been sent to indicate the final result.
465b06ebda0SMatthew Dillon 		 */
466b06ebda0SMatthew Dillon 
467b06ebda0SMatthew Dillon 		error = EINVAL;
468b06ebda0SMatthew Dillon 		break;
469b06ebda0SMatthew Dillon 	}
470b06ebda0SMatthew Dillon 
471b06ebda0SMatthew Dillon 	NG_FREE_M(mcp);
472b06ebda0SMatthew Dillon 	NG_FREE_M(mrp);
473b06ebda0SMatthew Dillon 
474b06ebda0SMatthew Dillon 	return (error);
475b06ebda0SMatthew Dillon } /* process_link_control_params */
476b06ebda0SMatthew Dillon 
477b06ebda0SMatthew Dillon /*
478b06ebda0SMatthew Dillon  * Process link policy command return parameters
479b06ebda0SMatthew Dillon  */
480b06ebda0SMatthew Dillon 
481b06ebda0SMatthew Dillon static int
process_link_policy_params(ng_hci_unit_p unit,u_int16_t ocf,struct mbuf * mcp,struct mbuf * mrp)482b06ebda0SMatthew Dillon process_link_policy_params(ng_hci_unit_p unit, u_int16_t ocf,
483b06ebda0SMatthew Dillon 		struct mbuf *mcp, struct mbuf *mrp)
484b06ebda0SMatthew Dillon {
485b06ebda0SMatthew Dillon 	int	error = 0;
486b06ebda0SMatthew Dillon 
487b06ebda0SMatthew Dillon 	switch (ocf){
488b06ebda0SMatthew Dillon 	case NG_HCI_OCF_ROLE_DISCOVERY: {
489b06ebda0SMatthew Dillon 		ng_hci_role_discovery_rp	*rp = NULL;
490b06ebda0SMatthew Dillon 		ng_hci_unit_con_t		*con = NULL;
491b06ebda0SMatthew Dillon 		u_int16_t			 h;
492b06ebda0SMatthew Dillon 
493b06ebda0SMatthew Dillon 		NG_HCI_M_PULLUP(mrp, sizeof(*rp));
494b06ebda0SMatthew Dillon 		if (mrp != NULL) {
495b06ebda0SMatthew Dillon 			rp = mtod(mrp, ng_hci_role_discovery_rp *);
496b06ebda0SMatthew Dillon 
497b06ebda0SMatthew Dillon 			h = NG_HCI_CON_HANDLE(le16toh(rp->con_handle));
498b06ebda0SMatthew Dillon 			con = ng_hci_con_by_handle(unit, h);
499b06ebda0SMatthew Dillon 			if (con == NULL) {
500b06ebda0SMatthew Dillon 				NG_HCI_ALERT(
501b06ebda0SMatthew Dillon "%s: %s - invalid connection handle=%d\n",
502b06ebda0SMatthew Dillon 					__func__, NG_NODE_NAME(unit->node), h);
503b06ebda0SMatthew Dillon 				error = ENOENT;
504b06ebda0SMatthew Dillon 			} else if (con->link_type != NG_HCI_LINK_ACL) {
505b06ebda0SMatthew Dillon 				NG_HCI_ALERT(
506b06ebda0SMatthew Dillon "%s: %s - invalid link type=%d\n", __func__, NG_NODE_NAME(unit->node),
507b06ebda0SMatthew Dillon 					con->link_type);
508b06ebda0SMatthew Dillon 				error = EINVAL;
509b06ebda0SMatthew Dillon 			} else
510b06ebda0SMatthew Dillon 				con->role = rp->role;
511b06ebda0SMatthew Dillon 		} else
512b06ebda0SMatthew Dillon 			error = ENOBUFS;
513b06ebda0SMatthew Dillon 		} break;
514b06ebda0SMatthew Dillon 
515b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_LINK_POLICY_SETTINGS:
516b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_LINK_POLICY_SETTINGS:
517b06ebda0SMatthew Dillon 		/* These do not need post processing */
518b06ebda0SMatthew Dillon 		break;
519b06ebda0SMatthew Dillon 
520b06ebda0SMatthew Dillon 	case NG_HCI_OCF_HOLD_MODE:
521b06ebda0SMatthew Dillon 	case NG_HCI_OCF_SNIFF_MODE:
522b06ebda0SMatthew Dillon 	case NG_HCI_OCF_EXIT_SNIFF_MODE:
523b06ebda0SMatthew Dillon 	case NG_HCI_OCF_PARK_MODE:
524b06ebda0SMatthew Dillon 	case NG_HCI_OCF_EXIT_PARK_MODE:
525b06ebda0SMatthew Dillon 	case NG_HCI_OCF_QOS_SETUP:
526b06ebda0SMatthew Dillon 	case NG_HCI_OCF_SWITCH_ROLE:
527b06ebda0SMatthew Dillon 	default:
528b06ebda0SMatthew Dillon 
529b06ebda0SMatthew Dillon 		/*
530b06ebda0SMatthew Dillon 		 * None of these command was supposed to generate
531b06ebda0SMatthew Dillon 		 * Command_Complete event. Instead Command_Status event
532b06ebda0SMatthew Dillon 		 * should have been generated and then appropriate event
533b06ebda0SMatthew Dillon 		 * should have been sent to indicate the final result.
534b06ebda0SMatthew Dillon 		 */
535b06ebda0SMatthew Dillon 
536b06ebda0SMatthew Dillon 		error = EINVAL;
537b06ebda0SMatthew Dillon 		break;
538b06ebda0SMatthew Dillon 	}
539b06ebda0SMatthew Dillon 
540b06ebda0SMatthew Dillon 	NG_FREE_M(mcp);
541b06ebda0SMatthew Dillon 	NG_FREE_M(mrp);
542b06ebda0SMatthew Dillon 
543b06ebda0SMatthew Dillon 	return (error);
544b06ebda0SMatthew Dillon } /* process_link_policy_params */
545b06ebda0SMatthew Dillon 
546b06ebda0SMatthew Dillon /*
547b06ebda0SMatthew Dillon  * Process HC and baseband command return parameters
548b06ebda0SMatthew Dillon  */
549b06ebda0SMatthew Dillon 
550b06ebda0SMatthew Dillon int
process_hc_baseband_params(ng_hci_unit_p unit,u_int16_t ocf,struct mbuf * mcp,struct mbuf * mrp)551b06ebda0SMatthew Dillon process_hc_baseband_params(ng_hci_unit_p unit, u_int16_t ocf,
552b06ebda0SMatthew Dillon 		struct mbuf *mcp, struct mbuf *mrp)
553b06ebda0SMatthew Dillon {
554b06ebda0SMatthew Dillon 	int	error = 0;
555b06ebda0SMatthew Dillon 
556b06ebda0SMatthew Dillon 	switch (ocf) {
557b06ebda0SMatthew Dillon 	case NG_HCI_OCF_SET_EVENT_MASK:
558b06ebda0SMatthew Dillon 	case NG_HCI_OCF_SET_EVENT_FILTER:
559b06ebda0SMatthew Dillon 	case NG_HCI_OCF_FLUSH:	/* XXX Do we need to handle that? */
560b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_PIN_TYPE:
561b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_PIN_TYPE:
562b06ebda0SMatthew Dillon 	case NG_HCI_OCF_CREATE_NEW_UNIT_KEY:
563b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_STORED_LINK_KEY:
564b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_CON_ACCEPT_TIMO:
565b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_PAGE_TIMO:
566b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_SCAN_ENABLE:
567b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_SCAN_ENABLE:
568b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_PAGE_SCAN_ACTIVITY:
569b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_INQUIRY_SCAN_ACTIVITY:
570b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_AUTH_ENABLE:
571b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_AUTH_ENABLE:
572b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_ENCRYPTION_MODE:
573b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_ENCRYPTION_MODE:
574b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_VOICE_SETTINGS:
575b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS:
576b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_NUM_BROADCAST_RETRANS:
577b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY:
578b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_HOLD_MODE_ACTIVITY:
579b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_SCO_FLOW_CONTROL:
580b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_SCO_FLOW_CONTROL:
581b06ebda0SMatthew Dillon 	case NG_HCI_OCF_H2HC_FLOW_CONTROL: /* XXX Not supported this time */
582b06ebda0SMatthew Dillon 	case NG_HCI_OCF_HOST_BUFFER_SIZE:
583b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_IAC_LAP:
584b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_IAC_LAP:
585b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_PAGE_SCAN_PERIOD:
586b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_PAGE_SCAN_PERIOD:
587b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_PAGE_SCAN:
588b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_PAGE_SCAN:
589b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO:
590b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_LINK_SUPERVISION_TIMO:
591b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_SUPPORTED_IAC_NUM:
592b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_STORED_LINK_KEY:
593b06ebda0SMatthew Dillon 	case NG_HCI_OCF_DELETE_STORED_LINK_KEY:
594b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_CON_ACCEPT_TIMO:
595b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_PAGE_TIMO:
596b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY:
597b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY:
598b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_VOICE_SETTINGS:
599b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_AUTO_FLUSH_TIMO:
600b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_AUTO_FLUSH_TIMO:
601b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_XMIT_LEVEL:
602b06ebda0SMatthew Dillon 	case NG_HCI_OCF_HOST_NUM_COMPL_PKTS:	/* XXX Can get here? */
603b06ebda0SMatthew Dillon 	case NG_HCI_OCF_CHANGE_LOCAL_NAME:
604b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_LOCAL_NAME:
605b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_UNIT_CLASS:
606b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_UNIT_CLASS:
607b06ebda0SMatthew Dillon 		/* These do not need post processing */
608b06ebda0SMatthew Dillon 		break;
609b06ebda0SMatthew Dillon 
610b06ebda0SMatthew Dillon 	case NG_HCI_OCF_RESET: {
611b06ebda0SMatthew Dillon 		ng_hci_unit_con_p	con = NULL;
612b06ebda0SMatthew Dillon 		int			size;
613b06ebda0SMatthew Dillon 
614b06ebda0SMatthew Dillon 		/*
615b06ebda0SMatthew Dillon 		 * XXX
616b06ebda0SMatthew Dillon 		 *
617b06ebda0SMatthew Dillon 		 * After RESET command unit goes into standby mode
618b06ebda0SMatthew Dillon 		 * and all operational state is lost. Host controller
619b06ebda0SMatthew Dillon 		 * will revert to default values for all parameters.
620b06ebda0SMatthew Dillon 		 *
621b06ebda0SMatthew Dillon 		 * For now we shall terminate all connections and drop
622b06ebda0SMatthew Dillon 		 * inited bit. After RESET unit must be re-initialized.
623b06ebda0SMatthew Dillon 		 */
624b06ebda0SMatthew Dillon 
625b06ebda0SMatthew Dillon 		while (!LIST_EMPTY(&unit->con_list)) {
626b06ebda0SMatthew Dillon 			con = LIST_FIRST(&unit->con_list);
627b06ebda0SMatthew Dillon 
628b06ebda0SMatthew Dillon 			/* Remove all timeouts (if any) */
629b06ebda0SMatthew Dillon 			if (con->flags & NG_HCI_CON_TIMEOUT_PENDING)
630b06ebda0SMatthew Dillon 				ng_hci_con_untimeout(con);
631b06ebda0SMatthew Dillon 
632b06ebda0SMatthew Dillon 			/* Connection terminated by local host */
633b06ebda0SMatthew Dillon 			ng_hci_lp_discon_ind(con, 0x16);
634b06ebda0SMatthew Dillon 			ng_hci_free_con(con);
635b06ebda0SMatthew Dillon 		}
636b06ebda0SMatthew Dillon 
637b06ebda0SMatthew Dillon 		NG_HCI_BUFF_ACL_TOTAL(unit->buffer, size);
638b06ebda0SMatthew Dillon 		NG_HCI_BUFF_ACL_FREE(unit->buffer, size);
639b06ebda0SMatthew Dillon 
640b06ebda0SMatthew Dillon 		NG_HCI_BUFF_SCO_TOTAL(unit->buffer, size);
641b06ebda0SMatthew Dillon 		NG_HCI_BUFF_SCO_FREE(unit->buffer, size);
642b06ebda0SMatthew Dillon 
643b06ebda0SMatthew Dillon 		unit->state &= ~NG_HCI_UNIT_INITED;
644b06ebda0SMatthew Dillon 		} break;
645b06ebda0SMatthew Dillon 
646b06ebda0SMatthew Dillon 	default:
647b06ebda0SMatthew Dillon 		error = EINVAL;
648b06ebda0SMatthew Dillon 		break;
649b06ebda0SMatthew Dillon 	}
650b06ebda0SMatthew Dillon 
651b06ebda0SMatthew Dillon 	NG_FREE_M(mcp);
652b06ebda0SMatthew Dillon 	NG_FREE_M(mrp);
653b06ebda0SMatthew Dillon 
654b06ebda0SMatthew Dillon 	return (error);
655b06ebda0SMatthew Dillon } /* process_hc_baseband_params */
656b06ebda0SMatthew Dillon 
657b06ebda0SMatthew Dillon /*
658b06ebda0SMatthew Dillon  * Process info command return parameters
659b06ebda0SMatthew Dillon  */
660b06ebda0SMatthew Dillon 
661b06ebda0SMatthew Dillon static int
process_info_params(ng_hci_unit_p unit,u_int16_t ocf,struct mbuf * mcp,struct mbuf * mrp)662b06ebda0SMatthew Dillon process_info_params(ng_hci_unit_p unit, u_int16_t ocf, struct mbuf *mcp,
663b06ebda0SMatthew Dillon 		struct mbuf *mrp)
664b06ebda0SMatthew Dillon {
665b06ebda0SMatthew Dillon 	int	error = 0, len;
666b06ebda0SMatthew Dillon 
667b06ebda0SMatthew Dillon 	switch (ocf) {
668b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_LOCAL_VER:
669b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_COUNTRY_CODE:
670b06ebda0SMatthew Dillon 		break;
671b06ebda0SMatthew Dillon 
672b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_LOCAL_FEATURES:
673b06ebda0SMatthew Dillon 		m_adj(mrp, sizeof(u_int8_t));
674b06ebda0SMatthew Dillon 		len = min(mrp->m_pkthdr.len, sizeof(unit->features));
675*05d02a38SAaron LI 		m_copydata(mrp, 0, len, unit->features);
676b06ebda0SMatthew Dillon 		break;
677b06ebda0SMatthew Dillon 
678b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_BUFFER_SIZE: {
679b06ebda0SMatthew Dillon 		ng_hci_read_buffer_size_rp	*rp = NULL;
680b06ebda0SMatthew Dillon 
681b06ebda0SMatthew Dillon 		/* Do not update buffer descriptor if node was initialized */
682b06ebda0SMatthew Dillon 		if ((unit->state & NG_HCI_UNIT_READY) == NG_HCI_UNIT_READY)
683b06ebda0SMatthew Dillon 			break;
684b06ebda0SMatthew Dillon 
685b06ebda0SMatthew Dillon 		NG_HCI_M_PULLUP(mrp, sizeof(*rp));
686b06ebda0SMatthew Dillon 		if (mrp != NULL) {
687b06ebda0SMatthew Dillon 			rp = mtod(mrp, ng_hci_read_buffer_size_rp *);
688b06ebda0SMatthew Dillon 
689b06ebda0SMatthew Dillon 			NG_HCI_BUFF_ACL_SET(
690b06ebda0SMatthew Dillon 				unit->buffer,
691b06ebda0SMatthew Dillon 				le16toh(rp->num_acl_pkt),  /* number */
692b06ebda0SMatthew Dillon 				le16toh(rp->max_acl_size), /* size */
693b06ebda0SMatthew Dillon 				le16toh(rp->num_acl_pkt)   /* free */
694b06ebda0SMatthew Dillon 			);
695b06ebda0SMatthew Dillon 
696b06ebda0SMatthew Dillon 			NG_HCI_BUFF_SCO_SET(
697b06ebda0SMatthew Dillon 				unit->buffer,
698b06ebda0SMatthew Dillon 				le16toh(rp->num_sco_pkt), /* number */
699b06ebda0SMatthew Dillon 				rp->max_sco_size,         /* size */
700b06ebda0SMatthew Dillon 				le16toh(rp->num_sco_pkt)  /* free */
701b06ebda0SMatthew Dillon 			);
702b06ebda0SMatthew Dillon 
703b06ebda0SMatthew Dillon 			/* Let upper layers know */
704b06ebda0SMatthew Dillon 			ng_hci_node_is_up(unit->node, unit->acl, NULL, 0);
705b06ebda0SMatthew Dillon 			ng_hci_node_is_up(unit->node, unit->sco, NULL, 0);
706b06ebda0SMatthew Dillon 		} else
707b06ebda0SMatthew Dillon 			error = ENOBUFS;
708b06ebda0SMatthew Dillon 		} break;
709b06ebda0SMatthew Dillon 
710b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_BDADDR:
711b06ebda0SMatthew Dillon 		/* Do not update BD_ADDR if node was initialized */
712b06ebda0SMatthew Dillon 		if ((unit->state & NG_HCI_UNIT_READY) == NG_HCI_UNIT_READY)
713b06ebda0SMatthew Dillon 			break;
714b06ebda0SMatthew Dillon 
715b06ebda0SMatthew Dillon 		m_adj(mrp, sizeof(u_int8_t));
716b06ebda0SMatthew Dillon 		len = min(mrp->m_pkthdr.len, sizeof(unit->bdaddr));
717*05d02a38SAaron LI 		m_copydata(mrp, 0, len, &unit->bdaddr);
718b06ebda0SMatthew Dillon 
719b06ebda0SMatthew Dillon 		/* Let upper layers know */
720b06ebda0SMatthew Dillon 		ng_hci_node_is_up(unit->node, unit->acl, NULL, 0);
721b06ebda0SMatthew Dillon 		ng_hci_node_is_up(unit->node, unit->sco, NULL, 0);
722b06ebda0SMatthew Dillon 		break;
723b06ebda0SMatthew Dillon 
724b06ebda0SMatthew Dillon 	default:
725b06ebda0SMatthew Dillon 		error = EINVAL;
726b06ebda0SMatthew Dillon 		break;
727b06ebda0SMatthew Dillon 	}
728b06ebda0SMatthew Dillon 
729b06ebda0SMatthew Dillon 	NG_FREE_M(mcp);
730b06ebda0SMatthew Dillon 	NG_FREE_M(mrp);
731b06ebda0SMatthew Dillon 
732b06ebda0SMatthew Dillon 	return (error);
733b06ebda0SMatthew Dillon } /* process_info_params */
734b06ebda0SMatthew Dillon 
735b06ebda0SMatthew Dillon /*
736b06ebda0SMatthew Dillon  * Process status command return parameters
737b06ebda0SMatthew Dillon  */
738b06ebda0SMatthew Dillon 
739b06ebda0SMatthew Dillon static int
process_status_params(ng_hci_unit_p unit,u_int16_t ocf,struct mbuf * mcp,struct mbuf * mrp)740b06ebda0SMatthew Dillon process_status_params(ng_hci_unit_p unit, u_int16_t ocf, struct mbuf *mcp,
741b06ebda0SMatthew Dillon 		struct mbuf *mrp)
742b06ebda0SMatthew Dillon {
743b06ebda0SMatthew Dillon 	int	error = 0;
744b06ebda0SMatthew Dillon 
745b06ebda0SMatthew Dillon 	switch (ocf) {
746b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_FAILED_CONTACT_CNTR:
747b06ebda0SMatthew Dillon 	case NG_HCI_OCF_RESET_FAILED_CONTACT_CNTR:
748b06ebda0SMatthew Dillon 	case NG_HCI_OCF_GET_LINK_QUALITY:
749b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_RSSI:
750b06ebda0SMatthew Dillon 		/* These do not need post processing */
751b06ebda0SMatthew Dillon 		break;
752b06ebda0SMatthew Dillon 
753b06ebda0SMatthew Dillon 	default:
754b06ebda0SMatthew Dillon 		error = EINVAL;
755b06ebda0SMatthew Dillon 		break;
756b06ebda0SMatthew Dillon 	}
757b06ebda0SMatthew Dillon 
758b06ebda0SMatthew Dillon 	NG_FREE_M(mcp);
759b06ebda0SMatthew Dillon 	NG_FREE_M(mrp);
760b06ebda0SMatthew Dillon 
761b06ebda0SMatthew Dillon 	return (error);
762b06ebda0SMatthew Dillon } /* process_status_params */
763b06ebda0SMatthew Dillon 
764b06ebda0SMatthew Dillon /*
765b06ebda0SMatthew Dillon  * Process testing command return parameters
766b06ebda0SMatthew Dillon  */
767b06ebda0SMatthew Dillon 
768b06ebda0SMatthew Dillon int
process_testing_params(ng_hci_unit_p unit,u_int16_t ocf,struct mbuf * mcp,struct mbuf * mrp)769b06ebda0SMatthew Dillon process_testing_params(ng_hci_unit_p unit, u_int16_t ocf, struct mbuf *mcp,
770b06ebda0SMatthew Dillon 		struct mbuf *mrp)
771b06ebda0SMatthew Dillon {
772b06ebda0SMatthew Dillon 	int	error = 0;
773b06ebda0SMatthew Dillon 
774b06ebda0SMatthew Dillon 	switch (ocf) {
775b06ebda0SMatthew Dillon 
776b06ebda0SMatthew Dillon 	/*
777b06ebda0SMatthew Dillon 	 * XXX FIXME
778b06ebda0SMatthew Dillon 	 * We do not support these features at this time. However,
779b06ebda0SMatthew Dillon 	 * HCI node could support this and do something smart. At least
780b06ebda0SMatthew Dillon 	 * node can change unit state.
781b06ebda0SMatthew Dillon 	 */
782b06ebda0SMatthew Dillon 
783b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_LOOPBACK_MODE:
784b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_LOOPBACK_MODE:
785b06ebda0SMatthew Dillon 	case NG_HCI_OCF_ENABLE_UNIT_UNDER_TEST:
786b06ebda0SMatthew Dillon 		break;
787b06ebda0SMatthew Dillon 
788b06ebda0SMatthew Dillon 	default:
789b06ebda0SMatthew Dillon 		error = EINVAL;
790b06ebda0SMatthew Dillon 		break;
791b06ebda0SMatthew Dillon 	}
792b06ebda0SMatthew Dillon 
793b06ebda0SMatthew Dillon 	NG_FREE_M(mcp);
794b06ebda0SMatthew Dillon 	NG_FREE_M(mrp);
795b06ebda0SMatthew Dillon 
796b06ebda0SMatthew Dillon 	return (error);
797b06ebda0SMatthew Dillon } /* process_testing_params */
798b06ebda0SMatthew Dillon 
799b06ebda0SMatthew Dillon /*
800b06ebda0SMatthew Dillon  * Process link control command status
801b06ebda0SMatthew Dillon  */
802b06ebda0SMatthew Dillon 
803b06ebda0SMatthew Dillon static int
process_link_control_status(ng_hci_unit_p unit,ng_hci_command_status_ep * ep,struct mbuf * mcp)804b06ebda0SMatthew Dillon process_link_control_status(ng_hci_unit_p unit, ng_hci_command_status_ep *ep,
805b06ebda0SMatthew Dillon 		struct mbuf *mcp)
806b06ebda0SMatthew Dillon {
807b06ebda0SMatthew Dillon 	int	error = 0;
808b06ebda0SMatthew Dillon 
809b06ebda0SMatthew Dillon 	switch (NG_HCI_OCF(ep->opcode)) {
810b06ebda0SMatthew Dillon 	case NG_HCI_OCF_INQUIRY:
811b06ebda0SMatthew Dillon 	case NG_HCI_OCF_DISCON:		/* XXX */
812b06ebda0SMatthew Dillon 	case NG_HCI_OCF_REJECT_CON:	/* XXX */
813b06ebda0SMatthew Dillon 	case NG_HCI_OCF_CHANGE_CON_PKT_TYPE:
814b06ebda0SMatthew Dillon 	case NG_HCI_OCF_AUTH_REQ:
815b06ebda0SMatthew Dillon 	case NG_HCI_OCF_SET_CON_ENCRYPTION:
816b06ebda0SMatthew Dillon 	case NG_HCI_OCF_CHANGE_CON_LINK_KEY:
817b06ebda0SMatthew Dillon 	case NG_HCI_OCF_MASTER_LINK_KEY:
818b06ebda0SMatthew Dillon 	case NG_HCI_OCF_REMOTE_NAME_REQ:
819b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_REMOTE_FEATURES:
820b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_REMOTE_VER_INFO:
821b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_CLOCK_OFFSET:
822b06ebda0SMatthew Dillon 		/* These do not need post processing */
823b06ebda0SMatthew Dillon 		break;
824b06ebda0SMatthew Dillon 
825b06ebda0SMatthew Dillon 	case NG_HCI_OCF_CREATE_CON:
826b06ebda0SMatthew Dillon 		break;
827b06ebda0SMatthew Dillon 
828b06ebda0SMatthew Dillon 	case NG_HCI_OCF_ADD_SCO_CON:
829b06ebda0SMatthew Dillon 		break;
830b06ebda0SMatthew Dillon 
831b06ebda0SMatthew Dillon 	case NG_HCI_OCF_ACCEPT_CON:
832b06ebda0SMatthew Dillon 		break;
833b06ebda0SMatthew Dillon 
834b06ebda0SMatthew Dillon 	case NG_HCI_OCF_INQUIRY_CANCEL:
835b06ebda0SMatthew Dillon 	case NG_HCI_OCF_PERIODIC_INQUIRY:
836b06ebda0SMatthew Dillon 	case NG_HCI_OCF_EXIT_PERIODIC_INQUIRY:
837b06ebda0SMatthew Dillon 	case NG_HCI_OCF_LINK_KEY_REP:
838b06ebda0SMatthew Dillon 	case NG_HCI_OCF_LINK_KEY_NEG_REP:
839b06ebda0SMatthew Dillon 	case NG_HCI_OCF_PIN_CODE_REP:
840b06ebda0SMatthew Dillon 	case NG_HCI_OCF_PIN_CODE_NEG_REP:
841b06ebda0SMatthew Dillon 	default:
842b06ebda0SMatthew Dillon 
843b06ebda0SMatthew Dillon 		/*
844b06ebda0SMatthew Dillon 		 * None of these command was supposed to generate
845b06ebda0SMatthew Dillon 		 * Command_Status event. Instead Command_Complete event
846b06ebda0SMatthew Dillon 		 * should have been sent.
847b06ebda0SMatthew Dillon 		 */
848b06ebda0SMatthew Dillon 
849b06ebda0SMatthew Dillon 		error = EINVAL;
850b06ebda0SMatthew Dillon 		break;
851b06ebda0SMatthew Dillon 	}
852b06ebda0SMatthew Dillon 
853b06ebda0SMatthew Dillon 	NG_FREE_M(mcp);
854b06ebda0SMatthew Dillon 
855b06ebda0SMatthew Dillon 	return (error);
856b06ebda0SMatthew Dillon } /* process_link_control_status */
857b06ebda0SMatthew Dillon 
858b06ebda0SMatthew Dillon /*
859b06ebda0SMatthew Dillon  * Process link policy command status
860b06ebda0SMatthew Dillon  */
861b06ebda0SMatthew Dillon 
862b06ebda0SMatthew Dillon static int
process_link_policy_status(ng_hci_unit_p unit,ng_hci_command_status_ep * ep,struct mbuf * mcp)863b06ebda0SMatthew Dillon process_link_policy_status(ng_hci_unit_p unit, ng_hci_command_status_ep *ep,
864b06ebda0SMatthew Dillon 		struct mbuf *mcp)
865b06ebda0SMatthew Dillon {
866b06ebda0SMatthew Dillon 	int	error = 0;
867b06ebda0SMatthew Dillon 
868b06ebda0SMatthew Dillon 	switch (NG_HCI_OCF(ep->opcode)) {
869b06ebda0SMatthew Dillon 	case NG_HCI_OCF_HOLD_MODE:
870b06ebda0SMatthew Dillon 	case NG_HCI_OCF_SNIFF_MODE:
871b06ebda0SMatthew Dillon 	case NG_HCI_OCF_EXIT_SNIFF_MODE:
872b06ebda0SMatthew Dillon 	case NG_HCI_OCF_PARK_MODE:
873b06ebda0SMatthew Dillon 	case NG_HCI_OCF_EXIT_PARK_MODE:
874b06ebda0SMatthew Dillon 	case NG_HCI_OCF_SWITCH_ROLE:
875b06ebda0SMatthew Dillon 		/* These do not need post processing */
876b06ebda0SMatthew Dillon 		break;
877b06ebda0SMatthew Dillon 
878b06ebda0SMatthew Dillon 	case NG_HCI_OCF_QOS_SETUP:
879b06ebda0SMatthew Dillon 		break;
880b06ebda0SMatthew Dillon 
881b06ebda0SMatthew Dillon 	case NG_HCI_OCF_ROLE_DISCOVERY:
882b06ebda0SMatthew Dillon 	case NG_HCI_OCF_READ_LINK_POLICY_SETTINGS:
883b06ebda0SMatthew Dillon 	case NG_HCI_OCF_WRITE_LINK_POLICY_SETTINGS:
884b06ebda0SMatthew Dillon 	default:
885b06ebda0SMatthew Dillon 
886b06ebda0SMatthew Dillon 		/*
887b06ebda0SMatthew Dillon 		 * None of these command was supposed to generate
888b06ebda0SMatthew Dillon 		 * Command_Status event. Instead Command_Complete event
889b06ebda0SMatthew Dillon 		 * should have been sent.
890b06ebda0SMatthew Dillon 		 */
891b06ebda0SMatthew Dillon 
892b06ebda0SMatthew Dillon 		error = EINVAL;
893b06ebda0SMatthew Dillon 		break;
894b06ebda0SMatthew Dillon 	}
895b06ebda0SMatthew Dillon 
896b06ebda0SMatthew Dillon 	NG_FREE_M(mcp);
897b06ebda0SMatthew Dillon 
898b06ebda0SMatthew Dillon 	return (error);
899b06ebda0SMatthew Dillon } /* process_link_policy_status */
900b06ebda0SMatthew Dillon 
901