urio.c revision 187970
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: head/sys/dev/usb2/storage/urio2.c 187970 2009-02-01 00:51:25Z thompsa $"); 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 <dev/usb2/include/usb2_devid.h> 48#include <dev/usb2/include/usb2_standard.h> 49#include <dev/usb2/include/usb2_mfunc.h> 50#include <dev/usb2/include/usb2_error.h> 51#include <dev/usb2/include/usb2_ioctl.h> 52#include <dev/usb/rio500_usb.h> 53 54#define USB_DEBUG_VAR urio_debug 55 56#include <dev/usb2/core/usb2_core.h> 57#include <dev/usb2/core/usb2_debug.h> 58#include <dev/usb2/core/usb2_process.h> 59#include <dev/usb2/core/usb2_request.h> 60#include <dev/usb2/core/usb2_lookup.h> 61#include <dev/usb2/core/usb2_util.h> 62#include <dev/usb2/core/usb2_busdma.h> 63#include <dev/usb2/core/usb2_mbuf.h> 64#include <dev/usb2/core/usb2_dev.h> 65#include <dev/usb2/core/usb2_generic.h> 66 67#if USB_DEBUG 68static int urio_debug = 0; 69 70SYSCTL_NODE(_hw_usb2, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio"); 71SYSCTL_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 84struct 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 100static device_probe_t urio_probe; 101static device_attach_t urio_attach; 102static device_detach_t urio_detach; 103 104static usb2_callback_t urio_write_callback; 105static usb2_callback_t urio_write_clear_stall_callback; 106static usb2_callback_t urio_read_callback; 107static usb2_callback_t urio_read_clear_stall_callback; 108 109static usb2_fifo_close_t urio_close; 110static usb2_fifo_cmd_t urio_start_read; 111static usb2_fifo_cmd_t urio_start_write; 112static usb2_fifo_cmd_t urio_stop_read; 113static usb2_fifo_cmd_t urio_stop_write; 114static usb2_fifo_ioctl_t urio_ioctl; 115static usb2_fifo_open_t urio_open; 116 117static 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 128static 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 170static devclass_t urio_devclass; 171 172static 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 180static driver_t urio_driver = { 181 .name = "urio", 182 .methods = urio_methods, 183 .size = sizeof(struct urio_softc), 184}; 185 186DRIVER_MODULE(urio, ushub, urio_driver, urio_devclass, NULL, 0); 187MODULE_DEPEND(urio, usb2_storage, 1, 1, 1); 188MODULE_DEPEND(urio, usb2_core, 1, 1, 1); 189 190static int 191urio_probe(device_t dev) 192{ 193 struct usb2_attach_arg *uaa = device_get_ivars(dev); 194 195 if (uaa->usb2_mode != USB_MODE_HOST) { 196 return (ENXIO); 197 } 198 if ((((uaa->info.idVendor == USB_VENDOR_DIAMOND) && 199 (uaa->info.idProduct == USB_PRODUCT_DIAMOND_RIO500USB)) || 200 ((uaa->info.idVendor == USB_VENDOR_DIAMOND2) && 201 ((uaa->info.idProduct == USB_PRODUCT_DIAMOND2_RIO600USB) || 202 (uaa->info.idProduct == USB_PRODUCT_DIAMOND2_RIO800USB))))) 203 return (0); 204 else 205 return (ENXIO); 206} 207 208static int 209urio_attach(device_t dev) 210{ 211 struct usb2_attach_arg *uaa = device_get_ivars(dev); 212 struct urio_softc *sc = device_get_softc(dev); 213 int error; 214 215 device_set_usb2_desc(dev); 216 217 sc->sc_udev = uaa->device; 218 219 mtx_init(&sc->sc_mtx, "urio lock", NULL, MTX_DEF | MTX_RECURSE); 220 221 snprintf(sc->sc_name, sizeof(sc->sc_name), 222 "%s", device_get_nameunit(dev)); 223 224 error = usb2_transfer_setup(uaa->device, 225 &uaa->info.bIfaceIndex, sc->sc_xfer, 226 urio_config, URIO_T_MAX, sc, &sc->sc_mtx); 227 228 if (error) { 229 DPRINTF("error=%s\n", usb2_errstr(error)); 230 goto detach; 231 } 232 /* set interface permissions */ 233 usb2_set_iface_perm(uaa->device, uaa->info.bIfaceIndex, 234 UID_ROOT, GID_OPERATOR, 0644); 235 236 error = usb2_fifo_attach(uaa->device, sc, &sc->sc_mtx, 237 &urio_fifo_methods, &sc->sc_fifo, 238 device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex); 239 if (error) { 240 goto detach; 241 } 242 return (0); /* success */ 243 244detach: 245 urio_detach(dev); 246 return (ENOMEM); /* failure */ 247} 248 249static void 250urio_write_callback(struct usb2_xfer *xfer) 251{ 252 struct urio_softc *sc = xfer->priv_sc; 253 struct usb2_fifo *f = sc->sc_fifo.fp[USB_FIFO_TX]; 254 uint32_t actlen; 255 256 switch (USB_GET_STATE(xfer)) { 257 case USB_ST_TRANSFERRED: 258 case USB_ST_SETUP: 259 if (sc->sc_flags & URIO_FLAG_WRITE_STALL) { 260 usb2_transfer_start(sc->sc_xfer[URIO_T_WR_CS]); 261 return; 262 } 263 if (usb2_fifo_get_data(f, xfer->frbuffers, 0, 264 xfer->max_data_length, &actlen, 0)) { 265 266 xfer->frlengths[0] = actlen; 267 usb2_start_hardware(xfer); 268 } 269 return; 270 271 default: /* Error */ 272 if (xfer->error != USB_ERR_CANCELLED) { 273 /* try to clear stall first */ 274 sc->sc_flags |= URIO_FLAG_WRITE_STALL; 275 usb2_transfer_start(sc->sc_xfer[URIO_T_WR_CS]); 276 } 277 return; 278 } 279} 280 281static void 282urio_write_clear_stall_callback(struct usb2_xfer *xfer) 283{ 284 struct urio_softc *sc = xfer->priv_sc; 285 struct usb2_xfer *xfer_other = sc->sc_xfer[URIO_T_WR]; 286 287 if (usb2_clear_stall_callback(xfer, xfer_other)) { 288 DPRINTF("stall cleared\n"); 289 sc->sc_flags &= ~URIO_FLAG_WRITE_STALL; 290 usb2_transfer_start(xfer_other); 291 } 292} 293 294static void 295urio_read_callback(struct usb2_xfer *xfer) 296{ 297 struct urio_softc *sc = xfer->priv_sc; 298 struct usb2_fifo *f = sc->sc_fifo.fp[USB_FIFO_RX]; 299 300 switch (USB_GET_STATE(xfer)) { 301 case USB_ST_TRANSFERRED: 302 usb2_fifo_put_data(f, xfer->frbuffers, 0, 303 xfer->actlen, 1); 304 305 case USB_ST_SETUP: 306 if (sc->sc_flags & URIO_FLAG_READ_STALL) { 307 usb2_transfer_start(sc->sc_xfer[URIO_T_RD_CS]); 308 return; 309 } 310 if (usb2_fifo_put_bytes_max(f) != 0) { 311 xfer->frlengths[0] = xfer->max_data_length; 312 usb2_start_hardware(xfer); 313 } 314 return; 315 316 default: /* Error */ 317 if (xfer->error != USB_ERR_CANCELLED) { 318 /* try to clear stall first */ 319 sc->sc_flags |= URIO_FLAG_READ_STALL; 320 usb2_transfer_start(sc->sc_xfer[URIO_T_RD_CS]); 321 } 322 return; 323 } 324} 325 326static void 327urio_read_clear_stall_callback(struct usb2_xfer *xfer) 328{ 329 struct urio_softc *sc = xfer->priv_sc; 330 struct usb2_xfer *xfer_other = sc->sc_xfer[URIO_T_RD]; 331 332 if (usb2_clear_stall_callback(xfer, xfer_other)) { 333 DPRINTF("stall cleared\n"); 334 sc->sc_flags &= ~URIO_FLAG_READ_STALL; 335 usb2_transfer_start(xfer_other); 336 } 337} 338 339static void 340urio_start_read(struct usb2_fifo *fifo) 341{ 342 struct urio_softc *sc = fifo->priv_sc0; 343 344 usb2_transfer_start(sc->sc_xfer[URIO_T_RD]); 345} 346 347static void 348urio_stop_read(struct usb2_fifo *fifo) 349{ 350 struct urio_softc *sc = fifo->priv_sc0; 351 352 usb2_transfer_stop(sc->sc_xfer[URIO_T_RD_CS]); 353 usb2_transfer_stop(sc->sc_xfer[URIO_T_RD]); 354} 355 356static void 357urio_start_write(struct usb2_fifo *fifo) 358{ 359 struct urio_softc *sc = fifo->priv_sc0; 360 361 usb2_transfer_start(sc->sc_xfer[URIO_T_WR]); 362} 363 364static void 365urio_stop_write(struct usb2_fifo *fifo) 366{ 367 struct urio_softc *sc = fifo->priv_sc0; 368 369 usb2_transfer_stop(sc->sc_xfer[URIO_T_WR_CS]); 370 usb2_transfer_stop(sc->sc_xfer[URIO_T_WR]); 371} 372 373static int 374urio_open(struct usb2_fifo *fifo, int fflags, struct thread *td) 375{ 376 struct urio_softc *sc = fifo->priv_sc0; 377 378 if ((fflags & (FWRITE | FREAD)) != (FWRITE | FREAD)) { 379 return (EACCES); 380 } 381 if (fflags & FREAD) { 382 /* clear stall first */ 383 mtx_lock(&sc->sc_mtx); 384 sc->sc_flags |= URIO_FLAG_READ_STALL; 385 mtx_unlock(&sc->sc_mtx); 386 387 if (usb2_fifo_alloc_buffer(fifo, 388 sc->sc_xfer[URIO_T_RD]->max_data_length, 389 URIO_IFQ_MAXLEN)) { 390 return (ENOMEM); 391 } 392 } 393 if (fflags & FWRITE) { 394 /* clear stall first */ 395 sc->sc_flags |= URIO_FLAG_WRITE_STALL; 396 397 if (usb2_fifo_alloc_buffer(fifo, 398 sc->sc_xfer[URIO_T_WR]->max_data_length, 399 URIO_IFQ_MAXLEN)) { 400 return (ENOMEM); 401 } 402 } 403 return (0); /* success */ 404} 405 406static void 407urio_close(struct usb2_fifo *fifo, int fflags, struct thread *td) 408{ 409 if (fflags & (FREAD | FWRITE)) { 410 usb2_fifo_free_buffer(fifo); 411 } 412} 413 414static int 415urio_ioctl(struct usb2_fifo *fifo, u_long cmd, void *addr, 416 int fflags, struct thread *td) 417{ 418 struct usb2_ctl_request ur; 419 struct RioCommand *rio_cmd; 420 int error; 421 422 switch (cmd) { 423 case RIO_RECV_COMMAND: 424 if (!(fflags & FWRITE)) { 425 error = EPERM; 426 goto done; 427 } 428 bzero(&ur, sizeof(ur)); 429 rio_cmd = addr; 430 ur.ucr_request.bmRequestType = 431 rio_cmd->requesttype | UT_READ_VENDOR_DEVICE; 432 break; 433 434 case RIO_SEND_COMMAND: 435 if (!(fflags & FWRITE)) { 436 error = EPERM; 437 goto done; 438 } 439 bzero(&ur, sizeof(ur)); 440 rio_cmd = addr; 441 ur.ucr_request.bmRequestType = 442 rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE; 443 break; 444 445 default: 446 error = EINVAL; 447 goto done; 448 } 449 450 DPRINTFN(2, "Sending command\n"); 451 452 /* Send rio control message */ 453 ur.ucr_request.bRequest = rio_cmd->request; 454 USETW(ur.ucr_request.wValue, rio_cmd->value); 455 USETW(ur.ucr_request.wIndex, rio_cmd->index); 456 USETW(ur.ucr_request.wLength, rio_cmd->length); 457 ur.ucr_data = rio_cmd->buffer; 458 459 /* reuse generic USB code */ 460 error = ugen_do_request(fifo, &ur); 461 462done: 463 return (error); 464} 465 466static int 467urio_detach(device_t dev) 468{ 469 struct urio_softc *sc = device_get_softc(dev); 470 471 DPRINTF("\n"); 472 473 usb2_fifo_detach(&sc->sc_fifo); 474 475 usb2_transfer_unsetup(sc->sc_xfer, URIO_T_MAX); 476 477 mtx_destroy(&sc->sc_mtx); 478 479 return (0); 480} 481