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