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