usb.c revision 1.22 1 /* $NetBSD: usb.c,v 1.22 1999/09/15 10:25:31 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) carlstedt.se) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * USB specifications and other documentation can be found at
42 * http://www.usb.org/developers/data/ and
43 * http://www.usb.org/developers/index.html .
44 */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #if defined(__NetBSD__) || defined(__OpenBSD__)
51 #include <sys/device.h>
52 #include <sys/kthread.h>
53 #elif defined(__FreeBSD__)
54 #include <sys/module.h>
55 #include <sys/bus.h>
56 #include <sys/ioccom.h>
57 #include <sys/uio.h>
58 #include <sys/conf.h>
59 #endif
60 #include <sys/conf.h>
61 #include <sys/poll.h>
62 #include <sys/proc.h>
63 #include <sys/select.h>
64
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
68
69 #if defined(__FreeBSD__)
70 MALLOC_DEFINE(M_USB, "USB", "USB");
71 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
72 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
73
74 #include "usb_if.h"
75 #endif /* defined(__FreeBSD__) */
76
77 #include <machine/bus.h>
78
79 #include <dev/usb/usbdivar.h>
80 #include <dev/usb/usb_quirks.h>
81
82 #ifdef USB_DEBUG
83 #define DPRINTF(x) if (usbdebug) logprintf x
84 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
85 int usbdebug = 0;
86 int uhcidebug;
87 int ohcidebug;
88 int usb_noexplore = 0;
89 #else
90 #define DPRINTF(x)
91 #define DPRINTFN(n,x)
92 #endif
93
94 int usb_nbus = 0;
95
96 #define USBUNIT(dev) (minor(dev))
97
98 struct usb_softc {
99 USBBASEDEVICE sc_dev; /* base device */
100 usbd_bus_handle sc_bus; /* USB controller */
101 struct usbd_port sc_port; /* dummy port for root hub */
102 char sc_exploring;
103 char sc_dying;
104 struct selinfo sc_consel; /* waiting for connect change */
105 struct proc *sc_event_thread;
106 };
107
108 #if defined(__NetBSD__) || defined(__OpenBSD__)
109 cdev_decl(usb);
110 #elif defined(__FreeBSD__)
111 d_open_t usbopen;
112 d_close_t usbclose;
113 d_ioctl_t usbioctl;
114 int usbpoll __P((dev_t, int, struct proc *));
115
116 struct cdevsw usb_cdevsw = {
117 usbopen, usbclose, noread, nowrite,
118 usbioctl, nullstop, nullreset, nodevtotty,
119 usbpoll, nommap, nostrat,
120 "usb", NULL, -1
121 };
122 #endif
123
124 usbd_status usb_discover __P((struct usb_softc *));
125 void usb_create_event_thread __P((void *));
126 void usb_event_thread __P((void *));
127
128 USB_DECLARE_DRIVER_INIT(usb, DEVMETHOD(bus_print_child, usbd_print_child));
129
130 USB_MATCH(usb)
131 {
132 DPRINTF(("usbd_match\n"));
133 return (UMATCH_GENERIC);
134 }
135
136 USB_ATTACH(usb)
137 {
138 #if defined(__NetBSD__) || defined(__OpenBSD__)
139 struct usb_softc *sc = (struct usb_softc *)self;
140 #elif defined(__FreeBSD__)
141 struct usb_softc *sc = device_get_softc(self);
142 void *aux = device_get_ivars(self);
143 #endif
144 usbd_device_handle dev;
145 usbd_status r;
146
147 #if defined(__NetBSD__) || defined(__OpenBSD__)
148 printf("\n");
149 #elif defined(__FreeBSD__)
150 sc->sc_dev = self;
151 #endif
152
153 DPRINTF(("usbd_attach\n"));
154 usbd_init();
155 sc->sc_bus = aux;
156 sc->sc_bus->usbctl = sc;
157 sc->sc_bus->use_polling = 1;
158 sc->sc_port.power = USB_MAX_POWER;
159 r = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0,0,0,
160 &sc->sc_port);
161
162 if (r == USBD_NORMAL_COMPLETION) {
163 dev = sc->sc_port.device;
164 if (!dev->hub) {
165 sc->sc_dying = 1;
166 printf("%s: root device is not a hub\n",
167 USBDEVNAME(sc->sc_dev));
168 USB_ATTACH_ERROR_RETURN;
169 }
170 sc->sc_bus->root_hub = dev;
171 dev->hub->explore(sc->sc_bus->root_hub);
172 } else {
173 printf("%s: root hub problem, error=%d\n",
174 USBDEVNAME(sc->sc_dev), r);
175 sc->sc_dying = 1;
176 }
177 sc->sc_bus->use_polling = 0;
178
179 kthread_create(usb_create_event_thread, sc);
180
181 usb_nbus++;
182 USB_ATTACH_SUCCESS_RETURN;
183 }
184
185 void
186 usb_create_event_thread(arg)
187 void *arg;
188 {
189 struct usb_softc *sc = arg;
190
191 if (kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
192 "%s", sc->sc_dev.dv_xname)) {
193 printf("%s: unable to create event thread for\n",
194 sc->sc_dev.dv_xname);
195 panic("usb_create_event_thread");
196 }
197 }
198
199 void
200 usb_event_thread(arg)
201 void *arg;
202 {
203 struct usb_softc *sc = arg;
204
205 while (!sc->sc_dying) {
206 #ifdef USB_DEBUG
207 if (!usb_noexplore)
208 #endif
209 usb_discover(sc);
210 (void)tsleep(&sc->sc_bus->needs_explore,
211 PWAIT, "usbevt", hz*60);
212 DPRINTFN(2,("usb_event_thread: woke up\n"));
213 }
214 sc->sc_event_thread = 0;
215
216 /* In case parent is waiting for us to exit. */
217 wakeup(sc);
218
219 kthread_exit(0);
220 }
221
222 #if defined(__NetBSD__) || defined(__OpenBSD__)
223 int
224 usbctlprint(aux, pnp)
225 void *aux;
226 const char *pnp;
227 {
228 /* only "usb"es can attach to host controllers */
229 if (pnp)
230 printf("usb at %s", pnp);
231
232 return (UNCONF);
233 }
234 #endif
235
236 int
237 usbopen(dev, flag, mode, p)
238 dev_t dev;
239 int flag, mode;
240 struct proc *p;
241 {
242 USB_GET_SC_OPEN(usb, USBUNIT(dev), sc);
243
244 if (sc == 0)
245 return (ENXIO);
246 if (sc->sc_dying)
247 return (EIO);
248
249 return (0);
250 }
251
252 int
253 usbclose(dev, flag, mode, p)
254 dev_t dev;
255 int flag, mode;
256 struct proc *p;
257 {
258 return (0);
259 }
260
261 int
262 usbioctl(dev, cmd, data, flag, p)
263 dev_t dev;
264 u_long cmd;
265 caddr_t data;
266 int flag;
267 struct proc *p;
268 {
269 USB_GET_SC(usb, USBUNIT(dev), sc);
270
271 if (sc->sc_dying)
272 return (EIO);
273
274 switch (cmd) {
275 #ifdef USB_DEBUG
276 case USB_SETDEBUG:
277 usbdebug = uhcidebug = ohcidebug = *(int *)data;
278 break;
279 #endif
280 #if 0
281 case USB_DISCOVER:
282 usb_discover(sc);
283 break;
284 #endif
285 case USB_REQUEST:
286 {
287 struct usb_ctl_request *ur = (void *)data;
288 int len = UGETW(ur->request.wLength);
289 struct iovec iov;
290 struct uio uio;
291 void *ptr = 0;
292 int addr = ur->addr;
293 usbd_status r;
294 int error = 0;
295
296 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
297 if (len < 0 || len > 32768)
298 return (EINVAL);
299 if (addr < 0 || addr >= USB_MAX_DEVICES ||
300 sc->sc_bus->devices[addr] == 0)
301 return (EINVAL);
302 if (len != 0) {
303 iov.iov_base = (caddr_t)ur->data;
304 iov.iov_len = len;
305 uio.uio_iov = &iov;
306 uio.uio_iovcnt = 1;
307 uio.uio_resid = len;
308 uio.uio_offset = 0;
309 uio.uio_segflg = UIO_USERSPACE;
310 uio.uio_rw =
311 ur->request.bmRequestType & UT_READ ?
312 UIO_READ : UIO_WRITE;
313 uio.uio_procp = p;
314 ptr = malloc(len, M_TEMP, M_WAITOK);
315 if (uio.uio_rw == UIO_WRITE) {
316 error = uiomove(ptr, len, &uio);
317 if (error)
318 goto ret;
319 }
320 }
321 r = usbd_do_request_flags(sc->sc_bus->devices[addr],
322 &ur->request, ptr,
323 ur->flags, &ur->actlen);
324 if (r != USBD_NORMAL_COMPLETION) {
325 error = EIO;
326 goto ret;
327 }
328 if (len != 0) {
329 if (uio.uio_rw == UIO_READ) {
330 error = uiomove(ptr, len, &uio);
331 if (error)
332 goto ret;
333 }
334 }
335 ret:
336 if (ptr)
337 free(ptr, M_TEMP);
338 return (error);
339 }
340
341 case USB_DEVICEINFO:
342 {
343 struct usb_device_info *di = (void *)data;
344 int addr = di->addr;
345 usbd_device_handle dev;
346
347 if (addr < 1 || addr >= USB_MAX_DEVICES)
348 return (EINVAL);
349 dev = sc->sc_bus->devices[addr];
350 if (dev == 0)
351 return (ENXIO);
352 usbd_fill_deviceinfo(dev, di);
353 break;
354 }
355
356 case USB_DEVICESTATS:
357 *(struct usb_device_stats *)data = sc->sc_bus->stats;
358 break;
359
360 default:
361 return (ENXIO);
362 }
363 return (0);
364 }
365
366 int
367 usbpoll(dev, events, p)
368 dev_t dev;
369 int events;
370 struct proc *p;
371 {
372 int revents, s;
373 USB_GET_SC(usb, USBUNIT(dev), sc);
374
375 if (sc->sc_dying)
376 return (EIO);
377
378 DPRINTFN(2, ("usbpoll: sc=%p events=0x%x\n", sc, events));
379 s = splusb();
380 revents = 0;
381 if (events & (POLLOUT | POLLWRNORM))
382 if (sc->sc_bus->needs_explore)
383 revents |= events & (POLLOUT | POLLWRNORM);
384 DPRINTFN(2, ("usbpoll: revents=0x%x\n", revents));
385 if (revents == 0) {
386 if (events & (POLLOUT | POLLWRNORM)) {
387 DPRINTFN(2, ("usbpoll: selrecord\n"));
388 selrecord(p, &sc->sc_consel);
389 }
390 }
391 splx(s);
392 return (revents);
393 }
394
395 usbd_status
396 usb_discover(sc)
397 struct usb_softc *sc;
398 {
399 int s;
400
401 /* Explore device tree from the root */
402 /* We need mutual exclusion while traversing the device tree. */
403 do {
404 s = splusb();
405 while (sc->sc_exploring)
406 tsleep(&sc->sc_exploring, PRIBIO, "usbdis", 0);
407 sc->sc_exploring = 1;
408 sc->sc_bus->needs_explore = 0;
409 splx(s);
410
411 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
412
413 s = splusb();
414 sc->sc_exploring = 0;
415 wakeup(&sc->sc_exploring);
416 splx(s);
417 } while (sc->sc_bus->needs_explore && !sc->sc_dying);
418 return (USBD_NORMAL_COMPLETION);
419 }
420
421 void
422 usb_needs_explore(bus)
423 usbd_bus_handle bus;
424 {
425 bus->needs_explore = 1;
426 selwakeup(&bus->usbctl->sc_consel);
427 wakeup(&bus->needs_explore);
428 }
429
430 int
431 usb_activate(self, act)
432 device_ptr_t self;
433 enum devact act;
434 {
435 struct usb_softc *sc = (struct usb_softc *)self;
436 int rv = 0;
437
438 switch (act) {
439 case DVACT_ACTIVATE:
440 return (EOPNOTSUPP);
441 break;
442
443 case DVACT_DEACTIVATE:
444 sc->sc_dying = 1;
445 break;
446 }
447 return (rv);
448 }
449
450 int
451 usb_detach(self, flags)
452 device_ptr_t self;
453 int flags;
454 {
455 struct usb_softc *sc = (struct usb_softc *)self;
456
457 sc->sc_dying = 1;
458
459 /* Make all devices disconnect. */
460 usb_disconnect_port(&sc->sc_port);
461
462 /* Kill off event thread. */
463 if (sc->sc_event_thread) {
464 wakeup(&sc->sc_bus->needs_explore);
465 if (tsleep(sc, PWAIT, "usbdet", hz * 60))
466 printf("%s: event thread didn't die\n",
467 USBDEVNAME(sc->sc_dev));
468 }
469
470 usb_nbus--;
471 return (0);
472 }
473
474 int
475 usbread(dev, uio, flag)
476 dev_t dev;
477 struct uio *uio;
478 int flag;
479 {
480 /* XXX */
481 return (0);
482 }
483
484 #if defined(__FreeBSD__)
485 DRIVER_MODULE(usb, root, usb_driver, usb_devclass, 0, 0);
486 #endif
487