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