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 $
31 * $FreeBSD: src/sys/netgraph/bluetooth/drivers/ubtbcmfw/ubtbcmfw.c,v 1.18 2007/06/23 04:34:38 imp Exp $
32 * $DragonFly: src/sys/netgraph7/bluetooth/drivers/ubtbcmfw/ubtbcmfw.c,v 1.2 2008/06/26 23:05:40 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/filio.h>
40 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
45 #include <sys/sysctl.h>
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdi_util.h>
55 * Download firmware to BCM2033.
58 #define UBTBCMFW_CONFIG_NO 1 /* Config number */
59 #define UBTBCMFW_IFACE_IDX 0 /* Control interface */
60 #define UBTBCMFW_INTR_IN_EP 0x81 /* Fixed endpoint */
61 #define UBTBCMFW_BULK_OUT_EP 0x02 /* Fixed endpoint */
62 #define UBTBCMFW_INTR_IN UE_GET_ADDR(UBTBCMFW_INTR_IN_EP)
63 #define UBTBCMFW_BULK_OUT UE_GET_ADDR(UBTBCMFW_BULK_OUT_EP)
65 struct ubtbcmfw_softc {
66 device_t sc_dev; /* base device */
67 usbd_device_handle sc_udev; /* USB device handle */
68 struct cdev *sc_ctrl_dev; /* control device */
69 struct cdev *sc_intr_in_dev; /* interrupt device */
70 struct cdev *sc_bulk_out_dev; /* bulk device */
71 usbd_pipe_handle sc_intr_in_pipe; /* interrupt pipe */
72 usbd_pipe_handle sc_bulk_out_pipe; /* bulk out pipe */
74 #define UBTBCMFW_CTRL_DEV (1 << 0)
75 #define UBTBCMFW_INTR_IN_DEV (1 << 1)
76 #define UBTBCMFW_BULK_OUT_DEV (1 << 2)
81 typedef struct ubtbcmfw_softc *ubtbcmfw_softc_p;
87 #define UBTBCMFW_UNIT(n) ((minor(n) >> 4) & 0xf)
88 #define UBTBCMFW_ENDPOINT(n) (minor(n) & 0xf)
89 #define UBTBCMFW_MINOR(u, e) (((u) << 4) | (e))
90 #define UBTBCMFW_BSIZE 1024
92 static d_open_t ubtbcmfw_open;
93 static d_close_t ubtbcmfw_close;
94 static d_read_t ubtbcmfw_read;
95 static d_write_t ubtbcmfw_write;
96 static d_ioctl_t ubtbcmfw_ioctl;
97 static d_poll_t ubtbcmfw_poll;
99 static struct cdevsw ubtbcmfw_cdevsw = {
100 .d_version = D_VERSION,
101 .d_flags = D_NEEDGIANT,
102 .d_open = ubtbcmfw_open,
103 .d_close = ubtbcmfw_close,
104 .d_read = ubtbcmfw_read,
105 .d_write = ubtbcmfw_write,
106 .d_ioctl = ubtbcmfw_ioctl,
107 .d_poll = ubtbcmfw_poll,
108 .d_name = "ubtbcmfw",
115 static device_probe_t ubtbcmfw_match;
116 static device_attach_t ubtbcmfw_attach;
117 static device_detach_t ubtbcmfw_detach;
119 static device_method_t ubtbcmfw_methods[] = {
120 /* Device interface */
121 DEVMETHOD(device_probe, ubtbcmfw_match),
122 DEVMETHOD(device_attach, ubtbcmfw_attach),
123 DEVMETHOD(device_detach, ubtbcmfw_detach),
128 static driver_t ubtbcmfw_driver = {
131 sizeof(struct ubtbcmfw_softc)
134 static devclass_t ubtbcmfw_devclass;
136 MODULE_DEPEND(ubtbcmfw, usb, 1, 1, 1);
137 DRIVER_MODULE(ubtbcmfw, uhub, ubtbcmfw_driver, ubtbcmfw_devclass,
138 usbd_driver_load, 0);
141 * Probe for a USB Bluetooth device
145 ubtbcmfw_match(device_t self)
147 #define USB_PRODUCT_BROADCOM_BCM2033NF 0x2033
148 struct usb_attach_arg *uaa = device_get_ivars(self);
150 if (uaa->iface != NULL)
151 return (UMATCH_NONE);
153 /* Match the boot device. */
154 if (uaa->vendor == USB_VENDOR_BROADCOM &&
155 uaa->product == USB_PRODUCT_BROADCOM_BCM2033NF)
156 return (UMATCH_VENDOR_PRODUCT);
158 return (UMATCH_NONE);
166 ubtbcmfw_attach(device_t self)
168 struct ubtbcmfw_softc *sc = device_get_softc(self);
169 struct usb_attach_arg *uaa = device_get_ivars(self);
170 usbd_interface_handle iface;
174 sc->sc_udev = uaa->device;
176 sc->sc_ctrl_dev = sc->sc_intr_in_dev = sc->sc_bulk_out_dev = NULL;
177 sc->sc_intr_in_pipe = sc->sc_bulk_out_pipe = NULL;
178 sc->sc_flags = sc->sc_refcnt = sc->sc_dying = 0;
180 err = usbd_set_config_no(sc->sc_udev, UBTBCMFW_CONFIG_NO, 1);
182 printf("%s: setting config no failed. %s\n",
183 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
187 err = usbd_device2interface_handle(sc->sc_udev, UBTBCMFW_IFACE_IDX,
190 printf("%s: getting interface handle failed. %s\n",
191 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
195 /* Will be used as a bulk pipe */
196 err = usbd_open_pipe(iface, UBTBCMFW_INTR_IN_EP, 0,
197 &sc->sc_intr_in_pipe);
199 printf("%s: open intr in failed. %s\n",
200 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
204 err = usbd_open_pipe(iface, UBTBCMFW_BULK_OUT_EP, 0,
205 &sc->sc_bulk_out_pipe);
207 printf("%s: open bulk out failed. %s\n",
208 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
212 /* Create device nodes */
213 sc->sc_ctrl_dev = make_dev(&ubtbcmfw_cdevsw,
214 UBTBCMFW_MINOR(device_get_unit(sc->sc_dev), 0),
215 UID_ROOT, GID_OPERATOR, 0644,
216 "%s", device_get_nameunit(sc->sc_dev));
218 sc->sc_intr_in_dev = make_dev(&ubtbcmfw_cdevsw,
219 UBTBCMFW_MINOR(device_get_unit(sc->sc_dev), UBTBCMFW_INTR_IN),
220 UID_ROOT, GID_OPERATOR, 0644,
221 "%s.%d", device_get_nameunit(sc->sc_dev), UBTBCMFW_INTR_IN);
223 sc->sc_bulk_out_dev = make_dev(&ubtbcmfw_cdevsw,
224 UBTBCMFW_MINOR(device_get_unit(sc->sc_dev), UBTBCMFW_BULK_OUT),
225 UID_ROOT, GID_OPERATOR, 0644,
226 "%s.%d", device_get_nameunit(sc->sc_dev), UBTBCMFW_BULK_OUT);
230 ubtbcmfw_detach(self);
239 ubtbcmfw_detach(device_t self)
241 struct ubtbcmfw_softc *sc = device_get_softc(self);
244 if (-- sc->sc_refcnt >= 0) {
245 if (sc->sc_intr_in_pipe != NULL)
246 usbd_abort_pipe(sc->sc_intr_in_pipe);
248 if (sc->sc_bulk_out_pipe != NULL)
249 usbd_abort_pipe(sc->sc_bulk_out_pipe);
251 usb_detach_wait(sc->sc_dev);
254 /* Destroy device nodes */
255 if (sc->sc_bulk_out_dev != NULL) {
256 destroy_dev(sc->sc_bulk_out_dev);
257 sc->sc_bulk_out_dev = NULL;
260 if (sc->sc_intr_in_dev != NULL) {
261 destroy_dev(sc->sc_intr_in_dev);
262 sc->sc_intr_in_dev = NULL;
265 if (sc->sc_ctrl_dev != NULL) {
266 destroy_dev(sc->sc_ctrl_dev);
267 sc->sc_ctrl_dev = NULL;
271 if (sc->sc_intr_in_pipe != NULL) {
272 usbd_close_pipe(sc->sc_intr_in_pipe);
273 sc->sc_intr_in_pipe = NULL;
276 if (sc->sc_bulk_out_pipe != NULL) {
277 usbd_close_pipe(sc->sc_bulk_out_pipe);
278 sc->sc_intr_in_pipe = NULL;
285 * Open endpoint device
286 * XXX FIXME softc locking
290 ubtbcmfw_open(struct cdev *dev, int flag, int mode, struct thread *p)
292 ubtbcmfw_softc_p sc = NULL;
295 /* checks for sc != NULL */
296 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
302 switch (UBTBCMFW_ENDPOINT(dev)) {
303 case USB_CONTROL_ENDPOINT:
304 if (!(sc->sc_flags & UBTBCMFW_CTRL_DEV))
305 sc->sc_flags |= UBTBCMFW_CTRL_DEV;
310 case UBTBCMFW_INTR_IN:
311 if (!(sc->sc_flags & UBTBCMFW_INTR_IN_DEV)) {
312 if (sc->sc_intr_in_pipe != NULL)
313 sc->sc_flags |= UBTBCMFW_INTR_IN_DEV;
320 case UBTBCMFW_BULK_OUT:
321 if (!(sc->sc_flags & UBTBCMFW_BULK_OUT_DEV)) {
322 if (sc->sc_bulk_out_pipe != NULL)
323 sc->sc_flags |= UBTBCMFW_BULK_OUT_DEV;
339 * Close endpoint device
340 * XXX FIXME softc locking
344 ubtbcmfw_close(struct cdev *dev, int flag, int mode, struct thread *p)
346 ubtbcmfw_softc_p sc = NULL;
348 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
352 switch (UBTBCMFW_ENDPOINT(dev)) {
353 case USB_CONTROL_ENDPOINT:
354 sc->sc_flags &= ~UBTBCMFW_CTRL_DEV;
357 case UBTBCMFW_INTR_IN:
358 if (sc->sc_intr_in_pipe != NULL)
359 usbd_abort_pipe(sc->sc_intr_in_pipe);
361 sc->sc_flags &= ~UBTBCMFW_INTR_IN_DEV;
364 case UBTBCMFW_BULK_OUT:
365 if (sc->sc_bulk_out_pipe != NULL)
366 usbd_abort_pipe(sc->sc_bulk_out_pipe);
368 sc->sc_flags &= ~UBTBCMFW_BULK_OUT_DEV;
376 * Read from the endpoint device
377 * XXX FIXME softc locking
381 ubtbcmfw_read(struct cdev *dev, struct uio *uio, int flag)
383 ubtbcmfw_softc_p sc = NULL;
384 u_int8_t buf[UBTBCMFW_BSIZE];
385 usbd_xfer_handle xfer;
387 int n, tn, error = 0;
389 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
390 if (sc == NULL || sc->sc_dying)
393 if (UBTBCMFW_ENDPOINT(dev) != UBTBCMFW_INTR_IN)
395 if (sc->sc_intr_in_pipe == NULL)
398 xfer = usbd_alloc_xfer(sc->sc_udev);
404 while ((n = (int)szmin(sizeof(buf), uio->uio_resid)) != 0) {
406 err = usbd_bulk_transfer(xfer, sc->sc_intr_in_pipe,
407 USBD_SHORT_XFER_OK, USBD_DEFAULT_TIMEOUT,
410 case USBD_NORMAL_COMPLETION:
411 error = uiomove(buf, (size_t)tn, uio);
414 case USBD_INTERRUPTED:
427 if (error != 0 || tn < n)
431 usbd_free_xfer(xfer);
433 if (-- sc->sc_refcnt < 0)
434 usb_detach_wakeup(sc->sc_dev);
440 * Write into the endpoint device
441 * XXX FIXME softc locking
445 ubtbcmfw_write(struct cdev *dev, struct uio *uio, int flag)
447 ubtbcmfw_softc_p sc = NULL;
448 u_int8_t buf[UBTBCMFW_BSIZE];
449 usbd_xfer_handle xfer;
453 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
454 if (sc == NULL || sc->sc_dying)
457 if (UBTBCMFW_ENDPOINT(dev) != UBTBCMFW_BULK_OUT)
459 if (sc->sc_bulk_out_pipe == NULL)
462 xfer = usbd_alloc_xfer(sc->sc_udev);
468 while ((n = (int)szmin(sizeof(buf), uio->uio_resid)) != 0) {
469 error = uiomove(buf, (size_t)n, uio);
473 err = usbd_bulk_transfer(xfer, sc->sc_bulk_out_pipe,
474 0, USBD_DEFAULT_TIMEOUT, buf, &n, "bcmwr");
476 case USBD_NORMAL_COMPLETION:
479 case USBD_INTERRUPTED:
496 usbd_free_xfer(xfer);
498 if (-- sc->sc_refcnt < 0)
499 usb_detach_wakeup(sc->sc_dev);
505 * Process ioctl on the endpoint device
506 * XXX FIXME softc locking
510 ubtbcmfw_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
513 ubtbcmfw_softc_p sc = NULL;
516 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
517 if (sc == NULL || sc->sc_dying)
520 if (UBTBCMFW_ENDPOINT(dev) != USB_CONTROL_ENDPOINT)
526 case USB_GET_DEVICE_DESC:
527 *(usb_device_descriptor_t *) data =
528 *usbd_get_device_descriptor(sc->sc_udev);
536 if (-- sc->sc_refcnt < 0)
537 usb_detach_wakeup(sc->sc_dev);
543 * Poll the endpoint device
544 * XXX FIXME softc locking
548 ubtbcmfw_poll(struct cdev *dev, int events, struct thread *p)
550 ubtbcmfw_softc_p sc = NULL;
553 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
557 switch (UBTBCMFW_ENDPOINT(dev)) {
558 case UBTBCMFW_INTR_IN:
559 if (sc->sc_intr_in_pipe != NULL)
560 revents |= events & (POLLIN | POLLRDNORM);
565 case UBTBCMFW_BULK_OUT:
566 if (sc->sc_bulk_out_pipe != NULL)
567 revents |= events & (POLLOUT | POLLWRNORM);
573 revents = EOPNOTSUPP;