bthidev.c revision 1.1.2.3 1 1.1.2.3 yamt /* $NetBSD: bthidev.c,v 1.1.2.3 2006/12/30 20:47:57 yamt Exp $ */
2 1.1.2.2 yamt
3 1.1.2.2 yamt /*-
4 1.1.2.2 yamt * Copyright (c) 2006 Itronix Inc.
5 1.1.2.2 yamt * All rights reserved.
6 1.1.2.2 yamt *
7 1.1.2.2 yamt * Written by Iain Hibbert for Itronix Inc.
8 1.1.2.2 yamt *
9 1.1.2.2 yamt * Redistribution and use in source and binary forms, with or without
10 1.1.2.2 yamt * modification, are permitted provided that the following conditions
11 1.1.2.2 yamt * are met:
12 1.1.2.2 yamt * 1. Redistributions of source code must retain the above copyright
13 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer.
14 1.1.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
15 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer in the
16 1.1.2.2 yamt * documentation and/or other materials provided with the distribution.
17 1.1.2.2 yamt * 3. The name of Itronix Inc. may not be used to endorse
18 1.1.2.2 yamt * or promote products derived from this software without specific
19 1.1.2.2 yamt * prior written permission.
20 1.1.2.2 yamt *
21 1.1.2.2 yamt * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 1.1.2.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 1.1.2.2 yamt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 1.1.2.2 yamt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1.2.2 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 1.1.2.2 yamt * ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
32 1.1.2.2 yamt */
33 1.1.2.2 yamt
34 1.1.2.2 yamt #include <sys/cdefs.h>
35 1.1.2.3 yamt __KERNEL_RCSID(0, "$NetBSD: bthidev.c,v 1.1.2.3 2006/12/30 20:47:57 yamt Exp $");
36 1.1.2.2 yamt
37 1.1.2.2 yamt #include <sys/param.h>
38 1.1.2.2 yamt #include <sys/conf.h>
39 1.1.2.2 yamt #include <sys/device.h>
40 1.1.2.2 yamt #include <sys/fcntl.h>
41 1.1.2.2 yamt #include <sys/kernel.h>
42 1.1.2.2 yamt #include <sys/queue.h>
43 1.1.2.2 yamt #include <sys/malloc.h>
44 1.1.2.2 yamt #include <sys/mbuf.h>
45 1.1.2.2 yamt #include <sys/proc.h>
46 1.1.2.2 yamt #include <sys/systm.h>
47 1.1.2.2 yamt
48 1.1.2.3 yamt #include <prop/proplib.h>
49 1.1.2.3 yamt
50 1.1.2.2 yamt #include <netbt/bluetooth.h>
51 1.1.2.2 yamt #include <netbt/l2cap.h>
52 1.1.2.2 yamt
53 1.1.2.2 yamt #include <dev/usb/hid.h>
54 1.1.2.2 yamt #include <dev/bluetooth/btdev.h>
55 1.1.2.2 yamt #include <dev/bluetooth/bthid.h>
56 1.1.2.2 yamt #include <dev/bluetooth/bthidev.h>
57 1.1.2.2 yamt
58 1.1.2.2 yamt #include "locators.h"
59 1.1.2.2 yamt
60 1.1.2.2 yamt /*****************************************************************************
61 1.1.2.2 yamt *
62 1.1.2.2 yamt * Bluetooth HID device
63 1.1.2.2 yamt */
64 1.1.2.2 yamt
65 1.1.2.2 yamt #define MAX_DESCRIPTOR_LEN 1024 /* sanity check */
66 1.1.2.2 yamt
67 1.1.2.2 yamt /* bthidev softc */
68 1.1.2.2 yamt struct bthidev_softc {
69 1.1.2.3 yamt struct btdev sc_btdev;
70 1.1.2.2 yamt uint16_t sc_state;
71 1.1.2.2 yamt uint16_t sc_flags;
72 1.1.2.2 yamt
73 1.1.2.2 yamt bdaddr_t sc_laddr; /* local address */
74 1.1.2.2 yamt bdaddr_t sc_raddr; /* remote address */
75 1.1.2.2 yamt
76 1.1.2.2 yamt uint16_t sc_ctlpsm; /* control PSM */
77 1.1.2.2 yamt struct l2cap_channel *sc_ctl; /* control channel */
78 1.1.2.2 yamt struct l2cap_channel *sc_ctl_l; /* control listen */
79 1.1.2.2 yamt
80 1.1.2.2 yamt uint16_t sc_intpsm; /* interrupt PSM */
81 1.1.2.2 yamt struct l2cap_channel *sc_int; /* interrupt channel */
82 1.1.2.2 yamt struct l2cap_channel *sc_int_l; /* interrupt listen */
83 1.1.2.2 yamt
84 1.1.2.2 yamt LIST_HEAD(,bthidev) sc_list; /* child list */
85 1.1.2.2 yamt
86 1.1.2.2 yamt struct callout sc_reconnect;
87 1.1.2.2 yamt int sc_attempts; /* connection attempts */
88 1.1.2.2 yamt };
89 1.1.2.2 yamt
90 1.1.2.3 yamt /* sc_flags */
91 1.1.2.3 yamt #define BTHID_RECONNECT (1 << 0) /* reconnect on link loss */
92 1.1.2.3 yamt #define BTHID_CONNECTING (1 << 1) /* we are connecting */
93 1.1.2.3 yamt
94 1.1.2.2 yamt /* device state */
95 1.1.2.2 yamt #define BTHID_CLOSED 0
96 1.1.2.2 yamt #define BTHID_WAIT_CTL 1
97 1.1.2.2 yamt #define BTHID_WAIT_INT 2
98 1.1.2.2 yamt #define BTHID_OPEN 3
99 1.1.2.2 yamt #define BTHID_DETACHING 4
100 1.1.2.2 yamt
101 1.1.2.2 yamt #define BTHID_RETRY_INTERVAL 5 /* seconds between connection attempts */
102 1.1.2.2 yamt
103 1.1.2.2 yamt /* bthidev internals */
104 1.1.2.2 yamt static void bthidev_timeout(void *);
105 1.1.2.2 yamt static int bthidev_listen(struct bthidev_softc *);
106 1.1.2.2 yamt static int bthidev_connect(struct bthidev_softc *);
107 1.1.2.2 yamt static int bthidev_output(struct bthidev *, uint8_t *, int);
108 1.1.2.2 yamt static void bthidev_null(struct bthidev *, uint8_t *, int);
109 1.1.2.2 yamt
110 1.1.2.2 yamt /* autoconf(9) glue */
111 1.1.2.2 yamt static int bthidev_match(struct device *, struct cfdata *, void *);
112 1.1.2.2 yamt static void bthidev_attach(struct device *, struct device *, void *);
113 1.1.2.2 yamt static int bthidev_detach(struct device *, int);
114 1.1.2.2 yamt static int bthidev_print(void *, const char *);
115 1.1.2.2 yamt
116 1.1.2.2 yamt CFATTACH_DECL(bthidev, sizeof(struct bthidev_softc),
117 1.1.2.2 yamt bthidev_match, bthidev_attach, bthidev_detach, NULL);
118 1.1.2.2 yamt
119 1.1.2.2 yamt /* bluetooth(9) protocol methods for L2CAP */
120 1.1.2.2 yamt static void bthidev_connecting(void *);
121 1.1.2.2 yamt static void bthidev_ctl_connected(void *);
122 1.1.2.2 yamt static void bthidev_int_connected(void *);
123 1.1.2.2 yamt static void bthidev_ctl_disconnected(void *, int);
124 1.1.2.2 yamt static void bthidev_int_disconnected(void *, int);
125 1.1.2.2 yamt static void *bthidev_ctl_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
126 1.1.2.2 yamt static void *bthidev_int_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
127 1.1.2.2 yamt static void bthidev_complete(void *, int);
128 1.1.2.2 yamt static void bthidev_input(void *, struct mbuf *);
129 1.1.2.2 yamt
130 1.1.2.2 yamt static const struct btproto bthidev_ctl_proto = {
131 1.1.2.2 yamt bthidev_connecting,
132 1.1.2.2 yamt bthidev_ctl_connected,
133 1.1.2.2 yamt bthidev_ctl_disconnected,
134 1.1.2.2 yamt bthidev_ctl_newconn,
135 1.1.2.2 yamt bthidev_complete,
136 1.1.2.2 yamt bthidev_input,
137 1.1.2.2 yamt };
138 1.1.2.2 yamt
139 1.1.2.2 yamt static const struct btproto bthidev_int_proto = {
140 1.1.2.2 yamt bthidev_connecting,
141 1.1.2.2 yamt bthidev_int_connected,
142 1.1.2.2 yamt bthidev_int_disconnected,
143 1.1.2.2 yamt bthidev_int_newconn,
144 1.1.2.2 yamt bthidev_complete,
145 1.1.2.2 yamt bthidev_input,
146 1.1.2.2 yamt };
147 1.1.2.2 yamt
148 1.1.2.2 yamt /*****************************************************************************
149 1.1.2.2 yamt *
150 1.1.2.2 yamt * bthidev autoconf(9) routines
151 1.1.2.2 yamt */
152 1.1.2.2 yamt
153 1.1.2.2 yamt static int
154 1.1.2.3 yamt bthidev_match(struct device *self, struct cfdata *cfdata,
155 1.1.2.3 yamt void *aux)
156 1.1.2.2 yamt {
157 1.1.2.3 yamt prop_dictionary_t dict = aux;
158 1.1.2.3 yamt prop_object_t obj;
159 1.1.2.2 yamt
160 1.1.2.3 yamt obj = prop_dictionary_get(dict, BTDEVservice);
161 1.1.2.3 yamt if (prop_string_equals_cstring(obj, "HID"))
162 1.1.2.3 yamt return 1;
163 1.1.2.2 yamt
164 1.1.2.3 yamt return 0;
165 1.1.2.2 yamt }
166 1.1.2.2 yamt
167 1.1.2.2 yamt static void
168 1.1.2.2 yamt bthidev_attach(struct device *parent, struct device *self, void *aux)
169 1.1.2.2 yamt {
170 1.1.2.2 yamt struct bthidev_softc *sc = (struct bthidev_softc *)self;
171 1.1.2.3 yamt prop_dictionary_t dict = aux;
172 1.1.2.3 yamt prop_object_t obj;
173 1.1.2.2 yamt struct bthidev_attach_args bha;
174 1.1.2.2 yamt struct bthidev *dev;
175 1.1.2.2 yamt struct hid_data *d;
176 1.1.2.2 yamt struct hid_item h;
177 1.1.2.3 yamt const void *desc;
178 1.1.2.2 yamt int locs[BTHIDBUSCF_NLOCS];
179 1.1.2.3 yamt int maxid, rep, s, dlen;
180 1.1.2.2 yamt
181 1.1.2.2 yamt /*
182 1.1.2.2 yamt * Init softc
183 1.1.2.2 yamt */
184 1.1.2.2 yamt LIST_INIT(&sc->sc_list);
185 1.1.2.2 yamt callout_init(&sc->sc_reconnect);
186 1.1.2.2 yamt callout_setfunc(&sc->sc_reconnect, bthidev_timeout, sc);
187 1.1.2.2 yamt sc->sc_state = BTHID_CLOSED;
188 1.1.2.3 yamt sc->sc_flags = BTHID_CONNECTING;
189 1.1.2.3 yamt sc->sc_ctlpsm = L2CAP_PSM_HID_CNTL;
190 1.1.2.3 yamt sc->sc_intpsm = L2CAP_PSM_HID_INTR;
191 1.1.2.2 yamt
192 1.1.2.2 yamt /*
193 1.1.2.3 yamt * extract config from proplist
194 1.1.2.2 yamt */
195 1.1.2.3 yamt obj = prop_dictionary_get(dict, BTDEVladdr);
196 1.1.2.3 yamt bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));
197 1.1.2.2 yamt
198 1.1.2.3 yamt obj = prop_dictionary_get(dict, BTDEVraddr);
199 1.1.2.3 yamt bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));
200 1.1.2.2 yamt
201 1.1.2.3 yamt obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
202 1.1.2.3 yamt if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
203 1.1.2.3 yamt sc->sc_ctlpsm = prop_number_integer_value(obj);
204 1.1.2.3 yamt if (L2CAP_PSM_INVALID(sc->sc_ctlpsm)) {
205 1.1.2.3 yamt aprint_error(" invalid %s\n", BTHIDEVcontrolpsm);
206 1.1.2.3 yamt return;
207 1.1.2.3 yamt }
208 1.1.2.3 yamt }
209 1.1.2.3 yamt
210 1.1.2.3 yamt obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
211 1.1.2.3 yamt if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
212 1.1.2.3 yamt sc->sc_intpsm = prop_number_integer_value(obj);
213 1.1.2.3 yamt if (L2CAP_PSM_INVALID(sc->sc_intpsm)) {
214 1.1.2.3 yamt aprint_error(" invalid %s\n", BTHIDEVinterruptpsm);
215 1.1.2.3 yamt return;
216 1.1.2.3 yamt }
217 1.1.2.3 yamt }
218 1.1.2.3 yamt
219 1.1.2.3 yamt obj = prop_dictionary_get(dict, BTHIDEVdescriptor);
220 1.1.2.3 yamt if (prop_object_type(obj) == PROP_TYPE_DATA) {
221 1.1.2.3 yamt dlen = prop_data_size(obj);
222 1.1.2.3 yamt desc = prop_data_data_nocopy(obj);
223 1.1.2.3 yamt } else {
224 1.1.2.3 yamt aprint_error(" no %s\n", BTHIDEVdescriptor);
225 1.1.2.2 yamt return;
226 1.1.2.2 yamt }
227 1.1.2.3 yamt
228 1.1.2.3 yamt obj = prop_dictionary_get(dict, BTHIDEVreconnect);
229 1.1.2.3 yamt if (prop_object_type(obj) == PROP_TYPE_BOOL
230 1.1.2.3 yamt && !prop_bool_true(obj))
231 1.1.2.3 yamt sc->sc_flags |= BTHID_RECONNECT;
232 1.1.2.2 yamt
233 1.1.2.2 yamt /*
234 1.1.2.2 yamt * Parse the descriptor and attach child devices, one per report.
235 1.1.2.2 yamt */
236 1.1.2.2 yamt maxid = -1;
237 1.1.2.2 yamt h.report_ID = 0;
238 1.1.2.3 yamt d = hid_start_parse(desc, dlen, hid_none);
239 1.1.2.2 yamt while (hid_get_item(d, &h)) {
240 1.1.2.2 yamt if (h.report_ID > maxid)
241 1.1.2.2 yamt maxid = h.report_ID;
242 1.1.2.2 yamt }
243 1.1.2.2 yamt hid_end_parse(d);
244 1.1.2.2 yamt
245 1.1.2.2 yamt if (maxid < 0) {
246 1.1.2.2 yamt aprint_error(" no reports found\n");
247 1.1.2.2 yamt return;
248 1.1.2.2 yamt }
249 1.1.2.2 yamt
250 1.1.2.2 yamt aprint_normal("\n");
251 1.1.2.2 yamt
252 1.1.2.2 yamt for (rep = 0 ; rep <= maxid ; rep++) {
253 1.1.2.3 yamt if (hid_report_size(desc, dlen, hid_feature, rep) == 0
254 1.1.2.3 yamt && hid_report_size(desc, dlen, hid_input, rep) == 0
255 1.1.2.3 yamt && hid_report_size(desc, dlen, hid_output, rep) == 0)
256 1.1.2.2 yamt continue;
257 1.1.2.2 yamt
258 1.1.2.3 yamt bha.ba_desc = desc;
259 1.1.2.3 yamt bha.ba_dlen = dlen;
260 1.1.2.2 yamt bha.ba_input = bthidev_null;
261 1.1.2.2 yamt bha.ba_feature = bthidev_null;
262 1.1.2.2 yamt bha.ba_output = bthidev_output;
263 1.1.2.2 yamt bha.ba_id = rep;
264 1.1.2.2 yamt
265 1.1.2.2 yamt locs[BTHIDBUSCF_REPORTID] = rep;
266 1.1.2.2 yamt
267 1.1.2.2 yamt dev = (struct bthidev *)config_found_sm_loc((struct device *)sc, "bthidbus",
268 1.1.2.2 yamt locs, &bha, bthidev_print, config_stdsubmatch);
269 1.1.2.2 yamt if (dev != NULL) {
270 1.1.2.3 yamt dev->sc_parent = (struct device *)sc;
271 1.1.2.2 yamt dev->sc_id = rep;
272 1.1.2.2 yamt dev->sc_input = bha.ba_input;
273 1.1.2.2 yamt dev->sc_feature = bha.ba_feature;
274 1.1.2.2 yamt LIST_INSERT_HEAD(&sc->sc_list, dev, sc_next);
275 1.1.2.2 yamt }
276 1.1.2.2 yamt }
277 1.1.2.2 yamt
278 1.1.2.2 yamt /*
279 1.1.2.2 yamt * start bluetooth connections
280 1.1.2.2 yamt */
281 1.1.2.2 yamt s = splsoftnet();
282 1.1.2.3 yamt if ((sc->sc_flags & BTHID_RECONNECT) == 0)
283 1.1.2.2 yamt bthidev_listen(sc);
284 1.1.2.2 yamt
285 1.1.2.3 yamt if (sc->sc_flags & BTHID_CONNECTING)
286 1.1.2.2 yamt bthidev_connect(sc);
287 1.1.2.2 yamt splx(s);
288 1.1.2.2 yamt }
289 1.1.2.2 yamt
290 1.1.2.2 yamt static int
291 1.1.2.2 yamt bthidev_detach(struct device *self, int flags)
292 1.1.2.2 yamt {
293 1.1.2.2 yamt struct bthidev_softc *sc = (struct bthidev_softc *)self;
294 1.1.2.2 yamt struct bthidev *dev;
295 1.1.2.2 yamt int s;
296 1.1.2.2 yamt
297 1.1.2.2 yamt s = splsoftnet();
298 1.1.2.2 yamt sc->sc_flags = 0; /* disable reconnecting */
299 1.1.2.2 yamt
300 1.1.2.2 yamt /* release interrupt listen */
301 1.1.2.2 yamt if (sc->sc_int_l != NULL) {
302 1.1.2.2 yamt l2cap_detach(&sc->sc_int_l);
303 1.1.2.2 yamt sc->sc_int_l = NULL;
304 1.1.2.2 yamt }
305 1.1.2.2 yamt
306 1.1.2.2 yamt /* release control listen */
307 1.1.2.2 yamt if (sc->sc_ctl_l != NULL) {
308 1.1.2.2 yamt l2cap_detach(&sc->sc_ctl_l);
309 1.1.2.2 yamt sc->sc_ctl_l = NULL;
310 1.1.2.2 yamt }
311 1.1.2.2 yamt
312 1.1.2.2 yamt /* close interrupt channel */
313 1.1.2.2 yamt if (sc->sc_int != NULL) {
314 1.1.2.2 yamt l2cap_disconnect(sc->sc_int, 0);
315 1.1.2.2 yamt l2cap_detach(&sc->sc_int);
316 1.1.2.2 yamt sc->sc_int = NULL;
317 1.1.2.2 yamt }
318 1.1.2.2 yamt
319 1.1.2.2 yamt /* close control channel */
320 1.1.2.2 yamt if (sc->sc_ctl != NULL) {
321 1.1.2.2 yamt l2cap_disconnect(sc->sc_ctl, 0);
322 1.1.2.2 yamt l2cap_detach(&sc->sc_ctl);
323 1.1.2.2 yamt sc->sc_ctl = NULL;
324 1.1.2.2 yamt }
325 1.1.2.2 yamt
326 1.1.2.2 yamt /* remove callout */
327 1.1.2.2 yamt sc->sc_state = BTHID_DETACHING;
328 1.1.2.2 yamt callout_stop(&sc->sc_reconnect);
329 1.1.2.2 yamt if (callout_invoking(&sc->sc_reconnect))
330 1.1.2.2 yamt tsleep(sc, PWAIT, "bthidetach", 0);
331 1.1.2.2 yamt
332 1.1.2.2 yamt splx(s);
333 1.1.2.2 yamt
334 1.1.2.2 yamt /* detach children */
335 1.1.2.2 yamt while ((dev = LIST_FIRST(&sc->sc_list)) != NULL) {
336 1.1.2.2 yamt LIST_REMOVE(dev, sc_next);
337 1.1.2.3 yamt config_detach((struct device *)dev, flags);
338 1.1.2.2 yamt }
339 1.1.2.2 yamt
340 1.1.2.2 yamt return 0;
341 1.1.2.2 yamt }
342 1.1.2.2 yamt
343 1.1.2.2 yamt /*
344 1.1.2.2 yamt * bthidev config print
345 1.1.2.2 yamt */
346 1.1.2.2 yamt static int
347 1.1.2.2 yamt bthidev_print(void *aux, const char *pnp)
348 1.1.2.2 yamt {
349 1.1.2.2 yamt struct bthidev_attach_args *ba = aux;
350 1.1.2.2 yamt
351 1.1.2.2 yamt if (pnp != NULL)
352 1.1.2.2 yamt aprint_normal("%s:", pnp);
353 1.1.2.2 yamt
354 1.1.2.2 yamt if (ba->ba_id > 0)
355 1.1.2.2 yamt aprint_normal(" reportid %d", ba->ba_id);
356 1.1.2.2 yamt
357 1.1.2.2 yamt return UNCONF;
358 1.1.2.2 yamt }
359 1.1.2.2 yamt
360 1.1.2.2 yamt /*****************************************************************************
361 1.1.2.2 yamt *
362 1.1.2.2 yamt * bluetooth(4) HID attach/detach routines
363 1.1.2.2 yamt */
364 1.1.2.2 yamt
365 1.1.2.2 yamt /*
366 1.1.2.3 yamt * callouts are scheduled after connections have been lost, in order
367 1.1.2.3 yamt * to clean up and reconnect.
368 1.1.2.2 yamt */
369 1.1.2.2 yamt static void
370 1.1.2.2 yamt bthidev_timeout(void *arg)
371 1.1.2.2 yamt {
372 1.1.2.2 yamt struct bthidev_softc *sc = arg;
373 1.1.2.3 yamt int s;
374 1.1.2.2 yamt
375 1.1.2.2 yamt s = splsoftnet();
376 1.1.2.2 yamt callout_ack(&sc->sc_reconnect);
377 1.1.2.2 yamt
378 1.1.2.2 yamt switch (sc->sc_state) {
379 1.1.2.2 yamt case BTHID_CLOSED:
380 1.1.2.3 yamt if (sc->sc_int != NULL) {
381 1.1.2.3 yamt l2cap_disconnect(sc->sc_int, 0);
382 1.1.2.3 yamt break;
383 1.1.2.3 yamt }
384 1.1.2.3 yamt
385 1.1.2.3 yamt if (sc->sc_ctl != NULL) {
386 1.1.2.3 yamt l2cap_disconnect(sc->sc_ctl, 0);
387 1.1.2.3 yamt break;
388 1.1.2.3 yamt }
389 1.1.2.3 yamt
390 1.1.2.3 yamt if (sc->sc_flags & BTHID_RECONNECT) {
391 1.1.2.3 yamt sc->sc_flags |= BTHID_CONNECTING;
392 1.1.2.3 yamt bthidev_connect(sc);
393 1.1.2.3 yamt break;
394 1.1.2.3 yamt }
395 1.1.2.3 yamt
396 1.1.2.2 yamt break;
397 1.1.2.2 yamt
398 1.1.2.2 yamt case BTHID_WAIT_CTL:
399 1.1.2.2 yamt break;
400 1.1.2.2 yamt
401 1.1.2.2 yamt case BTHID_WAIT_INT:
402 1.1.2.2 yamt break;
403 1.1.2.2 yamt
404 1.1.2.2 yamt case BTHID_OPEN:
405 1.1.2.2 yamt break;
406 1.1.2.2 yamt
407 1.1.2.2 yamt case BTHID_DETACHING:
408 1.1.2.2 yamt wakeup(sc);
409 1.1.2.2 yamt break;
410 1.1.2.2 yamt
411 1.1.2.2 yamt default:
412 1.1.2.2 yamt break;
413 1.1.2.2 yamt }
414 1.1.2.2 yamt splx(s);
415 1.1.2.2 yamt }
416 1.1.2.2 yamt
417 1.1.2.2 yamt /*
418 1.1.2.2 yamt * listen for our device
419 1.1.2.2 yamt */
420 1.1.2.2 yamt static int
421 1.1.2.2 yamt bthidev_listen(struct bthidev_softc *sc)
422 1.1.2.2 yamt {
423 1.1.2.2 yamt struct sockaddr_bt sa;
424 1.1.2.2 yamt int err;
425 1.1.2.2 yamt
426 1.1.2.2 yamt memset(&sa, 0, sizeof(sa));
427 1.1.2.2 yamt sa.bt_len = sizeof(sa);
428 1.1.2.2 yamt sa.bt_family = AF_BLUETOOTH;
429 1.1.2.2 yamt bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
430 1.1.2.2 yamt
431 1.1.2.2 yamt /*
432 1.1.2.2 yamt * Listen on control PSM
433 1.1.2.2 yamt */
434 1.1.2.2 yamt err = l2cap_attach(&sc->sc_ctl_l, &bthidev_ctl_proto, sc);
435 1.1.2.2 yamt if (err)
436 1.1.2.2 yamt return err;
437 1.1.2.2 yamt
438 1.1.2.2 yamt sa.bt_psm = sc->sc_ctlpsm;
439 1.1.2.2 yamt err = l2cap_bind(sc->sc_ctl_l, &sa);
440 1.1.2.2 yamt if (err)
441 1.1.2.2 yamt return err;
442 1.1.2.2 yamt
443 1.1.2.2 yamt err = l2cap_listen(sc->sc_ctl_l);
444 1.1.2.2 yamt if (err)
445 1.1.2.2 yamt return err;
446 1.1.2.2 yamt
447 1.1.2.2 yamt /*
448 1.1.2.2 yamt * Listen on interrupt PSM
449 1.1.2.2 yamt */
450 1.1.2.2 yamt err = l2cap_attach(&sc->sc_int_l, &bthidev_int_proto, sc);
451 1.1.2.2 yamt if (err)
452 1.1.2.2 yamt return err;
453 1.1.2.2 yamt
454 1.1.2.2 yamt sa.bt_psm = sc->sc_intpsm;
455 1.1.2.2 yamt err = l2cap_bind(sc->sc_int_l, &sa);
456 1.1.2.2 yamt if (err)
457 1.1.2.2 yamt return err;
458 1.1.2.2 yamt
459 1.1.2.2 yamt err = l2cap_listen(sc->sc_int_l);
460 1.1.2.2 yamt if (err)
461 1.1.2.2 yamt return err;
462 1.1.2.2 yamt
463 1.1.2.2 yamt sc->sc_state = BTHID_WAIT_CTL;
464 1.1.2.2 yamt return 0;
465 1.1.2.2 yamt }
466 1.1.2.2 yamt
467 1.1.2.2 yamt /*
468 1.1.2.2 yamt * start connecting to our device
469 1.1.2.2 yamt */
470 1.1.2.2 yamt static int
471 1.1.2.2 yamt bthidev_connect(struct bthidev_softc *sc)
472 1.1.2.2 yamt {
473 1.1.2.2 yamt struct sockaddr_bt sa;
474 1.1.2.2 yamt int err;
475 1.1.2.2 yamt
476 1.1.2.2 yamt if (sc->sc_attempts++ > 0)
477 1.1.2.3 yamt printf("%s: connect (#%d)\n",
478 1.1.2.3 yamt device_xname((struct device *)sc), sc->sc_attempts);
479 1.1.2.2 yamt
480 1.1.2.2 yamt memset(&sa, 0, sizeof(sa));
481 1.1.2.2 yamt sa.bt_len = sizeof(sa);
482 1.1.2.2 yamt sa.bt_family = AF_BLUETOOTH;
483 1.1.2.2 yamt
484 1.1.2.2 yamt err = l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
485 1.1.2.2 yamt if (err) {
486 1.1.2.3 yamt printf("%s: l2cap_attach failed (%d)\n",
487 1.1.2.3 yamt device_xname((struct device *)sc), err);
488 1.1.2.2 yamt return err;
489 1.1.2.2 yamt }
490 1.1.2.2 yamt
491 1.1.2.2 yamt bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
492 1.1.2.2 yamt err = l2cap_bind(sc->sc_ctl, &sa);
493 1.1.2.2 yamt if (err) {
494 1.1.2.3 yamt printf("%s: l2cap_bind failed (%d)\n",
495 1.1.2.3 yamt device_xname((struct device *)sc), err);
496 1.1.2.2 yamt return err;
497 1.1.2.2 yamt }
498 1.1.2.2 yamt
499 1.1.2.2 yamt sa.bt_psm = sc->sc_ctlpsm;
500 1.1.2.2 yamt bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
501 1.1.2.2 yamt err = l2cap_connect(sc->sc_ctl, &sa);
502 1.1.2.2 yamt if (err) {
503 1.1.2.3 yamt printf("%s: l2cap_connect failed (%d)\n",
504 1.1.2.3 yamt device_xname((struct device *)sc), err);
505 1.1.2.2 yamt return err;
506 1.1.2.2 yamt }
507 1.1.2.2 yamt
508 1.1.2.2 yamt sc->sc_state = BTHID_WAIT_CTL;
509 1.1.2.2 yamt return 0;
510 1.1.2.2 yamt }
511 1.1.2.2 yamt
512 1.1.2.2 yamt /*****************************************************************************
513 1.1.2.2 yamt *
514 1.1.2.2 yamt * bluetooth(9) callback methods for L2CAP
515 1.1.2.2 yamt *
516 1.1.2.2 yamt * All these are called from Bluetooth Protocol code, in a soft
517 1.1.2.2 yamt * interrupt context at IPL_SOFTNET.
518 1.1.2.2 yamt */
519 1.1.2.2 yamt
520 1.1.2.2 yamt static void
521 1.1.2.2 yamt bthidev_connecting(void *arg)
522 1.1.2.2 yamt {
523 1.1.2.2 yamt
524 1.1.2.2 yamt /* dont care */
525 1.1.2.2 yamt }
526 1.1.2.2 yamt
527 1.1.2.2 yamt static void
528 1.1.2.2 yamt bthidev_ctl_connected(void *arg)
529 1.1.2.2 yamt {
530 1.1.2.2 yamt struct sockaddr_bt sa;
531 1.1.2.2 yamt struct bthidev_softc *sc = arg;
532 1.1.2.2 yamt int err;
533 1.1.2.2 yamt
534 1.1.2.2 yamt if (sc->sc_state != BTHID_WAIT_CTL)
535 1.1.2.2 yamt return;
536 1.1.2.2 yamt
537 1.1.2.2 yamt KASSERT(sc->sc_ctl != NULL);
538 1.1.2.2 yamt KASSERT(sc->sc_int == NULL);
539 1.1.2.2 yamt
540 1.1.2.3 yamt if (sc->sc_flags & BTHID_CONNECTING) {
541 1.1.2.2 yamt /* initiate connect on interrupt PSM */
542 1.1.2.2 yamt err = l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
543 1.1.2.2 yamt if (err)
544 1.1.2.2 yamt goto fail;
545 1.1.2.2 yamt
546 1.1.2.2 yamt memset(&sa, 0, sizeof(sa));
547 1.1.2.2 yamt sa.bt_len = sizeof(sa);
548 1.1.2.2 yamt sa.bt_family = AF_BLUETOOTH;
549 1.1.2.2 yamt bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
550 1.1.2.2 yamt
551 1.1.2.2 yamt err = l2cap_bind(sc->sc_int, &sa);
552 1.1.2.2 yamt if (err)
553 1.1.2.2 yamt goto fail;
554 1.1.2.2 yamt
555 1.1.2.2 yamt sa.bt_psm = sc->sc_intpsm;
556 1.1.2.2 yamt bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
557 1.1.2.2 yamt err = l2cap_connect(sc->sc_int, &sa);
558 1.1.2.2 yamt if (err)
559 1.1.2.2 yamt goto fail;
560 1.1.2.2 yamt }
561 1.1.2.2 yamt
562 1.1.2.2 yamt sc->sc_state = BTHID_WAIT_INT;
563 1.1.2.2 yamt return;
564 1.1.2.2 yamt
565 1.1.2.2 yamt fail:
566 1.1.2.2 yamt l2cap_detach(&sc->sc_ctl);
567 1.1.2.2 yamt sc->sc_ctl = NULL;
568 1.1.2.3 yamt
569 1.1.2.3 yamt printf("%s: connect failed (%d)\n",
570 1.1.2.3 yamt device_xname((struct device *)sc), err);
571 1.1.2.2 yamt }
572 1.1.2.2 yamt
573 1.1.2.2 yamt static void
574 1.1.2.2 yamt bthidev_int_connected(void *arg)
575 1.1.2.2 yamt {
576 1.1.2.2 yamt struct bthidev_softc *sc = arg;
577 1.1.2.2 yamt
578 1.1.2.2 yamt if (sc->sc_state != BTHID_WAIT_INT)
579 1.1.2.2 yamt return;
580 1.1.2.2 yamt
581 1.1.2.2 yamt KASSERT(sc->sc_ctl != NULL);
582 1.1.2.2 yamt KASSERT(sc->sc_int != NULL);
583 1.1.2.2 yamt
584 1.1.2.2 yamt sc->sc_attempts = 0;
585 1.1.2.3 yamt sc->sc_flags &= ~BTHID_CONNECTING;
586 1.1.2.2 yamt sc->sc_state = BTHID_OPEN;
587 1.1.2.2 yamt
588 1.1.2.3 yamt printf("%s: connected\n", device_xname((struct device *)sc));
589 1.1.2.2 yamt }
590 1.1.2.2 yamt
591 1.1.2.2 yamt /*
592 1.1.2.2 yamt * Disconnected
593 1.1.2.2 yamt *
594 1.1.2.2 yamt * Depending on our state, this could mean several things, but essentially
595 1.1.2.3 yamt * we are lost. If both channels are closed, and we are marked to reconnect,
596 1.1.2.3 yamt * schedule another try otherwise just give up. They will contact us.
597 1.1.2.2 yamt */
598 1.1.2.2 yamt static void
599 1.1.2.2 yamt bthidev_ctl_disconnected(void *arg, int err)
600 1.1.2.2 yamt {
601 1.1.2.2 yamt struct bthidev_softc *sc = arg;
602 1.1.2.2 yamt
603 1.1.2.2 yamt if (sc->sc_ctl != NULL) {
604 1.1.2.2 yamt l2cap_detach(&sc->sc_ctl);
605 1.1.2.2 yamt sc->sc_ctl = NULL;
606 1.1.2.2 yamt }
607 1.1.2.2 yamt
608 1.1.2.2 yamt sc->sc_state = BTHID_CLOSED;
609 1.1.2.2 yamt
610 1.1.2.2 yamt if (sc->sc_int == NULL) {
611 1.1.2.3 yamt printf("%s: disconnected\n",
612 1.1.2.3 yamt device_xname((struct device *)sc));
613 1.1.2.3 yamt sc->sc_flags &= ~BTHID_CONNECTING;
614 1.1.2.2 yamt
615 1.1.2.3 yamt if (sc->sc_flags & BTHID_RECONNECT)
616 1.1.2.2 yamt callout_schedule(&sc->sc_reconnect,
617 1.1.2.2 yamt BTHID_RETRY_INTERVAL * hz);
618 1.1.2.2 yamt else
619 1.1.2.2 yamt sc->sc_state = BTHID_WAIT_CTL;
620 1.1.2.3 yamt } else {
621 1.1.2.3 yamt /*
622 1.1.2.3 yamt * The interrupt channel should have been closed first,
623 1.1.2.3 yamt * but its potentially unsafe to detach that from here.
624 1.1.2.3 yamt * Give them a second to do the right thing or let the
625 1.1.2.3 yamt * callout handle it.
626 1.1.2.3 yamt */
627 1.1.2.3 yamt callout_schedule(&sc->sc_reconnect, hz);
628 1.1.2.2 yamt }
629 1.1.2.2 yamt }
630 1.1.2.2 yamt
631 1.1.2.2 yamt static void
632 1.1.2.2 yamt bthidev_int_disconnected(void *arg, int err)
633 1.1.2.2 yamt {
634 1.1.2.2 yamt struct bthidev_softc *sc = arg;
635 1.1.2.2 yamt
636 1.1.2.2 yamt if (sc->sc_int != NULL) {
637 1.1.2.2 yamt l2cap_detach(&sc->sc_int);
638 1.1.2.2 yamt sc->sc_int = NULL;
639 1.1.2.2 yamt }
640 1.1.2.2 yamt
641 1.1.2.2 yamt sc->sc_state = BTHID_CLOSED;
642 1.1.2.2 yamt
643 1.1.2.2 yamt if (sc->sc_ctl == NULL) {
644 1.1.2.3 yamt printf("%s: disconnected\n",
645 1.1.2.3 yamt device_xname((struct device *)sc));
646 1.1.2.3 yamt sc->sc_flags &= ~BTHID_CONNECTING;
647 1.1.2.2 yamt
648 1.1.2.3 yamt if (sc->sc_flags & BTHID_RECONNECT)
649 1.1.2.2 yamt callout_schedule(&sc->sc_reconnect,
650 1.1.2.2 yamt BTHID_RETRY_INTERVAL * hz);
651 1.1.2.2 yamt else
652 1.1.2.2 yamt sc->sc_state = BTHID_WAIT_CTL;
653 1.1.2.3 yamt } else {
654 1.1.2.3 yamt /*
655 1.1.2.3 yamt * The control channel should be closing also, allow
656 1.1.2.3 yamt * them a chance to do that before we force it.
657 1.1.2.3 yamt */
658 1.1.2.3 yamt callout_schedule(&sc->sc_reconnect, hz);
659 1.1.2.2 yamt }
660 1.1.2.2 yamt }
661 1.1.2.2 yamt
662 1.1.2.2 yamt /*
663 1.1.2.2 yamt * New Connections
664 1.1.2.2 yamt *
665 1.1.2.2 yamt * We give a new L2CAP handle back if this matches the BDADDR we are
666 1.1.2.2 yamt * listening for and we are in the right state. bthidev_connected will
667 1.1.2.2 yamt * be called when the connection is open, so nothing else to do here
668 1.1.2.2 yamt */
669 1.1.2.2 yamt static void *
670 1.1.2.3 yamt bthidev_ctl_newconn(void *arg, struct sockaddr_bt *laddr,
671 1.1.2.3 yamt struct sockaddr_bt *raddr)
672 1.1.2.2 yamt {
673 1.1.2.2 yamt struct bthidev_softc *sc = arg;
674 1.1.2.2 yamt
675 1.1.2.2 yamt if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0
676 1.1.2.3 yamt || (sc->sc_flags & BTHID_CONNECTING)
677 1.1.2.2 yamt || sc->sc_state != BTHID_WAIT_CTL
678 1.1.2.2 yamt || sc->sc_ctl != NULL
679 1.1.2.2 yamt || sc->sc_int != NULL)
680 1.1.2.2 yamt return NULL;
681 1.1.2.2 yamt
682 1.1.2.2 yamt l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
683 1.1.2.2 yamt return sc->sc_ctl;
684 1.1.2.2 yamt }
685 1.1.2.2 yamt
686 1.1.2.2 yamt static void *
687 1.1.2.3 yamt bthidev_int_newconn(void *arg, struct sockaddr_bt *laddr,
688 1.1.2.3 yamt struct sockaddr_bt *raddr)
689 1.1.2.2 yamt {
690 1.1.2.2 yamt struct bthidev_softc *sc = arg;
691 1.1.2.2 yamt
692 1.1.2.2 yamt if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0
693 1.1.2.3 yamt || (sc->sc_flags & BTHID_CONNECTING)
694 1.1.2.2 yamt || sc->sc_state != BTHID_WAIT_INT
695 1.1.2.2 yamt || sc->sc_ctl == NULL
696 1.1.2.2 yamt || sc->sc_int != NULL)
697 1.1.2.2 yamt return NULL;
698 1.1.2.2 yamt
699 1.1.2.2 yamt l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
700 1.1.2.2 yamt return sc->sc_int;
701 1.1.2.2 yamt }
702 1.1.2.2 yamt
703 1.1.2.2 yamt static void
704 1.1.2.2 yamt bthidev_complete(void *arg, int count)
705 1.1.2.2 yamt {
706 1.1.2.2 yamt
707 1.1.2.2 yamt /* dont care */
708 1.1.2.2 yamt }
709 1.1.2.2 yamt
710 1.1.2.2 yamt /*
711 1.1.2.2 yamt * Receive reports from the protocol stack.
712 1.1.2.2 yamt */
713 1.1.2.2 yamt static void
714 1.1.2.2 yamt bthidev_input(void *arg, struct mbuf *m)
715 1.1.2.2 yamt {
716 1.1.2.2 yamt struct bthidev_softc *sc = arg;
717 1.1.2.2 yamt struct bthidev *dev;
718 1.1.2.2 yamt uint8_t *data;
719 1.1.2.2 yamt int len;
720 1.1.2.2 yamt
721 1.1.2.2 yamt if (sc->sc_state != BTHID_OPEN)
722 1.1.2.2 yamt goto release;
723 1.1.2.2 yamt
724 1.1.2.2 yamt if (m->m_pkthdr.len > m->m_len)
725 1.1.2.3 yamt printf("%s: truncating HID report\n",
726 1.1.2.3 yamt device_xname((struct device *)sc));
727 1.1.2.2 yamt
728 1.1.2.2 yamt len = m->m_len;
729 1.1.2.2 yamt data = mtod(m, uint8_t *);
730 1.1.2.2 yamt
731 1.1.2.2 yamt if (BTHID_TYPE(data[0]) == BTHID_DATA) {
732 1.1.2.2 yamt /*
733 1.1.2.2 yamt * data[0] == type / parameter
734 1.1.2.2 yamt * data[1] == id
735 1.1.2.2 yamt * data[2..len] == report
736 1.1.2.2 yamt */
737 1.1.2.2 yamt if (len < 3)
738 1.1.2.2 yamt goto release;
739 1.1.2.2 yamt
740 1.1.2.2 yamt LIST_FOREACH(dev, &sc->sc_list, sc_next) {
741 1.1.2.2 yamt if (data[1] == dev->sc_id) {
742 1.1.2.2 yamt switch (BTHID_DATA_PARAM(data[0])) {
743 1.1.2.2 yamt case BTHID_DATA_INPUT:
744 1.1.2.2 yamt (*dev->sc_input)(dev, data + 2, len - 2);
745 1.1.2.2 yamt break;
746 1.1.2.2 yamt
747 1.1.2.2 yamt case BTHID_DATA_FEATURE:
748 1.1.2.2 yamt (*dev->sc_feature)(dev, data + 2, len - 2);
749 1.1.2.2 yamt break;
750 1.1.2.2 yamt
751 1.1.2.2 yamt default:
752 1.1.2.2 yamt break;
753 1.1.2.2 yamt }
754 1.1.2.2 yamt
755 1.1.2.2 yamt goto release;
756 1.1.2.2 yamt }
757 1.1.2.2 yamt }
758 1.1.2.2 yamt printf("%s: report id %d, len = %d ignored\n",
759 1.1.2.3 yamt device_xname((struct device *)sc), data[1], len - 2);
760 1.1.2.2 yamt
761 1.1.2.2 yamt goto release;
762 1.1.2.2 yamt }
763 1.1.2.2 yamt
764 1.1.2.2 yamt if (BTHID_TYPE(data[0]) == BTHID_CONTROL) {
765 1.1.2.2 yamt if (len < 1)
766 1.1.2.2 yamt goto release;
767 1.1.2.2 yamt
768 1.1.2.2 yamt if (BTHID_DATA_PARAM(data[0]) == BTHID_CONTROL_UNPLUG) {
769 1.1.2.3 yamt printf("%s: unplugged\n",
770 1.1.2.3 yamt device_xname((struct device *)sc));
771 1.1.2.2 yamt
772 1.1.2.2 yamt /* close interrupt channel */
773 1.1.2.2 yamt if (sc->sc_int != NULL) {
774 1.1.2.2 yamt l2cap_disconnect(sc->sc_int, 0);
775 1.1.2.2 yamt l2cap_detach(&sc->sc_int);
776 1.1.2.2 yamt sc->sc_int = NULL;
777 1.1.2.2 yamt }
778 1.1.2.2 yamt
779 1.1.2.2 yamt /* close control channel */
780 1.1.2.2 yamt if (sc->sc_ctl != NULL) {
781 1.1.2.2 yamt l2cap_disconnect(sc->sc_ctl, 0);
782 1.1.2.2 yamt l2cap_detach(&sc->sc_ctl);
783 1.1.2.2 yamt sc->sc_ctl = NULL;
784 1.1.2.2 yamt }
785 1.1.2.2 yamt }
786 1.1.2.2 yamt
787 1.1.2.2 yamt goto release;
788 1.1.2.2 yamt }
789 1.1.2.2 yamt
790 1.1.2.2 yamt release:
791 1.1.2.2 yamt m_freem(m);
792 1.1.2.2 yamt }
793 1.1.2.2 yamt
794 1.1.2.2 yamt /*****************************************************************************
795 1.1.2.2 yamt *
796 1.1.2.2 yamt * IO routines
797 1.1.2.2 yamt */
798 1.1.2.2 yamt
799 1.1.2.2 yamt static void
800 1.1.2.3 yamt bthidev_null(struct bthidev *dev, uint8_t *report,
801 1.1.2.3 yamt int len)
802 1.1.2.2 yamt {
803 1.1.2.2 yamt
804 1.1.2.2 yamt /*
805 1.1.2.2 yamt * empty routine just in case the device
806 1.1.2.2 yamt * provided no method to handle this report
807 1.1.2.2 yamt */
808 1.1.2.2 yamt }
809 1.1.2.2 yamt
810 1.1.2.2 yamt static int
811 1.1.2.2 yamt bthidev_output(struct bthidev *dev, uint8_t *report, int rlen)
812 1.1.2.2 yamt {
813 1.1.2.2 yamt struct bthidev_softc *sc = (struct bthidev_softc *)dev->sc_parent;
814 1.1.2.2 yamt struct mbuf *m;
815 1.1.2.2 yamt int s, err;
816 1.1.2.2 yamt
817 1.1.2.2 yamt if (sc == NULL || sc->sc_state != BTHID_OPEN)
818 1.1.2.2 yamt return ENOTCONN;
819 1.1.2.2 yamt
820 1.1.2.2 yamt KASSERT(sc->sc_ctl != NULL);
821 1.1.2.2 yamt KASSERT(sc->sc_int != NULL);
822 1.1.2.2 yamt
823 1.1.2.2 yamt if (rlen == 0 || report == NULL)
824 1.1.2.2 yamt return 0;
825 1.1.2.2 yamt
826 1.1.2.2 yamt if (rlen > MHLEN - 2) {
827 1.1.2.2 yamt printf("%s: output report too long (%d)!\n",
828 1.1.2.3 yamt device_xname((struct device *)sc), rlen);
829 1.1.2.2 yamt
830 1.1.2.2 yamt return EMSGSIZE;
831 1.1.2.2 yamt }
832 1.1.2.2 yamt
833 1.1.2.2 yamt m = m_gethdr(M_DONTWAIT, MT_DATA);
834 1.1.2.2 yamt if (m == NULL)
835 1.1.2.2 yamt return ENOMEM;
836 1.1.2.2 yamt
837 1.1.2.2 yamt /*
838 1.1.2.2 yamt * data[0] = type / parameter
839 1.1.2.2 yamt * data[1] = id
840 1.1.2.2 yamt * data[2..N] = report
841 1.1.2.2 yamt */
842 1.1.2.2 yamt mtod(m, uint8_t *)[0] = (uint8_t)((BTHID_DATA << 4) | BTHID_DATA_OUTPUT);
843 1.1.2.2 yamt mtod(m, uint8_t *)[1] = dev->sc_id;
844 1.1.2.2 yamt memcpy(mtod(m, uint8_t *) + 2, report, rlen);
845 1.1.2.2 yamt m->m_pkthdr.len = m->m_len = rlen + 2;
846 1.1.2.2 yamt
847 1.1.2.2 yamt s = splsoftnet();
848 1.1.2.2 yamt err = l2cap_send(sc->sc_int, m);
849 1.1.2.2 yamt splx(s);
850 1.1.2.2 yamt
851 1.1.2.2 yamt return err;
852 1.1.2.2 yamt }
853