USB changes

M. Warner Losh imp at bsdimp.com
Wed Sep 6 04:01:28 UTC 2006


In message: <20060905.215047.-494097326.imp at bsdimp.com>
            "M. Warner Losh" <imp at bsdimp.com> writes:
: The time has come to reduce the #ifdefs that have grown to infest the
: usb code.  The other BSDs have already done this to a greater or
: lessor extent.  Many of the #ifdef sections have decayed and no longer
: work on the target system.  There's been a number of cases where bugs
: have resulted, and they generally make the code harder to read and
: understand.
: 
: I'd like to remove many of the ifdefs, as well as expand many of the
: portability hacks that are there.  I'll do my best to make sure that
: each of the changes bring the files closer to style(9) as well as not
: change the md5 of the .o file.
: 
: Comments?

Here's ugen.c that I've partially converted that retains the same md5
for ugen.o when compiled as a module.

Warner
-------------- next part --------------
Index: ugen.c
===================================================================
RCS file: /cache/ncvs/src/sys/dev/usb/ugen.c,v
retrieving revision 1.106
diff -u -r1.106 ugen.c
--- ugen.c	5 Jun 2006 14:44:39 -0000	1.106
+++ ugen.c	6 Sep 2006 03:57:03 -0000
@@ -52,24 +52,15 @@
 #include <sys/systm.h>
 #include <sys/kernel.h>
 #include <sys/malloc.h>
-#if defined(__NetBSD__) || defined(__OpenBSD__)
-#include <sys/device.h>
-#include <sys/ioctl.h>
-#elif defined(__FreeBSD__)
 #include <sys/module.h>
 #include <sys/bus.h>
 #include <sys/ioccom.h>
 #include <sys/conf.h>
 #include <sys/fcntl.h>
 #include <sys/filio.h>
-#endif
 #include <sys/tty.h>
 #include <sys/file.h>
-#if __FreeBSD_version >= 500014
 #include <sys/selinfo.h>
-#else
-#include <sys/select.h>
-#endif
 #include <sys/poll.h>
 #include <sys/sysctl.h>
 #include <sys/uio.h>
@@ -100,9 +91,7 @@
 
 struct ugen_endpoint {
 	struct ugen_softc *sc;
-#if defined(__FreeBSD__)
 	struct cdev *dev;
-#endif
 	usb_endpoint_descriptor_t *edesc;
 	usbd_interface_handle iface;
 	int state;
@@ -127,16 +116,13 @@
 struct ugen_softc {
 	USBBASEDEVICE sc_dev;		/* base device */
 	usbd_device_handle sc_udev;
-#if defined(__FreeBSD__)
 	struct cdev *dev;
-#endif
 
 	char sc_is_open[USB_MAX_ENDPOINTS];
 	struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
 #define OUT 0
 #define IN  1
 
-#ifdef __FreeBSD__
 #define	UGEN_DEV_REF(dev, sc)					\
 	if ((sc)->sc_dying || dev_refthread(dev) == NULL)	\
 		return (ENXIO)
@@ -146,26 +132,9 @@
 	/* handled by dev layer */
 #define	UGEN_DEV_CLOSE(dev, sc)		\
 	/* handled by dev layer */
-#else
-	int sc_refcnt;
-#define	UGEN_DEV_REF(dev, sc)	\
-	if ((sc)->sc_dying)	\
-		return (ENXIO);	\
-	(sc)->sc_refcnt++
-#define	UGEN_DEV_RELE(dev, sc)				\
-	if (--(sc)->sc_refcnt < 0)			\
-		usb_detach_wakeup(USBDEV((sc)->sc_dev))
-#define	UGEN_DEV_OPEN(dev, sc)	\
-	(sc)->sc_refcnt++
-#define	UGEN_DEV_CLOSE(dev, sc)	\
-	UGEN_DEV_RELE(dev, sc)
-#endif
 	u_char sc_dying;
 };
 
-#if defined(__NetBSD__) || defined(__OpenBSD__)
-cdev_decl(ugen);
-#elif defined(__FreeBSD__)
 d_open_t  ugenopen;
 d_close_t ugenclose;
 d_read_t  ugenread;
@@ -174,7 +143,7 @@
 d_poll_t  ugenpoll;
 d_purge_t ugenpurge;
 
-Static struct cdevsw ugenctl_cdevsw = {
+static struct cdevsw ugenctl_cdevsw = {
 	.d_version =	D_VERSION,
 	.d_flags =	D_NEEDGIANT,
 	.d_open =	ugenopen,
@@ -182,12 +151,9 @@
 	.d_ioctl =	ugenioctl,
 	.d_purge =	ugenpurge,
 	.d_name =	"ugenctl",
-#if __FreeBSD_version < 500014
-	.d_bmaj		-1
-#endif
 };
 
-Static struct cdevsw ugen_cdevsw = {
+static struct cdevsw ugen_cdevsw = {
 	.d_version =	D_VERSION,
 	.d_flags =	D_NEEDGIANT,
 	.d_open =	ugenopen,
@@ -198,29 +164,23 @@
 	.d_poll =	ugenpoll,
 	.d_purge =	ugenpurge,
 	.d_name =	"ugen",
-#if __FreeBSD_version < 500014
-	.d_bmaj		-1
-#endif
 };
-#endif
 
-Static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
+static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
 		     usbd_status status);
-Static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
+static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
 			    usbd_status status);
-Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
-Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
-Static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
+static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
+static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
+static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
 			 caddr_t, int, usb_proc_ptr);
