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