File:  [DragonFly] / src / sys / dev / usbmisc / urio / urio.c
Revision 1.10: download - view: text, annotated - select for diffs
Wed May 19 22:52:52 2004 UTC (9 years, 11 months ago) by dillon
Branches: MAIN
CVS tags: HEAD, DragonFly_Stable, DragonFly_Snap29Sep2004, DragonFly_Snap13Sep2004, DragonFly_RELEASE_1_2_Slip, DragonFly_RELEASE_1_2, DragonFly_1_0_REL, DragonFly_1_0_RC1, DragonFly_1_0A_REL
Device layer rollup commit.

* cdevsw_add() is now required.  cdevsw_add() and cdevsw_remove() may specify
  a mask/match indicating the range of supported minor numbers.  Multiple
  cdevsw_add()'s using the same major number, but distinctly different
  ranges, may be issued.  All devices that failed to call cdevsw_add() before
  now do.

* cdevsw_remove() now automatically marks all devices within its supported
  range as being destroyed.

* vnode->v_rdev is no longer resolved when the vnode is created.  Instead,
  only v_udev (a newly added field) is resolved.  v_rdev is resolved when
  the vnode is opened and cleared on the last close.

* A great deal of code was making rather dubious assumptions with regards
  to the validity of devices associated with vnodes, primarily due to
  the persistence of a device structure due to being indexed by (major, minor)
  instead of by (cdevsw, major, minor).  In particular, if you run a program
  which connects to a USB device and then you pull the USB device and plug
  it back in, the vnode subsystem will continue to believe that the device
  is open when, in fact, it isn't (because it was destroyed and recreated).

  In particular, note that all the VFS mount procedures now check devices
  via v_udev instead of v_rdev prior to calling VOP_OPEN(), since v_rdev
  is NULL prior to the first open.

* The disk layer's device interaction has been rewritten.  The disk layer
  (i.e. the slice and disklabel management layer) no longer overloads
  its data onto the device structure representing the underlying physical
  disk.  Instead, the disk layer uses the new cdevsw_add() functionality
  to register its own cdevsw using the underlying device's major number,
  and simply does NOT register the underlying device's cdevsw.  No
  confusion is created because the device hash is now based on
  (cdevsw,major,minor) rather then (major,minor).

  NOTE: This also means that underlying raw disk devices may use the entire
  device minor number instead of having to reserve the bits used by the disk
  layer, and also means that can we (theoretically) stack a fully
  disklabel-supported 'disk' on top of any block device.

* The new reference counting scheme prevents this by associating a device
  with a cdevsw and disconnecting the device from its cdevsw when the cdevsw
  is removed.  Additionally, all udev2dev() lookups run through the cdevsw
  mask/match and only successfully find devices still associated with an
  active cdevsw.

* Major work on MFS:  MFS no longer shortcuts vnode and device creation.  It
  now creates a real vnode and a real device and implements real open and
  close VOPs.  Additionally, due to the disk layer changes, MFS is no longer
  limited to 255 mounts.  The new limit is 16 million.  Since MFS creates a
  real device node, mount_mfs will now create a real /dev/mfs<PID> device
  that can be read from userland (e.g. so you can dump an MFS filesystem).

* BUF AND DEVICE STRATEGY changes.  The struct buf contains a b_dev field.
  In order to properly handle stacked devices we now require that the b_dev
  field be initialized before the device strategy routine is called.  This
  required some additional work in various VFS implementations.  To enforce
  this requirement, biodone() now sets b_dev to NODEV.  The new disk layer
  will adjust b_dev before forwarding a request to the actual physical
  device.

* A bug in the ISO CD boot sequence which resulted in a panic has been fixed.

