1 /* 2 * ubtbcmfw.c 3 */ 4 5 /*- 6 * Copyright (c) 2003-2009 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 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. 17 * 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 28 * SUCH DAMAGE. 29 * 30 * $Id: ubtbcmfw.c,v 1.3 2003/10/10 19:15:08 max Exp $ 31 * $FreeBSD$ 32 */ 33 34 #include <sys/stdint.h> 35 #include <sys/stddef.h> 36 #include <sys/param.h> 37 #include <sys/queue.h> 38 #include <sys/types.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/bus.h> 42 #include <sys/linker_set.h> 43 #include <sys/module.h> 44 #include <sys/lock.h> 45 #include <sys/mutex.h> 46 #include <sys/condvar.h> 47 #include <sys/sysctl.h> 48 #include <sys/sx.h> 49 #include <sys/unistd.h> 50 #include <sys/callout.h> 51 #include <sys/malloc.h> 52 #include <sys/priv.h> 53 #include <sys/conf.h> 54 #include <sys/fcntl.h> 55 56 #include "usbdevs.h" 57 #include <dev/usb/usb.h> 58 #include <dev/usb/usbdi.h> 59 #include <dev/usb/usb_ioctl.h> 60 61 #define USB_DEBUG_VAR usb_debug 62 #include <dev/usb/usb_debug.h> 63 #include <dev/usb/usb_dev.h> 64 65 /* 66 * Download firmware to BCM2033. 67 */ 68 69 #define UBTBCMFW_CONFIG_NO 1 /* Config number */ 70 #define UBTBCMFW_IFACE_IDX 0 /* Control interface */ 71 72 #define UBTBCMFW_BSIZE 1024 73 #define UBTBCMFW_IFQ_MAXLEN 2 74 75 enum { 76 UBTBCMFW_BULK_DT_WR = 0, 77 UBTBCMFW_INTR_DT_RD, 78 UBTBCMFW_N_TRANSFER, 79 }; 80 81 struct ubtbcmfw_softc { 82 struct usb_device *sc_udev; 83 struct mtx sc_mtx; 84 struct usb_xfer *sc_xfer[UBTBCMFW_N_TRANSFER]; 85 struct usb_fifo_sc sc_fifo; 86 }; 87 88 /* 89 * Prototypes 90 */ 91 92 static device_probe_t ubtbcmfw_probe; 93 static device_attach_t ubtbcmfw_attach; 94 static device_detach_t ubtbcmfw_detach; 95 96 static usb_callback_t ubtbcmfw_write_callback; 97 static usb_callback_t ubtbcmfw_read_callback; 98 99 static usb_fifo_close_t ubtbcmfw_close; 100 static usb_fifo_cmd_t ubtbcmfw_start_read; 101 static usb_fifo_cmd_t ubtbcmfw_start_write; 102 static usb_fifo_cmd_t ubtbcmfw_stop_read; 103 static usb_fifo_cmd_t ubtbcmfw_stop_write; 104 static usb_fifo_ioctl_t ubtbcmfw_ioctl; 105 static usb_fifo_open_t ubtbcmfw_open; 106 107 static struct usb_fifo_methods ubtbcmfw_fifo_methods = 108 { 109 .f_close = &ubtbcmfw_close, 110 .f_ioctl = &ubtbcmfw_ioctl, 111 .f_open = &ubtbcmfw_open, 112 .f_start_read = &ubtbcmfw_start_read, 113 .f_start_write = &ubtbcmfw_start_write, 114 .f_stop_read = &ubtbcmfw_stop_read, 115 .f_stop_write = &ubtbcmfw_stop_write, 116 .basename[0] = "ubtbcmfw", 117 .basename[1] = "ubtbcmfw", 118 .basename[2] = "ubtbcmfw", 119 .postfix[0] = "", 120 .postfix[1] = ".1", 121 .postfix[2] = ".2", 122 }; 123 124 /* 125 * Device's config structure 126 */ 127 128 static const struct usb_config ubtbcmfw_config[UBTBCMFW_N_TRANSFER] = 129 { 130 [UBTBCMFW_BULK_DT_WR] = { 131 .type = UE_BULK, 132 .endpoint = 0x02, /* fixed */ 133 .direction = UE_DIR_OUT, 134 .if_index = UBTBCMFW_IFACE_IDX, 135 .bufsize = UBTBCMFW_BSIZE, 136 .flags = { .pipe_bof = 1, .force_short_xfer = 1, 137 .proxy_buffer = 1, }, 138 .callback = &ubtbcmfw_write_callback, 139 }, 140 141 [UBTBCMFW_INTR_DT_RD] = { 142 .type = UE_INTERRUPT, 143 .endpoint = 0x01, /* fixed */ 144 .direction = UE_DIR_IN, 145 .if_index = UBTBCMFW_IFACE_IDX, 146 .bufsize = UBTBCMFW_BSIZE, 147 .flags = { .pipe_bof = 1, .short_xfer_ok = 1, 148 .proxy_buffer = 1, }, 149 .callback = &ubtbcmfw_read_callback, 150 }, 151 }; 152 153 /* 154 * Module 155 */ 156 157 static devclass_t ubtbcmfw_devclass; 158 159 static device_method_t ubtbcmfw_methods[] = 160 { 161 DEVMETHOD(device_probe, ubtbcmfw_probe), 162 DEVMETHOD(device_attach, ubtbcmfw_attach), 163 DEVMETHOD(device_detach, ubtbcmfw_detach), 164 {0, 0} 165 }; 166 167 static driver_t ubtbcmfw_driver = 168 { 169 .name = "ubtbcmfw", 170 .methods = ubtbcmfw_methods, 171 .size = sizeof(struct ubtbcmfw_softc), 172 }; 173 174 DRIVER_MODULE(ubtbcmfw, uhub, ubtbcmfw_driver, ubtbcmfw_devclass, NULL, 0); 175 MODULE_DEPEND(ubtbcmfw, usb, 1, 1, 1); 176 177 /* 178 * Probe for a USB Bluetooth device 179 */ 180 181 static int 182 ubtbcmfw_probe(device_t dev) 183 { 184 const struct usb_device_id devs[] = { 185 /* Broadcom BCM2033 devices only */ 186 { USB_VPI(USB_VENDOR_BROADCOM, USB_PRODUCT_BROADCOM_BCM2033, 0) }, 187 }; 188 189 struct usb_attach_arg *uaa = device_get_ivars(dev); 190 191 if (uaa->usb_mode != USB_MODE_HOST) 192 return (ENXIO); 193 194 if (uaa->info.bIfaceIndex != 0) 195 return (ENXIO); 196 197 return (usbd_lookup_id_by_uaa(devs, sizeof(devs), uaa)); 198 } /* ubtbcmfw_probe */ 199 200 /* 201 * Attach the device 202 */ 203 204 static int 205 ubtbcmfw_attach(device_t dev) 206 { 207 struct usb_attach_arg *uaa = device_get_ivars(dev); 208 struct ubtbcmfw_softc *sc = device_get_softc(dev); 209 uint8_t iface_index; 210 int error; 211 212 sc->sc_udev = uaa->device; 213 214 device_set_usb_desc(dev); 215 216 mtx_init(&sc->sc_mtx, "ubtbcmfw lock", NULL, MTX_DEF | MTX_RECURSE); 217 218 iface_index = UBTBCMFW_IFACE_IDX; 219 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer, 220 ubtbcmfw_config, UBTBCMFW_N_TRANSFER, 221 sc, &sc->sc_mtx); 222 if (error != 0) { 223 device_printf(dev, "allocating USB transfers failed. %s\n", 224 usbd_errstr(error)); 225 goto detach; 226 } 227 228 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx, 229 &ubtbcmfw_fifo_methods, &sc->sc_fifo, 230 device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex, 231 UID_ROOT, GID_OPERATOR, 0644); 232 if (error != 0) { 233 device_printf(dev, "could not attach fifo. %s\n", 234 usbd_errstr(error)); 235 goto detach; 236 } 237 238 return (0); /* success */ 239 240 detach: 241 ubtbcmfw_detach(dev); 242 243 return (ENXIO); /* failure */ 244 } /* ubtbcmfw_attach */ 245 246 /* 247 * Detach the device 248 */ 249 250 static int 251 ubtbcmfw_detach(device_t dev) 252 { 253 struct ubtbcmfw_softc *sc = device_get_softc(dev); 254 255 usb_fifo_detach(&sc->sc_fifo); 256 257 usbd_transfer_unsetup(sc->sc_xfer, UBTBCMFW_N_TRANSFER); 258 259 mtx_destroy(&sc->sc_mtx); 260 261 return (0); 262 } /* ubtbcmfw_detach */ 263 264 /* 265 * USB write callback 266 */ 267 268 static void 269 ubtbcmfw_write_callback(struct usb_xfer *xfer, usb_error_t error) 270 { 271 struct ubtbcmfw_softc *sc = usbd_xfer_softc(xfer); 272 struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_TX]; 273 struct usb_page_cache *pc; 274 uint32_t actlen; 275 276 switch (USB_GET_STATE(xfer)) { 277 case USB_ST_SETUP: 278 case USB_ST_TRANSFERRED: 279 setup_next: 280 pc = usbd_xfer_get_frame(xfer, 0); 281 if (usb_fifo_get_data(f, pc, 0, usbd_xfer_max_len(xfer), 282 &actlen, 0)) { 283 usbd_xfer_set_frame_len(xfer, 0, actlen); 284 usbd_transfer_submit(xfer); 285 } 286 break; 287 288 default: /* Error */ 289 if (error != USB_ERR_CANCELLED) { 290 /* try to clear stall first */ 291 usbd_xfer_set_stall(xfer); 292 goto setup_next; 293 } 294 break; 295 } 296 } /* ubtbcmfw_write_callback */ 297 298 /* 299 * USB read callback 300 */ 301 302 static void 303 ubtbcmfw_read_callback(struct usb_xfer *xfer, usb_error_t error) 304 { 305 struct ubtbcmfw_softc *sc = usbd_xfer_softc(xfer); 306 struct usb_fifo *fifo = sc->sc_fifo.fp[USB_FIFO_RX]; 307 struct usb_page_cache *pc; 308 int actlen; 309 310 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 311 312 switch (USB_GET_STATE(xfer)) { 313 case USB_ST_TRANSFERRED: 314 pc = usbd_xfer_get_frame(xfer, 0); 315 usb_fifo_put_data(fifo, pc, 0, actlen, 1); 316 /* FALLTHROUGH */ 317 318 case USB_ST_SETUP: 319 setup_next: 320 if (usb_fifo_put_bytes_max(fifo) > 0) { 321 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 322 usbd_transfer_submit(xfer); 323 } 324 break; 325 326 default: /* Error */ 327 if (error != USB_ERR_CANCELLED) { 328 /* try to clear stall first */ 329 usbd_xfer_set_stall(xfer); 330 goto setup_next; 331 } 332 break; 333 } 334 } /* ubtbcmfw_read_callback */ 335 336 /* 337 * Called when we about to start read()ing from the device 338 */ 339 340 static void 341 ubtbcmfw_start_read(struct usb_fifo *fifo) 342 { 343 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo); 344 345 usbd_transfer_start(sc->sc_xfer[UBTBCMFW_INTR_DT_RD]); 346 } /* ubtbcmfw_start_read */ 347 348 /* 349 * Called when we about to stop reading (i.e. closing fifo) 350 */ 351 352 static void 353 ubtbcmfw_stop_read(struct usb_fifo *fifo) 354 { 355 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo); 356 357 usbd_transfer_stop(sc->sc_xfer[UBTBCMFW_INTR_DT_RD]); 358 } /* ubtbcmfw_stop_read */ 359 360 /* 361 * Called when we about to start write()ing to the device, poll()ing 362 * for write or flushing fifo 363 */ 364 365 static void 366 ubtbcmfw_start_write(struct usb_fifo *fifo) 367 { 368 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo); 369 370 usbd_transfer_start(sc->sc_xfer[UBTBCMFW_BULK_DT_WR]); 371 } /* ubtbcmfw_start_write */ 372 373 /* 374 * Called when we about to stop writing (i.e. closing fifo) 375 */ 376 377 static void 378 ubtbcmfw_stop_write(struct usb_fifo *fifo) 379 { 380 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo); 381 382 usbd_transfer_stop(sc->sc_xfer[UBTBCMFW_BULK_DT_WR]); 383 } /* ubtbcmfw_stop_write */ 384 385 /* 386 * Called when fifo is open 387 */ 388 389 static int 390 ubtbcmfw_open(struct usb_fifo *fifo, int fflags) 391 { 392 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo); 393 struct usb_xfer *xfer; 394 395 /* 396 * f_open fifo method can only be called with either FREAD 397 * or FWRITE flag set at one time. 398 */ 399 400 if (fflags & FREAD) 401 xfer = sc->sc_xfer[UBTBCMFW_INTR_DT_RD]; 402 else if (fflags & FWRITE) 403 xfer = sc->sc_xfer[UBTBCMFW_BULK_DT_WR]; 404 else 405 return (EINVAL); /* should not happen */ 406 407 if (usb_fifo_alloc_buffer(fifo, usbd_xfer_max_len(xfer), 408 UBTBCMFW_IFQ_MAXLEN) != 0) 409 return (ENOMEM); 410 411 return (0); 412 } /* ubtbcmfw_open */ 413 414 /* 415 * Called when fifo is closed 416 */ 417 418 static void 419 ubtbcmfw_close(struct usb_fifo *fifo, int fflags) 420 { 421 if (fflags & (FREAD | FWRITE)) 422 usb_fifo_free_buffer(fifo); 423 } /* ubtbcmfw_close */ 424 425 /* 426 * Process ioctl() on USB device 427 */ 428 429 static int 430 ubtbcmfw_ioctl(struct usb_fifo *fifo, u_long cmd, void *data, 431 int fflags) 432 { 433 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo); 434 int error = 0; 435 436 switch (cmd) { 437 case USB_GET_DEVICE_DESC: 438 memcpy(data, usbd_get_device_descriptor(sc->sc_udev), 439 sizeof(struct usb_device_descriptor)); 440 break; 441 442 default: 443 error = EINVAL; 444 break; 445 } 446 447 return (error); 448 } /* ubtbcmfw_ioctl */ 449