Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.196
      1  1.196  riastrad /*	$NetBSD: usb.c,v 1.196 2021/06/13 14:48:10 riastradh 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.196  riastrad __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.196 2021/06/13 14:48:10 riastradh Exp $");
     41   1.92     pavel 
     42  1.137  christos #ifdef _KERNEL_OPT
     43  1.156     skrll #include "opt_usb.h"
     44  1.189       mrg #include "opt_ddb.h"
     45   1.92     pavel #include "opt_compat_netbsd.h"
     46  1.137  christos #endif
     47   1.76   dsainty 
     48    1.1  augustss #include <sys/param.h>
     49    1.1  augustss #include <sys/systm.h>
     50    1.1  augustss #include <sys/kernel.h>
     51  1.162     skrll #include <sys/kmem.h>
     52    1.1  augustss #include <sys/device.h>
     53   1.13  augustss #include <sys/kthread.h>
     54   1.30  augustss #include <sys/proc.h>
     55   1.22  augustss #include <sys/conf.h>
     56   1.69  augustss #include <sys/fcntl.h>
     57    1.1  augustss #include <sys/poll.h>
     58    1.1  augustss #include <sys/select.h>
     59   1.26  augustss #include <sys/vnode.h>
     60   1.26  augustss #include <sys/signalvar.h>
     61  1.100        ad #include <sys/intr.h>
     62  1.121  pgoyette #include <sys/module.h>
     63  1.130       mrg #include <sys/mutex.h>
     64  1.130       mrg #include <sys/bus.h>
     65  1.130       mrg #include <sys/once.h>
     66  1.152  riastrad #include <sys/atomic.h>
     67  1.156     skrll #include <sys/sysctl.h>
     68  1.175  pgoyette #include <sys/compat_stub.h>
     69  1.183  riastrad #include <sys/sdt.h>
     70    1.1  augustss 
     71    1.1  augustss #include <dev/usb/usb.h>
     72   1.13  augustss #include <dev/usb/usbdi.h>
     73   1.13  augustss #include <dev/usb/usbdi_util.h>
     74  1.130       mrg #include <dev/usb/usbdivar.h>
     75  1.121  pgoyette #include <dev/usb/usb_verbose.h>
     76  1.130       mrg #include <dev/usb/usb_quirks.h>
     77  1.156     skrll #include <dev/usb/usbhist.h>
     78  1.183  riastrad #include <dev/usb/usb_sdt.h>
     79  1.156     skrll 
     80  1.179       mrg #include "ioconf.h"
     81  1.179       mrg 
     82  1.160       mrg #if defined(USB_DEBUG)
     83  1.156     skrll 
     84  1.156     skrll #ifndef USBHIST_SIZE
     85  1.156     skrll #define USBHIST_SIZE 50000
     86  1.156     skrll #endif
     87  1.156     skrll 
     88  1.157     skrll static struct kern_history_ent usbhistbuf[USBHIST_SIZE];
     89  1.157     skrll USBHIST_DEFINE(usbhist) = KERNHIST_INITIALIZER(usbhist, usbhistbuf);
     90  1.157     skrll 
     91  1.156     skrll #endif
     92    1.1  augustss 
     93   1.26  augustss #define USB_DEV_MINOR 255
     94   1.26  augustss 
     95    1.1  augustss #ifdef USB_DEBUG
     96   1.66  augustss /*
     97   1.34  augustss  * 0  - do usual exploration
     98   1.34  augustss  * 1  - do not use timeout exploration
     99   1.34  augustss  * >1 - do no exploration
    100   1.34  augustss  */
    101   1.21  augustss int	usb_noexplore = 0;
    102  1.156     skrll 
    103  1.162     skrll int	usbdebug = 0;
    104  1.156     skrll SYSCTL_SETUP(sysctl_hw_usb_setup, "sysctl hw.usb setup")
    105  1.156     skrll {
    106  1.156     skrll 	int err;
    107  1.156     skrll 	const struct sysctlnode *rnode;
    108  1.156     skrll 	const struct sysctlnode *cnode;
    109  1.156     skrll 
    110  1.156     skrll 	err = sysctl_createv(clog, 0, NULL, &rnode,
    111  1.156     skrll 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "usb",
    112  1.156     skrll 	    SYSCTL_DESCR("usb global controls"),
    113  1.156     skrll 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
    114  1.156     skrll 
    115  1.156     skrll 	if (err)
    116  1.156     skrll 		goto fail;
    117  1.156     skrll 
    118  1.156     skrll 	/* control debugging printfs */
    119  1.156     skrll 	err = sysctl_createv(clog, 0, &rnode, &cnode,
    120  1.156     skrll 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    121  1.156     skrll 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    122  1.156     skrll 	    NULL, 0, &usbdebug, sizeof(usbdebug), CTL_CREATE, CTL_EOL);
    123  1.156     skrll 	if (err)
    124  1.156     skrll 		goto fail;
    125  1.156     skrll 
    126  1.156     skrll 	return;
    127  1.156     skrll fail:
    128  1.156     skrll 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    129  1.156     skrll }
    130    1.1  augustss #else
    131  1.130       mrg #define	usb_noexplore 0
    132    1.1  augustss #endif
    133    1.1  augustss 
    134  1.162     skrll #define	DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(usbdebug,FMT,A,B,C,D)
    135  1.162     skrll #define	DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usbdebug,N,FMT,A,B,C,D)
    136  1.162     skrll 
    137    1.1  augustss struct usb_softc {
    138  1.109  drochner #if 0
    139  1.123    dyoung 	device_t	sc_dev;		/* base device */
    140  1.109  drochner #endif
    141  1.185     skrll 	struct usbd_bus *sc_bus;	/* USB controller */
    142    1.1  augustss 	struct usbd_port sc_port;	/* dummy port for root hub */
    143   1.25  augustss 
    144   1.97        ad 	struct lwp	*sc_event_thread;
    145  1.196  riastrad 	struct lwp	*sc_attach_thread;
    146   1.25  augustss 
    147   1.25  augustss 	char		sc_dying;
    148  1.173       mrg 	bool		sc_pmf_registered;
    149    1.1  augustss };
    150    1.1  augustss 
    151   1.90     joerg struct usb_taskq {
    152   1.90     joerg 	TAILQ_HEAD(, usb_task) tasks;
    153  1.130       mrg 	kmutex_t lock;
    154  1.130       mrg 	kcondvar_t cv;
    155   1.97        ad 	struct lwp *task_thread_lwp;
    156   1.90     joerg 	const char *name;
    157  1.170  riastrad 	struct usb_task *current_task;
    158   1.90     joerg };
    159   1.90     joerg 
    160   1.90     joerg static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
    161   1.58  augustss 
    162  1.183  riastrad /* XXX wrong place */
    163  1.183  riastrad #ifdef KDTRACE_HOOKS
    164  1.183  riastrad #define	__dtrace_used
    165  1.183  riastrad #else
    166  1.183  riastrad #define	__dtrace_used	__unused
    167  1.183  riastrad #endif
    168  1.183  riastrad 
    169  1.183  riastrad SDT_PROVIDER_DEFINE(usb);
    170  1.183  riastrad 
    171  1.183  riastrad SDT_PROBE_DEFINE3(usb, kernel, task, add,
    172  1.183  riastrad     "struct usbd_device *"/*dev*/, "struct usb_task *"/*task*/, "int"/*q*/);
    173  1.183  riastrad SDT_PROBE_DEFINE2(usb, kernel, task, rem__start,
    174  1.183  riastrad     "struct usbd_device *"/*dev*/, "struct usb_task *"/*task*/);
    175  1.183  riastrad SDT_PROBE_DEFINE3(usb, kernel, task, rem__done,
    176  1.183  riastrad     "struct usbd_device *"/*dev*/,
    177  1.183  riastrad     "struct usb_task *"/*task*/,
    178  1.183  riastrad     "bool"/*removed*/);
    179  1.183  riastrad SDT_PROBE_DEFINE4(usb, kernel, task, rem__wait__start,
    180  1.183  riastrad     "struct usbd_device *"/*dev*/,
    181  1.183  riastrad     "struct usb_task *"/*task*/,
    182  1.183  riastrad     "int"/*queue*/,
    183  1.183  riastrad     "kmutex_t *"/*interlock*/);
    184  1.183  riastrad SDT_PROBE_DEFINE5(usb, kernel, task, rem__wait__done,
    185  1.183  riastrad     "struct usbd_device *"/*dev*/,
    186  1.183  riastrad     "struct usb_task *"/*task*/,
    187  1.183  riastrad     "int"/*queue*/,
    188  1.183  riastrad     "kmutex_t *"/*interlock*/,
    189  1.183  riastrad     "bool"/*done*/);
    190  1.183  riastrad 
    191  1.183  riastrad SDT_PROBE_DEFINE1(usb, kernel, task, start,  "struct usb_task *"/*task*/);
    192  1.183  riastrad SDT_PROBE_DEFINE1(usb, kernel, task, done,  "struct usb_task *"/*task*/);
    193  1.183  riastrad 
    194  1.183  riastrad SDT_PROBE_DEFINE1(usb, kernel, bus, needs__explore,
    195  1.183  riastrad     "struct usbd_bus *"/*bus*/);
    196  1.183  riastrad SDT_PROBE_DEFINE1(usb, kernel, bus, needs__reattach,
    197  1.183  riastrad     "struct usbd_bus *"/*bus*/);
    198  1.183  riastrad SDT_PROBE_DEFINE1(usb, kernel, bus, discover__start,
    199  1.183  riastrad     "struct usbd_bus *"/*bus*/);
    200  1.183  riastrad SDT_PROBE_DEFINE1(usb, kernel, bus, discover__done,
    201  1.183  riastrad     "struct usbd_bus *"/*bus*/);
    202  1.183  riastrad SDT_PROBE_DEFINE1(usb, kernel, bus, explore__start,
    203  1.183  riastrad     "struct usbd_bus *"/*bus*/);
    204  1.183  riastrad SDT_PROBE_DEFINE1(usb, kernel, bus, explore__done,
    205  1.183  riastrad     "struct usbd_bus *"/*bus*/);
    206  1.183  riastrad 
    207  1.183  riastrad SDT_PROBE_DEFINE1(usb, kernel, event, add,  "struct usb_event *"/*uep*/);
    208  1.183  riastrad SDT_PROBE_DEFINE1(usb, kernel, event, drop,  "struct usb_event *"/*uep*/);
    209  1.183  riastrad 
    210   1.72   gehenna dev_type_open(usbopen);
    211   1.72   gehenna dev_type_close(usbclose);
    212   1.72   gehenna dev_type_read(usbread);
    213   1.72   gehenna dev_type_ioctl(usbioctl);
    214   1.72   gehenna dev_type_poll(usbpoll);
    215   1.74  jdolecek dev_type_kqfilter(usbkqfilter);
    216   1.72   gehenna 
    217   1.72   gehenna const struct cdevsw usb_cdevsw = {
    218  1.149  dholland 	.d_open = usbopen,
    219  1.149  dholland 	.d_close = usbclose,
    220  1.149  dholland 	.d_read = usbread,
    221  1.149  dholland 	.d_write = nowrite,
    222  1.149  dholland 	.d_ioctl = usbioctl,
    223  1.149  dholland 	.d_stop = nostop,
    224  1.149  dholland 	.d_tty = notty,
    225  1.149  dholland 	.d_poll = usbpoll,
    226  1.149  dholland 	.d_mmap = nommap,
    227  1.149  dholland 	.d_kqfilter = usbkqfilter,
    228  1.154  dholland 	.d_discard = nodiscard,
    229  1.149  dholland 	.d_flag = D_OTHER
    230   1.72   gehenna };
    231    1.7  augustss 
    232  1.109  drochner Static void	usb_discover(struct usb_softc *);
    233  1.109  drochner Static void	usb_create_event_thread(device_t);
    234   1.45  augustss Static void	usb_event_thread(void *);
    235   1.58  augustss Static void	usb_task_thread(void *);
    236    1.1  augustss 
    237  1.187     skrll /*
    238  1.187     skrll  * Count of USB busses
    239  1.187     skrll  */
    240  1.187     skrll int nusbbusses = 0;
    241  1.187     skrll 
    242   1.41  augustss #define USB_MAX_EVENTS 100
    243   1.26  augustss struct usb_event_q {
    244   1.26  augustss 	struct usb_event ue;
    245   1.26  augustss 	SIMPLEQ_ENTRY(usb_event_q) next;
    246   1.26  augustss };
    247   1.66  augustss Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    248   1.29  augustss 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    249   1.42  augustss Static int usb_nevents = 0;
    250   1.42  augustss Static struct selinfo usb_selevent;
    251  1.130       mrg Static kmutex_t usb_event_lock;
    252  1.130       mrg Static kcondvar_t usb_event_cv;
    253  1.173       mrg /* XXX this is gross and broken */
    254  1.123    dyoung Static proc_t *usb_async_proc;  /* process that wants USB SIGIO */
    255  1.112        ad Static void *usb_async_sih;
    256   1.42  augustss Static int usb_dev_open = 0;
    257   1.87  christos Static struct usb_event *usb_alloc_event(void);
    258   1.87  christos Static void usb_free_event(struct usb_event *);
    259   1.45  augustss Static void usb_add_event(int, struct usb_event *);
    260   1.45  augustss Static int usb_get_next_event(struct usb_event *);
    261  1.112        ad Static void usb_async_intr(void *);
    262  1.130       mrg Static void usb_soft_intr(void *);
    263   1.23  augustss 
    264   1.42  augustss Static const char *usbrev_str[] = USBREV_STR;
    265   1.31  augustss 
    266  1.117    cegger static int usb_match(device_t, cfdata_t, void *);
    267  1.106    dyoung static void usb_attach(device_t, device_t, void *);
    268  1.106    dyoung static int usb_detach(device_t, int);
    269  1.106    dyoung static int usb_activate(device_t, enum devact);
    270  1.106    dyoung static void usb_childdet(device_t, device_t);
    271  1.130       mrg static int usb_once_init(void);
    272  1.110        ad static void usb_doattach(device_t);
    273  1.106    dyoung 
    274  1.116    dyoung CFATTACH_DECL3_NEW(usb, sizeof(struct usb_softc),
    275  1.116    dyoung     usb_match, usb_attach, usb_detach, usb_activate, NULL, usb_childdet,
    276  1.116    dyoung     DVF_DETACH_SHUTDOWN);
    277    1.1  augustss 
    278  1.134       mrg static const char *taskq_names[] = USB_TASKQ_NAMES;
    279  1.134       mrg 
    280  1.118    dyoung int
    281  1.118    dyoung usb_match(device_t parent, cfdata_t match, void *aux)
    282    1.1  augustss {
    283  1.162     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    284  1.162     skrll 
    285  1.162     skrll 	return UMATCH_GENERIC;
    286    1.1  augustss }
    287    1.1  augustss 
    288  1.118    dyoung void
    289  1.118    dyoung usb_attach(device_t parent, device_t self, void *aux)
    290    1.1  augustss {
    291  1.130       mrg 	static ONCE_DECL(init_control);
    292  1.110        ad 	struct usb_softc *sc = device_private(self);
    293  1.110        ad 	int usbrev;
    294  1.110        ad 
    295  1.110        ad 	sc->sc_bus = aux;
    296  1.162     skrll 	usbrev = sc->sc_bus->ub_revision;
    297  1.110        ad 
    298  1.173       mrg 	cv_init(&sc->sc_bus->ub_needsexplore_cv, "usbevt");
    299  1.173       mrg 	sc->sc_pmf_registered = false;
    300  1.173       mrg 
    301  1.110        ad 	aprint_naive("\n");
    302  1.110        ad 	aprint_normal(": USB revision %s", usbrev_str[usbrev]);
    303  1.110        ad 	switch (usbrev) {
    304  1.110        ad 	case USBREV_1_0:
    305  1.110        ad 	case USBREV_1_1:
    306  1.110        ad 	case USBREV_2_0:
    307  1.155     skrll 	case USBREV_3_0:
    308  1.169   msaitoh 	case USBREV_3_1:
    309  1.110        ad 		break;
    310  1.110        ad 	default:
    311  1.110        ad 		aprint_error(", not supported\n");
    312  1.110        ad 		sc->sc_dying = 1;
    313  1.123    dyoung 		return;
    314  1.110        ad 	}
    315  1.110        ad 	aprint_normal("\n");
    316  1.110        ad 
    317  1.143  jakllsch 	/* XXX we should have our own level */
    318  1.164     skrll 	sc->sc_bus->ub_soft = softint_establish(SOFTINT_USB | SOFTINT_MPSAFE,
    319  1.143  jakllsch 	    usb_soft_intr, sc->sc_bus);
    320  1.162     skrll 	if (sc->sc_bus->ub_soft == NULL) {
    321  1.143  jakllsch 		aprint_error("%s: can't register softintr\n",
    322  1.143  jakllsch 			     device_xname(self));
    323  1.143  jakllsch 		sc->sc_dying = 1;
    324  1.143  jakllsch 		return;
    325  1.143  jakllsch 	}
    326  1.143  jakllsch 
    327  1.162     skrll 	sc->sc_bus->ub_methods->ubm_getlock(sc->sc_bus, &sc->sc_bus->ub_lock);
    328  1.162     skrll 	KASSERT(sc->sc_bus->ub_lock != NULL);
    329  1.134       mrg 
    330  1.130       mrg 	RUN_ONCE(&init_control, usb_once_init);
    331  1.110        ad 	config_interrupts(self, usb_doattach);
    332  1.110        ad }
    333  1.110        ad 
    334  1.189       mrg #ifdef DDB
    335  1.189       mrg #include <machine/db_machdep.h>
    336  1.189       mrg #include <ddb/db_output.h>
    337  1.189       mrg #include <ddb/db_command.h>
    338  1.189       mrg 
    339  1.189       mrg static void
    340  1.189       mrg db_usb_xfer(db_expr_t addr, bool have_addr, db_expr_t count,
    341  1.189       mrg     const char *modif)
    342  1.189       mrg {
    343  1.193       mrg 	struct usbd_xfer *xfer = (struct usbd_xfer *)(uintptr_t)addr;
    344  1.189       mrg 
    345  1.190       mrg 	if (!have_addr) {
    346  1.189       mrg 		db_printf("%s: need usbd_xfer address\n", __func__);
    347  1.189       mrg 		return;
    348  1.189       mrg 	}
    349  1.189       mrg 
    350  1.189       mrg 	db_printf("usb xfer: %p pipe %p priv %p buffer %p\n",
    351  1.189       mrg 	    xfer, xfer->ux_pipe, xfer->ux_priv, xfer->ux_buffer);
    352  1.189       mrg 	db_printf(" len %x actlen %x flags %x timeout %x status %x\n",
    353  1.189       mrg 	    xfer->ux_length, xfer->ux_actlen, xfer->ux_flags, xfer->ux_timeout,
    354  1.189       mrg 	    xfer->ux_status);
    355  1.189       mrg 	db_printf(" callback %p done %x state %x tm_set %x tm_reset %x\n",
    356  1.189       mrg 	    xfer->ux_callback, xfer->ux_done, xfer->ux_state,
    357  1.189       mrg 	    xfer->ux_timeout_set, xfer->ux_timeout_reset);
    358  1.189       mrg }
    359  1.189       mrg 
    360  1.189       mrg static void
    361  1.189       mrg db_usb_xferlist(db_expr_t addr, bool have_addr, db_expr_t count,
    362  1.189       mrg     const char *modif)
    363  1.189       mrg {
    364  1.193       mrg 	struct usbd_pipe *pipe = (struct usbd_pipe *)(uintptr_t)addr;
    365  1.189       mrg 	struct usbd_xfer *xfer;
    366  1.189       mrg 
    367  1.190       mrg 	if (!have_addr) {
    368  1.189       mrg 		db_printf("%s: need usbd_pipe address\n", __func__);
    369  1.189       mrg 		return;
    370  1.189       mrg 	}
    371  1.189       mrg 
    372  1.189       mrg 	db_printf("usb pipe: %p\n", pipe);
    373  1.192       mrg 	unsigned xfercount = 0;
    374  1.189       mrg 	SIMPLEQ_FOREACH(xfer, &pipe->up_queue, ux_next) {
    375  1.192       mrg 		db_printf("  xfer = %p%s", xfer,
    376  1.192       mrg 		    xfercount == 0 || xfercount % 2 == 0 ? "" : "\n");
    377  1.192       mrg 		xfercount++;
    378  1.189       mrg 	}
    379  1.189       mrg }
    380  1.189       mrg 
    381  1.190       mrg static const struct db_command db_usb_command_table[] = {
    382  1.189       mrg 	{ DDB_ADD_CMD("usbxfer",	db_usb_xfer,	0,
    383  1.189       mrg 	  "display a USB xfer structure",
    384  1.189       mrg 	  NULL, NULL) },
    385  1.189       mrg 	{ DDB_ADD_CMD("usbxferlist",	db_usb_xferlist,	0,
    386  1.189       mrg 	  "display a USB xfer structure given pipe",
    387  1.189       mrg 	  NULL, NULL) },
    388  1.191       mrg 	{ DDB_END_CMD },
    389  1.189       mrg };
    390  1.189       mrg 
    391  1.189       mrg static void
    392  1.189       mrg usb_init_ddb(void)
    393  1.189       mrg {
    394  1.189       mrg 
    395  1.189       mrg 	(void)db_register_tbl(DDB_SHOW_CMD, db_usb_command_table);
    396  1.189       mrg }
    397  1.189       mrg #else
    398  1.189       mrg #define usb_init_ddb() /* nothing */
    399  1.189       mrg #endif
    400  1.189       mrg 
    401  1.130       mrg static int
    402  1.130       mrg usb_once_init(void)
    403  1.130       mrg {
    404  1.134       mrg 	struct usb_taskq *taskq;
    405  1.134       mrg 	int i;
    406  1.130       mrg 
    407  1.159     skrll 	USBHIST_LINK_STATIC(usbhist);
    408  1.158     skrll 
    409  1.130       mrg 	selinit(&usb_selevent);
    410  1.130       mrg 	mutex_init(&usb_event_lock, MUTEX_DEFAULT, IPL_NONE);
    411  1.130       mrg 	cv_init(&usb_event_cv, "usbrea");
    412  1.134       mrg 
    413  1.134       mrg 	for (i = 0; i < USB_NUM_TASKQS; i++) {
    414  1.134       mrg 		taskq = &usb_taskq[i];
    415  1.134       mrg 
    416  1.134       mrg 		TAILQ_INIT(&taskq->tasks);
    417  1.136       mrg 		/*
    418  1.139     skrll 		 * Since USB task methods usb_{add,rem}_task are callable
    419  1.139     skrll 		 * from any context, we have to make this lock a spinlock.
    420  1.136       mrg 		 */
    421  1.136       mrg 		mutex_init(&taskq->lock, MUTEX_DEFAULT, IPL_USB);
    422  1.134       mrg 		cv_init(&taskq->cv, "usbtsk");
    423  1.134       mrg 		taskq->name = taskq_names[i];
    424  1.170  riastrad 		taskq->current_task = NULL;
    425  1.134       mrg 		if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    426  1.134       mrg 		    usb_task_thread, taskq, &taskq->task_thread_lwp,
    427  1.134       mrg 		    "%s", taskq->name)) {
    428  1.134       mrg 			printf("unable to create task thread: %s\n", taskq->name);
    429  1.134       mrg 			panic("usb_create_event_thread task");
    430  1.134       mrg 		}
    431  1.134       mrg 		/*
    432  1.134       mrg 		 * XXX we should make sure these threads are alive before
    433  1.134       mrg 		 * end up using them in usb_doattach().
    434  1.134       mrg 		 */
    435  1.134       mrg 	}
    436  1.173       mrg 
    437  1.173       mrg 	KASSERT(usb_async_sih == NULL);
    438  1.173       mrg 	usb_async_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    439  1.173       mrg 	   usb_async_intr, NULL);
    440  1.173       mrg 
    441  1.189       mrg 	usb_init_ddb();
    442  1.189       mrg 
    443  1.130       mrg 	return 0;
    444  1.130       mrg }
    445  1.130       mrg 
    446  1.110        ad static void
    447  1.110        ad usb_doattach(device_t self)
    448  1.110        ad {
    449  1.109  drochner 	struct usb_softc *sc = device_private(self);
    450  1.162     skrll 	struct usbd_device *dev;
    451   1.29  augustss 	usbd_status err;
    452   1.57  augustss 	int speed;
    453   1.87  christos 	struct usb_event *ue;
    454   1.66  augustss 
    455  1.162     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    456   1.31  augustss 
    457  1.195  riastrad 	KASSERT(KERNEL_LOCKED_P());
    458  1.195  riastrad 
    459  1.187     skrll 	/* Protected by KERNEL_LOCK */
    460  1.187     skrll 	nusbbusses++;
    461  1.187     skrll 
    462  1.162     skrll 	sc->sc_bus->ub_usbctl = self;
    463  1.162     skrll 	sc->sc_port.up_power = USB_MAX_POWER;
    464   1.31  augustss 
    465  1.162     skrll 	switch (sc->sc_bus->ub_revision) {
    466   1.57  augustss 	case USBREV_1_0:
    467   1.57  augustss 	case USBREV_1_1:
    468   1.57  augustss 		speed = USB_SPEED_FULL;
    469   1.57  augustss 		break;
    470   1.57  augustss 	case USBREV_2_0:
    471   1.57  augustss 		speed = USB_SPEED_HIGH;
    472   1.57  augustss 		break;
    473  1.155     skrll 	case USBREV_3_0:
    474  1.155     skrll 		speed = USB_SPEED_SUPER;
    475  1.155     skrll 		break;
    476  1.169   msaitoh 	case USBREV_3_1:
    477  1.169   msaitoh 		speed = USB_SPEED_SUPER_PLUS;
    478  1.169   msaitoh 		break;
    479   1.57  augustss 	default:
    480  1.110        ad 		panic("usb_doattach");
    481   1.32  augustss 	}
    482   1.35  augustss 
    483   1.87  christos 	ue = usb_alloc_event();
    484  1.109  drochner 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
    485   1.87  christos 	usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
    486   1.38  augustss 
    487  1.196  riastrad 	sc->sc_attach_thread = curlwp;
    488  1.109  drochner 	err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
    489   1.29  augustss 		  &sc->sc_port);
    490  1.196  riastrad 	sc->sc_attach_thread = NULL;
    491   1.29  augustss 	if (!err) {
    492  1.162     skrll 		dev = sc->sc_port.up_dev;
    493  1.162     skrll 		if (dev->ud_hub == NULL) {
    494   1.22  augustss 			sc->sc_dying = 1;
    495  1.102  jmcneill 			aprint_error("%s: root device is not a hub\n",
    496  1.109  drochner 				     device_xname(self));
    497  1.123    dyoung 			return;
    498    1.1  augustss 		}
    499  1.162     skrll 		sc->sc_bus->ub_roothub = dev;
    500  1.135       mrg 		usb_create_event_thread(self);
    501    1.1  augustss 	} else {
    502  1.142  jakllsch 		aprint_error("%s: root hub problem, error=%s\n",
    503  1.142  jakllsch 			     device_xname(self), usbd_errstr(err));
    504   1.22  augustss 		sc->sc_dying = 1;
    505    1.1  augustss 	}
    506    1.7  augustss 
    507  1.172       mrg 	/*
    508  1.172       mrg 	 * Drop this reference after the first set of attachments in the
    509  1.172       mrg 	 * event thread.
    510  1.172       mrg 	 */
    511  1.145  christos 	config_pending_incr(self);
    512   1.28  augustss 
    513  1.104  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    514  1.104  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    515  1.173       mrg 	else
    516  1.173       mrg 		sc->sc_pmf_registered = true;
    517  1.112        ad 
    518  1.123    dyoung 	return;
    519    1.1  augustss }
    520    1.1  augustss 
    521   1.13  augustss void
    522  1.109  drochner usb_create_event_thread(device_t self)
    523   1.13  augustss {
    524  1.109  drochner 	struct usb_softc *sc = device_private(self);
    525   1.13  augustss 
    526  1.194  riastrad 	if (kthread_create(PRI_NONE, 0, NULL,
    527  1.130       mrg 	    usb_event_thread, sc, &sc->sc_event_thread,
    528  1.130       mrg 	    "%s", device_xname(self))) {
    529   1.13  augustss 		printf("%s: unable to create event thread for\n",
    530  1.109  drochner 		       device_xname(self));
    531   1.13  augustss 		panic("usb_create_event_thread");
    532   1.13  augustss 	}
    533   1.13  augustss }
    534   1.13  augustss 
    535  1.196  riastrad bool
    536  1.196  riastrad usb_in_event_thread(device_t dev)
    537  1.196  riastrad {
    538  1.196  riastrad 	struct usb_softc *sc;
    539  1.196  riastrad 
    540  1.196  riastrad 	if (cold)
    541  1.196  riastrad 		return true;
    542  1.196  riastrad 
    543  1.196  riastrad 	for (; dev; dev = device_parent(dev)) {
    544  1.196  riastrad 		if (device_is_a(dev, "usb"))
    545  1.196  riastrad 			break;
    546  1.196  riastrad 	}
    547  1.196  riastrad 	if (dev == NULL)
    548  1.196  riastrad 		return false;
    549  1.196  riastrad 	sc = device_private(dev);
    550  1.196  riastrad 
    551  1.196  riastrad 	return curlwp == sc->sc_event_thread || curlwp == sc->sc_attach_thread;
    552  1.196  riastrad }
    553  1.196  riastrad 
    554   1.52  augustss /*
    555   1.62  augustss  * Add a task to be performed by the task thread.  This function can be
    556   1.52  augustss  * called from any context and the task will be executed in a process
    557   1.52  augustss  * context ASAP.
    558   1.52  augustss  */
    559   1.13  augustss void
    560  1.162     skrll usb_add_task(struct usbd_device *dev, struct usb_task *task, int queue)
    561   1.51  augustss {
    562   1.90     joerg 	struct usb_taskq *taskq;
    563   1.51  augustss 
    564  1.162     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    565  1.183  riastrad 	SDT_PROBE3(usb, kernel, task, add,  dev, task, queue);
    566  1.162     skrll 
    567  1.150  riastrad 	KASSERT(0 <= queue);
    568  1.150  riastrad 	KASSERT(queue < USB_NUM_TASKQS);
    569   1.90     joerg 	taskq = &usb_taskq[queue];
    570  1.130       mrg 	mutex_enter(&taskq->lock);
    571  1.150  riastrad 	if (atomic_cas_uint(&task->queue, USB_NUM_TASKQS, queue) ==
    572  1.150  riastrad 	    USB_NUM_TASKQS) {
    573  1.168  pgoyette 		DPRINTFN(2, "task=%#jx", (uintptr_t)task, 0, 0, 0);
    574   1.90     joerg 		TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
    575  1.150  riastrad 		cv_signal(&taskq->cv);
    576   1.62  augustss 	} else {
    577  1.168  pgoyette 		DPRINTFN(2, "task=%#jx on q", (uintptr_t)task, 0, 0, 0);
    578   1.62  augustss 	}
    579  1.130       mrg 	mutex_exit(&taskq->lock);
    580   1.51  augustss }
    581   1.51  augustss 
    582  1.150  riastrad /*
    583  1.170  riastrad  * usb_rem_task(dev, task)
    584  1.170  riastrad  *
    585  1.181  riastrad  *	If task is queued to run, remove it from the queue.  Return
    586  1.181  riastrad  *	true if it successfully removed the task from the queue, false
    587  1.181  riastrad  *	if not.
    588  1.170  riastrad  *
    589  1.170  riastrad  *	Caller is _not_ guaranteed that the task is not running when
    590  1.170  riastrad  *	this is done.
    591  1.170  riastrad  *
    592  1.170  riastrad  *	Never sleeps.
    593  1.150  riastrad  */
    594  1.181  riastrad bool
    595  1.162     skrll usb_rem_task(struct usbd_device *dev, struct usb_task *task)
    596   1.53  augustss {
    597  1.150  riastrad 	unsigned queue;
    598   1.53  augustss 
    599  1.162     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    600  1.183  riastrad 	SDT_PROBE2(usb, kernel, task, rem__start,  dev, task);
    601  1.162     skrll 
    602  1.150  riastrad 	while ((queue = task->queue) != USB_NUM_TASKQS) {
    603  1.150  riastrad 		struct usb_taskq *taskq = &usb_taskq[queue];
    604  1.133  christos 		mutex_enter(&taskq->lock);
    605  1.150  riastrad 		if (__predict_true(task->queue == queue)) {
    606  1.150  riastrad 			TAILQ_REMOVE(&taskq->tasks, task, next);
    607  1.150  riastrad 			task->queue = USB_NUM_TASKQS;
    608  1.150  riastrad 			mutex_exit(&taskq->lock);
    609  1.183  riastrad 			SDT_PROBE3(usb, kernel, task, rem__done,
    610  1.183  riastrad 			    dev, task, true);
    611  1.181  riastrad 			return true; /* removed from the queue */
    612  1.150  riastrad 		}
    613  1.133  christos 		mutex_exit(&taskq->lock);
    614  1.132    cegger 	}
    615  1.181  riastrad 
    616  1.183  riastrad 	SDT_PROBE3(usb, kernel, task, rem__done,  dev, task, false);
    617  1.181  riastrad 	return false;		/* was not removed from the queue */
    618   1.53  augustss }
    619   1.53  augustss 
    620  1.170  riastrad /*
    621  1.171  riastrad  * usb_rem_task_wait(dev, task, queue, interlock)
    622  1.170  riastrad  *
    623  1.170  riastrad  *	If task is scheduled to run, remove it from the queue.  If it
    624  1.171  riastrad  *	may have already begun to run, drop interlock if not null, wait
    625  1.171  riastrad  *	for it to complete, and reacquire interlock if not null.
    626  1.171  riastrad  *	Return true if it successfully removed the task from the queue,
    627  1.171  riastrad  *	false if not.
    628  1.170  riastrad  *
    629  1.170  riastrad  *	Caller MUST guarantee that task will not be scheduled on a
    630  1.170  riastrad  *	_different_ queue, at least until after this returns.
    631  1.170  riastrad  *
    632  1.170  riastrad  *	If caller guarantees that task will not be scheduled on the
    633  1.170  riastrad  *	same queue before this returns, then caller is guaranteed that
    634  1.170  riastrad  *	the task is not running at all when this returns.
    635  1.170  riastrad  *
    636  1.170  riastrad  *	May sleep.
    637  1.170  riastrad  */
    638  1.171  riastrad bool
    639  1.171  riastrad usb_rem_task_wait(struct usbd_device *dev, struct usb_task *task, int queue,
    640  1.171  riastrad     kmutex_t *interlock)
    641  1.170  riastrad {
    642  1.170  riastrad 	struct usb_taskq *taskq;
    643  1.170  riastrad 	int queue1;
    644  1.171  riastrad 	bool removed;
    645  1.170  riastrad 
    646  1.170  riastrad 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    647  1.183  riastrad 	SDT_PROBE4(usb, kernel, task, rem__wait__start,
    648  1.183  riastrad 	    dev, task, queue, interlock);
    649  1.170  riastrad 	ASSERT_SLEEPABLE();
    650  1.170  riastrad 	KASSERT(0 <= queue);
    651  1.170  riastrad 	KASSERT(queue < USB_NUM_TASKQS);
    652  1.170  riastrad 
    653  1.170  riastrad 	taskq = &usb_taskq[queue];
    654  1.171  riastrad 	mutex_enter(&taskq->lock);
    655  1.171  riastrad 	queue1 = task->queue;
    656  1.171  riastrad 	if (queue1 == USB_NUM_TASKQS) {
    657  1.170  riastrad 		/*
    658  1.171  riastrad 		 * It is not on the queue.  It may be about to run, or
    659  1.171  riastrad 		 * it may have already finished running -- there is no
    660  1.171  riastrad 		 * stopping it now.  Wait for it if it is running.
    661  1.170  riastrad 		 */
    662  1.171  riastrad 		if (interlock)
    663  1.171  riastrad 			mutex_exit(interlock);
    664  1.171  riastrad 		while (taskq->current_task == task)
    665  1.171  riastrad 			cv_wait(&taskq->cv, &taskq->lock);
    666  1.171  riastrad 		removed = false;
    667  1.170  riastrad 	} else {
    668  1.170  riastrad 		/*
    669  1.171  riastrad 		 * It is still on the queue.  We can stop it before the
    670  1.171  riastrad 		 * task thread will run it.
    671  1.170  riastrad 		 */
    672  1.171  riastrad 		KASSERTMSG(queue1 == queue, "task %p on q%d expected on q%d",
    673  1.171  riastrad 		    task, queue1, queue);
    674  1.171  riastrad 		TAILQ_REMOVE(&taskq->tasks, task, next);
    675  1.171  riastrad 		task->queue = USB_NUM_TASKQS;
    676  1.171  riastrad 		removed = true;
    677  1.170  riastrad 	}
    678  1.171  riastrad 	mutex_exit(&taskq->lock);
    679  1.171  riastrad 
    680  1.171  riastrad 	/*
    681  1.171  riastrad 	 * If there's an interlock, and we dropped it to wait,
    682  1.171  riastrad 	 * reacquire it.
    683  1.171  riastrad 	 */
    684  1.171  riastrad 	if (interlock && !removed)
    685  1.171  riastrad 		mutex_enter(interlock);
    686  1.171  riastrad 
    687  1.183  riastrad 	SDT_PROBE5(usb, kernel, task, rem__wait__done,
    688  1.183  riastrad 	    dev, task, queue, interlock, removed);
    689  1.171  riastrad 	return removed;
    690  1.170  riastrad }
    691  1.170  riastrad 
    692  1.182  riastrad /*
    693  1.182  riastrad  * usb_task_pending(dev, task)
    694  1.182  riastrad  *
    695  1.182  riastrad  *	True if task is queued, false if not.  Note that if task is
    696  1.182  riastrad  *	already running, it is not considered queued.
    697  1.182  riastrad  *
    698  1.182  riastrad  *	For _negative_ diagnostic assertions only:
    699  1.182  riastrad  *
    700  1.182  riastrad  *		KASSERT(!usb_task_pending(dev, task));
    701  1.182  riastrad  */
    702  1.182  riastrad bool
    703  1.182  riastrad usb_task_pending(struct usbd_device *dev, struct usb_task *task)
    704  1.182  riastrad {
    705  1.182  riastrad 
    706  1.182  riastrad 	return task->queue != USB_NUM_TASKQS;
    707  1.182  riastrad }
    708  1.182  riastrad 
    709   1.53  augustss void
    710   1.45  augustss usb_event_thread(void *arg)
    711   1.13  augustss {
    712   1.13  augustss 	struct usb_softc *sc = arg;
    713  1.172       mrg 	struct usbd_bus *bus = sc->sc_bus;
    714   1.13  augustss 
    715  1.162     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    716   1.40  augustss 
    717  1.195  riastrad 	KASSERT(KERNEL_LOCKED_P());
    718  1.195  riastrad 
    719   1.60  augustss 	/*
    720   1.60  augustss 	 * In case this controller is a companion controller to an
    721   1.60  augustss 	 * EHCI controller we need to wait until the EHCI controller
    722   1.60  augustss 	 * has grabbed the port.
    723   1.64  augustss 	 * XXX It would be nicer to do this with a tsleep(), but I don't
    724   1.64  augustss 	 * know how to synchronize the creation of the threads so it
    725   1.64  augustss 	 * will work.
    726   1.60  augustss 	 */
    727  1.172       mrg 	usb_delay_ms(bus, 500);
    728   1.60  augustss 
    729   1.40  augustss 	/* Make sure first discover does something. */
    730  1.172       mrg 	mutex_enter(bus->ub_lock);
    731  1.162     skrll 	sc->sc_bus->ub_needsexplore = 1;
    732   1.51  augustss 	usb_discover(sc);
    733  1.172       mrg 	mutex_exit(bus->ub_lock);
    734  1.172       mrg 
    735  1.172       mrg 	/* Drop the config_pending reference from attach. */
    736  1.172       mrg 	config_pending_decr(bus->ub_usbctl);
    737   1.27  augustss 
    738  1.172       mrg 	mutex_enter(bus->ub_lock);
    739   1.22  augustss 	while (!sc->sc_dying) {
    740  1.172       mrg #if 0 /* not yet */
    741  1.172       mrg 		while (sc->sc_bus->ub_usepolling)
    742  1.172       mrg 			kpause("usbpoll", true, hz, bus->ub_lock);
    743  1.172       mrg #endif
    744  1.172       mrg 
    745   1.58  augustss 		if (usb_noexplore < 2)
    746  1.130       mrg 			usb_discover(sc);
    747  1.130       mrg 
    748  1.172       mrg 		cv_timedwait(&bus->ub_needsexplore_cv,
    749  1.172       mrg 		    bus->ub_lock, usb_noexplore ? 0 : hz * 60);
    750  1.147     skrll 
    751  1.168  pgoyette 		DPRINTFN(2, "sc %#jx woke up", (uintptr_t)sc, 0, 0, 0);
    752   1.13  augustss 	}
    753   1.50  augustss 	sc->sc_event_thread = NULL;
    754   1.13  augustss 
    755   1.13  augustss 	/* In case parent is waiting for us to exit. */
    756  1.172       mrg 	cv_signal(&bus->ub_needsexplore_cv);
    757  1.172       mrg 	mutex_exit(bus->ub_lock);
    758   1.13  augustss 
    759  1.168  pgoyette 	DPRINTF("sc %#jx exit", (uintptr_t)sc, 0, 0, 0);
    760   1.13  augustss 	kthread_exit(0);
    761   1.13  augustss }
    762   1.13  augustss 
    763   1.58  augustss void
    764   1.90     joerg usb_task_thread(void *arg)
    765   1.58  augustss {
    766   1.58  augustss 	struct usb_task *task;
    767   1.90     joerg 	struct usb_taskq *taskq;
    768  1.151  riastrad 	bool mpsafe;
    769   1.58  augustss 
    770  1.180       mrg 	taskq = arg;
    771  1.162     skrll 
    772  1.180       mrg 	USBHIST_FUNC();
    773  1.180       mrg 	USBHIST_CALLARGS(usbdebug, "start taskq %#jx",
    774  1.180       mrg 	    (uintptr_t)taskq, 0, 0, 0);
    775   1.58  augustss 
    776  1.130       mrg 	mutex_enter(&taskq->lock);
    777   1.58  augustss 	for (;;) {
    778   1.90     joerg 		task = TAILQ_FIRST(&taskq->tasks);
    779   1.58  augustss 		if (task == NULL) {
    780  1.130       mrg 			cv_wait(&taskq->cv, &taskq->lock);
    781   1.90     joerg 			task = TAILQ_FIRST(&taskq->tasks);
    782   1.58  augustss 		}
    783  1.168  pgoyette 		DPRINTFN(2, "woke up task=%#jx", (uintptr_t)task, 0, 0, 0);
    784   1.58  augustss 		if (task != NULL) {
    785  1.151  riastrad 			mpsafe = ISSET(task->flags, USB_TASKQ_MPSAFE);
    786   1.90     joerg 			TAILQ_REMOVE(&taskq->tasks, task, next);
    787  1.150  riastrad 			task->queue = USB_NUM_TASKQS;
    788  1.170  riastrad 			taskq->current_task = task;
    789  1.130       mrg 			mutex_exit(&taskq->lock);
    790  1.140  jmcneill 
    791  1.151  riastrad 			if (!mpsafe)
    792  1.140  jmcneill 				KERNEL_LOCK(1, curlwp);
    793  1.183  riastrad 			SDT_PROBE1(usb, kernel, task, start,  task);
    794   1.58  augustss 			task->fun(task->arg);
    795  1.151  riastrad 			/* Can't dereference task after this point.  */
    796  1.183  riastrad 			SDT_PROBE1(usb, kernel, task, done,  task);
    797  1.151  riastrad 			if (!mpsafe)
    798  1.140  jmcneill 				KERNEL_UNLOCK_ONE(curlwp);
    799  1.140  jmcneill 
    800  1.130       mrg 			mutex_enter(&taskq->lock);
    801  1.170  riastrad 			KASSERTMSG(taskq->current_task == task,
    802  1.170  riastrad 			    "somebody scribbled on usb taskq %p", taskq);
    803  1.170  riastrad 			taskq->current_task = NULL;
    804  1.170  riastrad 			cv_broadcast(&taskq->cv);
    805   1.58  augustss 		}
    806   1.58  augustss 	}
    807  1.130       mrg 	mutex_exit(&taskq->lock);
    808   1.58  augustss }
    809   1.58  augustss 
    810    1.1  augustss int
    811   1.91  christos usbctlprint(void *aux, const char *pnp)
    812    1.1  augustss {
    813    1.1  augustss 	/* only "usb"es can attach to host controllers */
    814    1.1  augustss 	if (pnp)
    815   1.77   thorpej 		aprint_normal("usb at %s", pnp);
    816    1.1  augustss 
    817  1.162     skrll 	return UNCONF;
    818    1.1  augustss }
    819    1.7  augustss 
    820    1.1  augustss int
    821   1.91  christos usbopen(dev_t dev, int flag, int mode, struct lwp *l)
    822    1.1  augustss {
    823   1.26  augustss 	int unit = minor(dev);
    824   1.26  augustss 	struct usb_softc *sc;
    825   1.26  augustss 
    826  1.187     skrll 	if (nusbbusses == 0)
    827  1.187     skrll 		return ENXIO;
    828  1.187     skrll 
    829   1.26  augustss 	if (unit == USB_DEV_MINOR) {
    830   1.26  augustss 		if (usb_dev_open)
    831  1.162     skrll 			return EBUSY;
    832   1.26  augustss 		usb_dev_open = 1;
    833  1.184        ad 		mutex_enter(&proc_lock);
    834  1.186     skrll 		usb_async_proc = NULL;
    835  1.184        ad 		mutex_exit(&proc_lock);
    836  1.162     skrll 		return 0;
    837   1.26  augustss 	}
    838   1.26  augustss 
    839  1.109  drochner 	sc = device_lookup_private(&usb_cd, unit);
    840  1.111  drochner 	if (!sc)
    841  1.162     skrll 		return ENXIO;
    842    1.1  augustss 
    843   1.22  augustss 	if (sc->sc_dying)
    844  1.162     skrll 		return EIO;
    845    1.1  augustss 
    846  1.162     skrll 	return 0;
    847    1.1  augustss }
    848    1.1  augustss 
    849    1.1  augustss int
    850   1.45  augustss usbread(dev_t dev, struct uio *uio, int flag)
    851   1.26  augustss {
    852   1.92     pavel 	struct usb_event *ue;
    853   1.93  christos 	struct usb_event_old *ueo = NULL;	/* XXXGCC */
    854  1.146  christos 	int useold = 0;
    855  1.146  christos 	int error, n;
    856   1.26  augustss 
    857   1.26  augustss 	if (minor(dev) != USB_DEV_MINOR)
    858  1.162     skrll 		return ENXIO;
    859   1.26  augustss 
    860   1.92     pavel 	switch (uio->uio_resid) {
    861   1.92     pavel 	case sizeof(struct usb_event_old):
    862  1.162     skrll 		ueo = kmem_zalloc(sizeof(struct usb_event_old), KM_SLEEP);
    863   1.92     pavel 		useold = 1;
    864  1.177       mrg 		/* FALLTHROUGH */
    865   1.92     pavel 	case sizeof(struct usb_event):
    866   1.92     pavel 		ue = usb_alloc_event();
    867   1.92     pavel 		break;
    868   1.92     pavel 	default:
    869  1.162     skrll 		return EINVAL;
    870   1.92     pavel 	}
    871   1.26  augustss 
    872   1.26  augustss 	error = 0;
    873  1.130       mrg 	mutex_enter(&usb_event_lock);
    874   1.26  augustss 	for (;;) {
    875   1.87  christos 		n = usb_get_next_event(ue);
    876   1.26  augustss 		if (n != 0)
    877   1.26  augustss 			break;
    878   1.26  augustss 		if (flag & IO_NDELAY) {
    879   1.26  augustss 			error = EWOULDBLOCK;
    880   1.26  augustss 			break;
    881   1.26  augustss 		}
    882  1.130       mrg 		error = cv_wait_sig(&usb_event_cv, &usb_event_lock);
    883   1.26  augustss 		if (error)
    884   1.26  augustss 			break;
    885   1.26  augustss 	}
    886  1.130       mrg 	mutex_exit(&usb_event_lock);
    887   1.92     pavel 	if (!error) {
    888   1.92     pavel 		if (useold) { /* copy fields to old struct */
    889  1.178  pgoyette 			MODULE_HOOK_CALL(usb_subr_copy_30_hook,
    890  1.175  pgoyette 			    (ue, ueo, uio), enosys(), error);
    891  1.175  pgoyette 			if (error == ENOSYS)
    892  1.175  pgoyette 				error = EINVAL;
    893   1.92     pavel 
    894  1.175  pgoyette 			if (!error)
    895  1.175  pgoyette 				error = uiomove((void *)ueo, sizeof(*ueo), uio);
    896   1.92     pavel 		} else
    897  1.162     skrll 			error = uiomove((void *)ue, sizeof(*ue), uio);
    898   1.92     pavel 	}
    899   1.87  christos 	usb_free_event(ue);
    900  1.175  pgoyette 	if (ueo)
    901  1.162     skrll 		kmem_free(ueo, sizeof(struct usb_event_old));
    902   1.26  augustss 
    903  1.162     skrll 	return error;
    904   1.26  augustss }
    905   1.26  augustss 
    906   1.26  augustss int
    907   1.91  christos usbclose(dev_t dev, int flag, int mode,
    908   1.91  christos     struct lwp *l)
    909    1.1  augustss {
    910   1.26  augustss 	int unit = minor(dev);
    911   1.26  augustss 
    912   1.26  augustss 	if (unit == USB_DEV_MINOR) {
    913  1.184        ad 		mutex_enter(&proc_lock);
    914  1.186     skrll 		usb_async_proc = NULL;
    915  1.184        ad 		mutex_exit(&proc_lock);
    916   1.26  augustss 		usb_dev_open = 0;
    917   1.26  augustss 	}
    918   1.26  augustss 
    919  1.162     skrll 	return 0;
    920    1.1  augustss }
    921    1.1  augustss 
    922    1.1  augustss int
    923   1.96  christos usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
    924    1.1  augustss {
    925   1.26  augustss 	struct usb_softc *sc;
    926   1.28  augustss 	int unit = minor(devt);
    927   1.26  augustss 
    928  1.180       mrg 	USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "cmd %#jx", cmd, 0, 0, 0);
    929  1.162     skrll 
    930   1.26  augustss 	if (unit == USB_DEV_MINOR) {
    931   1.26  augustss 		switch (cmd) {
    932   1.26  augustss 		case FIONBIO:
    933   1.26  augustss 			/* All handled in the upper FS layer. */
    934  1.162     skrll 			return 0;
    935   1.66  augustss 
    936   1.26  augustss 		case FIOASYNC:
    937  1.184        ad 			mutex_enter(&proc_lock);
    938   1.26  augustss 			if (*(int *)data)
    939   1.84  christos 				usb_async_proc = l->l_proc;
    940   1.26  augustss 			else
    941  1.186     skrll 				usb_async_proc = NULL;
    942  1.184        ad 			mutex_exit(&proc_lock);
    943  1.162     skrll 			return 0;
    944   1.26  augustss 
    945   1.26  augustss 		default:
    946  1.162     skrll 			return EINVAL;
    947   1.26  augustss 		}
    948   1.26  augustss 	}
    949   1.26  augustss 
    950  1.109  drochner 	sc = device_lookup_private(&usb_cd, unit);
    951    1.1  augustss 
    952   1.22  augustss 	if (sc->sc_dying)
    953  1.162     skrll 		return EIO;
    954   1.22  augustss 
    955  1.163     skrll 	int error = 0;
    956    1.1  augustss 	switch (cmd) {
    957    1.1  augustss #ifdef USB_DEBUG
    958    1.1  augustss 	case USB_SETDEBUG:
    959   1.69  augustss 		if (!(flag & FWRITE))
    960  1.162     skrll 			return EBADF;
    961   1.30  augustss 		usbdebug  = ((*(int *)data) & 0x000000ff);
    962    1.1  augustss 		break;
    963   1.56  augustss #endif /* USB_DEBUG */
    964    1.1  augustss 	case USB_REQUEST:
    965    1.1  augustss 	{
    966    1.1  augustss 		struct usb_ctl_request *ur = (void *)data;
    967   1.68  christos 		int len = UGETW(ur->ucr_request.wLength);
    968    1.1  augustss 		struct iovec iov;
    969    1.1  augustss 		struct uio uio;
    970    1.1  augustss 		void *ptr = 0;
    971   1.68  christos 		int addr = ur->ucr_addr;
    972   1.29  augustss 		usbd_status err;
    973   1.69  augustss 
    974  1.163     skrll 		if (!(flag & FWRITE)) {
    975  1.163     skrll 			error = EBADF;
    976  1.163     skrll 			goto fail;
    977  1.163     skrll 		}
    978    1.1  augustss 
    979  1.168  pgoyette 		DPRINTF("USB_REQUEST addr=%jd len=%jd", addr, len, 0, 0);
    980  1.163     skrll 		if (len < 0 || len > 32768) {
    981  1.163     skrll 			error = EINVAL;
    982  1.163     skrll 			goto fail;
    983  1.163     skrll 		}
    984  1.165     skrll 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
    985  1.165     skrll 			error = EINVAL;
    986  1.165     skrll 			goto fail;
    987  1.165     skrll 		}
    988  1.165     skrll 		size_t dindex = usb_addr2dindex(addr);
    989  1.165     skrll 		if (sc->sc_bus->ub_devices[dindex] == NULL) {
    990  1.163     skrll 			error = EINVAL;
    991  1.163     skrll 			goto fail;
    992  1.163     skrll 		}
    993    1.1  augustss 		if (len != 0) {
    994   1.96  christos 			iov.iov_base = (void *)ur->ucr_data;
    995    1.1  augustss 			iov.iov_len = len;
    996    1.1  augustss 			uio.uio_iov = &iov;
    997    1.1  augustss 			uio.uio_iovcnt = 1;
    998    1.1  augustss 			uio.uio_resid = len;
    999    1.1  augustss 			uio.uio_offset = 0;
   1000    1.1  augustss 			uio.uio_rw =
   1001   1.68  christos 				ur->ucr_request.bmRequestType & UT_READ ?
   1002    1.1  augustss 				UIO_READ : UIO_WRITE;
   1003   1.85      yamt 			uio.uio_vmspace = l->l_proc->p_vmspace;
   1004  1.162     skrll 			ptr = kmem_alloc(len, KM_SLEEP);
   1005    1.1  augustss 			if (uio.uio_rw == UIO_WRITE) {
   1006    1.1  augustss 				error = uiomove(ptr, len, &uio);
   1007    1.1  augustss 				if (error)
   1008    1.1  augustss 					goto ret;
   1009    1.1  augustss 			}
   1010    1.1  augustss 		}
   1011  1.165     skrll 		err = usbd_do_request_flags(sc->sc_bus->ub_devices[dindex],
   1012   1.68  christos 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
   1013   1.67  augustss 			  USBD_DEFAULT_TIMEOUT);
   1014   1.29  augustss 		if (err) {
   1015    1.1  augustss 			error = EIO;
   1016    1.1  augustss 			goto ret;
   1017    1.1  augustss 		}
   1018  1.108        ws 		if (len > ur->ucr_actlen)
   1019  1.108        ws 			len = ur->ucr_actlen;
   1020    1.1  augustss 		if (len != 0) {
   1021    1.1  augustss 			if (uio.uio_rw == UIO_READ) {
   1022    1.1  augustss 				error = uiomove(ptr, len, &uio);
   1023    1.1  augustss 				if (error)
   1024    1.1  augustss 					goto ret;
   1025    1.1  augustss 			}
   1026    1.1  augustss 		}
   1027    1.1  augustss 	ret:
   1028  1.162     skrll 		if (ptr) {
   1029  1.162     skrll 			len = UGETW(ur->ucr_request.wLength);
   1030  1.162     skrll 			kmem_free(ptr, len);
   1031  1.162     skrll 		}
   1032  1.166     skrll 		break;
   1033    1.1  augustss 	}
   1034    1.1  augustss 
   1035    1.1  augustss 	case USB_DEVICEINFO:
   1036   1.93  christos 	{
   1037  1.162     skrll 		struct usbd_device *dev;
   1038   1.93  christos 		struct usb_device_info *di = (void *)data;
   1039   1.93  christos 		int addr = di->udi_addr;
   1040   1.93  christos 
   1041  1.163     skrll 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
   1042  1.163     skrll 			error = EINVAL;
   1043  1.163     skrll 			goto fail;
   1044  1.163     skrll 		}
   1045  1.165     skrll 		size_t dindex = usb_addr2dindex(addr);
   1046  1.165     skrll 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
   1047  1.163     skrll 			error = ENXIO;
   1048  1.163     skrll 			goto fail;
   1049  1.163     skrll 		}
   1050   1.93  christos 		usbd_fill_deviceinfo(dev, di, 1);
   1051   1.93  christos 		break;
   1052   1.93  christos 	}
   1053   1.93  christos 
   1054   1.92     pavel 	case USB_DEVICEINFO_OLD:
   1055    1.1  augustss 	{
   1056  1.162     skrll 		struct usbd_device *dev;
   1057   1.93  christos 		struct usb_device_info_old *di = (void *)data;
   1058   1.93  christos 		int addr = di->udi_addr;
   1059    1.1  augustss 
   1060  1.163     skrll 		if (addr < 1 || addr >= USB_MAX_DEVICES) {
   1061  1.163     skrll 			error = EINVAL;
   1062  1.163     skrll 			goto fail;
   1063  1.163     skrll 		}
   1064  1.165     skrll 		size_t dindex = usb_addr2dindex(addr);
   1065  1.165     skrll 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
   1066  1.163     skrll 			error = ENXIO;
   1067  1.163     skrll 			goto fail;
   1068  1.163     skrll 		}
   1069  1.178  pgoyette 		MODULE_HOOK_CALL(usb_subr_fill_30_hook,
   1070  1.175  pgoyette 		    (dev, di, 1, usbd_devinfo_vp, usbd_printBCD),
   1071  1.175  pgoyette 		    enosys(), error);
   1072  1.175  pgoyette 		if (error == ENOSYS)
   1073  1.175  pgoyette 			error = EINVAL;
   1074  1.175  pgoyette 		if (error)
   1075  1.175  pgoyette 			goto fail;
   1076  1.177       mrg 		break;
   1077    1.1  augustss 	}
   1078    1.2  augustss 
   1079    1.2  augustss 	case USB_DEVICESTATS:
   1080  1.162     skrll 		*(struct usb_device_stats *)data = sc->sc_bus->ub_stats;
   1081    1.2  augustss 		break;
   1082    1.1  augustss 
   1083    1.1  augustss 	default:
   1084  1.163     skrll 		error = EINVAL;
   1085    1.1  augustss 	}
   1086  1.163     skrll 
   1087  1.163     skrll fail:
   1088  1.163     skrll 
   1089  1.168  pgoyette 	DPRINTF("... done (error = %jd)", error, 0, 0, 0);
   1090  1.163     skrll 
   1091  1.163     skrll 	return error;
   1092    1.1  augustss }
   1093    1.1  augustss 
   1094    1.1  augustss int
   1095   1.84  christos usbpoll(dev_t dev, int events, struct lwp *l)
   1096    1.1  augustss {
   1097  1.130       mrg 	int revents, mask;
   1098    1.1  augustss 
   1099   1.30  augustss 	if (minor(dev) == USB_DEV_MINOR) {
   1100   1.30  augustss 		revents = 0;
   1101   1.30  augustss 		mask = POLLIN | POLLRDNORM;
   1102   1.66  augustss 
   1103  1.130       mrg 		mutex_enter(&usb_event_lock);
   1104   1.30  augustss 		if (events & mask && usb_nevents > 0)
   1105   1.30  augustss 			revents |= events & mask;
   1106   1.30  augustss 		if (revents == 0 && events & mask)
   1107   1.84  christos 			selrecord(l, &usb_selevent);
   1108  1.130       mrg 		mutex_exit(&usb_event_lock);
   1109   1.66  augustss 
   1110  1.162     skrll 		return revents;
   1111   1.30  augustss 	} else {
   1112  1.162     skrll 		return 0;
   1113    1.1  augustss 	}
   1114    1.1  augustss }
   1115    1.1  augustss 
   1116   1.74  jdolecek static void
   1117   1.74  jdolecek filt_usbrdetach(struct knote *kn)
   1118   1.74  jdolecek {
   1119   1.74  jdolecek 
   1120  1.130       mrg 	mutex_enter(&usb_event_lock);
   1121  1.188   thorpej 	selremove_knote(&usb_selevent, kn);
   1122  1.130       mrg 	mutex_exit(&usb_event_lock);
   1123   1.74  jdolecek }
   1124   1.74  jdolecek 
   1125   1.74  jdolecek static int
   1126   1.91  christos filt_usbread(struct knote *kn, long hint)
   1127   1.74  jdolecek {
   1128   1.74  jdolecek 
   1129   1.74  jdolecek 	if (usb_nevents == 0)
   1130  1.162     skrll 		return 0;
   1131   1.74  jdolecek 
   1132   1.74  jdolecek 	kn->kn_data = sizeof(struct usb_event);
   1133  1.162     skrll 	return 1;
   1134   1.74  jdolecek }
   1135   1.74  jdolecek 
   1136  1.167      maya static const struct filterops usbread_filtops = {
   1137  1.167      maya 	.f_isfd = 1,
   1138  1.167      maya 	.f_attach = NULL,
   1139  1.167      maya 	.f_detach = filt_usbrdetach,
   1140  1.167      maya 	.f_event = filt_usbread,
   1141  1.167      maya };
   1142   1.74  jdolecek 
   1143   1.74  jdolecek int
   1144   1.74  jdolecek usbkqfilter(dev_t dev, struct knote *kn)
   1145   1.74  jdolecek {
   1146   1.74  jdolecek 
   1147   1.74  jdolecek 	switch (kn->kn_filter) {
   1148   1.74  jdolecek 	case EVFILT_READ:
   1149   1.74  jdolecek 		if (minor(dev) != USB_DEV_MINOR)
   1150  1.162     skrll 			return 1;
   1151   1.74  jdolecek 		kn->kn_fop = &usbread_filtops;
   1152   1.74  jdolecek 		break;
   1153   1.74  jdolecek 
   1154   1.74  jdolecek 	default:
   1155  1.162     skrll 		return EINVAL;
   1156   1.74  jdolecek 	}
   1157   1.74  jdolecek 
   1158   1.74  jdolecek 	kn->kn_hook = NULL;
   1159   1.74  jdolecek 
   1160  1.130       mrg 	mutex_enter(&usb_event_lock);
   1161  1.188   thorpej 	selrecord_knote(&usb_selevent, kn);
   1162  1.130       mrg 	mutex_exit(&usb_event_lock);
   1163   1.74  jdolecek 
   1164  1.162     skrll 	return 0;
   1165   1.74  jdolecek }
   1166   1.74  jdolecek 
   1167   1.25  augustss /* Explore device tree from the root. */
   1168   1.51  augustss Static void
   1169  1.109  drochner usb_discover(struct usb_softc *sc)
   1170    1.1  augustss {
   1171  1.172       mrg 	struct usbd_bus *bus = sc->sc_bus;
   1172   1.51  augustss 
   1173  1.162     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1174  1.162     skrll 
   1175  1.195  riastrad 	KASSERT(KERNEL_LOCKED_P());
   1176  1.172       mrg 	KASSERT(mutex_owned(bus->ub_lock));
   1177  1.130       mrg 
   1178   1.51  augustss 	if (usb_noexplore > 1)
   1179   1.51  augustss 		return;
   1180  1.172       mrg 
   1181   1.66  augustss 	/*
   1182   1.25  augustss 	 * We need mutual exclusion while traversing the device tree,
   1183   1.25  augustss 	 * but this is guaranteed since this function is only called
   1184   1.25  augustss 	 * from the event thread for the controller.
   1185  1.130       mrg 	 *
   1186  1.172       mrg 	 * Also, we now have bus->ub_lock held, and in combination
   1187  1.172       mrg 	 * with ub_exploring, avoids interferring with polling.
   1188   1.25  augustss 	 */
   1189  1.183  riastrad 	SDT_PROBE1(usb, kernel, bus, discover__start,  bus);
   1190  1.172       mrg 	while (bus->ub_needsexplore && !sc->sc_dying) {
   1191  1.172       mrg 		bus->ub_needsexplore = 0;
   1192  1.162     skrll 		mutex_exit(sc->sc_bus->ub_lock);
   1193  1.183  riastrad 		SDT_PROBE1(usb, kernel, bus, explore__start,  bus);
   1194  1.172       mrg 		bus->ub_roothub->ud_hub->uh_explore(bus->ub_roothub);
   1195  1.183  riastrad 		SDT_PROBE1(usb, kernel, bus, explore__done,  bus);
   1196  1.172       mrg 		mutex_enter(bus->ub_lock);
   1197   1.30  augustss 	}
   1198  1.183  riastrad 	SDT_PROBE1(usb, kernel, bus, discover__done,  bus);
   1199    1.1  augustss }
   1200    1.1  augustss 
   1201    1.1  augustss void
   1202  1.162     skrll usb_needs_explore(struct usbd_device *dev)
   1203    1.1  augustss {
   1204  1.162     skrll 
   1205  1.162     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1206  1.183  riastrad 	SDT_PROBE1(usb, kernel, bus, needs__explore,  dev->ud_bus);
   1207  1.162     skrll 
   1208  1.162     skrll 	mutex_enter(dev->ud_bus->ub_lock);
   1209  1.162     skrll 	dev->ud_bus->ub_needsexplore = 1;
   1210  1.162     skrll 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
   1211  1.162     skrll 	mutex_exit(dev->ud_bus->ub_lock);
   1212    1.1  augustss }
   1213    1.7  augustss 
   1214   1.81      joff void
   1215  1.162     skrll usb_needs_reattach(struct usbd_device *dev)
   1216   1.81      joff {
   1217  1.162     skrll 
   1218  1.162     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1219  1.183  riastrad 	SDT_PROBE1(usb, kernel, bus, needs__reattach,  dev->ud_bus);
   1220  1.162     skrll 
   1221  1.162     skrll 	mutex_enter(dev->ud_bus->ub_lock);
   1222  1.162     skrll 	dev->ud_powersrc->up_reattach = 1;
   1223  1.162     skrll 	dev->ud_bus->ub_needsexplore = 1;
   1224  1.162     skrll 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
   1225  1.162     skrll 	mutex_exit(dev->ud_bus->ub_lock);
   1226   1.81      joff }
   1227   1.81      joff 
   1228  1.130       mrg /* Called at with usb_event_lock held. */
   1229   1.26  augustss int
   1230   1.45  augustss usb_get_next_event(struct usb_event *ue)
   1231   1.26  augustss {
   1232   1.26  augustss 	struct usb_event_q *ueq;
   1233   1.26  augustss 
   1234  1.130       mrg 	KASSERT(mutex_owned(&usb_event_lock));
   1235  1.130       mrg 
   1236   1.26  augustss 	if (usb_nevents <= 0)
   1237  1.162     skrll 		return 0;
   1238   1.26  augustss 	ueq = SIMPLEQ_FIRST(&usb_events);
   1239   1.65  augustss #ifdef DIAGNOSTIC
   1240   1.65  augustss 	if (ueq == NULL) {
   1241   1.65  augustss 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
   1242   1.65  augustss 		usb_nevents = 0;
   1243  1.162     skrll 		return 0;
   1244   1.65  augustss 	}
   1245   1.65  augustss #endif
   1246   1.83  drochner 	if (ue)
   1247   1.83  drochner 		*ue = ueq->ue;
   1248   1.71     lukem 	SIMPLEQ_REMOVE_HEAD(&usb_events, next);
   1249   1.87  christos 	usb_free_event((struct usb_event *)(void *)ueq);
   1250   1.26  augustss 	usb_nevents--;
   1251  1.162     skrll 	return 1;
   1252   1.26  augustss }
   1253   1.26  augustss 
   1254   1.26  augustss void
   1255  1.162     skrll usbd_add_dev_event(int type, struct usbd_device *udev)
   1256   1.38  augustss {
   1257   1.87  christos 	struct usb_event *ue = usb_alloc_event();
   1258   1.38  augustss 
   1259  1.161     skrll 	usbd_fill_deviceinfo(udev, &ue->u.ue_device, false);
   1260   1.87  christos 	usb_add_event(type, ue);
   1261   1.38  augustss }
   1262   1.38  augustss 
   1263   1.38  augustss void
   1264  1.162     skrll usbd_add_drv_event(int type, struct usbd_device *udev, device_t dev)
   1265   1.38  augustss {
   1266   1.87  christos 	struct usb_event *ue = usb_alloc_event();
   1267   1.87  christos 
   1268  1.162     skrll 	ue->u.ue_driver.ue_cookie = udev->ud_cookie;
   1269  1.123    dyoung 	strncpy(ue->u.ue_driver.ue_devname, device_xname(dev),
   1270  1.162     skrll 	    sizeof(ue->u.ue_driver.ue_devname));
   1271   1.87  christos 	usb_add_event(type, ue);
   1272   1.87  christos }
   1273   1.87  christos 
   1274   1.87  christos Static struct usb_event *
   1275   1.87  christos usb_alloc_event(void)
   1276   1.87  christos {
   1277   1.87  christos 	/* Yes, this is right; we allocate enough so that we can use it later */
   1278  1.162     skrll 	return kmem_zalloc(sizeof(struct usb_event_q), KM_SLEEP);
   1279   1.87  christos }
   1280   1.38  augustss 
   1281   1.87  christos Static void
   1282   1.87  christos usb_free_event(struct usb_event *uep)
   1283   1.87  christos {
   1284  1.162     skrll 	kmem_free(uep, sizeof(struct usb_event_q));
   1285   1.38  augustss }
   1286   1.38  augustss 
   1287   1.42  augustss Static void
   1288   1.45  augustss usb_add_event(int type, struct usb_event *uep)
   1289   1.26  augustss {
   1290   1.26  augustss 	struct usb_event_q *ueq;
   1291   1.26  augustss 	struct timeval thetime;
   1292   1.26  augustss 
   1293  1.162     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1294  1.162     skrll 
   1295   1.38  augustss 	microtime(&thetime);
   1296  1.130       mrg 	/* Don't want to wait here with usb_event_lock held */
   1297   1.87  christos 	ueq = (struct usb_event_q *)(void *)uep;
   1298   1.38  augustss 	ueq->ue = *uep;
   1299   1.38  augustss 	ueq->ue.ue_type = type;
   1300   1.38  augustss 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
   1301  1.183  riastrad 	SDT_PROBE1(usb, kernel, event, add,  uep);
   1302   1.38  augustss 
   1303  1.130       mrg 	mutex_enter(&usb_event_lock);
   1304   1.26  augustss 	if (++usb_nevents >= USB_MAX_EVENTS) {
   1305   1.26  augustss 		/* Too many queued events, drop an old one. */
   1306  1.162     skrll 		DPRINTF("event dropped", 0, 0, 0, 0);
   1307  1.183  riastrad #ifdef KDTRACE_HOOKS
   1308  1.183  riastrad 		struct usb_event oue;
   1309  1.183  riastrad 		if (usb_get_next_event(&oue))
   1310  1.183  riastrad 			SDT_PROBE1(usb, kernel, event, drop,  &oue);
   1311  1.183  riastrad #else
   1312  1.183  riastrad 		usb_get_next_event(NULL);
   1313  1.183  riastrad #endif
   1314   1.26  augustss 	}
   1315   1.26  augustss 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
   1316  1.130       mrg 	cv_signal(&usb_event_cv);
   1317  1.107     rmind 	selnotify(&usb_selevent, 0, 0);
   1318   1.94        ad 	if (usb_async_proc != NULL) {
   1319  1.130       mrg 		kpreempt_disable();
   1320  1.112        ad 		softint_schedule(usb_async_sih);
   1321  1.130       mrg 		kpreempt_enable();
   1322   1.94        ad 	}
   1323  1.130       mrg 	mutex_exit(&usb_event_lock);
   1324   1.39  augustss }
   1325   1.60  augustss 
   1326  1.112        ad Static void
   1327  1.112        ad usb_async_intr(void *cookie)
   1328  1.112        ad {
   1329  1.112        ad 	proc_t *proc;
   1330  1.112        ad 
   1331  1.184        ad 	mutex_enter(&proc_lock);
   1332  1.112        ad 	if ((proc = usb_async_proc) != NULL)
   1333  1.112        ad 		psignal(proc, SIGIO);
   1334  1.184        ad 	mutex_exit(&proc_lock);
   1335  1.112        ad }
   1336  1.112        ad 
   1337  1.130       mrg Static void
   1338  1.130       mrg usb_soft_intr(void *arg)
   1339  1.130       mrg {
   1340  1.162     skrll 	struct usbd_bus *bus = arg;
   1341  1.130       mrg 
   1342  1.162     skrll 	mutex_enter(bus->ub_lock);
   1343  1.162     skrll 	bus->ub_methods->ubm_softint(bus);
   1344  1.162     skrll 	mutex_exit(bus->ub_lock);
   1345  1.130       mrg }
   1346  1.130       mrg 
   1347   1.39  augustss void
   1348  1.162     skrll usb_schedsoftintr(struct usbd_bus *bus)
   1349   1.39  augustss {
   1350  1.130       mrg 
   1351  1.180       mrg 	USBHIST_FUNC();
   1352  1.180       mrg 	USBHIST_CALLARGS(usbdebug, "polling=%jd", bus->ub_usepolling, 0, 0, 0);
   1353  1.130       mrg 
   1354  1.173       mrg 	/* In case the bus never finished setting up. */
   1355  1.173       mrg 	if (__predict_false(bus->ub_soft == NULL))
   1356  1.173       mrg 		return;
   1357  1.173       mrg 
   1358  1.162     skrll 	if (bus->ub_usepolling) {
   1359  1.162     skrll 		bus->ub_methods->ubm_softint(bus);
   1360   1.49  augustss 	} else {
   1361  1.130       mrg 		kpreempt_disable();
   1362  1.162     skrll 		softint_schedule(bus->ub_soft);
   1363  1.130       mrg 		kpreempt_enable();
   1364   1.49  augustss 	}
   1365   1.26  augustss }
   1366   1.26  augustss 
   1367    1.7  augustss int
   1368  1.106    dyoung usb_activate(device_t self, enum devact act)
   1369    1.7  augustss {
   1370  1.106    dyoung 	struct usb_softc *sc = device_private(self);
   1371   1.22  augustss 
   1372   1.22  augustss 	switch (act) {
   1373   1.22  augustss 	case DVACT_DEACTIVATE:
   1374   1.22  augustss 		sc->sc_dying = 1;
   1375  1.119    dyoung 		return 0;
   1376  1.119    dyoung 	default:
   1377  1.119    dyoung 		return EOPNOTSUPP;
   1378   1.22  augustss 	}
   1379   1.13  augustss }
   1380    1.7  augustss 
   1381  1.106    dyoung void
   1382  1.106    dyoung usb_childdet(device_t self, device_t child)
   1383  1.106    dyoung {
   1384  1.114  drochner 	int i;
   1385  1.106    dyoung 	struct usb_softc *sc = device_private(self);
   1386  1.106    dyoung 	struct usbd_device *dev;
   1387  1.106    dyoung 
   1388  1.162     skrll 	if ((dev = sc->sc_port.up_dev) == NULL || dev->ud_subdevlen == 0)
   1389  1.106    dyoung 		return;
   1390  1.106    dyoung 
   1391  1.162     skrll 	for (i = 0; i < dev->ud_subdevlen; i++)
   1392  1.162     skrll 		if (dev->ud_subdevs[i] == child)
   1393  1.162     skrll 			dev->ud_subdevs[i] = NULL;
   1394  1.106    dyoung }
   1395  1.106    dyoung 
   1396   1.13  augustss int
   1397  1.106    dyoung usb_detach(device_t self, int flags)
   1398   1.13  augustss {
   1399  1.106    dyoung 	struct usb_softc *sc = device_private(self);
   1400   1.87  christos 	struct usb_event *ue;
   1401  1.119    dyoung 	int rc;
   1402   1.22  augustss 
   1403  1.162     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1404   1.27  augustss 
   1405  1.119    dyoung 	/* Make all devices disconnect. */
   1406  1.162     skrll 	if (sc->sc_port.up_dev != NULL &&
   1407  1.119    dyoung 	    (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
   1408  1.119    dyoung 		return rc;
   1409  1.119    dyoung 
   1410  1.173       mrg 	if (sc->sc_pmf_registered)
   1411  1.173       mrg 		pmf_device_deregister(self);
   1412   1.99  kiyohara 	/* Kill off event thread. */
   1413  1.119    dyoung 	sc->sc_dying = 1;
   1414   1.99  kiyohara 	while (sc->sc_event_thread != NULL) {
   1415  1.162     skrll 		mutex_enter(sc->sc_bus->ub_lock);
   1416  1.162     skrll 		cv_signal(&sc->sc_bus->ub_needsexplore_cv);
   1417  1.162     skrll 		cv_timedwait(&sc->sc_bus->ub_needsexplore_cv,
   1418  1.162     skrll 		    sc->sc_bus->ub_lock, hz * 60);
   1419  1.162     skrll 		mutex_exit(sc->sc_bus->ub_lock);
   1420  1.162     skrll 	}
   1421  1.162     skrll 	DPRINTF("event thread dead", 0, 0, 0, 0);
   1422  1.162     skrll 
   1423  1.162     skrll 	if (sc->sc_bus->ub_soft != NULL) {
   1424  1.162     skrll 		softint_disestablish(sc->sc_bus->ub_soft);
   1425  1.162     skrll 		sc->sc_bus->ub_soft = NULL;
   1426   1.49  augustss 	}
   1427   1.38  augustss 
   1428   1.87  christos 	ue = usb_alloc_event();
   1429  1.109  drochner 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
   1430   1.87  christos 	usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
   1431   1.38  augustss 
   1432  1.162     skrll 	cv_destroy(&sc->sc_bus->ub_needsexplore_cv);
   1433  1.130       mrg 
   1434  1.162     skrll 	return 0;
   1435    1.7  augustss }
   1436