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