Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sys / dev / usbmisc / ubt / ubt.c
1 /* $OpenBSD: src/sys/dev/usb/ubt.c,v 1.11 2008/02/24 21:34:48 uwe Exp $ */
2 /* $NetBSD: ubt.c,v 1.30 2007/12/16 19:01:37 christos Exp $ */
3
4 /*-
5  * Copyright (c) 2006 Itronix Inc.
6  * All rights reserved.
7  *
8  * Written by Iain Hibbert for Itronix Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of Itronix Inc. may not be used to endorse
19  *    or promote products derived from this software without specific
20  *    prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 /*
35  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
36  * All rights reserved.
37  *
38  * This code is derived from software contributed to The NetBSD Foundation
39  * by Lennart Augustsson (lennart@augustsson.net) and
40  * David Sainty (David.Sainty@dtsp.co.nz).
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *        This product includes software developed by the NetBSD
53  *        Foundation, Inc. and its contributors.
54  * 4. Neither the name of The NetBSD Foundation nor the names of its
55  *    contributors may be used to endorse or promote products derived
56  *    from this software without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
59  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68  * POSSIBILITY OF SUCH DAMAGE.
69  */
70 /*
71  * This driver originally written by Lennart Augustsson and David Sainty,
72  * but was mostly rewritten for the NetBSD Bluetooth protocol stack by
73  * Iain Hibbert for Itronix, Inc using the FreeBSD ng_ubt.c driver as a
74  * reference.
75  */
76
77 #include <sys/bus.h>
78 #include <sys/kernel.h>
79 #include <sys/proc.h>
80 #include <sys/sysctl.h>
81
82 #include <bus/usb/usb.h>
83 #include <bus/usb/usbdi.h>
84 #include <bus/usb/usbdi_util.h>
85 #include <bus/usb/usbdivar.h>
86
87 #include <netbt/bluetooth.h>
88 #include <netbt/hci.h>
89
90 /*******************************************************************************
91  *
92  *      debugging stuff
93  */
94 #undef DPRINTF
95 #undef DPRINTFN
96
97 #ifdef UBT_DEBUG
98 int     ubt_debug = UBT_DEBUG;
99
100 #define DPRINTF(fmt, args...)           do {            \
101         if (ubt_debug)                                  \
102                 kprintf("%s: "fmt, __func__ , ##args);  \
103 } while (/* CONSTCOND */0)
104
105 #define DPRINTFN(n, fmt, args...)       do {            \
106         if (ubt_debug > (n))                            \
107                 kprintf("%s: "fmt, __func__ , ##args);  \
108 } while (/* CONSTCOND */0)
109
110 #else
111 #define DPRINTF(...)
112 #define DPRINTFN(...)
113 #endif
114
115 /*******************************************************************************
116  *
117  *      ubt softc structure
118  *
119  */
120
121 /* buffer sizes */
122 /*
123  * NB: although ACL packets can extend to 65535 bytes, most devices
124  * have max_acl_size at much less (largest I have seen is 384)
125  */
126 #define UBT_BUFSIZ_CMD          (HCI_CMD_PKT_SIZE - 1)
127 #define UBT_BUFSIZ_ACL          (2048 - 1)
128 #define UBT_BUFSIZ_EVENT        (HCI_EVENT_PKT_SIZE - 1)
129
130 /* Transmit timeouts */
131
132 #define UBT_CMD_TIMEOUT         100
133 #define UBT_ACL_TIMEOUT         100
134
135 /*
136  * Specification says, that is must be 1ms, but it causes kernel panic.
137  * Setting interval to USBD_DEFAULT_INTERVAL is not working for some of
138  * us either.
139  * XXX: Make it sysctl.
140  */
141 #define UBT_INTR_TIMEOUT                USBD_DEFAULT_INTERVAL
142
143 /*
144  * ISOC transfers
145  *
146  * xfer buffer size depends on the frame size, and the number
147  * of frames per transfer is fixed, as each frame should be
148  * 1ms worth of data. This keeps the rate that xfers complete
149  * fairly constant. We use multiple xfers to keep the hardware
150  * busy
151  */
152 #define UBT_NXFERS              3       /* max xfers to queue */
153 #define UBT_NFRAMES             10      /* frames per xfer */
154
155 struct ubt_isoc_xfer {
156         struct ubt_softc        *softc;
157         usbd_xfer_handle         xfer;
158         uint8_t                 *buf;
159         uint16_t                 size[UBT_NFRAMES];
160         int                      busy;
161 };
162
163 struct ubt_softc {
164         device_t                 sc_dev;
165         usbd_device_handle       sc_udev;
166         int                      sc_refcnt;
167         int                      sc_dying;
168         int                      sc_enabled;
169
170         /* Control Interface */
171         usbd_interface_handle    sc_iface0;
172
173         /* Commands (control) */
174         usbd_xfer_handle         sc_cmd_xfer;
175         u_char                  *sc_cmd_buf;
176         int                      sc_cmd_busy;   /* write active */
177         struct ifqueue           sc_cmd_queue;  /* output queue */
178
179         /* Events (interrupt) */
180         int                      sc_evt_addr;   /* endpoint address */
181         usbd_pipe_handle         sc_evt_pipe;
182         u_char                  *sc_evt_buf;
183
184
185         /* ACL data (in) */
186         int                      sc_aclrd_addr; /* endpoint address */
187         usbd_pipe_handle         sc_aclrd_pipe; /* read pipe */
188         usbd_xfer_handle         sc_aclrd_xfer; /* read xfer */
189         u_char                  *sc_aclrd_buf;  /* read buffer */
190         int                      sc_aclrd_busy; /* reading */
191
192         /* ACL data (out) */
193         int                      sc_aclwr_addr; /* endpoint address */
194         usbd_pipe_handle         sc_aclwr_pipe; /* write pipe */
195         usbd_xfer_handle         sc_aclwr_xfer; /* write xfer */
196         u_char                  *sc_aclwr_buf;  /* write buffer */
197         int                      sc_aclwr_busy; /* write active */
198         struct ifqueue           sc_aclwr_queue;/* output queue */
199
200         /* ISOC interface */
201         usbd_interface_handle    sc_iface1;     /* ISOC interface */
202         struct sysctllog        *sc_log;        /* sysctl log */
203         int                      sc_config;     /* current config no */
204         int                      sc_alt_config; /* no of alternates */
205
206         /* SCO data (in) */
207         int                      sc_scord_addr; /* endpoint address */
208         usbd_pipe_handle         sc_scord_pipe; /* read pipe */
209         int                      sc_scord_size; /* frame length */
210         struct ubt_isoc_xfer     sc_scord[UBT_NXFERS];
211         struct mbuf             *sc_scord_mbuf; /* current packet */
212
213         /* SCO data (out) */
214         int                      sc_scowr_addr; /* endpoint address */
215         usbd_pipe_handle         sc_scowr_pipe; /* write pipe */
216         int                      sc_scowr_size; /* frame length */
217         struct ubt_isoc_xfer     sc_scowr[UBT_NXFERS];
218         struct mbuf             *sc_scowr_mbuf; /* current packet */
219         int                      sc_scowr_busy; /* write active */
220         struct ifqueue           sc_scowr_queue;/* output queue */
221
222         /* Protocol structure */
223         struct hci_unit         *sc_unit;
224         struct bt_stats          sc_stats;
225
226         /* Successfully attached */
227         int                      sc_ok;
228
229         struct sysctl_ctx_list   sysctl_ctx;
230         struct sysctl_oid       *sysctl_tree;
231 };
232
233 /*
234  * Bluetooth unit/USB callback routines
235  */
236 int ubt_enable(struct device *);
237 void ubt_disable(struct device *);
238
239 void ubt_xmit_cmd(struct device *, struct mbuf *);
240 void ubt_xmit_cmd_start(struct ubt_softc *);
241 void ubt_xmit_cmd_complete(usbd_xfer_handle,
242                                 usbd_private_handle, usbd_status);
243
244 void ubt_xmit_acl(struct device *, struct mbuf *);
245 void ubt_xmit_acl_start(struct ubt_softc *);
246 void ubt_xmit_acl_complete(usbd_xfer_handle,
247                                 usbd_private_handle, usbd_status);
248
249 void ubt_xmit_sco(struct device *, struct mbuf *);
250 void ubt_xmit_sco_start(struct ubt_softc *);
251 void ubt_xmit_sco_start1(struct ubt_softc *, struct ubt_isoc_xfer *);
252 void ubt_xmit_sco_complete(usbd_xfer_handle,
253                                 usbd_private_handle, usbd_status);
254
255 void ubt_recv_event(usbd_xfer_handle,
256                                 usbd_private_handle, usbd_status);
257
258 void ubt_recv_acl_start(struct ubt_softc *);
259 void ubt_recv_acl_complete(usbd_xfer_handle,
260                                 usbd_private_handle, usbd_status);
261
262 void ubt_recv_sco_start1(struct ubt_softc *, struct ubt_isoc_xfer *);
263 void ubt_recv_sco_complete(usbd_xfer_handle,
264                                 usbd_private_handle, usbd_status);
265
266 void ubt_stats(struct device *, struct bt_stats *, int);
267
268 static device_probe_t ubt_match;
269 static device_attach_t ubt_attach;
270 static device_detach_t ubt_detach;
271
272 static devclass_t ubt_devclass;
273
274 static device_method_t ubt_methods[] = {
275         DEVMETHOD(device_probe, ubt_match),
276         DEVMETHOD(device_attach, ubt_attach),
277         DEVMETHOD(device_detach, ubt_detach),
278         {0,0},
279         {0,0}
280 };
281
282 static driver_t ubt_driver = {
283         "ubt",
284         ubt_methods,
285         sizeof(struct ubt_softc)
286 };
287
288 DRIVER_MODULE(ubt, uhub, ubt_driver, ubt_devclass, usbd_driver_load, NULL);
289 MODULE_DEPEND(ubt, netbt, 1, 1, 1);
290 #if 0 /* not yet */
291 MODULE_DEPEND(ubt, bthub, 1, 1, 1);
292 #endif
293 MODULE_DEPEND(ubt, usb, 1, 1, 1);
294
295 const struct hci_if ubt_hci = {
296         .enable = ubt_enable,
297         .disable = ubt_disable,
298         .output_cmd = ubt_xmit_cmd,
299         .output_acl = ubt_xmit_acl,
300         .output_sco = ubt_xmit_sco,
301         .get_stats = ubt_stats,
302 };
303
304 static int ubt_set_isoc_config(struct ubt_softc *);
305 static int ubt_sysctl_config(SYSCTL_HANDLER_ARGS);
306 static void ubt_abortdealloc(struct ubt_softc *);
307
308 /*
309  * Match against the whole device, since we want to take
310  * both interfaces. If a device should be ignored then add
311  *
312  *      { VendorID, ProductID }
313  *
314  * to the ubt_ignore list.
315  */
316 static const struct usb_devno ubt_ignore[] = {
317         { USB_DEVICE(0x0a5c, 0x2033) }, /* Broadcom BCM2033 */
318         { 0, 0 }                        /* end of list */
319 };
320
321 static int
322 ubt_match(device_t self)
323 {
324         struct usb_attach_arg *uaa = device_get_ivars(self);
325         usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
326
327         DPRINTFN(50, "ubt_match\n");
328
329         if (usb_lookup(ubt_ignore, uaa->vendor, uaa->product))
330                 return UMATCH_NONE;
331
332         if (dd->bDeviceClass == UDCLASS_WIRELESS
333             && dd->bDeviceSubClass == UDSUBCLASS_RF
334             && dd->bDeviceProtocol == UDPROTO_BLUETOOTH)
335                 return UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO;
336         return UMATCH_NONE;
337 }
338
339 static int
340 ubt_attach(device_t self)
341 {
342         struct ubt_softc *sc = device_get_softc(self);
343         struct usb_attach_arg *uaa = device_get_ivars(self);
344
345         usb_config_descriptor_t *cd;
346         usb_endpoint_descriptor_t *ed;
347         int err;
348         uint8_t count, i;
349
350         DPRINTFN(50, "ubt_attach: sc=%p\n", sc);
351
352         sc->sc_udev = uaa->device;
353         sc->sc_dev = self;
354
355         /*
356          * Move the device into the configured state
357          */
358         err = usbd_set_config_index(sc->sc_udev, 0, 1);
359         if (err) {
360                 kprintf("%s: failed to set configuration idx 0: %s\n",
361                     device_get_nameunit(sc->sc_dev), usbd_errstr(err));
362
363                 return ENXIO;
364         }
365
366         /*
367          * Interface 0 must have 3 endpoints
368          *      1) Interrupt endpoint to receive HCI events
369          *      2) Bulk IN endpoint to receive ACL data
370          *      3) Bulk OUT endpoint to send ACL data
371          */
372         err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface0);
373         if (err) {
374                 kprintf("%s: Could not get interface 0 handle %s (%d)\n",
375                                 device_get_nameunit(sc->sc_dev), usbd_errstr(err), err);
376
377                 return ENXIO;
378         }
379
380         sc->sc_evt_addr = -1;
381         sc->sc_aclrd_addr = -1;
382         sc->sc_aclwr_addr = -1;
383
384         count = 0;
385         (void)usbd_endpoint_count(sc->sc_iface0, &count);
386
387         for (i = 0 ; i < count ; i++) {
388                 int dir, type;
389
390                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface0, i);
391                 if (ed == NULL) {
392                         kprintf("%s: could not read endpoint descriptor %d\n",
393                             device_get_nameunit(sc->sc_dev), i);
394
395                         return ENXIO;
396                 }
397
398                 dir = UE_GET_DIR(ed->bEndpointAddress);
399                 type = UE_GET_XFERTYPE(ed->bmAttributes);
400
401                 if (dir == UE_DIR_IN && type == UE_INTERRUPT)
402                         sc->sc_evt_addr = ed->bEndpointAddress;
403                 else if (dir == UE_DIR_IN && type == UE_BULK)
404                         sc->sc_aclrd_addr = ed->bEndpointAddress;
405                 else if (dir == UE_DIR_OUT && type == UE_BULK)
406                         sc->sc_aclwr_addr = ed->bEndpointAddress;
407         }
408
409         if (sc->sc_evt_addr == -1) {
410                 kprintf("%s: missing INTERRUPT endpoint on interface 0\n",
411                                 device_get_nameunit(sc->sc_dev));
412
413                 return ENXIO;
414         }
415         if (sc->sc_aclrd_addr == -1) {
416                 kprintf("%s: missing BULK IN endpoint on interface 0\n",
417                                 device_get_nameunit(sc->sc_dev));
418
419                 return ENXIO;
420         }
421         if (sc->sc_aclwr_addr == -1) {
422                 kprintf("%s: missing BULK OUT endpoint on interface 0\n",
423                                 device_get_nameunit(sc->sc_dev));
424
425                 return ENXIO;
426         }
427
428         /*
429          * Interface 1 must have 2 endpoints
430          *      1) Isochronous IN endpoint to receive SCO data
431          *      2) Isochronous OUT endpoint to send SCO data
432          *
433          * and will have several configurations, which can be selected
434          * via a sysctl variable. We select config 0 to start, which
435          * means that no SCO data will be available.
436          */
437         err = usbd_device2interface_handle(sc->sc_udev, 1, &sc->sc_iface1);
438         if (err) {
439                 kprintf("%s: Could not get interface 1 handle %s (%d)\n",
440                     device_get_nameunit(sc->sc_dev), usbd_errstr(err), err);
441
442                 return ENXIO;
443         }
444
445         cd = usbd_get_config_descriptor(sc->sc_udev);
446         if (cd == NULL) {
447                 kprintf("%s: could not get config descriptor\n",
448                         device_get_nameunit(sc->sc_dev));
449
450                 return ENXIO;
451         }
452
453         sc->sc_alt_config = usbd_get_no_alts(cd, 1);
454
455         /* set initial config */
456         err = ubt_set_isoc_config(sc);
457         if (err) {
458                 kprintf("%s: ISOC config failed\n",
459                         device_get_nameunit(sc->sc_dev));
460
461                 return ENXIO;
462         }
463
464         /* Attach HCI */
465         sc->sc_unit = hci_attach(&ubt_hci, sc->sc_dev, 0);
466
467         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
468                            sc->sc_dev);
469
470         sc->sc_ok = 1;
471
472         sysctl_ctx_init(&sc->sysctl_ctx);
473         sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
474                                           SYSCTL_STATIC_CHILDREN(_hw),
475                                           OID_AUTO,
476                                           device_get_nameunit(sc->sc_dev),
477                                           CTLFLAG_RD, 0, "");
478
479         if (sc->sysctl_tree == NULL) {
480                 /* Failure isn't fatal */
481                 device_printf(sc->sc_dev, "Unable to create sysctl tree\n");
482                 return 0;
483         }
484
485         SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
486                         OID_AUTO, "config", CTLTYPE_INT|CTLFLAG_RW, (void *)sc,
487                         0, ubt_sysctl_config, "I", "Configuration number");
488         SYSCTL_ADD_INT(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
489                        OID_AUTO, "alt_config", CTLFLAG_RD, &sc->sc_alt_config,
490                        0, "Number of alternate configurations");
491         SYSCTL_ADD_INT(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
492                        OID_AUTO, "sco_rxsize", CTLFLAG_RD, &sc->sc_scord_size,
493                        0, "Max SCO receive size");
494         SYSCTL_ADD_INT(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
495                        OID_AUTO, "sco_wrsize", CTLFLAG_RD, &sc->sc_scowr_size,
496                        0, "Max SCO transmit size");
497
498         return 0;
499 }
500
501 static int
502 ubt_detach(device_t self)
503 {
504
505         struct ubt_softc *sc = device_get_softc(self);
506
507         DPRINTF("sc=%p \n", sc);
508
509         sc->sc_dying = 1;
510
511         if (!sc->sc_ok)
512                 return 0;
513
514         /* Detach HCI interface */
515         if (sc->sc_unit) {
516                 hci_detach(sc->sc_unit);
517                 sc->sc_unit = NULL;
518         }
519
520         /*
521          * Abort all pipes. Causes processes waiting for transfer to wake.
522          *
523          * Actually, hci_detach() above will call ubt_disable() which may
524          * call ubt_abortdealloc(), but lets be sure since doing it twice
525          * wont cause an error.
526          */
527         ubt_abortdealloc(sc);
528
529         /* wait for all processes to finish */
530         if (sc->sc_refcnt-- > 0)
531                 usb_detach_wait(sc->sc_dev);
532
533         if (sc->sysctl_tree != NULL) {
534                 sc->sysctl_tree = NULL;
535                 sysctl_ctx_free(&sc->sysctl_ctx);
536         }
537
538         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
539                            sc->sc_dev);
540
541         DPRINTFN(1, "driver detached\n");
542
543         return 0;
544 }
545
546 /* set ISOC configuration */
547 int
548 ubt_set_isoc_config(struct ubt_softc *sc)
549 {
550         usb_endpoint_descriptor_t *ed;
551         int rd_addr, wr_addr, rd_size, wr_size;
552         uint8_t count, i;
553         int err;
554
555         err = usbd_set_interface(sc->sc_iface1, sc->sc_config);
556         if (err != USBD_NORMAL_COMPLETION) {
557                 kprintf(
558                     "%s: Could not set config %d on ISOC interface. %s (%d)\n",
559                     device_get_nameunit(sc->sc_dev), sc->sc_config, usbd_errstr(err), err);
560
561                 return err == USBD_IN_USE ? EBUSY : EIO;
562         }
563
564         /*
565          * We wont get past the above if there are any pipes open, so no
566          * need to worry about buf/xfer/pipe deallocation. If we get an
567          * error after this, the frame quantities will be 0 and no SCO
568          * data will be possible.
569          */
570
571         sc->sc_scord_size = rd_size = 0;
572         sc->sc_scord_addr = rd_addr = -1;
573
574         sc->sc_scowr_size = wr_size = 0;
575         sc->sc_scowr_addr = wr_addr = -1;
576
577         count = 0;
578         (void)usbd_endpoint_count(sc->sc_iface1, &count);
579
580         for (i = 0 ; i < count ; i++) {
581                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface1, i);
582                 if (ed == NULL) {
583                         kprintf("%s: could not read endpoint descriptor %d\n",
584                             device_get_nameunit(sc->sc_dev), i);
585
586                         return EIO;
587                 }
588
589                 DPRINTFN(5, "%s: endpoint type %02x (%02x) addr %02x (%s)\n",
590                         device_get_nameunit(sc->sc_dev),
591                         UE_GET_XFERTYPE(ed->bmAttributes),
592                         UE_GET_ISO_TYPE(ed->bmAttributes),
593                         ed->bEndpointAddress,
594                         UE_GET_DIR(ed->bEndpointAddress) ? "in" : "out");
595
596                 if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS)
597                         continue;
598
599                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) {
600                         rd_addr = ed->bEndpointAddress;
601                         rd_size = UGETW(ed->wMaxPacketSize);
602                 } else {
603                         wr_addr = ed->bEndpointAddress;
604                         wr_size = UGETW(ed->wMaxPacketSize);
605                 }
606         }
607
608         if (rd_addr == -1) {
609                 kprintf(
610                     "%s: missing ISOC IN endpoint on interface config %d\n",
611                     device_get_nameunit(sc->sc_dev), sc->sc_config);
612
613                 return ENOENT;
614         }
615         if (wr_addr == -1) {
616                 kprintf(
617                     "%s: missing ISOC OUT endpoint on interface config %d\n",
618                     device_get_nameunit(sc->sc_dev), sc->sc_config);
619
620                 return ENOENT;
621         }
622
623 #ifdef DIAGNOSTIC
624         if (rd_size > MLEN) {
625                 kprintf("%s: rd_size=%d exceeds MLEN\n",
626                     device_get_nameunit(sc->sc_dev), rd_size);
627
628                 return EOVERFLOW;
629         }
630
631         if (wr_size > MLEN) {
632                 kprintf("%s: wr_size=%d exceeds MLEN\n",
633                     device_get_nameunit(sc->sc_dev), wr_size);
634
635                 return EOVERFLOW;
636         }
637 #endif
638
639         sc->sc_scord_size = rd_size;
640         sc->sc_scord_addr = rd_addr;
641
642         sc->sc_scowr_size = wr_size;
643         sc->sc_scowr_addr = wr_addr;
644
645         return 0;
646 }
647
648 static int
649 ubt_sysctl_config(SYSCTL_HANDLER_ARGS)
650 {
651         struct ubt_softc *sc = (struct ubt_softc *)arg1;
652         int t, error;
653
654         t = sc->sc_config;
655         error = sysctl_handle_int(oidp, &t, sizeof(t), req);
656         if (error || req->newptr == NULL)
657                 return error;
658
659         if (t < 0 || t >= sc->sc_alt_config)
660                 return EINVAL;
661
662         /* This may not change when the unit is enabled */
663         if (sc->sc_enabled)
664                 return EBUSY;
665
666         sc->sc_config = t;
667         return ubt_set_isoc_config(sc);
668 }
669
670 void
671 ubt_abortdealloc(struct ubt_softc *sc)
672 {
673         int i;
674
675         DPRINTFN(1, "sc=%p\n", sc);
676
677         crit_enter();
678         /* Abort all pipes */
679         if (sc->sc_evt_pipe != NULL) {
680                 usbd_abort_pipe(sc->sc_evt_pipe);
681                 usbd_close_pipe(sc->sc_evt_pipe);
682                 sc->sc_evt_pipe = NULL;
683         }
684
685         if (sc->sc_aclrd_pipe != NULL) {
686                 usbd_abort_pipe(sc->sc_aclrd_pipe);
687                 usbd_close_pipe(sc->sc_aclrd_pipe);
688                 sc->sc_aclrd_pipe = NULL;
689         }
690
691         if (sc->sc_aclwr_pipe != NULL) {
692                 usbd_abort_pipe(sc->sc_aclwr_pipe);
693                 usbd_close_pipe(sc->sc_aclwr_pipe);
694                 sc->sc_aclwr_pipe = NULL;
695         }
696
697         if (sc->sc_scord_pipe != NULL) {
698                 usbd_abort_pipe(sc->sc_scord_pipe);
699                 usbd_close_pipe(sc->sc_scord_pipe);
700                 sc->sc_scord_pipe = NULL;
701         }
702
703         if (sc->sc_scowr_pipe != NULL) {
704                 usbd_abort_pipe(sc->sc_scowr_pipe);
705                 usbd_close_pipe(sc->sc_scowr_pipe);
706                 sc->sc_scowr_pipe = NULL;
707         }
708
709         /* Free event buffer */
710         if (sc->sc_evt_buf != NULL) {
711                 kfree(sc->sc_evt_buf, M_USBDEV);
712                 sc->sc_evt_buf = NULL;
713         }
714
715         /* Free all xfers and xfer buffers (implicit) */
716         if (sc->sc_cmd_xfer != NULL) {
717                 usbd_free_xfer(sc->sc_cmd_xfer);
718                 sc->sc_cmd_xfer = NULL;
719                 sc->sc_cmd_buf = NULL;
720         }
721
722         if (sc->sc_aclrd_xfer != NULL) {
723                 usbd_free_xfer(sc->sc_aclrd_xfer);
724                 sc->sc_aclrd_xfer = NULL;
725                 sc->sc_aclrd_buf = NULL;
726         }
727
728         if (sc->sc_aclwr_xfer != NULL) {
729                 usbd_free_xfer(sc->sc_aclwr_xfer);
730                 sc->sc_aclwr_xfer = NULL;
731                 sc->sc_aclwr_buf = NULL;
732         }
733
734         for (i = 0 ; i < UBT_NXFERS ; i++) {
735                 if (sc->sc_scord[i].xfer != NULL) {
736                         usbd_free_xfer(sc->sc_scord[i].xfer);
737                         sc->sc_scord[i].xfer = NULL;
738                         sc->sc_scord[i].buf = NULL;
739                 }
740
741                 if (sc->sc_scowr[i].xfer != NULL) {
742                         usbd_free_xfer(sc->sc_scowr[i].xfer);
743                         sc->sc_scowr[i].xfer = NULL;
744                         sc->sc_scowr[i].buf = NULL;
745                 }
746         }
747
748         /* Free partial SCO packets */
749         if (sc->sc_scord_mbuf != NULL) {
750                 m_freem(sc->sc_scord_mbuf);
751                 sc->sc_scord_mbuf = NULL;
752         }
753
754         if (sc->sc_scowr_mbuf != NULL) {
755                 m_freem(sc->sc_scowr_mbuf);
756                 sc->sc_scowr_mbuf = NULL;
757         }
758
759         /* Empty mbuf queues */
760         IF_DRAIN(&sc->sc_cmd_queue);
761         IF_DRAIN(&sc->sc_aclwr_queue);
762         IF_DRAIN(&sc->sc_scowr_queue);
763
764         crit_exit();    
765 }
766
767 /*******************************************************************************
768  *
769  * Bluetooth Unit/USB callbacks
770  *
771  * All of this will be called at the IPL_ we specified above
772  */
773 int
774 ubt_enable(struct device *self)
775 {
776         struct ubt_softc *sc = device_get_softc(self);
777         usbd_status err;
778         int i, error;
779
780         DPRINTFN(1, "%s: sc=%p\n", __func__, sc);
781
782         if (sc->sc_enabled)
783                 return 0;
784
785         crit_enter();
786
787         /* Events */
788         sc->sc_evt_buf = kmalloc(UBT_BUFSIZ_EVENT, M_USBDEV, M_NOWAIT);
789         if (sc->sc_evt_buf == NULL) {
790                 error = ENOMEM;
791                 goto bad;
792         }
793
794         err = usbd_open_pipe_intr(sc->sc_iface0,
795                                   sc->sc_evt_addr,
796                                   USBD_SHORT_XFER_OK,
797                                   &sc->sc_evt_pipe,
798                                   sc,
799                                   sc->sc_evt_buf,
800                                   UBT_BUFSIZ_EVENT,
801                                   ubt_recv_event,
802                                   UBT_INTR_TIMEOUT);
803         if (err != USBD_NORMAL_COMPLETION) {
804                 error = EIO;
805                 kprintf("can't open events pipe_intr\n");
806                 goto bad;
807         }
808
809         /* Commands */
810         sc->sc_cmd_xfer = usbd_alloc_xfer(sc->sc_udev);
811         if (sc->sc_cmd_xfer == NULL) {
812                 kprintf("can't allocate cmd_xfer\n");
813                 error = ENOMEM;
814                 goto bad;
815         }
816         sc->sc_cmd_buf = usbd_alloc_buffer(sc->sc_cmd_xfer, UBT_BUFSIZ_CMD);
817         if (sc->sc_cmd_buf == NULL) {
818                 kprintf("can't allocate cmd_buf\n");
819                 error = ENOMEM;
820                 goto bad;
821         }
822         sc->sc_cmd_busy = 0;
823
824         /* ACL read */
825         err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclrd_addr,
826                                 USBD_EXCLUSIVE_USE, &sc->sc_aclrd_pipe);
827         if (err != USBD_NORMAL_COMPLETION) {
828                 kprintf("can't open aclrd pipe\n");
829                 error = EIO;
830                 goto bad;
831         }
832         sc->sc_aclrd_xfer = usbd_alloc_xfer(sc->sc_udev);
833         if (sc->sc_aclrd_xfer == NULL) {
834                 kprintf("can't allocate aclrd_xfer\n");
835                 error = ENOMEM;
836                 goto bad;
837         }
838         sc->sc_aclrd_buf = usbd_alloc_buffer(sc->sc_aclrd_xfer, UBT_BUFSIZ_ACL);
839         if (sc->sc_aclrd_buf == NULL) {
840                 kprintf("can't allocate aclrd_buf\n");
841                 error = ENOMEM;
842                 goto bad;
843         }
844         sc->sc_aclrd_busy = 0;
845         ubt_recv_acl_start(sc);
846
847         /* ACL write */
848         err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclwr_addr,
849                                 USBD_EXCLUSIVE_USE, &sc->sc_aclwr_pipe);
850         if (err != USBD_NORMAL_COMPLETION) {
851                 kprintf("can't open aclwr pipe\n");
852                 error = EIO;
853                 goto bad;
854         }
855         sc->sc_aclwr_xfer = usbd_alloc_xfer(sc->sc_udev);
856         if (sc->sc_aclwr_xfer == NULL) {
857                 kprintf("can't allocate aclwr_xfer\n");
858                 error = ENOMEM;
859                 goto bad;
860         }
861         sc->sc_aclwr_buf = usbd_alloc_buffer(sc->sc_aclwr_xfer, UBT_BUFSIZ_ACL);
862         if (sc->sc_aclwr_buf == NULL) {
863                 kprintf("can't allocate aclwr_buf\n");
864                 error = ENOMEM;
865                 goto bad;
866         }
867         sc->sc_aclwr_busy = 0;
868
869         /* SCO read */
870         if (sc->sc_scord_size > 0) {
871                 err = usbd_open_pipe(sc->sc_iface1, sc->sc_scord_addr,
872                                         USBD_EXCLUSIVE_USE, &sc->sc_scord_pipe);
873                 if (err != USBD_NORMAL_COMPLETION) {
874                         error = EIO;
875                         goto bad;
876                 }
877
878                 for (i = 0 ; i < UBT_NXFERS ; i++) {
879                         sc->sc_scord[i].xfer = usbd_alloc_xfer(sc->sc_udev);
880                         if (sc->sc_scord[i].xfer == NULL) {
881                                 error = ENOMEM;
882                                 goto bad;
883                         }
884                         sc->sc_scord[i].buf = usbd_alloc_buffer(sc->sc_scord[i].xfer,
885                                                 sc->sc_scord_size * UBT_NFRAMES);
886                         if (sc->sc_scord[i].buf == NULL) {
887                                 error = ENOMEM;
888                                 goto bad;
889                         }
890                         sc->sc_scord[i].softc = sc;
891                         sc->sc_scord[i].busy = 0;
892                         ubt_recv_sco_start1(sc, &sc->sc_scord[i]);
893                 }
894         }
895
896         /* SCO write */
897         if (sc->sc_scowr_size > 0) {
898                 err = usbd_open_pipe(sc->sc_iface1, sc->sc_scowr_addr,
899                                         USBD_EXCLUSIVE_USE, &sc->sc_scowr_pipe);
900                 if (err != USBD_NORMAL_COMPLETION) {
901                         error = EIO;
902                         goto bad;
903                 }
904
905                 for (i = 0 ; i < UBT_NXFERS ; i++) {
906                         sc->sc_scowr[i].xfer = usbd_alloc_xfer(sc->sc_udev);
907                         if (sc->sc_scowr[i].xfer == NULL) {
908                                 error = ENOMEM;
909                                 goto bad;
910                         }
911                         sc->sc_scowr[i].buf = usbd_alloc_buffer(sc->sc_scowr[i].xfer,
912                                                 sc->sc_scowr_size * UBT_NFRAMES);
913                         if (sc->sc_scowr[i].buf == NULL) {
914                                 error = ENOMEM;
915                                 goto bad;
916                         }
917                         sc->sc_scowr[i].softc = sc;
918                         sc->sc_scowr[i].busy = 0;
919                 }
920
921                 sc->sc_scowr_busy = 0;
922         }
923
924         sc->sc_enabled = 1;
925         crit_exit();
926         return 0;
927
928 bad:
929         ubt_abortdealloc(sc);
930         crit_exit();
931         return error;
932 }
933
934 void
935 ubt_disable(struct device *self)
936 {
937         struct ubt_softc *sc = device_get_softc(self);
938         
939         DPRINTFN(1, "sc=%p\n", sc);
940
941         if (sc->sc_enabled == 0)
942                 return;
943
944         ubt_abortdealloc(sc);
945         sc->sc_enabled = 0;
946 }
947
948 void
949 ubt_xmit_cmd(struct device *self, struct mbuf *m)
950 {
951         struct ubt_softc *sc = device_get_softc(self);
952
953         KKASSERT(sc->sc_enabled);
954
955         crit_enter();
956         IF_ENQUEUE(&sc->sc_cmd_queue, m);
957
958         if (sc->sc_cmd_busy == 0)
959                 ubt_xmit_cmd_start(sc);
960
961         crit_exit();
962 }
963
964 void
965 ubt_xmit_cmd_start(struct ubt_softc *sc)
966 {
967         usb_device_request_t req;
968         usbd_status status;
969         struct mbuf *m;
970         int len;
971
972         if (sc->sc_dying)
973                 return;
974
975         if (IF_QEMPTY(&sc->sc_cmd_queue))
976                 return;
977
978         IF_DEQUEUE(&sc->sc_cmd_queue, m);
979         KKASSERT(m != NULL);
980         DPRINTFN(15, " %s: xmit CMD packet (%d bytes)\n",
981             device_get_nameunit(sc->sc_dev), m->m_pkthdr.len);
982
983         sc->sc_refcnt++;
984
985         sc->sc_cmd_busy = 1;
986
987         len = m->m_pkthdr.len - 1;
988         m_copydata(m, 1, len, sc->sc_cmd_buf);
989         m_freem(m);
990
991         memset(&req, 0, sizeof(req));
992         req.bmRequestType = UT_WRITE_CLASS_DEVICE;
993         USETW(req.wLength, len);
994
995         usbd_setup_default_xfer(sc->sc_cmd_xfer,
996                                 sc->sc_udev,
997                                 sc,
998                                 UBT_CMD_TIMEOUT,
999                                 &req,
1000                                 sc->sc_cmd_buf,
1001                                 len,
1002                                 USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1003                                 ubt_xmit_cmd_complete);
1004
1005         status = usbd_transfer(sc->sc_cmd_xfer);
1006
1007         KKASSERT(status != USBD_NORMAL_COMPLETION);
1008
1009         if (status != USBD_IN_PROGRESS) {
1010                 DPRINTF("usbd_transfer status=%s (%d)\n",
1011                         usbd_errstr(status), status);
1012                 sc->sc_refcnt--;
1013                 sc->sc_cmd_busy = 0;
1014         }
1015         
1016 }
1017
1018 void
1019 ubt_xmit_cmd_complete(usbd_xfer_handle xfer,
1020                         usbd_private_handle h, usbd_status status)
1021 {
1022         struct ubt_softc *sc = h;
1023         uint32_t count;
1024
1025         DPRINTFN(15, " %s: CMD complete status=%s (%d)\n",
1026             device_get_nameunit(sc->sc_dev), usbd_errstr(status), status);
1027
1028         sc->sc_cmd_busy = 0;
1029
1030         if (--sc->sc_refcnt < 0) {
1031                 DPRINTF("sc_refcnt=%d\n", sc->sc_refcnt);
1032                 usb_detach_wakeup(sc->sc_dev);
1033                 return;
1034         }
1035
1036         if (sc->sc_dying) {
1037                 DPRINTF("sc_dying\n");
1038                 return;
1039         }
1040
1041         if (status != USBD_NORMAL_COMPLETION) {
1042                 DPRINTF("status=%s (%d)\n",
1043                         usbd_errstr(status), status);
1044                 sc->sc_stats.err_tx++;
1045                 return;
1046         }
1047
1048         usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1049         sc->sc_stats.cmd_tx++;
1050         sc->sc_stats.byte_tx += count;
1051
1052         ubt_xmit_cmd_start(sc);
1053 }
1054
1055 void
1056 ubt_xmit_acl(struct device *self, struct mbuf *m)
1057 {
1058         struct ubt_softc *sc = device_get_softc(self);
1059
1060         KKASSERT(sc->sc_enabled);
1061
1062         crit_enter();
1063         IF_ENQUEUE(&sc->sc_aclwr_queue, m);
1064
1065         if (sc->sc_aclwr_busy == 0)
1066                 ubt_xmit_acl_start(sc);
1067
1068         crit_exit();
1069 }
1070
1071 void
1072 ubt_xmit_acl_start(struct ubt_softc *sc)
1073 {
1074         struct mbuf *m;
1075         usbd_status status;
1076         int len;
1077
1078         if (sc->sc_dying)
1079                 return;
1080
1081
1082         if (IF_QEMPTY(&sc->sc_aclwr_queue))
1083                 return;
1084
1085         sc->sc_refcnt++;
1086         sc->sc_aclwr_busy = 1;
1087
1088         IF_DEQUEUE(&sc->sc_aclwr_queue, m);
1089         KKASSERT(m != NULL);
1090
1091         DPRINTFN(15, "%s: xmit ACL packet (%d bytes)\n",
1092             device_get_nameunit(sc->sc_dev), m->m_pkthdr.len);
1093
1094         len = m->m_pkthdr.len - 1;
1095         if (len > UBT_BUFSIZ_ACL) {
1096                 DPRINTF("%s: truncating ACL packet (%d => %d)!\n",
1097                     device_get_nameunit(sc->sc_dev), len, UBT_BUFSIZ_ACL);
1098
1099                 len = UBT_BUFSIZ_ACL;
1100         }
1101
1102         m_copydata(m, 1, len, sc->sc_aclwr_buf);
1103         m_freem(m);
1104
1105         sc->sc_stats.acl_tx++;
1106         sc->sc_stats.byte_tx += len;
1107
1108         usbd_setup_xfer(sc->sc_aclwr_xfer,
1109                         sc->sc_aclwr_pipe,
1110                         sc,
1111                         sc->sc_aclwr_buf,
1112                         len,
1113                         USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1114                         UBT_ACL_TIMEOUT,
1115                         ubt_xmit_acl_complete);
1116
1117         status = usbd_transfer(sc->sc_aclwr_xfer);
1118
1119         KKASSERT(status != USBD_NORMAL_COMPLETION);
1120
1121         if (status != USBD_IN_PROGRESS) {
1122                 DPRINTF("usbd_transfer status=%s (%d)\n",
1123                         usbd_errstr(status), status);
1124
1125                 sc->sc_refcnt--;
1126                 sc->sc_aclwr_busy = 0;
1127         }
1128         
1129 }
1130
1131 void
1132 ubt_xmit_acl_complete(usbd_xfer_handle xfer,
1133                 usbd_private_handle h, usbd_status status)
1134 {
1135         struct ubt_softc *sc = h;
1136
1137         DPRINTFN(15, "%s: ACL complete status=%s (%d)\n",
1138             device_get_nameunit(sc->sc_dev), usbd_errstr(status), status);
1139
1140         sc->sc_aclwr_busy = 0;
1141
1142         if (--sc->sc_refcnt < 0) {
1143                 usb_detach_wakeup(sc->sc_dev);
1144                 return;
1145         }
1146
1147         if (sc->sc_dying)
1148                 return;
1149
1150         if (status != USBD_NORMAL_COMPLETION) {
1151                 DPRINTF("status=%s (%d)\n",
1152                         usbd_errstr(status), status);
1153
1154                 sc->sc_stats.err_tx++;
1155
1156                 if (status == USBD_STALLED)
1157                         usbd_clear_endpoint_stall_async(sc->sc_aclwr_pipe);
1158                 else
1159                         return;
1160         }
1161
1162         ubt_xmit_acl_start(sc);
1163         
1164 }
1165
1166 void
1167 ubt_xmit_sco(struct device *self, struct mbuf *m)
1168 {
1169         struct ubt_softc *sc = device_get_softc(self);
1170
1171         KKASSERT(sc->sc_enabled);
1172
1173         crit_enter();
1174         IF_ENQUEUE(&sc->sc_scowr_queue, m);
1175
1176         if (sc->sc_scowr_busy == 0)
1177                 ubt_xmit_sco_start(sc);
1178
1179         crit_exit();
1180 }
1181
1182 void
1183 ubt_xmit_sco_start(struct ubt_softc *sc)
1184 {
1185         int i;
1186
1187         if (sc->sc_dying || sc->sc_scowr_size == 0)
1188                 return;
1189
1190         for (i = 0 ; i < UBT_NXFERS ; i++) {
1191                 if (sc->sc_scowr[i].busy)
1192                         continue;
1193
1194                 ubt_xmit_sco_start1(sc, &sc->sc_scowr[i]);
1195         }
1196 }
1197
1198 void
1199 ubt_xmit_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
1200 {
1201         struct mbuf *m;
1202         uint8_t *buf;
1203         int num, len, size, space;
1204
1205         if (sc->sc_dying)
1206                 return;
1207
1208         space = sc->sc_scowr_size * UBT_NFRAMES;
1209         buf = isoc->buf;
1210         len = 0;
1211
1212         /*
1213          * Fill the request buffer with data from the queue,
1214          * keeping any leftover packet on our private hook.
1215          *
1216          * Complete packets are passed back up to the stack
1217          * for disposal, since we can't rely on the controller
1218          * to tell us when it has finished with them.
1219          */
1220
1221         m = sc->sc_scowr_mbuf;
1222         while (space > 0) {
1223                 if (m == NULL) {
1224                         crit_enter();
1225                         IF_DEQUEUE(&sc->sc_scowr_queue, m);
1226                         crit_exit();
1227                         if (m == NULL)
1228                                 break;
1229
1230                         m_adj(m, 1);    /* packet type */
1231
1232                 }
1233
1234                 if (m->m_pkthdr.len > 0) {
1235                         size = MIN(m->m_pkthdr.len, space);
1236
1237                         m_copydata(m, 0, size, buf);
1238                         m_adj(m, size);
1239
1240                         buf += size;
1241                         len += size;
1242                         space -= size;
1243                 }
1244
1245                 if (m->m_pkthdr.len == 0) {
1246                         sc->sc_stats.sco_tx++;
1247                         if (!hci_complete_sco(sc->sc_unit, m))
1248                                 sc->sc_stats.err_tx++;
1249
1250                         m = NULL;
1251                 }
1252         }
1253         sc->sc_scowr_mbuf = m;
1254
1255         DPRINTFN(15, "isoc=%p, len=%d, space=%d\n", isoc, len, space);
1256
1257         if (len == 0)   /* nothing to send */
1258         
1259                 return;
1260
1261         sc->sc_refcnt++;
1262         sc->sc_scowr_busy = 1;
1263         sc->sc_stats.byte_tx += len;
1264         isoc->busy = 1;
1265
1266         /*
1267          * calculate number of isoc frames and sizes
1268          */
1269
1270         for (num = 0 ; len > 0 ; num++) {
1271                 size = MIN(sc->sc_scowr_size, len);
1272
1273                 isoc->size[num] = size;
1274                 len -= size;
1275         }
1276
1277         usbd_setup_isoc_xfer(isoc->xfer,
1278                              sc->sc_scowr_pipe,
1279                              isoc,
1280                              isoc->size,
1281                              num,
1282                              USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1283                              ubt_xmit_sco_complete);
1284
1285         usbd_transfer(isoc->xfer);
1286 }
1287
1288 void
1289 ubt_xmit_sco_complete(usbd_xfer_handle xfer,
1290                 usbd_private_handle h, usbd_status status)
1291 {
1292         struct ubt_isoc_xfer *isoc = h;
1293         struct ubt_softc *sc;
1294         int i;
1295
1296         KKASSERT(xfer == isoc->xfer);
1297         sc = isoc->softc;
1298
1299         DPRINTFN(15, "isoc=%p, status=%s (%d)\n",
1300                 isoc, usbd_errstr(status), status);
1301
1302         isoc->busy = 0;
1303
1304         for (i = 0 ; ; i++) {
1305                 if (i == UBT_NXFERS) {
1306                         sc->sc_scowr_busy = 0;
1307                         break;
1308                 }
1309
1310                 if (sc->sc_scowr[i].busy)
1311                         break;
1312         }
1313
1314         if (--sc->sc_refcnt < 0) {
1315                 usb_detach_wakeup(sc->sc_dev);
1316                 return;
1317         }
1318
1319         if (sc->sc_dying)
1320                 return;
1321
1322         if (status != USBD_NORMAL_COMPLETION) {
1323                 DPRINTF("status=%s (%d)\n",
1324                         usbd_errstr(status), status);
1325
1326                 sc->sc_stats.err_tx++;
1327
1328                 if (status == USBD_STALLED)
1329                         usbd_clear_endpoint_stall_async(sc->sc_scowr_pipe);
1330                 else
1331                         return;
1332         }
1333
1334         ubt_xmit_sco_start(sc);
1335 }
1336
1337 /*
1338  * Load incoming data into an mbuf with leading type byte.
1339  */
1340 static struct mbuf *
1341 ubt_mbufload(uint8_t *buf, int count, uint8_t type)
1342 {
1343         struct mbuf *m;
1344         MGETHDR(m, MB_DONTWAIT, MT_DATA);
1345         if (m == NULL) {kprintf(" MGETHDR return NULL\n");
1346                 return NULL; }
1347         *mtod(m, uint8_t *) = type;
1348         m->m_pkthdr.len = m->m_len = MHLEN;
1349         m_copyback(m, 1, count, buf);   /* (extends if necessary)*/
1350         if (m->m_pkthdr.len != MAX(MHLEN, count + 1)) {
1351                 m_free(m);
1352                 kprintf(" m->m_pkthdr.len != MAX() \n");
1353                 return NULL;
1354         }
1355         m->m_pkthdr.len = count + 1;
1356         m->m_len = MIN(MHLEN, m->m_pkthdr.len);
1357         return m;
1358 }
1359
1360 void
1361 ubt_recv_event(usbd_xfer_handle xfer, usbd_private_handle h, usbd_status status)
1362 {
1363         struct ubt_softc *sc = h;
1364         struct mbuf *m;
1365         uint32_t count;
1366         void *buf;
1367
1368         DPRINTFN(15, "sc=%p status=%s (%d)\n",
1369                     sc, usbd_errstr(status), status);
1370
1371         if (status != USBD_NORMAL_COMPLETION || sc->sc_dying)
1372                 return;
1373
1374         usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);
1375
1376         if (count < sizeof(hci_event_hdr_t) - 1) {
1377                 DPRINTF("dumped undersized event (count = %d)\n", count);
1378                 sc->sc_stats.err_rx++;
1379                 return;
1380         }
1381
1382         sc->sc_stats.evt_rx++;
1383         sc->sc_stats.byte_rx += count;
1384
1385         m = ubt_mbufload(buf, count, HCI_EVENT_PKT);
1386         if (m == NULL || !hci_input_event(sc->sc_unit, m))
1387                 sc->sc_stats.err_rx++;
1388 }
1389
1390 void
1391 ubt_recv_acl_start(struct ubt_softc *sc)
1392 {
1393         usbd_status status;
1394
1395         DPRINTFN(15, "sc=%p\n", sc);
1396
1397         if (sc->sc_aclrd_busy || sc->sc_dying) {
1398                 DPRINTF("sc_aclrd_busy=%d, sc_dying=%d\n",
1399                         sc->sc_aclrd_busy,
1400                         sc->sc_dying);
1401
1402                 return;
1403         }
1404
1405         sc->sc_refcnt++;
1406         sc->sc_aclrd_busy = 1;
1407
1408         usbd_setup_xfer(sc->sc_aclrd_xfer,
1409                         sc->sc_aclrd_pipe,
1410                         sc,
1411                         sc->sc_aclrd_buf,
1412                         UBT_BUFSIZ_ACL,
1413                         USBD_NO_COPY | USBD_SHORT_XFER_OK,
1414                         USBD_NO_TIMEOUT,
1415                         ubt_recv_acl_complete);
1416
1417         status = usbd_transfer(sc->sc_aclrd_xfer);
1418
1419         KKASSERT(status != USBD_NORMAL_COMPLETION);
1420
1421         if (status != USBD_IN_PROGRESS) {
1422                 DPRINTF("usbd_transfer status=%s (%d)\n",
1423                         usbd_errstr(status), status);
1424
1425                 sc->sc_refcnt--;
1426                 sc->sc_aclrd_busy = 0;
1427         }
1428 }
1429
1430 void
1431 ubt_recv_acl_complete(usbd_xfer_handle xfer,
1432                 usbd_private_handle h, usbd_status status)
1433 {
1434         struct ubt_softc *sc = h;
1435         struct mbuf *m;
1436         uint32_t count;
1437         void *buf;
1438
1439         DPRINTFN(15, "sc=%p status=%s (%d)\n",
1440                         sc, usbd_errstr(status), status);
1441
1442         sc->sc_aclrd_busy = 0;
1443
1444         if (--sc->sc_refcnt < 0) {
1445                 DPRINTF("refcnt = %d\n", sc->sc_refcnt);
1446                 usb_detach_wakeup(sc->sc_dev);
1447                 return;
1448         }
1449
1450         if (sc->sc_dying) {
1451                 DPRINTF("sc_dying\n");
1452                 return;
1453         }
1454
1455         if (status != USBD_NORMAL_COMPLETION) {
1456                 DPRINTF("status=%s (%d)\n",
1457                         usbd_errstr(status), status);
1458
1459                 sc->sc_stats.err_rx++;
1460
1461                 if (status == USBD_STALLED)
1462                         usbd_clear_endpoint_stall_async(sc->sc_aclrd_pipe);
1463                 else
1464                         return;
1465         } else {
1466                 usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);
1467
1468                 if (count < sizeof(hci_acldata_hdr_t) - 1) {
1469                         DPRINTF("dumped undersized packet (%d)\n", count);
1470                         sc->sc_stats.err_rx++;
1471                 } else {
1472                         sc->sc_stats.acl_rx++;
1473                         sc->sc_stats.byte_rx += count;
1474
1475                         m = ubt_mbufload(buf, count, HCI_ACL_DATA_PKT);
1476                         if (m == NULL || !hci_input_acl(sc->sc_unit, m))
1477                                 sc->sc_stats.err_rx++;
1478                 }
1479         }
1480
1481         /* and restart */
1482         ubt_recv_acl_start(sc);
1483 }
1484
1485 void
1486 ubt_recv_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
1487 {
1488         int i;
1489
1490         DPRINTFN(15, "sc=%p, isoc=%p\n", sc, isoc);
1491
1492         if (isoc->busy || sc->sc_dying || sc->sc_scord_size == 0) {
1493                 DPRINTF("%s%s%s\n",
1494                         isoc->busy ? " busy" : "",
1495                         sc->sc_dying ? " dying" : "",
1496                         sc->sc_scord_size == 0 ? " size=0" : "");
1497
1498                 return;
1499         }
1500
1501         sc->sc_refcnt++;
1502         isoc->busy = 1;
1503
1504         for (i = 0 ; i < UBT_NFRAMES ; i++)
1505                 isoc->size[i] = sc->sc_scord_size;
1506
1507         usbd_setup_isoc_xfer(isoc->xfer,
1508                              sc->sc_scord_pipe,
1509                              isoc,
1510                              isoc->size,
1511                              UBT_NFRAMES,
1512                              USBD_NO_COPY | USBD_SHORT_XFER_OK,
1513                              ubt_recv_sco_complete);
1514
1515         usbd_transfer(isoc->xfer);
1516 }
1517
1518 void
1519 ubt_recv_sco_complete(usbd_xfer_handle xfer,
1520                 usbd_private_handle h, usbd_status status)
1521 {
1522         struct ubt_isoc_xfer *isoc = h;
1523         struct ubt_softc *sc;
1524         struct mbuf *m;
1525         uint32_t count;
1526         uint8_t *ptr, *frame;
1527         int i, size, got, want;
1528
1529         KKASSERT(isoc != NULL);
1530         KKASSERT(isoc->xfer == xfer);
1531
1532         sc = isoc->softc;
1533         isoc->busy = 0;
1534
1535         if (--sc->sc_refcnt < 0) {
1536                 DPRINTF("refcnt=%d\n", sc->sc_refcnt);
1537                 usb_detach_wakeup(sc->sc_dev);
1538                 return;
1539         }
1540
1541         if (sc->sc_dying) {
1542                 DPRINTF("sc_dying\n");
1543                 return;
1544         }
1545
1546         if (status != USBD_NORMAL_COMPLETION) {
1547                 DPRINTF("status=%s (%d)\n",
1548                         usbd_errstr(status), status);
1549
1550                 sc->sc_stats.err_rx++;
1551
1552                 if (status == USBD_STALLED) {
1553                         usbd_clear_endpoint_stall_async(sc->sc_scord_pipe);
1554                         goto restart;
1555                 }
1556
1557                 return;
1558         }
1559
1560         usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1561         if (count == 0)
1562                 goto restart;
1563
1564         DPRINTFN(15, "sc=%p, isoc=%p, count=%u\n",
1565                         sc, isoc, count);
1566
1567         sc->sc_stats.byte_rx += count;
1568
1569         /*
1570          * Extract SCO packets from ISOC frames. The way we have it,
1571          * no SCO packet can be bigger than MHLEN. This is unlikely
1572          * to actually happen, but if we ran out of mbufs and lost
1573          * sync then we may get spurious data that makes it seem that
1574          * way, so we discard data that wont fit. This doesnt really
1575          * help with the lost sync situation alas.
1576          */
1577
1578         m = sc->sc_scord_mbuf;
1579         if (m != NULL) {
1580                 sc->sc_scord_mbuf = NULL;
1581                 ptr = mtod(m, uint8_t *) + m->m_pkthdr.len;
1582                 got = m->m_pkthdr.len;
1583                 want = sizeof(hci_scodata_hdr_t);
1584                 if (got >= want)
1585                         want += mtod(m, hci_scodata_hdr_t *)->length ;
1586         } else {
1587                 ptr = NULL;
1588                 got = 0;
1589                 want = 0;
1590         }
1591
1592         for (i = 0 ; i < UBT_NFRAMES ; i++) {
1593                 frame = isoc->buf + (i * sc->sc_scord_size);
1594
1595                 while (isoc->size[i] > 0) {
1596                         size = isoc->size[i];
1597
1598                         if (m == NULL) {
1599                                 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1600                                 if (m == NULL) {
1601                                         kprintf("%s: out of memory (xfer halted)\n",
1602                                                 device_get_nameunit(sc->sc_dev));
1603
1604                                         sc->sc_stats.err_rx++;
1605                                         return;         /* lost sync */
1606                                 }
1607
1608                                 ptr = mtod(m, uint8_t *);
1609                                 *ptr++ = HCI_SCO_DATA_PKT;
1610                                 got = 1;
1611                                 want = sizeof(hci_scodata_hdr_t);
1612                         }
1613
1614                         if (got + size > want)
1615                                 size = want - got;
1616
1617                         if (got + size > MHLEN)
1618                                 memcpy(ptr, frame, MHLEN - got);
1619                         else
1620                                 memcpy(ptr, frame, size);
1621
1622                         ptr += size;
1623                         got += size;
1624                         frame += size;
1625
1626                         if (got == want) {
1627                                 /*
1628                                  * If we only got a header, add the packet
1629                                  * length to our want count. Send complete
1630                                  * packets up to protocol stack.
1631                                  */
1632                                 if (want == sizeof(hci_scodata_hdr_t))
1633                                         want += mtod(m, hci_scodata_hdr_t *)->length;
1634
1635                                 if (got == want) {
1636                                         m->m_pkthdr.len = m->m_len = got;
1637                                         sc->sc_stats.sco_rx++;
1638                                         if (!hci_input_sco(sc->sc_unit, m))
1639                                                 sc->sc_stats.err_rx++;
1640                                                 
1641                                         m = NULL;
1642                                 }
1643                         }
1644
1645                         isoc->size[i] -= size;
1646                 }
1647         }
1648
1649         if (m != NULL) {
1650                 m->m_pkthdr.len = m->m_len = got;
1651                 sc->sc_scord_mbuf = m;
1652         }
1653
1654 restart: /* and restart */
1655         ubt_recv_sco_start1(sc, isoc);
1656 }
1657
1658 void
1659 ubt_stats(struct device *self, struct bt_stats *dest, int flush)
1660 {
1661         struct ubt_softc *sc = device_get_softc(self);
1662
1663         crit_enter();
1664         memcpy(dest, &sc->sc_stats, sizeof(struct bt_stats));
1665
1666         if (flush)
1667                 memset(&sc->sc_stats, 0, sizeof(struct bt_stats));
1668
1669         crit_exit();
1670 }