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