File:  [DragonFly] / src / sys / dev / usbmisc / ufm / ufm.c
Revision 1.8: download - view: text, annotated - select for diffs
Wed May 19 22:52:51 2004 UTC (10 years, 3 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) 2001 M. Warner Losh
    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/ufm.c,v 1.16 2003/10/04 21:41:01 joe Exp $
   33:  * $DragonFly: src/sys/dev/usbmisc/ufm/ufm.c,v 1.8 2004/05/19 22:52:51 dillon Exp $
   34:  */
   35: 
   36: #include <sys/param.h>
   37: #include <sys/systm.h>
   38: #include <sys/kernel.h>
   39: #include <sys/malloc.h>
   40: #if defined(__NetBSD__)
   41: #include <sys/device.h>
   42: #include <sys/ioctl.h>
   43: #elif defined(__FreeBSD__) || defined(__DragonFly__)
   44: #include <sys/module.h>
   45: #include <sys/bus.h>
   46: #include <sys/ioccom.h>
   47: #endif
   48: #include <sys/fcntl.h>
   49: #include <sys/filio.h>
   50: #include <sys/conf.h>
   51: #include <sys/uio.h>
   52: #include <sys/tty.h>
   53: #include <sys/file.h>
   54: #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
   55: #include <sys/selinfo.h>
   56: #else
   57: #include <sys/select.h>
   58: #endif
   59: #include <sys/vnode.h>
   60: #include <sys/poll.h>
   61: #include <sys/sysctl.h>
   62: 
   63: #include <bus/usb/usb.h>
   64: #include <bus/usb/usbdi.h>
   65: #include <bus/usb/usbdi_util.h>
   66: 
   67: #include <bus/usb/usbdevs.h>
   68: #include <bus/usb/dsbr100io.h>
   69: 
   70: #ifdef USB_DEBUG
   71: #define DPRINTF(x)	if (ufmdebug) logprintf x
   72: #define DPRINTFN(n,x)	if (ufmdebug>(n)) logprintf x
   73: int	ufmdebug = 0;
   74: SYSCTL_NODE(_hw_usb, OID_AUTO, ufm, CTLFLAG_RW, 0, "USB ufm");
   75: SYSCTL_INT(_hw_usb_ufm, OID_AUTO, debug, CTLFLAG_RW,
   76: 	   &ufmdebug, 0, "ufm debug level");
   77: #else
   78: #define DPRINTF(x)
   79: #define DPRINTFN(n,x)
   80: #endif
   81: 
   82: #if defined(__NetBSD__) || defined(__OpenBSD__)
   83: int ufmopen(dev_t, int, int, usb_proc_ptr);
   84: int ufmclose(dev_t, int, int, usb_proc_ptr);
   85: int ufmioctl(dev_t, u_long, caddr_t, int, usb_proc_ptr);
   86: 
   87: cdev_decl(ufm);
   88: #elif defined(__FreeBSD__) || defined(__DragonFly__)
   89: d_open_t  ufmopen;
   90: d_close_t ufmclose;
   91: d_ioctl_t ufmioctl;
   92: 
   93: #define UFM_CDEV_MAJOR	200
   94: 
   95: Static struct cdevsw ufm_cdevsw = {
   96:  	/* name */	"ufm",	
   97: 	/* cmaj */	UFM_CDEV_MAJOR,	
   98: 	/* flags */	0,
   99: 	/* port */	NULL,
  100: 	/* clone */	NULL,
  101: 	ufmopen,	ufmclose,	noread,		nowrite,
  102:  	ufmioctl,	nopoll,		nommap,		nostrategy,
  103: 	nodump,		nopsize
  104: };
  105: #endif  /*defined(__FreeBSD__)*/
  106: 
  107: #define FM_CMD0		0x00
  108: #define FM_CMD_SET_FREQ	0x01
  109: #define FM_CMD2		0x02
  110: 
  111: struct ufm_softc {
  112:  	USBBASEDEVICE sc_dev;
  113: 	usbd_device_handle sc_udev;
  114: 	usbd_interface_handle sc_iface;
  115: 
  116: 	int sc_opened;
  117: 	int sc_epaddr;
  118: 	int sc_freq;
  119: 
  120: 	int sc_refcnt;
  121: #if defined(__NetBSD__) || defined(__OpenBSD__)
  122: 	u_char sc_dying;
  123: #endif
  124: };
  125: 
  126: #define UFMUNIT(n) (minor(n))
  127: 
  128: USB_DECLARE_DRIVER(ufm);
  129: 
  130: USB_MATCH(ufm)
  131: {
  132: 	USB_MATCH_START(ufm, uaa);
  133: 	usb_device_descriptor_t *dd;
  134: 
  135: 	DPRINTFN(10,("ufm_match\n"));
  136: 	if (!uaa->iface)
  137: 		return UMATCH_NONE;
  138: 
  139: 	dd = usbd_get_device_descriptor(uaa->device);
  140: 
  141: 	if (dd &&
  142: 	    ((UGETW(dd->idVendor) == USB_VENDOR_CYPRESS &&
  143: 	    UGETW(dd->idProduct) == USB_PRODUCT_CYPRESS_FMRADIO)))
  144: 		return UMATCH_VENDOR_PRODUCT;
  145: 	else
  146: 		return UMATCH_NONE;
  147: }
  148: 
  149: USB_ATTACH(ufm)
  150: {
  151: 	USB_ATTACH_START(ufm, sc, uaa);
  152: 	char devinfo[1024];
  153: 	usb_endpoint_descriptor_t *edesc;
  154: 	usbd_device_handle udev;
  155: 	usbd_interface_handle iface;
  156: 	u_int8_t epcount;
  157: #if defined(__NetBSD__) || defined(__OpenBSD__)
  158: 	u_int8_t niface;
  159: #endif
  160: 	usbd_status r;
  161: 	char * ermsg = "<none>";
  162: 
  163: 	DPRINTFN(10,("ufm_attach: sc=%p\n", sc));
  164: 	usbd_devinfo(uaa->device, 0, devinfo);
  165: 	USB_ATTACH_SETUP;
  166: 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
  167: 
  168: 	sc->sc_udev = udev = uaa->device;
  169: 
  170: #if defined(__FreeBSD__) || defined(__DragonFly__)
  171:  	if ((!uaa->device) || (!uaa->iface)) {
  172: 		ermsg = "device or iface";
  173:  		goto nobulk;
  174: 	}
  175: 	sc->sc_iface = iface = uaa->iface;
  176: #elif defined(__NetBSD__) || defined(__OpenBSD__)
  177:  	if (!udev) {
  178: 		ermsg = "device";
  179:  		goto nobulk;
  180: 	}
  181: 	r = usbd_interface_count(udev, &niface);
  182: 	if (r) {
  183: 		ermsg = "iface";
  184: 		goto nobulk;
  185: 	}
  186: 	r = usbd_device2interface_handle(udev, 0, &iface);
  187: 	if (r) {
  188: 		ermsg = "iface";
  189: 		goto nobulk;
  190: 	}
  191: 	sc->sc_iface = iface;
  192: #endif
  193: 	sc->sc_opened = 0;
  194: 	sc->sc_refcnt = 0;
  195: 
  196: 	r = usbd_endpoint_count(iface, &epcount);
  197: 	if (r != USBD_NORMAL_COMPLETION) {
  198: 		ermsg = "endpoints";
  199: 		goto nobulk;
  200: 	}
  201: 
  202: 	edesc = usbd_interface2endpoint_descriptor(iface, 0);
  203: 	if (!edesc) {
  204: 		ermsg = "interface endpoint";
  205: 		goto nobulk;
  206: 	}
  207: 	sc->sc_epaddr = edesc->bEndpointAddress;
  208: 
  209: #if defined(__FreeBSD__) || defined(__DragonFly__)
  210: 	/* XXX no error trapping, no storing of dev_t */
  211: 	cdevsw_add(&ufm_cdevsw, -1, device_get_unit(self));
  212: 	make_dev(&ufm_cdevsw, device_get_unit(self),
  213: 			UID_ROOT, GID_OPERATOR,
  214: 			0644, "ufm%d", device_get_unit(self));
  215: #elif defined(__NetBSD__) || defined(__OpenBSD__)
  216: 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
  217: 			   USBDEV(sc->sc_dev));
  218: #endif
  219: 
  220: 	DPRINTFN(10, ("ufm_attach: %p\n", sc->sc_udev));
  221: 
  222: 	USB_ATTACH_SUCCESS_RETURN;
  223: 
  224:  nobulk:
  225: 	printf("%s: could not find %s\n", USBDEVNAME(sc->sc_dev),ermsg);
  226: 	USB_ATTACH_ERROR_RETURN;
  227: }
  228: 
  229: 
  230: int
  231: ufmopen(dev_t dev, int flag, int mode, usb_proc_ptr td)
  232: {
  233: 	struct ufm_softc *sc;
  234: 
  235: 	int unit = UFMUNIT(dev);
  236: 	USB_GET_SC_OPEN(ufm, unit, sc);
  237: 
  238: 	DPRINTFN(5, ("ufmopen: flag=%d, mode=%d, unit=%d\n",
  239: 		     flag, mode, unit));
  240: 
  241: 	if (sc->sc_opened)
  242: 		return (EBUSY);
  243: 
  244: 	if ((flag & (FWRITE|FREAD)) != (FWRITE|FREAD))
  245: 		return (EACCES);
  246: 
  247: 	sc->sc_opened = 1;
  248: 	return (0);
  249: }
  250: 
  251: int
  252: ufmclose(dev_t dev, int flag, int mode, usb_proc_ptr td)
  253: {
  254: 	struct ufm_softc *sc;
  255: 
  256: 	int unit = UFMUNIT(dev);
  257: 	USB_GET_SC(ufm, unit, sc);
  258: 
  259: 	DPRINTFN(5, ("ufmclose: flag=%d, mode=%d, unit=%d\n", flag, mode, unit));
  260: 	sc->sc_opened = 0;
  261: 	sc->sc_refcnt = 0;
  262: 	return 0;
  263: }
  264: 
  265: static int
  266: ufm_do_req(struct ufm_softc *sc, u_int8_t reqtype, u_int8_t request,
  267:     u_int16_t value, u_int16_t index, u_int8_t len, void *retbuf)
  268: {
  269: 	int s;
  270: 	usb_device_request_t req;
  271: 	usbd_status err;
  272: 
  273: 	s = splusb();
  274: 	req.bmRequestType = reqtype;
  275: 	req.bRequest = request;
  276: 	USETW(req.wValue, value);
  277: 	USETW(req.wIndex, index);
  278: 	USETW(req.wLength, len);
  279: 	err = usbd_do_request_flags(sc->sc_udev, &req, retbuf, 0, NULL,
  280: 	    USBD_DEFAULT_TIMEOUT);
  281: 	splx(s);
  282: 	if (err) {
  283: 		printf("usbd_do_request_flags returned %#x\n", err);
  284: 		return (EIO);
  285: 	}
  286: 	return (0);
  287: }
  288: 
  289: static int
  290: ufm_set_freq(struct ufm_softc *sc, caddr_t addr)
  291: {
  292: 	int freq = *(int *)addr;
  293: 	u_int8_t ret;
  294: 
  295: 	/*
  296: 	 * Freq now is in Hz.  We need to convert it to the frequency
  297: 	 * that the radio wants.  This frequency is 10.7MHz above
  298: 	 * the actual frequency.  We then need to convert to
  299: 	 * units of 12.5kHz.  We add one to the IFM to make rounding
  300: 	 * easier.
  301: 	 */
  302: 	sc->sc_freq = freq;
  303: 	freq = (freq + 10700001) / 12500;
  304: 	/* This appears to set the frequency */
  305: 	if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD_SET_FREQ, freq >> 8,
  306: 	    freq, 1, &ret) != 0)
  307: 		return (EIO);
  308: 	/* Not sure what this does */
  309: 	if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x96, 0xb7, 1,
  310: 	    &ret) != 0)
  311: 		return (EIO);
  312: 	return (0);
  313: }
  314: 
  315: static int
  316: ufm_get_freq(struct ufm_softc *sc, caddr_t addr)
  317: {
  318: 	int *valp = (int *)addr;
  319: 	*valp = sc->sc_freq;
  320: 	return (0);
  321: }
  322: 
  323: static int
  324: ufm_start(struct ufm_softc *sc, caddr_t addr)
  325: {
  326: 	u_int8_t ret;
  327: 
  328: 	if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0xc7,
  329: 	    1, &ret))
  330: 		return (EIO);
  331: 	if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x01, 0x00,
  332: 	    1, &ret))
  333: 		return (EIO);
  334: 	if (ret & 0x1)
  335: 		return (EIO);
  336: 	return (0);
  337: }
  338: 
  339: static int
  340: ufm_stop(struct ufm_softc *sc, caddr_t addr)
  341: {
  342: 	u_int8_t ret;
  343: 
  344: 	if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x16, 0x1C,
  345: 	    1, &ret))
  346: 		return (EIO);
  347: 	if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x00, 0x00,
  348: 	    1, &ret))
  349: 		return (EIO);
  350: 	return (0);
  351: }
  352: 
  353: static int
  354: ufm_get_stat(struct ufm_softc *sc, caddr_t addr)
  355: {
  356: 	u_int8_t ret;
  357: 
  358: 	/*
  359: 	 * Note, there's a 240ms settle time before the status
  360: 	 * will be valid, so tsleep that amount.  hz/4 is a good
  361: 	 * approximation of that.  Since this is a short sleep
  362: 	 * we don't try to catch any signals to keep things
  363: 	 * simple.
  364: 	 */
  365: 	tsleep(sc, 0, "ufmwait", hz/4);
  366: 	if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0x24,
  367: 	    1, &ret))
  368: 		return (EIO);
  369: 	*(int *)addr = ret;
  370: 
  371: 	return (0);
  372: }
  373: 
  374: int
  375: ufmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr td)
  376: {
  377: 	struct ufm_softc *sc;
  378: 
  379: 	int unit = UFMUNIT(dev);
  380: 	int error = 0;
  381: 
  382: 	USB_GET_SC(ufm, unit, sc);
  383: 
  384: 	switch (cmd) {
  385: 	case FM_SET_FREQ:
  386: 		error = ufm_set_freq(sc, addr);
  387: 		break;
  388: 	case FM_GET_FREQ:
  389: 		error = ufm_get_freq(sc, addr);
  390: 		break;
  391: 	case FM_START:
  392: 		error = ufm_start(sc, addr);
  393: 		break;
  394: 	case FM_STOP:
  395: 		error = ufm_stop(sc, addr);
  396: 		break;
  397: 	case FM_GET_STAT:
  398: 		error = ufm_get_stat(sc, addr);
  399: 		break;
  400: 	default:
  401: 		return ENOTTY;
  402: 		break;
  403: 	}
  404: 	return error;
  405: }
  406: 
  407: 
  408: #if defined(__NetBSD__) || defined(__OpenBSD__)
  409: int
  410: ufm_activate(device_ptr_t self, enum devact act)
  411: {
  412: 	struct ufm_softc *sc = (struct ufm_softc *)self;
  413: 
  414: 	switch (act) {
  415: 	case DVACT_ACTIVATE:
  416: 		return (EOPNOTSUPP);
  417: 		break;
  418: 
  419: 	case DVACT_DEACTIVATE:
  420: 		sc->sc_dying = 1;
  421: 		break;
  422: 	}
  423: 	return (0);
  424: }
  425: 
  426: USB_DETACH(ufm)
  427: {
  428: 	USB_DETACH_START(ufm, sc);
  429: 	struct ufm_endpoint *sce;
  430: 	int i, dir;
  431: 	int s;
  432: #if defined(__NetBSD__) || defined(__OpenBSD__)
  433: 	int maj, mn;
  434: 
  435: 	DPRINTF(("ufm_detach: sc=%p flags=%d\n", sc, flags));
  436: #elif defined(__FreeBSD__) || defined(__DragonFly__)
  437: 	DPRINTF(("ufm_detach: sc=%p\n", sc));
  438: #endif
  439: 
  440: 	sc->sc_dying = 1;
  441: 
  442: 	s = splusb();
  443: 	if (--sc->sc_refcnt >= 0) {
  444: 		/* Wait for processes to go away. */
  445: 		usb_detach_wait(USBDEV(sc->sc_dev));
  446: 	}
  447: 	splx(s);
  448: 
  449: #if defined(__NetBSD__) || defined(__OpenBSD__)
  450: 	/* locate the major number */
  451: 	for (maj = 0; maj < nchrdev; maj++)
  452: 		if (cdevsw[maj].d_open == ufmopen)
  453: 			break;
  454: 
  455: 	/* Nuke the vnodes for any open instances (calls close). */
  456: 	mn = self->dv_unit * USB_MAX_ENDPOINTS;
  457: 	vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
  458: #elif defined(__FreeBSD__) || defined(__DragonFly__)
  459: 	/* XXX not implemented yet */
  460: #endif
  461: 
  462: 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
  463: 			   USBDEV(sc->sc_dev));
  464: 
  465: 	return (0);
  466: }
  467: #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
  468: 
  469: #if defined(__FreeBSD__) || defined(__DragonFly__)
  470: Static int
  471: ufm_detach(device_t self)
  472: {
  473: 	DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));
  474: 	return 0;
  475: }
  476: 
  477: DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, usbd_driver_load, 0);
  478: #endif