ugensa.c revision 239299
1/* $FreeBSD: head/sys/dev/usb/serial/ugensa.c 239299 2012-08-15 15:42:57Z hselasky $ */ 2/* $NetBSD: ugensa.c,v 1.9.2.1 2007/03/24 14:55:50 yamt Exp $ */ 3 4/* 5 * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Roland C. Dowdeswell <elric@netbsd.org>. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33/* 34 * NOTE: all function names beginning like "ugensa_cfg_" can only 35 * be called from within the config thread function ! 36 */ 37 38#include <sys/stdint.h> 39#include <sys/stddef.h> 40#include <sys/param.h> 41#include <sys/queue.h> 42#include <sys/types.h> 43#include <sys/systm.h> 44#include <sys/kernel.h> 45#include <sys/bus.h> 46#include <sys/module.h> 47#include <sys/lock.h> 48#include <sys/mutex.h> 49#include <sys/condvar.h> 50#include <sys/sysctl.h> 51#include <sys/sx.h> 52#include <sys/unistd.h> 53#include <sys/callout.h> 54#include <sys/malloc.h> 55#include <sys/priv.h> 56 57#include <dev/usb/usb.h> 58#include <dev/usb/usbdi.h> 59#include "usbdevs.h" 60 61#define USB_DEBUG_VAR usb_debug 62#include <dev/usb/usb_debug.h> 63#include <dev/usb/usb_process.h> 64 65#include <dev/usb/serial/usb_serial.h> 66 67#define UGENSA_BUF_SIZE 2048 /* bytes */ 68#define UGENSA_CONFIG_INDEX 0 69#define UGENSA_IFACE_INDEX 0 70#define UGENSA_IFACE_MAX 8 /* exclusivly */ 71 72enum { 73 UGENSA_BULK_DT_WR, 74 UGENSA_BULK_DT_RD, 75 UGENSA_N_TRANSFER, 76}; 77 78struct ugensa_sub_softc { 79 struct ucom_softc *sc_ucom_ptr; 80 struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER]; 81}; 82 83struct ugensa_softc { 84 struct ucom_super_softc sc_super_ucom; 85 struct ucom_softc sc_ucom[UGENSA_IFACE_MAX]; 86 struct ugensa_sub_softc sc_sub[UGENSA_IFACE_MAX]; 87 88 struct mtx sc_mtx; 89 uint8_t sc_niface; 90}; 91 92/* prototypes */ 93 94static device_probe_t ugensa_probe; 95static device_attach_t ugensa_attach; 96static device_detach_t ugensa_detach; 97static void ugensa_free_softc(struct ugensa_softc *); 98 99static usb_callback_t ugensa_bulk_write_callback; 100static usb_callback_t ugensa_bulk_read_callback; 101 102static void ugensa_free(struct ucom_softc *); 103static void ugensa_start_read(struct ucom_softc *); 104static void ugensa_stop_read(struct ucom_softc *); 105static void ugensa_start_write(struct ucom_softc *); 106static void ugensa_stop_write(struct ucom_softc *); 107static void ugensa_poll(struct ucom_softc *ucom); 108 109static const struct usb_config ugensa_xfer_config[UGENSA_N_TRANSFER] = { 110 111 [UGENSA_BULK_DT_WR] = { 112 .type = UE_BULK, 113 .endpoint = UE_ADDR_ANY, 114 .direction = UE_DIR_OUT, 115 .bufsize = UGENSA_BUF_SIZE, 116 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 117 .callback = &ugensa_bulk_write_callback, 118 }, 119 120 [UGENSA_BULK_DT_RD] = { 121 .type = UE_BULK, 122 .endpoint = UE_ADDR_ANY, 123 .direction = UE_DIR_IN, 124 .bufsize = UGENSA_BUF_SIZE, 125 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 126 .callback = &ugensa_bulk_read_callback, 127 }, 128}; 129 130static const struct ucom_callback ugensa_callback = { 131 .ucom_start_read = &ugensa_start_read, 132 .ucom_stop_read = &ugensa_stop_read, 133 .ucom_start_write = &ugensa_start_write, 134 .ucom_stop_write = &ugensa_stop_write, 135 .ucom_poll = &ugensa_poll, 136 .ucom_free = &ugensa_free, 137}; 138 139static device_method_t ugensa_methods[] = { 140 /* Device methods */ 141 DEVMETHOD(device_probe, ugensa_probe), 142 DEVMETHOD(device_attach, ugensa_attach), 143 DEVMETHOD(device_detach, ugensa_detach), 144 DEVMETHOD_END 145}; 146 147static devclass_t ugensa_devclass; 148 149static driver_t ugensa_driver = { 150 .name = "ugensa", 151 .methods = ugensa_methods, 152 .size = sizeof(struct ugensa_softc), 153}; 154 155DRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0); 156MODULE_DEPEND(ugensa, ucom, 1, 1, 1); 157MODULE_DEPEND(ugensa, usb, 1, 1, 1); 158MODULE_VERSION(ugensa, 1); 159 160static const STRUCT_USB_HOST_ID ugensa_devs[] = { 161 {USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)}, 162 {USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)}, 163 {USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)}, 164 {USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)}, 165 {USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)}, 166}; 167 168static int 169ugensa_probe(device_t dev) 170{ 171 struct usb_attach_arg *uaa = device_get_ivars(dev); 172 173 if (uaa->usb_mode != USB_MODE_HOST) { 174 return (ENXIO); 175 } 176 if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) { 177 return (ENXIO); 178 } 179 if (uaa->info.bIfaceIndex != 0) { 180 return (ENXIO); 181 } 182 return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa)); 183} 184 185static int 186ugensa_attach(device_t dev) 187{ 188 struct usb_attach_arg *uaa = device_get_ivars(dev); 189 struct ugensa_softc *sc = device_get_softc(dev); 190 struct ugensa_sub_softc *ssc; 191 struct usb_interface *iface; 192 int32_t error; 193 uint8_t iface_index; 194 int x, cnt; 195 196 device_set_usb_desc(dev); 197 mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF); 198 ucom_ref(&sc->sc_super_ucom); 199 200 /* Figure out how many interfaces this device has got */ 201 for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) { 202 if ((usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) || 203 (usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) { 204 /* we have reached the end */ 205 break; 206 } 207 } 208 209 if (cnt == 0) { 210 device_printf(dev, "No interfaces\n"); 211 goto detach; 212 } 213 for (x = 0; x < cnt; x++) { 214 iface = usbd_get_iface(uaa->device, x); 215 if (iface->idesc->bInterfaceClass != UICLASS_VENDOR) 216 /* Not a serial port, most likely a SD reader */ 217 continue; 218 219 ssc = sc->sc_sub + sc->sc_niface; 220 ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_niface; 221 222 iface_index = (UGENSA_IFACE_INDEX + x); 223 error = usbd_transfer_setup(uaa->device, 224 &iface_index, ssc->sc_xfer, ugensa_xfer_config, 225 UGENSA_N_TRANSFER, ssc, &sc->sc_mtx); 226 227 if (error) { 228 device_printf(dev, "allocating USB " 229 "transfers failed\n"); 230 goto detach; 231 } 232 /* clear stall at first run */ 233 mtx_lock(&sc->sc_mtx); 234 usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]); 235 usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]); 236 mtx_unlock(&sc->sc_mtx); 237 238 /* initialize port number */ 239 ssc->sc_ucom_ptr->sc_portno = sc->sc_niface; 240 sc->sc_niface++; 241 if (x != uaa->info.bIfaceIndex) 242 usbd_set_parent_iface(uaa->device, x, 243 uaa->info.bIfaceIndex); 244 } 245 device_printf(dev, "Found %d interfaces.\n", sc->sc_niface); 246 247 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc, 248 &ugensa_callback, &sc->sc_mtx); 249 if (error) { 250 DPRINTF("attach failed\n"); 251 goto detach; 252 } 253 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 254 255 return (0); /* success */ 256 257detach: 258 ugensa_detach(dev); 259 return (ENXIO); /* failure */ 260} 261 262static int 263ugensa_detach(device_t dev) 264{ 265 struct ugensa_softc *sc = device_get_softc(dev); 266 uint8_t x; 267 268 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom); 269 270 for (x = 0; x < sc->sc_niface; x++) { 271 usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER); 272 } 273 274 device_claim_softc(dev); 275 276 ugensa_free_softc(sc); 277 278 return (0); 279} 280 281UCOM_UNLOAD_DRAIN(ugensa); 282 283static void 284ugensa_free_softc(struct ugensa_softc *sc) 285{ 286 if (ucom_unref(&sc->sc_super_ucom)) { 287 mtx_destroy(&sc->sc_mtx); 288 device_free_softc(sc); 289 } 290} 291 292static void 293ugensa_free(struct ucom_softc *ucom) 294{ 295 ugensa_free_softc(ucom->sc_parent); 296} 297 298static void 299ugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 300{ 301 struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer); 302 struct usb_page_cache *pc; 303 uint32_t actlen; 304 305 switch (USB_GET_STATE(xfer)) { 306 case USB_ST_SETUP: 307 case USB_ST_TRANSFERRED: 308tr_setup: 309 pc = usbd_xfer_get_frame(xfer, 0); 310 if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0, 311 UGENSA_BUF_SIZE, &actlen)) { 312 usbd_xfer_set_frame_len(xfer, 0, actlen); 313 usbd_transfer_submit(xfer); 314 } 315 return; 316 317 default: /* Error */ 318 if (error != USB_ERR_CANCELLED) { 319 /* try to clear stall first */ 320 usbd_xfer_set_stall(xfer); 321 goto tr_setup; 322 } 323 return; 324 } 325} 326 327static void 328ugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 329{ 330 struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer); 331 struct usb_page_cache *pc; 332 int actlen; 333 334 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 335 336 switch (USB_GET_STATE(xfer)) { 337 case USB_ST_TRANSFERRED: 338 pc = usbd_xfer_get_frame(xfer, 0); 339 ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen); 340 341 case USB_ST_SETUP: 342tr_setup: 343 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 344 usbd_transfer_submit(xfer); 345 return; 346 347 default: /* Error */ 348 if (error != USB_ERR_CANCELLED) { 349 /* try to clear stall first */ 350 usbd_xfer_set_stall(xfer); 351 goto tr_setup; 352 } 353 return; 354 } 355} 356 357static void 358ugensa_start_read(struct ucom_softc *ucom) 359{ 360 struct ugensa_softc *sc = ucom->sc_parent; 361 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 362 363 usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]); 364} 365 366static void 367ugensa_stop_read(struct ucom_softc *ucom) 368{ 369 struct ugensa_softc *sc = ucom->sc_parent; 370 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 371 372 usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]); 373} 374 375static void 376ugensa_start_write(struct ucom_softc *ucom) 377{ 378 struct ugensa_softc *sc = ucom->sc_parent; 379 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 380 381 usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]); 382} 383 384static void 385ugensa_stop_write(struct ucom_softc *ucom) 386{ 387 struct ugensa_softc *sc = ucom->sc_parent; 388 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 389 390 usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]); 391} 392 393static void 394ugensa_poll(struct ucom_softc *ucom) 395{ 396 struct ugensa_softc *sc = ucom->sc_parent; 397 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 398 399 usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER); 400} 401