-#if defined(__FreeBSD__)
-Static void ugen_make_devnodes(struct ugen_softc *sc);
-Static void ugen_destroy_devnodes(struct ugen_softc *sc);
-#endif
-Static int ugen_set_config(struct ugen_softc *sc, int configno);
-Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
+static void ugen_make_devnodes(struct ugen_softc *sc);
+static void ugen_destroy_devnodes(struct ugen_softc *sc);
+static int ugen_set_config(struct ugen_softc *sc, int configno);
+static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
 					       int index, int *lenp);
-Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
-Static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
+static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
+static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
 
 #define UGENUNIT(n) ((minor(n) >> 4) & 0xf)
 #define UGENENDPOINT(n) (minor(n) & 0xf)
@@ -276,13 +236,11 @@
 		USB_ATTACH_ERROR_RETURN;
 	}
 
-#if defined(__FreeBSD__)
 	/* the main device, ctrl endpoint */
 	sc->dev = make_dev(&ugenctl_cdevsw,
 	    UGENMINOR(USBDEVUNIT(sc->sc_dev), 0), UID_ROOT, GID_OPERATOR, 0644,
 	    "%s", USBDEVNAME(sc->sc_dev));
 	ugen_make_devnodes(sc);
-#endif
 
 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
 	    USBDEV(sc->sc_dev));
@@ -290,8 +248,7 @@
 	USB_ATTACH_SUCCESS_RETURN;
 }
 
-#if defined(__FreeBSD__)
-Static void
+static void
 ugen_make_devnodes(struct ugen_softc *sc)
 {
 	int endptno;
@@ -322,7 +279,7 @@
 	}
 }
 
-Static void
+static void
 ugen_destroy_devnodes(struct ugen_softc *sc)
 {
 	int endptno, prev_sc_dying;
@@ -358,9 +315,8 @@
 	}
 	sc->sc_dying = prev_sc_dying;
 }
-#endif
 
-Static int
+static int
 ugen_set_config(struct ugen_softc *sc, int configno)
 {
 	usbd_device_handle dev = sc->sc_udev;
@@ -433,9 +389,7 @@
 		}
 	}
 
-#if defined(__FreeBSD__)
 	ugen_destroy_devnodes(sc);
-#endif
 
 	/* now we can clear the old interface's ugen_endpoints */
 	for(ifaceno = 0; ifaceno < niface_cache; ifaceno++) {
@@ -698,7 +652,7 @@
 	return (0);
 }
 
-Static int
+static int
 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
 {
 	struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
@@ -858,7 +812,7 @@
 	return (error);
 }
 
-Static int
+static int
 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
 {
 	struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
@@ -962,25 +916,6 @@
 	return (error);
 }
 
-#if defined(__NetBSD__) || defined(__OpenBSD__)
-int
-ugen_activate(device_ptr_t self, enum devact act)
-{
-	struct ugen_softc *sc = (struct ugen_softc *)self;
-
-	switch (act) {
-	case DVACT_ACTIVATE:
-		return (EOPNOTSUPP);
-
-	case DVACT_DEACTIVATE:
-		sc->sc_dying = 1;
-		break;
-	}
-	return (0);
-}
-#endif
-
-#ifdef __FreeBSD__
 void
 ugenpurge(struct cdev *dev)
 {
@@ -1009,22 +944,14 @@
 	}
 	selwakeuppri(&sce->rsel, PZERO);
 }
