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 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 35 /* 36 * 2000/3/24 added NetBSD/OpenBSD support (from Alex Nemirovsky) 37 * 2000/3/07 use two bulk-pipe handles for read and write (Dirk) 38 * 2000/3/06 change major number(143), and copyright header 39 * some fix for 4.0 (Dirk) 40 * 2000/3/05 codes for FreeBSD 4.x - CURRENT (Thanks to Dirk-Willem van Gulik) 41 * 2000/3/01 remove retry code from urioioctl() 42 * change method of bulk transfer (no interrupt) 43 * 2000/2/28 small fixes for new rio_usb.h 44 * 2000/2/24 first version. 45 */ 46 47 #include "usbdevs.h" 48 #include <dev/usb/usb.h> 49 #include <dev/usb/usb_mfunc.h> 50 #include <dev/usb/usb_error.h> 51 #include <dev/usb/usb_ioctl.h> 52 #include <dev/usb/storage/rio500_usb.h> 53 54 #define USB_DEBUG_VAR urio_debug 55 56 #include <dev/usb/usb_core.h> 57 #include <dev/usb/usb_debug.h> 58 #include <dev/usb/usb_process.h> 59 #include <dev/usb/usb_request.h> 60 #include <dev/usb/usb_lookup.h> 61 #include <dev/usb/usb_util.h> 62 #include <dev/usb/usb_busdma.h> 63 #include <dev/usb/usb_mbuf.h> 64 #include <dev/usb/usb_dev.h> 65 #include <dev/usb/usb_generic.h> 66 67 #if USB_DEBUG 68 static int urio_debug = 0; 69 70 SYSCTL_NODE(_hw_usb2, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio"); 71 SYSCTL_INT(_hw_usb2_urio, OID_AUTO, debug, CTLFLAG_RW, 72 &urio_debug, 0, "urio debug level"); 73 #endif 74 75 #define URIO_T_WR 0 76 #define URIO_T_RD 1 77 #define URIO_T_WR_CS 2 78 #define URIO_T_RD_CS 3 79 #define URIO_T_MAX 4 80 81 #define URIO_BSIZE (1<<12) /* bytes */ 82 #define URIO_IFQ_MAXLEN 2 /* units */ 83 84 struct urio_softc { 85 struct usb2_fifo_sc sc_fifo; 86 struct mtx sc_mtx; 87 88 struct usb2_device *sc_udev; 89 struct usb2_xfer *sc_xfer[URIO_T_MAX]; 90 91 uint8_t sc_flags; 92 #define URIO_FLAG_READ_STALL 0x01 /* read transfer stalled */ 93 #define URIO_FLAG_WRITE_STALL 0x02 /* write transfer stalled */ 94 95 uint8_t sc_name[16]; 96 }; 97 98 /* prototypes */ 99 100 static device_probe_t urio_probe; 101 static device_attach_t urio_attach; 102 static device_detach_t urio_detach; 103 104 static usb2_callback_t urio_write_callback; 105 static usb2_callback_t urio_write_clear_stall_callback; 106 static usb2_callback_t urio_read_callback; 107 static usb2_callback_t urio_read_clear_stall_callback; 108 109 static usb2_fifo_close_t urio_close; 110 static usb2_fifo_cmd_t urio_start_read; 111 static usb2_fifo_cmd_t urio_start_write; 112 static usb2_fifo_cmd_t urio_stop_read; 113 static usb2_fifo_cmd_t urio_stop_write; 114 static usb2_fifo_ioctl_t urio_ioctl; 115 static usb2_fifo_open_t urio_open; 116 117 static struct usb2_fifo_methods urio_fifo_methods = { 118 .f_close = &urio_close, 119 .f_ioctl = &urio_ioctl, 120 .f_open = &urio_open, 121 .f_start_read = &urio_start_read, 122 .f_start_write = &urio_start_write, 123 .f_stop_read = &urio_stop_read, 124 .f_stop_write = &urio_stop_write, 125 .basename[0] = "urio", 126 }; 127 128 static const struct usb2_config urio_config[URIO_T_MAX] = { 129 [URIO_T_WR] = { 130 .type = UE_BULK, 131 .endpoint = UE_ADDR_ANY, 132 .direction = UE_DIR_OUT, 133 .mh.bufsize = URIO_BSIZE, 134 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,.proxy_buffer = 1,}, 135 .mh.callback = &urio_write_callback, 136 }, 137 138 [URIO_T_RD] = { 139 .type = UE_BULK, 140 .endpoint = UE_ADDR_ANY, 141 .direction = UE_DIR_IN, 142 .mh.bufsize = URIO_BSIZE, 143 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1,}, 144 .mh.callback = &urio_read_callback, 145 }, 146 147 [URIO_T_WR_CS] = { 148 .type = UE_CONTROL, 149 .endpoint = 0x00, /* Control pipe */ 150 .direction = UE_DIR_ANY, 151 .mh.bufsize = sizeof(struct usb2_device_request), 152 .mh.callback = &urio_write_clear_stall_callback, 153 .mh.timeout = 1000, /* 1 second */ 154 .mh.interval = 50, /* 50ms */ 155 }, 156 157 [URIO_T_RD_CS] = { 158 .type = UE_CONTROL, 159 .endpoint = 0x00, /* Control pipe */ 160 .direction = UE_DIR_ANY, 161 .mh.bufsize = sizeof(struct usb2_device_request), 162 .mh.callback = &urio_read_clear_stall_callback, 163 .mh.timeout = 1000, /* 1 second */ 164 .mh.interval = 50, /* 50ms */ 165 }, 166 }; 167 168 static devclass_t urio_devclass; 169 170 static device_method_t urio_methods[] = { 171 /* Device interface */ 172 DEVMETHOD(device_probe, urio_probe), 173 DEVMETHOD(device_attach, urio_attach), 174 DEVMETHOD(device_detach, urio_detach), 175 {0, 0} 176 }; 177 178 static driver_t urio_driver = { 179 .name = "urio", 180 .methods = urio_methods, 181 .size = sizeof(struct urio_softc), 182 }; 183 184 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, NULL, 0); 185 MODULE_DEPEND(urio, usb, 1, 1, 1); 186 187 static int 188 urio_probe(device_t dev) 189 { 190 struct usb2_attach_arg *uaa = device_get_ivars(dev); 191 192 if (uaa->usb2_mode != USB_MODE_HOST) { 193 return (ENXIO); 194 } 195 if ((((uaa->info.idVendor == USB_VENDOR_DIAMOND) && 196 (uaa->info.idProduct == USB_PRODUCT_DIAMOND_RIO500USB)) || 197 ((uaa->info.idVendor == USB_VENDOR_DIAMOND2) && 198 ((uaa->info.idProduct == USB_PRODUCT_DIAMOND2_RIO600USB) || 199 (uaa->info.idProduct == USB_PRODUCT_DIAMOND2_RIO800USB))))) 200 return (0); 201 else 202 return (ENXIO); 203 } 204 205 static int 206 urio_attach(device_t dev) 207 { 208 struct usb2_attach_arg *uaa = device_get_ivars(dev); 209 struct urio_softc *sc = device_get_softc(dev); 210 int error; 211 212 device_set_usb2_desc(dev); 213 214 sc->sc_udev = uaa->device; 215 216 mtx_init(&sc->sc_mtx, "urio lock", NULL, MTX_DEF | MTX_RECURSE); 217 218 snprintf(sc->sc_name, sizeof(sc->sc_name), 219 "%s", device_get_nameunit(dev)); 220 221 error = usb2_transfer_setup(uaa->device, 222 &uaa->info.bIfaceIndex, sc->sc_xfer, 223 urio_config, URIO_T_MAX, sc, &sc->sc_mtx); 224 225 if (error) { 226 DPRINTF("error=%s\n", usb2_errstr(error)); 227 goto detach; 228 } 229 230 error = usb2_fifo_attach(uaa->device, sc, &sc->sc_mtx, 231 &urio_fifo_methods, &sc->sc_fifo, 232 device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex, 233 UID_ROOT, GID_OPERATOR, 0644); 234 if (error) { 235 goto detach; 236 } 237 return (0); /* success */ 238 239 detach: 240 urio_detach(dev); 241 return (ENOMEM); /* failure */ 242 } 243 244 static void 245 urio_write_callback(struct usb2_xfer *xfer) 246 { 247 struct urio_softc *sc = xfer->priv_sc; 248 struct usb2_fifo *f = sc->sc_fifo.fp[USB_FIFO_TX]; 249 uint32_t actlen; 250 251 switch (USB_GET_STATE(xfer)) { 252 case USB_ST_TRANSFERRED: 253 case USB_ST_SETUP: 254 if (sc->sc_flags & URIO_FLAG_WRITE_STALL) { 255 usb2_transfer_start(sc->sc_xfer[URIO_T_WR_CS]); 256 return; 257 } 258 if (usb2_fifo_get_data(f, xfer->frbuffers, 0, 259 xfer->max_data_length, &actlen, 0)) { 260 261 xfer->frlengths[0] = actlen; 262 usb2_start_hardware(xfer); 263 } 264 return; 265 266 default: /* Error */ 267 if (xfer->error != USB_ERR_CANCELLED) { 268 /* try to clear stall first */ 269 sc->sc_flags |= URIO_FLAG_WRITE_STALL; 270 usb2_transfer_start(sc->sc_xfer[URIO_T_WR_CS]); 271 } 272 return; 273 } 274 } 275 276 static void 277 urio_write_clear_stall_callback(struct usb2_xfer *xfer) 278 { 279 struct urio_softc *sc = xfer->priv_sc; 280 struct usb2_xfer *xfer_other = sc->sc_xfer[URIO_T_WR]; 281 282 if (usb2_clear_stall_callback(xfer, xfer_other)) { 283 DPRINTF("stall cleared\n"); 284 sc->sc_flags &= ~URIO_FLAG_WRITE_STALL; 285 usb2_transfer_start(xfer_other); 286 } 287 } 288 289 static void 290 urio_read_callback(struct usb2_xfer *xfer) 291 { 292 struct urio_softc *sc = xfer->priv_sc; 293 struct usb2_fifo *f = sc->sc_fifo.fp[USB_FIFO_RX]; 294 295 switch (USB_GET_STATE(xfer)) { 296 case USB_ST_TRANSFERRED: 297 usb2_fifo_put_data(f, xfer->frbuffers, 0, 298 xfer->actlen, 1); 299 300 case USB_ST_SETUP: 301 if (sc->sc_flags & URIO_FLAG_READ_STALL) { 302 usb2_transfer_start(sc->sc_xfer[URIO_T_RD_CS]); 303 return; 304 } 305 if (usb2_fifo_put_bytes_max(f) != 0) { 306 xfer->frlengths[0] = xfer->max_data_length; 307 usb2_start_hardware(xfer); 308 } 309 return; 310 311 default: /* Error */ 312 if (xfer->error != USB_ERR_CANCELLED) { 313 /* try to clear stall first */ 314 sc->sc_flags |= URIO_FLAG_READ_STALL; 315 usb2_transfer_start(sc->sc_xfer[URIO_T_RD_CS]); 316 } 317 return; 318 } 319 } 320 321 static void 322 urio_read_clear_stall_callback(struct usb2_xfer *xfer) 323 { 324 struct urio_softc *sc = xfer->priv_sc; 325 struct usb2_xfer *xfer_other = sc->sc_xfer[URIO_T_RD]; 326 327 if (usb2_clear_stall_callback(xfer, xfer_other)) { 328 DPRINTF("stall cleared\n"); 329 sc->sc_flags &= ~URIO_FLAG_READ_STALL; 330 usb2_transfer_start(xfer_other); 331 } 332 } 333 334 static void 335 urio_start_read(struct usb2_fifo *fifo) 336 { 337 struct urio_softc *sc = fifo->priv_sc0; 338 339 usb2_transfer_start(sc->sc_xfer[URIO_T_RD]); 340 } 341 342 static void 343 urio_stop_read(struct usb2_fifo *fifo) 344 { 345 struct urio_softc *sc = fifo->priv_sc0; 346 347 usb2_transfer_stop(sc->sc_xfer[URIO_T_RD_CS]); 348 usb2_transfer_stop(sc->sc_xfer[URIO_T_RD]); 349 } 350 351 static void 352 urio_start_write(struct usb2_fifo *fifo) 353 { 354 struct urio_softc *sc = fifo->priv_sc0; 355 356 usb2_transfer_start(sc->sc_xfer[URIO_T_WR]); 357 } 358 359 static void 360 urio_stop_write(struct usb2_fifo *fifo) 361 { 362 struct urio_softc *sc = fifo->priv_sc0; 363 364 usb2_transfer_stop(sc->sc_xfer[URIO_T_WR_CS]); 365 usb2_transfer_stop(sc->sc_xfer[URIO_T_WR]); 366 } 367 368 static int 369 urio_open(struct usb2_fifo *fifo, int fflags) 370 { 371 struct urio_softc *sc = fifo->priv_sc0; 372 373 if ((fflags & (FWRITE | FREAD)) != (FWRITE | FREAD)) { 374 return (EACCES); 375 } 376 if (fflags & FREAD) { 377 /* clear stall first */ 378 mtx_lock(&sc->sc_mtx); 379 sc->sc_flags |= URIO_FLAG_READ_STALL; 380 mtx_unlock(&sc->sc_mtx); 381 382 if (usb2_fifo_alloc_buffer(fifo, 383 sc->sc_xfer[URIO_T_RD]->max_data_length, 384 URIO_IFQ_MAXLEN)) { 385 return (ENOMEM); 386 } 387 } 388 if (fflags & FWRITE) { 389 /* clear stall first */ 390 sc->sc_flags |= URIO_FLAG_WRITE_STALL; 391 392 if (usb2_fifo_alloc_buffer(fifo, 393 sc->sc_xfer[URIO_T_WR]->max_data_length, 394 URIO_IFQ_MAXLEN)) { 395 return (ENOMEM); 396 } 397 } 398 return (0); /* success */ 399 } 400 401 static void 402 urio_close(struct usb2_fifo *fifo, int fflags) 403 { 404 if (fflags & (FREAD | FWRITE)) { 405 usb2_fifo_free_buffer(fifo); 406 } 407 } 408 409 static int 410 urio_ioctl(struct usb2_fifo *fifo, u_long cmd, void *addr, 411 int fflags) 412 { 413 struct usb2_ctl_request ur; 414 struct RioCommand *rio_cmd; 415 int error; 416 417 switch (cmd) { 418 case RIO_RECV_COMMAND: 419 if (!(fflags & FWRITE)) { 420 error = EPERM; 421 goto done; 422 } 423 bzero(&ur, sizeof(ur)); 424 rio_cmd = addr; 425 ur.ucr_request.bmRequestType = 426 rio_cmd->requesttype | UT_READ_VENDOR_DEVICE; 427 break; 428 429 case RIO_SEND_COMMAND: 430 if (!(fflags & FWRITE)) { 431 error = EPERM; 432 goto done; 433 } 434 bzero(&ur, sizeof(ur)); 435 rio_cmd = addr; 436 ur.ucr_request.bmRequestType = 437 rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE; 438 break; 439 440 default: 441 error = EINVAL; 442 goto done; 443 } 444 445 DPRINTFN(2, "Sending command\n"); 446 447 /* Send rio control message */ 448 ur.ucr_request.bRequest = rio_cmd->request; 449 USETW(ur.ucr_request.wValue, rio_cmd->value); 450 USETW(ur.ucr_request.wIndex, rio_cmd->index); 451 USETW(ur.ucr_request.wLength, rio_cmd->length); 452 ur.ucr_data = rio_cmd->buffer; 453 454 /* reuse generic USB code */ 455 error = ugen_do_request(fifo, &ur); 456 457 done: 458 return (error); 459 } 460 461 static int 462 urio_detach(device_t dev) 463 { 464 struct urio_softc *sc = device_get_softc(dev); 465 466 DPRINTF("\n"); 467 468 usb2_fifo_detach(&sc->sc_fifo); 469 470 usb2_transfer_unsetup(sc->sc_xfer, URIO_T_MAX); 471 472 mtx_destroy(&sc->sc_mtx); 473 474 return (0); 475 } 476