usb.c revision 1.156.2.5 1 /* $NetBSD: usb.c,v 1.156.2.5 2015/03/19 17:26:43 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.5 2015/03/19 17:26:43 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/kmem.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 struct usbd_bus *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 struct usbd_device *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(struct usbd_device *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(struct usbd_device *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 = kmem_zalloc(sizeof(struct usb_event_old), KM_SLEEP);
577 useold = 1;
578 /* FALLTHRU */
579 #endif
580 case sizeof(struct usb_event):
581 ue = usb_alloc_event();
582 break;
583 default:
584 return EINVAL;
585 }
586
587 error = 0;
588 mutex_enter(&usb_event_lock);
589 for (;;) {
590 n = usb_get_next_event(ue);
591 if (n != 0)
592 break;
593 if (flag & IO_NDELAY) {
594 error = EWOULDBLOCK;
595 break;
596 }
597 error = cv_wait_sig(&usb_event_cv, &usb_event_lock);
598 if (error)
599 break;
600 }
601 mutex_exit(&usb_event_lock);
602 if (!error) {
603 #ifdef COMPAT_30
604 if (useold) { /* copy fields to old struct */
605 ueo->ue_type = ue->ue_type;
606 memcpy(&ueo->ue_time, &ue->ue_time,
607 sizeof(struct timespec));
608 switch (ue->ue_type) {
609 case USB_EVENT_DEVICE_ATTACH:
610 case USB_EVENT_DEVICE_DETACH:
611 usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
612 break;
613
614 case USB_EVENT_CTRLR_ATTACH:
615 case USB_EVENT_CTRLR_DETACH:
616 ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
617 break;
618
619 case USB_EVENT_DRIVER_ATTACH:
620 case USB_EVENT_DRIVER_DETACH:
621 ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
622 memcpy(ueo->u.ue_driver.ue_devname,
623 ue->u.ue_driver.ue_devname,
624 sizeof(ue->u.ue_driver.ue_devname));
625 break;
626 default:
627 ;
628 }
629
630 error = uiomove((void *)ueo, sizeof *ueo, uio);
631 } else
632 #endif
633 error = uiomove((void *)ue, sizeof *ue, uio);
634 }
635 usb_free_event(ue);
636 #ifdef COMPAT_30
637 if (useold)
638 kmem_free(ueo, sizeof(struct usb_event_old));
639 #endif
640
641 return error;
642 }
643
644 int
645 usbclose(dev_t dev, int flag, int mode,
646 struct lwp *l)
647 {
648 int unit = minor(dev);
649
650 if (unit == USB_DEV_MINOR) {
651 mutex_enter(proc_lock);
652 usb_async_proc = 0;
653 mutex_exit(proc_lock);
654 usb_dev_open = 0;
655 }
656
657 return 0;
658 }
659
660 int
661 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
662 {
663 struct usb_softc *sc;
664 int unit = minor(devt);
665
666 if (unit == USB_DEV_MINOR) {
667 switch (cmd) {
668 case FIONBIO:
669 /* All handled in the upper FS layer. */
670 return 0;
671
672 case FIOASYNC:
673 mutex_enter(proc_lock);
674 if (*(int *)data)
675 usb_async_proc = l->l_proc;
676 else
677 usb_async_proc = 0;
678 mutex_exit(proc_lock);
679 return 0;
680
681 default:
682 return EINVAL;
683 }
684 }
685
686 sc = device_lookup_private(&usb_cd, unit);
687
688 if (sc->sc_dying)
689 return EIO;
690
691 switch (cmd) {
692 #ifdef USB_DEBUG
693 case USB_SETDEBUG:
694 if (!(flag & FWRITE))
695 return EBADF;
696 usbdebug = ((*(int *)data) & 0x000000ff);
697 break;
698 #endif /* USB_DEBUG */
699 case USB_REQUEST:
700 {
701 struct usb_ctl_request *ur = (void *)data;
702 int len = UGETW(ur->ucr_request.wLength);
703 struct iovec iov;
704 struct uio uio;
705 void *ptr = 0;
706 int addr = ur->ucr_addr;
707 usbd_status err;
708 int error = 0;
709
710 if (!(flag & FWRITE))
711 return EBADF;
712
713 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
714 if (len < 0 || len > 32768)
715 return EINVAL;
716 if (addr < 0 || addr >= USB_MAX_DEVICES ||
717 sc->sc_bus->ub_devices[addr] == NULL)
718 return EINVAL;
719 if (len != 0) {
720 iov.iov_base = (void *)ur->ucr_data;
721 iov.iov_len = len;
722 uio.uio_iov = &iov;
723 uio.uio_iovcnt = 1;
724 uio.uio_resid = len;
725 uio.uio_offset = 0;
726 uio.uio_rw =
727 ur->ucr_request.bmRequestType & UT_READ ?
728 UIO_READ : UIO_WRITE;
729 uio.uio_vmspace = l->l_proc->p_vmspace;
730 ptr = kmem_alloc(len, KM_SLEEP);
731 if (uio.uio_rw == UIO_WRITE) {
732 error = uiomove(ptr, len, &uio);
733 if (error)
734 goto ret;
735 }
736 }
737 err = usbd_do_request_flags(sc->sc_bus->ub_devices[addr],
738 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
739 USBD_DEFAULT_TIMEOUT);
740 if (err) {
741 error = EIO;
742 goto ret;
743 }
744 if (len > ur->ucr_actlen)
745 len = ur->ucr_actlen;
746 if (len != 0) {
747 if (uio.uio_rw == UIO_READ) {
748 error = uiomove(ptr, len, &uio);
749 if (error)
750 goto ret;
751 }
752 }
753 ret:
754 if (ptr) {
755 len = UGETW(ur->ucr_request.wLength);
756 kmem_free(ptr, len);
757 }
758 return error;
759 }
760
761 case USB_DEVICEINFO:
762 {
763 struct usbd_device *dev;
764 struct usb_device_info *di = (void *)data;
765 int addr = di->udi_addr;
766
767 if (addr < 0 || addr >= USB_MAX_DEVICES)
768 return EINVAL;
769 if ((dev = sc->sc_bus->ub_devices[addr]) == NULL)
770 return ENXIO;
771 usbd_fill_deviceinfo(dev, di, 1);
772 break;
773 }
774
775 #ifdef COMPAT_30
776 case USB_DEVICEINFO_OLD:
777 {
778 struct usbd_device *dev;
779 struct usb_device_info_old *di = (void *)data;
780 int addr = di->udi_addr;
781
782 if (addr < 1 || addr >= USB_MAX_DEVICES)
783 return EINVAL;
784 if ((dev = sc->sc_bus->ub_devices[addr]) == NULL)
785 return ENXIO;
786 usbd_fill_deviceinfo_old(dev, di, 1);
787 break;
788 }
789 #endif
790
791 case USB_DEVICESTATS:
792 *(struct usb_device_stats *)data = sc->sc_bus->ub_stats;
793 break;
794
795 default:
796 return EINVAL;
797 }
798 return 0;
799 }
800
801 int
802 usbpoll(dev_t dev, int events, struct lwp *l)
803 {
804 int revents, mask;
805
806 if (minor(dev) == USB_DEV_MINOR) {
807 revents = 0;
808 mask = POLLIN | POLLRDNORM;
809
810 mutex_enter(&usb_event_lock);
811 if (events & mask && usb_nevents > 0)
812 revents |= events & mask;
813 if (revents == 0 && events & mask)
814 selrecord(l, &usb_selevent);
815 mutex_exit(&usb_event_lock);
816
817 return revents;
818 } else {
819 return 0;
820 }
821 }
822
823 static void
824 filt_usbrdetach(struct knote *kn)
825 {
826
827 mutex_enter(&usb_event_lock);
828 SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
829 mutex_exit(&usb_event_lock);
830 }
831
832 static int
833 filt_usbread(struct knote *kn, long hint)
834 {
835
836 if (usb_nevents == 0)
837 return 0;
838
839 kn->kn_data = sizeof(struct usb_event);
840 return 1;
841 }
842
843 static const struct filterops usbread_filtops =
844 { 1, NULL, filt_usbrdetach, filt_usbread };
845
846 int
847 usbkqfilter(dev_t dev, struct knote *kn)
848 {
849 struct klist *klist;
850
851 switch (kn->kn_filter) {
852 case EVFILT_READ:
853 if (minor(dev) != USB_DEV_MINOR)
854 return 1;
855 klist = &usb_selevent.sel_klist;
856 kn->kn_fop = &usbread_filtops;
857 break;
858
859 default:
860 return EINVAL;
861 }
862
863 kn->kn_hook = NULL;
864
865 mutex_enter(&usb_event_lock);
866 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
867 mutex_exit(&usb_event_lock);
868
869 return 0;
870 }
871
872 /* Explore device tree from the root. */
873 Static void
874 usb_discover(struct usb_softc *sc)
875 {
876
877 KASSERT(mutex_owned(sc->sc_bus->ub_lock));
878
879 DPRINTFN(2,("usb_discover\n"));
880 if (usb_noexplore > 1)
881 return;
882 /*
883 * We need mutual exclusion while traversing the device tree,
884 * but this is guaranteed since this function is only called
885 * from the event thread for the controller.
886 *
887 * Also, we now have sc_bus->ub_lock held.
888 */
889 while (sc->sc_bus->ub_needsexplore && !sc->sc_dying) {
890 sc->sc_bus->ub_needsexplore = 0;
891 mutex_exit(sc->sc_bus->ub_lock);
892 sc->sc_bus->ub_roothub->ud_hub->uh_explore(sc->sc_bus->ub_roothub);
893 mutex_enter(sc->sc_bus->ub_lock);
894 }
895 }
896
897 void
898 usb_needs_explore(struct usbd_device *dev)
899 {
900 DPRINTFN(2,("usb_needs_explore\n"));
901 mutex_enter(dev->ud_bus->ub_lock);
902 dev->ud_bus->ub_needsexplore = 1;
903 cv_signal(&dev->ud_bus->ub_needsexplore_cv);
904 mutex_exit(dev->ud_bus->ub_lock);
905 }
906
907 void
908 usb_needs_reattach(struct usbd_device *dev)
909 {
910 DPRINTFN(2,("usb_needs_reattach\n"));
911 mutex_enter(dev->ud_bus->ub_lock);
912 dev->ud_powersrc->up_reattach = 1;
913 dev->ud_bus->ub_needsexplore = 1;
914 cv_signal(&dev->ud_bus->ub_needsexplore_cv);
915 mutex_exit(dev->ud_bus->ub_lock);
916 }
917
918 /* Called at with usb_event_lock held. */
919 int
920 usb_get_next_event(struct usb_event *ue)
921 {
922 struct usb_event_q *ueq;
923
924 KASSERT(mutex_owned(&usb_event_lock));
925
926 if (usb_nevents <= 0)
927 return 0;
928 ueq = SIMPLEQ_FIRST(&usb_events);
929 #ifdef DIAGNOSTIC
930 if (ueq == NULL) {
931 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
932 usb_nevents = 0;
933 return 0;
934 }
935 #endif
936 if (ue)
937 *ue = ueq->ue;
938 SIMPLEQ_REMOVE_HEAD(&usb_events, next);
939 usb_free_event((struct usb_event *)(void *)ueq);
940 usb_nevents--;
941 return 1;
942 }
943
944 void
945 usbd_add_dev_event(int type, struct usbd_device *udev)
946 {
947 struct usb_event *ue = usb_alloc_event();
948
949 usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
950 usb_add_event(type, ue);
951 }
952
953 void
954 usbd_add_drv_event(int type, struct usbd_device *udev, device_t dev)
955 {
956 struct usb_event *ue = usb_alloc_event();
957
958 ue->u.ue_driver.ue_cookie = udev->ud_cookie;
959 strncpy(ue->u.ue_driver.ue_devname, device_xname(dev),
960 sizeof ue->u.ue_driver.ue_devname);
961 usb_add_event(type, ue);
962 }
963
964 Static struct usb_event *
965 usb_alloc_event(void)
966 {
967 /* Yes, this is right; we allocate enough so that we can use it later */
968 return kmem_zalloc(sizeof(struct usb_event_q), KM_SLEEP);
969 }
970
971 Static void
972 usb_free_event(struct usb_event *uep)
973 {
974 kmem_free(uep, sizeof(struct usb_event_q));
975 }
976
977 Static void
978 usb_add_event(int type, struct usb_event *uep)
979 {
980 struct usb_event_q *ueq;
981 struct timeval thetime;
982
983 microtime(&thetime);
984 /* Don't want to wait here with usb_event_lock held */
985 ueq = (struct usb_event_q *)(void *)uep;
986 ueq->ue = *uep;
987 ueq->ue.ue_type = type;
988 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
989
990 mutex_enter(&usb_event_lock);
991 if (++usb_nevents >= USB_MAX_EVENTS) {
992 /* Too many queued events, drop an old one. */
993 DPRINTFN(-1,("usb: event dropped\n"));
994 (void)usb_get_next_event(0);
995 }
996 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
997 cv_signal(&usb_event_cv);
998 selnotify(&usb_selevent, 0, 0);
999 if (usb_async_proc != NULL) {
1000 kpreempt_disable();
1001 softint_schedule(usb_async_sih);
1002 kpreempt_enable();
1003 }
1004 mutex_exit(&usb_event_lock);
1005 }
1006
1007 Static void
1008 usb_async_intr(void *cookie)
1009 {
1010 proc_t *proc;
1011
1012 mutex_enter(proc_lock);
1013 if ((proc = usb_async_proc) != NULL)
1014 psignal(proc, SIGIO);
1015 mutex_exit(proc_lock);
1016 }
1017
1018 Static void
1019 usb_soft_intr(void *arg)
1020 {
1021 struct usbd_bus *bus = arg;
1022
1023 mutex_enter(bus->ub_lock);
1024 bus->ub_methods->ubm_softint(bus);
1025 mutex_exit(bus->ub_lock);
1026 }
1027
1028 void
1029 usb_schedsoftintr(struct usbd_bus *bus)
1030 {
1031
1032 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->ub_usepolling));
1033
1034 if (bus->ub_usepolling) {
1035 bus->ub_methods->ubm_softint(bus);
1036 } else {
1037 kpreempt_disable();
1038 softint_schedule(bus->ub_soft);
1039 kpreempt_enable();
1040 }
1041 }
1042
1043 int
1044 usb_activate(device_t self, enum devact act)
1045 {
1046 struct usb_softc *sc = device_private(self);
1047
1048 switch (act) {
1049 case DVACT_DEACTIVATE:
1050 sc->sc_dying = 1;
1051 return 0;
1052 default:
1053 return EOPNOTSUPP;
1054 }
1055 }
1056
1057 void
1058 usb_childdet(device_t self, device_t child)
1059 {
1060 int i;
1061 struct usb_softc *sc = device_private(self);
1062 struct usbd_device *dev;
1063
1064 if ((dev = sc->sc_port.up_dev) == NULL || dev->ud_subdevlen == 0)
1065 return;
1066
1067 for (i = 0; i < dev->ud_subdevlen; i++)
1068 if (dev->ud_subdevs[i] == child)
1069 dev->ud_subdevs[i] = NULL;
1070 }
1071
1072 int
1073 usb_detach(device_t self, int flags)
1074 {
1075 struct usb_softc *sc = device_private(self);
1076 struct usb_event *ue;
1077 int rc;
1078
1079 DPRINTF(("usb_detach: start\n"));
1080
1081 /* Make all devices disconnect. */
1082 if (sc->sc_port.up_dev != NULL &&
1083 (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
1084 return rc;
1085
1086 pmf_device_deregister(self);
1087 /* Kill off event thread. */
1088 sc->sc_dying = 1;
1089 while (sc->sc_event_thread != NULL) {
1090 mutex_enter(sc->sc_bus->ub_lock);
1091 cv_signal(&sc->sc_bus->ub_needsexplore_cv);
1092 cv_timedwait(&sc->sc_bus->ub_needsexplore_cv,
1093 sc->sc_bus->ub_lock, hz * 60);
1094 mutex_exit(sc->sc_bus->ub_lock);
1095 }
1096 DPRINTF(("usb_detach: event thread dead\n"));
1097
1098 if (sc->sc_bus->ub_soft != NULL) {
1099 softint_disestablish(sc->sc_bus->ub_soft);
1100 sc->sc_bus->ub_soft = NULL;
1101 }
1102
1103 ue = usb_alloc_event();
1104 ue->u.ue_ctrlr.ue_bus = device_unit(self);
1105 usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
1106
1107 cv_destroy(&sc->sc_bus->ub_needsexplore_cv);
1108
1109 return 0;
1110 }
1111
1112 #ifdef COMPAT_30
1113 Static void
1114 usb_copy_old_devinfo(struct usb_device_info_old *uo,
1115 const struct usb_device_info *ue)
1116 {
1117 const unsigned char *p;
1118 unsigned char *q;
1119 int i, n;
1120
1121 uo->udi_bus = ue->udi_bus;
1122 uo->udi_addr = ue->udi_addr;
1123 uo->udi_cookie = ue->udi_cookie;
1124 for (i = 0, p = (const unsigned char *)ue->udi_product,
1125 q = (unsigned char *)uo->udi_product;
1126 *p && i < USB_MAX_STRING_LEN - 1; p++) {
1127 if (*p < 0x80)
1128 q[i++] = *p;
1129 else {
1130 q[i++] = '?';
1131 if ((*p & 0xe0) == 0xe0)
1132 p++;
1133 p++;
1134 }
1135 }
1136 q[i] = 0;
1137
1138 for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
1139 *p && i < USB_MAX_STRING_LEN - 1; p++) {
1140 if (* p < 0x80)
1141 q[i++] = *p;
1142 else {
1143 q[i++] = '?';
1144 p++;
1145 if ((*p & 0xe0) == 0xe0)
1146 p++;
1147 }
1148 }
1149 q[i] = 0;
1150
1151 memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
1152
1153 uo->udi_productNo = ue->udi_productNo;
1154 uo->udi_vendorNo = ue->udi_vendorNo;
1155 uo->udi_releaseNo = ue->udi_releaseNo;
1156 uo->udi_class = ue->udi_class;
1157 uo->udi_subclass = ue->udi_subclass;
1158 uo->udi_protocol = ue->udi_protocol;
1159 uo->udi_config = ue->udi_config;
1160 uo->udi_speed = ue->udi_speed;
1161 uo->udi_power = ue->udi_power;
1162 uo->udi_nports = ue->udi_nports;
1163
1164 for (n=0; n<USB_MAX_DEVNAMES; n++)
1165 memcpy(uo->udi_devnames[n],
1166 ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
1167 memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
1168 }
1169 #endif
1170