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