1184610Salfred/* $FreeBSD: stable/11/sys/dev/usb/serial/ugensa.c 366877 2020-10-20 08:15:12Z hselasky $ */ 2184610Salfred/* $NetBSD: ugensa.c,v 1.9.2.1 2007/03/24 14:55:50 yamt Exp $ */ 3184610Salfred 4331722Seadler/* 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/module.h> 47194677Sthompsa#include <sys/lock.h> 48194677Sthompsa#include <sys/mutex.h> 49194677Sthompsa#include <sys/condvar.h> 50194677Sthompsa#include <sys/sysctl.h> 51194677Sthompsa#include <sys/sx.h> 52194677Sthompsa#include <sys/unistd.h> 53194677Sthompsa#include <sys/callout.h> 54194677Sthompsa#include <sys/malloc.h> 55194677Sthompsa#include <sys/priv.h> 56194677Sthompsa 57194677Sthompsa#include <dev/usb/usb.h> 58194677Sthompsa#include <dev/usb/usbdi.h> 59188746Sthompsa#include "usbdevs.h" 60184610Salfred 61194228Sthompsa#define USB_DEBUG_VAR usb_debug 62188942Sthompsa#include <dev/usb/usb_debug.h> 63188942Sthompsa#include <dev/usb/usb_process.h> 64184610Salfred 65188942Sthompsa#include <dev/usb/serial/usb_serial.h> 66184610Salfred 67184610Salfred#define UGENSA_BUF_SIZE 2048 /* bytes */ 68184610Salfred#define UGENSA_CONFIG_INDEX 0 69184610Salfred#define UGENSA_IFACE_INDEX 0 70184610Salfred#define UGENSA_IFACE_MAX 8 /* exclusivly */ 71184610Salfred 72187259Sthompsaenum { 73187259Sthompsa UGENSA_BULK_DT_WR, 74187259Sthompsa UGENSA_BULK_DT_RD, 75188413Sthompsa UGENSA_N_TRANSFER, 76187259Sthompsa}; 77187259Sthompsa 78184610Salfredstruct ugensa_sub_softc { 79194228Sthompsa struct ucom_softc *sc_ucom_ptr; 80192984Sthompsa struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER]; 81184610Salfred}; 82184610Salfred 83184610Salfredstruct ugensa_softc { 84192984Sthompsa struct ucom_super_softc sc_super_ucom; 85192984Sthompsa struct ucom_softc sc_ucom[UGENSA_IFACE_MAX]; 86184610Salfred struct ugensa_sub_softc sc_sub[UGENSA_IFACE_MAX]; 87184610Salfred 88184610Salfred struct mtx sc_mtx; 89184610Salfred uint8_t sc_niface; 90184610Salfred}; 91184610Salfred 92184610Salfred/* prototypes */ 93184610Salfred 94184610Salfredstatic device_probe_t ugensa_probe; 95184610Salfredstatic device_attach_t ugensa_attach; 96184610Salfredstatic device_detach_t ugensa_detach; 97239299Shselaskystatic void ugensa_free_softc(struct ugensa_softc *); 98184610Salfred 99193045Sthompsastatic usb_callback_t ugensa_bulk_write_callback; 100193045Sthompsastatic usb_callback_t ugensa_bulk_read_callback; 101184610Salfred 102239180Shselaskystatic void ugensa_free(struct ucom_softc *); 103192984Sthompsastatic void ugensa_start_read(struct ucom_softc *); 104192984Sthompsastatic void ugensa_stop_read(struct ucom_softc *); 105192984Sthompsastatic void ugensa_start_write(struct ucom_softc *); 106192984Sthompsastatic void ugensa_stop_write(struct ucom_softc *); 107197570Sthompsastatic void ugensa_poll(struct ucom_softc *ucom); 108184610Salfred 109197570Sthompsastatic const struct usb_config ugensa_xfer_config[UGENSA_N_TRANSFER] = { 110184610Salfred 111187259Sthompsa [UGENSA_BULK_DT_WR] = { 112184610Salfred .type = UE_BULK, 113184610Salfred .endpoint = UE_ADDR_ANY, 114184610Salfred .direction = UE_DIR_OUT, 115190734Sthompsa .bufsize = UGENSA_BUF_SIZE, 116190734Sthompsa .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 117190734Sthompsa .callback = &ugensa_bulk_write_callback, 118184610Salfred }, 119184610Salfred 120187259Sthompsa [UGENSA_BULK_DT_RD] = { 121184610Salfred .type = UE_BULK, 122184610Salfred .endpoint = UE_ADDR_ANY, 123184610Salfred .direction = UE_DIR_IN, 124190734Sthompsa .bufsize = UGENSA_BUF_SIZE, 125190734Sthompsa .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 126190734Sthompsa .callback = &ugensa_bulk_read_callback, 127184610Salfred }, 128184610Salfred}; 129184610Salfred 130192984Sthompsastatic const struct ucom_callback ugensa_callback = { 131194228Sthompsa .ucom_start_read = &ugensa_start_read, 132194228Sthompsa .ucom_stop_read = &ugensa_stop_read, 133194228Sthompsa .ucom_start_write = &ugensa_start_write, 134194228Sthompsa .ucom_stop_write = &ugensa_stop_write, 135197570Sthompsa .ucom_poll = &ugensa_poll, 136239180Shselasky .ucom_free = &ugensa_free, 137184610Salfred}; 138184610Salfred 139184610Salfredstatic device_method_t ugensa_methods[] = { 140184610Salfred /* Device methods */ 141184610Salfred DEVMETHOD(device_probe, ugensa_probe), 142184610Salfred DEVMETHOD(device_attach, ugensa_attach), 143184610Salfred DEVMETHOD(device_detach, ugensa_detach), 144239180Shselasky DEVMETHOD_END 145184610Salfred}; 146184610Salfred 147184610Salfredstatic devclass_t ugensa_devclass; 148184610Salfred 149184610Salfredstatic driver_t ugensa_driver = { 150184610Salfred .name = "ugensa", 151184610Salfred .methods = ugensa_methods, 152184610Salfred .size = sizeof(struct ugensa_softc), 153184610Salfred}; 154184610Salfred 155223486Shselaskystatic const STRUCT_USB_HOST_ID ugensa_devs[] = { 156184610Salfred {USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)}, 157184610Salfred {USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)}, 158184610Salfred {USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)}, 159184610Salfred {USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)}, 160184610Salfred {USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)}, 161366877Shselasky {USB_VENDOR(USB_VENDOR_GOOGLE), USB_IFACE_CLASS(UICLASS_VENDOR), 162366877Shselasky USB_IFACE_SUBCLASS(0x50), USB_IFACE_PROTOCOL(0x01), USB_DRIVER_INFO(10)}, 163184610Salfred}; 164184610Salfred 165292080SimpDRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0); 166292080SimpMODULE_DEPEND(ugensa, ucom, 1, 1, 1); 167292080SimpMODULE_DEPEND(ugensa, usb, 1, 1, 1); 168292080SimpMODULE_VERSION(ugensa, 1); 169292080SimpUSB_PNP_HOST_INFO(ugensa_devs); 170292080Simp 171184610Salfredstatic int 172184610Salfredugensa_probe(device_t dev) 173184610Salfred{ 174192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 175184610Salfred 176192499Sthompsa if (uaa->usb_mode != USB_MODE_HOST) { 177184610Salfred return (ENXIO); 178184610Salfred } 179184610Salfred if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) { 180184610Salfred return (ENXIO); 181184610Salfred } 182184610Salfred if (uaa->info.bIfaceIndex != 0) { 183184610Salfred return (ENXIO); 184184610Salfred } 185194228Sthompsa return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa)); 186184610Salfred} 187184610Salfred 188184610Salfredstatic int 189184610Salfredugensa_attach(device_t dev) 190184610Salfred{ 191192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 192184610Salfred struct ugensa_softc *sc = device_get_softc(dev); 193184610Salfred struct ugensa_sub_softc *ssc; 194192984Sthompsa struct usb_interface *iface; 195184610Salfred int32_t error; 196184610Salfred uint8_t iface_index; 197184610Salfred int x, cnt; 198184610Salfred 199194228Sthompsa device_set_usb_desc(dev); 200184610Salfred mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF); 201239180Shselasky ucom_ref(&sc->sc_super_ucom); 202184610Salfred 203184610Salfred /* Figure out how many interfaces this device has got */ 204184610Salfred for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) { 205194228Sthompsa if ((usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) || 206194228Sthompsa (usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) { 207184610Salfred /* we have reached the end */ 208184610Salfred break; 209184610Salfred } 210184610Salfred } 211184610Salfred 212184610Salfred if (cnt == 0) { 213199816Sthompsa device_printf(dev, "No interfaces\n"); 214184610Salfred goto detach; 215184610Salfred } 216184610Salfred for (x = 0; x < cnt; x++) { 217194228Sthompsa iface = usbd_get_iface(uaa->device, x); 218184610Salfred if (iface->idesc->bInterfaceClass != UICLASS_VENDOR) 219184610Salfred /* Not a serial port, most likely a SD reader */ 220184610Salfred continue; 221184610Salfred 222184610Salfred ssc = sc->sc_sub + sc->sc_niface; 223194228Sthompsa ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_niface; 224184610Salfred 225184610Salfred iface_index = (UGENSA_IFACE_INDEX + x); 226194228Sthompsa error = usbd_transfer_setup(uaa->device, 227184610Salfred &iface_index, ssc->sc_xfer, ugensa_xfer_config, 228184610Salfred UGENSA_N_TRANSFER, ssc, &sc->sc_mtx); 229184610Salfred 230184610Salfred if (error) { 231184610Salfred device_printf(dev, "allocating USB " 232199816Sthompsa "transfers failed\n"); 233184610Salfred goto detach; 234184610Salfred } 235184610Salfred /* clear stall at first run */ 236189265Sthompsa mtx_lock(&sc->sc_mtx); 237194677Sthompsa usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]); 238194677Sthompsa usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]); 239189265Sthompsa mtx_unlock(&sc->sc_mtx); 240184610Salfred 241184610Salfred /* initialize port number */ 242194228Sthompsa ssc->sc_ucom_ptr->sc_portno = sc->sc_niface; 243184610Salfred sc->sc_niface++; 244184610Salfred if (x != uaa->info.bIfaceIndex) 245194228Sthompsa usbd_set_parent_iface(uaa->device, x, 246184610Salfred uaa->info.bIfaceIndex); 247184610Salfred } 248184610Salfred device_printf(dev, "Found %d interfaces.\n", sc->sc_niface); 249184610Salfred 250194228Sthompsa error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc, 251184610Salfred &ugensa_callback, &sc->sc_mtx); 252184610Salfred if (error) { 253184610Salfred DPRINTF("attach failed\n"); 254184610Salfred goto detach; 255184610Salfred } 256214843Sn_hibma ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 257214843Sn_hibma 258184610Salfred return (0); /* success */ 259184610Salfred 260184610Salfreddetach: 261184610Salfred ugensa_detach(dev); 262184610Salfred return (ENXIO); /* failure */ 263184610Salfred} 264184610Salfred 265184610Salfredstatic int 266184610Salfredugensa_detach(device_t dev) 267184610Salfred{ 268184610Salfred struct ugensa_softc *sc = device_get_softc(dev); 269184610Salfred uint8_t x; 270184610Salfred 271214761Sn_hibma ucom_detach(&sc->sc_super_ucom, sc->sc_ucom); 272184610Salfred 273184610Salfred for (x = 0; x < sc->sc_niface; x++) { 274194228Sthompsa usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER); 275184610Salfred } 276184610Salfred 277239299Shselasky device_claim_softc(dev); 278239299Shselasky 279239299Shselasky ugensa_free_softc(sc); 280239299Shselasky 281184610Salfred return (0); 282184610Salfred} 283184610Salfred 284239180ShselaskyUCOM_UNLOAD_DRAIN(ugensa); 285239180Shselasky 286184610Salfredstatic void 287239299Shselaskyugensa_free_softc(struct ugensa_softc *sc) 288239180Shselasky{ 289239180Shselasky if (ucom_unref(&sc->sc_super_ucom)) { 290239299Shselasky mtx_destroy(&sc->sc_mtx); 291239299Shselasky device_free_softc(sc); 292239180Shselasky } 293239180Shselasky} 294239180Shselasky 295239180Shselaskystatic void 296239180Shselaskyugensa_free(struct ucom_softc *ucom) 297239180Shselasky{ 298239299Shselasky ugensa_free_softc(ucom->sc_parent); 299239180Shselasky} 300239180Shselasky 301239180Shselaskystatic void 302194677Sthompsaugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 303184610Salfred{ 304194677Sthompsa struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer); 305194677Sthompsa struct usb_page_cache *pc; 306184610Salfred uint32_t actlen; 307184610Salfred 308184610Salfred switch (USB_GET_STATE(xfer)) { 309184610Salfred case USB_ST_SETUP: 310184610Salfred case USB_ST_TRANSFERRED: 311188413Sthompsatr_setup: 312194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 313194677Sthompsa if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0, 314184610Salfred UGENSA_BUF_SIZE, &actlen)) { 315194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, actlen); 316194228Sthompsa usbd_transfer_submit(xfer); 317184610Salfred } 318184610Salfred return; 319184610Salfred 320184610Salfred default: /* Error */ 321194677Sthompsa if (error != USB_ERR_CANCELLED) { 322188413Sthompsa /* try to clear stall first */ 323194677Sthompsa usbd_xfer_set_stall(xfer); 324188413Sthompsa goto tr_setup; 325184610Salfred } 326184610Salfred return; 327184610Salfred } 328184610Salfred} 329184610Salfred 330184610Salfredstatic void 331194677Sthompsaugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 332184610Salfred{ 333194677Sthompsa struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer); 334194677Sthompsa struct usb_page_cache *pc; 335194677Sthompsa int actlen; 336184610Salfred 337194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 338194677Sthompsa 339184610Salfred switch (USB_GET_STATE(xfer)) { 340184610Salfred case USB_ST_TRANSFERRED: 341194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 342194677Sthompsa ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen); 343184610Salfred 344184610Salfred case USB_ST_SETUP: 345188413Sthompsatr_setup: 346194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 347194228Sthompsa usbd_transfer_submit(xfer); 348184610Salfred return; 349184610Salfred 350184610Salfred default: /* Error */ 351194677Sthompsa if (error != USB_ERR_CANCELLED) { 352188413Sthompsa /* try to clear stall first */ 353194677Sthompsa usbd_xfer_set_stall(xfer); 354188413Sthompsa goto tr_setup; 355184610Salfred } 356184610Salfred return; 357184610Salfred } 358184610Salfred} 359184610Salfred 360184610Salfredstatic void 361192984Sthompsaugensa_start_read(struct ucom_softc *ucom) 362184610Salfred{ 363184610Salfred struct ugensa_softc *sc = ucom->sc_parent; 364184610Salfred struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 365184610Salfred 366194228Sthompsa usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]); 367184610Salfred} 368184610Salfred 369184610Salfredstatic void 370192984Sthompsaugensa_stop_read(struct ucom_softc *ucom) 371184610Salfred{ 372184610Salfred struct ugensa_softc *sc = ucom->sc_parent; 373184610Salfred struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 374184610Salfred 375194228Sthompsa usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]); 376184610Salfred} 377184610Salfred 378184610Salfredstatic void 379192984Sthompsaugensa_start_write(struct ucom_softc *ucom) 380184610Salfred{ 381184610Salfred struct ugensa_softc *sc = ucom->sc_parent; 382184610Salfred struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 383184610Salfred 384194228Sthompsa usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]); 385184610Salfred} 386184610Salfred 387184610Salfredstatic void 388192984Sthompsaugensa_stop_write(struct ucom_softc *ucom) 389184610Salfred{ 390184610Salfred struct ugensa_softc *sc = ucom->sc_parent; 391184610Salfred struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 392184610Salfred 393194228Sthompsa usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]); 394184610Salfred} 395197570Sthompsa 396197570Sthompsastatic void 397197570Sthompsaugensa_poll(struct ucom_softc *ucom) 398197570Sthompsa{ 399197570Sthompsa struct ugensa_softc *sc = ucom->sc_parent; 400197570Sthompsa struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno; 401197570Sthompsa 402197570Sthompsa usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER); 403197570Sthompsa} 404