-#endif
 
 USB_DETACH(ugen)
 {
 	USB_DETACH_START(ugen, sc);
 	struct ugen_endpoint *sce;
 	int i, dir;
-#if defined(__NetBSD__) || defined(__OpenBSD__)
-	int maj, mn, c, s;
-#endif
 
-#if defined(__NetBSD__) || defined(__OpenBSD__)
-	DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
-#elif defined(__FreeBSD__)
 	DPRINTF(("ugen_detach: sc=%p\n", sc));
-#endif
 
 	sc->sc_dying = 1;
 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
@@ -1037,41 +964,8 @@
 		}
 	}
 
-#if defined(__NetBSD__) || defined(__OpenBSD__)
-	s = splusb();
-	if (sc->sc_refcnt > 0) {
-		/* Wake everyone */
-		for (i = 0; i < USB_MAX_ENDPOINTS; i++)
-			wakeup(&sc->sc_endpoints[i][IN]);
-		/* Wait for processes to go away. */
-		while (sc->sc_refcnt > 0)
-			usb_detach_wait(USBDEV(sc->sc_dev));
-	}
-	splx(s);
-
-	/* Wait for opens to go away. */
-	do {
-		c = 0;
-		for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
-			if (sc->sc_is_open[i])
-				c++;
-		}
-		if (c != 0)
-			tsleep(&sc->sc_dying, PZERO, "ugendr", hz);
-	} while (c != 0);
-
-	/* locate the major number */
-	for (maj = 0; maj < nchrdev; maj++)
-		if (cdevsw[maj].d_open == ugenopen)
-			break;
-
-	/* Nuke the vnodes for any open instances (calls close). */
-	mn = self->dv_unit * USB_MAX_ENDPOINTS;
-	vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
-#elif defined(__FreeBSD__)
 	/* destroy the device for the control endpoint */
 	destroy_dev(sc->dev);
-#endif
 
 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
 	    USBDEV(sc->sc_dev));
@@ -1079,7 +973,7 @@
 	return (0);
 }
 
-Static void
+static void
 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
 {
 	struct ugen_endpoint *sce = addr;
@@ -1115,7 +1009,7 @@
 	selwakeuppri(&sce->rsel, PZERO);
 }
 
-Static void
+static void
 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
 		usbd_status status)
 {
@@ -1174,7 +1068,7 @@
 	selwakeuppri(&sce->rsel, PZERO);
 }
 
-Static usbd_status
+static usbd_status
 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
 {
 	usbd_interface_handle iface;
@@ -1222,10 +1116,8 @@
 	if (err)
 		panic("ugen_set_interface: endpoint count failed");
 
-#if defined(__FreeBSD__)
 	/* destroy the existing devices, we remake the new ones in a moment */
 	ugen_destroy_devnodes(sc);
-#endif
 
 	/* now we can clear the old interface's ugen_endpoints */
 	for (endptno = 0; endptno < nendpt_cache; endptno++) {
@@ -1247,16 +1139,14 @@
 		sce->iface = iface;
 	}
 
-#if defined(__FreeBSD__)
 	/* make the new devices */
 	ugen_make_devnodes(sc);
-#endif
 
 	return (0);
 }
 
 /* Retrieve a complete descriptor for a certain device and index. */
-Static usb_config_descriptor_t *
+static usb_config_descriptor_t *
 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
 {
 	usb_config_descriptor_t *cdesc, *tdesc, cdescr;
@@ -1289,7 +1179,7 @@
 	return (cdesc);
 }
 
-Static int
+static int
 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
 {
 	usbd_interface_handle iface;
@@ -1301,7 +1191,7 @@
 	return (usbd_get_interface_altindex(iface));
 }
 
-Static int
+static int
 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
 	      caddr_t addr, int flag, usb_proc_ptr p)
 {
@@ -1374,9 +1264,7 @@
 		err = ugen_set_config(sc, *(int *)addr);
 		switch (err) {
 		case USBD_NORMAL_COMPLETION:
-#if defined(__FreeBSD__)
 			ugen_make_devnodes(sc);
-#endif
 			break;
 		case USBD_IN_USE:
 			return (EBUSY);
@@ -1666,6 +1554,4 @@
 	return (revents);
 }
 
-#if defined(__FreeBSD__)
 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);
-#endif


More information about the freebsd-arch mailing list