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.flags = {}, 153 .mh.callback = &urio_write_clear_stall_callback, 154 .mh.timeout = 1000, /* 1 second */ 155 .mh.interval = 50, /* 50ms */ 156 }, 157 158 [URIO_T_RD_CS] = { 159 .type = UE_CONTROL, 160 .endpoint = 0x00, /* Control pipe */ 161 .direction = UE_DIR_ANY, 162 .mh.bufsize = sizeof(struct usb2_device_request), 163 .mh.flags = {}, 164 .mh.callback = &urio_read_clear_stall_callback, 165 .mh.timeout = 1000, /* 1 second */ 166 .mh.interval = 50, /* 50ms */ 167 }, 168 }; 169 170 static devclass_t urio_devclass; 171 172 static device_method_t urio_methods[] = { 173 /* Device interface */ 174 DEVMETHOD(device_probe, urio_probe), 175 DEVMETHOD(device_attach, urio_attach), 176 DEVMETHOD(device_detach, urio_detach), 177 {0, 0} 178 }; 179 180 static driver_t urio_driver = { 181 .name = "urio", 182 .methods = urio_methods, 183 .size = sizeof(struct urio_softc), 184 }; 185 186 DRIVER_MODULE(urio, ushub, urio_driver, urio_devclass, NULL, 0); 187 MODULE_DEPEND(urio, usb, 1, 1, 1); 188 189 static int 190 urio_probe(device_t dev) 191 { 192 struct usb2_attach_arg *uaa = device_get_ivars(dev); 193 194 if (uaa->usb2_mode != USB_MODE_HOST) { 195 return (ENXIO); 196 } 197 if ((((uaa->info.idVendor == USB_VENDOR_DIAMOND) && 198 (uaa->info.idProduct == USB_PRODUCT_DIAMOND_RIO500USB)) || 199 ((uaa->info.idVendor == USB_VENDOR_DIAMOND2) && 200 ((uaa->info.idProduct == USB_PRODUCT_DIAMOND2_RIO600USB) || 201 (uaa->info.idProduct == USB_PRODUCT_DIAMOND2_RIO800USB))))) 202 return (0); 203 else 204 return (ENXIO); 205 } 206 207 static int 208 urio_attach(device_t dev) 209 { 210 struct usb2_attach_arg *uaa = device_get_ivars(dev); 211 struct urio_softc *sc = device_get_softc(dev); 212 int error; 213 214 device_set_usb2_desc(dev); 215 216 sc->sc_udev = uaa->device; 217 218 mtx_init(&sc->sc_mtx, "urio lock", NULL, MTX_DEF | MTX_RECURSE); 219 220 snprintf(sc->sc_name, sizeof(sc->sc_name), 221 "%s", device_get_nameunit(dev)); 222 223 error = usb2_transfer_setup(uaa->device, 224 &uaa->info.bIfaceIndex, sc->sc_xfer, 225 urio_config, URIO_T_MAX, sc, &sc->sc_mtx); 226 227 if (error) { 228 DPRINTF("error=%s\n", usb2_errstr(error)); 229 goto detach; 230 } 231 /* set interface permissions */ 232 usb2_set_iface_perm(uaa->device, uaa->info.bIfaceIndex, 233 UID_ROOT, GID_OPERATOR, 0644); 234 235 error = usb2_fifo_attach(uaa->device, sc, &sc->sc_mtx, 236 &urio_fifo_methods, &sc->sc_fifo, 237 device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex); 238 if (error) { 239 goto detach; 240 } 241 return (0); /* success */ 242 243 detach: 244 urio_detach(dev); 245 return (ENOMEM); /* failure */ 246 } 247 248 static void 249 urio_write_callback(struct usb2_xfer *xfer) 250 { 251 struct urio_softc *sc = xfer->priv_sc; 252 struct usb2_fifo *f = sc->sc_fifo.fp[USB_FIFO_TX]; 253 uint32_t actlen; 254 255 switch (USB_GET_STATE(xfer)) { 256 case USB_ST_TRANSFERRED: 257 case USB_ST_SETUP: 258 if (sc->sc_flags & URIO_FLAG_WRITE_STALL) { 259 usb2_transfer_start(sc->sc_xfer[URIO_T_WR_CS]); 260 return; 261 } 262 if (usb2_fifo_get_data(f, xfer->frbuffers, 0, 263 xfer->max_data_length, &actlen, 0)) { 264 265 xfer->frlengths[0] = actlen; 266 usb2_start_hardware(xfer); 267 } 268 return; 269 270 default: /* Error */ 271 if (xfer->error != USB_ERR_CANCELLED) { 272 /* try to clear stall first */ 273 sc->sc_flags |= URIO_FLAG_WRITE_STALL; 274 usb2_transfer_start(sc->sc_xfer[URIO_T_WR_CS]); 275 } 276 return; 277 } 278 } 279 280 static void 281 urio_write_clear_stall_callback(struct usb2_xfer *xfer) 282 { 283 struct urio_softc *sc = xfer->priv_sc; 284 struct usb2_xfer *xfer_other = sc->sc_xfer[URIO_T_WR]; 285 286 if (usb2_clear_stall_callback(xfer, xfer_other)) { 287 DPRINTF("stall cleared\n"); 288 sc->sc_flags &= ~URIO_FLAG_WRITE_STALL; 289 usb2_transfer_start(xfer_other); 290 } 291 } 292 293 static void 294 urio_read_callback(struct usb2_xfer *xfer) 295 { 296 struct urio_softc *sc = xfer->priv_sc; 297 struct usb2_fifo *f = sc->sc_fifo.fp[USB_FIFO_RX]; 298 299 switch (USB_GET_STATE(xfer)) { 300 case USB_ST_TRANSFERRED: 301 usb2_fifo_put_data(f, xfer->frbuffers, 0, 302 xfer->actlen, 1); 303 304 case USB_ST_SETUP: 305 if (sc->sc_flags & URIO_FLAG_READ_STALL) { 306 usb2_transfer_start(sc->sc_xfer[URIO_T_RD_CS]); 307 return; 308 } 309 if (usb2_fifo_put_bytes_max(f) != 0) { 310 xfer->frlengths[0] = xfer->max_data_length; 311 usb2_start_hardware(xfer); 312 } 313 return; 314 315 default: /* Error */ 316 if (xfer->error != USB_ERR_CANCELLED) { 317 /* try to clear stall first */ 318 sc->sc_flags |= URIO_FLAG_READ_STALL; 319 usb2_transfer_start(sc->sc_xfer[URIO_T_RD_CS]); 320 } 321 return; 322 } 323 } 324 325 static void 326 urio_read_clear_stall_callback(struct usb2_xfer *xfer) 327 { 328 struct urio_softc *sc = xfer->priv_sc; 329 struct usb2_xfer *xfer_other = sc->sc_xfer[URIO_T_RD]; 330 331 if (usb2_clear_stall_callback(xfer, xfer_other)) { 332 DPRINTF("stall cleared\n"); 333 sc->sc_flags &= ~URIO_FLAG_READ_STALL; 334 usb2_transfer_start(xfer_other); 335 } 336 } 337 338 static void 339 urio_start_read(struct usb2_fifo *fifo) 340 { 341 struct urio_softc *sc = fifo->priv_sc0; 342 343 usb2_transfer_start(sc->sc_xfer[URIO_T_RD]); 344 } 345 346 static void 347 urio_stop_read(struct usb2_fifo *fifo) 348 { 349 struct urio_softc *sc = fifo->priv_sc0; 350 351 usb2_transfer_stop(sc->sc_xfer[URIO_T_RD_CS]); 352 usb2_transfer_stop(sc->sc_xfer[URIO_T_RD]); 353 } 354 355 static void 356 urio_start_write(struct usb2_fifo *fifo) 357 { 358 struct urio_softc *sc = fifo->priv_sc0; 359 360 usb2_transfer_start(sc->sc_xfer[URIO_T_WR]); 361 } 362 363 static void 364 urio_stop_write(struct usb2_fifo *fifo) 365 { 366 struct urio_softc *sc = fifo->priv_sc0; 367 368 usb2_transfer_stop(sc->sc_xfer[URIO_T_WR_CS]); 369 usb2_transfer_stop(sc->sc_xfer[URIO_T_WR]); 370 } 371 372 static int 373 urio_open(struct usb2_fifo *fifo, int fflags, struct thread *td) 374 { 375 struct urio_softc *sc = fifo->priv_sc0; 376 377 if ((fflags & (FWRITE | FREAD)) != (FWRITE | FREAD)) { 378 return (EACCES); 379 } 380 if (fflags & FREAD) { 381 /* clear stall first */ 382 mtx_lock(&sc->sc_mtx); 383 sc->sc_flags |= URIO_FLAG_READ_STALL; 384 mtx_unlock(&sc->sc_mtx); 385 386 if (usb2_fifo_alloc_buffer(fifo, 387 sc->sc_xfer[URIO_T_RD]->max_data_length, 388 URIO_IFQ_MAXLEN)) { 389 return (ENOMEM); 390 } 391 } 392 if (fflags & FWRITE) { 393 /* clear stall first */ 394 sc->sc_flags |= URIO_FLAG_WRITE_STALL; 395 396 if (usb2_fifo_alloc_buffer(fifo, 397 sc->sc_xfer[URIO_T_WR]->max_data_length, 398 URIO_IFQ_MAXLEN)) { 399 return (ENOMEM); 400 } 401 } 402 return (0); /* success */ 403 } 404 405 static void 406 urio_close(struct usb2_fifo *fifo, int fflags, struct thread *td) 407 { 408 if (fflags & (FREAD | FWRITE)) { 409 usb2_fifo_free_buffer(fifo); 410 } 411 } 412 413 static int 414 urio_ioctl(struct usb2_fifo *fifo, u_long cmd, void *addr, 415 int fflags, struct thread *td) 416 { 417 struct usb2_ctl_request ur; 418 struct RioCommand *rio_cmd; 419 int error; 420 421 switch (cmd) { 422 case RIO_RECV_COMMAND: 423 if (!(fflags & FWRITE)) { 424 error = EPERM; 425 goto done; 426 } 427 bzero(&ur, sizeof(ur)); 428 rio_cmd = addr; 429 ur.ucr_request.bmRequestType = 430 rio_cmd->requesttype | UT_READ_VENDOR_DEVICE; 431 break; 432 433 case RIO_SEND_COMMAND: 434 if (!(fflags & FWRITE)) { 435 error = EPERM; 436 goto done; 437 } 438 bzero(&ur, sizeof(ur)); 439 rio_cmd = addr; 440 ur.ucr_request.bmRequestType = 441 rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE; 442 break; 443 444 default: 445 error = EINVAL; 446 goto done; 447 } 448 449 DPRINTFN(2, "Sending command\n"); 450 451 /* Send rio control message */ 452 ur.ucr_request.bRequest = rio_cmd->request; 453 USETW(ur.ucr_request.wValue, rio_cmd->value); 454 USETW(ur.ucr_request.wIndex, rio_cmd->index); 455 USETW(ur.ucr_request.wLength, rio_cmd->length); 456 ur.ucr_data = rio_cmd->buffer; 457 458 /* reuse generic USB code */ 459 error = ugen_do_request(fifo, &ur); 460 461 done: 462 return (error); 463 } 464 465 static int 466 urio_detach(device_t dev) 467 { 468 struct urio_softc *sc = device_get_softc(dev); 469 470 DPRINTF("\n"); 471 472 usb2_fifo_detach(&sc->sc_fifo); 473 474 usb2_transfer_unsetup(sc->sc_xfer, URIO_T_MAX); 475 476 mtx_destroy(&sc->sc_mtx); 477 478 return (0); 479 } 480