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