Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.157
      1  1.157     skrll /*	$NetBSD: usb.c,v 1.157 2015/05/28 09:06:36 skrll Exp $	*/
      2    1.1  augustss 
      3    1.1  augustss /*
      4  1.129       mrg  * Copyright (c) 1998, 2002, 2008, 2012 The NetBSD Foundation, Inc.
      5    1.1  augustss  * All rights reserved.
      6    1.1  augustss  *
      7    1.5  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8   1.44  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  1.129       mrg  * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna.com.au).
     10    1.1  augustss  *
     11    1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12    1.1  augustss  * modification, are permitted provided that the following conditions
     13    1.1  augustss  * are met:
     14    1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15    1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16    1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17    1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18    1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19    1.1  augustss  *
     20    1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21    1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22    1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23    1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24    1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25    1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26    1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27    1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28    1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29    1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30    1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     31    1.1  augustss  */
     32    1.1  augustss 
     33    1.1  augustss /*
     34    1.8  augustss  * USB specifications and other documentation can be found at
     35   1.80       wiz  * http://www.usb.org/developers/docs/ and
     36   1.80       wiz  * http://www.usb.org/developers/devclass_docs/
     37    1.1  augustss  */
     38   1.55     lukem 
     39   1.55     lukem #include <sys/cdefs.h>
     40  1.157     skrll __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.157 2015/05/28 09:06:36 skrll Exp $");
     41   1.92     pavel 
     42  1.137  christos #ifdef _KERNEL_OPT
     43  1.156     skrll #include "opt_usb.h"
     44   1.92     pavel #include "opt_compat_netbsd.h"
     45  1.137  christos #endif
     46   1.76   dsainty 
     47    1.1  augustss #include <sys/param.h>
     48    1.1  augustss #include <sys/systm.h>
     49    1.1  augustss #include <sys/kernel.h>
     50    1.1  augustss #include <sys/malloc.h>
     51    1.1  augustss #include <sys/device.h>
     52   1.13  augustss #include <sys/kthread.h>
     53   1.30  augustss #include <sys/proc.h>
     54   1.22  augustss #include <sys/conf.h>
     55   1.69  augustss #include <sys/fcntl.h>
     56    1.1  augustss #include <sys/poll.h>
     57    1.1  augustss #include <sys/select.h>
     58   1.26  augustss #include <sys/vnode.h>
     59   1.26  augustss #include <sys/signalvar.h>
     60  1.100        ad #include <sys/intr.h>
     61  1.121  pgoyette #include <sys/module.h>
     62  1.130       mrg #include <sys/mutex.h>
     63  1.130       mrg #include <sys/bus.h>
     64  1.130       mrg #include <sys/once.h>
     65  1.152  riastrad #include <sys/atomic.h>
     66  1.156     skrll #include <sys/sysctl.h>
     67    1.1  augustss 
     68    1.1  augustss #include <dev/usb/usb.h>
     69   1.13  augustss #include <dev/usb/usbdi.h>
     70   1.13  augustss #include <dev/usb/usbdi_util.h>
     71  1.130       mrg #include <dev/usb/usbdivar.h>
     72  1.121  pgoyette #include <dev/usb/usb_verbose.h>
     73  1.130       mrg #include <dev/usb/usb_quirks.h>
     74  1.156     skrll #include <dev/usb/usbhist.h>
     75  1.156     skrll 
     76  1.156     skrll #if defined(USBHIST)
     77  1.156     skrll 
     78  1.156     skrll #ifndef USBHIST_SIZE
     79  1.156     skrll #define USBHIST_SIZE 50000
     80  1.156     skrll #endif
     81  1.156     skrll 
     82  1.157     skrll static struct kern_history_ent usbhistbuf[USBHIST_SIZE];
     83  1.157     skrll USBHIST_DEFINE(usbhist) = KERNHIST_INITIALIZER(usbhist, usbhistbuf);
     84  1.157     skrll 
     85  1.156     skrll #endif
     86    1.1  augustss 
     87   1.26  augustss #define USB_DEV_MINOR 255
     88   1.26  augustss 
     89    1.1  augustss #ifdef USB_DEBUG
     90  1.123    dyoung #define DPRINTF(x)	if (usbdebug) printf x
     91  1.123    dyoung #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
     92    1.1  augustss int	usbdebug = 0;
     93   1.66  augustss /*
     94   1.34  augustss  * 0  - do usual exploration
     95   1.34  augustss  * 1  - do not use timeout exploration
     96   1.34  augustss  * >1 - do no exploration
     97   1.34  augustss  */
     98   1.21  augustss int	usb_noexplore = 0;
     99  1.156     skrll 
    100  1.156     skrll SYSCTL_SETUP(sysctl_hw_usb_setup, "sysctl hw.usb setup")
    101  1.156     skrll {
    102  1.156     skrll 	int err;
    103  1.156     skrll 	const struct sysctlnode *rnode;
    104  1.156     skrll 	const struct sysctlnode *cnode;
    105  1.156     skrll 
    106  1.156     skrll 	err = sysctl_createv(clog, 0, NULL, &rnode,
    107  1.156     skrll 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "usb",
    108  1.156     skrll 	    SYSCTL_DESCR("usb global controls"),
    109  1.156     skrll 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
    110  1.156     skrll 
    111  1.156     skrll 	if (err)
    112  1.156     skrll 		goto fail;
    113  1.156     skrll 
    114  1.156     skrll 	/* control debugging printfs */
    115  1.156     skrll 	err = sysctl_createv(clog, 0, &rnode, &cnode,
    116  1.156     skrll 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    117  1.156     skrll 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    118  1.156     skrll 	    NULL, 0, &usbdebug, sizeof(usbdebug), CTL_CREATE, CTL_EOL);
    119  1.156     skrll 	if (err)
    120  1.156     skrll 		goto fail;
    121  1.156     skrll 
    122  1.156     skrll 	return;
    123  1.156     skrll fail:
    124  1.156     skrll 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    125  1.156     skrll }
    126  1.156     skrll 
    127    1.1  augustss #else
    128    1.1  augustss #define DPRINTF(x)
    129    1.1  augustss #define DPRINTFN(n,x)
    130  1.130       mrg #define	usb_noexplore 0
    131    1.1  augustss #endif
    132    1.1  augustss 
    133    1.1  augustss struct usb_softc {
    134  1.109  drochner #if 0
    135  1.123    dyoung 	device_t	sc_dev;		/* base device */
    136  1.109  drochner #endif
    137    1.1  augustss 	usbd_bus_handle sc_bus;		/* USB controller */
    138    1.1  augustss 	struct usbd_port sc_port;	/* dummy port for root hub */
    139   1.25  augustss 
    140   1.97        ad 	struct lwp	*sc_event_thread;
    141   1.25  augustss 
    142   1.25  augustss 	char		sc_dying;
    143    1.1  augustss };
    144    1.1  augustss 
    145   1.90     joerg struct usb_taskq {
    146   1.90     joerg 	TAILQ_HEAD(, usb_task) tasks;
    147  1.130       mrg 	kmutex_t lock;
    148  1.130       mrg 	kcondvar_t cv;
    149   1.97        ad 	struct lwp *task_thread_lwp;
    150   1.90     joerg 	const char *name;
    151   1.90     joerg };
    152   1.90     joerg 
    153   1.90     joerg static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
    154   1.58  augustss 
    155   1.72   gehenna dev_type_open(usbopen);
    156   1.72   gehenna dev_type_close(usbclose);
    157   1.72   gehenna dev_type_read(usbread);
    158   1.72   gehenna dev_type_ioctl(usbioctl);
    159   1.72   gehenna dev_type_poll(usbpoll);
    160   1.74  jdolecek dev_type_kqfilter(usbkqfilter);
    161   1.72   gehenna 
    162   1.72   gehenna const struct cdevsw usb_cdevsw = {
    163  1.149  dholland 	.d_open = usbopen,
    164  1.149  dholland 	.d_close = usbclose,
    165  1.149  dholland 	.d_read = usbread,
    166  1.149  dholland 	.d_write = nowrite,
    167  1.149  dholland 	.d_ioctl = usbioctl,
    168  1.149  dholland 	.d_stop = nostop,
    169  1.149  dholland 	.d_tty = notty,
    170  1.149  dholland 	.d_poll = usbpoll,
    171  1.149  dholland 	.d_mmap = nommap,
    172  1.149  dholland 	.d_kqfilter = usbkqfilter,
    173  1.154  dholland 	.d_discard = nodiscard,
    174  1.149  dholland 	.d_flag = D_OTHER
    175   1.72   gehenna };
    176    1.7  augustss 
    177  1.109  drochner Static void	usb_discover(struct usb_softc *);
    178  1.109  drochner Static void	usb_create_event_thread(device_t);
    179   1.45  augustss Static void	usb_event_thread(void *);
    180   1.58  augustss Static void	usb_task_thread(void *);
    181    1.1  augustss 
    182   1.41  augustss #define USB_MAX_EVENTS 100
    183   1.26  augustss struct usb_event_q {
    184   1.26  augustss 	struct usb_event ue;
    185   1.26  augustss 	SIMPLEQ_ENTRY(usb_event_q) next;
    186   1.26  augustss };
    187   1.66  augustss Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    188   1.29  augustss 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    189   1.42  augustss Static int usb_nevents = 0;
    190   1.42  augustss Static struct selinfo usb_selevent;
    191  1.130       mrg Static kmutex_t usb_event_lock;
    192  1.130       mrg Static kcondvar_t usb_event_cv;
    193  1.123    dyoung Static proc_t *usb_async_proc;  /* process that wants USB SIGIO */
    194  1.112        ad Static void *usb_async_sih;
    195   1.42  augustss Static int usb_dev_open = 0;
    196   1.87  christos Static struct usb_event *usb_alloc_event(void);
    197   1.87  christos Static void usb_free_event(struct usb_event *);
    198   1.45  augustss Static void usb_add_event(int, struct usb_event *);
    199   1.45  augustss Static int usb_get_next_event(struct usb_event *);
    200  1.112        ad Static void usb_async_intr(void *);
    201  1.130       mrg Static void usb_soft_intr(void *);
    202   1.23  augustss 
    203   1.92     pavel #ifdef COMPAT_30
    204   1.92     pavel Static void usb_copy_old_devinfo(struct usb_device_info_old *, const struct usb_device_info *);
    205   1.92     pavel #endif
    206   1.92     pavel 
    207   1.42  augustss Static const char *usbrev_str[] = USBREV_STR;
    208   1.31  augustss 
    209  1.117    cegger static int usb_match(device_t, cfdata_t, void *);
    210  1.106    dyoung static void usb_attach(device_t, device_t, void *);
    211  1.106    dyoung static int usb_detach(device_t, int);
    212  1.106    dyoung static int usb_activate(device_t, enum devact);
    213  1.106    dyoung static void usb_childdet(device_t, device_t);
    214  1.130       mrg static int usb_once_init(void);
    215  1.110        ad static void usb_doattach(device_t);
    216  1.106    dyoung 
    217  1.106    dyoung extern struct cfdriver usb_cd;
    218  1.106    dyoung 
    219  1.116    dyoung CFATTACH_DECL3_NEW(usb, sizeof(struct usb_softc),
    220  1.116    dyoung     usb_match, usb_attach, usb_detach, usb_activate, NULL, usb_childdet,
    221  1.116    dyoung     DVF_DETACH_SHUTDOWN);
    222    1.1  augustss 
    223  1.134       mrg static const char *taskq_names[] = USB_TASKQ_NAMES;
    224  1.134       mrg 
    225  1.118    dyoung int
    226  1.118    dyoung usb_match(device_t parent, cfdata_t match, void *aux)
    227    1.1  augustss {
    228    1.1  augustss 	DPRINTF(("usbd_match\n"));
    229    1.7  augustss 	return (UMATCH_GENERIC);
    230    1.1  augustss }
    231    1.1  augustss 
    232  1.118    dyoung void
    233  1.118    dyoung usb_attach(device_t parent, device_t self, void *aux)
    234    1.1  augustss {
    235  1.130       mrg 	static ONCE_DECL(init_control);
    236  1.110        ad 	struct usb_softc *sc = device_private(self);
    237  1.110        ad 	int usbrev;
    238  1.110        ad 
    239  1.110        ad 	sc->sc_bus = aux;
    240  1.110        ad 	usbrev = sc->sc_bus->usbrev;
    241  1.110        ad 
    242  1.110        ad 	aprint_naive("\n");
    243  1.110        ad 	aprint_normal(": USB revision %s", usbrev_str[usbrev]);
    244  1.110        ad 	switch (usbrev) {
    245  1.110        ad 	case USBREV_1_0:
    246  1.110        ad 	case USBREV_1_1:
    247  1.110        ad 	case USBREV_2_0:
    248  1.155     skrll 	case USBREV_3_0:
    249  1.110        ad 		break;
    250  1.110        ad 	default:
    251  1.110        ad 		aprint_error(", not supported\n");
    252  1.110        ad 		sc->sc_dying = 1;
    253  1.123    dyoung 		return;
    254  1.110        ad 	}
    255  1.110        ad 	aprint_normal("\n");
    256  1.110        ad 
    257  1.143  jakllsch 	/* XXX we should have our own level */
    258  1.147     skrll 	sc->sc_bus->soft = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
    259  1.143  jakllsch 	    usb_soft_intr, sc->sc_bus);
    260  1.143  jakllsch 	if (sc->sc_bus->soft == NULL) {
    261  1.143  jakllsch 		aprint_error("%s: can't register softintr\n",
    262  1.143  jakllsch 			     device_xname(self));
    263  1.143  jakllsch 		sc->sc_dying = 1;
    264  1.143  jakllsch 		return;
    265  1.143  jakllsch 	}
    266  1.143  jakllsch 
    267  1.147     skrll 	sc->sc_bus->methods->get_lock(sc->sc_bus, &sc->sc_bus->lock);
    268  1.147     skrll 	KASSERT(sc->sc_bus->lock != NULL);
    269  1.134       mrg 
    270  1.130       mrg 	RUN_ONCE(&init_control, usb_once_init);
    271  1.110        ad 	config_interrupts(self, usb_doattach);
    272  1.110        ad }
    273  1.110        ad 
    274  1.130       mrg static int
    275  1.130       mrg usb_once_init(void)
    276  1.130       mrg {
    277  1.134       mrg 	struct usb_taskq *taskq;
    278  1.134       mrg 	int i;
    279  1.130       mrg 
    280  1.130       mrg 	selinit(&usb_selevent);
    281  1.130       mrg 	mutex_init(&usb_event_lock, MUTEX_DEFAULT, IPL_NONE);
    282  1.130       mrg 	cv_init(&usb_event_cv, "usbrea");
    283  1.134       mrg 
    284  1.134       mrg 	for (i = 0; i < USB_NUM_TASKQS; i++) {
    285  1.134       mrg 		taskq = &usb_taskq[i];
    286  1.134       mrg 
    287  1.134       mrg 		TAILQ_INIT(&taskq->tasks);
    288  1.136       mrg 		/*
    289  1.139     skrll 		 * Since USB task methods usb_{add,rem}_task are callable
    290  1.139     skrll 		 * from any context, we have to make this lock a spinlock.
    291  1.136       mrg 		 */
    292  1.136       mrg 		mutex_init(&taskq->lock, MUTEX_DEFAULT, IPL_USB);
    293  1.134       mrg 		cv_init(&taskq->cv, "usbtsk");
    294  1.134       mrg 		taskq->name = taskq_names[i];
    295  1.134       mrg 		if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    296  1.134       mrg 		    usb_task_thread, taskq, &taskq->task_thread_lwp,
    297  1.134       mrg 		    "%s", taskq->name)) {
    298  1.134       mrg 			printf("unable to create task thread: %s\n", taskq->name);
    299  1.134       mrg 			panic("usb_create_event_thread task");
    300  1.134       mrg 		}
    301  1.134       mrg 		/*
    302  1.134       mrg 		 * XXX we should make sure these threads are alive before
    303  1.134       mrg 		 * end up using them in usb_doattach().
    304  1.134       mrg 		 */
    305  1.134       mrg 	}
    306  1.130       mrg 	return 0;
    307  1.130       mrg }
    308  1.130       mrg 
    309  1.110        ad static void
    310  1.110        ad usb_doattach(device_t self)
    311  1.110        ad {
    312  1.109  drochner 	struct usb_softc *sc = device_private(self);
    313    1.1  augustss 	usbd_device_handle dev;
    314   1.29  augustss 	usbd_status err;
    315   1.57  augustss 	int speed;
    316   1.87  christos 	struct usb_event *ue;
    317   1.66  augustss 
    318  1.110        ad 	DPRINTF(("usbd_doattach\n"));
    319   1.31  augustss 
    320  1.109  drochner 	sc->sc_bus->usbctl = self;
    321    1.1  augustss 	sc->sc_port.power = USB_MAX_POWER;
    322   1.31  augustss 
    323  1.110        ad 	switch (sc->sc_bus->usbrev) {
    324   1.57  augustss 	case USBREV_1_0:
    325   1.57  augustss 	case USBREV_1_1:
    326   1.57  augustss 		speed = USB_SPEED_FULL;
    327   1.57  augustss 		break;
    328   1.57  augustss 	case USBREV_2_0:
    329   1.57  augustss 		speed = USB_SPEED_HIGH;
    330   1.57  augustss 		break;
    331  1.155     skrll 	case USBREV_3_0:
    332  1.155     skrll 		speed = USB_SPEED_SUPER;
    333  1.155     skrll 		break;
    334   1.57  augustss 	default:
    335  1.110        ad 		panic("usb_doattach");
    336   1.32  augustss 	}
    337   1.35  augustss 
    338  1.130       mrg 	cv_init(&sc->sc_bus->needs_explore_cv, "usbevt");
    339  1.130       mrg 
    340   1.87  christos 	ue = usb_alloc_event();
    341  1.109  drochner 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
    342   1.87  christos 	usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
    343   1.38  augustss 
    344  1.109  drochner 	err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
    345   1.29  augustss 		  &sc->sc_port);
    346   1.29  augustss 	if (!err) {
    347    1.1  augustss 		dev = sc->sc_port.device;
    348   1.29  augustss 		if (dev->hub == NULL) {
    349   1.22  augustss 			sc->sc_dying = 1;
    350  1.102  jmcneill 			aprint_error("%s: root device is not a hub\n",
    351  1.109  drochner 				     device_xname(self));
    352  1.123    dyoung 			return;
    353    1.1  augustss 		}
    354    1.1  augustss 		sc->sc_bus->root_hub = dev;
    355  1.135       mrg 		usb_create_event_thread(self);
    356   1.24  augustss #if 1
    357   1.66  augustss 		/*
    358   1.24  augustss 		 * Turning this code off will delay attachment of USB devices
    359   1.24  augustss 		 * until the USB event thread is running, which means that
    360   1.24  augustss 		 * the keyboard will not work until after cold boot.
    361   1.24  augustss 		 */
    362  1.109  drochner 		if (cold && (device_cfdata(self)->cf_flags & 1))
    363   1.24  augustss 			dev->hub->explore(sc->sc_bus->root_hub);
    364   1.24  augustss #endif
    365    1.1  augustss 	} else {
    366  1.142  jakllsch 		aprint_error("%s: root hub problem, error=%s\n",
    367  1.142  jakllsch 			     device_xname(self), usbd_errstr(err));
    368   1.22  augustss 		sc->sc_dying = 1;
    369    1.1  augustss 	}
    370    1.7  augustss 
    371  1.145  christos 	config_pending_incr(self);
    372   1.28  augustss 
    373  1.104  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    374  1.104  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    375  1.104  jmcneill 
    376  1.112        ad 	usb_async_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    377  1.112        ad 	   usb_async_intr, NULL);
    378  1.112        ad 
    379  1.123    dyoung 	return;
    380    1.1  augustss }
    381    1.1  augustss 
    382   1.13  augustss void
    383  1.109  drochner usb_create_event_thread(device_t self)
    384   1.13  augustss {
    385  1.109  drochner 	struct usb_softc *sc = device_private(self);
    386   1.13  augustss 
    387  1.148     skrll 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    388  1.130       mrg 	    usb_event_thread, sc, &sc->sc_event_thread,
    389  1.130       mrg 	    "%s", device_xname(self))) {
    390   1.13  augustss 		printf("%s: unable to create event thread for\n",
    391  1.109  drochner 		       device_xname(self));
    392   1.13  augustss 		panic("usb_create_event_thread");
    393   1.13  augustss 	}
    394   1.13  augustss }
    395   1.13  augustss 
    396   1.52  augustss /*
    397   1.62  augustss  * Add a task to be performed by the task thread.  This function can be
    398   1.52  augustss  * called from any context and the task will be executed in a process
    399   1.52  augustss  * context ASAP.
    400   1.52  augustss  */
    401   1.13  augustss void
    402   1.91  christos usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
    403   1.51  augustss {
    404   1.90     joerg 	struct usb_taskq *taskq;
    405   1.51  augustss 
    406  1.150  riastrad 	KASSERT(0 <= queue);
    407  1.150  riastrad 	KASSERT(queue < USB_NUM_TASKQS);
    408   1.90     joerg 	taskq = &usb_taskq[queue];
    409  1.130       mrg 	mutex_enter(&taskq->lock);
    410  1.150  riastrad 	if (atomic_cas_uint(&task->queue, USB_NUM_TASKQS, queue) ==
    411  1.150  riastrad 	    USB_NUM_TASKQS) {
    412   1.58  augustss 		DPRINTFN(2,("usb_add_task: task=%p\n", task));
    413   1.90     joerg 		TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
    414  1.150  riastrad 		cv_signal(&taskq->cv);
    415   1.62  augustss 	} else {
    416   1.58  augustss 		DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
    417   1.62  augustss 	}
    418  1.130       mrg 	mutex_exit(&taskq->lock);
    419   1.51  augustss }
    420   1.51  augustss 
    421  1.150  riastrad /*
    422  1.150  riastrad  * XXX This does not wait for completion!  Most uses need such an
    423  1.150  riastrad  * operation.  Urgh...
    424  1.150  riastrad  */
    425   1.51  augustss void
    426   1.91  christos usb_rem_task(usbd_device_handle dev, struct usb_task *task)
    427   1.53  augustss {
    428  1.150  riastrad 	unsigned queue;
    429   1.53  augustss 
    430  1.150  riastrad 	while ((queue = task->queue) != USB_NUM_TASKQS) {
    431  1.150  riastrad 		struct usb_taskq *taskq = &usb_taskq[queue];
    432  1.133  christos 		mutex_enter(&taskq->lock);
    433  1.150  riastrad 		if (__predict_true(task->queue == queue)) {
    434  1.150  riastrad 			TAILQ_REMOVE(&taskq->tasks, task, next);
    435  1.150  riastrad 			task->queue = USB_NUM_TASKQS;
    436  1.150  riastrad 			mutex_exit(&taskq->lock);
    437  1.150  riastrad 			break;
    438  1.150  riastrad 		}
    439  1.133  christos 		mutex_exit(&taskq->lock);
    440  1.132    cegger 	}
    441   1.53  augustss }
    442   1.53  augustss 
    443   1.53  augustss void
    444   1.45  augustss usb_event_thread(void *arg)
    445   1.13  augustss {
    446   1.13  augustss 	struct usb_softc *sc = arg;
    447   1.13  augustss 
    448   1.27  augustss 	DPRINTF(("usb_event_thread: start\n"));
    449   1.40  augustss 
    450   1.60  augustss 	/*
    451   1.60  augustss 	 * In case this controller is a companion controller to an
    452   1.60  augustss 	 * EHCI controller we need to wait until the EHCI controller
    453   1.60  augustss 	 * has grabbed the port.
    454   1.64  augustss 	 * XXX It would be nicer to do this with a tsleep(), but I don't
    455   1.64  augustss 	 * know how to synchronize the creation of the threads so it
    456   1.64  augustss 	 * will work.
    457   1.60  augustss 	 */
    458   1.61  augustss 	usb_delay_ms(sc->sc_bus, 500);
    459   1.60  augustss 
    460   1.40  augustss 	/* Make sure first discover does something. */
    461  1.147     skrll 	mutex_enter(sc->sc_bus->lock);
    462   1.40  augustss 	sc->sc_bus->needs_explore = 1;
    463   1.51  augustss 	usb_discover(sc);
    464  1.147     skrll 	mutex_exit(sc->sc_bus->lock);
    465  1.145  christos 	config_pending_decr(sc->sc_bus->usbctl);
    466   1.27  augustss 
    467  1.147     skrll 	mutex_enter(sc->sc_bus->lock);
    468   1.22  augustss 	while (!sc->sc_dying) {
    469   1.58  augustss 		if (usb_noexplore < 2)
    470  1.130       mrg 			usb_discover(sc);
    471  1.130       mrg 
    472  1.147     skrll 		cv_timedwait(&sc->sc_bus->needs_explore_cv,
    473  1.147     skrll 		    sc->sc_bus->lock, usb_noexplore ? 0 : hz * 60);
    474  1.147     skrll 
    475   1.58  augustss 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    476   1.13  augustss 	}
    477   1.50  augustss 	sc->sc_event_thread = NULL;
    478   1.13  augustss 
    479   1.13  augustss 	/* In case parent is waiting for us to exit. */
    480  1.147     skrll 	cv_signal(&sc->sc_bus->needs_explore_cv);
    481  1.147     skrll 	mutex_exit(sc->sc_bus->lock);
    482   1.13  augustss 
    483   1.27  augustss 	DPRINTF(("usb_event_thread: exit\n"));
    484   1.13  augustss 	kthread_exit(0);
    485   1.13  augustss }
    486   1.13  augustss 
    487   1.58  augustss void
    488   1.90     joerg usb_task_thread(void *arg)
    489   1.58  augustss {
    490   1.58  augustss 	struct usb_task *task;
    491   1.90     joerg 	struct usb_taskq *taskq;
    492  1.151  riastrad 	bool mpsafe;
    493   1.58  augustss 
    494   1.90     joerg 	taskq = arg;
    495   1.90     joerg 	DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
    496   1.58  augustss 
    497  1.130       mrg 	mutex_enter(&taskq->lock);
    498   1.58  augustss 	for (;;) {
    499   1.90     joerg 		task = TAILQ_FIRST(&taskq->tasks);
    500   1.58  augustss 		if (task == NULL) {
    501  1.130       mrg 			cv_wait(&taskq->cv, &taskq->lock);
    502   1.90     joerg 			task = TAILQ_FIRST(&taskq->tasks);
    503   1.58  augustss 		}
    504   1.58  augustss 		DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
    505   1.58  augustss 		if (task != NULL) {
    506  1.151  riastrad 			mpsafe = ISSET(task->flags, USB_TASKQ_MPSAFE);
    507   1.90     joerg 			TAILQ_REMOVE(&taskq->tasks, task, next);
    508  1.150  riastrad 			task->queue = USB_NUM_TASKQS;
    509  1.130       mrg 			mutex_exit(&taskq->lock);
    510  1.140  jmcneill 
    511  1.151  riastrad 			if (!mpsafe)
    512  1.140  jmcneill 				KERNEL_LOCK(1, curlwp);
    513   1.58  augustss 			task->fun(task->arg);
    514  1.151  riastrad 			/* Can't dereference task after this point.  */
    515  1.151  riastrad 			if (!mpsafe)
    516  1.140  jmcneill 				KERNEL_UNLOCK_ONE(curlwp);
    517  1.140  jmcneill 
    518  1.130       mrg 			mutex_enter(&taskq->lock);
    519   1.58  augustss 		}
    520   1.58  augustss 	}
    521  1.130       mrg 	mutex_exit(&taskq->lock);
    522   1.58  augustss }
    523   1.58  augustss 
    524    1.1  augustss int
    525   1.91  christos usbctlprint(void *aux, const char *pnp)
    526    1.1  augustss {
    527    1.1  augustss 	/* only "usb"es can attach to host controllers */
    528    1.1  augustss 	if (pnp)
    529   1.77   thorpej 		aprint_normal("usb at %s", pnp);
    530    1.1  augustss 
    531    1.1  augustss 	return (UNCONF);
    532    1.1  augustss }
    533    1.7  augustss 
    534    1.1  augustss int
    535   1.91  christos usbopen(dev_t dev, int flag, int mode, struct lwp *l)
    536    1.1  augustss {
    537   1.26  augustss 	int unit = minor(dev);
    538   1.26  augustss 	struct usb_softc *sc;
    539   1.26  augustss 
    540   1.26  augustss 	if (unit == USB_DEV_MINOR) {
    541   1.26  augustss 		if (usb_dev_open)
    542   1.26  augustss 			return (EBUSY);
    543   1.26  augustss 		usb_dev_open = 1;
    544  1.112        ad 		mutex_enter(proc_lock);
    545   1.26  augustss 		usb_async_proc = 0;
    546  1.112        ad 		mutex_exit(proc_lock);
    547   1.26  augustss 		return (0);
    548   1.26  augustss 	}
    549   1.26  augustss 
    550  1.109  drochner 	sc = device_lookup_private(&usb_cd, unit);
    551  1.111  drochner 	if (!sc)
    552  1.111  drochner 		return (ENXIO);
    553    1.1  augustss 
    554   1.22  augustss 	if (sc->sc_dying)
    555   1.22  augustss 		return (EIO);
    556    1.1  augustss 
    557    1.1  augustss 	return (0);
    558    1.1  augustss }
    559    1.1  augustss 
    560    1.1  augustss int
    561   1.45  augustss usbread(dev_t dev, struct uio *uio, int flag)
    562   1.26  augustss {
    563   1.92     pavel 	struct usb_event *ue;
    564   1.92     pavel #ifdef COMPAT_30
    565   1.93  christos 	struct usb_event_old *ueo = NULL;	/* XXXGCC */
    566  1.146  christos 	int useold = 0;
    567   1.92     pavel #endif
    568  1.146  christos 	int error, n;
    569   1.26  augustss 
    570   1.26  augustss 	if (minor(dev) != USB_DEV_MINOR)
    571   1.26  augustss 		return (ENXIO);
    572   1.26  augustss 
    573   1.92     pavel 	switch (uio->uio_resid) {
    574   1.92     pavel #ifdef COMPAT_30
    575   1.92     pavel 	case sizeof(struct usb_event_old):
    576   1.92     pavel 		ueo = malloc(sizeof(struct usb_event_old), M_USBDEV,
    577   1.92     pavel 			     M_WAITOK|M_ZERO);
    578   1.92     pavel 		useold = 1;
    579   1.92     pavel 		/* FALLTHRU */
    580   1.92     pavel #endif
    581   1.92     pavel 	case sizeof(struct usb_event):
    582   1.92     pavel 		ue = usb_alloc_event();
    583   1.92     pavel 		break;
    584   1.92     pavel 	default:
    585   1.26  augustss 		return (EINVAL);
    586   1.92     pavel 	}
    587   1.26  augustss 
    588   1.26  augustss 	error = 0;
    589  1.130       mrg 	mutex_enter(&usb_event_lock);
    590   1.26  augustss 	for (;;) {
    591   1.87  christos 		n = usb_get_next_event(ue);
    592   1.26  augustss 		if (n != 0)
    593   1.26  augustss 			break;
    594   1.26  augustss 		if (flag & IO_NDELAY) {
    595   1.26  augustss 			error = EWOULDBLOCK;
    596   1.26  augustss 			break;
    597   1.26  augustss 		}
    598  1.130       mrg 		error = cv_wait_sig(&usb_event_cv, &usb_event_lock);
    599   1.26  augustss 		if (error)
    600   1.26  augustss 			break;
    601   1.26  augustss 	}
    602  1.130       mrg 	mutex_exit(&usb_event_lock);
    603   1.92     pavel 	if (!error) {
    604   1.92     pavel #ifdef COMPAT_30
    605   1.92     pavel 		if (useold) { /* copy fields to old struct */
    606   1.92     pavel 			ueo->ue_type = ue->ue_type;
    607   1.92     pavel 			memcpy(&ueo->ue_time, &ue->ue_time,
    608   1.92     pavel 			      sizeof(struct timespec));
    609   1.92     pavel 			switch (ue->ue_type) {
    610   1.92     pavel 				case USB_EVENT_DEVICE_ATTACH:
    611   1.92     pavel 				case USB_EVENT_DEVICE_DETACH:
    612   1.92     pavel 					usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
    613   1.92     pavel 					break;
    614   1.92     pavel 
    615   1.92     pavel 				case USB_EVENT_CTRLR_ATTACH:
    616   1.92     pavel 				case USB_EVENT_CTRLR_DETACH:
    617   1.92     pavel 					ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
    618   1.92     pavel 					break;
    619   1.92     pavel 
    620   1.92     pavel 				case USB_EVENT_DRIVER_ATTACH:
    621   1.92     pavel 				case USB_EVENT_DRIVER_DETACH:
    622   1.92     pavel 					ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
    623   1.92     pavel 					memcpy(ueo->u.ue_driver.ue_devname,
    624  1.137  christos 					       ue->u.ue_driver.ue_devname,
    625   1.92     pavel 					       sizeof(ue->u.ue_driver.ue_devname));
    626   1.92     pavel 					break;
    627   1.92     pavel 				default:
    628   1.92     pavel 					;
    629   1.92     pavel 			}
    630   1.92     pavel 
    631  1.108        ws 			error = uiomove((void *)ueo, sizeof *ueo, uio);
    632   1.92     pavel 		} else
    633   1.92     pavel #endif
    634  1.108        ws 			error = uiomove((void *)ue, sizeof *ue, uio);
    635   1.92     pavel 	}
    636   1.87  christos 	usb_free_event(ue);
    637   1.92     pavel #ifdef COMPAT_30
    638   1.92     pavel 	if (useold)
    639   1.92     pavel 		free(ueo, M_USBDEV);
    640   1.92     pavel #endif
    641   1.26  augustss 
    642   1.26  augustss 	return (error);
    643   1.26  augustss }
    644   1.26  augustss 
    645   1.26  augustss int
    646   1.91  christos usbclose(dev_t dev, int flag, int mode,
    647   1.91  christos     struct lwp *l)
    648    1.1  augustss {
    649   1.26  augustss 	int unit = minor(dev);
    650   1.26  augustss 
    651   1.26  augustss 	if (unit == USB_DEV_MINOR) {
    652  1.112        ad 		mutex_enter(proc_lock);
    653   1.26  augustss 		usb_async_proc = 0;
    654  1.112        ad 		mutex_exit(proc_lock);
    655   1.26  augustss 		usb_dev_open = 0;
    656   1.26  augustss 	}
    657   1.26  augustss 
    658    1.1  augustss 	return (0);
    659    1.1  augustss }
    660    1.1  augustss 
    661    1.1  augustss int
    662   1.96  christos usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
    663    1.1  augustss {
    664   1.26  augustss 	struct usb_softc *sc;
    665   1.28  augustss 	int unit = minor(devt);
    666   1.26  augustss 
    667   1.26  augustss 	if (unit == USB_DEV_MINOR) {
    668   1.26  augustss 		switch (cmd) {
    669   1.26  augustss 		case FIONBIO:
    670   1.26  augustss 			/* All handled in the upper FS layer. */
    671   1.26  augustss 			return (0);
    672   1.66  augustss 
    673   1.26  augustss 		case FIOASYNC:
    674  1.112        ad 			mutex_enter(proc_lock);
    675   1.26  augustss 			if (*(int *)data)
    676   1.84  christos 				usb_async_proc = l->l_proc;
    677   1.26  augustss 			else
    678   1.26  augustss 				usb_async_proc = 0;
    679  1.112        ad 			mutex_exit(proc_lock);
    680   1.26  augustss 			return (0);
    681   1.26  augustss 
    682   1.26  augustss 		default:
    683   1.26  augustss 			return (EINVAL);
    684   1.26  augustss 		}
    685   1.26  augustss 	}
    686   1.26  augustss 
    687  1.109  drochner 	sc = device_lookup_private(&usb_cd, unit);
    688    1.1  augustss 
    689   1.22  augustss 	if (sc->sc_dying)
    690   1.22  augustss 		return (EIO);
    691   1.22  augustss 
    692    1.1  augustss 	switch (cmd) {
    693    1.1  augustss #ifdef USB_DEBUG
    694    1.1  augustss 	case USB_SETDEBUG:
    695   1.69  augustss 		if (!(flag & FWRITE))
    696   1.69  augustss 			return (EBADF);
    697   1.30  augustss 		usbdebug  = ((*(int *)data) & 0x000000ff);
    698    1.1  augustss 		break;
    699   1.56  augustss #endif /* USB_DEBUG */
    700    1.1  augustss 	case USB_REQUEST:
    701    1.1  augustss 	{
    702    1.1  augustss 		struct usb_ctl_request *ur = (void *)data;
    703   1.68  christos 		int len = UGETW(ur->ucr_request.wLength);
    704    1.1  augustss 		struct iovec iov;
    705    1.1  augustss 		struct uio uio;
    706    1.1  augustss 		void *ptr = 0;
    707   1.68  christos 		int addr = ur->ucr_addr;
    708   1.29  augustss 		usbd_status err;
    709    1.1  augustss 		int error = 0;
    710   1.69  augustss 
    711   1.69  augustss 		if (!(flag & FWRITE))
    712   1.69  augustss 			return (EBADF);
    713    1.1  augustss 
    714    1.9  augustss 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    715    1.1  augustss 		if (len < 0 || len > 32768)
    716    1.9  augustss 			return (EINVAL);
    717   1.66  augustss 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    718  1.141  jakllsch 		    sc->sc_bus->devices[addr] == NULL)
    719    1.9  augustss 			return (EINVAL);
    720    1.1  augustss 		if (len != 0) {
    721   1.96  christos 			iov.iov_base = (void *)ur->ucr_data;
    722    1.1  augustss 			iov.iov_len = len;
    723    1.1  augustss 			uio.uio_iov = &iov;
    724    1.1  augustss 			uio.uio_iovcnt = 1;
    725    1.1  augustss 			uio.uio_resid = len;
    726    1.1  augustss 			uio.uio_offset = 0;
    727    1.1  augustss 			uio.uio_rw =
    728   1.68  christos 				ur->ucr_request.bmRequestType & UT_READ ?
    729    1.1  augustss 				UIO_READ : UIO_WRITE;
    730   1.85      yamt 			uio.uio_vmspace = l->l_proc->p_vmspace;
    731    1.1  augustss 			ptr = malloc(len, M_TEMP, M_WAITOK);
    732    1.1  augustss 			if (uio.uio_rw == UIO_WRITE) {
    733    1.1  augustss 				error = uiomove(ptr, len, &uio);
    734    1.1  augustss 				if (error)
    735    1.1  augustss 					goto ret;
    736    1.1  augustss 			}
    737    1.1  augustss 		}
    738   1.29  augustss 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
    739   1.68  christos 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
    740   1.67  augustss 			  USBD_DEFAULT_TIMEOUT);
    741   1.29  augustss 		if (err) {
    742    1.1  augustss 			error = EIO;
    743    1.1  augustss 			goto ret;
    744    1.1  augustss 		}
    745  1.108        ws 		if (len > ur->ucr_actlen)
    746  1.108        ws 			len = ur->ucr_actlen;
    747    1.1  augustss 		if (len != 0) {
    748    1.1  augustss 			if (uio.uio_rw == UIO_READ) {
    749    1.1  augustss 				error = uiomove(ptr, len, &uio);
    750    1.1  augustss 				if (error)
    751    1.1  augustss 					goto ret;
    752    1.1  augustss 			}
    753    1.1  augustss 		}
    754    1.1  augustss 	ret:
    755    1.1  augustss 		if (ptr)
    756    1.1  augustss 			free(ptr, M_TEMP);
    757    1.1  augustss 		return (error);
    758    1.1  augustss 	}
    759    1.1  augustss 
    760    1.1  augustss 	case USB_DEVICEINFO:
    761   1.93  christos 	{
    762   1.93  christos 		usbd_device_handle dev;
    763   1.93  christos 		struct usb_device_info *di = (void *)data;
    764   1.93  christos 		int addr = di->udi_addr;
    765   1.93  christos 
    766  1.144  jakllsch 		if (addr < 0 || addr >= USB_MAX_DEVICES)
    767   1.93  christos 			return EINVAL;
    768   1.93  christos 		if ((dev = sc->sc_bus->devices[addr]) == NULL)
    769   1.93  christos 			return ENXIO;
    770   1.93  christos 		usbd_fill_deviceinfo(dev, di, 1);
    771   1.93  christos 		break;
    772   1.93  christos 	}
    773   1.93  christos 
    774   1.92     pavel #ifdef COMPAT_30
    775   1.92     pavel 	case USB_DEVICEINFO_OLD:
    776    1.1  augustss 	{
    777   1.30  augustss 		usbd_device_handle dev;
    778   1.93  christos 		struct usb_device_info_old *di = (void *)data;
    779   1.93  christos 		int addr = di->udi_addr;
    780    1.1  augustss 
    781    1.1  augustss 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    782   1.93  christos 			return EINVAL;
    783   1.93  christos 		if ((dev = sc->sc_bus->devices[addr]) == NULL)
    784   1.93  christos 			return ENXIO;
    785   1.93  christos 		usbd_fill_deviceinfo_old(dev, di, 1);
    786    1.1  augustss 		break;
    787    1.1  augustss 	}
    788   1.93  christos #endif
    789    1.2  augustss 
    790    1.2  augustss 	case USB_DEVICESTATS:
    791    1.2  augustss 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    792    1.2  augustss 		break;
    793    1.1  augustss 
    794    1.1  augustss 	default:
    795   1.26  augustss 		return (EINVAL);
    796    1.1  augustss 	}
    797    1.1  augustss 	return (0);
    798    1.1  augustss }
    799    1.1  augustss 
    800    1.1  augustss int
    801   1.84  christos usbpoll(dev_t dev, int events, struct lwp *l)
    802    1.1  augustss {
    803  1.130       mrg 	int revents, mask;
    804    1.1  augustss 
    805   1.30  augustss 	if (minor(dev) == USB_DEV_MINOR) {
    806   1.30  augustss 		revents = 0;
    807   1.30  augustss 		mask = POLLIN | POLLRDNORM;
    808   1.66  augustss 
    809  1.130       mrg 		mutex_enter(&usb_event_lock);
    810   1.30  augustss 		if (events & mask && usb_nevents > 0)
    811   1.30  augustss 			revents |= events & mask;
    812   1.30  augustss 		if (revents == 0 && events & mask)
    813   1.84  christos 			selrecord(l, &usb_selevent);
    814  1.130       mrg 		mutex_exit(&usb_event_lock);
    815   1.66  augustss 
    816   1.30  augustss 		return (revents);
    817   1.30  augustss 	} else {
    818   1.82        ws 		return (0);
    819    1.1  augustss 	}
    820    1.1  augustss }
    821    1.1  augustss 
    822   1.74  jdolecek static void
    823   1.74  jdolecek filt_usbrdetach(struct knote *kn)
    824   1.74  jdolecek {
    825   1.74  jdolecek 
    826  1.130       mrg 	mutex_enter(&usb_event_lock);
    827   1.75  christos 	SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
    828  1.130       mrg 	mutex_exit(&usb_event_lock);
    829   1.74  jdolecek }
    830   1.74  jdolecek 
    831   1.74  jdolecek static int
    832   1.91  christos filt_usbread(struct knote *kn, long hint)
    833   1.74  jdolecek {
    834   1.74  jdolecek 
    835   1.74  jdolecek 	if (usb_nevents == 0)
    836   1.74  jdolecek 		return (0);
    837   1.74  jdolecek 
    838   1.74  jdolecek 	kn->kn_data = sizeof(struct usb_event);
    839   1.74  jdolecek 	return (1);
    840   1.74  jdolecek }
    841   1.74  jdolecek 
    842   1.74  jdolecek static const struct filterops usbread_filtops =
    843   1.74  jdolecek 	{ 1, NULL, filt_usbrdetach, filt_usbread };
    844   1.74  jdolecek 
    845   1.74  jdolecek int
    846   1.74  jdolecek usbkqfilter(dev_t dev, struct knote *kn)
    847   1.74  jdolecek {
    848   1.74  jdolecek 	struct klist *klist;
    849   1.74  jdolecek 
    850   1.74  jdolecek 	switch (kn->kn_filter) {
    851   1.74  jdolecek 	case EVFILT_READ:
    852   1.74  jdolecek 		if (minor(dev) != USB_DEV_MINOR)
    853   1.74  jdolecek 			return (1);
    854   1.75  christos 		klist = &usb_selevent.sel_klist;
    855   1.74  jdolecek 		kn->kn_fop = &usbread_filtops;
    856   1.74  jdolecek 		break;
    857   1.74  jdolecek 
    858   1.74  jdolecek 	default:
    859  1.103     pooka 		return (EINVAL);
    860   1.74  jdolecek 	}
    861   1.74  jdolecek 
    862   1.74  jdolecek 	kn->kn_hook = NULL;
    863   1.74  jdolecek 
    864  1.130       mrg 	mutex_enter(&usb_event_lock);
    865   1.74  jdolecek 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    866  1.130       mrg 	mutex_exit(&usb_event_lock);
    867   1.74  jdolecek 
    868   1.74  jdolecek 	return (0);
    869   1.74  jdolecek }
    870   1.74  jdolecek 
    871   1.25  augustss /* Explore device tree from the root. */
    872   1.51  augustss Static void
    873  1.109  drochner usb_discover(struct usb_softc *sc)
    874    1.1  augustss {
    875   1.51  augustss 
    876  1.147     skrll 	KASSERT(mutex_owned(sc->sc_bus->lock));
    877  1.130       mrg 
    878   1.51  augustss 	DPRINTFN(2,("usb_discover\n"));
    879   1.51  augustss 	if (usb_noexplore > 1)
    880   1.51  augustss 		return;
    881   1.66  augustss 	/*
    882   1.25  augustss 	 * We need mutual exclusion while traversing the device tree,
    883   1.25  augustss 	 * but this is guaranteed since this function is only called
    884   1.25  augustss 	 * from the event thread for the controller.
    885  1.130       mrg 	 *
    886  1.147     skrll 	 * Also, we now have sc_bus->lock held.
    887   1.25  augustss 	 */
    888   1.30  augustss 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
    889   1.13  augustss 		sc->sc_bus->needs_explore = 0;
    890  1.147     skrll 		mutex_exit(sc->sc_bus->lock);
    891   1.13  augustss 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    892  1.147     skrll 		mutex_enter(sc->sc_bus->lock);
    893   1.30  augustss 	}
    894    1.1  augustss }
    895    1.1  augustss 
    896    1.1  augustss void
    897   1.51  augustss usb_needs_explore(usbd_device_handle dev)
    898    1.1  augustss {
    899   1.51  augustss 	DPRINTFN(2,("usb_needs_explore\n"));
    900  1.147     skrll 	mutex_enter(dev->bus->lock);
    901   1.51  augustss 	dev->bus->needs_explore = 1;
    902  1.147     skrll 	cv_signal(&dev->bus->needs_explore_cv);
    903  1.147     skrll 	mutex_exit(dev->bus->lock);
    904    1.1  augustss }
    905    1.7  augustss 
    906   1.81      joff void
    907   1.81      joff usb_needs_reattach(usbd_device_handle dev)
    908   1.81      joff {
    909   1.81      joff 	DPRINTFN(2,("usb_needs_reattach\n"));
    910  1.147     skrll 	mutex_enter(dev->bus->lock);
    911   1.81      joff 	dev->powersrc->reattach = 1;
    912   1.81      joff 	dev->bus->needs_explore = 1;
    913  1.147     skrll 	cv_signal(&dev->bus->needs_explore_cv);
    914  1.147     skrll 	mutex_exit(dev->bus->lock);
    915   1.81      joff }
    916   1.81      joff 
    917  1.130       mrg /* Called at with usb_event_lock held. */
    918   1.26  augustss int
    919   1.45  augustss usb_get_next_event(struct usb_event *ue)
    920   1.26  augustss {
    921   1.26  augustss 	struct usb_event_q *ueq;
    922   1.26  augustss 
    923  1.130       mrg 	KASSERT(mutex_owned(&usb_event_lock));
    924  1.130       mrg 
    925   1.26  augustss 	if (usb_nevents <= 0)
    926   1.26  augustss 		return (0);
    927   1.26  augustss 	ueq = SIMPLEQ_FIRST(&usb_events);
    928   1.65  augustss #ifdef DIAGNOSTIC
    929   1.65  augustss 	if (ueq == NULL) {
    930   1.65  augustss 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
    931   1.65  augustss 		usb_nevents = 0;
    932   1.65  augustss 		return (0);
    933   1.65  augustss 	}
    934   1.65  augustss #endif
    935   1.83  drochner 	if (ue)
    936   1.83  drochner 		*ue = ueq->ue;
    937   1.71     lukem 	SIMPLEQ_REMOVE_HEAD(&usb_events, next);
    938   1.87  christos 	usb_free_event((struct usb_event *)(void *)ueq);
    939   1.26  augustss 	usb_nevents--;
    940   1.26  augustss 	return (1);
    941   1.26  augustss }
    942   1.26  augustss 
    943   1.26  augustss void
    944   1.45  augustss usbd_add_dev_event(int type, usbd_device_handle udev)
    945   1.38  augustss {
    946   1.87  christos 	struct usb_event *ue = usb_alloc_event();
    947   1.38  augustss 
    948   1.87  christos 	usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
    949   1.87  christos 	usb_add_event(type, ue);
    950   1.38  augustss }
    951   1.38  augustss 
    952   1.38  augustss void
    953  1.118    dyoung usbd_add_drv_event(int type, usbd_device_handle udev, device_t dev)
    954   1.38  augustss {
    955   1.87  christos 	struct usb_event *ue = usb_alloc_event();
    956   1.87  christos 
    957   1.87  christos 	ue->u.ue_driver.ue_cookie = udev->cookie;
    958  1.123    dyoung 	strncpy(ue->u.ue_driver.ue_devname, device_xname(dev),
    959   1.87  christos 	    sizeof ue->u.ue_driver.ue_devname);
    960   1.87  christos 	usb_add_event(type, ue);
    961   1.87  christos }
    962   1.87  christos 
    963   1.87  christos Static struct usb_event *
    964   1.87  christos usb_alloc_event(void)
    965   1.87  christos {
    966   1.87  christos 	/* Yes, this is right; we allocate enough so that we can use it later */
    967   1.87  christos 	return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO);
    968   1.87  christos }
    969   1.38  augustss 
    970   1.87  christos Static void
    971   1.87  christos usb_free_event(struct usb_event *uep)
    972   1.87  christos {
    973   1.87  christos 	free(uep, M_USBDEV);
    974   1.38  augustss }
    975   1.38  augustss 
    976   1.42  augustss Static void
    977   1.45  augustss usb_add_event(int type, struct usb_event *uep)
    978   1.26  augustss {
    979   1.26  augustss 	struct usb_event_q *ueq;
    980   1.26  augustss 	struct timeval thetime;
    981   1.26  augustss 
    982   1.38  augustss 	microtime(&thetime);
    983  1.130       mrg 	/* Don't want to wait here with usb_event_lock held */
    984   1.87  christos 	ueq = (struct usb_event_q *)(void *)uep;
    985   1.38  augustss 	ueq->ue = *uep;
    986   1.38  augustss 	ueq->ue.ue_type = type;
    987   1.38  augustss 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
    988   1.38  augustss 
    989  1.130       mrg 	mutex_enter(&usb_event_lock);
    990   1.26  augustss 	if (++usb_nevents >= USB_MAX_EVENTS) {
    991   1.26  augustss 		/* Too many queued events, drop an old one. */
    992   1.26  augustss 		DPRINTFN(-1,("usb: event dropped\n"));
    993   1.83  drochner 		(void)usb_get_next_event(0);
    994   1.26  augustss 	}
    995   1.26  augustss 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
    996  1.130       mrg 	cv_signal(&usb_event_cv);
    997  1.107     rmind 	selnotify(&usb_selevent, 0, 0);
    998   1.94        ad 	if (usb_async_proc != NULL) {
    999  1.130       mrg 		kpreempt_disable();
   1000  1.112        ad 		softint_schedule(usb_async_sih);
   1001  1.130       mrg 		kpreempt_enable();
   1002   1.94        ad 	}
   1003  1.130       mrg 	mutex_exit(&usb_event_lock);
   1004   1.39  augustss }
   1005   1.60  augustss 
   1006  1.112        ad Static void
   1007  1.112        ad usb_async_intr(void *cookie)
   1008  1.112        ad {
   1009  1.112        ad 	proc_t *proc;
   1010  1.112        ad 
   1011  1.112        ad 	mutex_enter(proc_lock);
   1012  1.112        ad 	if ((proc = usb_async_proc) != NULL)
   1013  1.112        ad 		psignal(proc, SIGIO);
   1014  1.112        ad 	mutex_exit(proc_lock);
   1015  1.112        ad }
   1016  1.112        ad 
   1017  1.130       mrg Static void
   1018  1.130       mrg usb_soft_intr(void *arg)
   1019  1.130       mrg {
   1020  1.130       mrg 	usbd_bus_handle bus = arg;
   1021  1.130       mrg 
   1022  1.147     skrll 	mutex_enter(bus->lock);
   1023  1.130       mrg 	(*bus->methods->soft_intr)(bus);
   1024  1.147     skrll 	mutex_exit(bus->lock);
   1025  1.130       mrg }
   1026  1.130       mrg 
   1027   1.39  augustss void
   1028   1.49  augustss usb_schedsoftintr(usbd_bus_handle bus)
   1029   1.39  augustss {
   1030  1.130       mrg 
   1031   1.54  augustss 	DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
   1032  1.130       mrg 
   1033   1.49  augustss 	if (bus->use_polling) {
   1034   1.49  augustss 		bus->methods->soft_intr(bus);
   1035   1.49  augustss 	} else {
   1036  1.130       mrg 		kpreempt_disable();
   1037  1.100        ad 		softint_schedule(bus->soft);
   1038  1.130       mrg 		kpreempt_enable();
   1039   1.49  augustss 	}
   1040   1.26  augustss }
   1041   1.26  augustss 
   1042    1.7  augustss int
   1043  1.106    dyoung usb_activate(device_t self, enum devact act)
   1044    1.7  augustss {
   1045  1.106    dyoung 	struct usb_softc *sc = device_private(self);
   1046   1.22  augustss 
   1047   1.22  augustss 	switch (act) {
   1048   1.22  augustss 	case DVACT_DEACTIVATE:
   1049   1.22  augustss 		sc->sc_dying = 1;
   1050  1.119    dyoung 		return 0;
   1051  1.119    dyoung 	default:
   1052  1.119    dyoung 		return EOPNOTSUPP;
   1053   1.22  augustss 	}
   1054   1.13  augustss }
   1055    1.7  augustss 
   1056  1.106    dyoung void
   1057  1.106    dyoung usb_childdet(device_t self, device_t child)
   1058  1.106    dyoung {
   1059  1.114  drochner 	int i;
   1060  1.106    dyoung 	struct usb_softc *sc = device_private(self);
   1061  1.106    dyoung 	struct usbd_device *dev;
   1062  1.106    dyoung 
   1063  1.114  drochner 	if ((dev = sc->sc_port.device) == NULL || dev->subdevlen == 0)
   1064  1.106    dyoung 		return;
   1065  1.106    dyoung 
   1066  1.114  drochner 	for (i = 0; i < dev->subdevlen; i++)
   1067  1.114  drochner 		if (dev->subdevs[i] == child)
   1068  1.114  drochner 			dev->subdevs[i] = NULL;
   1069  1.106    dyoung }
   1070  1.106    dyoung 
   1071   1.13  augustss int
   1072  1.106    dyoung usb_detach(device_t self, int flags)
   1073   1.13  augustss {
   1074  1.106    dyoung 	struct usb_softc *sc = device_private(self);
   1075   1.87  christos 	struct usb_event *ue;
   1076  1.119    dyoung 	int rc;
   1077   1.22  augustss 
   1078   1.27  augustss 	DPRINTF(("usb_detach: start\n"));
   1079   1.27  augustss 
   1080  1.119    dyoung 	/* Make all devices disconnect. */
   1081  1.119    dyoung 	if (sc->sc_port.device != NULL &&
   1082  1.119    dyoung 	    (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
   1083  1.119    dyoung 		return rc;
   1084  1.119    dyoung 
   1085  1.105       smb 	pmf_device_deregister(self);
   1086   1.99  kiyohara 	/* Kill off event thread. */
   1087  1.119    dyoung 	sc->sc_dying = 1;
   1088   1.99  kiyohara 	while (sc->sc_event_thread != NULL) {
   1089  1.147     skrll 		mutex_enter(sc->sc_bus->lock);
   1090  1.147     skrll 		cv_signal(&sc->sc_bus->needs_explore_cv);
   1091  1.147     skrll 		cv_timedwait(&sc->sc_bus->needs_explore_cv,
   1092  1.147     skrll 		    sc->sc_bus->lock, hz * 60);
   1093  1.147     skrll 		mutex_exit(sc->sc_bus->lock);
   1094   1.99  kiyohara 	}
   1095   1.99  kiyohara 	DPRINTF(("usb_detach: event thread dead\n"));
   1096   1.22  augustss 
   1097   1.49  augustss 	if (sc->sc_bus->soft != NULL) {
   1098  1.100        ad 		softint_disestablish(sc->sc_bus->soft);
   1099   1.49  augustss 		sc->sc_bus->soft = NULL;
   1100   1.49  augustss 	}
   1101   1.38  augustss 
   1102   1.87  christos 	ue = usb_alloc_event();
   1103  1.109  drochner 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
   1104   1.87  christos 	usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
   1105   1.38  augustss 
   1106  1.130       mrg 	cv_destroy(&sc->sc_bus->needs_explore_cv);
   1107  1.130       mrg 
   1108    1.7  augustss 	return (0);
   1109    1.7  augustss }
   1110   1.92     pavel 
   1111   1.92     pavel #ifdef COMPAT_30
   1112   1.92     pavel Static void
   1113   1.92     pavel usb_copy_old_devinfo(struct usb_device_info_old *uo,
   1114   1.92     pavel 		     const struct usb_device_info *ue)
   1115   1.92     pavel {
   1116   1.92     pavel 	const unsigned char *p;
   1117   1.92     pavel 	unsigned char *q;
   1118   1.92     pavel 	int i, n;
   1119   1.92     pavel 
   1120   1.92     pavel 	uo->udi_bus = ue->udi_bus;
   1121  1.137  christos 	uo->udi_addr = ue->udi_addr;
   1122   1.92     pavel 	uo->udi_cookie = ue->udi_cookie;
   1123   1.92     pavel 	for (i = 0, p = (const unsigned char *)ue->udi_product,
   1124   1.92     pavel 	     q = (unsigned char *)uo->udi_product;
   1125   1.92     pavel 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
   1126   1.92     pavel 		if (*p < 0x80)
   1127   1.92     pavel 			q[i++] = *p;
   1128   1.92     pavel 		else {
   1129   1.92     pavel 			q[i++] = '?';
   1130   1.92     pavel 			if ((*p & 0xe0) == 0xe0)
   1131   1.92     pavel 				p++;
   1132   1.92     pavel 			p++;
   1133   1.92     pavel 		}
   1134   1.92     pavel 	}
   1135   1.92     pavel 	q[i] = 0;
   1136   1.92     pavel 
   1137   1.92     pavel 	for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
   1138   1.92     pavel 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
   1139   1.92     pavel 		if (* p < 0x80)
   1140   1.92     pavel 			q[i++] = *p;
   1141   1.92     pavel 		else {
   1142   1.92     pavel 			q[i++] = '?';
   1143   1.92     pavel 			p++;
   1144   1.92     pavel 			if ((*p & 0xe0) == 0xe0)
   1145   1.92     pavel 				p++;
   1146   1.92     pavel 		}
   1147   1.92     pavel 	}
   1148   1.92     pavel 	q[i] = 0;
   1149   1.92     pavel 
   1150   1.92     pavel 	memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
   1151   1.92     pavel 
   1152   1.92     pavel 	uo->udi_productNo = ue->udi_productNo;
   1153   1.92     pavel 	uo->udi_vendorNo = ue->udi_vendorNo;
   1154   1.92     pavel 	uo->udi_releaseNo = ue->udi_releaseNo;
   1155   1.92     pavel 	uo->udi_class = ue->udi_class;
   1156   1.92     pavel 	uo->udi_subclass = ue->udi_subclass;
   1157   1.92     pavel 	uo->udi_protocol = ue->udi_protocol;
   1158   1.92     pavel 	uo->udi_config = ue->udi_config;
   1159   1.92     pavel 	uo->udi_speed = ue->udi_speed;
   1160  1.137  christos 	uo->udi_power = ue->udi_power;
   1161   1.92     pavel 	uo->udi_nports = ue->udi_nports;
   1162   1.92     pavel 
   1163   1.92     pavel 	for (n=0; n<USB_MAX_DEVNAMES; n++)
   1164   1.92     pavel 		memcpy(uo->udi_devnames[n],
   1165   1.92     pavel 		       ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
   1166   1.92     pavel 	memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
   1167   1.92     pavel }
   1168   1.92     pavel #endif
   1169