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