ugensa.c revision 214761
1184610Salfred/* $FreeBSD: head/sys/dev/usb/serial/ugensa.c 214761 2010-11-03 21:50:49Z n_hibma $ */ 2184610Salfred/* $NetBSD: ugensa.c,v 1.9.2.1 2007/03/24 14:55:50 yamt Exp $ */ 3184610Salfred 4184610Salfred/* 5184610Salfred * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc. 6184610Salfred * All rights reserved. 7184610Salfred * 8184610Salfred * This code is derived from software contributed to The NetBSD Foundation 9184610Salfred * by Roland C. Dowdeswell <elric@netbsd.org>. 10184610Salfred * 11184610Salfred * Redistribution and use in source and binary forms, with or without 12184610Salfred * modification, are permitted provided that the following conditions 13184610Salfred * are met: 14184610Salfred * 1. Redistributions of source code must retain the above copyright 15184610Salfred * notice, this list of conditions and the following disclaimer. 16184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 17184610Salfred * notice, this list of conditions and the following disclaimer in the 18184610Salfred * documentation and/or other materials provided with the distribution. 19184610Salfred * 20184610Salfred * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21184610Salfred * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22184610Salfred * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23184610Salfred * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24184610Salfred * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25184610Salfred * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26184610Salfred * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27184610Salfred * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28184610Salfred * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29184610Salfred * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30184610Salfred * POSSIBILITY OF SUCH DAMAGE. 31184610Salfred */ 32184610Salfred 33184610Salfred/* 34184610Salfred * NOTE: all function names beginning like "ugensa_cfg_" can only 35184610Salfred * be called from within the config thread function ! 36184610Salfred */ 37184610Salfred 38194677Sthompsa#include <sys/stdint.h> 39194677Sthompsa#include <sys/stddef.h> 40194677Sthompsa#include <sys/param.h> 41194677Sthompsa#include <sys/queue.h> 42194677Sthompsa#include <sys/types.h> 43194677Sthompsa#include <sys/systm.h> 44194677Sthompsa#include <sys/kernel.h> 45194677Sthompsa#include <sys/bus.h> 46194677Sthompsa#include <sys/linker_set.h> 47194677Sthompsa#include <sys/module.h> 48194677Sthompsa#include <sys/lock.h> 49194677Sthompsa#include <sys/mutex.h> 50194677Sthompsa#include <sys/condvar.h> 51194677Sthompsa#include <sys/sysctl.h> 52194677Sthompsa#include <sys/sx.h> 53194677Sthompsa#include <sys/unistd.h> 54194677Sthompsa#include <sys/callout.h> 55194677Sthompsa#include <sys/malloc.h> 56194677Sthompsa#include <sys/priv.h> 57194677Sthompsa 58194677Sthompsa#include <dev/usb/usb.h> 59194677Sthompsa#include <dev/usb/usbdi.h> 60188746Sthompsa#include "usbdevs.h" 61184610Salfred 62194228Sthompsa#define USB_DEBUG_VAR usb_debug 63188942Sthompsa#include <dev/usb/usb_debug.h> 64188942Sthompsa#include <dev/usb/usb_process.h> 65184610Salfred 66188942Sthompsa#include <dev/usb/serial/usb_serial.h> 67184610Salfred 68184610Salfred#define UGENSA_BUF_SIZE 2048 /* bytes */ 69184610Salfred#define UGENSA_CONFIG_INDEX 0 70184610Salfred#define UGENSA_IFACE_INDEX 0 71184610Salfred#define UGENSA_IFACE_MAX 8 /* exclusivly */ 72184610Salfred 73187259Sthompsaenum { 74187259Sthompsa UGENSA_BULK_DT_WR, 75187259Sthompsa UGENSA_BULK_DT_RD, 76188413Sthompsa UGENSA_N_TRANSFER, 77187259Sthompsa}; 78187259Sthompsa 79184610Salfredstruct ugensa_sub_softc { 80194228Sthompsa struct ucom_softc *sc_ucom_ptr; 81192984Sthompsa struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER]; 82184610Salfred}; 83184610Salfred 84184610Salfredstruct ugensa_softc { 85192984Sthompsa struct ucom_super_softc sc_super_ucom; 86192984Sthompsa struct ucom_softc sc_ucom[UGENSA_IFACE_MAX]; 87184610Salfred struct ugensa_sub_softc sc_sub[UGENSA_IFACE_MAX]; 88184610Salfred 89184610Salfred struct mtx sc_mtx; 90184610Salfred uint8_t sc_niface; 91184610Salfred}; 92184610Salfred 93184610Salfred/* prototypes */ 94184610Salfred 95184610Salfredstatic device_probe_t ugensa_probe; 96184610Salfredstatic device_attach_t ugensa_attach; 97184610Salfredstatic device_detach_t ugensa_detach; 98184610Salfred 99193045Sthompsastatic usb_callback_t ugensa_bulk_write_callback; 100193045Sthompsastatic usb_callback_t ugensa_bulk_read_callback; 101184610Salfred 102192984Sthompsastatic void ugensa_start_read(struct ucom_softc *); 103192984Sthompsastatic void ugensa_stop_read(struct ucom_softc *); 104192984Sthompsastatic void ugensa_start_write(struct ucom_softc *); 105192984Sthompsastatic void ugensa_stop_write(struct ucom_softc *); 106197570Sthompsastatic void ugensa_poll(struct ucom_softc *ucom); 107184610Salfred 108197570Sthompsastatic const struct usb_config ugensa_xfer_config[UGENSA_N_TRANSFER] = { 109184610Salfred 110187259Sthompsa [UGENSA_BULK_DT_WR] = { 111184610Salfred .type = UE_BULK, 112184610Salfred .endpoint = UE_ADDR_ANY, 113184610Salfred .direction = UE_DIR_OUT, 114190734Sthompsa .bufsize = UGENSA_BUF_SIZE, 115190734Sthompsa .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 116190734Sthompsa .callback = &ugensa_bulk_write_callback, 117184610Salfred }, 118184610Salfred 119187259Sthompsa [UGENSA_BULK_DT_RD] = { 120184610Salfred .type = UE_BULK, 121184610Salfred .endpoint = UE_ADDR_ANY, 122184610Salfred .direction = UE_DIR_IN, 123190734Sthompsa .bufsize = UGENSA_BUF_SIZE, 124190734Sthompsa .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 125190734Sthompsa .callback = &ugensa_bulk_read_callback, 126184610Salfred }, 127184610Salfred}; 128184610Salfred 129192984Sthompsastatic const struct ucom_callback ugensa_callback = { 130194228Sthompsa .ucom_start_read = &ugensa_start_read, 131194228Sthompsa .ucom_stop_read = &ugensa_stop_read, 132194228Sthompsa .ucom_start_write = &ugensa_start_write, 133194228Sthompsa .ucom_stop_write = &ugensa_stop_write, 134197570Sthompsa .ucom_poll = &ugensa_poll, 135184610Salfred}; 136184610Salfred 137184610Salfredstatic device_method_t ugensa_methods[] = { 138184610Salfred /* Device methods */ 139184610Salfred DEVMETHOD(device_probe, ugensa_probe), 140184610Salfred DEVMETHOD(device_attach, ugensa_attach), 141184610Salfred DEVMETHOD(device_detach, ugensa_detach), 142184610Salfred {0, 0} 143184610Salfred}; 144184610Salfred 145184610Salfredstatic devclass_t ugensa_devclass; 146184610Salfred 147184610Salfredstatic driver_t ugensa_driver = { 148184610Salfred .name = "ugensa", 149184610Salfred .methods = ugensa_methods, 150184610Salfred .size = sizeof(struct ugensa_softc), 151184610Salfred}; 152184610Salfred 153189275SthompsaDRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0); 154188942SthompsaMODULE_DEPEND(ugensa, ucom, 1, 1, 1); 155188942SthompsaMODULE_DEPEND(ugensa, usb, 1, 1, 1); 156212122SthompsaMODULE_VERSION(ugensa, 1); 157184610Salfred 158192984Sthompsastatic const struct usb_device_id ugensa_devs[] = { 159184610Salfred {USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)}, 160184610Salfred {USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)}, 161184610Salfred {USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)}, 162184610Salfred {USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)}, 163184610Salfred {USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)}, 164184610Salfred}; 165184610Salfred 166184610Salfredstatic int 167184610Salfredugensa_probe(device_t dev) 168184610Salfred{ 169192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 170184610Salfred 171192499Sthompsa if (uaa->usb_mode != USB_MODE_HOST) { 172184610Salfred return (ENXIO); 173184610Salfred } 174184610Salfred if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) { 175184610Salfred return (ENXIO); 176184610Salfred } 177184610Salfred if (uaa->info.bIfaceIndex != 0) { 178184610Salfred return (ENXIO); 179184610Salfred } 180194228Sthompsa return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa)); 181184610Salfred} 182184610Salfred 183184610Salfredstatic int 184184610Salfredugensa_attach(device_t dev) 185184610Salfred{ 186192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 187184610Salfred struct ugensa_softc *sc = device_get_softc(dev); 188184610Salfred struct ugensa_sub_softc *ssc; 189192984Sthompsa struct usb_interface *iface; 190184610Salfred int32_t error; 191184610Salfred uint8_t iface_index; 192184610Salfred int x, cnt; 193184610Salfred 194194228Sthompsa device_set_usb_desc(dev); 195184610Salfred mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF); 196184610Salfred 197184610Salfred /* Figure out how many interfaces this device has got */ 198184610Salfred for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) { 199194228Sthompsa if ((usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) || 200194228Sthompsa (usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) { 201184610Salfred /* we have reached the end */ 202184610Salfred break; 203184610Salfred } 204184610Salfred } 205184610Salfred 206184610Salfred if (cnt == 0) { 207199816Sthompsa device_printf(dev, "No interfaces\n"); 208184610Salfred goto detach; 209184610Salfred } 210184610Salfred for (x = 0; x < cnt; x++) { 211194228Sthompsa iface = usbd_get_iface(uaa->device, x); 212184610Salfred if (iface->idesc->bInterfaceClass != UICLASS_VENDOR) 213184610Salfred /* Not a serial port, most likely a SD reader */ 214184610Salfred continue; 215184610Salfred 216184610Salfred ssc = sc->sc_sub + sc->sc_niface; 217194228Sthompsa ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_niface; 218184610Salfred 219184610Salfred iface_index = (UGENSA_IFACE_INDEX + x); 220194228Sthompsa error = usbd_transfer_setup(uaa->device, 221184610Salfred &iface_index, ssc->sc_xfer, ugensa_xfer_config, 222184610Salfred UGENSA_N_TRANSFER, ssc, &sc->sc_mtx); 223184610Salfred 224184610Salfred if (error) { 225184610Salfred device_printf(dev, "allocating USB " 226199816Sthompsa "transfers failed\n"); 227184610Salfred goto detach; 228184610Salfred } 229184610Salfred /* clear stall at first run */ 230189265Sthompsa mtx_lock(&sc->sc_mtx); 231194677Sthompsa usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]); 232194677Sthompsa usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]); 233189265Sthompsa mtx_unlock(&sc->sc_mtx); 234184610Salfred 235184610Salfred /* initialize port number */ 236194228Sthompsa ssc->sc_ucom_ptr->sc_portno = sc->sc_niface; 237184610Salfred sc->sc_niface++; 238184610Salfred if (x != uaa->info.bIfaceIndex) 239194228Sthompsa usbd_set_parent_iface(uaa->device, x, 240184610Salfred uaa->info.bIfaceIndex); 241184610Salfred } 242184610Salfred device_printf(dev, "Found %d interfaces.\n", sc->sc_niface); 243184610Salfred 244194228Sthompsa error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc, 245184610Salfred &ugensa_callback, &sc->sc_mtx); 246184610Salfred if (error) { 247184610Salfred DPRINTF("attach failed\n"); 248184610Salfred goto detach; 249184610Salfred } 250184610Salfred return (0); /* success */ 251184610Salfred 252184610Salfreddetach: 253184610Salfred ugensa_detach(dev); 254184610Salfred return (ENXIO); /* failure */ 255184610Salfred} 256184610Salfred 257184610Salfredstatic int 258184610Salfredugensa_detach(device_t dev) 259184610Salfred{ 260184610Salfred struct ugensa_softc *sc = device_get_softc(dev); 261184610Salfred uint8_t x; 262184610Salfred 263214761Sn_hibma ucom_detach(&sc->sc_super_ucom, sc->sc_ucom); 264184610Salfred 265184610Salfred for (x = 0; x < sc->sc_niface; x++) { 266194228Sthompsa usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER); 267184610Salfred } 268184610Salfred mtx_destroy(&sc->sc_mtx); 269184610Salfred 270184610Salfred return (0); 271184610Salfred} 272184610Salfred 273184610Salfredstatic void 274194677Sthompsaugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 275184610Salfred{ 276194677Sthompsa struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer); 277194677Sthompsa struct usb_page_cache *pc; 278184610Salfred uint32_t actlen; 279184610Salfred 280184610Salfred switch (USB_GET_STATE(xfer)) { 281184610Salfred case USB_ST_SETUP: 282184610Salfred case USB_ST_TRANSFERRED: 283188413Sthompsatr_setup: 284194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 285194677Sthompsa if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0, 286184610Salfred UGENSA_BUF_SIZE, &actlen)) { 287194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, actlen); 288194228Sthompsa usbd_transfer_submit(xfer); 289184610Salfred } 290184610Salfred return; 291184610Salfred 292184610Salfred default: /* Error */ 293194677Sthompsa if (error != USB_ERR_CANCELLED) { 294188413Sthompsa /* try to clear stall first */ 295194677Sthompsa usbd_xfer_set_stall(xfer); 296188413Sthompsa goto tr_setup; 297184610Salfred } 298184610Salfred return; 299184610Salfred } 300184610Salfred} 301184610Salfred 302184610Salfredstatic void 303194677Sthompsaugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 304184610Salfred{ 305194677Sthompsa struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer); 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 ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen); 315184610Salfred 316184610Salfred case USB_ST_SETUP: 317188413Sthompsatr_setup: 318194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 319194228Sthompsa usbd_transfer_submit(xfer); 320184610Salfred return; 321184610Salfred 322184610Salfred default: /* Error */ 323194677Sthompsa if (error != USB_ERR_CANCELLED) { 324188413Sthompsa /* try to clear stall first */ 325194677Sthompsa usbd_xfer_set_stall(xfer); 326188413Sthompsa goto tr_setup; 327184610Salfred } 328184610Salfred return; 329184610Salfred } 330184610Salfred} 331184610Salfred 332184610Salfredstatic void 333192984Sthompsaugensa_start_read(struct ucom_softc *ucom) 334184610Salfred{ 335184610Salfred struct ugensa_softc *sc = ucom->sc_parent; 336184610Salfred struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 337184610Salfred 338194228Sthompsa usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]); 339184610Salfred} 340184610Salfred 341184610Salfredstatic void 342192984Sthompsaugensa_stop_read(struct ucom_softc *ucom) 343184610Salfred{ 344184610Salfred struct ugensa_softc *sc = ucom->sc_parent; 345184610Salfred struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 346184610Salfred 347194228Sthompsa usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]); 348184610Salfred} 349184610Salfred 350184610Salfredstatic void 351192984Sthompsaugensa_start_write(struct ucom_softc *ucom) 352184610Salfred{ 353184610Salfred struct ugensa_softc *sc = ucom->sc_parent; 354184610Salfred struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 355184610Salfred 356194228Sthompsa usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]); 357184610Salfred} 358184610Salfred 359184610Salfredstatic void 360192984Sthompsaugensa_stop_write(struct ucom_softc *ucom) 361184610Salfred{ 362184610Salfred struct ugensa_softc *sc = ucom->sc_parent; 363184610Salfred struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 364184610Salfred 365194228Sthompsa usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]); 366184610Salfred} 367197570Sthompsa 368197570Sthompsastatic void 369197570Sthompsaugensa_poll(struct ucom_softc *ucom) 370197570Sthompsa{ 371197570Sthompsa struct ugensa_softc *sc = ucom->sc_parent; 372197570Sthompsa struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 373197570Sthompsa 374197570Sthompsa usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER); 375197570Sthompsa} 376