1 /*- 2 * Copyright (c) 2000 Iwasa Kazmi 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions, and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * This code is based on ugen.c and ulpt.c developed by Lennart Augustsson. 27 * This code includes software developed by the NetBSD Foundation, Inc. and 28 * its contributors. 29 * 30 * $FreeBSD: head/sys/dev/usb/storage/urio.c 246128 2013-01-30 18:01:20Z sbz $ 31 */ 32 33 /* 34 * 2000/3/24 added NetBSD/OpenBSD support (from Alex Nemirovsky) 35 * 2000/3/07 use two bulk-pipe handles for read and write (Dirk) 36 * 2000/3/06 change major number(143), and copyright header 37 * some fix for 4.0 (Dirk) 38 * 2000/3/05 codes for FreeBSD 4.x - CURRENT (Thanks to Dirk-Willem van Gulik) 39 * 2000/3/01 remove retry code from urioioctl() 40 * change method of bulk transfer (no interrupt) 41 * 2000/2/28 small fixes for new rio_usb.h 42 * 2000/2/24 first version. 43 */ 44 45 #include <sys/stdint.h> 46 #include <sys/param.h> 47 #include <sys/queue.h> 48 #include <sys/types.h> 49 #include <sys/systm.h> 50 #include <sys/kernel.h> 51 #include <sys/bus.h> 52 #include <sys/module.h> 53 #include <sys/lock.h> 54 #include <sys/condvar.h> 55 #include <sys/sysctl.h> 56 #include <sys/unistd.h> 57 #include <sys/callout.h> 58 #include <sys/malloc.h> 59 #include <sys/priv.h> 60 #include <sys/conf.h> 61 #include <sys/fcntl.h> 62 63 #include <bus/u4b/usb.h> 64 #include <bus/u4b/usbdi.h> 65 #include "usbdevs.h" 66 67 #include <bus/u4b/usb_ioctl.h> 68 #include <bus/u4b/usb_generic.h> 69 70 #define USB_DEBUG_VAR urio_debug 71 #include <bus/u4b/usb_debug.h> 72 73 #include <bus/u4b/storage/rio500_usb.h> 74 75 #ifdef USB_DEBUG 76 static int urio_debug = 0; 77 78 static SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio"); 79 SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RW, 80 &urio_debug, 0, "urio debug level"); 81 #endif 82 83 #define URIO_T_WR 0 84 #define URIO_T_RD 1 85 #define URIO_T_WR_CS 2 86 #define URIO_T_RD_CS 3 87 #define URIO_T_MAX 4 88 89 #define URIO_BSIZE (1<<12) /* bytes */ 90 #define URIO_IFQ_MAXLEN 2 /* units */ 91 92 struct urio_softc { 93 struct usb_fifo_sc sc_fifo; 94 struct lock sc_lock; 95 96 struct usb_device *sc_udev; 97 struct usb_xfer *sc_xfer[URIO_T_MAX]; 98 99 uint8_t sc_flags; 100 #define URIO_FLAG_READ_STALL 0x01 /* read transfer stalled */ 101 #define URIO_FLAG_WRITE_STALL 0x02 /* write transfer stalled */ 102 103 uint8_t sc_name[16]; 104 }; 105 106 /* prototypes */ 107 108 static device_probe_t urio_probe; 109 static device_attach_t urio_attach; 110 static device_detach_t urio_detach; 111 112 static usb_callback_t urio_write_callback; 113 static usb_callback_t urio_write_clear_stall_callback; 114 static usb_callback_t urio_read_callback; 115 static usb_callback_t urio_read_clear_stall_callback; 116 117 static usb_fifo_close_t urio_close; 118 static usb_fifo_cmd_t urio_start_read; 119 static usb_fifo_cmd_t urio_start_write; 120 static usb_fifo_cmd_t urio_stop_read; 121 static usb_fifo_cmd_t urio_stop_write; 122 static usb_fifo_ioctl_t urio_ioctl; 123 static usb_fifo_open_t urio_open; 124 125 static struct usb_fifo_methods urio_fifo_methods = { 126 .f_close = &urio_close, 127 .f_ioctl = &urio_ioctl, 128 .f_open = &urio_open, 129 .f_start_read = &urio_start_read, 130 .f_start_write = &urio_start_write, 131 .f_stop_read = &urio_stop_read, 132 .f_stop_write = &urio_stop_write, 133 .basename[0] = "urio", 134 }; 135 136 static const struct usb_config urio_config[URIO_T_MAX] = { 137 [URIO_T_WR] = { 138 .type = UE_BULK, 139 .endpoint = UE_ADDR_ANY, 140 .direction = UE_DIR_OUT, 141 .bufsize = URIO_BSIZE, 142 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.proxy_buffer = 1,}, 143 .callback = &urio_write_callback, 144 }, 145 146 [URIO_T_RD] = { 147 .type = UE_BULK, 148 .endpoint = UE_ADDR_ANY, 149 .direction = UE_DIR_IN, 150 .bufsize = URIO_BSIZE, 151 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1,}, 152 .callback = &urio_read_callback, 153 }, 154 155 [URIO_T_WR_CS] = { 156 .type = UE_CONTROL, 157 .endpoint = 0x00, /* Control pipe */ 158 .direction = UE_DIR_ANY, 159 .bufsize = sizeof(struct usb_device_request), 160 .callback = &urio_write_clear_stall_callback, 161 .timeout = 1000, /* 1 second */ 162 .interval = 50, /* 50ms */ 163 }, 164 165 [URIO_T_RD_CS] = { 166 .type = UE_CONTROL, 167 .endpoint = 0x00, /* Control pipe */ 168 .direction = UE_DIR_ANY, 169 .bufsize = sizeof(struct usb_device_request), 170 .callback = &urio_read_clear_stall_callback, 171 .timeout = 1000, /* 1 second */ 172 .interval = 50, /* 50ms */ 173 }, 174 }; 175 176 static devclass_t urio_devclass; 177 178 static device_method_t urio_methods[] = { 179 /* Device interface */ 180 DEVMETHOD(device_probe, urio_probe), 181 DEVMETHOD(device_attach, urio_attach), 182 DEVMETHOD(device_detach, urio_detach), 183 184 DEVMETHOD_END 185 }; 186 187 static driver_t urio_driver = { 188 .name = "urio", 189 .methods = urio_methods, 190 .size = sizeof(struct urio_softc), 191 }; 192 193 static const STRUCT_USB_HOST_ID urio_devs[] = { 194 {USB_VPI(USB_VENDOR_DIAMOND, USB_PRODUCT_DIAMOND_RIO500USB, 0)}, 195 {USB_VPI(USB_VENDOR_DIAMOND2, USB_PRODUCT_DIAMOND2_RIO600USB, 0)}, 196 {USB_VPI(USB_VENDOR_DIAMOND2, USB_PRODUCT_DIAMOND2_RIO800USB, 0)}, 197 }; 198 199 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, NULL, NULL); 200 MODULE_DEPEND(urio, usb, 1, 1, 1); 201 MODULE_VERSION(urio, 1); 202 203 static int 204 urio_probe(device_t dev) 205 { 206 struct usb_attach_arg *uaa = device_get_ivars(dev); 207 208 if (uaa->usb_mode != USB_MODE_HOST) 209 return (ENXIO); 210 if (uaa->info.bConfigIndex != 0) 211 return (ENXIO); 212 if (uaa->info.bIfaceIndex != 0) 213 return (ENXIO); 214 215 return (usbd_lookup_id_by_uaa(urio_devs, sizeof(urio_devs), uaa)); 216 } 217 218 static int 219 urio_attach(device_t dev) 220 { 221 struct usb_attach_arg *uaa = device_get_ivars(dev); 222 struct urio_softc *sc = device_get_softc(dev); 223 int error; 224 225 device_set_usb_desc(dev); 226 227 sc->sc_udev = uaa->device; 228 229 lockinit(&sc->sc_lock, "urio lock", 0, LK_CANRECURSE); 230 231 ksnprintf(sc->sc_name, sizeof(sc->sc_name), 232 "%s", device_get_nameunit(dev)); 233 234 error = usbd_transfer_setup(uaa->device, 235 &uaa->info.bIfaceIndex, sc->sc_xfer, 236 urio_config, URIO_T_MAX, sc, &sc->sc_lock); 237 238 if (error) { 239 DPRINTF("error=%s\n", usbd_errstr(error)); 240 goto detach; 241 } 242 243 error = usb_fifo_attach(uaa->device, sc, &sc->sc_lock, 244 &urio_fifo_methods, &sc->sc_fifo, 245 device_get_unit(dev), -1, uaa->info.bIfaceIndex, 246 UID_ROOT, GID_OPERATOR, 0644); 247 if (error) { 248 goto detach; 249 } 250 return (0); /* success */ 251 252 detach: 253 urio_detach(dev); 254 return (ENOMEM); /* failure */ 255 } 256 257 static void 258 urio_write_callback(struct usb_xfer *xfer, usb_error_t error) 259 { 260 struct urio_softc *sc = usbd_xfer_softc(xfer); 261 struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_TX]; 262 struct usb_page_cache *pc; 263 uint32_t actlen; 264 265 switch (USB_GET_STATE(xfer)) { 266 case USB_ST_TRANSFERRED: 267 case USB_ST_SETUP: 268 if (sc->sc_flags & URIO_FLAG_WRITE_STALL) { 269 usbd_transfer_start(sc->sc_xfer[URIO_T_WR_CS]); 270 return; 271 } 272 pc = usbd_xfer_get_frame(xfer, 0); 273 if (usb_fifo_get_data(f, pc, 0, 274 usbd_xfer_max_len(xfer), &actlen, 0)) { 275 276 usbd_xfer_set_frame_len(xfer, 0, actlen); 277 usbd_transfer_submit(xfer); 278 } 279 return; 280 281 default: /* Error */ 282 if (error != USB_ERR_CANCELLED) { 283 /* try to clear stall first */ 284 sc->sc_flags |= URIO_FLAG_WRITE_STALL; 285 usbd_transfer_start(sc->sc_xfer[URIO_T_WR_CS]); 286 } 287 return; 288 } 289 } 290 291 static void 292 urio_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 293 { 294 struct urio_softc *sc = usbd_xfer_softc(xfer); 295 struct usb_xfer *xfer_other = sc->sc_xfer[URIO_T_WR]; 296 297 if (usbd_clear_stall_callback(xfer, xfer_other)) { 298 DPRINTF("stall cleared\n"); 299 sc->sc_flags &= ~URIO_FLAG_WRITE_STALL; 300 usbd_transfer_start(xfer_other); 301 } 302 } 303 304 static void 305 urio_read_callback(struct usb_xfer *xfer, usb_error_t error) 306 { 307 struct urio_softc *sc = usbd_xfer_softc(xfer); 308 struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_RX]; 309 struct usb_page_cache *pc; 310 int actlen; 311 312 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 313 314 switch (USB_GET_STATE(xfer)) { 315 case USB_ST_TRANSFERRED: 316 pc = usbd_xfer_get_frame(xfer, 0); 317 usb_fifo_put_data(f, pc, 0, actlen, 1); 318 319 case USB_ST_SETUP: 320 if (sc->sc_flags & URIO_FLAG_READ_STALL) { 321 usbd_transfer_start(sc->sc_xfer[URIO_T_RD_CS]); 322 return; 323 } 324 if (usb_fifo_put_bytes_max(f) != 0) { 325 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 326 usbd_transfer_submit(xfer); 327 } 328 return; 329 330 default: /* Error */ 331 if (error != USB_ERR_CANCELLED) { 332 /* try to clear stall first */ 333 sc->sc_flags |= URIO_FLAG_READ_STALL; 334 usbd_transfer_start(sc->sc_xfer[URIO_T_RD_CS]); 335 } 336 return; 337 } 338 } 339 340 static void 341 urio_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 342 { 343 struct urio_softc *sc = usbd_xfer_softc(xfer); 344 struct usb_xfer *xfer_other = sc->sc_xfer[URIO_T_RD]; 345 346 if (usbd_clear_stall_callback(xfer, xfer_other)) { 347 DPRINTF("stall cleared\n"); 348 sc->sc_flags &= ~URIO_FLAG_READ_STALL; 349 usbd_transfer_start(xfer_other); 350 } 351 } 352 353 static void 354 urio_start_read(struct usb_fifo *fifo) 355 { 356 struct urio_softc *sc = usb_fifo_softc(fifo); 357 358 usbd_transfer_start(sc->sc_xfer[URIO_T_RD]); 359 } 360 361 static void 362 urio_stop_read(struct usb_fifo *fifo) 363 { 364 struct urio_softc *sc = usb_fifo_softc(fifo); 365 366 usbd_transfer_stop(sc->sc_xfer[URIO_T_RD_CS]); 367 usbd_transfer_stop(sc->sc_xfer[URIO_T_RD]); 368 } 369 370 static void 371 urio_start_write(struct usb_fifo *fifo) 372 { 373 struct urio_softc *sc = usb_fifo_softc(fifo); 374 375 usbd_transfer_start(sc->sc_xfer[URIO_T_WR]); 376 } 377 378 static void 379 urio_stop_write(struct usb_fifo *fifo) 380 { 381 struct urio_softc *sc = usb_fifo_softc(fifo); 382 383 usbd_transfer_stop(sc->sc_xfer[URIO_T_WR_CS]); 384 usbd_transfer_stop(sc->sc_xfer[URIO_T_WR]); 385 } 386 387 static int 388 urio_open(struct usb_fifo *fifo, int fflags) 389 { 390 struct urio_softc *sc = usb_fifo_softc(fifo); 391 392 if (fflags & FREAD) { 393 /* clear stall first */ 394 lockmgr(&sc->sc_lock, LK_EXCLUSIVE); 395 sc->sc_flags |= URIO_FLAG_READ_STALL; 396 lockmgr(&sc->sc_lock, LK_RELEASE); 397 398 if (usb_fifo_alloc_buffer(fifo, 399 usbd_xfer_max_len(sc->sc_xfer[URIO_T_RD]), 400 URIO_IFQ_MAXLEN)) { 401 return (ENOMEM); 402 } 403 } 404 if (fflags & FWRITE) { 405 /* clear stall first */ 406 sc->sc_flags |= URIO_FLAG_WRITE_STALL; 407 408 if (usb_fifo_alloc_buffer(fifo, 409 usbd_xfer_max_len(sc->sc_xfer[URIO_T_WR]), 410 URIO_IFQ_MAXLEN)) { 411 return (ENOMEM); 412 } 413 } 414 return (0); /* success */ 415 } 416 417 static void 418 urio_close(struct usb_fifo *fifo, int fflags) 419 { 420 if (fflags & (FREAD | FWRITE)) { 421 usb_fifo_free_buffer(fifo); 422 } 423 } 424 425 static int 426 urio_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, 427 int fflags) 428 { 429 struct usb_ctl_request ur; 430 struct RioCommand *rio_cmd; 431 int error; 432 433 switch (cmd) { 434 case RIO_RECV_COMMAND: 435 if (!(fflags & FWRITE)) { 436 error = EPERM; 437 goto done; 438 } 439 memset(&ur, 0, sizeof(ur)); 440 rio_cmd = addr; 441 ur.ucr_request.bmRequestType = 442 rio_cmd->requesttype | UT_READ_VENDOR_DEVICE; 443 break; 444 445 case RIO_SEND_COMMAND: 446 if (!(fflags & FWRITE)) { 447 error = EPERM; 448 goto done; 449 } 450 memset(&ur, 0, sizeof(ur)); 451 rio_cmd = addr; 452 ur.ucr_request.bmRequestType = 453 rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE; 454 break; 455 456 default: 457 error = EINVAL; 458 goto done; 459 } 460 461 DPRINTFN(2, "Sending command\n"); 462 463 /* Send rio control message */ 464 ur.ucr_request.bRequest = rio_cmd->request; 465 USETW(ur.ucr_request.wValue, rio_cmd->value); 466 USETW(ur.ucr_request.wIndex, rio_cmd->index); 467 USETW(ur.ucr_request.wLength, rio_cmd->length); 468 ur.ucr_data = rio_cmd->buffer; 469 470 /* reuse generic USB code */ 471 error = ugen_do_request(fifo, &ur); 472 473 done: 474 return (error); 475 } 476 477 static int 478 urio_detach(device_t dev) 479 { 480 struct urio_softc *sc = device_get_softc(dev); 481 482 DPRINTF("\n"); 483 484 usb_fifo_detach(&sc->sc_fifo); 485 486 usbd_transfer_unsetup(sc->sc_xfer, URIO_T_MAX); 487 488 lockuninit(&sc->sc_lock); 489 490 return (0); 491 } 492