6 * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: ubtbcmfw.c,v 1.3 2003/10/10 19:15:08 max Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/filio.h>
39 #include <sys/fcntl.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
44 #include <sys/sysctl.h>
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdi_util.h>
54 * Download firmware to BCM2033.
57 #define UBTBCMFW_CONFIG_NO 1 /* Config number */
58 #define UBTBCMFW_IFACE_IDX 0 /* Control interface */
59 #define UBTBCMFW_INTR_IN_EP 0x81 /* Fixed endpoint */
60 #define UBTBCMFW_BULK_OUT_EP 0x02 /* Fixed endpoint */
61 #define UBTBCMFW_INTR_IN UE_GET_ADDR(UBTBCMFW_INTR_IN_EP)
62 #define UBTBCMFW_BULK_OUT UE_GET_ADDR(UBTBCMFW_BULK_OUT_EP)
64 struct ubtbcmfw_softc {
65 device_t sc_dev; /* base device */
66 usbd_device_handle sc_udev; /* USB device handle */
67 struct cdev *sc_ctrl_dev; /* control device */
68 struct cdev *sc_intr_in_dev; /* interrupt device */
69 struct cdev *sc_bulk_out_dev; /* bulk device */
70 usbd_pipe_handle sc_intr_in_pipe; /* interrupt pipe */
71 usbd_pipe_handle sc_bulk_out_pipe; /* bulk out pipe */
73 #define UBTBCMFW_CTRL_DEV (1 << 0)
74 #define UBTBCMFW_INTR_IN_DEV (1 << 1)
75 #define UBTBCMFW_BULK_OUT_DEV (1 << 2)
80 typedef struct ubtbcmfw_softc *ubtbcmfw_softc_p;
86 #define UBTBCMFW_UNIT(n) ((minor(n) >> 4) & 0xf)
87 #define UBTBCMFW_ENDPOINT(n) (minor(n) & 0xf)
88 #define UBTBCMFW_MINOR(u, e) (((u) << 4) | (e))
89 #define UBTBCMFW_BSIZE 1024
91 static d_open_t ubtbcmfw_open;
92 static d_close_t ubtbcmfw_close;
93 static d_read_t ubtbcmfw_read;
94 static d_write_t ubtbcmfw_write;
95 static d_ioctl_t ubtbcmfw_ioctl;
96 static d_poll_t ubtbcmfw_poll;
98 static struct cdevsw ubtbcmfw_cdevsw = {
99 .d_version = D_VERSION,
100 .d_flags = D_NEEDGIANT,
101 .d_open = ubtbcmfw_open,
102 .d_close = ubtbcmfw_close,
103 .d_read = ubtbcmfw_read,
104 .d_write = ubtbcmfw_write,
105 .d_ioctl = ubtbcmfw_ioctl,
106 .d_poll = ubtbcmfw_poll,
107 .d_name = "ubtbcmfw",
114 static device_probe_t ubtbcmfw_match;
115 static device_attach_t ubtbcmfw_attach;
116 static device_detach_t ubtbcmfw_detach;
118 static device_method_t ubtbcmfw_methods[] = {
119 /* Device interface */
120 DEVMETHOD(device_probe, ubtbcmfw_match),
121 DEVMETHOD(device_attach, ubtbcmfw_attach),
122 DEVMETHOD(device_detach, ubtbcmfw_detach),
127 static driver_t ubtbcmfw_driver = {
130 sizeof(struct ubtbcmfw_softc)
133 static devclass_t ubtbcmfw_devclass;
134 DRIVER_MODULE(ubtbcmfw, uhub, ubtbcmfw_driver, ubtbcmfw_devclass,
135 usbd_driver_load, 0);
138 * Probe for a USB Bluetooth device
142 ubtbcmfw_match(device_t self)
144 #define USB_PRODUCT_BROADCOM_BCM2033NF 0x2033
145 struct usb_attach_arg *uaa = device_get_ivars(self);
147 if (uaa->iface != NULL)
148 return (UMATCH_NONE);
150 /* Match the boot device. */
151 if (uaa->vendor == USB_VENDOR_BROADCOM &&
152 uaa->product == USB_PRODUCT_BROADCOM_BCM2033NF)
153 return (UMATCH_VENDOR_PRODUCT);
155 return (UMATCH_NONE);
163 ubtbcmfw_attach(device_t self)
165 struct ubtbcmfw_softc *sc = device_get_softc(self);
166 struct usb_attach_arg *uaa = device_get_ivars(self);
167 usbd_interface_handle iface;
171 sc->sc_udev = uaa->device;
173 sc->sc_ctrl_dev = sc->sc_intr_in_dev = sc->sc_bulk_out_dev = NULL;
174 sc->sc_intr_in_pipe = sc->sc_bulk_out_pipe = NULL;
175 sc->sc_flags = sc->sc_refcnt = sc->sc_dying = 0;
177 err = usbd_set_config_no(sc->sc_udev, UBTBCMFW_CONFIG_NO, 1);
179 printf("%s: setting config no failed. %s\n",
180 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
184 err = usbd_device2interface_handle(sc->sc_udev, UBTBCMFW_IFACE_IDX,
187 printf("%s: getting interface handle failed. %s\n",
188 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
192 /* Will be used as a bulk pipe */
193 err = usbd_open_pipe(iface, UBTBCMFW_INTR_IN_EP, 0,
194 &sc->sc_intr_in_pipe);
196 printf("%s: open intr in failed. %s\n",
197 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
201 err = usbd_open_pipe(iface, UBTBCMFW_BULK_OUT_EP, 0,
202 &sc->sc_bulk_out_pipe);
204 printf("%s: open bulk out failed. %s\n",
205 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
209 /* Create device nodes */
210 sc->sc_ctrl_dev = make_dev(&ubtbcmfw_cdevsw,
211 UBTBCMFW_MINOR(device_get_unit(sc->sc_dev), 0),
212 UID_ROOT, GID_OPERATOR, 0644,
213 "%s", device_get_nameunit(sc->sc_dev));
215 sc->sc_intr_in_dev = make_dev(&ubtbcmfw_cdevsw,
216 UBTBCMFW_MINOR(device_get_unit(sc->sc_dev), UBTBCMFW_INTR_IN),
217 UID_ROOT, GID_OPERATOR, 0644,
218 "%s.%d", device_get_nameunit(sc->sc_dev), UBTBCMFW_INTR_IN);
220 sc->sc_bulk_out_dev = make_dev(&ubtbcmfw_cdevsw,
221 UBTBCMFW_MINOR(device_get_unit(sc->sc_dev), UBTBCMFW_BULK_OUT),
222 UID_ROOT, GID_OPERATOR, 0644,
223 "%s.%d", device_get_nameunit(sc->sc_dev), UBTBCMFW_BULK_OUT);
227 ubtbcmfw_detach(self);
236 ubtbcmfw_detach(device_t self)
238 struct ubtbcmfw_softc *sc = device_get_softc(self);
241 if (-- sc->sc_refcnt >= 0) {
242 if (sc->sc_intr_in_pipe != NULL)
243 usbd_abort_pipe(sc->sc_intr_in_pipe);
245 if (sc->sc_bulk_out_pipe != NULL)
246 usbd_abort_pipe(sc->sc_bulk_out_pipe);
248 usb_detach_wait(sc->sc_dev);
251 /* Destroy device nodes */
252 if (sc->sc_bulk_out_dev != NULL) {
253 destroy_dev(sc->sc_bulk_out_dev);
254 sc->sc_bulk_out_dev = NULL;
257 if (sc->sc_intr_in_dev != NULL) {
258 destroy_dev(sc->sc_intr_in_dev);
259 sc->sc_intr_in_dev = NULL;
262 if (sc->sc_ctrl_dev != NULL) {
263 destroy_dev(sc->sc_ctrl_dev);
264 sc->sc_ctrl_dev = NULL;
268 if (sc->sc_intr_in_pipe != NULL) {
269 usbd_close_pipe(sc->sc_intr_in_pipe);
270 sc->sc_intr_in_pipe = NULL;
273 if (sc->sc_bulk_out_pipe != NULL) {
274 usbd_close_pipe(sc->sc_bulk_out_pipe);
275 sc->sc_intr_in_pipe = NULL;
282 * Open endpoint device
283 * XXX FIXME softc locking
287 ubtbcmfw_open(struct cdev *dev, int flag, int mode, struct thread *p)
289 ubtbcmfw_softc_p sc = NULL;
292 /* checks for sc != NULL */
293 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
299 switch (UBTBCMFW_ENDPOINT(dev)) {
300 case USB_CONTROL_ENDPOINT:
301 if (!(sc->sc_flags & UBTBCMFW_CTRL_DEV))
302 sc->sc_flags |= UBTBCMFW_CTRL_DEV;
307 case UBTBCMFW_INTR_IN:
308 if (!(sc->sc_flags & UBTBCMFW_INTR_IN_DEV)) {
309 if (sc->sc_intr_in_pipe != NULL)
310 sc->sc_flags |= UBTBCMFW_INTR_IN_DEV;
317 case UBTBCMFW_BULK_OUT:
318 if (!(sc->sc_flags & UBTBCMFW_BULK_OUT_DEV)) {
319 if (sc->sc_bulk_out_pipe != NULL)
320 sc->sc_flags |= UBTBCMFW_BULK_OUT_DEV;
336 * Close endpoint device
337 * XXX FIXME softc locking
341 ubtbcmfw_close(struct cdev *dev, int flag, int mode, struct thread *p)
343 ubtbcmfw_softc_p sc = NULL;
345 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
349 switch (UBTBCMFW_ENDPOINT(dev)) {
350 case USB_CONTROL_ENDPOINT:
351 sc->sc_flags &= ~UBTBCMFW_CTRL_DEV;
354 case UBTBCMFW_INTR_IN:
355 if (sc->sc_intr_in_pipe != NULL)
356 usbd_abort_pipe(sc->sc_intr_in_pipe);
358 sc->sc_flags &= ~UBTBCMFW_INTR_IN_DEV;
361 case UBTBCMFW_BULK_OUT:
362 if (sc->sc_bulk_out_pipe != NULL)
363 usbd_abort_pipe(sc->sc_bulk_out_pipe);
365 sc->sc_flags &= ~UBTBCMFW_BULK_OUT_DEV;
373 * Read from the endpoint device
374 * XXX FIXME softc locking
378 ubtbcmfw_read(struct cdev *dev, struct uio *uio, int flag)
380 ubtbcmfw_softc_p sc = NULL;
381 u_int8_t buf[UBTBCMFW_BSIZE];
382 usbd_xfer_handle xfer;
384 int n, tn, error = 0;
386 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
387 if (sc == NULL || sc->sc_dying)
390 if (UBTBCMFW_ENDPOINT(dev) != UBTBCMFW_INTR_IN)
392 if (sc->sc_intr_in_pipe == NULL)
395 xfer = usbd_alloc_xfer(sc->sc_udev);
401 while ((n = min(sizeof(buf), uio->uio_resid)) != 0) {
403 err = usbd_bulk_transfer(xfer, sc->sc_intr_in_pipe,
404 USBD_SHORT_XFER_OK, USBD_DEFAULT_TIMEOUT,
407 case USBD_NORMAL_COMPLETION:
408 error = uiomove(buf, tn, uio);
411 case USBD_INTERRUPTED:
424 if (error != 0 || tn < n)
428 usbd_free_xfer(xfer);
430 if (-- sc->sc_refcnt < 0)
431 usb_detach_wakeup(sc->sc_dev);
437 * Write into the endpoint device
438 * XXX FIXME softc locking
442 ubtbcmfw_write(struct cdev *dev, struct uio *uio, int flag)
444 ubtbcmfw_softc_p sc = NULL;
445 u_int8_t buf[UBTBCMFW_BSIZE];
446 usbd_xfer_handle xfer;
450 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
451 if (sc == NULL || sc->sc_dying)
454 if (UBTBCMFW_ENDPOINT(dev) != UBTBCMFW_BULK_OUT)
456 if (sc->sc_bulk_out_pipe == NULL)
459 xfer = usbd_alloc_xfer(sc->sc_udev);
465 while ((n = min(sizeof(buf), uio->uio_resid)) != 0) {
466 error = uiomove(buf, n, uio);
470 err = usbd_bulk_transfer(xfer, sc->sc_bulk_out_pipe,
471 0, USBD_DEFAULT_TIMEOUT, buf, &n, "bcmwr");
473 case USBD_NORMAL_COMPLETION:
476 case USBD_INTERRUPTED:
493 usbd_free_xfer(xfer);
495 if (-- sc->sc_refcnt < 0)
496 usb_detach_wakeup(sc->sc_dev);
502 * Process ioctl on the endpoint device
503 * XXX FIXME softc locking
507 ubtbcmfw_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
510 ubtbcmfw_softc_p sc = NULL;
513 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
514 if (sc == NULL || sc->sc_dying)
517 if (UBTBCMFW_ENDPOINT(dev) != USB_CONTROL_ENDPOINT)
523 case USB_GET_DEVICE_DESC:
524 *(usb_device_descriptor_t *) data =
525 *usbd_get_device_descriptor(sc->sc_udev);
533 if (-- sc->sc_refcnt < 0)
534 usb_detach_wakeup(sc->sc_dev);
540 * Poll the endpoint device
541 * XXX FIXME softc locking
545 ubtbcmfw_poll(struct cdev *dev, int events, struct thread *p)
547 ubtbcmfw_softc_p sc = NULL;
550 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
554 switch (UBTBCMFW_ENDPOINT(dev)) {
555 case UBTBCMFW_INTR_IN:
556 if (sc->sc_intr_in_pipe != NULL)
557 revents |= events & (POLLIN | POLLRDNORM);
562 case UBTBCMFW_BULK_OUT:
563 if (sc->sc_bulk_out_pipe != NULL)
564 revents |= events & (POLLOUT | POLLWRNORM);
570 revents = EOPNOTSUPP;