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