hci_event.c revision 1.1.2.8 1 1.1.2.8 yamt /* $NetBSD: hci_event.c,v 1.1.2.8 2008/01/21 09:47:11 yamt Exp $ */
2 1.1.2.2 yamt
3 1.1.2.2 yamt /*-
4 1.1.2.2 yamt * Copyright (c) 2005 Iain Hibbert.
5 1.1.2.2 yamt * Copyright (c) 2006 Itronix Inc.
6 1.1.2.2 yamt * All rights reserved.
7 1.1.2.2 yamt *
8 1.1.2.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.1.2.2 yamt * modification, are permitted provided that the following conditions
10 1.1.2.2 yamt * are met:
11 1.1.2.2 yamt * 1. Redistributions of source code must retain the above copyright
12 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer.
13 1.1.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer in the
15 1.1.2.2 yamt * documentation and/or other materials provided with the distribution.
16 1.1.2.2 yamt * 3. The name of Itronix Inc. may not be used to endorse
17 1.1.2.2 yamt * or promote products derived from this software without specific
18 1.1.2.2 yamt * prior written permission.
19 1.1.2.2 yamt *
20 1.1.2.2 yamt * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21 1.1.2.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24 1.1.2.2 yamt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 1.1.2.2 yamt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.1.2.2 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 1.1.2.2 yamt * ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
31 1.1.2.2 yamt */
32 1.1.2.2 yamt
33 1.1.2.2 yamt #include <sys/cdefs.h>
34 1.1.2.8 yamt __KERNEL_RCSID(0, "$NetBSD: hci_event.c,v 1.1.2.8 2008/01/21 09:47:11 yamt Exp $");
35 1.1.2.2 yamt
36 1.1.2.2 yamt #include <sys/param.h>
37 1.1.2.2 yamt #include <sys/kernel.h>
38 1.1.2.2 yamt #include <sys/malloc.h>
39 1.1.2.2 yamt #include <sys/mbuf.h>
40 1.1.2.2 yamt #include <sys/proc.h>
41 1.1.2.2 yamt #include <sys/systm.h>
42 1.1.2.2 yamt
43 1.1.2.2 yamt #include <netbt/bluetooth.h>
44 1.1.2.2 yamt #include <netbt/hci.h>
45 1.1.2.2 yamt #include <netbt/sco.h>
46 1.1.2.2 yamt
47 1.1.2.2 yamt static void hci_event_inquiry_result(struct hci_unit *, struct mbuf *);
48 1.1.2.5 yamt static void hci_event_rssi_result(struct hci_unit *, struct mbuf *);
49 1.1.2.2 yamt static void hci_event_command_status(struct hci_unit *, struct mbuf *);
50 1.1.2.2 yamt static void hci_event_command_compl(struct hci_unit *, struct mbuf *);
51 1.1.2.2 yamt static void hci_event_con_compl(struct hci_unit *, struct mbuf *);
52 1.1.2.2 yamt static void hci_event_discon_compl(struct hci_unit *, struct mbuf *);
53 1.1.2.2 yamt static void hci_event_con_req(struct hci_unit *, struct mbuf *);
54 1.1.2.2 yamt static void hci_event_num_compl_pkts(struct hci_unit *, struct mbuf *);
55 1.1.2.4 yamt static void hci_event_auth_compl(struct hci_unit *, struct mbuf *);
56 1.1.2.4 yamt static void hci_event_encryption_change(struct hci_unit *, struct mbuf *);
57 1.1.2.4 yamt static void hci_event_change_con_link_key_compl(struct hci_unit *, struct mbuf *);
58 1.1.2.5 yamt static void hci_event_read_clock_offset_compl(struct hci_unit *, struct mbuf *);
59 1.1.2.2 yamt static void hci_cmd_read_bdaddr(struct hci_unit *, struct mbuf *);
60 1.1.2.2 yamt static void hci_cmd_read_buffer_size(struct hci_unit *, struct mbuf *);
61 1.1.2.2 yamt static void hci_cmd_read_local_features(struct hci_unit *, struct mbuf *);
62 1.1.2.8 yamt static void hci_cmd_read_local_ver(struct hci_unit *, struct mbuf *);
63 1.1.2.8 yamt static void hci_cmd_read_local_commands(struct hci_unit *, struct mbuf *);
64 1.1.2.2 yamt static void hci_cmd_reset(struct hci_unit *, struct mbuf *);
65 1.1.2.2 yamt
66 1.1.2.2 yamt #ifdef BLUETOOTH_DEBUG
67 1.1.2.4 yamt int bluetooth_debug;
68 1.1.2.2 yamt
69 1.1.2.2 yamt static const char *hci_eventnames[] = {
70 1.1.2.2 yamt /* 0x00 */ "NULL",
71 1.1.2.2 yamt /* 0x01 */ "INQUIRY COMPLETE",
72 1.1.2.2 yamt /* 0x02 */ "INQUIRY RESULT",
73 1.1.2.2 yamt /* 0x03 */ "CONN COMPLETE",
74 1.1.2.2 yamt /* 0x04 */ "CONN REQ",
75 1.1.2.2 yamt /* 0x05 */ "DISCONN COMPLETE",
76 1.1.2.2 yamt /* 0x06 */ "AUTH COMPLETE",
77 1.1.2.2 yamt /* 0x07 */ "REMOTE NAME REQ COMPLETE",
78 1.1.2.2 yamt /* 0x08 */ "ENCRYPTION CHANGE",
79 1.1.2.2 yamt /* 0x09 */ "CHANGE CONN LINK KEY COMPLETE",
80 1.1.2.2 yamt /* 0x0a */ "MASTER LINK KEY COMPLETE",
81 1.1.2.2 yamt /* 0x0b */ "READ REMOTE FEATURES COMPLETE",
82 1.1.2.2 yamt /* 0x0c */ "READ REMOTE VERSION INFO COMPLETE",
83 1.1.2.2 yamt /* 0x0d */ "QoS SETUP COMPLETE",
84 1.1.2.2 yamt /* 0x0e */ "COMMAND COMPLETE",
85 1.1.2.2 yamt /* 0x0f */ "COMMAND STATUS",
86 1.1.2.2 yamt /* 0x10 */ "HARDWARE ERROR",
87 1.1.2.2 yamt /* 0x11 */ "FLUSH OCCUR",
88 1.1.2.2 yamt /* 0x12 */ "ROLE CHANGE",
89 1.1.2.2 yamt /* 0x13 */ "NUM COMPLETED PACKETS",
90 1.1.2.2 yamt /* 0x14 */ "MODE CHANGE",
91 1.1.2.2 yamt /* 0x15 */ "RETURN LINK KEYS",
92 1.1.2.2 yamt /* 0x16 */ "PIN CODE REQ",
93 1.1.2.2 yamt /* 0x17 */ "LINK KEY REQ",
94 1.1.2.2 yamt /* 0x18 */ "LINK KEY NOTIFICATION",
95 1.1.2.2 yamt /* 0x19 */ "LOOPBACK COMMAND",
96 1.1.2.2 yamt /* 0x1a */ "DATA BUFFER OVERFLOW",
97 1.1.2.2 yamt /* 0x1b */ "MAX SLOT CHANGE",
98 1.1.2.2 yamt /* 0x1c */ "READ CLOCK OFFSET COMPLETE",
99 1.1.2.2 yamt /* 0x1d */ "CONN PKT TYPE CHANGED",
100 1.1.2.2 yamt /* 0x1e */ "QOS VIOLATION",
101 1.1.2.2 yamt /* 0x1f */ "PAGE SCAN MODE CHANGE",
102 1.1.2.2 yamt /* 0x20 */ "PAGE SCAN REP MODE CHANGE",
103 1.1.2.2 yamt /* 0x21 */ "FLOW SPECIFICATION COMPLETE",
104 1.1.2.2 yamt /* 0x22 */ "RSSI RESULT",
105 1.1.2.2 yamt /* 0x23 */ "READ REMOTE EXT FEATURES"
106 1.1.2.2 yamt };
107 1.1.2.2 yamt
108 1.1.2.2 yamt static const char *
109 1.1.2.2 yamt hci_eventstr(unsigned int event)
110 1.1.2.2 yamt {
111 1.1.2.2 yamt
112 1.1.2.2 yamt if (event < (sizeof(hci_eventnames) / sizeof(*hci_eventnames)))
113 1.1.2.2 yamt return hci_eventnames[event];
114 1.1.2.2 yamt
115 1.1.2.2 yamt switch (event) {
116 1.1.2.2 yamt case HCI_EVENT_SCO_CON_COMPL: /* 0x2c */
117 1.1.2.2 yamt return "SCO CON COMPLETE";
118 1.1.2.2 yamt
119 1.1.2.2 yamt case HCI_EVENT_SCO_CON_CHANGED: /* 0x2d */
120 1.1.2.2 yamt return "SCO CON CHANGED";
121 1.1.2.2 yamt
122 1.1.2.2 yamt case HCI_EVENT_BT_LOGO: /* 0xfe */
123 1.1.2.2 yamt return "BT_LOGO";
124 1.1.2.2 yamt
125 1.1.2.2 yamt case HCI_EVENT_VENDOR: /* 0xff */
126 1.1.2.2 yamt return "VENDOR";
127 1.1.2.2 yamt }
128 1.1.2.2 yamt
129 1.1.2.2 yamt return "UNRECOGNISED";
130 1.1.2.2 yamt }
131 1.1.2.2 yamt #endif /* BLUETOOTH_DEBUG */
132 1.1.2.2 yamt
133 1.1.2.2 yamt /*
134 1.1.2.2 yamt * process HCI Events
135 1.1.2.2 yamt *
136 1.1.2.2 yamt * We will free the mbuf at the end, no need for any sub
137 1.1.2.2 yamt * functions to handle that. We kind of assume that the
138 1.1.2.2 yamt * device sends us valid events.
139 1.1.2.2 yamt */
140 1.1.2.2 yamt void
141 1.1.2.2 yamt hci_event(struct mbuf *m, struct hci_unit *unit)
142 1.1.2.2 yamt {
143 1.1.2.2 yamt hci_event_hdr_t hdr;
144 1.1.2.2 yamt
145 1.1.2.2 yamt KASSERT(m->m_flags & M_PKTHDR);
146 1.1.2.2 yamt
147 1.1.2.2 yamt KASSERT(m->m_pkthdr.len >= sizeof(hdr));
148 1.1.2.2 yamt m_copydata(m, 0, sizeof(hdr), &hdr);
149 1.1.2.2 yamt m_adj(m, sizeof(hdr));
150 1.1.2.2 yamt
151 1.1.2.2 yamt KASSERT(hdr.type == HCI_EVENT_PKT);
152 1.1.2.2 yamt
153 1.1.2.6 yamt DPRINTFN(1, "(%s) event %s\n",
154 1.1.2.6 yamt device_xname(unit->hci_dev), hci_eventstr(hdr.event));
155 1.1.2.2 yamt
156 1.1.2.2 yamt switch(hdr.event) {
157 1.1.2.2 yamt case HCI_EVENT_COMMAND_STATUS:
158 1.1.2.2 yamt hci_event_command_status(unit, m);
159 1.1.2.2 yamt break;
160 1.1.2.2 yamt
161 1.1.2.2 yamt case HCI_EVENT_COMMAND_COMPL:
162 1.1.2.2 yamt hci_event_command_compl(unit, m);
163 1.1.2.2 yamt break;
164 1.1.2.2 yamt
165 1.1.2.2 yamt case HCI_EVENT_NUM_COMPL_PKTS:
166 1.1.2.2 yamt hci_event_num_compl_pkts(unit, m);
167 1.1.2.2 yamt break;
168 1.1.2.2 yamt
169 1.1.2.2 yamt case HCI_EVENT_INQUIRY_RESULT:
170 1.1.2.2 yamt hci_event_inquiry_result(unit, m);
171 1.1.2.2 yamt break;
172 1.1.2.2 yamt
173 1.1.2.5 yamt case HCI_EVENT_RSSI_RESULT:
174 1.1.2.5 yamt hci_event_rssi_result(unit, m);
175 1.1.2.5 yamt break;
176 1.1.2.5 yamt
177 1.1.2.2 yamt case HCI_EVENT_CON_COMPL:
178 1.1.2.2 yamt hci_event_con_compl(unit, m);
179 1.1.2.2 yamt break;
180 1.1.2.2 yamt
181 1.1.2.2 yamt case HCI_EVENT_DISCON_COMPL:
182 1.1.2.2 yamt hci_event_discon_compl(unit, m);
183 1.1.2.2 yamt break;
184 1.1.2.2 yamt
185 1.1.2.2 yamt case HCI_EVENT_CON_REQ:
186 1.1.2.2 yamt hci_event_con_req(unit, m);
187 1.1.2.2 yamt break;
188 1.1.2.2 yamt
189 1.1.2.2 yamt case HCI_EVENT_AUTH_COMPL:
190 1.1.2.4 yamt hci_event_auth_compl(unit, m);
191 1.1.2.4 yamt break;
192 1.1.2.4 yamt
193 1.1.2.2 yamt case HCI_EVENT_ENCRYPTION_CHANGE:
194 1.1.2.4 yamt hci_event_encryption_change(unit, m);
195 1.1.2.4 yamt break;
196 1.1.2.4 yamt
197 1.1.2.2 yamt case HCI_EVENT_CHANGE_CON_LINK_KEY_COMPL:
198 1.1.2.4 yamt hci_event_change_con_link_key_compl(unit, m);
199 1.1.2.4 yamt break;
200 1.1.2.4 yamt
201 1.1.2.5 yamt case HCI_EVENT_READ_CLOCK_OFFSET_COMPL:
202 1.1.2.5 yamt hci_event_read_clock_offset_compl(unit, m);
203 1.1.2.5 yamt break;
204 1.1.2.5 yamt
205 1.1.2.4 yamt case HCI_EVENT_SCO_CON_COMPL:
206 1.1.2.4 yamt case HCI_EVENT_INQUIRY_COMPL:
207 1.1.2.4 yamt case HCI_EVENT_REMOTE_NAME_REQ_COMPL:
208 1.1.2.2 yamt case HCI_EVENT_MASTER_LINK_KEY_COMPL:
209 1.1.2.2 yamt case HCI_EVENT_READ_REMOTE_FEATURES_COMPL:
210 1.1.2.2 yamt case HCI_EVENT_READ_REMOTE_VER_INFO_COMPL:
211 1.1.2.2 yamt case HCI_EVENT_QOS_SETUP_COMPL:
212 1.1.2.2 yamt case HCI_EVENT_HARDWARE_ERROR:
213 1.1.2.2 yamt case HCI_EVENT_FLUSH_OCCUR:
214 1.1.2.2 yamt case HCI_EVENT_ROLE_CHANGE:
215 1.1.2.2 yamt case HCI_EVENT_MODE_CHANGE:
216 1.1.2.2 yamt case HCI_EVENT_RETURN_LINK_KEYS:
217 1.1.2.2 yamt case HCI_EVENT_PIN_CODE_REQ:
218 1.1.2.2 yamt case HCI_EVENT_LINK_KEY_REQ:
219 1.1.2.2 yamt case HCI_EVENT_LINK_KEY_NOTIFICATION:
220 1.1.2.2 yamt case HCI_EVENT_LOOPBACK_COMMAND:
221 1.1.2.2 yamt case HCI_EVENT_DATA_BUFFER_OVERFLOW:
222 1.1.2.2 yamt case HCI_EVENT_MAX_SLOT_CHANGE:
223 1.1.2.2 yamt case HCI_EVENT_CON_PKT_TYPE_CHANGED:
224 1.1.2.2 yamt case HCI_EVENT_QOS_VIOLATION:
225 1.1.2.2 yamt case HCI_EVENT_PAGE_SCAN_MODE_CHANGE:
226 1.1.2.2 yamt case HCI_EVENT_PAGE_SCAN_REP_MODE_CHANGE:
227 1.1.2.2 yamt case HCI_EVENT_FLOW_SPECIFICATION_COMPL:
228 1.1.2.2 yamt case HCI_EVENT_READ_REMOTE_EXTENDED_FEATURES:
229 1.1.2.2 yamt case HCI_EVENT_SCO_CON_CHANGED:
230 1.1.2.2 yamt case HCI_EVENT_BT_LOGO:
231 1.1.2.2 yamt case HCI_EVENT_VENDOR:
232 1.1.2.2 yamt break;
233 1.1.2.2 yamt
234 1.1.2.2 yamt default:
235 1.1.2.2 yamt UNKNOWN(hdr.event);
236 1.1.2.2 yamt break;
237 1.1.2.2 yamt }
238 1.1.2.2 yamt
239 1.1.2.2 yamt m_freem(m);
240 1.1.2.2 yamt }
241 1.1.2.2 yamt
242 1.1.2.2 yamt /*
243 1.1.2.2 yamt * Command Status
244 1.1.2.2 yamt *
245 1.1.2.2 yamt * Update our record of num_cmd_pkts then post-process any pending commands
246 1.1.2.2 yamt * and optionally restart cmd output on the unit.
247 1.1.2.2 yamt */
248 1.1.2.2 yamt static void
249 1.1.2.2 yamt hci_event_command_status(struct hci_unit *unit, struct mbuf *m)
250 1.1.2.2 yamt {
251 1.1.2.2 yamt hci_command_status_ep ep;
252 1.1.2.2 yamt
253 1.1.2.2 yamt KASSERT(m->m_pkthdr.len >= sizeof(ep));
254 1.1.2.2 yamt m_copydata(m, 0, sizeof(ep), &ep);
255 1.1.2.2 yamt m_adj(m, sizeof(ep));
256 1.1.2.2 yamt
257 1.1.2.4 yamt DPRINTFN(1, "(%s) opcode (%03x|%04x) status = 0x%x num_cmd_pkts = %d\n",
258 1.1.2.6 yamt device_xname(unit->hci_dev),
259 1.1.2.2 yamt HCI_OGF(le16toh(ep.opcode)), HCI_OCF(le16toh(ep.opcode)),
260 1.1.2.4 yamt ep.status,
261 1.1.2.2 yamt ep.num_cmd_pkts);
262 1.1.2.2 yamt
263 1.1.2.7 yamt if (ep.status > 0)
264 1.1.2.7 yamt aprint_error_dev(unit->hci_dev,
265 1.1.2.7 yamt "CommandStatus opcode (%03x|%04x) failed (status=0x%02x)\n",
266 1.1.2.7 yamt HCI_OGF(le16toh(ep.opcode)), HCI_OCF(le16toh(ep.opcode)),
267 1.1.2.7 yamt ep.status);
268 1.1.2.7 yamt
269 1.1.2.2 yamt unit->hci_num_cmd_pkts = ep.num_cmd_pkts;
270 1.1.2.2 yamt
271 1.1.2.2 yamt /*
272 1.1.2.2 yamt * post processing of pending commands
273 1.1.2.2 yamt */
274 1.1.2.2 yamt switch(le16toh(ep.opcode)) {
275 1.1.2.2 yamt default:
276 1.1.2.2 yamt break;
277 1.1.2.2 yamt }
278 1.1.2.2 yamt
279 1.1.2.2 yamt while (unit->hci_num_cmd_pkts > 0 && MBUFQ_FIRST(&unit->hci_cmdwait)) {
280 1.1.2.2 yamt MBUFQ_DEQUEUE(&unit->hci_cmdwait, m);
281 1.1.2.2 yamt hci_output_cmd(unit, m);
282 1.1.2.2 yamt }
283 1.1.2.2 yamt }
284 1.1.2.2 yamt
285 1.1.2.2 yamt /*
286 1.1.2.2 yamt * Command Complete
287 1.1.2.2 yamt *
288 1.1.2.2 yamt * Update our record of num_cmd_pkts then handle the completed command,
289 1.1.2.2 yamt * and optionally restart cmd output on the unit.
290 1.1.2.2 yamt */
291 1.1.2.2 yamt static void
292 1.1.2.2 yamt hci_event_command_compl(struct hci_unit *unit, struct mbuf *m)
293 1.1.2.2 yamt {
294 1.1.2.2 yamt hci_command_compl_ep ep;
295 1.1.2.7 yamt hci_status_rp rp;
296 1.1.2.2 yamt
297 1.1.2.2 yamt KASSERT(m->m_pkthdr.len >= sizeof(ep));
298 1.1.2.2 yamt m_copydata(m, 0, sizeof(ep), &ep);
299 1.1.2.2 yamt m_adj(m, sizeof(ep));
300 1.1.2.2 yamt
301 1.1.2.2 yamt DPRINTFN(1, "(%s) opcode (%03x|%04x) num_cmd_pkts = %d\n",
302 1.1.2.6 yamt device_xname(unit->hci_dev),
303 1.1.2.2 yamt HCI_OGF(le16toh(ep.opcode)), HCI_OCF(le16toh(ep.opcode)),
304 1.1.2.2 yamt ep.num_cmd_pkts);
305 1.1.2.2 yamt
306 1.1.2.7 yamt /*
307 1.1.2.7 yamt * I am not sure if this is completely correct, it is not guaranteed
308 1.1.2.7 yamt * that a command_complete packet will contain the status though most
309 1.1.2.7 yamt * do seem to.
310 1.1.2.7 yamt */
311 1.1.2.7 yamt m_copydata(m, 0, sizeof(rp), &rp);
312 1.1.2.7 yamt if (rp.status > 0)
313 1.1.2.7 yamt aprint_error_dev(unit->hci_dev,
314 1.1.2.7 yamt "CommandComplete opcode (%03x|%04x) failed (status=0x%02x)\n",
315 1.1.2.7 yamt HCI_OGF(le16toh(ep.opcode)), HCI_OCF(le16toh(ep.opcode)),
316 1.1.2.7 yamt rp.status);
317 1.1.2.7 yamt
318 1.1.2.2 yamt unit->hci_num_cmd_pkts = ep.num_cmd_pkts;
319 1.1.2.2 yamt
320 1.1.2.2 yamt /*
321 1.1.2.2 yamt * post processing of completed commands
322 1.1.2.2 yamt */
323 1.1.2.2 yamt switch(le16toh(ep.opcode)) {
324 1.1.2.2 yamt case HCI_CMD_READ_BDADDR:
325 1.1.2.2 yamt hci_cmd_read_bdaddr(unit, m);
326 1.1.2.2 yamt break;
327 1.1.2.2 yamt
328 1.1.2.2 yamt case HCI_CMD_READ_BUFFER_SIZE:
329 1.1.2.2 yamt hci_cmd_read_buffer_size(unit, m);
330 1.1.2.2 yamt break;
331 1.1.2.2 yamt
332 1.1.2.2 yamt case HCI_CMD_READ_LOCAL_FEATURES:
333 1.1.2.2 yamt hci_cmd_read_local_features(unit, m);
334 1.1.2.2 yamt break;
335 1.1.2.2 yamt
336 1.1.2.8 yamt case HCI_CMD_READ_LOCAL_VER:
337 1.1.2.8 yamt hci_cmd_read_local_ver(unit, m);
338 1.1.2.8 yamt break;
339 1.1.2.8 yamt
340 1.1.2.8 yamt case HCI_CMD_READ_LOCAL_COMMANDS:
341 1.1.2.8 yamt hci_cmd_read_local_commands(unit, m);
342 1.1.2.8 yamt break;
343 1.1.2.8 yamt
344 1.1.2.2 yamt case HCI_CMD_RESET:
345 1.1.2.2 yamt hci_cmd_reset(unit, m);
346 1.1.2.2 yamt break;
347 1.1.2.2 yamt
348 1.1.2.2 yamt default:
349 1.1.2.2 yamt break;
350 1.1.2.2 yamt }
351 1.1.2.2 yamt
352 1.1.2.2 yamt while (unit->hci_num_cmd_pkts > 0 && MBUFQ_FIRST(&unit->hci_cmdwait)) {
353 1.1.2.2 yamt MBUFQ_DEQUEUE(&unit->hci_cmdwait, m);
354 1.1.2.2 yamt hci_output_cmd(unit, m);
355 1.1.2.2 yamt }
356 1.1.2.2 yamt }
357 1.1.2.2 yamt
358 1.1.2.2 yamt /*
359 1.1.2.2 yamt * Number of Completed Packets
360 1.1.2.2 yamt *
361 1.1.2.2 yamt * This is sent periodically by the Controller telling us how many
362 1.1.2.2 yamt * buffers are now freed up and which handle was using them. From
363 1.1.2.2 yamt * this we determine which type of buffer it was and add the qty
364 1.1.2.2 yamt * back into the relevant packet counter, then restart output on
365 1.1.2.2 yamt * links that have halted.
366 1.1.2.2 yamt */
367 1.1.2.2 yamt static void
368 1.1.2.2 yamt hci_event_num_compl_pkts(struct hci_unit *unit, struct mbuf *m)
369 1.1.2.2 yamt {
370 1.1.2.2 yamt hci_num_compl_pkts_ep ep;
371 1.1.2.2 yamt struct hci_link *link, *next;
372 1.1.2.2 yamt uint16_t handle, num;
373 1.1.2.2 yamt int num_acl = 0, num_sco = 0;
374 1.1.2.2 yamt
375 1.1.2.2 yamt KASSERT(m->m_pkthdr.len >= sizeof(ep));
376 1.1.2.2 yamt m_copydata(m, 0, sizeof(ep), &ep);
377 1.1.2.2 yamt m_adj(m, sizeof(ep));
378 1.1.2.2 yamt
379 1.1.2.2 yamt while (ep.num_con_handles--) {
380 1.1.2.4 yamt m_copydata(m, 0, sizeof(handle), &handle);
381 1.1.2.2 yamt m_adj(m, sizeof(handle));
382 1.1.2.2 yamt handle = le16toh(handle);
383 1.1.2.2 yamt
384 1.1.2.4 yamt m_copydata(m, 0, sizeof(num), &num);
385 1.1.2.2 yamt m_adj(m, sizeof(num));
386 1.1.2.2 yamt num = le16toh(num);
387 1.1.2.2 yamt
388 1.1.2.2 yamt link = hci_link_lookup_handle(unit, handle);
389 1.1.2.2 yamt if (link) {
390 1.1.2.2 yamt if (link->hl_type == HCI_LINK_ACL) {
391 1.1.2.2 yamt num_acl += num;
392 1.1.2.2 yamt hci_acl_complete(link, num);
393 1.1.2.2 yamt } else {
394 1.1.2.2 yamt num_sco += num;
395 1.1.2.2 yamt hci_sco_complete(link, num);
396 1.1.2.2 yamt }
397 1.1.2.2 yamt } else {
398 1.1.2.4 yamt /* XXX need to issue Read_Buffer_Size or Reset? */
399 1.1.2.6 yamt aprint_error_dev(unit->hci_dev,
400 1.1.2.6 yamt "unknown handle %d! (losing track of %d packet buffer%s)\n",
401 1.1.2.6 yamt handle, num, (num == 1 ? "" : "s"));
402 1.1.2.2 yamt }
403 1.1.2.2 yamt }
404 1.1.2.2 yamt
405 1.1.2.2 yamt /*
406 1.1.2.2 yamt * Move up any queued packets. When a link has sent data, it will move
407 1.1.2.2 yamt * to the back of the queue - technically then if a link had something
408 1.1.2.2 yamt * to send and there were still buffers available it could get started
409 1.1.2.2 yamt * twice but it seemed more important to to handle higher loads fairly
410 1.1.2.2 yamt * than worry about wasting cycles when we are not busy.
411 1.1.2.2 yamt */
412 1.1.2.2 yamt
413 1.1.2.2 yamt unit->hci_num_acl_pkts += num_acl;
414 1.1.2.2 yamt unit->hci_num_sco_pkts += num_sco;
415 1.1.2.2 yamt
416 1.1.2.2 yamt link = TAILQ_FIRST(&unit->hci_links);
417 1.1.2.2 yamt while (link && (unit->hci_num_acl_pkts > 0 || unit->hci_num_sco_pkts > 0)) {
418 1.1.2.2 yamt next = TAILQ_NEXT(link, hl_next);
419 1.1.2.2 yamt
420 1.1.2.2 yamt if (link->hl_type == HCI_LINK_ACL) {
421 1.1.2.2 yamt if (unit->hci_num_acl_pkts > 0 && link->hl_txqlen > 0)
422 1.1.2.2 yamt hci_acl_start(link);
423 1.1.2.2 yamt } else {
424 1.1.2.2 yamt if (unit->hci_num_sco_pkts > 0 && link->hl_txqlen > 0)
425 1.1.2.2 yamt hci_sco_start(link);
426 1.1.2.2 yamt }
427 1.1.2.2 yamt
428 1.1.2.2 yamt link = next;
429 1.1.2.2 yamt }
430 1.1.2.2 yamt }
431 1.1.2.2 yamt
432 1.1.2.2 yamt /*
433 1.1.2.2 yamt * Inquiry Result
434 1.1.2.2 yamt *
435 1.1.2.2 yamt * keep a note of devices seen, so we know which unit to use
436 1.1.2.2 yamt * on outgoing connections
437 1.1.2.2 yamt */
438 1.1.2.2 yamt static void
439 1.1.2.2 yamt hci_event_inquiry_result(struct hci_unit *unit, struct mbuf *m)
440 1.1.2.2 yamt {
441 1.1.2.2 yamt hci_inquiry_result_ep ep;
442 1.1.2.5 yamt hci_inquiry_response ir;
443 1.1.2.2 yamt struct hci_memo *memo;
444 1.1.2.2 yamt
445 1.1.2.2 yamt KASSERT(m->m_pkthdr.len >= sizeof(ep));
446 1.1.2.2 yamt m_copydata(m, 0, sizeof(ep), &ep);
447 1.1.2.2 yamt m_adj(m, sizeof(ep));
448 1.1.2.2 yamt
449 1.1.2.2 yamt DPRINTFN(1, "%d response%s\n", ep.num_responses,
450 1.1.2.2 yamt (ep.num_responses == 1 ? "" : "s"));
451 1.1.2.2 yamt
452 1.1.2.2 yamt while(ep.num_responses--) {
453 1.1.2.5 yamt KASSERT(m->m_pkthdr.len >= sizeof(ir));
454 1.1.2.5 yamt m_copydata(m, 0, sizeof(ir), &ir);
455 1.1.2.5 yamt m_adj(m, sizeof(ir));
456 1.1.2.2 yamt
457 1.1.2.2 yamt DPRINTFN(1, "bdaddr %02x:%02x:%02x:%02x:%02x:%02x\n",
458 1.1.2.5 yamt ir.bdaddr.b[5], ir.bdaddr.b[4], ir.bdaddr.b[3],
459 1.1.2.5 yamt ir.bdaddr.b[2], ir.bdaddr.b[1], ir.bdaddr.b[0]);
460 1.1.2.2 yamt
461 1.1.2.5 yamt memo = hci_memo_new(unit, &ir.bdaddr);
462 1.1.2.5 yamt if (memo != NULL) {
463 1.1.2.5 yamt memo->page_scan_rep_mode = ir.page_scan_rep_mode;
464 1.1.2.5 yamt memo->page_scan_mode = ir.page_scan_mode;
465 1.1.2.5 yamt memo->clock_offset = ir.clock_offset;
466 1.1.2.2 yamt }
467 1.1.2.5 yamt }
468 1.1.2.5 yamt }
469 1.1.2.2 yamt
470 1.1.2.5 yamt /*
471 1.1.2.5 yamt * Inquiry Result with RSSI
472 1.1.2.5 yamt *
473 1.1.2.5 yamt * as above but different packet when RSSI result is enabled
474 1.1.2.5 yamt */
475 1.1.2.5 yamt static void
476 1.1.2.5 yamt hci_event_rssi_result(struct hci_unit *unit, struct mbuf *m)
477 1.1.2.5 yamt {
478 1.1.2.5 yamt hci_rssi_result_ep ep;
479 1.1.2.5 yamt hci_rssi_response rr;
480 1.1.2.5 yamt struct hci_memo *memo;
481 1.1.2.3 yamt
482 1.1.2.5 yamt KASSERT(m->m_pkthdr.len >= sizeof(ep));
483 1.1.2.5 yamt m_copydata(m, 0, sizeof(ep), &ep);
484 1.1.2.5 yamt m_adj(m, sizeof(ep));
485 1.1.2.5 yamt
486 1.1.2.5 yamt DPRINTFN(1, "%d response%s\n", ep.num_responses,
487 1.1.2.5 yamt (ep.num_responses == 1 ? "" : "s"));
488 1.1.2.5 yamt
489 1.1.2.5 yamt while(ep.num_responses--) {
490 1.1.2.5 yamt KASSERT(m->m_pkthdr.len >= sizeof(rr));
491 1.1.2.5 yamt m_copydata(m, 0, sizeof(rr), &rr);
492 1.1.2.5 yamt m_adj(m, sizeof(rr));
493 1.1.2.5 yamt
494 1.1.2.5 yamt DPRINTFN(1, "bdaddr %02x:%02x:%02x:%02x:%02x:%02x\n",
495 1.1.2.5 yamt rr.bdaddr.b[5], rr.bdaddr.b[4], rr.bdaddr.b[3],
496 1.1.2.5 yamt rr.bdaddr.b[2], rr.bdaddr.b[1], rr.bdaddr.b[0]);
497 1.1.2.5 yamt
498 1.1.2.5 yamt memo = hci_memo_new(unit, &rr.bdaddr);
499 1.1.2.5 yamt if (memo != NULL) {
500 1.1.2.5 yamt memo->page_scan_rep_mode = rr.page_scan_rep_mode;
501 1.1.2.5 yamt memo->page_scan_mode = 0;
502 1.1.2.5 yamt memo->clock_offset = rr.clock_offset;
503 1.1.2.5 yamt }
504 1.1.2.2 yamt }
505 1.1.2.2 yamt }
506 1.1.2.2 yamt
507 1.1.2.2 yamt /*
508 1.1.2.2 yamt * Connection Complete
509 1.1.2.2 yamt *
510 1.1.2.2 yamt * Sent to us when a connection is made. If there is no link
511 1.1.2.2 yamt * structure already allocated for this, we must have changed
512 1.1.2.2 yamt * our mind, so just disconnect.
513 1.1.2.2 yamt */
514 1.1.2.2 yamt static void
515 1.1.2.2 yamt hci_event_con_compl(struct hci_unit *unit, struct mbuf *m)
516 1.1.2.2 yamt {
517 1.1.2.2 yamt hci_con_compl_ep ep;
518 1.1.2.2 yamt hci_write_link_policy_settings_cp cp;
519 1.1.2.2 yamt struct hci_link *link;
520 1.1.2.2 yamt int err;
521 1.1.2.2 yamt
522 1.1.2.2 yamt KASSERT(m->m_pkthdr.len >= sizeof(ep));
523 1.1.2.2 yamt m_copydata(m, 0, sizeof(ep), &ep);
524 1.1.2.2 yamt m_adj(m, sizeof(ep));
525 1.1.2.2 yamt
526 1.1.2.2 yamt DPRINTFN(1, "(%s) %s connection complete for "
527 1.1.2.2 yamt "%02x:%02x:%02x:%02x:%02x:%02x status %#x\n",
528 1.1.2.6 yamt device_xname(unit->hci_dev),
529 1.1.2.2 yamt (ep.link_type == HCI_LINK_ACL ? "ACL" : "SCO"),
530 1.1.2.2 yamt ep.bdaddr.b[5], ep.bdaddr.b[4], ep.bdaddr.b[3],
531 1.1.2.2 yamt ep.bdaddr.b[2], ep.bdaddr.b[1], ep.bdaddr.b[0],
532 1.1.2.2 yamt ep.status);
533 1.1.2.2 yamt
534 1.1.2.2 yamt link = hci_link_lookup_bdaddr(unit, &ep.bdaddr, ep.link_type);
535 1.1.2.2 yamt
536 1.1.2.2 yamt if (ep.status) {
537 1.1.2.2 yamt if (link != NULL) {
538 1.1.2.2 yamt switch (ep.status) {
539 1.1.2.2 yamt case 0x04: /* "Page Timeout" */
540 1.1.2.2 yamt err = EHOSTDOWN;
541 1.1.2.2 yamt break;
542 1.1.2.2 yamt
543 1.1.2.2 yamt case 0x08: /* "Connection Timed Out" */
544 1.1.2.2 yamt err = ETIMEDOUT;
545 1.1.2.2 yamt break;
546 1.1.2.2 yamt
547 1.1.2.2 yamt case 0x16: /* "Connection Terminated by Local Host" */
548 1.1.2.2 yamt err = 0;
549 1.1.2.2 yamt break;
550 1.1.2.2 yamt
551 1.1.2.2 yamt default:
552 1.1.2.2 yamt err = ECONNREFUSED;
553 1.1.2.2 yamt break;
554 1.1.2.2 yamt }
555 1.1.2.2 yamt
556 1.1.2.2 yamt hci_link_free(link, err);
557 1.1.2.2 yamt }
558 1.1.2.2 yamt
559 1.1.2.2 yamt return;
560 1.1.2.2 yamt }
561 1.1.2.2 yamt
562 1.1.2.2 yamt if (link == NULL) {
563 1.1.2.2 yamt hci_discon_cp dp;
564 1.1.2.2 yamt
565 1.1.2.2 yamt dp.con_handle = ep.con_handle;
566 1.1.2.2 yamt dp.reason = 0x13; /* "Remote User Terminated Connection" */
567 1.1.2.2 yamt
568 1.1.2.2 yamt hci_send_cmd(unit, HCI_CMD_DISCONNECT, &dp, sizeof(dp));
569 1.1.2.2 yamt return;
570 1.1.2.2 yamt }
571 1.1.2.2 yamt
572 1.1.2.4 yamt /* XXX could check auth_enable here */
573 1.1.2.4 yamt
574 1.1.2.4 yamt if (ep.encryption_mode)
575 1.1.2.4 yamt link->hl_flags |= (HCI_LINK_AUTH | HCI_LINK_ENCRYPT);
576 1.1.2.4 yamt
577 1.1.2.2 yamt link->hl_state = HCI_LINK_OPEN;
578 1.1.2.2 yamt link->hl_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
579 1.1.2.2 yamt
580 1.1.2.2 yamt if (ep.link_type == HCI_LINK_ACL) {
581 1.1.2.2 yamt cp.con_handle = ep.con_handle;
582 1.1.2.2 yamt cp.settings = htole16(unit->hci_link_policy);
583 1.1.2.2 yamt err = hci_send_cmd(unit, HCI_CMD_WRITE_LINK_POLICY_SETTINGS,
584 1.1.2.2 yamt &cp, sizeof(cp));
585 1.1.2.2 yamt if (err)
586 1.1.2.6 yamt aprint_error_dev(unit->hci_dev,
587 1.1.2.6 yamt "Warning, could not write link policy\n");
588 1.1.2.2 yamt
589 1.1.2.5 yamt err = hci_send_cmd(unit, HCI_CMD_READ_CLOCK_OFFSET,
590 1.1.2.5 yamt &cp.con_handle, sizeof(cp.con_handle));
591 1.1.2.5 yamt if (err)
592 1.1.2.6 yamt aprint_error_dev(unit->hci_dev,
593 1.1.2.6 yamt "Warning, could not read clock offset\n");
594 1.1.2.5 yamt
595 1.1.2.4 yamt err = hci_acl_setmode(link);
596 1.1.2.4 yamt if (err == EINPROGRESS)
597 1.1.2.4 yamt return;
598 1.1.2.4 yamt
599 1.1.2.4 yamt hci_acl_linkmode(link);
600 1.1.2.2 yamt } else {
601 1.1.2.2 yamt (*link->hl_sco->sp_proto->connected)(link->hl_sco->sp_upper);
602 1.1.2.2 yamt }
603 1.1.2.2 yamt }
604 1.1.2.2 yamt
605 1.1.2.2 yamt /*
606 1.1.2.2 yamt * Disconnection Complete
607 1.1.2.2 yamt *
608 1.1.2.2 yamt * This is sent in response to a disconnection request, but also if
609 1.1.2.2 yamt * the remote device goes out of range.
610 1.1.2.2 yamt */
611 1.1.2.2 yamt static void
612 1.1.2.2 yamt hci_event_discon_compl(struct hci_unit *unit, struct mbuf *m)
613 1.1.2.2 yamt {
614 1.1.2.2 yamt hci_discon_compl_ep ep;
615 1.1.2.2 yamt struct hci_link *link;
616 1.1.2.2 yamt
617 1.1.2.2 yamt KASSERT(m->m_pkthdr.len >= sizeof(ep));
618 1.1.2.2 yamt m_copydata(m, 0, sizeof(ep), &ep);
619 1.1.2.2 yamt m_adj(m, sizeof(ep));
620 1.1.2.2 yamt
621 1.1.2.2 yamt ep.con_handle = le16toh(ep.con_handle);
622 1.1.2.2 yamt
623 1.1.2.2 yamt DPRINTFN(1, "handle #%d, status=0x%x\n", ep.con_handle, ep.status);
624 1.1.2.2 yamt
625 1.1.2.2 yamt link = hci_link_lookup_handle(unit, HCI_CON_HANDLE(ep.con_handle));
626 1.1.2.2 yamt if (link)
627 1.1.2.2 yamt hci_link_free(link, ENOLINK);
628 1.1.2.2 yamt }
629 1.1.2.2 yamt
630 1.1.2.2 yamt /*
631 1.1.2.2 yamt * Connect Request
632 1.1.2.2 yamt *
633 1.1.2.2 yamt * We check upstream for appropriate listeners and accept connections
634 1.1.2.2 yamt * that are wanted.
635 1.1.2.2 yamt */
636 1.1.2.2 yamt static void
637 1.1.2.2 yamt hci_event_con_req(struct hci_unit *unit, struct mbuf *m)
638 1.1.2.2 yamt {
639 1.1.2.2 yamt hci_con_req_ep ep;
640 1.1.2.2 yamt hci_accept_con_cp ap;
641 1.1.2.2 yamt hci_reject_con_cp rp;
642 1.1.2.2 yamt struct hci_link *link;
643 1.1.2.2 yamt
644 1.1.2.2 yamt KASSERT(m->m_pkthdr.len >= sizeof(ep));
645 1.1.2.2 yamt m_copydata(m, 0, sizeof(ep), &ep);
646 1.1.2.2 yamt m_adj(m, sizeof(ep));
647 1.1.2.2 yamt
648 1.1.2.2 yamt DPRINTFN(1, "bdaddr %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x "
649 1.1.2.2 yamt "class %2.2x%2.2x%2.2x type %s\n",
650 1.1.2.2 yamt ep.bdaddr.b[5], ep.bdaddr.b[4], ep.bdaddr.b[3],
651 1.1.2.2 yamt ep.bdaddr.b[2], ep.bdaddr.b[1], ep.bdaddr.b[0],
652 1.1.2.2 yamt ep.uclass[0], ep.uclass[1], ep.uclass[2],
653 1.1.2.2 yamt ep.link_type == HCI_LINK_ACL ? "ACL" : "SCO");
654 1.1.2.2 yamt
655 1.1.2.2 yamt if (ep.link_type == HCI_LINK_ACL)
656 1.1.2.2 yamt link = hci_acl_newconn(unit, &ep.bdaddr);
657 1.1.2.2 yamt else
658 1.1.2.2 yamt link = hci_sco_newconn(unit, &ep.bdaddr);
659 1.1.2.2 yamt
660 1.1.2.2 yamt if (link == NULL) {
661 1.1.2.2 yamt memset(&rp, 0, sizeof(rp));
662 1.1.2.2 yamt bdaddr_copy(&rp.bdaddr, &ep.bdaddr);
663 1.1.2.2 yamt rp.reason = 0x0f; /* Unacceptable BD_ADDR */
664 1.1.2.2 yamt
665 1.1.2.2 yamt hci_send_cmd(unit, HCI_CMD_REJECT_CON, &rp, sizeof(rp));
666 1.1.2.2 yamt } else {
667 1.1.2.2 yamt memset(&ap, 0, sizeof(ap));
668 1.1.2.2 yamt bdaddr_copy(&ap.bdaddr, &ep.bdaddr);
669 1.1.2.2 yamt if (unit->hci_link_policy & HCI_LINK_POLICY_ENABLE_ROLE_SWITCH)
670 1.1.2.2 yamt ap.role = HCI_ROLE_MASTER;
671 1.1.2.2 yamt else
672 1.1.2.2 yamt ap.role = HCI_ROLE_SLAVE;
673 1.1.2.2 yamt
674 1.1.2.2 yamt hci_send_cmd(unit, HCI_CMD_ACCEPT_CON, &ap, sizeof(ap));
675 1.1.2.2 yamt }
676 1.1.2.2 yamt }
677 1.1.2.2 yamt
678 1.1.2.2 yamt /*
679 1.1.2.4 yamt * Auth Complete
680 1.1.2.4 yamt *
681 1.1.2.4 yamt * Authentication has been completed on an ACL link. We can notify the
682 1.1.2.4 yamt * upper layer protocols unless further mode changes are pending.
683 1.1.2.4 yamt */
684 1.1.2.4 yamt static void
685 1.1.2.4 yamt hci_event_auth_compl(struct hci_unit *unit, struct mbuf *m)
686 1.1.2.4 yamt {
687 1.1.2.4 yamt hci_auth_compl_ep ep;
688 1.1.2.4 yamt struct hci_link *link;
689 1.1.2.4 yamt int err;
690 1.1.2.4 yamt
691 1.1.2.4 yamt KASSERT(m->m_pkthdr.len >= sizeof(ep));
692 1.1.2.4 yamt m_copydata(m, 0, sizeof(ep), &ep);
693 1.1.2.4 yamt m_adj(m, sizeof(ep));
694 1.1.2.4 yamt
695 1.1.2.4 yamt ep.con_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
696 1.1.2.4 yamt
697 1.1.2.4 yamt DPRINTFN(1, "handle #%d, status=0x%x\n", ep.con_handle, ep.status);
698 1.1.2.4 yamt
699 1.1.2.4 yamt link = hci_link_lookup_handle(unit, ep.con_handle);
700 1.1.2.4 yamt if (link == NULL || link->hl_type != HCI_LINK_ACL)
701 1.1.2.4 yamt return;
702 1.1.2.4 yamt
703 1.1.2.4 yamt if (ep.status == 0) {
704 1.1.2.4 yamt link->hl_flags |= HCI_LINK_AUTH;
705 1.1.2.4 yamt
706 1.1.2.4 yamt if (link->hl_state == HCI_LINK_WAIT_AUTH)
707 1.1.2.4 yamt link->hl_state = HCI_LINK_OPEN;
708 1.1.2.4 yamt
709 1.1.2.4 yamt err = hci_acl_setmode(link);
710 1.1.2.4 yamt if (err == EINPROGRESS)
711 1.1.2.4 yamt return;
712 1.1.2.4 yamt }
713 1.1.2.4 yamt
714 1.1.2.4 yamt hci_acl_linkmode(link);
715 1.1.2.4 yamt }
716 1.1.2.4 yamt
717 1.1.2.4 yamt /*
718 1.1.2.4 yamt * Encryption Change
719 1.1.2.4 yamt *
720 1.1.2.4 yamt * The encryption status has changed. Basically, we note the change
721 1.1.2.4 yamt * then notify the upper layer protocol unless further mode changes
722 1.1.2.4 yamt * are pending.
723 1.1.2.4 yamt * Note that if encryption gets disabled when it has been requested,
724 1.1.2.4 yamt * we will attempt to enable it again.. (its a feature not a bug :)
725 1.1.2.4 yamt */
726 1.1.2.4 yamt static void
727 1.1.2.4 yamt hci_event_encryption_change(struct hci_unit *unit, struct mbuf *m)
728 1.1.2.4 yamt {
729 1.1.2.4 yamt hci_encryption_change_ep ep;
730 1.1.2.4 yamt struct hci_link *link;
731 1.1.2.4 yamt int err;
732 1.1.2.4 yamt
733 1.1.2.4 yamt KASSERT(m->m_pkthdr.len >= sizeof(ep));
734 1.1.2.4 yamt m_copydata(m, 0, sizeof(ep), &ep);
735 1.1.2.4 yamt m_adj(m, sizeof(ep));
736 1.1.2.4 yamt
737 1.1.2.4 yamt ep.con_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
738 1.1.2.4 yamt
739 1.1.2.4 yamt DPRINTFN(1, "handle #%d, status=0x%x, encryption_enable=0x%x\n",
740 1.1.2.4 yamt ep.con_handle, ep.status, ep.encryption_enable);
741 1.1.2.4 yamt
742 1.1.2.4 yamt link = hci_link_lookup_handle(unit, ep.con_handle);
743 1.1.2.4 yamt if (link == NULL || link->hl_type != HCI_LINK_ACL)
744 1.1.2.4 yamt return;
745 1.1.2.4 yamt
746 1.1.2.4 yamt if (ep.status == 0) {
747 1.1.2.4 yamt if (ep.encryption_enable == 0)
748 1.1.2.4 yamt link->hl_flags &= ~HCI_LINK_ENCRYPT;
749 1.1.2.4 yamt else
750 1.1.2.4 yamt link->hl_flags |= (HCI_LINK_AUTH | HCI_LINK_ENCRYPT);
751 1.1.2.4 yamt
752 1.1.2.4 yamt if (link->hl_state == HCI_LINK_WAIT_ENCRYPT)
753 1.1.2.4 yamt link->hl_state = HCI_LINK_OPEN;
754 1.1.2.4 yamt
755 1.1.2.4 yamt err = hci_acl_setmode(link);
756 1.1.2.4 yamt if (err == EINPROGRESS)
757 1.1.2.4 yamt return;
758 1.1.2.4 yamt }
759 1.1.2.4 yamt
760 1.1.2.4 yamt hci_acl_linkmode(link);
761 1.1.2.4 yamt }
762 1.1.2.4 yamt
763 1.1.2.4 yamt /*
764 1.1.2.4 yamt * Change Connection Link Key Complete
765 1.1.2.4 yamt *
766 1.1.2.4 yamt * Link keys are handled in userland but if we are waiting to secure
767 1.1.2.4 yamt * this link, we should notify the upper protocols. A SECURE request
768 1.1.2.4 yamt * only needs a single key change, so we can cancel the request.
769 1.1.2.4 yamt */
770 1.1.2.4 yamt static void
771 1.1.2.4 yamt hci_event_change_con_link_key_compl(struct hci_unit *unit, struct mbuf *m)
772 1.1.2.4 yamt {
773 1.1.2.4 yamt hci_change_con_link_key_compl_ep ep;
774 1.1.2.4 yamt struct hci_link *link;
775 1.1.2.4 yamt int err;
776 1.1.2.4 yamt
777 1.1.2.4 yamt KASSERT(m->m_pkthdr.len >= sizeof(ep));
778 1.1.2.4 yamt m_copydata(m, 0, sizeof(ep), &ep);
779 1.1.2.4 yamt m_adj(m, sizeof(ep));
780 1.1.2.4 yamt
781 1.1.2.4 yamt ep.con_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
782 1.1.2.4 yamt
783 1.1.2.4 yamt DPRINTFN(1, "handle #%d, status=0x%x\n", ep.con_handle, ep.status);
784 1.1.2.4 yamt
785 1.1.2.4 yamt link = hci_link_lookup_handle(unit, ep.con_handle);
786 1.1.2.4 yamt if (link == NULL || link->hl_type != HCI_LINK_ACL)
787 1.1.2.4 yamt return;
788 1.1.2.4 yamt
789 1.1.2.4 yamt link->hl_flags &= ~HCI_LINK_SECURE_REQ;
790 1.1.2.4 yamt
791 1.1.2.4 yamt if (ep.status == 0) {
792 1.1.2.4 yamt link->hl_flags |= (HCI_LINK_AUTH | HCI_LINK_SECURE);
793 1.1.2.4 yamt
794 1.1.2.4 yamt if (link->hl_state == HCI_LINK_WAIT_SECURE)
795 1.1.2.4 yamt link->hl_state = HCI_LINK_OPEN;
796 1.1.2.4 yamt
797 1.1.2.4 yamt err = hci_acl_setmode(link);
798 1.1.2.4 yamt if (err == EINPROGRESS)
799 1.1.2.4 yamt return;
800 1.1.2.4 yamt }
801 1.1.2.4 yamt
802 1.1.2.4 yamt hci_acl_linkmode(link);
803 1.1.2.4 yamt }
804 1.1.2.4 yamt
805 1.1.2.4 yamt /*
806 1.1.2.5 yamt * Read Clock Offset Complete
807 1.1.2.5 yamt *
808 1.1.2.5 yamt * We keep a note of the clock offset of remote devices when a
809 1.1.2.5 yamt * link is made, in order to facilitate reconnections to the device
810 1.1.2.5 yamt */
811 1.1.2.5 yamt static void
812 1.1.2.5 yamt hci_event_read_clock_offset_compl(struct hci_unit *unit, struct mbuf *m)
813 1.1.2.5 yamt {
814 1.1.2.5 yamt hci_read_clock_offset_compl_ep ep;
815 1.1.2.5 yamt struct hci_link *link;
816 1.1.2.5 yamt
817 1.1.2.5 yamt KASSERT(m->m_pkthdr.len >= sizeof(ep));
818 1.1.2.5 yamt m_copydata(m, 0, sizeof(ep), &ep);
819 1.1.2.5 yamt m_adj(m, sizeof(ep));
820 1.1.2.5 yamt
821 1.1.2.5 yamt DPRINTFN(1, "handle #%d, offset=%u, status=0x%x\n",
822 1.1.2.5 yamt le16toh(ep.con_handle), le16toh(ep.clock_offset), ep.status);
823 1.1.2.5 yamt
824 1.1.2.5 yamt ep.con_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
825 1.1.2.5 yamt link = hci_link_lookup_handle(unit, ep.con_handle);
826 1.1.2.5 yamt
827 1.1.2.5 yamt if (ep.status != 0 || link == NULL)
828 1.1.2.5 yamt return;
829 1.1.2.5 yamt
830 1.1.2.5 yamt link->hl_clock = ep.clock_offset;
831 1.1.2.5 yamt }
832 1.1.2.5 yamt
833 1.1.2.5 yamt /*
834 1.1.2.2 yamt * process results of read_bdaddr command_complete event
835 1.1.2.2 yamt */
836 1.1.2.2 yamt static void
837 1.1.2.2 yamt hci_cmd_read_bdaddr(struct hci_unit *unit, struct mbuf *m)
838 1.1.2.2 yamt {
839 1.1.2.2 yamt hci_read_bdaddr_rp rp;
840 1.1.2.2 yamt
841 1.1.2.2 yamt KASSERT(m->m_pkthdr.len >= sizeof(rp));
842 1.1.2.2 yamt m_copydata(m, 0, sizeof(rp), &rp);
843 1.1.2.2 yamt m_adj(m, sizeof(rp));
844 1.1.2.2 yamt
845 1.1.2.2 yamt if (rp.status > 0)
846 1.1.2.2 yamt return;
847 1.1.2.2 yamt
848 1.1.2.2 yamt if ((unit->hci_flags & BTF_INIT_BDADDR) == 0)
849 1.1.2.2 yamt return;
850 1.1.2.2 yamt
851 1.1.2.2 yamt bdaddr_copy(&unit->hci_bdaddr, &rp.bdaddr);
852 1.1.2.2 yamt
853 1.1.2.2 yamt unit->hci_flags &= ~BTF_INIT_BDADDR;
854 1.1.2.2 yamt
855 1.1.2.2 yamt wakeup(unit);
856 1.1.2.2 yamt }
857 1.1.2.2 yamt
858 1.1.2.2 yamt /*
859 1.1.2.2 yamt * process results of read_buffer_size command_complete event
860 1.1.2.2 yamt */
861 1.1.2.2 yamt static void
862 1.1.2.2 yamt hci_cmd_read_buffer_size(struct hci_unit *unit, struct mbuf *m)
863 1.1.2.2 yamt {
864 1.1.2.2 yamt hci_read_buffer_size_rp rp;
865 1.1.2.2 yamt
866 1.1.2.2 yamt KASSERT(m->m_pkthdr.len >= sizeof(rp));
867 1.1.2.2 yamt m_copydata(m, 0, sizeof(rp), &rp);
868 1.1.2.2 yamt m_adj(m, sizeof(rp));
869 1.1.2.2 yamt
870 1.1.2.2 yamt if (rp.status > 0)
871 1.1.2.2 yamt return;
872 1.1.2.2 yamt
873 1.1.2.2 yamt if ((unit->hci_flags & BTF_INIT_BUFFER_SIZE) == 0)
874 1.1.2.2 yamt return;
875 1.1.2.2 yamt
876 1.1.2.2 yamt unit->hci_max_acl_size = le16toh(rp.max_acl_size);
877 1.1.2.2 yamt unit->hci_num_acl_pkts = le16toh(rp.num_acl_pkts);
878 1.1.2.2 yamt unit->hci_max_sco_size = rp.max_sco_size;
879 1.1.2.2 yamt unit->hci_num_sco_pkts = le16toh(rp.num_sco_pkts);
880 1.1.2.2 yamt
881 1.1.2.2 yamt unit->hci_flags &= ~BTF_INIT_BUFFER_SIZE;
882 1.1.2.2 yamt
883 1.1.2.2 yamt wakeup(unit);
884 1.1.2.2 yamt }
885 1.1.2.2 yamt
886 1.1.2.2 yamt /*
887 1.1.2.2 yamt * process results of read_local_features command_complete event
888 1.1.2.2 yamt */
889 1.1.2.2 yamt static void
890 1.1.2.2 yamt hci_cmd_read_local_features(struct hci_unit *unit, struct mbuf *m)
891 1.1.2.2 yamt {
892 1.1.2.2 yamt hci_read_local_features_rp rp;
893 1.1.2.2 yamt
894 1.1.2.2 yamt KASSERT(m->m_pkthdr.len >= sizeof(rp));
895 1.1.2.2 yamt m_copydata(m, 0, sizeof(rp), &rp);
896 1.1.2.2 yamt m_adj(m, sizeof(rp));
897 1.1.2.2 yamt
898 1.1.2.2 yamt if (rp.status > 0)
899 1.1.2.2 yamt return;
900 1.1.2.2 yamt
901 1.1.2.2 yamt if ((unit->hci_flags & BTF_INIT_FEATURES) == 0)
902 1.1.2.2 yamt return;
903 1.1.2.2 yamt
904 1.1.2.2 yamt unit->hci_lmp_mask = 0;
905 1.1.2.2 yamt
906 1.1.2.2 yamt if (rp.features[0] & HCI_LMP_ROLE_SWITCH)
907 1.1.2.2 yamt unit->hci_lmp_mask |= HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
908 1.1.2.2 yamt
909 1.1.2.2 yamt if (rp.features[0] & HCI_LMP_HOLD_MODE)
910 1.1.2.2 yamt unit->hci_lmp_mask |= HCI_LINK_POLICY_ENABLE_HOLD_MODE;
911 1.1.2.2 yamt
912 1.1.2.2 yamt if (rp.features[0] & HCI_LMP_SNIFF_MODE)
913 1.1.2.2 yamt unit->hci_lmp_mask |= HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
914 1.1.2.2 yamt
915 1.1.2.2 yamt if (rp.features[1] & HCI_LMP_PARK_MODE)
916 1.1.2.2 yamt unit->hci_lmp_mask |= HCI_LINK_POLICY_ENABLE_PARK_MODE;
917 1.1.2.2 yamt
918 1.1.2.2 yamt /* ACL packet mask */
919 1.1.2.2 yamt unit->hci_acl_mask = HCI_PKT_DM1 | HCI_PKT_DH1;
920 1.1.2.2 yamt
921 1.1.2.2 yamt if (rp.features[0] & HCI_LMP_3SLOT)
922 1.1.2.2 yamt unit->hci_acl_mask |= HCI_PKT_DM3 | HCI_PKT_DH3;
923 1.1.2.2 yamt
924 1.1.2.2 yamt if (rp.features[0] & HCI_LMP_5SLOT)
925 1.1.2.2 yamt unit->hci_acl_mask |= HCI_PKT_DM5 | HCI_PKT_DH5;
926 1.1.2.2 yamt
927 1.1.2.2 yamt if ((rp.features[3] & HCI_LMP_EDR_ACL_2MBPS) == 0)
928 1.1.2.2 yamt unit->hci_acl_mask |= HCI_PKT_2MBPS_DH1
929 1.1.2.2 yamt | HCI_PKT_2MBPS_DH3
930 1.1.2.2 yamt | HCI_PKT_2MBPS_DH5;
931 1.1.2.2 yamt
932 1.1.2.2 yamt if ((rp.features[3] & HCI_LMP_EDR_ACL_3MBPS) == 0)
933 1.1.2.2 yamt unit->hci_acl_mask |= HCI_PKT_3MBPS_DH1
934 1.1.2.2 yamt | HCI_PKT_3MBPS_DH3
935 1.1.2.2 yamt | HCI_PKT_3MBPS_DH5;
936 1.1.2.2 yamt
937 1.1.2.2 yamt if ((rp.features[4] & HCI_LMP_3SLOT_EDR_ACL) == 0)
938 1.1.2.2 yamt unit->hci_acl_mask |= HCI_PKT_2MBPS_DH3
939 1.1.2.2 yamt | HCI_PKT_3MBPS_DH3;
940 1.1.2.2 yamt
941 1.1.2.2 yamt if ((rp.features[5] & HCI_LMP_5SLOT_EDR_ACL) == 0)
942 1.1.2.2 yamt unit->hci_acl_mask |= HCI_PKT_2MBPS_DH5
943 1.1.2.2 yamt | HCI_PKT_3MBPS_DH5;
944 1.1.2.2 yamt
945 1.1.2.2 yamt unit->hci_packet_type = unit->hci_acl_mask;
946 1.1.2.2 yamt
947 1.1.2.2 yamt /* SCO packet mask */
948 1.1.2.2 yamt unit->hci_sco_mask = 0;
949 1.1.2.2 yamt if (rp.features[1] & HCI_LMP_SCO_LINK)
950 1.1.2.2 yamt unit->hci_sco_mask |= HCI_PKT_HV1;
951 1.1.2.2 yamt
952 1.1.2.2 yamt if (rp.features[1] & HCI_LMP_HV2_PKT)
953 1.1.2.2 yamt unit->hci_sco_mask |= HCI_PKT_HV2;
954 1.1.2.2 yamt
955 1.1.2.2 yamt if (rp.features[1] & HCI_LMP_HV3_PKT)
956 1.1.2.2 yamt unit->hci_sco_mask |= HCI_PKT_HV3;
957 1.1.2.2 yamt
958 1.1.2.2 yamt if (rp.features[3] & HCI_LMP_EV3_PKT)
959 1.1.2.2 yamt unit->hci_sco_mask |= HCI_PKT_EV3;
960 1.1.2.2 yamt
961 1.1.2.2 yamt if (rp.features[4] & HCI_LMP_EV4_PKT)
962 1.1.2.2 yamt unit->hci_sco_mask |= HCI_PKT_EV4;
963 1.1.2.2 yamt
964 1.1.2.2 yamt if (rp.features[4] & HCI_LMP_EV5_PKT)
965 1.1.2.2 yamt unit->hci_sco_mask |= HCI_PKT_EV5;
966 1.1.2.2 yamt
967 1.1.2.4 yamt /* XXX what do 2MBPS/3MBPS/3SLOT eSCO mean? */
968 1.1.2.2 yamt
969 1.1.2.2 yamt unit->hci_flags &= ~BTF_INIT_FEATURES;
970 1.1.2.2 yamt
971 1.1.2.2 yamt wakeup(unit);
972 1.1.2.2 yamt
973 1.1.2.2 yamt DPRINTFN(1, "%s: lmp_mask %4.4x, acl_mask %4.4x, sco_mask %4.4x\n",
974 1.1.2.6 yamt device_xname(unit->hci_dev), unit->hci_lmp_mask,
975 1.1.2.2 yamt unit->hci_acl_mask, unit->hci_sco_mask);
976 1.1.2.2 yamt }
977 1.1.2.2 yamt
978 1.1.2.2 yamt /*
979 1.1.2.8 yamt * process results of read_local_ver command_complete event
980 1.1.2.8 yamt *
981 1.1.2.8 yamt * reading local supported commands is only supported from 1.2 spec
982 1.1.2.8 yamt */
983 1.1.2.8 yamt static void
984 1.1.2.8 yamt hci_cmd_read_local_ver(struct hci_unit *unit, struct mbuf *m)
985 1.1.2.8 yamt {
986 1.1.2.8 yamt hci_read_local_ver_rp rp;
987 1.1.2.8 yamt
988 1.1.2.8 yamt KASSERT(m->m_pkthdr.len >= sizeof(rp));
989 1.1.2.8 yamt m_copydata(m, 0, sizeof(rp), &rp);
990 1.1.2.8 yamt m_adj(m, sizeof(rp));
991 1.1.2.8 yamt
992 1.1.2.8 yamt if (rp.status != 0)
993 1.1.2.8 yamt return;
994 1.1.2.8 yamt
995 1.1.2.8 yamt if ((unit->hci_flags & BTF_INIT_COMMANDS) == 0)
996 1.1.2.8 yamt return;
997 1.1.2.8 yamt
998 1.1.2.8 yamt if (rp.hci_version < HCI_SPEC_V12) {
999 1.1.2.8 yamt unit->hci_flags &= ~BTF_INIT_COMMANDS;
1000 1.1.2.8 yamt wakeup(unit);
1001 1.1.2.8 yamt return;
1002 1.1.2.8 yamt }
1003 1.1.2.8 yamt
1004 1.1.2.8 yamt hci_send_cmd(unit, HCI_CMD_READ_LOCAL_COMMANDS, NULL, 0);
1005 1.1.2.8 yamt }
1006 1.1.2.8 yamt
1007 1.1.2.8 yamt /*
1008 1.1.2.8 yamt * process results of read_local_commands command_complete event
1009 1.1.2.8 yamt */
1010 1.1.2.8 yamt static void
1011 1.1.2.8 yamt hci_cmd_read_local_commands(struct hci_unit *unit, struct mbuf *m)
1012 1.1.2.8 yamt {
1013 1.1.2.8 yamt hci_read_local_commands_rp rp;
1014 1.1.2.8 yamt
1015 1.1.2.8 yamt KASSERT(m->m_pkthdr.len >= sizeof(rp));
1016 1.1.2.8 yamt m_copydata(m, 0, sizeof(rp), &rp);
1017 1.1.2.8 yamt m_adj(m, sizeof(rp));
1018 1.1.2.8 yamt
1019 1.1.2.8 yamt if (rp.status != 0)
1020 1.1.2.8 yamt return;
1021 1.1.2.8 yamt
1022 1.1.2.8 yamt if ((unit->hci_flags & BTF_INIT_COMMANDS) == 0)
1023 1.1.2.8 yamt return;
1024 1.1.2.8 yamt
1025 1.1.2.8 yamt unit->hci_flags &= ~BTF_INIT_COMMANDS;
1026 1.1.2.8 yamt memcpy(unit->hci_cmds, rp.commands, HCI_COMMANDS_SIZE);
1027 1.1.2.8 yamt
1028 1.1.2.8 yamt wakeup(unit);
1029 1.1.2.8 yamt }
1030 1.1.2.8 yamt
1031 1.1.2.8 yamt /*
1032 1.1.2.2 yamt * process results of reset command_complete event
1033 1.1.2.2 yamt *
1034 1.1.2.2 yamt * This has killed all the connections, so close down anything we have left,
1035 1.1.2.2 yamt * and reinitialise the unit.
1036 1.1.2.2 yamt */
1037 1.1.2.2 yamt static void
1038 1.1.2.2 yamt hci_cmd_reset(struct hci_unit *unit, struct mbuf *m)
1039 1.1.2.2 yamt {
1040 1.1.2.2 yamt hci_reset_rp rp;
1041 1.1.2.2 yamt struct hci_link *link, *next;
1042 1.1.2.2 yamt int acl;
1043 1.1.2.2 yamt
1044 1.1.2.2 yamt KASSERT(m->m_pkthdr.len >= sizeof(rp));
1045 1.1.2.2 yamt m_copydata(m, 0, sizeof(rp), &rp);
1046 1.1.2.2 yamt m_adj(m, sizeof(rp));
1047 1.1.2.2 yamt
1048 1.1.2.2 yamt if (rp.status != 0)
1049 1.1.2.2 yamt return;
1050 1.1.2.2 yamt
1051 1.1.2.2 yamt /*
1052 1.1.2.2 yamt * release SCO links first, since they may be holding
1053 1.1.2.2 yamt * an ACL link reference.
1054 1.1.2.2 yamt */
1055 1.1.2.2 yamt for (acl = 0 ; acl < 2 ; acl++) {
1056 1.1.2.2 yamt next = TAILQ_FIRST(&unit->hci_links);
1057 1.1.2.2 yamt while ((link = next) != NULL) {
1058 1.1.2.2 yamt next = TAILQ_NEXT(link, hl_next);
1059 1.1.2.2 yamt if (acl || link->hl_type != HCI_LINK_ACL)
1060 1.1.2.2 yamt hci_link_free(link, ECONNABORTED);
1061 1.1.2.2 yamt }
1062 1.1.2.2 yamt }
1063 1.1.2.2 yamt
1064 1.1.2.2 yamt unit->hci_num_acl_pkts = 0;
1065 1.1.2.2 yamt unit->hci_num_sco_pkts = 0;
1066 1.1.2.2 yamt
1067 1.1.2.2 yamt if (hci_send_cmd(unit, HCI_CMD_READ_BDADDR, NULL, 0))
1068 1.1.2.2 yamt return;
1069 1.1.2.2 yamt
1070 1.1.2.2 yamt if (hci_send_cmd(unit, HCI_CMD_READ_BUFFER_SIZE, NULL, 0))
1071 1.1.2.2 yamt return;
1072 1.1.2.2 yamt
1073 1.1.2.2 yamt if (hci_send_cmd(unit, HCI_CMD_READ_LOCAL_FEATURES, NULL, 0))
1074 1.1.2.2 yamt return;
1075 1.1.2.8 yamt
1076 1.1.2.8 yamt if (hci_send_cmd(unit, HCI_CMD_READ_LOCAL_VER, NULL, 0))
1077 1.1.2.8 yamt return;
1078 1.1.2.2 yamt }
1079