Testing by: lots of people, but David Rhodus found the most aggregious bugs.

    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: /*
   32:  * $FreeBSD: src/sys/dev/usb/urio.c,v 1.28 2003/08/25 22:01:06 joe Exp $
   33:  * $DragonFly: src/sys/dev/usbmisc/urio/urio.c,v 1.10 2004/05/19 22:52:52 dillon Exp $
   34:  */
   35: 
   36: /*
   37:  * 2000/3/24  added NetBSD/OpenBSD support (from Alex Nemirovsky)
   38:  * 2000/3/07  use two bulk-pipe handles for read and write (Dirk)
   39:  * 2000/3/06  change major number(143), and copyright header
   40:  *            some fix for 4.0 (Dirk)
   41:  * 2000/3/05  codes for FreeBSD 4.x - CURRENT (Thanks to Dirk-Willem van Gulik)
   42:  * 2000/3/01  remove retry code from urioioctl()
   43:  *            change method of bulk transfer (no interrupt)
   44:  * 2000/2/28  small fixes for new rio_usb.h
   45:  * 2000/2/24  first version.
   46:  */
   47: 
   48: #include <sys/param.h>
   49: #include <sys/systm.h>
   50: #include <sys/kernel.h>
   51: #include <sys/malloc.h>
   52: #if defined(__NetBSD__)
   53: #include <sys/device.h>
   54: #include <sys/ioctl.h>
   55: #elif defined(__FreeBSD__) || defined(__DragonFly__)
   56: #include <sys/module.h>
   57: #include <sys/bus.h>
   58: #include <sys/ioccom.h>
   59: #endif
   60: #include <sys/fcntl.h>
   61: #include <sys/filio.h>
   62: #include <sys/conf.h>
   63: #include <sys/uio.h>
   64: #include <sys/tty.h>
   65: #include <sys/file.h>
   66: #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
   67: #include <sys/selinfo.h>
   68: #else
   69: #include <sys/select.h>
   70: #endif
   71: #include <sys/vnode.h>
   72: #include <sys/poll.h>
   73: #include <sys/sysctl.h>
   74: #include <sys/proc.h>
   75: 
   76: #include <bus/usb/usb.h>
   77: #include <bus/usb/usbdi.h>
   78: #include <bus/usb/usbdi_util.h>
   79: 
   80: #include <bus/usb/usbdevs.h>
   81: #include <bus/usb/rio500_usb.h>
   82: 
   83: #ifdef USB_DEBUG
   84: #define DPRINTF(x)	if (uriodebug) logprintf x
   85: #define DPRINTFN(n,x)	if (uriodebug>(n)) logprintf x
   86: int	uriodebug = 0;
   87: SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
   88: SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RW,
   89: 	   &uriodebug, 0, "urio debug level");
   90: #else
   91: #define DPRINTF(x)
   92: #define DPRINTFN(n,x)
   93: #endif
   94: 
   95: /* difference of usbd interface */
   96: #define USBDI 1
   97: 
   98: #define RIO_OUT 0
   99: #define RIO_IN  1
  100: #define RIO_NODIR  2
  101: 
  102: #if defined(__NetBSD__)
  103: int urioopen(dev_t, int, int, struct proc *);
  104: int urioclose(dev_t, int, int, struct proc *p);
  105: int urioread(dev_t, struct uio *uio, int);
  106: int uriowrite(dev_t, struct uio *uio, int);
  107: int urioioctl(dev_t, u_long, caddr_t, int, struct proc *);
  108: 
  109: cdev_decl(urio);
  110: #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
  111: 			  ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
  112: 							   RIO_NODIR))
  113: #elif defined(__FreeBSD__) || defined(__DragonFly__)
  114: d_open_t  urioopen;
  115: d_close_t urioclose;
  116: d_read_t  urioread;
  117: d_write_t uriowrite;
  118: d_ioctl_t urioioctl;
  119: 
  120: #define URIO_CDEV_MAJOR	143
  121: 
  122: Static struct cdevsw urio_cdevsw = {
  123:  	/* name */	"urio",		
  124: 	/* cmaj */	URIO_CDEV_MAJOR,
  125:  	/* flags */	0,
  126: 	/* port */	NULL,
  127: 	/* clone */	NULL,
  128: 	urioopen,	urioclose,	urioread,	uriowrite,
  129:  	urioioctl,	nopoll,		nommap,		nostrategy,
  130: 	nodump,		nopsize
  131: };
  132: #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
  133: 		 	  ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
  134: 			    				   RIO_NODIR))
  135: #endif  /*defined(__FreeBSD__)*/
  136: 
  137: #define	URIO_BBSIZE	1024
  138: 
  139: struct urio_softc {
  140:  	USBBASEDEVICE sc_dev;
  141: 	usbd_device_handle sc_udev;
  142: 	usbd_interface_handle sc_iface;
  143: 
  144: 	int sc_opened;
  145: 	usbd_pipe_handle sc_pipeh_in;
  146: 	usbd_pipe_handle sc_pipeh_out;
  147: 	int sc_epaddr[2];
  148: 
  149: 	int sc_refcnt;
  150: #if defined(__NetBSD__) || defined(__OpenBSD__)
  151: 	u_char sc_dying;
  152: #endif
  153: };
  154: 
  155: #define URIOUNIT(n) (minor(n))
  156: 
  157: #define RIO_RW_TIMEOUT 4000	/* ms */
  158: 
  159: USB_DECLARE_DRIVER(urio);
  160: 
  161: USB_MATCH(urio)
  162: {
  163: 	USB_MATCH_START(urio, uaa);
  164: 	usb_device_descriptor_t *dd;
  165: 
  166: 	DPRINTFN(10,("urio_match\n"));
  167: 	if (!uaa->iface)
  168: 		return UMATCH_NONE;
  169: 
  170: 	dd = usbd_get_device_descriptor(uaa->device);
  171: 
  172: 	if (dd &&
  173: 	    ((UGETW(dd->idVendor) == USB_VENDOR_DIAMOND &&
  174: 	    UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND_RIO500USB) ||
  175: 	    (UGETW(dd->idVendor) == USB_VENDOR_DIAMOND2 &&
  176: 	      (UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO600USB ||
  177: 	      UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO800USB))))
  178: 		return UMATCH_VENDOR_PRODUCT;
  179: 	else
  180: 		return UMATCH_NONE;
  181: }
  182: 
  183: USB_ATTACH(urio)
  184: {
  185: 	USB_ATTACH_START(urio, sc, uaa);
  186: 	char devinfo[1024];
  187: 	usbd_device_handle udev;
  188: 	usbd_interface_handle iface;
  189: 	u_int8_t epcount;
  190: #if defined(__NetBSD__) || defined(__OpenBSD__)
  191: 	u_int8_t niface;
  192: #endif
  193: 	usbd_status r;
  194: 	char * ermsg = "<none>";
  195: 	int i;
  196: 
  197: 	DPRINTFN(10,("urio_attach: sc=%p\n", sc));
  198: 	usbd_devinfo(uaa->device, 0, devinfo);
  199: 	USB_ATTACH_SETUP;
  200: 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
  201: 
  202: 	sc->sc_udev = udev = uaa->device;
  203: 
  204: #if defined(__FreeBSD__) || defined(__DragonFly__)
  205:  	if ((!uaa->device) || (!uaa->iface)) {
  206: 		ermsg = "device or iface";
  207:  		goto nobulk;
  208: 	}
  209: 	sc->sc_iface = iface = uaa->iface;
  210: #elif defined(__NetBSD__) || defined(__OpenBSD__)
  211:  	if (!udev) {
  212: 		ermsg = "device";
  213:  		goto nobulk;
  214: 	}
  215: 	r = usbd_interface_count(udev, &niface);
  216: 	if (r) {
  217: 		ermsg = "iface";
  218: 		goto nobulk;
  219: 	}
  220: 	r = usbd_device2interface_handle(udev, 0, &iface);
  221: 	if (r) {
  222: 		ermsg = "iface";
  223: 		goto nobulk;
  224: 	}
  225: 	sc->sc_iface = iface;
  226: #endif
  227: 	sc->sc_opened = 0;
  228: 	sc->sc_pipeh_in = 0;
  229: 	sc->sc_pipeh_out = 0;
  230: 	sc->sc_refcnt = 0;
  231: 
  232: 	r = usbd_endpoint_count(iface, &epcount);
  233: 	if (r != USBD_NORMAL_COMPLETION) {
  234: 		ermsg = "endpoints";
  235: 		goto nobulk;
  236: 	}
  237: 
  238: 	sc->sc_epaddr[RIO_OUT] = 0xff;
  239: 	sc->sc_epaddr[RIO_IN] = 0x00;
  240: 
  241: 	for (i = 0; i < epcount; i++) {
  242: 		usb_endpoint_descriptor_t *edesc =
  243: 			usbd_interface2endpoint_descriptor(iface, i);
  244: 		int d;
  245: 
  246: 		if (!edesc) {
  247: 			ermsg = "interface endpoint";
  248: 			goto nobulk;
  249: 		}
  250: 
  251: 		d = RIO_UE_GET_DIR(edesc->bEndpointAddress);
  252: 		if (d != RIO_NODIR)
  253: 			sc->sc_epaddr[d] = edesc->bEndpointAddress;
  254: 	}
  255: 	if ( sc->sc_epaddr[RIO_OUT] == 0xff ||
  256: 	     sc->sc_epaddr[RIO_IN] == 0x00) {
  257: 		ermsg = "Rio I&O";
  258: 		goto nobulk;
  259: 	}
  260: 
  261: #if defined(__FreeBSD__) || defined(__DragonFly__)
  262: 	cdevsw_add(&urio_cdevsw, -1, device_get_unit(self));
  263: 	make_dev(&urio_cdevsw, device_get_unit(self),
  264: 			UID_ROOT, GID_OPERATOR,
  265: 			0644, "urio%d", device_get_unit(self));
  266: #elif defined(__NetBSD__) || defined(__OpenBSD__)
  267: 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
  268: 			   USBDEV(sc->sc_dev));
  269: #endif
  270: 
  271: 	DPRINTFN(10, ("urio_attach: %p\n", sc->sc_udev));
  272: 
  273: 	USB_ATTACH_SUCCESS_RETURN;
  274: 
  275:  nobulk:
  276: 	printf("%s: could not find %s\n", USBDEVNAME(sc->sc_dev),ermsg);
  277: 	USB_ATTACH_ERROR_RETURN;
  278: }
  279: 
  280: 
  281: int
  282: urioopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
  283: {
  284: #if (USBDI >= 1)
  285: 	struct urio_softc * sc;
  286: #endif
  287: 	int unit = URIOUNIT(dev);
  288: 	USB_GET_SC_OPEN(urio, unit, sc);
  289: 
  290: 	DPRINTFN(5, ("urioopen: flag=%d, mode=%d, unit=%d\n",
  291: 		     flag, mode, unit));
  292: 
  293: 	if (sc->sc_opened)
  294: 		return EBUSY;
  295: 
  296: 	if ((flag & (FWRITE|FREAD)) != (FWRITE|FREAD))
  297: 		return EACCES;
  298: 
  299: 	sc->sc_opened = 1;
  300: 	sc->sc_pipeh_in = 0;
  301: 	sc->sc_pipeh_out = 0;
  302: 	if (usbd_open_pipe(sc->sc_iface,
  303: 		sc->sc_epaddr[RIO_IN], 0, &sc->sc_pipeh_in)
  304: 	   		!= USBD_NORMAL_COMPLETION)
  305: 	{
  306: 			sc->sc_pipeh_in = 0;
  307: 			return EIO;
  308: 	};
  309: 	if (usbd_open_pipe(sc->sc_iface,
  310: 		sc->sc_epaddr[RIO_OUT], 0, &sc->sc_pipeh_out)
  311: 	   		!= USBD_NORMAL_COMPLETION)
  312: 	{
  313: 			usbd_close_pipe(sc->sc_pipeh_in);
  314: 			sc->sc_pipeh_in = 0;
  315: 			sc->sc_pipeh_out = 0;
  316: 			return EIO;
  317: 	};
  318: 	return 0;
  319: }
  320: 
  321: int
  322: urioclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
  323: {
  324: #if (USBDI >= 1)
  325: 	struct urio_softc * sc;
  326: #endif
  327: 	int unit = URIOUNIT(dev);
  328: 	USB_GET_SC(urio, unit, sc);
  329: 
  330: 	DPRINTFN(5, ("urioclose: flag=%d, mode=%d, unit=%d\n", flag, mode, unit));
  331: 	if (sc->sc_pipeh_in)
  332: 		usbd_close_pipe(sc->sc_pipeh_in);
  333: 
  334: 	if (sc->sc_pipeh_out)
  335: 		usbd_close_pipe(sc->sc_pipeh_out);
  336: 
  337: 	sc->sc_pipeh_in = 0;
  338: 	sc->sc_pipeh_out = 0;
  339: 	sc->sc_opened = 0;
  340: 	sc->sc_refcnt = 0;
  341: 	return 0;
  342: }
  343: 
  344: int
  345: urioread(dev_t dev, struct uio *uio, int flag)
  346: {
  347: #if (USBDI >= 1)
  348: 	struct urio_softc * sc;
  349: 	usbd_xfer_handle reqh;
  350: #else
  351: 	usbd_request_handle reqh;
  352: 	usbd_private_handle r_priv;
  353:         void *r_buff;
  354:         usbd_status r_status;
  355: #endif
  356: 	int unit = URIOUNIT(dev);
  357: 	usbd_status r;
  358: 	char buf[URIO_BBSIZE];
  359: 	u_int32_t n, tn;
  360: 	int error = 0;
  361: 
  362: 	USB_GET_SC(urio, unit, sc);
  363: 
  364: 	DPRINTFN(5, ("urioread: %d\n", unit));
  365: 	if (!sc->sc_opened)
  366: 		return EIO;
  367: 
  368: #if (USBDI >= 1)
  369: 	sc->sc_refcnt++;
  370: 	reqh = usbd_alloc_xfer(sc->sc_udev);
  371: #else
  372: 	reqh = usbd_alloc_request();
  373: #endif
  374: 	if (reqh == 0)
  375: 		return ENOMEM;
  376: 	while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
  377: 		DPRINTFN(1, ("urioread: start transfer %d bytes\n", n));
  378: 		tn = n;
  379: #if (USBDI >= 1)
  380:  		usbd_setup_xfer(reqh, sc->sc_pipeh_in, 0, buf, tn,
  381: 				       0, RIO_RW_TIMEOUT, 0);
  382: #else
  383: 		r = usbd_setup_request(reqh, sc->sc_pipeh_in, 0, buf, tn,
  384: 				       0, RIO_RW_TIMEOUT, 0);
  385: 		if (r != USBD_NORMAL_COMPLETION) {
  386: 			error = EIO;
  387: 			break;
  388: 		}
  389: #endif
  390: 		r = usbd_sync_transfer(reqh);
  391: 		if (r != USBD_NORMAL_COMPLETION) {
  392: 			DPRINTFN(1, ("urioread: error=%d\n", r));
  393: 			usbd_clear_endpoint_stall(sc->sc_pipeh_in);
  394: 			tn = 0;
  395: 			error = EIO;
  396: 			break;
  397: 		}
  398: #if (USBDI >= 1)
  399: 		usbd_get_xfer_status(reqh, 0, 0, &tn, 0);
  400: #else
  401: 		usbd_get_request_status(reqh, &r_priv, &r_buff, &tn, &r_status);
  402: #endif
  403: 
  404: 		DPRINTFN(1, ("urioread: got %d bytes\n", tn));
  405: 		error = uiomove(buf, tn, uio);
  406: 		if (error || tn < n)
  407: 			break;
  408: 	}
  409: #if (USBDI >= 1)
  410: 	usbd_free_xfer(reqh);
  411: #else
  412: 	usbd_free_request(reqh);
  413: #endif
  414: 
  415: 	return error;
  416: }
  417: 
  418: int
  419: uriowrite(dev_t dev, struct uio *uio, int flag)
  420: {
  421: #if (USBDI >= 1)
  422: 	struct urio_softc * sc;
  423: 	usbd_xfer_handle reqh;
  424: #else
  425: 	usbd_request_handle reqh;
  426: #endif
  427: 	int unit = URIOUNIT(dev);
  428: 	usbd_status r;
  429: 	char buf[URIO_BBSIZE];
  430: 	u_int32_t n;
  431: 	int error = 0;
  432: 
  433: 	USB_GET_SC(urio, unit, sc);
  434: 
  435: 	DPRINTFN(5, ("uriowrite: %d\n", unit));
  436: 	if (!sc->sc_opened)
  437: 		return EIO;
  438: 
  439: #if (USBDI >= 1)
  440: 	sc->sc_refcnt++;
  441: 	reqh = usbd_alloc_xfer(sc->sc_udev);
  442: #else
  443: 	reqh = usbd_alloc_request();
  444: #endif
  445: 	if (reqh == 0)
  446: 		return EIO;
  447: 	while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
  448: 		error = uiomove(buf, n, uio);
  449: 		if (error)
  450: 			break;
  451: 		DPRINTFN(1, ("uriowrite: transfer %d bytes\n", n));
  452: #if (USBDI >= 1)
  453:  		usbd_setup_xfer(reqh, sc->sc_pipeh_out, 0, buf, n,
  454: 				       0, RIO_RW_TIMEOUT, 0);
  455: #else
  456: 		r = usbd_setup_request(reqh, sc->sc_pipeh_out, 0, buf, n,
  457: 				       0, RIO_RW_TIMEOUT, 0);
  458: 		if (r != USBD_NORMAL_COMPLETION) {
  459: 			error = EIO;
  460: 			break;
  461: 		}
  462: #endif
  463: 		r = usbd_sync_transfer(reqh);
  464: 		if (r != USBD_NORMAL_COMPLETION) {
  465: 			DPRINTFN(1, ("uriowrite: error=%d\n", r));
  466: 			usbd_clear_endpoint_stall(sc->sc_pipeh_out);
  467: 			error = EIO;
  468: 			break;
  469: 		}
  470: #if (USBDI >= 1)
  471: 		usbd_get_xfer_status(reqh, 0, 0, 0, 0);
  472: #endif
  473: 	}
  474: 
  475: #if (USBDI >= 1)
  476: 	usbd_free_xfer(reqh);
  477: #else
  478: 	usbd_free_request(reqh);
  479: #endif
  480: 
  481: 	return error;
  482: }
  483: 
  484: 
  485: int
  486: urioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
  487: {
  488: #if (USBDI >= 1)
  489: 	struct urio_softc * sc;
  490: #endif
  491: 	int unit = URIOUNIT(dev);
  492: 	struct RioCommand *rio_cmd;
  493: 	int requesttype, len;
  494: 	struct iovec iov;
  495: 	struct uio uio;
  496: 	usb_device_request_t req;
  497: 	int req_flags = 0, req_actlen = 0;
  498: 	void *ptr = 0;
  499: 	int error = 0;
  500: 	usbd_status r;
  501: 
  502: 	USB_GET_SC(urio, unit, sc);
  503: 
  504: 	switch (cmd) {
  505: 	case RIO_RECV_COMMAND:
  506: 		if (!(flag & FWRITE))
  507: 			return EPERM;
  508: 		rio_cmd = (struct RioCommand *)addr;
  509: 		if (rio_cmd == NULL)
  510: 			return EINVAL;
  511: 		len = rio_cmd->length;
  512: 
  513: 		requesttype = rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
  514: 		DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
  515: 			requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
  516: 		break;
  517: 
  518: 	case RIO_SEND_COMMAND:
  519: 		if (!(flag & FWRITE))
  520: 			return EPERM;
  521: 		rio_cmd = (struct RioCommand *)addr;
  522: 		if (rio_cmd == NULL)
  523: 			return EINVAL;
  524: 		len = rio_cmd->length;
  525: 
  526: 		requesttype = rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
  527: 		DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
  528: 			requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
  529: 		break;
  530: 
  531: 	default:
  532: 		return EINVAL;
  533: 		break;
  534: 	}
  535: 
  536: 	/* Send rio control message */
  537: 	req.bmRequestType = requesttype;
  538: 	req.bRequest = rio_cmd->request;
  539: 	USETW(req.wValue, rio_cmd->value);
  540: 	USETW(req.wIndex, rio_cmd->index);
  541: 	USETW(req.wLength, len);
  542: 
  543: 	if (len < 0 || len > 32767)
  544: 		return EINVAL;
  545: 	if (len != 0) {
  546: 		iov.iov_base = (caddr_t)rio_cmd->buffer;
  547: 		iov.iov_len = len;
  548: 		uio.uio_iov = &iov;
  549: 		uio.uio_iovcnt = 1;
  550: 		uio.uio_resid = len;
  551: 		uio.uio_offset = 0;
  552: 		uio.uio_segflg = UIO_USERSPACE;
  553: 		uio.uio_rw =
  554: 			req.bmRequestType & UT_READ ?
  555: 			UIO_READ : UIO_WRITE;
  556: 		uio.uio_td = p;
  557: 		ptr = malloc(len, M_TEMP, M_WAITOK);
  558: 		if (uio.uio_rw == UIO_WRITE) {
  559: 			error = uiomove(ptr, len, &uio);
  560: 			if (error)
  561: 				goto ret;
  562: 		}
  563: 	}
  564: 
  565: 	r = usbd_do_request_flags(sc->sc_udev, &req,
  566: 				  ptr, req_flags, &req_actlen,
  567: 				  USBD_DEFAULT_TIMEOUT);
  568: 	if (r == USBD_NORMAL_COMPLETION) {
  569: 		error = 0;
  570: 		if (len != 0) {
  571: 			if (uio.uio_rw == UIO_READ) {
  572: 				error = uiomove(ptr, len, &uio);
  573: 			}
  574: 		}
  575: 	} else {
  576: 		error = EIO;
  577: 	}
  578: 
  579: ret:
  580: 	if (ptr)
  581: 		free(ptr, M_TEMP);
  582: 	return error;
  583: }
  584: 
  585: 
  586: #if defined(__NetBSD__) || defined(__OpenBSD__)
  587: int
  588: urio_activate(device_ptr_t self, enum devact act)
  589: {
  590: 	struct urio_softc *sc = (struct urio_softc *)self;
  591: 
  592: 	switch (act) {
  593: 	case DVACT_ACTIVATE:
  594: 		return (EOPNOTSUPP);
  595: 		break;
  596: 
  597: 	case DVACT_DEACTIVATE:
  598: 		sc->sc_dying = 1;
  599: 		break;
  600: 	}
  601: 	return (0);
  602: }
  603: 
  604: USB_DETACH(urio)
  605: {
  606: 	USB_DETACH_START(urio, sc);
  607: 	struct urio_endpoint *sce;
  608: 	int i, dir;
  609: 	int s;
  610: #if defined(__NetBSD__) || defined(__OpenBSD__)
  611: 	int maj, mn;
  612: 
  613: 	DPRINTF(("urio_detach: sc=%p flags=%d\n", sc, flags));
  614: #elif defined(__FreeBSD__) || defined(__DragonFly__)
  615: 	DPRINTF(("urio_detach: sc=%p\n", sc));
  616: #endif
  617: 
  618: 	sc->sc_dying = 1;
  619: 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
  620: #if 0
  621: 	for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
  622: 		for (dir = OUT; dir <= IN; dir++) {
  623: 			sce = &sc->sc_endpoints[i][dir];
  624: 			if (sce && sce->pipeh)
  625: 				usbd_abort_pipe(sce->pipeh);
  626: 		}
  627: 	}
  628: 
  629: 	s = splusb();
  630: 	if (--sc->sc_refcnt >= 0) {
  631: 		/* Wake everyone */
  632: 		for (i = 0; i < USB_MAX_ENDPOINTS; i++)
  633: 			wakeup(&sc->sc_endpoints[i][IN]);
  634: 		/* Wait for processes to go away. */
  635: 		usb_detach_wait(USBDEV(sc->sc_dev));
  636: 	}
  637: 	splx(s);
  638: #else
  639: 	if (sc->sc_pipeh_in)
  640: 		usbd_abort_pipe(sc->sc_pipeh_in);
  641: 
  642: 	if (sc->sc_pipeh_out)
  643: 		usbd_abort_pipe(sc->sc_pipeh_out);
  644: 
  645: 	s = splusb();
  646: 	if (--sc->sc_refcnt >= 0) {
  647: 		/* Wait for processes to go away. */
  648: 		usb_detach_wait(USBDEV(sc->sc_dev));
  649: 	}
  650: 	splx(s);
  651: #endif
  652: 
  653: #if defined(__NetBSD__) || defined(__OpenBSD__)
  654: 	/* locate the major number */
  655: 	for (maj = 0; maj < nchrdev; maj++)
  656: 		if (cdevsw[maj].d_open == urioopen)
  657: 			break;
  658: 
  659: 	/* Nuke the vnodes for any open instances (calls close). */
  660: 	mn = self->dv_unit * USB_MAX_ENDPOINTS;
  661: 	vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
  662: #endif
  663: 
  664: 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
  665: 			   USBDEV(sc->sc_dev));
  666: 
  667: 	return (0);
  668: }
  669: #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
  670: 
  671: #if defined(__FreeBSD__) || defined(__DragonFly__)
  672: Static int
  673: urio_detach(device_t self)
  674: {
  675: 	DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));
  676: 	cdevsw_remove(&urio_cdevsw, -1, device_get_unit(self));
  677: 	/* XXX not implemented yet */
  678: 	device_set_desc(self, NULL);
  679: 	return 0;
  680: }
  681: 
  682: DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, usbd_driver_load, 0);
  683: #endif