usb.c revision 1.48 1 1.48 augustss /* $NetBSD: usb.c,v 1.48 2000/12/13 04:05:14 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.44 augustss * by Lennart Augustsson (lennart (at) augustsson.net) 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.33 augustss int uhcidebug;
92 1.30 augustss #endif
93 1.30 augustss #ifdef OHCI_DEBUG
94 1.33 augustss int ohcidebug;
95 1.30 augustss #endif
96 1.34 augustss /*
97 1.34 augustss * 0 - do usual exploration
98 1.34 augustss * 1 - do not use timeout exploration
99 1.34 augustss * >1 - do no exploration
100 1.34 augustss */
101 1.21 augustss int usb_noexplore = 0;
102 1.1 augustss #else
103 1.1 augustss #define DPRINTF(x)
104 1.1 augustss #define DPRINTFN(n,x)
105 1.1 augustss #endif
106 1.1 augustss
107 1.1 augustss struct usb_softc {
108 1.22 augustss USBBASEDEVICE sc_dev; /* base device */
109 1.1 augustss usbd_bus_handle sc_bus; /* USB controller */
110 1.1 augustss struct usbd_port sc_port; /* dummy port for root hub */
111 1.25 augustss
112 1.30 augustss #if defined(__FreeBSD__)
113 1.30 augustss /* This part should be deleted when kthreads is available */
114 1.22 augustss struct selinfo sc_consel; /* waiting for connect change */
115 1.30 augustss #else
116 1.22 augustss struct proc *sc_event_thread;
117 1.30 augustss #endif
118 1.25 augustss
119 1.25 augustss char sc_dying;
120 1.1 augustss };
121 1.1 augustss
122 1.17 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
123 1.22 augustss cdev_decl(usb);
124 1.7 augustss #elif defined(__FreeBSD__)
125 1.7 augustss d_open_t usbopen;
126 1.7 augustss d_close_t usbclose;
127 1.30 augustss d_read_t usbread;
128 1.7 augustss d_ioctl_t usbioctl;
129 1.45 augustss int usbpoll(dev_t, int, struct proc *);
130 1.7 augustss
131 1.7 augustss struct cdevsw usb_cdevsw = {
132 1.28 augustss /* open */ usbopen,
133 1.28 augustss /* close */ usbclose,
134 1.28 augustss /* read */ noread,
135 1.28 augustss /* write */ nowrite,
136 1.28 augustss /* ioctl */ usbioctl,
137 1.28 augustss /* poll */ usbpoll,
138 1.28 augustss /* mmap */ nommap,
139 1.28 augustss /* strategy */ nostrategy,
140 1.28 augustss /* name */ "usb",
141 1.28 augustss /* maj */ USB_CDEV_MAJOR,
142 1.28 augustss /* dump */ nodump,
143 1.28 augustss /* psize */ nopsize,
144 1.28 augustss /* flags */ 0,
145 1.28 augustss /* bmaj */ -1
146 1.7 augustss };
147 1.7 augustss #endif
148 1.7 augustss
149 1.45 augustss Static usbd_status usb_discover(struct usb_softc *);
150 1.45 augustss Static void usb_create_event_thread(void *);
151 1.45 augustss Static void usb_event_thread(void *);
152 1.1 augustss
153 1.41 augustss #define USB_MAX_EVENTS 100
154 1.26 augustss struct usb_event_q {
155 1.26 augustss struct usb_event ue;
156 1.26 augustss SIMPLEQ_ENTRY(usb_event_q) next;
157 1.26 augustss };
158 1.42 augustss Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
159 1.29 augustss SIMPLEQ_HEAD_INITIALIZER(usb_events);
160 1.42 augustss Static int usb_nevents = 0;
161 1.42 augustss Static struct selinfo usb_selevent;
162 1.47 augustss Static struct proc *usb_async_proc; /* process that wants USB SIGIO */
163 1.42 augustss Static int usb_dev_open = 0;
164 1.45 augustss Static void usb_add_event(int, struct usb_event *);
165 1.26 augustss
166 1.45 augustss Static int usb_get_next_event(struct usb_event *);
167 1.23 augustss
168 1.42 augustss Static const char *usbrev_str[] = USBREV_STR;
169 1.31 augustss
170 1.28 augustss USB_DECLARE_DRIVER(usb);
171 1.1 augustss
172 1.7 augustss USB_MATCH(usb)
173 1.1 augustss {
174 1.1 augustss DPRINTF(("usbd_match\n"));
175 1.7 augustss return (UMATCH_GENERIC);
176 1.1 augustss }
177 1.1 augustss
178 1.7 augustss USB_ATTACH(usb)
179 1.1 augustss {
180 1.17 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
181 1.1 augustss struct usb_softc *sc = (struct usb_softc *)self;
182 1.7 augustss #elif defined(__FreeBSD__)
183 1.11 augustss struct usb_softc *sc = device_get_softc(self);
184 1.11 augustss void *aux = device_get_ivars(self);
185 1.7 augustss #endif
186 1.1 augustss usbd_device_handle dev;
187 1.29 augustss usbd_status err;
188 1.31 augustss int usbrev;
189 1.38 augustss struct usb_event ue;
190 1.1 augustss
191 1.32 augustss #if defined(__FreeBSD__)
192 1.32 augustss printf("%s", USBDEVNAME(sc->sc_dev));
193 1.11 augustss sc->sc_dev = self;
194 1.7 augustss #endif
195 1.4 augustss
196 1.1 augustss DPRINTF(("usbd_attach\n"));
197 1.31 augustss
198 1.4 augustss usbd_init();
199 1.1 augustss sc->sc_bus = aux;
200 1.1 augustss sc->sc_bus->usbctl = sc;
201 1.1 augustss sc->sc_port.power = USB_MAX_POWER;
202 1.31 augustss
203 1.31 augustss usbrev = sc->sc_bus->usbrev;
204 1.32 augustss printf(": USB revision %s", usbrev_str[usbrev]);
205 1.31 augustss if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
206 1.31 augustss printf(", not supported\n");
207 1.31 augustss USB_ATTACH_ERROR_RETURN;
208 1.32 augustss }
209 1.32 augustss printf("\n");
210 1.31 augustss
211 1.35 augustss /* Make sure not to use tsleep() if we are cold booting. */
212 1.35 augustss if (cold)
213 1.35 augustss sc->sc_bus->use_polling++;
214 1.35 augustss
215 1.38 augustss ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
216 1.38 augustss usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
217 1.38 augustss
218 1.29 augustss err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
219 1.29 augustss &sc->sc_port);
220 1.29 augustss if (!err) {
221 1.1 augustss dev = sc->sc_port.device;
222 1.29 augustss if (dev->hub == NULL) {
223 1.22 augustss sc->sc_dying = 1;
224 1.7 augustss printf("%s: root device is not a hub\n",
225 1.7 augustss USBDEVNAME(sc->sc_dev));
226 1.7 augustss USB_ATTACH_ERROR_RETURN;
227 1.1 augustss }
228 1.1 augustss sc->sc_bus->root_hub = dev;
229 1.24 augustss #if 1
230 1.24 augustss /*
231 1.24 augustss * Turning this code off will delay attachment of USB devices
232 1.24 augustss * until the USB event thread is running, which means that
233 1.24 augustss * the keyboard will not work until after cold boot.
234 1.24 augustss */
235 1.36 augustss if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
236 1.24 augustss dev->hub->explore(sc->sc_bus->root_hub);
237 1.24 augustss #endif
238 1.1 augustss } else {
239 1.1 augustss printf("%s: root hub problem, error=%d\n",
240 1.29 augustss USBDEVNAME(sc->sc_dev), err);
241 1.22 augustss sc->sc_dying = 1;
242 1.1 augustss }
243 1.35 augustss if (cold)
244 1.35 augustss sc->sc_bus->use_polling--;
245 1.7 augustss
246 1.37 thorpej config_pending_incr();
247 1.43 augustss usb_kthread_create(usb_create_event_thread, sc);
248 1.28 augustss
249 1.28 augustss #if defined(__FreeBSD__)
250 1.28 augustss make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
251 1.28 augustss 0644, "usb%d", device_get_unit(self));
252 1.28 augustss #endif
253 1.13 augustss
254 1.7 augustss USB_ATTACH_SUCCESS_RETURN;
255 1.1 augustss }
256 1.1 augustss
257 1.28 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
258 1.13 augustss void
259 1.45 augustss usb_create_event_thread(void *arg)
260 1.13 augustss {
261 1.13 augustss struct usb_softc *sc = arg;
262 1.13 augustss
263 1.43 augustss if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
264 1.13 augustss "%s", sc->sc_dev.dv_xname)) {
265 1.13 augustss printf("%s: unable to create event thread for\n",
266 1.13 augustss sc->sc_dev.dv_xname);
267 1.13 augustss panic("usb_create_event_thread");
268 1.13 augustss }
269 1.13 augustss }
270 1.13 augustss
271 1.13 augustss void
272 1.45 augustss usb_event_thread(void *arg)
273 1.13 augustss {
274 1.13 augustss struct usb_softc *sc = arg;
275 1.37 thorpej int first = 1;
276 1.13 augustss
277 1.27 augustss DPRINTF(("usb_event_thread: start\n"));
278 1.40 augustss
279 1.40 augustss /* Make sure first discover does something. */
280 1.40 augustss sc->sc_bus->needs_explore = 1;
281 1.27 augustss
282 1.22 augustss while (!sc->sc_dying) {
283 1.21 augustss #ifdef USB_DEBUG
284 1.34 augustss if (usb_noexplore < 2)
285 1.21 augustss #endif
286 1.22 augustss usb_discover(sc);
287 1.37 thorpej if (first) {
288 1.37 thorpej config_pending_decr();
289 1.37 thorpej first = 0;
290 1.37 thorpej }
291 1.46 thorpej #ifdef USB_DEBUG
292 1.46 thorpej (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
293 1.46 thorpej usb_noexplore ? 0 : hz * 60);
294 1.46 thorpej #else
295 1.34 augustss (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
296 1.46 thorpej hz * 60);
297 1.34 augustss #endif
298 1.13 augustss DPRINTFN(2,("usb_event_thread: woke up\n"));
299 1.13 augustss }
300 1.22 augustss sc->sc_event_thread = 0;
301 1.13 augustss
302 1.13 augustss /* In case parent is waiting for us to exit. */
303 1.13 augustss wakeup(sc);
304 1.13 augustss
305 1.27 augustss DPRINTF(("usb_event_thread: exit\n"));
306 1.13 augustss kthread_exit(0);
307 1.13 augustss }
308 1.13 augustss
309 1.1 augustss int
310 1.45 augustss usbctlprint(void *aux, const char *pnp)
311 1.1 augustss {
312 1.1 augustss /* only "usb"es can attach to host controllers */
313 1.1 augustss if (pnp)
314 1.1 augustss printf("usb at %s", pnp);
315 1.1 augustss
316 1.1 augustss return (UNCONF);
317 1.1 augustss }
318 1.28 augustss #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
319 1.7 augustss
320 1.1 augustss int
321 1.45 augustss usbopen(dev_t dev, int flag, int mode, struct proc *p)
322 1.1 augustss {
323 1.26 augustss int unit = minor(dev);
324 1.26 augustss struct usb_softc *sc;
325 1.26 augustss
326 1.26 augustss if (unit == USB_DEV_MINOR) {
327 1.26 augustss if (usb_dev_open)
328 1.26 augustss return (EBUSY);
329 1.26 augustss usb_dev_open = 1;
330 1.26 augustss usb_async_proc = 0;
331 1.26 augustss return (0);
332 1.26 augustss }
333 1.26 augustss
334 1.26 augustss USB_GET_SC_OPEN(usb, unit, sc);
335 1.1 augustss
336 1.22 augustss if (sc->sc_dying)
337 1.22 augustss return (EIO);
338 1.1 augustss
339 1.1 augustss return (0);
340 1.1 augustss }
341 1.1 augustss
342 1.1 augustss int
343 1.45 augustss usbread(dev_t dev, struct uio *uio, int flag)
344 1.26 augustss {
345 1.26 augustss struct usb_event ue;
346 1.26 augustss int s, error, n;
347 1.26 augustss
348 1.26 augustss if (minor(dev) != USB_DEV_MINOR)
349 1.26 augustss return (ENXIO);
350 1.26 augustss
351 1.26 augustss if (uio->uio_resid != sizeof(struct usb_event))
352 1.26 augustss return (EINVAL);
353 1.26 augustss
354 1.26 augustss error = 0;
355 1.26 augustss s = splusb();
356 1.26 augustss for (;;) {
357 1.26 augustss n = usb_get_next_event(&ue);
358 1.26 augustss if (n != 0)
359 1.26 augustss break;
360 1.26 augustss if (flag & IO_NDELAY) {
361 1.26 augustss error = EWOULDBLOCK;
362 1.26 augustss break;
363 1.26 augustss }
364 1.26 augustss error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
365 1.26 augustss if (error)
366 1.26 augustss break;
367 1.26 augustss }
368 1.26 augustss splx(s);
369 1.26 augustss if (!error)
370 1.30 augustss error = uiomove((void *)&ue, uio->uio_resid, uio);
371 1.26 augustss
372 1.26 augustss return (error);
373 1.26 augustss }
374 1.26 augustss
375 1.26 augustss int
376 1.45 augustss usbclose(dev_t dev, int flag, int mode, struct proc *p)
377 1.1 augustss {
378 1.26 augustss int unit = minor(dev);
379 1.26 augustss
380 1.26 augustss if (unit == USB_DEV_MINOR) {
381 1.26 augustss usb_async_proc = 0;
382 1.26 augustss usb_dev_open = 0;
383 1.26 augustss }
384 1.26 augustss
385 1.1 augustss return (0);
386 1.1 augustss }
387 1.1 augustss
388 1.1 augustss int
389 1.45 augustss usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
390 1.1 augustss {
391 1.26 augustss struct usb_softc *sc;
392 1.28 augustss int unit = minor(devt);
393 1.26 augustss
394 1.26 augustss if (unit == USB_DEV_MINOR) {
395 1.26 augustss switch (cmd) {
396 1.26 augustss case FIONBIO:
397 1.26 augustss /* All handled in the upper FS layer. */
398 1.26 augustss return (0);
399 1.26 augustss
400 1.26 augustss case FIOASYNC:
401 1.26 augustss if (*(int *)data)
402 1.26 augustss usb_async_proc = p;
403 1.26 augustss else
404 1.26 augustss usb_async_proc = 0;
405 1.26 augustss return (0);
406 1.26 augustss
407 1.26 augustss default:
408 1.26 augustss return (EINVAL);
409 1.26 augustss }
410 1.26 augustss }
411 1.26 augustss
412 1.26 augustss USB_GET_SC(usb, unit, sc);
413 1.1 augustss
414 1.22 augustss if (sc->sc_dying)
415 1.22 augustss return (EIO);
416 1.22 augustss
417 1.1 augustss switch (cmd) {
418 1.28 augustss #if defined(__FreeBSD__)
419 1.30 augustss /* This part should be deleted when kthreads is available */
420 1.28 augustss case USB_DISCOVER:
421 1.28 augustss usb_discover(sc);
422 1.28 augustss break;
423 1.28 augustss #endif
424 1.1 augustss #ifdef USB_DEBUG
425 1.1 augustss case USB_SETDEBUG:
426 1.30 augustss usbdebug = ((*(int *)data) & 0x000000ff);
427 1.30 augustss #ifdef UHCI_DEBUG
428 1.30 augustss uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
429 1.30 augustss #endif
430 1.30 augustss #ifdef OHCI_DEBUG
431 1.30 augustss ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
432 1.30 augustss #endif
433 1.1 augustss break;
434 1.1 augustss #endif
435 1.1 augustss case USB_REQUEST:
436 1.1 augustss {
437 1.1 augustss struct usb_ctl_request *ur = (void *)data;
438 1.1 augustss int len = UGETW(ur->request.wLength);
439 1.1 augustss struct iovec iov;
440 1.1 augustss struct uio uio;
441 1.1 augustss void *ptr = 0;
442 1.1 augustss int addr = ur->addr;
443 1.29 augustss usbd_status err;
444 1.1 augustss int error = 0;
445 1.1 augustss
446 1.9 augustss DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
447 1.1 augustss if (len < 0 || len > 32768)
448 1.9 augustss return (EINVAL);
449 1.1 augustss if (addr < 0 || addr >= USB_MAX_DEVICES ||
450 1.1 augustss sc->sc_bus->devices[addr] == 0)
451 1.9 augustss return (EINVAL);
452 1.1 augustss if (len != 0) {
453 1.1 augustss iov.iov_base = (caddr_t)ur->data;
454 1.1 augustss iov.iov_len = len;
455 1.1 augustss uio.uio_iov = &iov;
456 1.1 augustss uio.uio_iovcnt = 1;
457 1.1 augustss uio.uio_resid = len;
458 1.1 augustss uio.uio_offset = 0;
459 1.1 augustss uio.uio_segflg = UIO_USERSPACE;
460 1.1 augustss uio.uio_rw =
461 1.1 augustss ur->request.bmRequestType & UT_READ ?
462 1.1 augustss UIO_READ : UIO_WRITE;
463 1.1 augustss uio.uio_procp = p;
464 1.1 augustss ptr = malloc(len, M_TEMP, M_WAITOK);
465 1.1 augustss if (uio.uio_rw == UIO_WRITE) {
466 1.1 augustss error = uiomove(ptr, len, &uio);
467 1.1 augustss if (error)
468 1.1 augustss goto ret;
469 1.1 augustss }
470 1.1 augustss }
471 1.29 augustss err = usbd_do_request_flags(sc->sc_bus->devices[addr],
472 1.29 augustss &ur->request, ptr, ur->flags, &ur->actlen);
473 1.29 augustss if (err) {
474 1.1 augustss error = EIO;
475 1.1 augustss goto ret;
476 1.1 augustss }
477 1.1 augustss if (len != 0) {
478 1.1 augustss if (uio.uio_rw == UIO_READ) {
479 1.1 augustss error = uiomove(ptr, len, &uio);
480 1.1 augustss if (error)
481 1.1 augustss goto ret;
482 1.1 augustss }
483 1.1 augustss }
484 1.1 augustss ret:
485 1.1 augustss if (ptr)
486 1.1 augustss free(ptr, M_TEMP);
487 1.1 augustss return (error);
488 1.1 augustss }
489 1.1 augustss
490 1.1 augustss case USB_DEVICEINFO:
491 1.1 augustss {
492 1.1 augustss struct usb_device_info *di = (void *)data;
493 1.1 augustss int addr = di->addr;
494 1.30 augustss usbd_device_handle dev;
495 1.1 augustss
496 1.1 augustss if (addr < 1 || addr >= USB_MAX_DEVICES)
497 1.1 augustss return (EINVAL);
498 1.30 augustss dev = sc->sc_bus->devices[addr];
499 1.30 augustss if (dev == NULL)
500 1.1 augustss return (ENXIO);
501 1.48 augustss usbd_fill_deviceinfo(dev, di, 1);
502 1.1 augustss break;
503 1.1 augustss }
504 1.2 augustss
505 1.2 augustss case USB_DEVICESTATS:
506 1.2 augustss *(struct usb_device_stats *)data = sc->sc_bus->stats;
507 1.2 augustss break;
508 1.1 augustss
509 1.1 augustss default:
510 1.26 augustss return (EINVAL);
511 1.1 augustss }
512 1.1 augustss return (0);
513 1.1 augustss }
514 1.1 augustss
515 1.1 augustss int
516 1.45 augustss usbpoll(dev_t dev, int events, struct proc *p)
517 1.1 augustss {
518 1.26 augustss int revents, mask, s;
519 1.1 augustss
520 1.30 augustss if (minor(dev) == USB_DEV_MINOR) {
521 1.30 augustss revents = 0;
522 1.30 augustss mask = POLLIN | POLLRDNORM;
523 1.30 augustss
524 1.30 augustss s = splusb();
525 1.30 augustss if (events & mask && usb_nevents > 0)
526 1.30 augustss revents |= events & mask;
527 1.30 augustss if (revents == 0 && events & mask)
528 1.30 augustss selrecord(p, &usb_selevent);
529 1.30 augustss splx(s);
530 1.30 augustss
531 1.30 augustss return (revents);
532 1.30 augustss } else {
533 1.30 augustss #if defined(__FreeBSD__)
534 1.30 augustss /* This part should be deleted when kthreads is available */
535 1.30 augustss struct usb_softc *sc;
536 1.30 augustss int unit = minor(dev);
537 1.30 augustss
538 1.30 augustss USB_GET_SC(usb, unit, sc);
539 1.30 augustss
540 1.30 augustss revents = 0;
541 1.30 augustss mask = POLLOUT | POLLRDNORM;
542 1.22 augustss
543 1.30 augustss s = splusb();
544 1.30 augustss if (events & mask && sc->sc_bus->needs_explore)
545 1.26 augustss revents |= events & mask;
546 1.30 augustss if (revents == 0 && events & mask)
547 1.30 augustss selrecord(p, &sc->sc_consel);
548 1.30 augustss splx(s);
549 1.26 augustss
550 1.30 augustss return (revents);
551 1.30 augustss #else
552 1.30 augustss return (ENXIO);
553 1.30 augustss #endif
554 1.1 augustss }
555 1.1 augustss }
556 1.1 augustss
557 1.25 augustss /* Explore device tree from the root. */
558 1.1 augustss usbd_status
559 1.45 augustss usb_discover(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.45 augustss usb_needs_explore(usbd_bus_handle bus)
593 1.1 augustss {
594 1.1 augustss bus->needs_explore = 1;
595 1.30 augustss #if defined(__FreeBSD__)
596 1.30 augustss /* This part should be deleted when kthreads is available */
597 1.30 augustss selwakeup(&bus->usbctl->sc_consel);
598 1.30 augustss #endif
599 1.13 augustss wakeup(&bus->needs_explore);
600 1.1 augustss }
601 1.7 augustss
602 1.26 augustss /* Called at splusb() */
603 1.26 augustss int
604 1.45 augustss usb_get_next_event(struct usb_event *ue)
605 1.26 augustss {
606 1.26 augustss struct usb_event_q *ueq;
607 1.26 augustss
608 1.26 augustss if (usb_nevents <= 0)
609 1.26 augustss return (0);
610 1.26 augustss ueq = SIMPLEQ_FIRST(&usb_events);
611 1.26 augustss *ue = ueq->ue;
612 1.26 augustss SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
613 1.26 augustss free(ueq, M_USBDEV);
614 1.26 augustss usb_nevents--;
615 1.26 augustss return (1);
616 1.26 augustss }
617 1.26 augustss
618 1.26 augustss void
619 1.45 augustss usbd_add_dev_event(int type, usbd_device_handle udev)
620 1.38 augustss {
621 1.38 augustss struct usb_event ue;
622 1.38 augustss
623 1.48 augustss usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
624 1.38 augustss usb_add_event(type, &ue);
625 1.38 augustss }
626 1.38 augustss
627 1.38 augustss void
628 1.45 augustss usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
629 1.38 augustss {
630 1.38 augustss struct usb_event ue;
631 1.38 augustss
632 1.38 augustss ue.u.ue_driver.ue_cookie = udev->cookie;
633 1.38 augustss strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
634 1.38 augustss sizeof ue.u.ue_driver.ue_devname);
635 1.38 augustss usb_add_event(type, &ue);
636 1.38 augustss }
637 1.38 augustss
638 1.42 augustss Static void
639 1.45 augustss usb_add_event(int type, struct usb_event *uep)
640 1.26 augustss {
641 1.26 augustss struct usb_event_q *ueq;
642 1.26 augustss struct usb_event ue;
643 1.26 augustss struct timeval thetime;
644 1.26 augustss int s;
645 1.26 augustss
646 1.38 augustss microtime(&thetime);
647 1.38 augustss /* Don't want to wait here inside splusb() */
648 1.38 augustss ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK);
649 1.38 augustss ueq->ue = *uep;
650 1.38 augustss ueq->ue.ue_type = type;
651 1.38 augustss TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
652 1.38 augustss
653 1.26 augustss s = splusb();
654 1.26 augustss if (++usb_nevents >= USB_MAX_EVENTS) {
655 1.26 augustss /* Too many queued events, drop an old one. */
656 1.26 augustss DPRINTFN(-1,("usb: event dropped\n"));
657 1.26 augustss (void)usb_get_next_event(&ue);
658 1.26 augustss }
659 1.26 augustss SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
660 1.26 augustss wakeup(&usb_events);
661 1.26 augustss selwakeup(&usb_selevent);
662 1.29 augustss if (usb_async_proc != NULL)
663 1.26 augustss psignal(usb_async_proc, SIGIO);
664 1.26 augustss splx(s);
665 1.39 augustss }
666 1.39 augustss void
667 1.45 augustss usb_schedsoftintr(struct usbd_bus *bus)
668 1.39 augustss {
669 1.39 augustss bus->methods->soft_intr(bus);
670 1.26 augustss }
671 1.26 augustss
672 1.28 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
673 1.7 augustss int
674 1.45 augustss usb_activate(device_ptr_t self, enum devact act)
675 1.7 augustss {
676 1.22 augustss struct usb_softc *sc = (struct usb_softc *)self;
677 1.25 augustss usbd_device_handle dev = sc->sc_port.device;
678 1.25 augustss int i, rv = 0;
679 1.22 augustss
680 1.22 augustss switch (act) {
681 1.22 augustss case DVACT_ACTIVATE:
682 1.22 augustss return (EOPNOTSUPP);
683 1.22 augustss break;
684 1.22 augustss
685 1.22 augustss case DVACT_DEACTIVATE:
686 1.22 augustss sc->sc_dying = 1;
687 1.25 augustss if (dev && dev->cdesc && dev->subdevs) {
688 1.25 augustss for (i = 0; dev->subdevs[i]; i++)
689 1.25 augustss rv |= config_deactivate(dev->subdevs[i]);
690 1.25 augustss }
691 1.22 augustss break;
692 1.22 augustss }
693 1.22 augustss return (rv);
694 1.13 augustss }
695 1.7 augustss
696 1.13 augustss int
697 1.45 augustss usb_detach(device_ptr_t self, int flags)
698 1.13 augustss {
699 1.22 augustss struct usb_softc *sc = (struct usb_softc *)self;
700 1.38 augustss struct usb_event ue;
701 1.22 augustss
702 1.27 augustss DPRINTF(("usb_detach: start\n"));
703 1.27 augustss
704 1.22 augustss sc->sc_dying = 1;
705 1.22 augustss
706 1.22 augustss /* Make all devices disconnect. */
707 1.25 augustss if (sc->sc_port.device)
708 1.27 augustss usb_disconnect_port(&sc->sc_port, self);
709 1.22 augustss
710 1.22 augustss /* Kill off event thread. */
711 1.22 augustss if (sc->sc_event_thread) {
712 1.22 augustss wakeup(&sc->sc_bus->needs_explore);
713 1.22 augustss if (tsleep(sc, PWAIT, "usbdet", hz * 60))
714 1.22 augustss printf("%s: event thread didn't die\n",
715 1.22 augustss USBDEVNAME(sc->sc_dev));
716 1.27 augustss DPRINTF(("usb_detach: event thread dead\n"));
717 1.22 augustss }
718 1.22 augustss
719 1.26 augustss usbd_finish();
720 1.38 augustss
721 1.38 augustss ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
722 1.38 augustss usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
723 1.38 augustss
724 1.7 augustss return (0);
725 1.7 augustss }
726 1.28 augustss #elif defined(__FreeBSD__)
727 1.28 augustss int
728 1.28 augustss usb_detach(device_t self)
729 1.28 augustss {
730 1.28 augustss DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
731 1.28 augustss
732 1.28 augustss return (EINVAL);
733 1.28 augustss }
734 1.28 augustss #endif
735 1.28 augustss
736 1.7 augustss
737 1.13 augustss #if defined(__FreeBSD__)
738 1.28 augustss DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
739 1.28 augustss DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
740 1.7 augustss #endif
741