1184610Salfred/* 2184610Salfred * ubtbcmfw.c 3184610Salfred */ 4184610Salfred 5184610Salfred/*- 6187866Semax * Copyright (c) 2003-2009 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7184610Salfred * All rights reserved. 8184610Salfred * 9184610Salfred * Redistribution and use in source and binary forms, with or without 10184610Salfred * modification, are permitted provided that the following conditions 11184610Salfred * are met: 12184610Salfred * 1. Redistributions of source code must retain the above copyright 13184610Salfred * notice, this list of conditions and the following disclaimer. 14184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 15184610Salfred * notice, this list of conditions and the following disclaimer in the 16184610Salfred * documentation and/or other materials provided with the distribution. 17184610Salfred * 18184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28184610Salfred * SUCH DAMAGE. 29184610Salfred * 30184610Salfred * $Id: ubtbcmfw.c,v 1.3 2003/10/10 19:15:08 max Exp $ 31184610Salfred * $FreeBSD$ 32184610Salfred */ 33184610Salfred 34194677Sthompsa#include <sys/stdint.h> 35194677Sthompsa#include <sys/stddef.h> 36194677Sthompsa#include <sys/param.h> 37194677Sthompsa#include <sys/queue.h> 38194677Sthompsa#include <sys/types.h> 39194677Sthompsa#include <sys/systm.h> 40194677Sthompsa#include <sys/kernel.h> 41194677Sthompsa#include <sys/bus.h> 42194677Sthompsa#include <sys/module.h> 43194677Sthompsa#include <sys/lock.h> 44194677Sthompsa#include <sys/mutex.h> 45194677Sthompsa#include <sys/condvar.h> 46194677Sthompsa#include <sys/sysctl.h> 47194677Sthompsa#include <sys/sx.h> 48194677Sthompsa#include <sys/unistd.h> 49194677Sthompsa#include <sys/callout.h> 50194677Sthompsa#include <sys/malloc.h> 51194677Sthompsa#include <sys/priv.h> 52194677Sthompsa#include <sys/conf.h> 53194677Sthompsa#include <sys/fcntl.h> 54194677Sthompsa 55188746Sthompsa#include "usbdevs.h" 56188942Sthompsa#include <dev/usb/usb.h> 57194677Sthompsa#include <dev/usb/usbdi.h> 58188942Sthompsa#include <dev/usb/usb_ioctl.h> 59184610Salfred 60194228Sthompsa#define USB_DEBUG_VAR usb_debug 61188942Sthompsa#include <dev/usb/usb_debug.h> 62188942Sthompsa#include <dev/usb/usb_dev.h> 63184610Salfred 64184610Salfred/* 65184610Salfred * Download firmware to BCM2033. 66184610Salfred */ 67184610Salfred 68184610Salfred#define UBTBCMFW_CONFIG_NO 1 /* Config number */ 69184610Salfred#define UBTBCMFW_IFACE_IDX 0 /* Control interface */ 70184610Salfred 71187866Semax#define UBTBCMFW_BSIZE 1024 72187866Semax#define UBTBCMFW_IFQ_MAXLEN 2 73187866Semax 74187259Sthompsaenum { 75187866Semax UBTBCMFW_BULK_DT_WR = 0, 76187866Semax UBTBCMFW_INTR_DT_RD, 77187866Semax UBTBCMFW_N_TRANSFER, 78187259Sthompsa}; 79187259Sthompsa 80184610Salfredstruct ubtbcmfw_softc { 81192984Sthompsa struct usb_device *sc_udev; 82187866Semax struct mtx sc_mtx; 83192984Sthompsa struct usb_xfer *sc_xfer[UBTBCMFW_N_TRANSFER]; 84192984Sthompsa struct usb_fifo_sc sc_fifo; 85184610Salfred}; 86184610Salfred 87187866Semax/* 88187866Semax * Prototypes 89187866Semax */ 90184610Salfred 91187866Semaxstatic device_probe_t ubtbcmfw_probe; 92187866Semaxstatic device_attach_t ubtbcmfw_attach; 93187866Semaxstatic device_detach_t ubtbcmfw_detach; 94184610Salfred 95193045Sthompsastatic usb_callback_t ubtbcmfw_write_callback; 96193045Sthompsastatic usb_callback_t ubtbcmfw_read_callback; 97184610Salfred 98193045Sthompsastatic usb_fifo_close_t ubtbcmfw_close; 99193045Sthompsastatic usb_fifo_cmd_t ubtbcmfw_start_read; 100193045Sthompsastatic usb_fifo_cmd_t ubtbcmfw_start_write; 101193045Sthompsastatic usb_fifo_cmd_t ubtbcmfw_stop_read; 102193045Sthompsastatic usb_fifo_cmd_t ubtbcmfw_stop_write; 103193045Sthompsastatic usb_fifo_ioctl_t ubtbcmfw_ioctl; 104193045Sthompsastatic usb_fifo_open_t ubtbcmfw_open; 105184610Salfred 106192984Sthompsastatic struct usb_fifo_methods ubtbcmfw_fifo_methods = 107187866Semax{ 108187866Semax .f_close = &ubtbcmfw_close, 109187866Semax .f_ioctl = &ubtbcmfw_ioctl, 110187866Semax .f_open = &ubtbcmfw_open, 111187866Semax .f_start_read = &ubtbcmfw_start_read, 112187866Semax .f_start_write = &ubtbcmfw_start_write, 113187866Semax .f_stop_read = &ubtbcmfw_stop_read, 114187866Semax .f_stop_write = &ubtbcmfw_stop_write, 115187866Semax .basename[0] = "ubtbcmfw", 116187866Semax .basename[1] = "ubtbcmfw", 117187866Semax .basename[2] = "ubtbcmfw", 118187866Semax .postfix[0] = "", 119187866Semax .postfix[1] = ".1", 120187866Semax .postfix[2] = ".2", 121184610Salfred}; 122184610Salfred 123187866Semax/* 124187866Semax * Device's config structure 125187866Semax */ 126184610Salfred 127192984Sthompsastatic const struct usb_config ubtbcmfw_config[UBTBCMFW_N_TRANSFER] = 128187866Semax{ 129187259Sthompsa [UBTBCMFW_BULK_DT_WR] = { 130187866Semax .type = UE_BULK, 131187866Semax .endpoint = 0x02, /* fixed */ 132187866Semax .direction = UE_DIR_OUT, 133187866Semax .if_index = UBTBCMFW_IFACE_IDX, 134190734Sthompsa .bufsize = UBTBCMFW_BSIZE, 135190734Sthompsa .flags = { .pipe_bof = 1, .force_short_xfer = 1, 136187866Semax .proxy_buffer = 1, }, 137190734Sthompsa .callback = &ubtbcmfw_write_callback, 138184610Salfred }, 139184610Salfred 140187866Semax [UBTBCMFW_INTR_DT_RD] = { 141187866Semax .type = UE_INTERRUPT, 142187866Semax .endpoint = 0x01, /* fixed */ 143187866Semax .direction = UE_DIR_IN, 144187866Semax .if_index = UBTBCMFW_IFACE_IDX, 145190734Sthompsa .bufsize = UBTBCMFW_BSIZE, 146190734Sthompsa .flags = { .pipe_bof = 1, .short_xfer_ok = 1, 147187866Semax .proxy_buffer = 1, }, 148190734Sthompsa .callback = &ubtbcmfw_read_callback, 149184610Salfred }, 150184610Salfred}; 151184610Salfred 152184610Salfred/* 153184610Salfred * Module 154184610Salfred */ 155184610Salfred 156187866Semaxstatic devclass_t ubtbcmfw_devclass; 157184610Salfred 158187866Semaxstatic device_method_t ubtbcmfw_methods[] = 159187866Semax{ 160184610Salfred DEVMETHOD(device_probe, ubtbcmfw_probe), 161184610Salfred DEVMETHOD(device_attach, ubtbcmfw_attach), 162184610Salfred DEVMETHOD(device_detach, ubtbcmfw_detach), 163184610Salfred {0, 0} 164184610Salfred}; 165184610Salfred 166187866Semaxstatic driver_t ubtbcmfw_driver = 167187866Semax{ 168187866Semax .name = "ubtbcmfw", 169187866Semax .methods = ubtbcmfw_methods, 170187866Semax .size = sizeof(struct ubtbcmfw_softc), 171184610Salfred}; 172184610Salfred 173189275SthompsaDRIVER_MODULE(ubtbcmfw, uhub, ubtbcmfw_driver, ubtbcmfw_devclass, NULL, 0); 174188942SthompsaMODULE_DEPEND(ubtbcmfw, usb, 1, 1, 1); 175184610Salfred 176184610Salfred/* 177184610Salfred * Probe for a USB Bluetooth device 178184610Salfred */ 179184610Salfred 180184610Salfredstatic int 181184610Salfredubtbcmfw_probe(device_t dev) 182184610Salfred{ 183223486Shselasky static const STRUCT_USB_HOST_ID devs[] = { 184187866Semax /* Broadcom BCM2033 devices only */ 185187866Semax { USB_VPI(USB_VENDOR_BROADCOM, USB_PRODUCT_BROADCOM_BCM2033, 0) }, 186187866Semax }; 187184610Salfred 188192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 189187866Semax 190192499Sthompsa if (uaa->usb_mode != USB_MODE_HOST) 191184610Salfred return (ENXIO); 192187866Semax 193184610Salfred if (uaa->info.bIfaceIndex != 0) 194184610Salfred return (ENXIO); 195184610Salfred 196194228Sthompsa return (usbd_lookup_id_by_uaa(devs, sizeof(devs), uaa)); 197187866Semax} /* ubtbcmfw_probe */ 198184610Salfred 199184610Salfred/* 200184610Salfred * Attach the device 201184610Salfred */ 202184610Salfred 203184610Salfredstatic int 204184610Salfredubtbcmfw_attach(device_t dev) 205184610Salfred{ 206192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 207187866Semax struct ubtbcmfw_softc *sc = device_get_softc(dev); 208187866Semax uint8_t iface_index; 209187866Semax int error; 210184610Salfred 211184610Salfred sc->sc_udev = uaa->device; 212184610Salfred 213194228Sthompsa device_set_usb_desc(dev); 214184610Salfred 215184610Salfred mtx_init(&sc->sc_mtx, "ubtbcmfw lock", NULL, MTX_DEF | MTX_RECURSE); 216184610Salfred 217184610Salfred iface_index = UBTBCMFW_IFACE_IDX; 218194228Sthompsa error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer, 219187866Semax ubtbcmfw_config, UBTBCMFW_N_TRANSFER, 220187866Semax sc, &sc->sc_mtx); 221187866Semax if (error != 0) { 222187866Semax device_printf(dev, "allocating USB transfers failed. %s\n", 223194228Sthompsa usbd_errstr(error)); 224184610Salfred goto detach; 225184610Salfred } 226187866Semax 227194228Sthompsa error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx, 228187866Semax &ubtbcmfw_fifo_methods, &sc->sc_fifo, 229189110Sthompsa device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex, 230189110Sthompsa UID_ROOT, GID_OPERATOR, 0644); 231187866Semax if (error != 0) { 232187866Semax device_printf(dev, "could not attach fifo. %s\n", 233194228Sthompsa usbd_errstr(error)); 234184610Salfred goto detach; 235184610Salfred } 236184610Salfred 237187866Semax return (0); /* success */ 238187866Semax 239184610Salfreddetach: 240184610Salfred ubtbcmfw_detach(dev); 241184610Salfred 242187866Semax return (ENXIO); /* failure */ 243187866Semax} /* ubtbcmfw_attach */ 244187866Semax 245184610Salfred/* 246184610Salfred * Detach the device 247184610Salfred */ 248184610Salfred 249184610Salfredstatic int 250184610Salfredubtbcmfw_detach(device_t dev) 251184610Salfred{ 252187866Semax struct ubtbcmfw_softc *sc = device_get_softc(dev); 253184610Salfred 254194228Sthompsa usb_fifo_detach(&sc->sc_fifo); 255184610Salfred 256194228Sthompsa usbd_transfer_unsetup(sc->sc_xfer, UBTBCMFW_N_TRANSFER); 257184610Salfred 258184610Salfred mtx_destroy(&sc->sc_mtx); 259184610Salfred 260184610Salfred return (0); 261187866Semax} /* ubtbcmfw_detach */ 262184610Salfred 263187866Semax/* 264187866Semax * USB write callback 265187866Semax */ 266187866Semax 267184610Salfredstatic void 268194677Sthompsaubtbcmfw_write_callback(struct usb_xfer *xfer, usb_error_t error) 269184610Salfred{ 270194677Sthompsa struct ubtbcmfw_softc *sc = usbd_xfer_softc(xfer); 271192984Sthompsa struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_TX]; 272194677Sthompsa struct usb_page_cache *pc; 273187866Semax uint32_t actlen; 274184610Salfred 275184610Salfred switch (USB_GET_STATE(xfer)) { 276187866Semax case USB_ST_SETUP: 277184610Salfred case USB_ST_TRANSFERRED: 278187866Semaxsetup_next: 279194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 280194677Sthompsa if (usb_fifo_get_data(f, pc, 0, usbd_xfer_max_len(xfer), 281194677Sthompsa &actlen, 0)) { 282194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, actlen); 283194228Sthompsa usbd_transfer_submit(xfer); 284184610Salfred } 285187866Semax break; 286184610Salfred 287187866Semax default: /* Error */ 288194677Sthompsa if (error != USB_ERR_CANCELLED) { 289184610Salfred /* try to clear stall first */ 290194677Sthompsa usbd_xfer_set_stall(xfer); 291187866Semax goto setup_next; 292184610Salfred } 293187866Semax break; 294184610Salfred } 295187866Semax} /* ubtbcmfw_write_callback */ 296184610Salfred 297187866Semax/* 298187866Semax * USB read callback 299187866Semax */ 300184610Salfred 301184610Salfredstatic void 302194677Sthompsaubtbcmfw_read_callback(struct usb_xfer *xfer, usb_error_t error) 303184610Salfred{ 304194677Sthompsa struct ubtbcmfw_softc *sc = usbd_xfer_softc(xfer); 305192984Sthompsa struct usb_fifo *fifo = sc->sc_fifo.fp[USB_FIFO_RX]; 306194677Sthompsa struct usb_page_cache *pc; 307194677Sthompsa int actlen; 308184610Salfred 309194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 310194677Sthompsa 311184610Salfred switch (USB_GET_STATE(xfer)) { 312184610Salfred case USB_ST_TRANSFERRED: 313194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 314194677Sthompsa usb_fifo_put_data(fifo, pc, 0, actlen, 1); 315187866Semax /* FALLTHROUGH */ 316184610Salfred 317184610Salfred case USB_ST_SETUP: 318187866Semaxsetup_next: 319194228Sthompsa if (usb_fifo_put_bytes_max(fifo) > 0) { 320194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 321194228Sthompsa usbd_transfer_submit(xfer); 322184610Salfred } 323187866Semax break; 324184610Salfred 325187866Semax default: /* Error */ 326194677Sthompsa if (error != USB_ERR_CANCELLED) { 327184610Salfred /* try to clear stall first */ 328194677Sthompsa usbd_xfer_set_stall(xfer); 329187866Semax goto setup_next; 330184610Salfred } 331187866Semax break; 332184610Salfred } 333187866Semax} /* ubtbcmfw_read_callback */ 334184610Salfred 335187866Semax/* 336187866Semax * Called when we about to start read()ing from the device 337187866Semax */ 338184610Salfred 339184610Salfredstatic void 340192984Sthompsaubtbcmfw_start_read(struct usb_fifo *fifo) 341184610Salfred{ 342194677Sthompsa struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo); 343184610Salfred 344194228Sthompsa usbd_transfer_start(sc->sc_xfer[UBTBCMFW_INTR_DT_RD]); 345187866Semax} /* ubtbcmfw_start_read */ 346184610Salfred 347187866Semax/* 348187866Semax * Called when we about to stop reading (i.e. closing fifo) 349187866Semax */ 350187866Semax 351184610Salfredstatic void 352192984Sthompsaubtbcmfw_stop_read(struct usb_fifo *fifo) 353184610Salfred{ 354194677Sthompsa struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo); 355184610Salfred 356194228Sthompsa usbd_transfer_stop(sc->sc_xfer[UBTBCMFW_INTR_DT_RD]); 357187866Semax} /* ubtbcmfw_stop_read */ 358184610Salfred 359187866Semax/* 360187866Semax * Called when we about to start write()ing to the device, poll()ing 361187866Semax * for write or flushing fifo 362187866Semax */ 363187866Semax 364184610Salfredstatic void 365192984Sthompsaubtbcmfw_start_write(struct usb_fifo *fifo) 366184610Salfred{ 367194677Sthompsa struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo); 368184610Salfred 369194228Sthompsa usbd_transfer_start(sc->sc_xfer[UBTBCMFW_BULK_DT_WR]); 370187866Semax} /* ubtbcmfw_start_write */ 371184610Salfred 372187866Semax/* 373187866Semax * Called when we about to stop writing (i.e. closing fifo) 374187866Semax */ 375187866Semax 376184610Salfredstatic void 377192984Sthompsaubtbcmfw_stop_write(struct usb_fifo *fifo) 378184610Salfred{ 379194677Sthompsa struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo); 380184610Salfred 381194228Sthompsa usbd_transfer_stop(sc->sc_xfer[UBTBCMFW_BULK_DT_WR]); 382187866Semax} /* ubtbcmfw_stop_write */ 383184610Salfred 384187866Semax/* 385187866Semax * Called when fifo is open 386187866Semax */ 387187866Semax 388184610Salfredstatic int 389192984Sthompsaubtbcmfw_open(struct usb_fifo *fifo, int fflags) 390184610Salfred{ 391194677Sthompsa struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo); 392192984Sthompsa struct usb_xfer *xfer; 393184610Salfred 394187866Semax /* 395187866Semax * f_open fifo method can only be called with either FREAD 396187866Semax * or FWRITE flag set at one time. 397187866Semax */ 398187866Semax 399187866Semax if (fflags & FREAD) 400187866Semax xfer = sc->sc_xfer[UBTBCMFW_INTR_DT_RD]; 401187866Semax else if (fflags & FWRITE) 402187866Semax xfer = sc->sc_xfer[UBTBCMFW_BULK_DT_WR]; 403187866Semax else 404187994Salfred return (EINVAL); /* should not happen */ 405187994Salfred 406194677Sthompsa if (usb_fifo_alloc_buffer(fifo, usbd_xfer_max_len(xfer), 407187866Semax UBTBCMFW_IFQ_MAXLEN) != 0) 408187866Semax return (ENOMEM); 409187866Semax 410184610Salfred return (0); 411187866Semax} /* ubtbcmfw_open */ 412184610Salfred 413187866Semax/* 414187866Semax * Called when fifo is closed 415187866Semax */ 416187866Semax 417184610Salfredstatic void 418192984Sthompsaubtbcmfw_close(struct usb_fifo *fifo, int fflags) 419184610Salfred{ 420187866Semax if (fflags & (FREAD | FWRITE)) 421194228Sthompsa usb_fifo_free_buffer(fifo); 422187866Semax} /* ubtbcmfw_close */ 423184610Salfred 424187866Semax/* 425187866Semax * Process ioctl() on USB device 426187866Semax */ 427187866Semax 428184610Salfredstatic int 429192984Sthompsaubtbcmfw_ioctl(struct usb_fifo *fifo, u_long cmd, void *data, 430189110Sthompsa int fflags) 431184610Salfred{ 432194677Sthompsa struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo); 433187866Semax int error = 0; 434184610Salfred 435184610Salfred switch (cmd) { 436184610Salfred case USB_GET_DEVICE_DESC: 437194228Sthompsa memcpy(data, usbd_get_device_descriptor(sc->sc_udev), 438192984Sthompsa sizeof(struct usb_device_descriptor)); 439184610Salfred break; 440184610Salfred 441184610Salfred default: 442184610Salfred error = EINVAL; 443184610Salfred break; 444184610Salfred } 445187866Semax 446184610Salfred return (error); 447187866Semax} /* ubtbcmfw_ioctl */ 448