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 $
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;
135 MODULE_DEPEND(ubtbcmfw, usb, 1, 1, 1);
136 DRIVER_MODULE(ubtbcmfw, uhub, ubtbcmfw_driver, ubtbcmfw_devclass,
137 usbd_driver_load, 0);
140 * Probe for a USB Bluetooth device
144 ubtbcmfw_match(device_t self)
146 #define USB_PRODUCT_BROADCOM_BCM2033NF 0x2033
147 struct usb_attach_arg *uaa = device_get_ivars(self);
149 if (uaa->iface != NULL)
150 return (UMATCH_NONE);
152 /* Match the boot device. */
153 if (uaa->vendor == USB_VENDOR_BROADCOM &&
154 uaa->product == USB_PRODUCT_BROADCOM_BCM2033NF)
155 return (UMATCH_VENDOR_PRODUCT);
157 return (UMATCH_NONE);
165 ubtbcmfw_attach(device_t self)
167 struct ubtbcmfw_softc *sc = device_get_softc(self);
168 struct usb_attach_arg *uaa = device_get_ivars(self);
169 usbd_interface_handle iface;
173 sc->sc_udev = uaa->device;
175 sc->sc_ctrl_dev = sc->sc_intr_in_dev = sc->sc_bulk_out_dev = NULL;
176 sc->sc_intr_in_pipe = sc->sc_bulk_out_pipe = NULL;
177 sc->sc_flags = sc->sc_refcnt = sc->sc_dying = 0;
179 err = usbd_set_config_no(sc->sc_udev, UBTBCMFW_CONFIG_NO, 1);
181 printf("%s: setting config no failed. %s\n",
182 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
186 err = usbd_device2interface_handle(sc->sc_udev, UBTBCMFW_IFACE_IDX,
189 printf("%s: getting interface handle failed. %s\n",
190 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
194 /* Will be used as a bulk pipe */
195 err = usbd_open_pipe(iface, UBTBCMFW_INTR_IN_EP, 0,
196 &sc->sc_intr_in_pipe);
198 printf("%s: open intr in failed. %s\n",
199 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
203 err = usbd_open_pipe(iface, UBTBCMFW_BULK_OUT_EP, 0,
204 &sc->sc_bulk_out_pipe);
206 printf("%s: open bulk out failed. %s\n",
207 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
211 /* Create device nodes */
212 sc->sc_ctrl_dev = make_dev(&ubtbcmfw_cdevsw,
213 UBTBCMFW_MINOR(device_get_unit(sc->sc_dev), 0),
214 UID_ROOT, GID_OPERATOR, 0644,
215 "%s", device_get_nameunit(sc->sc_dev));
217 sc->sc_intr_in_dev = make_dev(&ubtbcmfw_cdevsw,
218 UBTBCMFW_MINOR(device_get_unit(sc->sc_dev), UBTBCMFW_INTR_IN),
219 UID_ROOT, GID_OPERATOR, 0644,
220 "%s.%d", device_get_nameunit(sc->sc_dev), UBTBCMFW_INTR_IN);
222 sc->sc_bulk_out_dev = make_dev(&ubtbcmfw_cdevsw,
223 UBTBCMFW_MINOR(device_get_unit(sc->sc_dev), UBTBCMFW_BULK_OUT),
224 UID_ROOT, GID_OPERATOR, 0644,
225 "%s.%d", device_get_nameunit(sc->sc_dev), UBTBCMFW_BULK_OUT);
229 ubtbcmfw_detach(self);
238 ubtbcmfw_detach(device_t self)
240 struct ubtbcmfw_softc *sc = device_get_softc(self);
243 if (-- sc->sc_refcnt >= 0) {
244 if (sc->sc_intr_in_pipe != NULL)
245 usbd_abort_pipe(sc->sc_intr_in_pipe);
247 if (sc->sc_bulk_out_pipe != NULL)
248 usbd_abort_pipe(sc->sc_bulk_out_pipe);
250 usb_detach_wait(sc->sc_dev);
253 /* Destroy device nodes */
254 if (sc->sc_bulk_out_dev != NULL) {
255 destroy_dev(sc->sc_bulk_out_dev);
256 sc->sc_bulk_out_dev = NULL;
259 if (sc->sc_intr_in_dev != NULL) {
260 destroy_dev(sc->sc_intr_in_dev);
261 sc->sc_intr_in_dev = NULL;
264 if (sc->sc_ctrl_dev != NULL) {
265 destroy_dev(sc->sc_ctrl_dev);
266 sc->sc_ctrl_dev = NULL;
270 if (sc->sc_intr_in_pipe != NULL) {
271 usbd_close_pipe(sc->sc_intr_in_pipe);
272 sc->sc_intr_in_pipe = NULL;
275 if (sc->sc_bulk_out_pipe != NULL) {
276 usbd_close_pipe(sc->sc_bulk_out_pipe);
277 sc->sc_intr_in_pipe = NULL;
284 * Open endpoint device
285 * XXX FIXME softc locking
289 ubtbcmfw_open(struct cdev *dev, int flag, int mode, struct thread *p)
291 ubtbcmfw_softc_p sc = NULL;
294 /* checks for sc != NULL */
295 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
301 switch (UBTBCMFW_ENDPOINT(dev)) {
302 case USB_CONTROL_ENDPOINT:
303 if (!(sc->sc_flags & UBTBCMFW_CTRL_DEV))
304 sc->sc_flags |= UBTBCMFW_CTRL_DEV;
309 case UBTBCMFW_INTR_IN:
310 if (!(sc->sc_flags & UBTBCMFW_INTR_IN_DEV)) {
311 if (sc->sc_intr_in_pipe != NULL)
312 sc->sc_flags |= UBTBCMFW_INTR_IN_DEV;
319 case UBTBCMFW_BULK_OUT:
320 if (!(sc->sc_flags & UBTBCMFW_BULK_OUT_DEV)) {
321 if (sc->sc_bulk_out_pipe != NULL)
322 sc->sc_flags |= UBTBCMFW_BULK_OUT_DEV;
338 * Close endpoint device
339 * XXX FIXME softc locking
343 ubtbcmfw_close(struct cdev *dev, int flag, int mode, struct thread *p)
345 ubtbcmfw_softc_p sc = NULL;
347 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
351 switch (UBTBCMFW_ENDPOINT(dev)) {
352 case USB_CONTROL_ENDPOINT:
353 sc->sc_flags &= ~UBTBCMFW_CTRL_DEV;
356 case UBTBCMFW_INTR_IN:
357 if (sc->sc_intr_in_pipe != NULL)
358 usbd_abort_pipe(sc->sc_intr_in_pipe);
360 sc->sc_flags &= ~UBTBCMFW_INTR_IN_DEV;
363 case UBTBCMFW_BULK_OUT:
364 if (sc->sc_bulk_out_pipe != NULL)
365 usbd_abort_pipe(sc->sc_bulk_out_pipe);
367 sc->sc_flags &= ~UBTBCMFW_BULK_OUT_DEV;
375 * Read from the endpoint device
376 * XXX FIXME softc locking
380 ubtbcmfw_read(struct cdev *dev, struct uio *uio, int flag)
382 ubtbcmfw_softc_p sc = NULL;
383 u_int8_t buf[UBTBCMFW_BSIZE];
384 usbd_xfer_handle xfer;
386 int n, tn, error = 0;
388 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
389 if (sc == NULL || sc->sc_dying)
392 if (UBTBCMFW_ENDPOINT(dev) != UBTBCMFW_INTR_IN)
394 if (sc->sc_intr_in_pipe == NULL)
397 xfer = usbd_alloc_xfer(sc->sc_udev);
403 while ((n = (int)szmin(sizeof(buf), uio->uio_resid)) != 0) {
405 err = usbd_bulk_transfer(xfer, sc->sc_intr_in_pipe,
406 USBD_SHORT_XFER_OK, USBD_DEFAULT_TIMEOUT,
409 case USBD_NORMAL_COMPLETION:
410 error = uiomove(buf, (size_t)tn, uio);
413 case USBD_INTERRUPTED:
426 if (error != 0 || tn < n)
430 usbd_free_xfer(xfer);
432 if (-- sc->sc_refcnt < 0)
433 usb_detach_wakeup(sc->sc_dev);
439 * Write into the endpoint device
440 * XXX FIXME softc locking
444 ubtbcmfw_write(struct cdev *dev, struct uio *uio, int flag)
446 ubtbcmfw_softc_p sc = NULL;
447 u_int8_t buf[UBTBCMFW_BSIZE];
448 usbd_xfer_handle xfer;
452 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
453 if (sc == NULL || sc->sc_dying)
456 if (UBTBCMFW_ENDPOINT(dev) != UBTBCMFW_BULK_OUT)
458 if (sc->sc_bulk_out_pipe == NULL)
461 xfer = usbd_alloc_xfer(sc->sc_udev);
467 while ((n = (int)szmin(sizeof(buf), uio->uio_resid)) != 0) {
468 error = uiomove(buf, (size_t)n, uio);
472 err = usbd_bulk_transfer(xfer, sc->sc_bulk_out_pipe,
473 0, USBD_DEFAULT_TIMEOUT, buf, &n, "bcmwr");
475 case USBD_NORMAL_COMPLETION:
478 case USBD_INTERRUPTED:
495 usbd_free_xfer(xfer);
497 if (-- sc->sc_refcnt < 0)
498 usb_detach_wakeup(sc->sc_dev);
504 * Process ioctl on the endpoint device
505 * XXX FIXME softc locking
509 ubtbcmfw_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
512 ubtbcmfw_softc_p sc = NULL;
515 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
516 if (sc == NULL || sc->sc_dying)
519 if (UBTBCMFW_ENDPOINT(dev) != USB_CONTROL_ENDPOINT)
525 case USB_GET_DEVICE_DESC:
526 *(usb_device_descriptor_t *) data =
527 *usbd_get_device_descriptor(sc->sc_udev);
535 if (-- sc->sc_refcnt < 0)
536 usb_detach_wakeup(sc->sc_dev);
542 * Poll the endpoint device
543 * XXX FIXME softc locking
547 ubtbcmfw_poll(struct cdev *dev, int events, struct thread *p)
549 ubtbcmfw_softc_p sc = NULL;
552 sc = devclass_get_softc(ubtbcmfw_devclass, UBTBCMFW_UNIT(dev));
556 switch (UBTBCMFW_ENDPOINT(dev)) {
557 case UBTBCMFW_INTR_IN:
558 if (sc->sc_intr_in_pipe != NULL)
559 revents |= events & (POLLIN | POLLRDNORM);
564 case UBTBCMFW_BULK_OUT:
565 if (sc->sc_bulk_out_pipe != NULL)
566 revents |= events & (POLLOUT | POLLWRNORM);
572 revents = EOPNOTSUPP;