usb.c revision 1.53.2.2 1 1.53.2.2 nathanw /* $NetBSD: usb.c,v 1.53.2.2 2002/01/08 00:32:17 nathanw Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 1998 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.5 augustss * Carlstedt Research & Technology.
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 * 3. All advertising materials mentioning features or use of this software
20 1.1 augustss * must display the following acknowledgement:
21 1.1 augustss * This product includes software developed by the NetBSD
22 1.1 augustss * Foundation, Inc. and its contributors.
23 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 augustss * contributors may be used to endorse or promote products derived
25 1.1 augustss * from this software without specific prior written permission.
26 1.1 augustss *
27 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
38 1.1 augustss */
39 1.1 augustss
40 1.1 augustss /*
41 1.8 augustss * USB specifications and other documentation can be found at
42 1.8 augustss * http://www.usb.org/developers/data/ and
43 1.8 augustss * http://www.usb.org/developers/index.html .
44 1.1 augustss */
45 1.1 augustss
46 1.53.2.1 nathanw #include <sys/cdefs.h>
47 1.53.2.2 nathanw __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.53.2.2 2002/01/08 00:32:17 nathanw Exp $");
48 1.53.2.1 nathanw
49 1.1 augustss #include <sys/param.h>
50 1.1 augustss #include <sys/systm.h>
51 1.1 augustss #include <sys/kernel.h>
52 1.1 augustss #include <sys/malloc.h>
53 1.1 augustss #include <sys/device.h>
54 1.13 augustss #include <sys/kthread.h>
55 1.30 augustss #include <sys/proc.h>
56 1.22 augustss #include <sys/conf.h>
57 1.1 augustss #include <sys/poll.h>
58 1.1 augustss #include <sys/select.h>
59 1.26 augustss #include <sys/vnode.h>
60 1.26 augustss #include <sys/signalvar.h>
61 1.1 augustss
62 1.1 augustss #include <dev/usb/usb.h>
63 1.13 augustss #include <dev/usb/usbdi.h>
64 1.13 augustss #include <dev/usb/usbdi_util.h>
65 1.1 augustss
66 1.26 augustss #define USB_DEV_MINOR 255
67 1.26 augustss
68 1.20 augustss #include <machine/bus.h>
69 1.7 augustss
70 1.1 augustss #include <dev/usb/usbdivar.h>
71 1.1 augustss #include <dev/usb/usb_quirks.h>
72 1.1 augustss
73 1.1 augustss #ifdef USB_DEBUG
74 1.16 augustss #define DPRINTF(x) if (usbdebug) logprintf x
75 1.16 augustss #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
76 1.1 augustss int usbdebug = 0;
77 1.30 augustss #ifdef UHCI_DEBUG
78 1.33 augustss int uhcidebug;
79 1.30 augustss #endif
80 1.30 augustss #ifdef OHCI_DEBUG
81 1.33 augustss int ohcidebug;
82 1.30 augustss #endif
83 1.34 augustss /*
84 1.34 augustss * 0 - do usual exploration
85 1.34 augustss * 1 - do not use timeout exploration
86 1.34 augustss * >1 - do no exploration
87 1.34 augustss */
88 1.21 augustss int usb_noexplore = 0;
89 1.1 augustss #else
90 1.1 augustss #define DPRINTF(x)
91 1.1 augustss #define DPRINTFN(n,x)
92 1.1 augustss #endif
93 1.1 augustss
94 1.1 augustss struct usb_softc {
95 1.22 augustss USBBASEDEVICE sc_dev; /* base device */
96 1.1 augustss usbd_bus_handle sc_bus; /* USB controller */
97 1.1 augustss struct usbd_port sc_port; /* dummy port for root hub */
98 1.25 augustss
99 1.22 augustss struct proc *sc_event_thread;
100 1.25 augustss
101 1.25 augustss char sc_dying;
102 1.1 augustss };
103 1.1 augustss
104 1.53.2.2 nathanw TAILQ_HEAD(, usb_task) usb_all_tasks;
105 1.53.2.2 nathanw
106 1.22 augustss cdev_decl(usb);
107 1.7 augustss
108 1.51 augustss Static void usb_discover(void *);
109 1.45 augustss Static void usb_create_event_thread(void *);
110 1.45 augustss Static void usb_event_thread(void *);
111 1.53.2.2 nathanw Static void usb_task_thread(void *);
112 1.53.2.2 nathanw Static struct proc *usb_task_thread_proc = NULL;
113 1.1 augustss
114 1.41 augustss #define USB_MAX_EVENTS 100
115 1.26 augustss struct usb_event_q {
116 1.26 augustss struct usb_event ue;
117 1.26 augustss SIMPLEQ_ENTRY(usb_event_q) next;
118 1.26 augustss };
119 1.42 augustss Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
120 1.29 augustss SIMPLEQ_HEAD_INITIALIZER(usb_events);
121 1.42 augustss Static int usb_nevents = 0;
122 1.42 augustss Static struct selinfo usb_selevent;
123 1.53.2.2 nathanw Static usb_proc_ptr usb_async_proc; /* process that wants USB SIGIO */
124 1.42 augustss Static int usb_dev_open = 0;
125 1.45 augustss Static void usb_add_event(int, struct usb_event *);
126 1.26 augustss
127 1.45 augustss Static int usb_get_next_event(struct usb_event *);
128 1.23 augustss
129 1.42 augustss Static const char *usbrev_str[] = USBREV_STR;
130 1.31 augustss
131 1.28 augustss USB_DECLARE_DRIVER(usb);
132 1.1 augustss
133 1.7 augustss USB_MATCH(usb)
134 1.1 augustss {
135 1.1 augustss DPRINTF(("usbd_match\n"));
136 1.7 augustss return (UMATCH_GENERIC);
137 1.1 augustss }
138 1.1 augustss
139 1.7 augustss USB_ATTACH(usb)
140 1.1 augustss {
141 1.1 augustss struct usb_softc *sc = (struct usb_softc *)self;
142 1.1 augustss usbd_device_handle dev;
143 1.29 augustss usbd_status err;
144 1.31 augustss int usbrev;
145 1.53.2.2 nathanw int speed;
146 1.38 augustss struct usb_event ue;
147 1.1 augustss
148 1.1 augustss DPRINTF(("usbd_attach\n"));
149 1.31 augustss
150 1.4 augustss usbd_init();
151 1.1 augustss sc->sc_bus = aux;
152 1.1 augustss sc->sc_bus->usbctl = sc;
153 1.1 augustss sc->sc_port.power = USB_MAX_POWER;
154 1.31 augustss
155 1.31 augustss usbrev = sc->sc_bus->usbrev;
156 1.32 augustss printf(": USB revision %s", usbrev_str[usbrev]);
157 1.53.2.2 nathanw switch (usbrev) {
158 1.53.2.2 nathanw case USBREV_1_0:
159 1.53.2.2 nathanw case USBREV_1_1:
160 1.53.2.2 nathanw speed = USB_SPEED_FULL;
161 1.53.2.2 nathanw break;
162 1.53.2.2 nathanw case USBREV_2_0:
163 1.53.2.2 nathanw speed = USB_SPEED_HIGH;
164 1.53.2.2 nathanw break;
165 1.53.2.2 nathanw default:
166 1.31 augustss printf(", not supported\n");
167 1.53.2.2 nathanw sc->sc_dying = 1;
168 1.31 augustss USB_ATTACH_ERROR_RETURN;
169 1.32 augustss }
170 1.32 augustss printf("\n");
171 1.31 augustss
172 1.35 augustss /* Make sure not to use tsleep() if we are cold booting. */
173 1.35 augustss if (cold)
174 1.35 augustss sc->sc_bus->use_polling++;
175 1.35 augustss
176 1.38 augustss ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
177 1.38 augustss usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
178 1.38 augustss
179 1.49 augustss #ifdef USB_USE_SOFTINTR
180 1.49 augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
181 1.49 augustss /* XXX we should have our own level */
182 1.49 augustss sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
183 1.49 augustss sc->sc_bus->methods->soft_intr, sc->sc_bus);
184 1.49 augustss if (sc->sc_bus->soft == NULL) {
185 1.49 augustss printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
186 1.49 augustss sc->sc_dying = 1;
187 1.53.2.2 nathanw USB_ATTACH_ERROR_RETURN;
188 1.49 augustss }
189 1.49 augustss #else
190 1.49 augustss callout_init(&sc->sc_bus->softi);
191 1.49 augustss #endif
192 1.49 augustss #endif
193 1.49 augustss
194 1.53.2.2 nathanw err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
195 1.29 augustss &sc->sc_port);
196 1.29 augustss if (!err) {
197 1.1 augustss dev = sc->sc_port.device;
198 1.29 augustss if (dev->hub == NULL) {
199 1.22 augustss sc->sc_dying = 1;
200 1.7 augustss printf("%s: root device is not a hub\n",
201 1.7 augustss USBDEVNAME(sc->sc_dev));
202 1.7 augustss USB_ATTACH_ERROR_RETURN;
203 1.1 augustss }
204 1.1 augustss sc->sc_bus->root_hub = dev;
205 1.24 augustss #if 1
206 1.24 augustss /*
207 1.24 augustss * Turning this code off will delay attachment of USB devices
208 1.24 augustss * until the USB event thread is running, which means that
209 1.24 augustss * the keyboard will not work until after cold boot.
210 1.24 augustss */
211 1.36 augustss if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
212 1.24 augustss dev->hub->explore(sc->sc_bus->root_hub);
213 1.24 augustss #endif
214 1.1 augustss } else {
215 1.1 augustss printf("%s: root hub problem, error=%d\n",
216 1.29 augustss USBDEVNAME(sc->sc_dev), err);
217 1.22 augustss sc->sc_dying = 1;
218 1.1 augustss }
219 1.35 augustss if (cold)
220 1.35 augustss sc->sc_bus->use_polling--;
221 1.7 augustss
222 1.37 thorpej config_pending_incr();
223 1.43 augustss usb_kthread_create(usb_create_event_thread, sc);
224 1.28 augustss
225 1.7 augustss USB_ATTACH_SUCCESS_RETURN;
226 1.1 augustss }
227 1.1 augustss
228 1.28 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
229 1.13 augustss void
230 1.45 augustss usb_create_event_thread(void *arg)
231 1.13 augustss {
232 1.13 augustss struct usb_softc *sc = arg;
233 1.53.2.2 nathanw static int created = 0;
234 1.13 augustss
235 1.43 augustss if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
236 1.13 augustss "%s", sc->sc_dev.dv_xname)) {
237 1.13 augustss printf("%s: unable to create event thread for\n",
238 1.13 augustss sc->sc_dev.dv_xname);
239 1.13 augustss panic("usb_create_event_thread");
240 1.13 augustss }
241 1.53.2.2 nathanw if (!created) {
242 1.53.2.2 nathanw created = 1;
243 1.53.2.2 nathanw TAILQ_INIT(&usb_all_tasks);
244 1.53.2.2 nathanw if (usb_kthread_create1(usb_task_thread, NULL,
245 1.53.2.2 nathanw &usb_task_thread_proc, "usbtask")) {
246 1.53.2.2 nathanw printf("unable to create task thread\n");
247 1.53.2.2 nathanw panic("usb_create_event_thread task");
248 1.53.2.2 nathanw }
249 1.53.2.2 nathanw }
250 1.13 augustss }
251 1.13 augustss
252 1.52 augustss /*
253 1.52 augustss * Add a task to be performed by the event thread. This function can be
254 1.52 augustss * called from any context and the task will be executed in a process
255 1.52 augustss * context ASAP.
256 1.52 augustss */
257 1.13 augustss void
258 1.51 augustss usb_add_task(usbd_device_handle dev, struct usb_task *task)
259 1.51 augustss {
260 1.51 augustss int s;
261 1.51 augustss
262 1.51 augustss s = splusb();
263 1.51 augustss if (!task->onqueue) {
264 1.53.2.2 nathanw DPRINTFN(2,("usb_add_task: task=%p\n", task));
265 1.53.2.2 nathanw TAILQ_INSERT_TAIL(&usb_all_tasks, task, next);
266 1.51 augustss task->onqueue = 1;
267 1.51 augustss } else
268 1.53.2.2 nathanw DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
269 1.53.2.2 nathanw wakeup(&usb_all_tasks);
270 1.51 augustss splx(s);
271 1.51 augustss }
272 1.51 augustss
273 1.51 augustss void
274 1.53 augustss usb_rem_task(usbd_device_handle dev, struct usb_task *task)
275 1.53 augustss {
276 1.53 augustss int s;
277 1.53 augustss
278 1.53 augustss s = splusb();
279 1.53 augustss if (task->onqueue) {
280 1.53.2.2 nathanw TAILQ_REMOVE(&usb_all_tasks, task, next);
281 1.53 augustss task->onqueue = 0;
282 1.53 augustss }
283 1.53 augustss splx(s);
284 1.53 augustss }
285 1.53 augustss
286 1.53 augustss void
287 1.45 augustss usb_event_thread(void *arg)
288 1.13 augustss {
289 1.13 augustss struct usb_softc *sc = arg;
290 1.13 augustss
291 1.27 augustss DPRINTF(("usb_event_thread: start\n"));
292 1.40 augustss
293 1.53.2.2 nathanw /*
294 1.53.2.2 nathanw * In case this controller is a companion controller to an
295 1.53.2.2 nathanw * EHCI controller we need to wait until the EHCI controller
296 1.53.2.2 nathanw * has grabbed the port.
297 1.53.2.2 nathanw */
298 1.53.2.2 nathanw usb_delay_ms(sc->sc_bus, 500);
299 1.53.2.2 nathanw
300 1.40 augustss /* Make sure first discover does something. */
301 1.40 augustss sc->sc_bus->needs_explore = 1;
302 1.51 augustss usb_discover(sc);
303 1.51 augustss config_pending_decr();
304 1.27 augustss
305 1.22 augustss while (!sc->sc_dying) {
306 1.53.2.2 nathanw #ifdef USB_DEBUG
307 1.53.2.2 nathanw if (usb_noexplore < 2)
308 1.53.2.2 nathanw #endif
309 1.53.2.2 nathanw usb_discover(sc);
310 1.53.2.2 nathanw #ifdef USB_DEBUG
311 1.53.2.2 nathanw (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
312 1.53.2.2 nathanw usb_noexplore ? 0 : hz * 60);
313 1.53.2.2 nathanw #else
314 1.53.2.2 nathanw (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
315 1.53.2.2 nathanw hz * 60);
316 1.53.2.2 nathanw #endif
317 1.53.2.2 nathanw DPRINTFN(2,("usb_event_thread: woke up\n"));
318 1.13 augustss }
319 1.50 augustss sc->sc_event_thread = NULL;
320 1.13 augustss
321 1.13 augustss /* In case parent is waiting for us to exit. */
322 1.13 augustss wakeup(sc);
323 1.13 augustss
324 1.27 augustss DPRINTF(("usb_event_thread: exit\n"));
325 1.13 augustss kthread_exit(0);
326 1.13 augustss }
327 1.13 augustss
328 1.53.2.2 nathanw void
329 1.53.2.2 nathanw usb_task_thread(void *arg)
330 1.53.2.2 nathanw {
331 1.53.2.2 nathanw struct usb_task *task;
332 1.53.2.2 nathanw int s;
333 1.53.2.2 nathanw
334 1.53.2.2 nathanw DPRINTF(("usb_task_thread: start\n"));
335 1.53.2.2 nathanw
336 1.53.2.2 nathanw s = splusb();
337 1.53.2.2 nathanw for (;;) {
338 1.53.2.2 nathanw task = TAILQ_FIRST(&usb_all_tasks);
339 1.53.2.2 nathanw if (task == NULL) {
340 1.53.2.2 nathanw tsleep(&usb_all_tasks, PWAIT, "usbtsk", 0);
341 1.53.2.2 nathanw task = TAILQ_FIRST(&usb_all_tasks);
342 1.53.2.2 nathanw }
343 1.53.2.2 nathanw DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
344 1.53.2.2 nathanw if (task != NULL) {
345 1.53.2.2 nathanw TAILQ_REMOVE(&usb_all_tasks, task, next);
346 1.53.2.2 nathanw task->onqueue = 0;
347 1.53.2.2 nathanw splx(s);
348 1.53.2.2 nathanw task->fun(task->arg);
349 1.53.2.2 nathanw s = splusb();
350 1.53.2.2 nathanw }
351 1.53.2.2 nathanw }
352 1.53.2.2 nathanw }
353 1.53.2.2 nathanw
354 1.1 augustss int
355 1.45 augustss usbctlprint(void *aux, const char *pnp)
356 1.1 augustss {
357 1.1 augustss /* only "usb"es can attach to host controllers */
358 1.1 augustss if (pnp)
359 1.1 augustss printf("usb at %s", pnp);
360 1.1 augustss
361 1.1 augustss return (UNCONF);
362 1.1 augustss }
363 1.28 augustss #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
364 1.7 augustss
365 1.1 augustss int
366 1.53.2.2 nathanw usbopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
367 1.1 augustss {
368 1.26 augustss int unit = minor(dev);
369 1.26 augustss struct usb_softc *sc;
370 1.26 augustss
371 1.26 augustss if (unit == USB_DEV_MINOR) {
372 1.26 augustss if (usb_dev_open)
373 1.26 augustss return (EBUSY);
374 1.26 augustss usb_dev_open = 1;
375 1.26 augustss usb_async_proc = 0;
376 1.26 augustss return (0);
377 1.26 augustss }
378 1.26 augustss
379 1.26 augustss USB_GET_SC_OPEN(usb, unit, sc);
380 1.1 augustss
381 1.22 augustss if (sc->sc_dying)
382 1.22 augustss return (EIO);
383 1.1 augustss
384 1.1 augustss return (0);
385 1.1 augustss }
386 1.1 augustss
387 1.1 augustss int
388 1.45 augustss usbread(dev_t dev, struct uio *uio, int flag)
389 1.26 augustss {
390 1.26 augustss struct usb_event ue;
391 1.26 augustss int s, error, n;
392 1.26 augustss
393 1.26 augustss if (minor(dev) != USB_DEV_MINOR)
394 1.26 augustss return (ENXIO);
395 1.26 augustss
396 1.26 augustss if (uio->uio_resid != sizeof(struct usb_event))
397 1.26 augustss return (EINVAL);
398 1.26 augustss
399 1.26 augustss error = 0;
400 1.26 augustss s = splusb();
401 1.26 augustss for (;;) {
402 1.26 augustss n = usb_get_next_event(&ue);
403 1.26 augustss if (n != 0)
404 1.26 augustss break;
405 1.26 augustss if (flag & IO_NDELAY) {
406 1.26 augustss error = EWOULDBLOCK;
407 1.26 augustss break;
408 1.26 augustss }
409 1.26 augustss error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
410 1.26 augustss if (error)
411 1.26 augustss break;
412 1.26 augustss }
413 1.26 augustss splx(s);
414 1.26 augustss if (!error)
415 1.30 augustss error = uiomove((void *)&ue, uio->uio_resid, uio);
416 1.26 augustss
417 1.26 augustss return (error);
418 1.26 augustss }
419 1.26 augustss
420 1.26 augustss int
421 1.53.2.2 nathanw usbclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
422 1.1 augustss {
423 1.26 augustss int unit = minor(dev);
424 1.26 augustss
425 1.26 augustss if (unit == USB_DEV_MINOR) {
426 1.26 augustss usb_async_proc = 0;
427 1.26 augustss usb_dev_open = 0;
428 1.26 augustss }
429 1.26 augustss
430 1.1 augustss return (0);
431 1.1 augustss }
432 1.1 augustss
433 1.1 augustss int
434 1.53.2.2 nathanw usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
435 1.1 augustss {
436 1.26 augustss struct usb_softc *sc;
437 1.28 augustss int unit = minor(devt);
438 1.26 augustss
439 1.26 augustss if (unit == USB_DEV_MINOR) {
440 1.26 augustss switch (cmd) {
441 1.26 augustss case FIONBIO:
442 1.26 augustss /* All handled in the upper FS layer. */
443 1.26 augustss return (0);
444 1.26 augustss
445 1.26 augustss case FIOASYNC:
446 1.26 augustss if (*(int *)data)
447 1.26 augustss usb_async_proc = p;
448 1.26 augustss else
449 1.26 augustss usb_async_proc = 0;
450 1.26 augustss return (0);
451 1.26 augustss
452 1.26 augustss default:
453 1.26 augustss return (EINVAL);
454 1.26 augustss }
455 1.26 augustss }
456 1.26 augustss
457 1.26 augustss USB_GET_SC(usb, unit, sc);
458 1.1 augustss
459 1.22 augustss if (sc->sc_dying)
460 1.22 augustss return (EIO);
461 1.22 augustss
462 1.1 augustss switch (cmd) {
463 1.1 augustss #ifdef USB_DEBUG
464 1.1 augustss case USB_SETDEBUG:
465 1.30 augustss usbdebug = ((*(int *)data) & 0x000000ff);
466 1.30 augustss #ifdef UHCI_DEBUG
467 1.30 augustss uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
468 1.30 augustss #endif
469 1.30 augustss #ifdef OHCI_DEBUG
470 1.30 augustss ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
471 1.30 augustss #endif
472 1.1 augustss break;
473 1.53.2.1 nathanw #endif /* USB_DEBUG */
474 1.1 augustss case USB_REQUEST:
475 1.1 augustss {
476 1.1 augustss struct usb_ctl_request *ur = (void *)data;
477 1.1 augustss int len = UGETW(ur->request.wLength);
478 1.1 augustss struct iovec iov;
479 1.1 augustss struct uio uio;
480 1.1 augustss void *ptr = 0;
481 1.1 augustss int addr = ur->addr;
482 1.29 augustss usbd_status err;
483 1.1 augustss int error = 0;
484 1.1 augustss
485 1.9 augustss DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
486 1.1 augustss if (len < 0 || len > 32768)
487 1.9 augustss return (EINVAL);
488 1.1 augustss if (addr < 0 || addr >= USB_MAX_DEVICES ||
489 1.1 augustss sc->sc_bus->devices[addr] == 0)
490 1.9 augustss return (EINVAL);
491 1.1 augustss if (len != 0) {
492 1.1 augustss iov.iov_base = (caddr_t)ur->data;
493 1.1 augustss iov.iov_len = len;
494 1.1 augustss uio.uio_iov = &iov;
495 1.1 augustss uio.uio_iovcnt = 1;
496 1.1 augustss uio.uio_resid = len;
497 1.1 augustss uio.uio_offset = 0;
498 1.1 augustss uio.uio_segflg = UIO_USERSPACE;
499 1.1 augustss uio.uio_rw =
500 1.1 augustss ur->request.bmRequestType & UT_READ ?
501 1.1 augustss UIO_READ : UIO_WRITE;
502 1.1 augustss uio.uio_procp = p;
503 1.1 augustss ptr = malloc(len, M_TEMP, M_WAITOK);
504 1.1 augustss if (uio.uio_rw == UIO_WRITE) {
505 1.1 augustss error = uiomove(ptr, len, &uio);
506 1.1 augustss if (error)
507 1.1 augustss goto ret;
508 1.1 augustss }
509 1.1 augustss }
510 1.29 augustss err = usbd_do_request_flags(sc->sc_bus->devices[addr],
511 1.29 augustss &ur->request, ptr, ur->flags, &ur->actlen);
512 1.29 augustss if (err) {
513 1.1 augustss error = EIO;
514 1.1 augustss goto ret;
515 1.1 augustss }
516 1.1 augustss if (len != 0) {
517 1.1 augustss if (uio.uio_rw == UIO_READ) {
518 1.1 augustss error = uiomove(ptr, len, &uio);
519 1.1 augustss if (error)
520 1.1 augustss goto ret;
521 1.1 augustss }
522 1.1 augustss }
523 1.1 augustss ret:
524 1.1 augustss if (ptr)
525 1.1 augustss free(ptr, M_TEMP);
526 1.1 augustss return (error);
527 1.1 augustss }
528 1.1 augustss
529 1.1 augustss case USB_DEVICEINFO:
530 1.1 augustss {
531 1.1 augustss struct usb_device_info *di = (void *)data;
532 1.1 augustss int addr = di->addr;
533 1.30 augustss usbd_device_handle dev;
534 1.1 augustss
535 1.1 augustss if (addr < 1 || addr >= USB_MAX_DEVICES)
536 1.1 augustss return (EINVAL);
537 1.30 augustss dev = sc->sc_bus->devices[addr];
538 1.30 augustss if (dev == NULL)
539 1.1 augustss return (ENXIO);
540 1.48 augustss usbd_fill_deviceinfo(dev, di, 1);
541 1.1 augustss break;
542 1.1 augustss }
543 1.2 augustss
544 1.2 augustss case USB_DEVICESTATS:
545 1.2 augustss *(struct usb_device_stats *)data = sc->sc_bus->stats;
546 1.2 augustss break;
547 1.1 augustss
548 1.1 augustss default:
549 1.26 augustss return (EINVAL);
550 1.1 augustss }
551 1.1 augustss return (0);
552 1.1 augustss }
553 1.1 augustss
554 1.1 augustss int
555 1.53.2.2 nathanw usbpoll(dev_t dev, int events, usb_proc_ptr p)
556 1.1 augustss {
557 1.26 augustss int revents, mask, s;
558 1.1 augustss
559 1.30 augustss if (minor(dev) == USB_DEV_MINOR) {
560 1.30 augustss revents = 0;
561 1.30 augustss mask = POLLIN | POLLRDNORM;
562 1.30 augustss
563 1.30 augustss s = splusb();
564 1.30 augustss if (events & mask && usb_nevents > 0)
565 1.30 augustss revents |= events & mask;
566 1.30 augustss if (revents == 0 && events & mask)
567 1.30 augustss selrecord(p, &usb_selevent);
568 1.30 augustss splx(s);
569 1.30 augustss
570 1.30 augustss return (revents);
571 1.30 augustss } else {
572 1.30 augustss return (ENXIO);
573 1.1 augustss }
574 1.1 augustss }
575 1.1 augustss
576 1.25 augustss /* Explore device tree from the root. */
577 1.51 augustss Static void
578 1.51 augustss usb_discover(void *v)
579 1.1 augustss {
580 1.51 augustss struct usb_softc *sc = v;
581 1.51 augustss
582 1.51 augustss DPRINTFN(2,("usb_discover\n"));
583 1.51 augustss #ifdef USB_DEBUG
584 1.51 augustss if (usb_noexplore > 1)
585 1.51 augustss return;
586 1.51 augustss #endif
587 1.25 augustss /*
588 1.25 augustss * We need mutual exclusion while traversing the device tree,
589 1.25 augustss * but this is guaranteed since this function is only called
590 1.25 augustss * from the event thread for the controller.
591 1.25 augustss */
592 1.30 augustss while (sc->sc_bus->needs_explore && !sc->sc_dying) {
593 1.13 augustss sc->sc_bus->needs_explore = 0;
594 1.13 augustss sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
595 1.30 augustss }
596 1.1 augustss }
597 1.1 augustss
598 1.1 augustss void
599 1.51 augustss usb_needs_explore(usbd_device_handle dev)
600 1.1 augustss {
601 1.51 augustss DPRINTFN(2,("usb_needs_explore\n"));
602 1.51 augustss dev->bus->needs_explore = 1;
603 1.53.2.2 nathanw wakeup(&dev->bus->needs_explore);
604 1.1 augustss }
605 1.7 augustss
606 1.26 augustss /* Called at splusb() */
607 1.26 augustss int
608 1.45 augustss usb_get_next_event(struct usb_event *ue)
609 1.26 augustss {
610 1.26 augustss struct usb_event_q *ueq;
611 1.26 augustss
612 1.26 augustss if (usb_nevents <= 0)
613 1.26 augustss return (0);
614 1.26 augustss ueq = SIMPLEQ_FIRST(&usb_events);
615 1.26 augustss *ue = ueq->ue;
616 1.26 augustss SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
617 1.26 augustss free(ueq, M_USBDEV);
618 1.26 augustss usb_nevents--;
619 1.26 augustss return (1);
620 1.26 augustss }
621 1.26 augustss
622 1.26 augustss void
623 1.45 augustss usbd_add_dev_event(int type, usbd_device_handle udev)
624 1.38 augustss {
625 1.38 augustss struct usb_event ue;
626 1.38 augustss
627 1.48 augustss usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
628 1.38 augustss usb_add_event(type, &ue);
629 1.38 augustss }
630 1.38 augustss
631 1.38 augustss void
632 1.45 augustss usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
633 1.38 augustss {
634 1.38 augustss struct usb_event ue;
635 1.38 augustss
636 1.38 augustss ue.u.ue_driver.ue_cookie = udev->cookie;
637 1.38 augustss strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
638 1.38 augustss sizeof ue.u.ue_driver.ue_devname);
639 1.38 augustss usb_add_event(type, &ue);
640 1.38 augustss }
641 1.38 augustss
642 1.42 augustss Static void
643 1.45 augustss usb_add_event(int type, struct usb_event *uep)
644 1.26 augustss {
645 1.26 augustss struct usb_event_q *ueq;
646 1.26 augustss struct usb_event ue;
647 1.26 augustss struct timeval thetime;
648 1.26 augustss int s;
649 1.26 augustss
650 1.38 augustss microtime(&thetime);
651 1.38 augustss /* Don't want to wait here inside splusb() */
652 1.38 augustss ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK);
653 1.38 augustss ueq->ue = *uep;
654 1.38 augustss ueq->ue.ue_type = type;
655 1.38 augustss TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
656 1.38 augustss
657 1.26 augustss s = splusb();
658 1.26 augustss if (++usb_nevents >= USB_MAX_EVENTS) {
659 1.26 augustss /* Too many queued events, drop an old one. */
660 1.26 augustss DPRINTFN(-1,("usb: event dropped\n"));
661 1.26 augustss (void)usb_get_next_event(&ue);
662 1.26 augustss }
663 1.26 augustss SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
664 1.26 augustss wakeup(&usb_events);
665 1.26 augustss selwakeup(&usb_selevent);
666 1.29 augustss if (usb_async_proc != NULL)
667 1.26 augustss psignal(usb_async_proc, SIGIO);
668 1.26 augustss splx(s);
669 1.39 augustss }
670 1.53.2.2 nathanw
671 1.39 augustss void
672 1.49 augustss usb_schedsoftintr(usbd_bus_handle bus)
673 1.39 augustss {
674 1.53.2.1 nathanw DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
675 1.49 augustss #ifdef USB_USE_SOFTINTR
676 1.49 augustss if (bus->use_polling) {
677 1.49 augustss bus->methods->soft_intr(bus);
678 1.49 augustss } else {
679 1.49 augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
680 1.49 augustss softintr_schedule(bus->soft);
681 1.49 augustss #else
682 1.49 augustss if (!callout_pending(&bus->softi))
683 1.49 augustss callout_reset(&bus->softi, 0, bus->methods->soft_intr,
684 1.49 augustss bus);
685 1.49 augustss #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
686 1.49 augustss }
687 1.49 augustss #else
688 1.39 augustss bus->methods->soft_intr(bus);
689 1.53.2.1 nathanw #endif /* USB_USE_SOFTINTR */
690 1.26 augustss }
691 1.26 augustss
692 1.7 augustss int
693 1.45 augustss usb_activate(device_ptr_t self, enum devact act)
694 1.7 augustss {
695 1.22 augustss struct usb_softc *sc = (struct usb_softc *)self;
696 1.25 augustss usbd_device_handle dev = sc->sc_port.device;
697 1.25 augustss int i, rv = 0;
698 1.22 augustss
699 1.22 augustss switch (act) {
700 1.22 augustss case DVACT_ACTIVATE:
701 1.22 augustss return (EOPNOTSUPP);
702 1.22 augustss break;
703 1.22 augustss
704 1.22 augustss case DVACT_DEACTIVATE:
705 1.22 augustss sc->sc_dying = 1;
706 1.25 augustss if (dev && dev->cdesc && dev->subdevs) {
707 1.25 augustss for (i = 0; dev->subdevs[i]; i++)
708 1.25 augustss rv |= config_deactivate(dev->subdevs[i]);
709 1.25 augustss }
710 1.22 augustss break;
711 1.22 augustss }
712 1.22 augustss return (rv);
713 1.13 augustss }
714 1.7 augustss
715 1.13 augustss int
716 1.45 augustss usb_detach(device_ptr_t self, int flags)
717 1.13 augustss {
718 1.22 augustss struct usb_softc *sc = (struct usb_softc *)self;
719 1.38 augustss struct usb_event ue;
720 1.22 augustss
721 1.27 augustss DPRINTF(("usb_detach: start\n"));
722 1.27 augustss
723 1.22 augustss sc->sc_dying = 1;
724 1.22 augustss
725 1.22 augustss /* Make all devices disconnect. */
726 1.25 augustss if (sc->sc_port.device)
727 1.27 augustss usb_disconnect_port(&sc->sc_port, self);
728 1.22 augustss
729 1.22 augustss /* Kill off event thread. */
730 1.22 augustss if (sc->sc_event_thread) {
731 1.53.2.2 nathanw wakeup(&sc->sc_bus->needs_explore);
732 1.22 augustss if (tsleep(sc, PWAIT, "usbdet", hz * 60))
733 1.22 augustss printf("%s: event thread didn't die\n",
734 1.22 augustss USBDEVNAME(sc->sc_dev));
735 1.27 augustss DPRINTF(("usb_detach: event thread dead\n"));
736 1.22 augustss }
737 1.22 augustss
738 1.26 augustss usbd_finish();
739 1.49 augustss
740 1.49 augustss #ifdef USB_USE_SOFTINTR
741 1.49 augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
742 1.49 augustss if (sc->sc_bus->soft != NULL) {
743 1.49 augustss softintr_disestablish(sc->sc_bus->soft);
744 1.49 augustss sc->sc_bus->soft = NULL;
745 1.49 augustss }
746 1.49 augustss #else
747 1.49 augustss callout_stop(&sc->sc_bus->softi);
748 1.49 augustss #endif
749 1.49 augustss #endif
750 1.38 augustss
751 1.38 augustss ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
752 1.38 augustss usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
753 1.38 augustss
754 1.7 augustss return (0);
755 1.7 augustss }
756