Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.96
      1  1.96  christos /*	$NetBSD: usb.c,v 1.96 2007/03/04 06:02:50 christos Exp $	*/
      2   1.1  augustss 
      3   1.1  augustss /*
      4  1.62  augustss  * Copyright (c) 1998, 2002 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.5  augustss  * Carlstedt Research & Technology.
     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  * 3. All advertising materials mentioning features or use of this software
     20   1.1  augustss  *    must display the following acknowledgement:
     21   1.1  augustss  *        This product includes software developed by the NetBSD
     22   1.1  augustss  *        Foundation, Inc. and its contributors.
     23   1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24   1.1  augustss  *    contributors may be used to endorse or promote products derived
     25   1.1  augustss  *    from this software without specific prior written permission.
     26   1.1  augustss  *
     27   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28   1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29   1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30   1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31   1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32   1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33   1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34   1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35   1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36   1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37   1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     38   1.1  augustss  */
     39   1.1  augustss 
     40   1.1  augustss /*
     41   1.8  augustss  * USB specifications and other documentation can be found at
     42  1.80       wiz  * http://www.usb.org/developers/docs/ and
     43  1.80       wiz  * http://www.usb.org/developers/devclass_docs/
     44   1.1  augustss  */
     45  1.55     lukem 
     46  1.55     lukem #include <sys/cdefs.h>
     47  1.96  christos __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.96 2007/03/04 06:02:50 christos Exp $");
     48  1.92     pavel 
     49  1.92     pavel #include "opt_compat_netbsd.h"
     50  1.76   dsainty 
     51  1.76   dsainty #include "ohci.h"
     52  1.76   dsainty #include "uhci.h"
     53   1.1  augustss 
     54   1.1  augustss #include <sys/param.h>
     55   1.1  augustss #include <sys/systm.h>
     56   1.1  augustss #include <sys/kernel.h>
     57   1.1  augustss #include <sys/malloc.h>
     58   1.1  augustss #include <sys/device.h>
     59  1.13  augustss #include <sys/kthread.h>
     60  1.30  augustss #include <sys/proc.h>
     61  1.22  augustss #include <sys/conf.h>
     62  1.69  augustss #include <sys/fcntl.h>
     63   1.1  augustss #include <sys/poll.h>
     64   1.1  augustss #include <sys/select.h>
     65  1.26  augustss #include <sys/vnode.h>
     66  1.26  augustss #include <sys/signalvar.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.1  augustss 
     72  1.26  augustss #define USB_DEV_MINOR 255
     73  1.26  augustss 
     74  1.20  augustss #include <machine/bus.h>
     75   1.7  augustss 
     76   1.1  augustss #include <dev/usb/usbdivar.h>
     77   1.1  augustss #include <dev/usb/usb_quirks.h>
     78   1.1  augustss 
     79   1.1  augustss #ifdef USB_DEBUG
     80  1.16  augustss #define DPRINTF(x)	if (usbdebug) logprintf x
     81  1.16  augustss #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     82   1.1  augustss int	usbdebug = 0;
     83  1.76   dsainty #if defined(UHCI_DEBUG) && NUHCI > 0
     84  1.76   dsainty extern int	uhcidebug;
     85  1.30  augustss #endif
     86  1.76   dsainty #if defined(OHCI_DEBUG) && NOHCI > 0
     87  1.76   dsainty extern int	ohcidebug;
     88  1.30  augustss #endif
     89  1.66  augustss /*
     90  1.34  augustss  * 0  - do usual exploration
     91  1.34  augustss  * 1  - do not use timeout exploration
     92  1.34  augustss  * >1 - do no exploration
     93  1.34  augustss  */
     94  1.21  augustss int	usb_noexplore = 0;
     95   1.1  augustss #else
     96   1.1  augustss #define DPRINTF(x)
     97   1.1  augustss #define DPRINTFN(n,x)
     98   1.1  augustss #endif
     99   1.1  augustss 
    100   1.1  augustss struct usb_softc {
    101  1.22  augustss 	USBBASEDEVICE	sc_dev;		/* base device */
    102   1.1  augustss 	usbd_bus_handle sc_bus;		/* USB controller */
    103   1.1  augustss 	struct usbd_port sc_port;	/* dummy port for root hub */
    104  1.25  augustss 
    105  1.64  augustss 	struct proc	*sc_event_thread;
    106  1.25  augustss 
    107  1.25  augustss 	char		sc_dying;
    108   1.1  augustss };
    109   1.1  augustss 
    110  1.90     joerg struct usb_taskq {
    111  1.90     joerg 	TAILQ_HEAD(, usb_task) tasks;
    112  1.90     joerg 	struct proc *task_thread_proc;
    113  1.90     joerg 	const char *name;
    114  1.90     joerg 	int taskcreated;	/* task thread exists. */
    115  1.90     joerg };
    116  1.90     joerg 
    117  1.90     joerg static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
    118  1.58  augustss 
    119  1.72   gehenna dev_type_open(usbopen);
    120  1.72   gehenna dev_type_close(usbclose);
    121  1.72   gehenna dev_type_read(usbread);
    122  1.72   gehenna dev_type_ioctl(usbioctl);
    123  1.72   gehenna dev_type_poll(usbpoll);
    124  1.74  jdolecek dev_type_kqfilter(usbkqfilter);
    125  1.72   gehenna 
    126  1.72   gehenna const struct cdevsw usb_cdevsw = {
    127  1.72   gehenna 	usbopen, usbclose, usbread, nowrite, usbioctl,
    128  1.88  christos 	nostop, notty, usbpoll, nommap, usbkqfilter, D_OTHER,
    129  1.72   gehenna };
    130   1.7  augustss 
    131  1.51  augustss Static void	usb_discover(void *);
    132  1.45  augustss Static void	usb_create_event_thread(void *);
    133  1.45  augustss Static void	usb_event_thread(void *);
    134  1.58  augustss Static void	usb_task_thread(void *);
    135   1.1  augustss 
    136  1.41  augustss #define USB_MAX_EVENTS 100
    137  1.26  augustss struct usb_event_q {
    138  1.26  augustss 	struct usb_event ue;
    139  1.26  augustss 	SIMPLEQ_ENTRY(usb_event_q) next;
    140  1.26  augustss };
    141  1.66  augustss Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    142  1.29  augustss 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    143  1.42  augustss Static int usb_nevents = 0;
    144  1.42  augustss Static struct selinfo usb_selevent;
    145  1.60  augustss Static usb_proc_ptr usb_async_proc;  /* process that wants USB SIGIO */
    146  1.42  augustss Static int usb_dev_open = 0;
    147  1.87  christos Static struct usb_event *usb_alloc_event(void);
    148  1.87  christos Static void usb_free_event(struct usb_event *);
    149  1.45  augustss Static void usb_add_event(int, struct usb_event *);
    150  1.26  augustss 
    151  1.45  augustss Static int usb_get_next_event(struct usb_event *);
    152  1.23  augustss 
    153  1.92     pavel #ifdef COMPAT_30
    154  1.92     pavel Static void usb_copy_old_devinfo(struct usb_device_info_old *, const struct usb_device_info *);
    155  1.92     pavel #endif
    156  1.92     pavel 
    157  1.42  augustss Static const char *usbrev_str[] = USBREV_STR;
    158  1.31  augustss 
    159  1.28  augustss USB_DECLARE_DRIVER(usb);
    160   1.1  augustss 
    161   1.7  augustss USB_MATCH(usb)
    162   1.1  augustss {
    163   1.1  augustss 	DPRINTF(("usbd_match\n"));
    164   1.7  augustss 	return (UMATCH_GENERIC);
    165   1.1  augustss }
    166   1.1  augustss 
    167   1.7  augustss USB_ATTACH(usb)
    168   1.1  augustss {
    169   1.1  augustss 	struct usb_softc *sc = (struct usb_softc *)self;
    170   1.1  augustss 	usbd_device_handle dev;
    171  1.29  augustss 	usbd_status err;
    172  1.31  augustss 	int usbrev;
    173  1.57  augustss 	int speed;
    174  1.87  christos 	struct usb_event *ue;
    175  1.66  augustss 
    176   1.1  augustss 	DPRINTF(("usbd_attach\n"));
    177  1.31  augustss 
    178   1.1  augustss 	sc->sc_bus = aux;
    179   1.1  augustss 	sc->sc_bus->usbctl = sc;
    180   1.1  augustss 	sc->sc_port.power = USB_MAX_POWER;
    181  1.31  augustss 
    182  1.31  augustss 	usbrev = sc->sc_bus->usbrev;
    183  1.32  augustss 	printf(": USB revision %s", usbrev_str[usbrev]);
    184  1.57  augustss 	switch (usbrev) {
    185  1.57  augustss 	case USBREV_1_0:
    186  1.57  augustss 	case USBREV_1_1:
    187  1.57  augustss 		speed = USB_SPEED_FULL;
    188  1.57  augustss 		break;
    189  1.57  augustss 	case USBREV_2_0:
    190  1.57  augustss 		speed = USB_SPEED_HIGH;
    191  1.57  augustss 		break;
    192  1.57  augustss 	default:
    193  1.31  augustss 		printf(", not supported\n");
    194  1.57  augustss 		sc->sc_dying = 1;
    195  1.31  augustss 		USB_ATTACH_ERROR_RETURN;
    196  1.32  augustss 	}
    197  1.32  augustss 	printf("\n");
    198  1.31  augustss 
    199  1.35  augustss 	/* Make sure not to use tsleep() if we are cold booting. */
    200  1.35  augustss 	if (cold)
    201  1.35  augustss 		sc->sc_bus->use_polling++;
    202  1.35  augustss 
    203  1.87  christos 	ue = usb_alloc_event();
    204  1.87  christos 	ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    205  1.87  christos 	usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
    206  1.38  augustss 
    207  1.49  augustss #ifdef USB_USE_SOFTINTR
    208  1.49  augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    209  1.49  augustss 	/* XXX we should have our own level */
    210  1.66  augustss 	sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
    211  1.49  augustss 	    sc->sc_bus->methods->soft_intr, sc->sc_bus);
    212  1.49  augustss 	if (sc->sc_bus->soft == NULL) {
    213  1.49  augustss 		printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
    214  1.49  augustss 		sc->sc_dying = 1;
    215  1.57  augustss 		USB_ATTACH_ERROR_RETURN;
    216  1.49  augustss 	}
    217  1.49  augustss #else
    218  1.70  augustss 	usb_callout_init(sc->sc_bus->softi);
    219  1.49  augustss #endif
    220  1.49  augustss #endif
    221  1.49  augustss 
    222  1.57  augustss 	err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
    223  1.29  augustss 		  &sc->sc_port);
    224  1.29  augustss 	if (!err) {
    225   1.1  augustss 		dev = sc->sc_port.device;
    226  1.29  augustss 		if (dev->hub == NULL) {
    227  1.22  augustss 			sc->sc_dying = 1;
    228  1.66  augustss 			printf("%s: root device is not a hub\n",
    229   1.7  augustss 			       USBDEVNAME(sc->sc_dev));
    230   1.7  augustss 			USB_ATTACH_ERROR_RETURN;
    231   1.1  augustss 		}
    232   1.1  augustss 		sc->sc_bus->root_hub = dev;
    233  1.24  augustss #if 1
    234  1.66  augustss 		/*
    235  1.24  augustss 		 * Turning this code off will delay attachment of USB devices
    236  1.24  augustss 		 * until the USB event thread is running, which means that
    237  1.24  augustss 		 * the keyboard will not work until after cold boot.
    238  1.24  augustss 		 */
    239  1.86   thorpej 		if (cold && (device_cfdata(&sc->sc_dev)->cf_flags & 1))
    240  1.24  augustss 			dev->hub->explore(sc->sc_bus->root_hub);
    241  1.24  augustss #endif
    242   1.1  augustss 	} else {
    243  1.66  augustss 		printf("%s: root hub problem, error=%d\n",
    244  1.66  augustss 		       USBDEVNAME(sc->sc_dev), err);
    245  1.22  augustss 		sc->sc_dying = 1;
    246   1.1  augustss 	}
    247  1.35  augustss 	if (cold)
    248  1.35  augustss 		sc->sc_bus->use_polling--;
    249   1.7  augustss 
    250  1.37   thorpej 	config_pending_incr();
    251  1.43  augustss 	usb_kthread_create(usb_create_event_thread, sc);
    252  1.28  augustss 
    253   1.7  augustss 	USB_ATTACH_SUCCESS_RETURN;
    254   1.1  augustss }
    255   1.1  augustss 
    256  1.90     joerg static const char *taskq_names[] = USB_TASKQ_NAMES;
    257  1.90     joerg 
    258  1.28  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    259  1.13  augustss void
    260  1.45  augustss usb_create_event_thread(void *arg)
    261  1.13  augustss {
    262  1.13  augustss 	struct usb_softc *sc = arg;
    263  1.90     joerg 	struct usb_taskq *taskq;
    264  1.90     joerg 	int i;
    265  1.13  augustss 
    266  1.43  augustss 	if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
    267  1.13  augustss 			   "%s", sc->sc_dev.dv_xname)) {
    268  1.13  augustss 		printf("%s: unable to create event thread for\n",
    269  1.13  augustss 		       sc->sc_dev.dv_xname);
    270  1.13  augustss 		panic("usb_create_event_thread");
    271  1.13  augustss 	}
    272  1.90     joerg 	for (i = 0; i < USB_NUM_TASKQS; i++) {
    273  1.90     joerg 		taskq = &usb_taskq[i];
    274  1.90     joerg 
    275  1.90     joerg 		if (taskq->taskcreated)
    276  1.90     joerg 			continue;
    277  1.90     joerg 
    278  1.90     joerg 		TAILQ_INIT(&taskq->tasks);
    279  1.90     joerg 		taskq->taskcreated = 1;
    280  1.90     joerg 		taskq->name = taskq_names[i];
    281  1.90     joerg 		if (usb_kthread_create1(usb_task_thread, taskq,
    282  1.90     joerg 					&taskq->task_thread_proc, taskq->name)) {
    283  1.90     joerg 			printf("unable to create task thread: %s\n", taskq->name);
    284  1.58  augustss 			panic("usb_create_event_thread task");
    285  1.58  augustss 		}
    286  1.58  augustss 	}
    287  1.13  augustss }
    288  1.13  augustss 
    289  1.52  augustss /*
    290  1.62  augustss  * Add a task to be performed by the task thread.  This function can be
    291  1.52  augustss  * called from any context and the task will be executed in a process
    292  1.52  augustss  * context ASAP.
    293  1.52  augustss  */
    294  1.13  augustss void
    295  1.91  christos usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
    296  1.51  augustss {
    297  1.90     joerg 	struct usb_taskq *taskq;
    298  1.51  augustss 	int s;
    299  1.51  augustss 
    300  1.90     joerg 	taskq = &usb_taskq[queue];
    301  1.51  augustss 	s = splusb();
    302  1.90     joerg 	if (task->queue == -1) {
    303  1.58  augustss 		DPRINTFN(2,("usb_add_task: task=%p\n", task));
    304  1.90     joerg 		TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
    305  1.90     joerg 		task->queue = queue;
    306  1.62  augustss 	} else {
    307  1.58  augustss 		DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
    308  1.62  augustss 	}
    309  1.90     joerg 	wakeup(&taskq->tasks);
    310  1.51  augustss 	splx(s);
    311  1.51  augustss }
    312  1.51  augustss 
    313  1.51  augustss void
    314  1.91  christos usb_rem_task(usbd_device_handle dev, struct usb_task *task)
    315  1.53  augustss {
    316  1.90     joerg 	struct usb_taskq *taskq;
    317  1.53  augustss 	int s;
    318  1.53  augustss 
    319  1.90     joerg 	taskq = &usb_taskq[task->queue];
    320  1.53  augustss 	s = splusb();
    321  1.90     joerg 	if (task->queue != -1) {
    322  1.90     joerg 		TAILQ_REMOVE(&taskq->tasks, task, next);
    323  1.90     joerg 		task->queue = -1;
    324  1.53  augustss 	}
    325  1.53  augustss 	splx(s);
    326  1.53  augustss }
    327  1.53  augustss 
    328  1.53  augustss void
    329  1.45  augustss usb_event_thread(void *arg)
    330  1.13  augustss {
    331  1.13  augustss 	struct usb_softc *sc = arg;
    332  1.13  augustss 
    333  1.27  augustss 	DPRINTF(("usb_event_thread: start\n"));
    334  1.40  augustss 
    335  1.60  augustss 	/*
    336  1.60  augustss 	 * In case this controller is a companion controller to an
    337  1.60  augustss 	 * EHCI controller we need to wait until the EHCI controller
    338  1.60  augustss 	 * has grabbed the port.
    339  1.64  augustss 	 * XXX It would be nicer to do this with a tsleep(), but I don't
    340  1.64  augustss 	 * know how to synchronize the creation of the threads so it
    341  1.64  augustss 	 * will work.
    342  1.60  augustss 	 */
    343  1.61  augustss 	usb_delay_ms(sc->sc_bus, 500);
    344  1.60  augustss 
    345  1.40  augustss 	/* Make sure first discover does something. */
    346  1.40  augustss 	sc->sc_bus->needs_explore = 1;
    347  1.51  augustss 	usb_discover(sc);
    348  1.51  augustss 	config_pending_decr();
    349  1.27  augustss 
    350  1.22  augustss 	while (!sc->sc_dying) {
    351  1.58  augustss #ifdef USB_DEBUG
    352  1.58  augustss 		if (usb_noexplore < 2)
    353  1.58  augustss #endif
    354  1.58  augustss 		usb_discover(sc);
    355  1.58  augustss #ifdef USB_DEBUG
    356  1.58  augustss 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    357  1.58  augustss 		    usb_noexplore ? 0 : hz * 60);
    358  1.58  augustss #else
    359  1.58  augustss 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    360  1.58  augustss 		    hz * 60);
    361  1.58  augustss #endif
    362  1.58  augustss 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    363  1.13  augustss 	}
    364  1.50  augustss 	sc->sc_event_thread = NULL;
    365  1.13  augustss 
    366  1.13  augustss 	/* In case parent is waiting for us to exit. */
    367  1.13  augustss 	wakeup(sc);
    368  1.13  augustss 
    369  1.27  augustss 	DPRINTF(("usb_event_thread: exit\n"));
    370  1.13  augustss 	kthread_exit(0);
    371  1.13  augustss }
    372  1.13  augustss 
    373  1.58  augustss void
    374  1.90     joerg usb_task_thread(void *arg)
    375  1.58  augustss {
    376  1.58  augustss 	struct usb_task *task;
    377  1.90     joerg 	struct usb_taskq *taskq;
    378  1.58  augustss 	int s;
    379  1.58  augustss 
    380  1.90     joerg 	taskq = arg;
    381  1.90     joerg 	DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
    382  1.58  augustss 
    383  1.58  augustss 	s = splusb();
    384  1.58  augustss 	for (;;) {
    385  1.90     joerg 		task = TAILQ_FIRST(&taskq->tasks);
    386  1.58  augustss 		if (task == NULL) {
    387  1.90     joerg 			tsleep(&taskq->tasks, PWAIT, "usbtsk", 0);
    388  1.90     joerg 			task = TAILQ_FIRST(&taskq->tasks);
    389  1.58  augustss 		}
    390  1.58  augustss 		DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
    391  1.58  augustss 		if (task != NULL) {
    392  1.90     joerg 			TAILQ_REMOVE(&taskq->tasks, task, next);
    393  1.90     joerg 			task->queue = -1;
    394  1.58  augustss 			splx(s);
    395  1.58  augustss 			task->fun(task->arg);
    396  1.58  augustss 			s = splusb();
    397  1.58  augustss 		}
    398  1.58  augustss 	}
    399  1.58  augustss }
    400  1.58  augustss 
    401   1.1  augustss int
    402  1.91  christos usbctlprint(void *aux, const char *pnp)
    403   1.1  augustss {
    404   1.1  augustss 	/* only "usb"es can attach to host controllers */
    405   1.1  augustss 	if (pnp)
    406  1.77   thorpej 		aprint_normal("usb at %s", pnp);
    407   1.1  augustss 
    408   1.1  augustss 	return (UNCONF);
    409   1.1  augustss }
    410  1.28  augustss #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    411   1.7  augustss 
    412   1.1  augustss int
    413  1.91  christos usbopen(dev_t dev, int flag, int mode, struct lwp *l)
    414   1.1  augustss {
    415  1.26  augustss 	int unit = minor(dev);
    416  1.26  augustss 	struct usb_softc *sc;
    417  1.26  augustss 
    418  1.26  augustss 	if (unit == USB_DEV_MINOR) {
    419  1.26  augustss 		if (usb_dev_open)
    420  1.26  augustss 			return (EBUSY);
    421  1.26  augustss 		usb_dev_open = 1;
    422  1.26  augustss 		usb_async_proc = 0;
    423  1.26  augustss 		return (0);
    424  1.26  augustss 	}
    425  1.26  augustss 
    426  1.26  augustss 	USB_GET_SC_OPEN(usb, unit, sc);
    427   1.1  augustss 
    428  1.22  augustss 	if (sc->sc_dying)
    429  1.22  augustss 		return (EIO);
    430   1.1  augustss 
    431   1.1  augustss 	return (0);
    432   1.1  augustss }
    433   1.1  augustss 
    434   1.1  augustss int
    435  1.45  augustss usbread(dev_t dev, struct uio *uio, int flag)
    436  1.26  augustss {
    437  1.92     pavel 	struct usb_event *ue;
    438  1.92     pavel #ifdef COMPAT_30
    439  1.93  christos 	struct usb_event_old *ueo = NULL;	/* XXXGCC */
    440  1.92     pavel #endif
    441  1.92     pavel 	int s, error, n, useold;
    442  1.26  augustss 
    443  1.26  augustss 	if (minor(dev) != USB_DEV_MINOR)
    444  1.26  augustss 		return (ENXIO);
    445  1.26  augustss 
    446  1.92     pavel 	useold = 0;
    447  1.92     pavel 	switch (uio->uio_resid) {
    448  1.92     pavel #ifdef COMPAT_30
    449  1.92     pavel 	case sizeof(struct usb_event_old):
    450  1.92     pavel 		ueo = malloc(sizeof(struct usb_event_old), M_USBDEV,
    451  1.92     pavel 			     M_WAITOK|M_ZERO);
    452  1.92     pavel 		useold = 1;
    453  1.92     pavel 		/* FALLTHRU */
    454  1.92     pavel #endif
    455  1.92     pavel 	case sizeof(struct usb_event):
    456  1.92     pavel 		ue = usb_alloc_event();
    457  1.92     pavel 		break;
    458  1.92     pavel 	default:
    459  1.26  augustss 		return (EINVAL);
    460  1.92     pavel 	}
    461  1.26  augustss 
    462  1.26  augustss 	error = 0;
    463  1.26  augustss 	s = splusb();
    464  1.26  augustss 	for (;;) {
    465  1.87  christos 		n = usb_get_next_event(ue);
    466  1.26  augustss 		if (n != 0)
    467  1.26  augustss 			break;
    468  1.26  augustss 		if (flag & IO_NDELAY) {
    469  1.26  augustss 			error = EWOULDBLOCK;
    470  1.26  augustss 			break;
    471  1.26  augustss 		}
    472  1.26  augustss 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
    473  1.26  augustss 		if (error)
    474  1.26  augustss 			break;
    475  1.26  augustss 	}
    476  1.26  augustss 	splx(s);
    477  1.92     pavel 	if (!error) {
    478  1.92     pavel #ifdef COMPAT_30
    479  1.92     pavel 		if (useold) { /* copy fields to old struct */
    480  1.92     pavel 			ueo->ue_type = ue->ue_type;
    481  1.92     pavel 			memcpy(&ueo->ue_time, &ue->ue_time,
    482  1.92     pavel 			      sizeof(struct timespec));
    483  1.92     pavel 			switch (ue->ue_type) {
    484  1.92     pavel 				case USB_EVENT_DEVICE_ATTACH:
    485  1.92     pavel 				case USB_EVENT_DEVICE_DETACH:
    486  1.92     pavel 					usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
    487  1.92     pavel 					break;
    488  1.92     pavel 
    489  1.92     pavel 				case USB_EVENT_CTRLR_ATTACH:
    490  1.92     pavel 				case USB_EVENT_CTRLR_DETACH:
    491  1.92     pavel 					ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
    492  1.92     pavel 					break;
    493  1.92     pavel 
    494  1.92     pavel 				case USB_EVENT_DRIVER_ATTACH:
    495  1.92     pavel 				case USB_EVENT_DRIVER_DETACH:
    496  1.92     pavel 					ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
    497  1.92     pavel 					memcpy(ueo->u.ue_driver.ue_devname,
    498  1.92     pavel 					       ue->u.ue_driver.ue_devname,
    499  1.92     pavel 					       sizeof(ue->u.ue_driver.ue_devname));
    500  1.92     pavel 					break;
    501  1.92     pavel 				default:
    502  1.92     pavel 					;
    503  1.92     pavel 			}
    504  1.92     pavel 
    505  1.92     pavel 			error = uiomove((void *)ueo, uio->uio_resid, uio);
    506  1.92     pavel 		} else
    507  1.92     pavel #endif
    508  1.92     pavel 			error = uiomove((void *)ue, uio->uio_resid, uio);
    509  1.92     pavel 	}
    510  1.87  christos 	usb_free_event(ue);
    511  1.92     pavel #ifdef COMPAT_30
    512  1.92     pavel 	if (useold)
    513  1.92     pavel 		free(ueo, M_USBDEV);
    514  1.92     pavel #endif
    515  1.26  augustss 
    516  1.26  augustss 	return (error);
    517  1.26  augustss }
    518  1.26  augustss 
    519  1.26  augustss int
    520  1.91  christos usbclose(dev_t dev, int flag, int mode,
    521  1.91  christos     struct lwp *l)
    522   1.1  augustss {
    523  1.26  augustss 	int unit = minor(dev);
    524  1.26  augustss 
    525  1.26  augustss 	if (unit == USB_DEV_MINOR) {
    526  1.26  augustss 		usb_async_proc = 0;
    527  1.26  augustss 		usb_dev_open = 0;
    528  1.26  augustss 	}
    529  1.26  augustss 
    530   1.1  augustss 	return (0);
    531   1.1  augustss }
    532   1.1  augustss 
    533   1.1  augustss int
    534  1.96  christos usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
    535   1.1  augustss {
    536  1.26  augustss 	struct usb_softc *sc;
    537  1.28  augustss 	int unit = minor(devt);
    538  1.26  augustss 
    539  1.26  augustss 	if (unit == USB_DEV_MINOR) {
    540  1.26  augustss 		switch (cmd) {
    541  1.26  augustss 		case FIONBIO:
    542  1.26  augustss 			/* All handled in the upper FS layer. */
    543  1.26  augustss 			return (0);
    544  1.66  augustss 
    545  1.26  augustss 		case FIOASYNC:
    546  1.26  augustss 			if (*(int *)data)
    547  1.84  christos 				usb_async_proc = l->l_proc;
    548  1.26  augustss 			else
    549  1.26  augustss 				usb_async_proc = 0;
    550  1.26  augustss 			return (0);
    551  1.26  augustss 
    552  1.26  augustss 		default:
    553  1.26  augustss 			return (EINVAL);
    554  1.26  augustss 		}
    555  1.26  augustss 	}
    556  1.26  augustss 
    557  1.26  augustss 	USB_GET_SC(usb, unit, sc);
    558   1.1  augustss 
    559  1.22  augustss 	if (sc->sc_dying)
    560  1.22  augustss 		return (EIO);
    561  1.22  augustss 
    562   1.1  augustss 	switch (cmd) {
    563   1.1  augustss #ifdef USB_DEBUG
    564   1.1  augustss 	case USB_SETDEBUG:
    565  1.69  augustss 		if (!(flag & FWRITE))
    566  1.69  augustss 			return (EBADF);
    567  1.30  augustss 		usbdebug  = ((*(int *)data) & 0x000000ff);
    568  1.76   dsainty #if defined(UHCI_DEBUG) && NUHCI > 0
    569  1.30  augustss 		uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
    570  1.30  augustss #endif
    571  1.76   dsainty #if defined(OHCI_DEBUG) && NOHCI > 0
    572  1.30  augustss 		ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
    573  1.30  augustss #endif
    574   1.1  augustss 		break;
    575  1.56  augustss #endif /* USB_DEBUG */
    576   1.1  augustss 	case USB_REQUEST:
    577   1.1  augustss 	{
    578   1.1  augustss 		struct usb_ctl_request *ur = (void *)data;
    579  1.68  christos 		int len = UGETW(ur->ucr_request.wLength);
    580   1.1  augustss 		struct iovec iov;
    581   1.1  augustss 		struct uio uio;
    582   1.1  augustss 		void *ptr = 0;
    583  1.68  christos 		int addr = ur->ucr_addr;
    584  1.29  augustss 		usbd_status err;
    585   1.1  augustss 		int error = 0;
    586  1.69  augustss 
    587  1.69  augustss 		if (!(flag & FWRITE))
    588  1.69  augustss 			return (EBADF);
    589   1.1  augustss 
    590   1.9  augustss 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    591   1.1  augustss 		if (len < 0 || len > 32768)
    592   1.9  augustss 			return (EINVAL);
    593  1.66  augustss 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    594   1.1  augustss 		    sc->sc_bus->devices[addr] == 0)
    595   1.9  augustss 			return (EINVAL);
    596   1.1  augustss 		if (len != 0) {
    597  1.96  christos 			iov.iov_base = (void *)ur->ucr_data;
    598   1.1  augustss 			iov.iov_len = len;
    599   1.1  augustss 			uio.uio_iov = &iov;
    600   1.1  augustss 			uio.uio_iovcnt = 1;
    601   1.1  augustss 			uio.uio_resid = len;
    602   1.1  augustss 			uio.uio_offset = 0;
    603   1.1  augustss 			uio.uio_rw =
    604  1.68  christos 				ur->ucr_request.bmRequestType & UT_READ ?
    605   1.1  augustss 				UIO_READ : UIO_WRITE;
    606  1.85      yamt 			uio.uio_vmspace = l->l_proc->p_vmspace;
    607   1.1  augustss 			ptr = malloc(len, M_TEMP, M_WAITOK);
    608   1.1  augustss 			if (uio.uio_rw == UIO_WRITE) {
    609   1.1  augustss 				error = uiomove(ptr, len, &uio);
    610   1.1  augustss 				if (error)
    611   1.1  augustss 					goto ret;
    612   1.1  augustss 			}
    613   1.1  augustss 		}
    614  1.29  augustss 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
    615  1.68  christos 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
    616  1.67  augustss 			  USBD_DEFAULT_TIMEOUT);
    617  1.29  augustss 		if (err) {
    618   1.1  augustss 			error = EIO;
    619   1.1  augustss 			goto ret;
    620   1.1  augustss 		}
    621   1.1  augustss 		if (len != 0) {
    622   1.1  augustss 			if (uio.uio_rw == UIO_READ) {
    623   1.1  augustss 				error = uiomove(ptr, len, &uio);
    624   1.1  augustss 				if (error)
    625   1.1  augustss 					goto ret;
    626   1.1  augustss 			}
    627   1.1  augustss 		}
    628   1.1  augustss 	ret:
    629   1.1  augustss 		if (ptr)
    630   1.1  augustss 			free(ptr, M_TEMP);
    631   1.1  augustss 		return (error);
    632   1.1  augustss 	}
    633   1.1  augustss 
    634   1.1  augustss 	case USB_DEVICEINFO:
    635  1.93  christos 	{
    636  1.93  christos 		usbd_device_handle dev;
    637  1.93  christos 		struct usb_device_info *di = (void *)data;
    638  1.93  christos 		int addr = di->udi_addr;
    639  1.93  christos 
    640  1.93  christos 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    641  1.93  christos 			return EINVAL;
    642  1.93  christos 		if ((dev = sc->sc_bus->devices[addr]) == NULL)
    643  1.93  christos 			return ENXIO;
    644  1.93  christos 		usbd_fill_deviceinfo(dev, di, 1);
    645  1.93  christos 		break;
    646  1.93  christos 	}
    647  1.93  christos 
    648  1.92     pavel #ifdef COMPAT_30
    649  1.92     pavel 	case USB_DEVICEINFO_OLD:
    650   1.1  augustss 	{
    651  1.30  augustss 		usbd_device_handle dev;
    652  1.93  christos 		struct usb_device_info_old *di = (void *)data;
    653  1.93  christos 		int addr = di->udi_addr;
    654   1.1  augustss 
    655   1.1  augustss 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    656  1.93  christos 			return EINVAL;
    657  1.93  christos 		if ((dev = sc->sc_bus->devices[addr]) == NULL)
    658  1.93  christos 			return ENXIO;
    659  1.93  christos 		usbd_fill_deviceinfo_old(dev, di, 1);
    660   1.1  augustss 		break;
    661   1.1  augustss 	}
    662  1.93  christos #endif
    663   1.2  augustss 
    664   1.2  augustss 	case USB_DEVICESTATS:
    665   1.2  augustss 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    666   1.2  augustss 		break;
    667   1.1  augustss 
    668   1.1  augustss 	default:
    669  1.26  augustss 		return (EINVAL);
    670   1.1  augustss 	}
    671   1.1  augustss 	return (0);
    672   1.1  augustss }
    673   1.1  augustss 
    674   1.1  augustss int
    675  1.84  christos usbpoll(dev_t dev, int events, struct lwp *l)
    676   1.1  augustss {
    677  1.26  augustss 	int revents, mask, s;
    678   1.1  augustss 
    679  1.30  augustss 	if (minor(dev) == USB_DEV_MINOR) {
    680  1.30  augustss 		revents = 0;
    681  1.30  augustss 		mask = POLLIN | POLLRDNORM;
    682  1.66  augustss 
    683  1.30  augustss 		s = splusb();
    684  1.30  augustss 		if (events & mask && usb_nevents > 0)
    685  1.30  augustss 			revents |= events & mask;
    686  1.30  augustss 		if (revents == 0 && events & mask)
    687  1.84  christos 			selrecord(l, &usb_selevent);
    688  1.30  augustss 		splx(s);
    689  1.66  augustss 
    690  1.30  augustss 		return (revents);
    691  1.30  augustss 	} else {
    692  1.82        ws 		return (0);
    693   1.1  augustss 	}
    694   1.1  augustss }
    695   1.1  augustss 
    696  1.74  jdolecek static void
    697  1.74  jdolecek filt_usbrdetach(struct knote *kn)
    698  1.74  jdolecek {
    699  1.74  jdolecek 	int s;
    700  1.74  jdolecek 
    701  1.74  jdolecek 	s = splusb();
    702  1.75  christos 	SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
    703  1.74  jdolecek 	splx(s);
    704  1.74  jdolecek }
    705  1.74  jdolecek 
    706  1.74  jdolecek static int
    707  1.91  christos filt_usbread(struct knote *kn, long hint)
    708  1.74  jdolecek {
    709  1.74  jdolecek 
    710  1.74  jdolecek 	if (usb_nevents == 0)
    711  1.74  jdolecek 		return (0);
    712  1.74  jdolecek 
    713  1.74  jdolecek 	kn->kn_data = sizeof(struct usb_event);
    714  1.74  jdolecek 	return (1);
    715  1.74  jdolecek }
    716  1.74  jdolecek 
    717  1.74  jdolecek static const struct filterops usbread_filtops =
    718  1.74  jdolecek 	{ 1, NULL, filt_usbrdetach, filt_usbread };
    719  1.74  jdolecek 
    720  1.74  jdolecek int
    721  1.74  jdolecek usbkqfilter(dev_t dev, struct knote *kn)
    722  1.74  jdolecek {
    723  1.74  jdolecek 	struct klist *klist;
    724  1.74  jdolecek 	int s;
    725  1.74  jdolecek 
    726  1.74  jdolecek 	switch (kn->kn_filter) {
    727  1.74  jdolecek 	case EVFILT_READ:
    728  1.74  jdolecek 		if (minor(dev) != USB_DEV_MINOR)
    729  1.74  jdolecek 			return (1);
    730  1.75  christos 		klist = &usb_selevent.sel_klist;
    731  1.74  jdolecek 		kn->kn_fop = &usbread_filtops;
    732  1.74  jdolecek 		break;
    733  1.74  jdolecek 
    734  1.74  jdolecek 	default:
    735  1.74  jdolecek 		return (1);
    736  1.74  jdolecek 	}
    737  1.74  jdolecek 
    738  1.74  jdolecek 	kn->kn_hook = NULL;
    739  1.74  jdolecek 
    740  1.74  jdolecek 	s = splusb();
    741  1.74  jdolecek 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    742  1.74  jdolecek 	splx(s);
    743  1.74  jdolecek 
    744  1.74  jdolecek 	return (0);
    745  1.74  jdolecek }
    746  1.74  jdolecek 
    747  1.25  augustss /* Explore device tree from the root. */
    748  1.51  augustss Static void
    749  1.51  augustss usb_discover(void *v)
    750   1.1  augustss {
    751  1.51  augustss 	struct usb_softc *sc = v;
    752  1.51  augustss 
    753  1.51  augustss 	DPRINTFN(2,("usb_discover\n"));
    754  1.51  augustss #ifdef USB_DEBUG
    755  1.51  augustss 	if (usb_noexplore > 1)
    756  1.51  augustss 		return;
    757  1.51  augustss #endif
    758  1.66  augustss 	/*
    759  1.25  augustss 	 * We need mutual exclusion while traversing the device tree,
    760  1.25  augustss 	 * but this is guaranteed since this function is only called
    761  1.25  augustss 	 * from the event thread for the controller.
    762  1.25  augustss 	 */
    763  1.30  augustss 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
    764  1.13  augustss 		sc->sc_bus->needs_explore = 0;
    765  1.13  augustss 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    766  1.30  augustss 	}
    767   1.1  augustss }
    768   1.1  augustss 
    769   1.1  augustss void
    770  1.51  augustss usb_needs_explore(usbd_device_handle dev)
    771   1.1  augustss {
    772  1.51  augustss 	DPRINTFN(2,("usb_needs_explore\n"));
    773  1.51  augustss 	dev->bus->needs_explore = 1;
    774  1.58  augustss 	wakeup(&dev->bus->needs_explore);
    775   1.1  augustss }
    776   1.7  augustss 
    777  1.81      joff void
    778  1.81      joff usb_needs_reattach(usbd_device_handle dev)
    779  1.81      joff {
    780  1.81      joff 	DPRINTFN(2,("usb_needs_reattach\n"));
    781  1.81      joff 	dev->powersrc->reattach = 1;
    782  1.81      joff 	dev->bus->needs_explore = 1;
    783  1.81      joff 	wakeup(&dev->bus->needs_explore);
    784  1.81      joff }
    785  1.81      joff 
    786  1.26  augustss /* Called at splusb() */
    787  1.26  augustss int
    788  1.45  augustss usb_get_next_event(struct usb_event *ue)
    789  1.26  augustss {
    790  1.26  augustss 	struct usb_event_q *ueq;
    791  1.26  augustss 
    792  1.26  augustss 	if (usb_nevents <= 0)
    793  1.26  augustss 		return (0);
    794  1.26  augustss 	ueq = SIMPLEQ_FIRST(&usb_events);
    795  1.65  augustss #ifdef DIAGNOSTIC
    796  1.65  augustss 	if (ueq == NULL) {
    797  1.65  augustss 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
    798  1.65  augustss 		usb_nevents = 0;
    799  1.65  augustss 		return (0);
    800  1.65  augustss 	}
    801  1.65  augustss #endif
    802  1.83  drochner 	if (ue)
    803  1.83  drochner 		*ue = ueq->ue;
    804  1.71     lukem 	SIMPLEQ_REMOVE_HEAD(&usb_events, next);
    805  1.87  christos 	usb_free_event((struct usb_event *)(void *)ueq);
    806  1.26  augustss 	usb_nevents--;
    807  1.26  augustss 	return (1);
    808  1.26  augustss }
    809  1.26  augustss 
    810  1.26  augustss void
    811  1.45  augustss usbd_add_dev_event(int type, usbd_device_handle udev)
    812  1.38  augustss {
    813  1.87  christos 	struct usb_event *ue = usb_alloc_event();
    814  1.38  augustss 
    815  1.87  christos 	usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
    816  1.87  christos 	usb_add_event(type, ue);
    817  1.38  augustss }
    818  1.38  augustss 
    819  1.38  augustss void
    820  1.45  augustss usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
    821  1.38  augustss {
    822  1.87  christos 	struct usb_event *ue = usb_alloc_event();
    823  1.87  christos 
    824  1.87  christos 	ue->u.ue_driver.ue_cookie = udev->cookie;
    825  1.87  christos 	strncpy(ue->u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
    826  1.87  christos 	    sizeof ue->u.ue_driver.ue_devname);
    827  1.87  christos 	usb_add_event(type, ue);
    828  1.87  christos }
    829  1.87  christos 
    830  1.87  christos Static struct usb_event *
    831  1.87  christos usb_alloc_event(void)
    832  1.87  christos {
    833  1.87  christos 	/* Yes, this is right; we allocate enough so that we can use it later */
    834  1.87  christos 	return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO);
    835  1.87  christos }
    836  1.38  augustss 
    837  1.87  christos Static void
    838  1.87  christos usb_free_event(struct usb_event *uep)
    839  1.87  christos {
    840  1.87  christos 	free(uep, M_USBDEV);
    841  1.38  augustss }
    842  1.38  augustss 
    843  1.42  augustss Static void
    844  1.45  augustss usb_add_event(int type, struct usb_event *uep)
    845  1.26  augustss {
    846  1.26  augustss 	struct usb_event_q *ueq;
    847  1.26  augustss 	struct timeval thetime;
    848  1.26  augustss 	int s;
    849  1.26  augustss 
    850  1.38  augustss 	microtime(&thetime);
    851  1.38  augustss 	/* Don't want to wait here inside splusb() */
    852  1.87  christos 	ueq = (struct usb_event_q *)(void *)uep;
    853  1.38  augustss 	ueq->ue = *uep;
    854  1.38  augustss 	ueq->ue.ue_type = type;
    855  1.38  augustss 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
    856  1.38  augustss 
    857  1.26  augustss 	s = splusb();
    858  1.26  augustss 	if (++usb_nevents >= USB_MAX_EVENTS) {
    859  1.26  augustss 		/* Too many queued events, drop an old one. */
    860  1.26  augustss 		DPRINTFN(-1,("usb: event dropped\n"));
    861  1.83  drochner 		(void)usb_get_next_event(0);
    862  1.26  augustss 	}
    863  1.26  augustss 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
    864  1.26  augustss 	wakeup(&usb_events);
    865  1.74  jdolecek 	selnotify(&usb_selevent, 0);
    866  1.94        ad 	if (usb_async_proc != NULL) {
    867  1.94        ad 		mutex_enter(&proclist_mutex);
    868  1.26  augustss 		psignal(usb_async_proc, SIGIO);
    869  1.94        ad 		mutex_exit(&proclist_mutex);
    870  1.94        ad 	}
    871  1.26  augustss 	splx(s);
    872  1.39  augustss }
    873  1.60  augustss 
    874  1.39  augustss void
    875  1.49  augustss usb_schedsoftintr(usbd_bus_handle bus)
    876  1.39  augustss {
    877  1.54  augustss 	DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
    878  1.49  augustss #ifdef USB_USE_SOFTINTR
    879  1.49  augustss 	if (bus->use_polling) {
    880  1.49  augustss 		bus->methods->soft_intr(bus);
    881  1.49  augustss 	} else {
    882  1.49  augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    883  1.49  augustss 		softintr_schedule(bus->soft);
    884  1.49  augustss #else
    885  1.49  augustss 		if (!callout_pending(&bus->softi))
    886  1.49  augustss 			callout_reset(&bus->softi, 0, bus->methods->soft_intr,
    887  1.49  augustss 			    bus);
    888  1.49  augustss #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
    889  1.49  augustss 	}
    890  1.49  augustss #else
    891  1.39  augustss 	bus->methods->soft_intr(bus);
    892  1.56  augustss #endif /* USB_USE_SOFTINTR */
    893  1.26  augustss }
    894  1.26  augustss 
    895   1.7  augustss int
    896  1.45  augustss usb_activate(device_ptr_t self, enum devact act)
    897   1.7  augustss {
    898  1.22  augustss 	struct usb_softc *sc = (struct usb_softc *)self;
    899  1.25  augustss 	usbd_device_handle dev = sc->sc_port.device;
    900  1.25  augustss 	int i, rv = 0;
    901  1.22  augustss 
    902  1.22  augustss 	switch (act) {
    903  1.22  augustss 	case DVACT_ACTIVATE:
    904  1.22  augustss 		return (EOPNOTSUPP);
    905  1.22  augustss 
    906  1.22  augustss 	case DVACT_DEACTIVATE:
    907  1.22  augustss 		sc->sc_dying = 1;
    908  1.62  augustss 		if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
    909  1.25  augustss 			for (i = 0; dev->subdevs[i]; i++)
    910  1.25  augustss 				rv |= config_deactivate(dev->subdevs[i]);
    911  1.25  augustss 		}
    912  1.22  augustss 		break;
    913  1.22  augustss 	}
    914  1.22  augustss 	return (rv);
    915  1.13  augustss }
    916   1.7  augustss 
    917  1.13  augustss int
    918  1.91  christos usb_detach(device_ptr_t self, int flags)
    919  1.13  augustss {
    920  1.22  augustss 	struct usb_softc *sc = (struct usb_softc *)self;
    921  1.87  christos 	struct usb_event *ue;
    922  1.22  augustss 
    923  1.27  augustss 	DPRINTF(("usb_detach: start\n"));
    924  1.27  augustss 
    925  1.22  augustss 	sc->sc_dying = 1;
    926  1.22  augustss 
    927  1.22  augustss 	/* Make all devices disconnect. */
    928  1.62  augustss 	if (sc->sc_port.device != NULL)
    929  1.27  augustss 		usb_disconnect_port(&sc->sc_port, self);
    930  1.22  augustss 
    931  1.22  augustss 	/* Kill off event thread. */
    932  1.62  augustss 	if (sc->sc_event_thread != NULL) {
    933  1.58  augustss 		wakeup(&sc->sc_bus->needs_explore);
    934  1.22  augustss 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
    935  1.22  augustss 			printf("%s: event thread didn't die\n",
    936  1.22  augustss 			       USBDEVNAME(sc->sc_dev));
    937  1.27  augustss 		DPRINTF(("usb_detach: event thread dead\n"));
    938  1.22  augustss 	}
    939  1.22  augustss 
    940  1.49  augustss #ifdef USB_USE_SOFTINTR
    941  1.49  augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    942  1.49  augustss 	if (sc->sc_bus->soft != NULL) {
    943  1.49  augustss 		softintr_disestablish(sc->sc_bus->soft);
    944  1.49  augustss 		sc->sc_bus->soft = NULL;
    945  1.49  augustss 	}
    946  1.49  augustss #else
    947  1.49  augustss 	callout_stop(&sc->sc_bus->softi);
    948  1.49  augustss #endif
    949  1.49  augustss #endif
    950  1.38  augustss 
    951  1.87  christos 	ue = usb_alloc_event();
    952  1.87  christos 	ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    953  1.87  christos 	usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
    954  1.38  augustss 
    955   1.7  augustss 	return (0);
    956   1.7  augustss }
    957  1.92     pavel 
    958  1.92     pavel #ifdef COMPAT_30
    959  1.92     pavel Static void
    960  1.92     pavel usb_copy_old_devinfo(struct usb_device_info_old *uo,
    961  1.92     pavel 		     const struct usb_device_info *ue)
    962  1.92     pavel {
    963  1.92     pavel 	const unsigned char *p;
    964  1.92     pavel 	unsigned char *q;
    965  1.92     pavel 	int i, n;
    966  1.92     pavel 
    967  1.92     pavel 	uo->udi_bus = ue->udi_bus;
    968  1.92     pavel 	uo->udi_addr = ue->udi_addr;
    969  1.92     pavel 	uo->udi_cookie = ue->udi_cookie;
    970  1.92     pavel 	for (i = 0, p = (const unsigned char *)ue->udi_product,
    971  1.92     pavel 	     q = (unsigned char *)uo->udi_product;
    972  1.92     pavel 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
    973  1.92     pavel 		if (*p < 0x80)
    974  1.92     pavel 			q[i++] = *p;
    975  1.92     pavel 		else {
    976  1.92     pavel 			q[i++] = '?';
    977  1.92     pavel 			if ((*p & 0xe0) == 0xe0)
    978  1.92     pavel 				p++;
    979  1.92     pavel 			p++;
    980  1.92     pavel 		}
    981  1.92     pavel 	}
    982  1.92     pavel 	q[i] = 0;
    983  1.92     pavel 
    984  1.92     pavel 	for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
    985  1.92     pavel 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
    986  1.92     pavel 		if (* p < 0x80)
    987  1.92     pavel 			q[i++] = *p;
    988  1.92     pavel 		else {
    989  1.92     pavel 			q[i++] = '?';
    990  1.92     pavel 			p++;
    991  1.92     pavel 			if ((*p & 0xe0) == 0xe0)
    992  1.92     pavel 				p++;
    993  1.92     pavel 		}
    994  1.92     pavel 	}
    995  1.92     pavel 	q[i] = 0;
    996  1.92     pavel 
    997  1.92     pavel 	memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
    998  1.92     pavel 
    999  1.92     pavel 	uo->udi_productNo = ue->udi_productNo;
   1000  1.92     pavel 	uo->udi_vendorNo = ue->udi_vendorNo;
   1001  1.92     pavel 	uo->udi_releaseNo = ue->udi_releaseNo;
   1002  1.92     pavel 	uo->udi_class = ue->udi_class;
   1003  1.92     pavel 	uo->udi_subclass = ue->udi_subclass;
   1004  1.92     pavel 	uo->udi_protocol = ue->udi_protocol;
   1005  1.92     pavel 	uo->udi_config = ue->udi_config;
   1006  1.92     pavel 	uo->udi_speed = ue->udi_speed;
   1007  1.92     pavel 	uo->udi_power = ue->udi_power;
   1008  1.92     pavel 	uo->udi_nports = ue->udi_nports;
   1009  1.92     pavel 
   1010  1.92     pavel 	for (n=0; n<USB_MAX_DEVNAMES; n++)
   1011  1.92     pavel 		memcpy(uo->udi_devnames[n],
   1012  1.92     pavel 		       ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
   1013  1.92     pavel 	memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
   1014  1.92     pavel }
   1015  1.92     pavel #endif
   1016