usb.c revision 1.95.4.2 1 1.95.4.2 itohy /* $NetBSD: usb.c,v 1.95.4.2 2007/06/17 01:22:12 itohy Exp $ */
2 1.1 augustss
3 1.95.4.1 itohy /*-
4 1.62 augustss * 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.80 wiz * http://www.usb.org/developers/docs/ and
43 1.80 wiz * http://www.usb.org/developers/devclass_docs/
44 1.1 augustss */
45 1.55 lukem
46 1.55 lukem #include <sys/cdefs.h>
47 1.95.4.2 itohy __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.95.4.2 2007/06/17 01:22:12 itohy Exp $");
48 1.95.4.1 itohy /* __FBSDID("$FreeBSD: src/sys/dev/usb/usb.c,v 1.111 2006/10/19 01:15:58 iedowse Exp $"); */
49 1.92 pavel
50 1.95.4.1 itohy #if defined(__NetBSD__)
51 1.92 pavel #include "opt_compat_netbsd.h"
52 1.76 dsainty #include "ohci.h"
53 1.76 dsainty #include "uhci.h"
54 1.95.4.1 itohy #endif
55 1.1 augustss
56 1.1 augustss #include <sys/param.h>
57 1.1 augustss #include <sys/systm.h>
58 1.1 augustss #include <sys/kernel.h>
59 1.95.4.1 itohy #include <sys/lock.h>
60 1.1 augustss #include <sys/malloc.h>
61 1.95.4.1 itohy #include <sys/fcntl.h>
62 1.95.4.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
63 1.1 augustss #include <sys/device.h>
64 1.95.4.1 itohy #include <sys/vnode.h>
65 1.95.4.1 itohy #elif defined(__FreeBSD__)
66 1.95.4.1 itohy #if __FreeBSD_version >= 500000
67 1.95.4.1 itohy #include <sys/mutex.h>
68 1.95.4.1 itohy #endif
69 1.95.4.1 itohy #include <sys/unistd.h>
70 1.95.4.1 itohy #include <sys/module.h>
71 1.95.4.1 itohy #include <sys/bus.h>
72 1.95.4.1 itohy #include <sys/filio.h>
73 1.95.4.1 itohy #include <sys/uio.h>
74 1.95.4.1 itohy #endif
75 1.13 augustss #include <sys/kthread.h>
76 1.30 augustss #include <sys/proc.h>
77 1.22 augustss #include <sys/conf.h>
78 1.1 augustss #include <sys/poll.h>
79 1.95.4.1 itohy #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
80 1.95.4.1 itohy #include <sys/selinfo.h>
81 1.95.4.1 itohy #else
82 1.1 augustss #include <sys/select.h>
83 1.95.4.1 itohy #endif
84 1.26 augustss #include <sys/signalvar.h>
85 1.95.4.1 itohy #include <sys/sysctl.h>
86 1.95.4.1 itohy #include <sys/uio.h>
87 1.1 augustss
88 1.1 augustss #include <dev/usb/usb.h>
89 1.13 augustss #include <dev/usb/usbdi.h>
90 1.13 augustss #include <dev/usb/usbdi_util.h>
91 1.1 augustss
92 1.95.4.1 itohy #define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
93 1.95.4.1 itohy #define USB_DEV_MINOR 255 /* event queue device */
94 1.95.4.1 itohy
95 1.95.4.1 itohy MALLOC_DEFINE(M_USB, "USB", "USB");
96 1.95.4.1 itohy MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
97 1.95.4.1 itohy MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
98 1.95.4.1 itohy
99 1.95.4.1 itohy #if defined(__FreeBSD__)
100 1.95.4.1 itohy #include "usb_if.h"
101 1.95.4.1 itohy #endif /* defined(__FreeBSD__) */
102 1.26 augustss
103 1.20 augustss #include <machine/bus.h>
104 1.7 augustss
105 1.1 augustss #include <dev/usb/usbdivar.h>
106 1.1 augustss #include <dev/usb/usb_quirks.h>
107 1.1 augustss
108 1.1 augustss #ifdef USB_DEBUG
109 1.16 augustss #define DPRINTF(x) if (usbdebug) logprintf x
110 1.16 augustss #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
111 1.1 augustss int usbdebug = 0;
112 1.76 dsainty #if defined(UHCI_DEBUG) && NUHCI > 0
113 1.76 dsainty extern int uhcidebug;
114 1.30 augustss #endif
115 1.76 dsainty #if defined(OHCI_DEBUG) && NOHCI > 0
116 1.76 dsainty extern int ohcidebug;
117 1.30 augustss #endif
118 1.66 augustss /*
119 1.34 augustss * 0 - do usual exploration
120 1.34 augustss * 1 - do not use timeout exploration
121 1.34 augustss * >1 - do no exploration
122 1.34 augustss */
123 1.21 augustss int usb_noexplore = 0;
124 1.1 augustss #else
125 1.1 augustss #define DPRINTF(x)
126 1.1 augustss #define DPRINTFN(n,x)
127 1.1 augustss #endif
128 1.1 augustss
129 1.95.4.1 itohy #ifdef __FreeBSD__
130 1.95.4.1 itohy /* Define this unconditionally in case a kernel module is loaded that
131 1.95.4.1 itohy * has been compiled with debugging options.
132 1.95.4.1 itohy */
133 1.95.4.1 itohy SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
134 1.95.4.1 itohy SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
135 1.95.4.1 itohy &usbdebug, 0, "usb debug level");
136 1.95.4.1 itohy #endif
137 1.95.4.1 itohy
138 1.1 augustss struct usb_softc {
139 1.22 augustss USBBASEDEVICE sc_dev; /* base device */
140 1.95.4.1 itohy #ifdef __FreeBSD__
141 1.95.4.1 itohy struct cdev *sc_usbdev; /* /dev/usbN device */
142 1.95.4.1 itohy TAILQ_ENTRY(usb_softc) sc_coldexplist; /* cold needs-explore list */
143 1.95.4.1 itohy #endif
144 1.1 augustss usbd_bus_handle sc_bus; /* USB controller */
145 1.1 augustss struct usbd_port sc_port; /* dummy port for root hub */
146 1.25 augustss
147 1.64 augustss struct proc *sc_event_thread;
148 1.25 augustss
149 1.25 augustss char sc_dying;
150 1.1 augustss };
151 1.1 augustss
152 1.90 joerg struct usb_taskq {
153 1.90 joerg TAILQ_HEAD(, usb_task) tasks;
154 1.90 joerg struct proc *task_thread_proc;
155 1.90 joerg const char *name;
156 1.90 joerg int taskcreated; /* task thread exists. */
157 1.90 joerg };
158 1.90 joerg
159 1.95.4.1 itohy static struct usb_taskq usb_taskq[USB_NUM_TASKQS] = {
160 1.95.4.1 itohy { TAILQ_HEAD_INITIALIZER(usb_taskq[0].tasks), NULL, NULL, 0 },
161 1.95.4.1 itohy { TAILQ_HEAD_INITIALIZER(usb_taskq[1].tasks), NULL, NULL, 0 }
162 1.95.4.1 itohy };
163 1.58 augustss
164 1.95.4.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
165 1.72 gehenna dev_type_open(usbopen);
166 1.72 gehenna dev_type_close(usbclose);
167 1.72 gehenna dev_type_read(usbread);
168 1.72 gehenna dev_type_ioctl(usbioctl);
169 1.72 gehenna dev_type_poll(usbpoll);
170 1.74 jdolecek dev_type_kqfilter(usbkqfilter);
171 1.72 gehenna
172 1.72 gehenna const struct cdevsw usb_cdevsw = {
173 1.72 gehenna usbopen, usbclose, usbread, nowrite, usbioctl,
174 1.88 christos nostop, notty, usbpoll, nommap, usbkqfilter, D_OTHER,
175 1.72 gehenna };
176 1.95.4.1 itohy #elif defined(__FreeBSD__)
177 1.95.4.1 itohy d_open_t usbopen;
178 1.95.4.1 itohy d_close_t usbclose;
179 1.95.4.1 itohy d_read_t usbread;
180 1.95.4.1 itohy d_ioctl_t usbioctl;
181 1.95.4.1 itohy d_poll_t usbpoll;
182 1.95.4.1 itohy
183 1.95.4.1 itohy struct cdevsw usb_cdevsw = {
184 1.95.4.1 itohy .d_version = D_VERSION,
185 1.95.4.1 itohy .d_flags = D_NEEDGIANT,
186 1.95.4.1 itohy .d_open = usbopen,
187 1.95.4.1 itohy .d_close = usbclose,
188 1.95.4.1 itohy .d_read = usbread,
189 1.95.4.1 itohy .d_ioctl = usbioctl,
190 1.95.4.1 itohy .d_poll = usbpoll,
191 1.95.4.1 itohy .d_name = "usb",
192 1.95.4.1 itohy #if __FreeBSD_version < 500014
193 1.95.4.1 itohy .d_bmaj = -1
194 1.95.4.1 itohy #endif
195 1.95.4.1 itohy };
196 1.95.4.1 itohy #endif
197 1.7 augustss
198 1.51 augustss Static void usb_discover(void *);
199 1.95.4.1 itohy #ifdef __FreeBSD__
200 1.95.4.1 itohy Static bus_child_detached_t usb_child_detached;
201 1.95.4.1 itohy #endif
202 1.45 augustss Static void usb_create_event_thread(void *);
203 1.45 augustss Static void usb_event_thread(void *);
204 1.58 augustss Static void usb_task_thread(void *);
205 1.1 augustss
206 1.95.4.1 itohy Static int usb_nbusses; /* Number of /dev/usbN busses. */
207 1.95.4.1 itohy #ifdef __FreeBSD__
208 1.95.4.1 itohy Static struct cdev *usb_dev; /* The /dev/usb device. */
209 1.95.4.1 itohy /* Busses to explore at the end of boot-time device configuration. */
210 1.95.4.1 itohy Static TAILQ_HEAD(, usb_softc) usb_coldexplist =
211 1.95.4.1 itohy TAILQ_HEAD_INITIALIZER(usb_coldexplist);
212 1.95.4.1 itohy #endif
213 1.95.4.1 itohy
214 1.41 augustss #define USB_MAX_EVENTS 100
215 1.26 augustss struct usb_event_q {
216 1.26 augustss struct usb_event ue;
217 1.95.4.1 itohy TAILQ_ENTRY(usb_event_q) next;
218 1.26 augustss };
219 1.95.4.1 itohy Static TAILQ_HEAD(, usb_event_q) usb_events =
220 1.95.4.1 itohy TAILQ_HEAD_INITIALIZER(usb_events);
221 1.42 augustss Static int usb_nevents = 0;
222 1.42 augustss Static struct selinfo usb_selevent;
223 1.95.4.1 itohy Static usb_sigproc_ptr usb_async_proc; /* process that wants USB SIGIO */
224 1.42 augustss Static int usb_dev_open = 0;
225 1.87 christos Static struct usb_event *usb_alloc_event(void);
226 1.87 christos Static void usb_free_event(struct usb_event *);
227 1.45 augustss Static void usb_add_event(int, struct usb_event *);
228 1.26 augustss
229 1.45 augustss Static int usb_get_next_event(struct usb_event *);
230 1.23 augustss
231 1.95.4.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
232 1.92 pavel Static void usb_copy_old_devinfo(struct usb_device_info_old *, const struct usb_device_info *);
233 1.92 pavel #endif
234 1.92 pavel
235 1.42 augustss Static const char *usbrev_str[] = USBREV_STR;
236 1.31 augustss
237 1.95.4.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
238 1.28 augustss USB_DECLARE_DRIVER(usb);
239 1.95.4.1 itohy #elif defined(__FreeBSD__)
240 1.95.4.1 itohy USB_DECLARE_DRIVER_INIT(usb,
241 1.95.4.1 itohy DEVMETHOD(bus_child_detached, usb_child_detached),
242 1.95.4.1 itohy DEVMETHOD(device_suspend, bus_generic_suspend),
243 1.95.4.1 itohy DEVMETHOD(device_resume, bus_generic_resume),
244 1.95.4.1 itohy DEVMETHOD(device_shutdown, bus_generic_shutdown)
245 1.95.4.1 itohy );
246 1.95.4.1 itohy MODULE_VERSION(usb, 1);
247 1.95.4.1 itohy #endif
248 1.1 augustss
249 1.7 augustss USB_MATCH(usb)
250 1.1 augustss {
251 1.1 augustss DPRINTF(("usbd_match\n"));
252 1.7 augustss return (UMATCH_GENERIC);
253 1.1 augustss }
254 1.1 augustss
255 1.7 augustss USB_ATTACH(usb)
256 1.1 augustss {
257 1.95.4.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
258 1.1 augustss struct usb_softc *sc = (struct usb_softc *)self;
259 1.95.4.1 itohy #elif defined(__FreeBSD__)
260 1.95.4.1 itohy struct usb_softc *sc = device_get_softc(self);
261 1.95.4.1 itohy void *aux = device_get_ivars(self);
262 1.95.4.1 itohy #endif
263 1.1 augustss usbd_device_handle dev;
264 1.29 augustss usbd_status err;
265 1.31 augustss int usbrev;
266 1.57 augustss int speed;
267 1.87 christos struct usb_event *ue;
268 1.66 augustss
269 1.95.4.1 itohy #ifdef __FreeBSD__
270 1.95.4.1 itohy sc->sc_dev = self;
271 1.95.4.1 itohy #endif
272 1.95.4.1 itohy
273 1.1 augustss DPRINTF(("usbd_attach\n"));
274 1.31 augustss
275 1.1 augustss sc->sc_bus = aux;
276 1.1 augustss sc->sc_bus->usbctl = sc;
277 1.1 augustss sc->sc_port.power = USB_MAX_POWER;
278 1.31 augustss
279 1.95.4.1 itohy #if defined(__FreeBSD__)
280 1.95.4.1 itohy printf("%s", USBDEVNAME(sc->sc_dev));
281 1.95.4.1 itohy #endif
282 1.31 augustss usbrev = sc->sc_bus->usbrev;
283 1.32 augustss printf(": USB revision %s", usbrev_str[usbrev]);
284 1.57 augustss switch (usbrev) {
285 1.57 augustss case USBREV_1_0:
286 1.57 augustss case USBREV_1_1:
287 1.57 augustss speed = USB_SPEED_FULL;
288 1.57 augustss break;
289 1.57 augustss case USBREV_2_0:
290 1.57 augustss speed = USB_SPEED_HIGH;
291 1.57 augustss break;
292 1.57 augustss default:
293 1.31 augustss printf(", not supported\n");
294 1.57 augustss sc->sc_dying = 1;
295 1.31 augustss USB_ATTACH_ERROR_RETURN;
296 1.32 augustss }
297 1.32 augustss printf("\n");
298 1.31 augustss
299 1.35 augustss /* Make sure not to use tsleep() if we are cold booting. */
300 1.35 augustss if (cold)
301 1.35 augustss sc->sc_bus->use_polling++;
302 1.35 augustss
303 1.87 christos ue = usb_alloc_event();
304 1.87 christos ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
305 1.87 christos usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
306 1.38 augustss
307 1.49 augustss #ifdef USB_USE_SOFTINTR
308 1.49 augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
309 1.49 augustss /* XXX we should have our own level */
310 1.66 augustss sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
311 1.49 augustss sc->sc_bus->methods->soft_intr, sc->sc_bus);
312 1.49 augustss if (sc->sc_bus->soft == NULL) {
313 1.49 augustss printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
314 1.49 augustss sc->sc_dying = 1;
315 1.57 augustss USB_ATTACH_ERROR_RETURN;
316 1.49 augustss }
317 1.49 augustss #else
318 1.70 augustss usb_callout_init(sc->sc_bus->softi);
319 1.49 augustss #endif
320 1.49 augustss #endif
321 1.49 augustss
322 1.57 augustss err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
323 1.29 augustss &sc->sc_port);
324 1.29 augustss if (!err) {
325 1.1 augustss dev = sc->sc_port.device;
326 1.29 augustss if (dev->hub == NULL) {
327 1.22 augustss sc->sc_dying = 1;
328 1.66 augustss printf("%s: root device is not a hub\n",
329 1.7 augustss USBDEVNAME(sc->sc_dev));
330 1.7 augustss USB_ATTACH_ERROR_RETURN;
331 1.1 augustss }
332 1.1 augustss sc->sc_bus->root_hub = dev;
333 1.24 augustss #if 1
334 1.66 augustss /*
335 1.24 augustss * Turning this code off will delay attachment of USB devices
336 1.24 augustss * until the USB event thread is running, which means that
337 1.24 augustss * the keyboard will not work until after cold boot.
338 1.24 augustss */
339 1.95.4.1 itohy #if defined(__FreeBSD__)
340 1.95.4.1 itohy if (cold) {
341 1.95.4.1 itohy /* Explore high-speed busses before others. */
342 1.95.4.1 itohy if (speed == USB_SPEED_HIGH)
343 1.95.4.1 itohy dev->hub->explore(sc->sc_bus->root_hub);
344 1.95.4.1 itohy else
345 1.95.4.1 itohy TAILQ_INSERT_TAIL(&usb_coldexplist, sc,
346 1.95.4.1 itohy sc_coldexplist);
347 1.95.4.1 itohy }
348 1.95.4.1 itohy #else
349 1.86 thorpej if (cold && (device_cfdata(&sc->sc_dev)->cf_flags & 1))
350 1.24 augustss dev->hub->explore(sc->sc_bus->root_hub);
351 1.24 augustss #endif
352 1.95.4.1 itohy #endif
353 1.1 augustss } else {
354 1.66 augustss printf("%s: root hub problem, error=%d\n",
355 1.66 augustss USBDEVNAME(sc->sc_dev), err);
356 1.22 augustss sc->sc_dying = 1;
357 1.1 augustss }
358 1.35 augustss if (cold)
359 1.35 augustss sc->sc_bus->use_polling--;
360 1.7 augustss
361 1.37 thorpej config_pending_incr();
362 1.95.4.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
363 1.43 augustss usb_kthread_create(usb_create_event_thread, sc);
364 1.95.4.1 itohy #endif
365 1.95.4.1 itohy
366 1.95.4.1 itohy usb_nbusses++;
367 1.95.4.1 itohy #if defined(__FreeBSD__)
368 1.95.4.1 itohy usb_create_event_thread(sc);
369 1.95.4.1 itohy /* The per controller devices (used for usb_discover) */
370 1.95.4.1 itohy /* XXX This is redundant now, but old usbd's will want it */
371 1.95.4.1 itohy sc->sc_usbdev = make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT,
372 1.95.4.1 itohy GID_OPERATOR, 0660, "usb%d", device_get_unit(self));
373 1.95.4.1 itohy if (usb_nbusses == 1) {
374 1.95.4.1 itohy /* The device spitting out events */
375 1.95.4.1 itohy usb_dev = make_dev(&usb_cdevsw, USB_DEV_MINOR, UID_ROOT,
376 1.95.4.1 itohy GID_OPERATOR, 0660, "usb");
377 1.95.4.1 itohy }
378 1.95.4.1 itohy #endif
379 1.28 augustss
380 1.7 augustss USB_ATTACH_SUCCESS_RETURN;
381 1.1 augustss }
382 1.1 augustss
383 1.90 joerg static const char *taskq_names[] = USB_TASKQ_NAMES;
384 1.90 joerg
385 1.13 augustss void
386 1.45 augustss usb_create_event_thread(void *arg)
387 1.13 augustss {
388 1.13 augustss struct usb_softc *sc = arg;
389 1.90 joerg struct usb_taskq *taskq;
390 1.90 joerg int i;
391 1.13 augustss
392 1.43 augustss if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
393 1.95.4.1 itohy "%s", USBDEVNAME(sc->sc_dev))) {
394 1.13 augustss printf("%s: unable to create event thread for\n",
395 1.95.4.1 itohy USBDEVNAME(sc->sc_dev));
396 1.13 augustss panic("usb_create_event_thread");
397 1.13 augustss }
398 1.90 joerg for (i = 0; i < USB_NUM_TASKQS; i++) {
399 1.90 joerg taskq = &usb_taskq[i];
400 1.90 joerg
401 1.95.4.1 itohy if (taskq->taskcreated == 0) {
402 1.95.4.1 itohy taskq->taskcreated = 1;
403 1.95.4.1 itohy taskq->name = taskq_names[i];
404 1.95.4.1 itohy if (usb_kthread_create2(usb_task_thread, taskq,
405 1.95.4.1 itohy &taskq->task_thread_proc, taskq->name)) {
406 1.95.4.1 itohy printf("unable to create task thread: %s\n",
407 1.95.4.1 itohy taskq->name);
408 1.95.4.1 itohy panic("usb_create_event_thread task");
409 1.95.4.1 itohy }
410 1.58 augustss }
411 1.58 augustss }
412 1.13 augustss }
413 1.13 augustss
414 1.52 augustss /*
415 1.62 augustss * Add a task to be performed by the task thread. This function can be
416 1.52 augustss * called from any context and the task will be executed in a process
417 1.52 augustss * context ASAP.
418 1.52 augustss */
419 1.13 augustss void
420 1.91 christos usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
421 1.51 augustss {
422 1.90 joerg struct usb_taskq *taskq;
423 1.51 augustss int s;
424 1.51 augustss
425 1.51 augustss s = splusb();
426 1.95.4.1 itohy taskq = &usb_taskq[queue];
427 1.90 joerg if (task->queue == -1) {
428 1.58 augustss DPRINTFN(2,("usb_add_task: task=%p\n", task));
429 1.90 joerg TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
430 1.90 joerg task->queue = queue;
431 1.62 augustss } else {
432 1.58 augustss DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
433 1.62 augustss }
434 1.90 joerg wakeup(&taskq->tasks);
435 1.51 augustss splx(s);
436 1.51 augustss }
437 1.51 augustss
438 1.51 augustss void
439 1.91 christos usb_rem_task(usbd_device_handle dev, struct usb_task *task)
440 1.53 augustss {
441 1.90 joerg struct usb_taskq *taskq;
442 1.53 augustss int s;
443 1.53 augustss
444 1.53 augustss s = splusb();
445 1.90 joerg if (task->queue != -1) {
446 1.95.4.1 itohy taskq = &usb_taskq[task->queue];
447 1.90 joerg TAILQ_REMOVE(&taskq->tasks, task, next);
448 1.90 joerg task->queue = -1;
449 1.53 augustss }
450 1.53 augustss splx(s);
451 1.53 augustss }
452 1.53 augustss
453 1.53 augustss void
454 1.45 augustss usb_event_thread(void *arg)
455 1.13 augustss {
456 1.95.4.1 itohy static int newthread_wchan;
457 1.13 augustss struct usb_softc *sc = arg;
458 1.13 augustss
459 1.95.4.1 itohy #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
460 1.95.4.1 itohy mtx_lock(&Giant);
461 1.95.4.1 itohy #endif
462 1.95.4.1 itohy
463 1.27 augustss DPRINTF(("usb_event_thread: start\n"));
464 1.40 augustss
465 1.60 augustss /*
466 1.60 augustss * In case this controller is a companion controller to an
467 1.60 augustss * EHCI controller we need to wait until the EHCI controller
468 1.95.4.1 itohy * has grabbed the port. What we do here is wait until no new
469 1.95.4.1 itohy * USB threads have been created in a while. XXX we actually
470 1.95.4.1 itohy * just want to wait for the PCI slot to be fully scanned.
471 1.95.4.1 itohy *
472 1.95.4.1 itohy * Note that when you `kldload usb' it actually attaches the
473 1.95.4.1 itohy * devices in order that the drivers appear in the kld, not the
474 1.95.4.1 itohy * normal PCI order, since the addition of each driver within
475 1.95.4.1 itohy * usb.ko (ohci, ehci etc.) causes a separate PCI bus re-scan.
476 1.60 augustss */
477 1.95.4.1 itohy wakeup(&newthread_wchan);
478 1.95.4.1 itohy for (;;) {
479 1.95.4.1 itohy if (tsleep(&newthread_wchan , PWAIT, "usbets", hz * 4) != 0)
480 1.95.4.1 itohy break;
481 1.95.4.1 itohy }
482 1.60 augustss
483 1.40 augustss /* Make sure first discover does something. */
484 1.40 augustss sc->sc_bus->needs_explore = 1;
485 1.51 augustss usb_discover(sc);
486 1.51 augustss config_pending_decr();
487 1.27 augustss
488 1.22 augustss while (!sc->sc_dying) {
489 1.58 augustss #ifdef USB_DEBUG
490 1.58 augustss if (usb_noexplore < 2)
491 1.58 augustss #endif
492 1.58 augustss usb_discover(sc);
493 1.58 augustss #ifdef USB_DEBUG
494 1.58 augustss (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
495 1.58 augustss usb_noexplore ? 0 : hz * 60);
496 1.58 augustss #else
497 1.58 augustss (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
498 1.58 augustss hz * 60);
499 1.58 augustss #endif
500 1.58 augustss DPRINTFN(2,("usb_event_thread: woke up\n"));
501 1.13 augustss }
502 1.50 augustss sc->sc_event_thread = NULL;
503 1.13 augustss
504 1.13 augustss /* In case parent is waiting for us to exit. */
505 1.13 augustss wakeup(sc);
506 1.13 augustss
507 1.27 augustss DPRINTF(("usb_event_thread: exit\n"));
508 1.13 augustss kthread_exit(0);
509 1.13 augustss }
510 1.13 augustss
511 1.58 augustss void
512 1.90 joerg usb_task_thread(void *arg)
513 1.58 augustss {
514 1.58 augustss struct usb_task *task;
515 1.90 joerg struct usb_taskq *taskq;
516 1.58 augustss int s;
517 1.58 augustss
518 1.95.4.1 itohy #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
519 1.95.4.1 itohy mtx_lock(&Giant);
520 1.95.4.1 itohy #endif
521 1.95.4.1 itohy
522 1.90 joerg taskq = arg;
523 1.90 joerg DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
524 1.58 augustss
525 1.58 augustss s = splusb();
526 1.95.4.1 itohy
527 1.95.4.1 itohy while (usb_nbusses > 0) {
528 1.90 joerg task = TAILQ_FIRST(&taskq->tasks);
529 1.58 augustss if (task == NULL) {
530 1.90 joerg tsleep(&taskq->tasks, PWAIT, "usbtsk", 0);
531 1.90 joerg task = TAILQ_FIRST(&taskq->tasks);
532 1.58 augustss }
533 1.58 augustss DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
534 1.58 augustss if (task != NULL) {
535 1.90 joerg TAILQ_REMOVE(&taskq->tasks, task, next);
536 1.90 joerg task->queue = -1;
537 1.58 augustss splx(s);
538 1.58 augustss task->fun(task->arg);
539 1.58 augustss s = splusb();
540 1.58 augustss }
541 1.58 augustss }
542 1.95.4.1 itohy
543 1.95.4.1 itohy splx(s);
544 1.95.4.1 itohy
545 1.95.4.1 itohy taskq->taskcreated = 0;
546 1.95.4.1 itohy wakeup(&taskq->taskcreated);
547 1.95.4.1 itohy
548 1.95.4.1 itohy DPRINTF(("usb_event_thread: exit\n"));
549 1.95.4.1 itohy kthread_exit(0);
550 1.58 augustss }
551 1.58 augustss
552 1.95.4.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
553 1.1 augustss int
554 1.91 christos usbctlprint(void *aux, const char *pnp)
555 1.1 augustss {
556 1.1 augustss /* only "usb"es can attach to host controllers */
557 1.1 augustss if (pnp)
558 1.77 thorpej aprint_normal("usb at %s", pnp);
559 1.1 augustss
560 1.1 augustss return (UNCONF);
561 1.1 augustss }
562 1.28 augustss #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
563 1.7 augustss
564 1.1 augustss int
565 1.95.4.1 itohy usbopen(usb_cdev_t dev, int flag, int mode, usb_proc_ptr p)
566 1.1 augustss {
567 1.95.4.1 itohy int unit = USBUNIT(dev);
568 1.26 augustss struct usb_softc *sc;
569 1.26 augustss
570 1.26 augustss if (unit == USB_DEV_MINOR) {
571 1.26 augustss if (usb_dev_open)
572 1.26 augustss return (EBUSY);
573 1.26 augustss usb_dev_open = 1;
574 1.26 augustss usb_async_proc = 0;
575 1.26 augustss return (0);
576 1.26 augustss }
577 1.26 augustss
578 1.26 augustss USB_GET_SC_OPEN(usb, unit, sc);
579 1.1 augustss
580 1.22 augustss if (sc->sc_dying)
581 1.22 augustss return (EIO);
582 1.1 augustss
583 1.1 augustss return (0);
584 1.1 augustss }
585 1.1 augustss
586 1.1 augustss int
587 1.95.4.1 itohy usbread(usb_cdev_t dev, struct uio *uio, int flag)
588 1.26 augustss {
589 1.92 pavel struct usb_event *ue;
590 1.95.4.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
591 1.93 christos struct usb_event_old *ueo = NULL; /* XXXGCC */
592 1.95.4.1 itohy int useold = 0;
593 1.92 pavel #endif
594 1.95.4.1 itohy int s, error, n;
595 1.26 augustss
596 1.95.4.1 itohy if (USBUNIT(dev) != USB_DEV_MINOR)
597 1.26 augustss return (ENXIO);
598 1.26 augustss
599 1.92 pavel switch (uio->uio_resid) {
600 1.95.4.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
601 1.92 pavel case sizeof(struct usb_event_old):
602 1.92 pavel ueo = malloc(sizeof(struct usb_event_old), M_USBDEV,
603 1.92 pavel M_WAITOK|M_ZERO);
604 1.92 pavel useold = 1;
605 1.92 pavel /* FALLTHRU */
606 1.92 pavel #endif
607 1.92 pavel case sizeof(struct usb_event):
608 1.92 pavel ue = usb_alloc_event();
609 1.92 pavel break;
610 1.92 pavel default:
611 1.26 augustss return (EINVAL);
612 1.92 pavel }
613 1.26 augustss
614 1.26 augustss error = 0;
615 1.26 augustss s = splusb();
616 1.26 augustss for (;;) {
617 1.87 christos n = usb_get_next_event(ue);
618 1.26 augustss if (n != 0)
619 1.26 augustss break;
620 1.95.4.1 itohy if (flag &
621 1.95.4.1 itohy #ifdef __FreeBSD__
622 1.95.4.1 itohy O_NONBLOCK
623 1.95.4.1 itohy #else
624 1.95.4.1 itohy IO_NDELAY
625 1.95.4.1 itohy #endif
626 1.95.4.1 itohy ) {
627 1.26 augustss error = EWOULDBLOCK;
628 1.26 augustss break;
629 1.26 augustss }
630 1.26 augustss error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
631 1.26 augustss if (error)
632 1.26 augustss break;
633 1.26 augustss }
634 1.26 augustss splx(s);
635 1.92 pavel if (!error) {
636 1.95.4.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
637 1.92 pavel if (useold) { /* copy fields to old struct */
638 1.92 pavel ueo->ue_type = ue->ue_type;
639 1.92 pavel memcpy(&ueo->ue_time, &ue->ue_time,
640 1.92 pavel sizeof(struct timespec));
641 1.92 pavel switch (ue->ue_type) {
642 1.92 pavel case USB_EVENT_DEVICE_ATTACH:
643 1.92 pavel case USB_EVENT_DEVICE_DETACH:
644 1.92 pavel usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
645 1.92 pavel break;
646 1.92 pavel
647 1.92 pavel case USB_EVENT_CTRLR_ATTACH:
648 1.92 pavel case USB_EVENT_CTRLR_DETACH:
649 1.92 pavel ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
650 1.92 pavel break;
651 1.92 pavel
652 1.92 pavel case USB_EVENT_DRIVER_ATTACH:
653 1.92 pavel case USB_EVENT_DRIVER_DETACH:
654 1.92 pavel ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
655 1.92 pavel memcpy(ueo->u.ue_driver.ue_devname,
656 1.92 pavel ue->u.ue_driver.ue_devname,
657 1.92 pavel sizeof(ue->u.ue_driver.ue_devname));
658 1.92 pavel break;
659 1.92 pavel default:
660 1.92 pavel ;
661 1.92 pavel }
662 1.92 pavel
663 1.92 pavel error = uiomove((void *)ueo, uio->uio_resid, uio);
664 1.92 pavel } else
665 1.92 pavel #endif
666 1.92 pavel error = uiomove((void *)ue, uio->uio_resid, uio);
667 1.92 pavel }
668 1.87 christos usb_free_event(ue);
669 1.95.4.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
670 1.92 pavel if (useold)
671 1.92 pavel free(ueo, M_USBDEV);
672 1.92 pavel #endif
673 1.26 augustss
674 1.26 augustss return (error);
675 1.26 augustss }
676 1.26 augustss
677 1.26 augustss int
678 1.95.4.1 itohy usbclose(usb_cdev_t dev, int flag, int mode, usb_proc_ptr p)
679 1.1 augustss {
680 1.95.4.1 itohy int unit = USBUNIT(dev);
681 1.26 augustss
682 1.26 augustss if (unit == USB_DEV_MINOR) {
683 1.26 augustss usb_async_proc = 0;
684 1.26 augustss usb_dev_open = 0;
685 1.26 augustss }
686 1.26 augustss
687 1.1 augustss return (0);
688 1.1 augustss }
689 1.1 augustss
690 1.1 augustss int
691 1.95.4.2 itohy usbioctl(usb_cdev_t devt, u_long cmd, usb_ioctlarg_t data, int flag,
692 1.95.4.2 itohy usb_proc_ptr p)
693 1.1 augustss {
694 1.26 augustss struct usb_softc *sc;
695 1.95.4.1 itohy int unit = USBUNIT(devt);
696 1.26 augustss
697 1.26 augustss if (unit == USB_DEV_MINOR) {
698 1.26 augustss switch (cmd) {
699 1.26 augustss case FIONBIO:
700 1.26 augustss /* All handled in the upper FS layer. */
701 1.26 augustss return (0);
702 1.66 augustss
703 1.26 augustss case FIOASYNC:
704 1.26 augustss if (*(int *)data)
705 1.95.4.1 itohy #if defined(__NetBSD__) && __NetBSD_Version__ >= 399001400
706 1.95.4.1 itohy usb_async_proc = p->l_proc;
707 1.95.4.1 itohy #elif defined(__FreeBSD__) && __FreeBSD_version >= 500000
708 1.95.4.1 itohy usb_async_proc = p->td_proc;
709 1.95.4.1 itohy #else
710 1.95.4.1 itohy usb_async_proc = p;
711 1.95.4.1 itohy #endif
712 1.26 augustss else
713 1.26 augustss usb_async_proc = 0;
714 1.26 augustss return (0);
715 1.26 augustss
716 1.26 augustss default:
717 1.26 augustss return (EINVAL);
718 1.26 augustss }
719 1.26 augustss }
720 1.26 augustss
721 1.26 augustss USB_GET_SC(usb, unit, sc);
722 1.1 augustss
723 1.22 augustss if (sc->sc_dying)
724 1.22 augustss return (EIO);
725 1.22 augustss
726 1.1 augustss switch (cmd) {
727 1.95.4.1 itohy #if defined(USB_DEBUG)
728 1.1 augustss case USB_SETDEBUG:
729 1.69 augustss if (!(flag & FWRITE))
730 1.69 augustss return (EBADF);
731 1.30 augustss usbdebug = ((*(int *)data) & 0x000000ff);
732 1.76 dsainty #if defined(UHCI_DEBUG) && NUHCI > 0
733 1.30 augustss uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
734 1.30 augustss #endif
735 1.76 dsainty #if defined(OHCI_DEBUG) && NOHCI > 0
736 1.30 augustss ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
737 1.30 augustss #endif
738 1.1 augustss break;
739 1.56 augustss #endif /* USB_DEBUG */
740 1.95.4.1 itohy #if defined(__FreeBSD__)
741 1.95.4.1 itohy /* This part should be deleted */
742 1.95.4.1 itohy case USB_DISCOVER:
743 1.95.4.1 itohy break;
744 1.95.4.1 itohy #endif
745 1.1 augustss case USB_REQUEST:
746 1.1 augustss {
747 1.1 augustss struct usb_ctl_request *ur = (void *)data;
748 1.68 christos int len = UGETW(ur->ucr_request.wLength);
749 1.1 augustss struct iovec iov;
750 1.1 augustss struct uio uio;
751 1.1 augustss void *ptr = 0;
752 1.68 christos int addr = ur->ucr_addr;
753 1.29 augustss usbd_status err;
754 1.1 augustss int error = 0;
755 1.69 augustss
756 1.69 augustss if (!(flag & FWRITE))
757 1.69 augustss return (EBADF);
758 1.1 augustss
759 1.9 augustss DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
760 1.1 augustss if (len < 0 || len > 32768)
761 1.9 augustss return (EINVAL);
762 1.66 augustss if (addr < 0 || addr >= USB_MAX_DEVICES ||
763 1.1 augustss sc->sc_bus->devices[addr] == 0)
764 1.9 augustss return (EINVAL);
765 1.1 augustss if (len != 0) {
766 1.95.4.2 itohy iov.iov_base = (void *)ur->ucr_data;
767 1.1 augustss iov.iov_len = len;
768 1.1 augustss uio.uio_iov = &iov;
769 1.1 augustss uio.uio_iovcnt = 1;
770 1.1 augustss uio.uio_resid = len;
771 1.1 augustss uio.uio_offset = 0;
772 1.1 augustss uio.uio_rw =
773 1.68 christos ur->ucr_request.bmRequestType & UT_READ ?
774 1.1 augustss UIO_READ : UIO_WRITE;
775 1.95.4.2 itohy USB_UIO_SET_PROC(&uio, p);
776 1.1 augustss ptr = malloc(len, M_TEMP, M_WAITOK);
777 1.1 augustss if (uio.uio_rw == UIO_WRITE) {
778 1.1 augustss error = uiomove(ptr, len, &uio);
779 1.1 augustss if (error)
780 1.1 augustss goto ret;
781 1.1 augustss }
782 1.1 augustss }
783 1.29 augustss err = usbd_do_request_flags(sc->sc_bus->devices[addr],
784 1.68 christos &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
785 1.67 augustss USBD_DEFAULT_TIMEOUT);
786 1.29 augustss if (err) {
787 1.1 augustss error = EIO;
788 1.1 augustss goto ret;
789 1.1 augustss }
790 1.1 augustss if (len != 0) {
791 1.1 augustss if (uio.uio_rw == UIO_READ) {
792 1.1 augustss error = uiomove(ptr, len, &uio);
793 1.1 augustss if (error)
794 1.1 augustss goto ret;
795 1.1 augustss }
796 1.1 augustss }
797 1.1 augustss ret:
798 1.1 augustss if (ptr)
799 1.1 augustss free(ptr, M_TEMP);
800 1.1 augustss return (error);
801 1.1 augustss }
802 1.1 augustss
803 1.1 augustss case USB_DEVICEINFO:
804 1.93 christos {
805 1.93 christos usbd_device_handle dev;
806 1.93 christos struct usb_device_info *di = (void *)data;
807 1.93 christos int addr = di->udi_addr;
808 1.93 christos
809 1.93 christos if (addr < 1 || addr >= USB_MAX_DEVICES)
810 1.93 christos return EINVAL;
811 1.93 christos if ((dev = sc->sc_bus->devices[addr]) == NULL)
812 1.93 christos return ENXIO;
813 1.93 christos usbd_fill_deviceinfo(dev, di, 1);
814 1.93 christos break;
815 1.93 christos }
816 1.93 christos
817 1.95.4.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
818 1.92 pavel case USB_DEVICEINFO_OLD:
819 1.1 augustss {
820 1.30 augustss usbd_device_handle dev;
821 1.93 christos struct usb_device_info_old *di = (void *)data;
822 1.93 christos int addr = di->udi_addr;
823 1.1 augustss
824 1.1 augustss if (addr < 1 || addr >= USB_MAX_DEVICES)
825 1.93 christos return EINVAL;
826 1.93 christos if ((dev = sc->sc_bus->devices[addr]) == NULL)
827 1.93 christos return ENXIO;
828 1.93 christos usbd_fill_deviceinfo_old(dev, di, 1);
829 1.1 augustss break;
830 1.1 augustss }
831 1.93 christos #endif
832 1.2 augustss
833 1.2 augustss case USB_DEVICESTATS:
834 1.2 augustss *(struct usb_device_stats *)data = sc->sc_bus->stats;
835 1.2 augustss break;
836 1.1 augustss
837 1.1 augustss default:
838 1.26 augustss return (EINVAL);
839 1.1 augustss }
840 1.1 augustss return (0);
841 1.1 augustss }
842 1.1 augustss
843 1.1 augustss int
844 1.95.4.1 itohy usbpoll(usb_cdev_t dev, int events, usb_proc_ptr p)
845 1.1 augustss {
846 1.26 augustss int revents, mask, s;
847 1.95.4.1 itohy int unit = USBUNIT(dev);
848 1.1 augustss
849 1.95.4.1 itohy if (unit == USB_DEV_MINOR) {
850 1.30 augustss revents = 0;
851 1.30 augustss mask = POLLIN | POLLRDNORM;
852 1.66 augustss
853 1.30 augustss s = splusb();
854 1.30 augustss if (events & mask && usb_nevents > 0)
855 1.30 augustss revents |= events & mask;
856 1.30 augustss if (revents == 0 && events & mask)
857 1.95.4.1 itohy selrecord(p, &usb_selevent);
858 1.30 augustss splx(s);
859 1.66 augustss
860 1.30 augustss return (revents);
861 1.30 augustss } else {
862 1.82 ws return (0);
863 1.1 augustss }
864 1.1 augustss }
865 1.1 augustss
866 1.95.4.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
867 1.74 jdolecek static void
868 1.74 jdolecek filt_usbrdetach(struct knote *kn)
869 1.74 jdolecek {
870 1.74 jdolecek int s;
871 1.74 jdolecek
872 1.74 jdolecek s = splusb();
873 1.75 christos SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
874 1.74 jdolecek splx(s);
875 1.74 jdolecek }
876 1.74 jdolecek
877 1.74 jdolecek static int
878 1.91 christos filt_usbread(struct knote *kn, long hint)
879 1.74 jdolecek {
880 1.74 jdolecek
881 1.74 jdolecek if (usb_nevents == 0)
882 1.74 jdolecek return (0);
883 1.74 jdolecek
884 1.74 jdolecek kn->kn_data = sizeof(struct usb_event);
885 1.74 jdolecek return (1);
886 1.74 jdolecek }
887 1.74 jdolecek
888 1.74 jdolecek static const struct filterops usbread_filtops =
889 1.74 jdolecek { 1, NULL, filt_usbrdetach, filt_usbread };
890 1.74 jdolecek
891 1.74 jdolecek int
892 1.74 jdolecek usbkqfilter(dev_t dev, struct knote *kn)
893 1.74 jdolecek {
894 1.74 jdolecek struct klist *klist;
895 1.74 jdolecek int s;
896 1.74 jdolecek
897 1.74 jdolecek switch (kn->kn_filter) {
898 1.74 jdolecek case EVFILT_READ:
899 1.74 jdolecek if (minor(dev) != USB_DEV_MINOR)
900 1.74 jdolecek return (1);
901 1.75 christos klist = &usb_selevent.sel_klist;
902 1.74 jdolecek kn->kn_fop = &usbread_filtops;
903 1.74 jdolecek break;
904 1.74 jdolecek
905 1.74 jdolecek default:
906 1.74 jdolecek return (1);
907 1.74 jdolecek }
908 1.74 jdolecek
909 1.74 jdolecek kn->kn_hook = NULL;
910 1.74 jdolecek
911 1.74 jdolecek s = splusb();
912 1.74 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
913 1.74 jdolecek splx(s);
914 1.74 jdolecek
915 1.74 jdolecek return (0);
916 1.74 jdolecek }
917 1.95.4.1 itohy #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
918 1.74 jdolecek
919 1.25 augustss /* Explore device tree from the root. */
920 1.51 augustss Static void
921 1.51 augustss usb_discover(void *v)
922 1.1 augustss {
923 1.51 augustss struct usb_softc *sc = v;
924 1.95.4.1 itohy /* splxxx should be changed to mutexes for preemption safety some day */
925 1.95.4.1 itohy int s;
926 1.51 augustss
927 1.51 augustss DPRINTFN(2,("usb_discover\n"));
928 1.51 augustss #ifdef USB_DEBUG
929 1.51 augustss if (usb_noexplore > 1)
930 1.51 augustss return;
931 1.51 augustss #endif
932 1.95.4.1 itohy
933 1.66 augustss /*
934 1.25 augustss * We need mutual exclusion while traversing the device tree,
935 1.25 augustss * but this is guaranteed since this function is only called
936 1.25 augustss * from the event thread for the controller.
937 1.25 augustss */
938 1.95.4.1 itohy s = splusb();
939 1.30 augustss while (sc->sc_bus->needs_explore && !sc->sc_dying) {
940 1.13 augustss sc->sc_bus->needs_explore = 0;
941 1.95.4.1 itohy splx(s);
942 1.13 augustss sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
943 1.95.4.1 itohy s = splusb();
944 1.30 augustss }
945 1.95.4.1 itohy splx(s);
946 1.1 augustss }
947 1.1 augustss
948 1.1 augustss void
949 1.51 augustss usb_needs_explore(usbd_device_handle dev)
950 1.1 augustss {
951 1.51 augustss DPRINTFN(2,("usb_needs_explore\n"));
952 1.51 augustss dev->bus->needs_explore = 1;
953 1.58 augustss wakeup(&dev->bus->needs_explore);
954 1.1 augustss }
955 1.7 augustss
956 1.81 joff void
957 1.81 joff usb_needs_reattach(usbd_device_handle dev)
958 1.81 joff {
959 1.81 joff DPRINTFN(2,("usb_needs_reattach\n"));
960 1.81 joff dev->powersrc->reattach = 1;
961 1.81 joff dev->bus->needs_explore = 1;
962 1.81 joff wakeup(&dev->bus->needs_explore);
963 1.81 joff }
964 1.81 joff
965 1.26 augustss /* Called at splusb() */
966 1.26 augustss int
967 1.45 augustss usb_get_next_event(struct usb_event *ue)
968 1.26 augustss {
969 1.26 augustss struct usb_event_q *ueq;
970 1.26 augustss
971 1.26 augustss if (usb_nevents <= 0)
972 1.26 augustss return (0);
973 1.95.4.1 itohy ueq = TAILQ_FIRST(&usb_events);
974 1.65 augustss #ifdef DIAGNOSTIC
975 1.65 augustss if (ueq == NULL) {
976 1.65 augustss printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
977 1.65 augustss usb_nevents = 0;
978 1.65 augustss return (0);
979 1.65 augustss }
980 1.65 augustss #endif
981 1.83 drochner if (ue)
982 1.83 drochner *ue = ueq->ue;
983 1.95.4.1 itohy TAILQ_REMOVE(&usb_events, ueq, next);
984 1.87 christos usb_free_event((struct usb_event *)(void *)ueq);
985 1.26 augustss usb_nevents--;
986 1.26 augustss return (1);
987 1.26 augustss }
988 1.26 augustss
989 1.26 augustss void
990 1.45 augustss usbd_add_dev_event(int type, usbd_device_handle udev)
991 1.38 augustss {
992 1.87 christos struct usb_event *ue = usb_alloc_event();
993 1.38 augustss
994 1.87 christos usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
995 1.87 christos usb_add_event(type, ue);
996 1.38 augustss }
997 1.38 augustss
998 1.38 augustss void
999 1.45 augustss usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
1000 1.38 augustss {
1001 1.87 christos struct usb_event *ue = usb_alloc_event();
1002 1.87 christos
1003 1.87 christos ue->u.ue_driver.ue_cookie = udev->cookie;
1004 1.87 christos strncpy(ue->u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
1005 1.87 christos sizeof ue->u.ue_driver.ue_devname);
1006 1.87 christos usb_add_event(type, ue);
1007 1.87 christos }
1008 1.87 christos
1009 1.87 christos Static struct usb_event *
1010 1.87 christos usb_alloc_event(void)
1011 1.87 christos {
1012 1.87 christos /* Yes, this is right; we allocate enough so that we can use it later */
1013 1.87 christos return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO);
1014 1.87 christos }
1015 1.38 augustss
1016 1.87 christos Static void
1017 1.87 christos usb_free_event(struct usb_event *uep)
1018 1.87 christos {
1019 1.87 christos free(uep, M_USBDEV);
1020 1.38 augustss }
1021 1.38 augustss
1022 1.42 augustss Static void
1023 1.45 augustss usb_add_event(int type, struct usb_event *uep)
1024 1.26 augustss {
1025 1.26 augustss struct usb_event_q *ueq;
1026 1.26 augustss struct timeval thetime;
1027 1.26 augustss int s;
1028 1.26 augustss
1029 1.38 augustss microtime(&thetime);
1030 1.38 augustss /* Don't want to wait here inside splusb() */
1031 1.87 christos ueq = (struct usb_event_q *)(void *)uep;
1032 1.38 augustss ueq->ue = *uep;
1033 1.38 augustss ueq->ue.ue_type = type;
1034 1.38 augustss TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
1035 1.38 augustss
1036 1.26 augustss s = splusb();
1037 1.95.4.1 itohy if (USB_EVENT_IS_DETACH(type)) {
1038 1.95.4.1 itohy struct usb_event_q *ueqi, *ueqi_next;
1039 1.95.4.1 itohy
1040 1.95.4.1 itohy for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
1041 1.95.4.1 itohy ueqi_next = TAILQ_NEXT(ueqi, next);
1042 1.95.4.1 itohy if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
1043 1.95.4.1 itohy uep->u.ue_device.udi_cookie.cookie) {
1044 1.95.4.1 itohy TAILQ_REMOVE(&usb_events, ueqi, next);
1045 1.95.4.1 itohy free(ueqi, M_USBDEV);
1046 1.95.4.1 itohy usb_nevents--;
1047 1.95.4.1 itohy ueqi_next = TAILQ_FIRST(&usb_events);
1048 1.95.4.1 itohy }
1049 1.95.4.1 itohy }
1050 1.95.4.1 itohy }
1051 1.95.4.1 itohy if (usb_nevents >= USB_MAX_EVENTS) {
1052 1.26 augustss /* Too many queued events, drop an old one. */
1053 1.26 augustss DPRINTFN(-1,("usb: event dropped\n"));
1054 1.83 drochner (void)usb_get_next_event(0);
1055 1.26 augustss }
1056 1.95.4.1 itohy TAILQ_INSERT_TAIL(&usb_events, ueq, next);
1057 1.95.4.1 itohy usb_nevents++;
1058 1.26 augustss wakeup(&usb_events);
1059 1.95.4.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
1060 1.74 jdolecek selnotify(&usb_selevent, 0);
1061 1.95.4.1 itohy #elif defined(__FreeBSD__)
1062 1.95.4.1 itohy selwakeuppri(&usb_selevent, PZERO);
1063 1.95.4.1 itohy #endif
1064 1.94 ad if (usb_async_proc != NULL) {
1065 1.95.4.1 itohy USB_PROC_LOCK(usb_async_proc);
1066 1.26 augustss psignal(usb_async_proc, SIGIO);
1067 1.95.4.1 itohy USB_PROC_UNLOCK(usb_async_proc);
1068 1.94 ad }
1069 1.26 augustss splx(s);
1070 1.39 augustss }
1071 1.60 augustss
1072 1.39 augustss void
1073 1.49 augustss usb_schedsoftintr(usbd_bus_handle bus)
1074 1.39 augustss {
1075 1.54 augustss DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
1076 1.49 augustss #ifdef USB_USE_SOFTINTR
1077 1.49 augustss if (bus->use_polling) {
1078 1.49 augustss bus->methods->soft_intr(bus);
1079 1.49 augustss } else {
1080 1.49 augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1081 1.49 augustss softintr_schedule(bus->soft);
1082 1.49 augustss #else
1083 1.49 augustss if (!callout_pending(&bus->softi))
1084 1.49 augustss callout_reset(&bus->softi, 0, bus->methods->soft_intr,
1085 1.49 augustss bus);
1086 1.49 augustss #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
1087 1.49 augustss }
1088 1.49 augustss #else
1089 1.39 augustss bus->methods->soft_intr(bus);
1090 1.56 augustss #endif /* USB_USE_SOFTINTR */
1091 1.26 augustss }
1092 1.26 augustss
1093 1.95.4.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
1094 1.7 augustss int
1095 1.45 augustss usb_activate(device_ptr_t self, enum devact act)
1096 1.7 augustss {
1097 1.22 augustss struct usb_softc *sc = (struct usb_softc *)self;
1098 1.25 augustss usbd_device_handle dev = sc->sc_port.device;
1099 1.25 augustss int i, rv = 0;
1100 1.22 augustss
1101 1.22 augustss switch (act) {
1102 1.22 augustss case DVACT_ACTIVATE:
1103 1.22 augustss return (EOPNOTSUPP);
1104 1.22 augustss
1105 1.22 augustss case DVACT_DEACTIVATE:
1106 1.22 augustss sc->sc_dying = 1;
1107 1.62 augustss if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
1108 1.25 augustss for (i = 0; dev->subdevs[i]; i++)
1109 1.25 augustss rv |= config_deactivate(dev->subdevs[i]);
1110 1.25 augustss }
1111 1.22 augustss break;
1112 1.22 augustss }
1113 1.22 augustss return (rv);
1114 1.13 augustss }
1115 1.95.4.1 itohy #endif
1116 1.7 augustss
1117 1.95.4.1 itohy USB_DETACH(usb)
1118 1.13 augustss {
1119 1.95.4.1 itohy USB_DETACH_START(usb, sc);
1120 1.87 christos struct usb_event *ue;
1121 1.95.4.1 itohy struct usb_taskq *taskq;
1122 1.95.4.1 itohy int i;
1123 1.22 augustss
1124 1.27 augustss DPRINTF(("usb_detach: start\n"));
1125 1.27 augustss
1126 1.22 augustss sc->sc_dying = 1;
1127 1.22 augustss
1128 1.22 augustss /* Make all devices disconnect. */
1129 1.62 augustss if (sc->sc_port.device != NULL)
1130 1.27 augustss usb_disconnect_port(&sc->sc_port, self);
1131 1.22 augustss
1132 1.22 augustss /* Kill off event thread. */
1133 1.62 augustss if (sc->sc_event_thread != NULL) {
1134 1.58 augustss wakeup(&sc->sc_bus->needs_explore);
1135 1.22 augustss if (tsleep(sc, PWAIT, "usbdet", hz * 60))
1136 1.22 augustss printf("%s: event thread didn't die\n",
1137 1.22 augustss USBDEVNAME(sc->sc_dev));
1138 1.27 augustss DPRINTF(("usb_detach: event thread dead\n"));
1139 1.22 augustss }
1140 1.22 augustss
1141 1.95.4.1 itohy #ifdef __FreeBSD__
1142 1.95.4.1 itohy destroy_dev(sc->sc_usbdev);
1143 1.95.4.1 itohy #endif
1144 1.95.4.1 itohy if (--usb_nbusses == 0) {
1145 1.95.4.1 itohy #ifdef __FreeBSD__
1146 1.95.4.1 itohy destroy_dev(usb_dev);
1147 1.95.4.1 itohy usb_dev = NULL;
1148 1.95.4.1 itohy #endif
1149 1.95.4.1 itohy for (i = 0; i < USB_NUM_TASKQS; i++) {
1150 1.95.4.1 itohy taskq = &usb_taskq[i];
1151 1.95.4.1 itohy wakeup(&taskq->tasks);
1152 1.95.4.1 itohy if (tsleep(&taskq->taskcreated, PWAIT, "usbtdt",
1153 1.95.4.1 itohy hz * 60)) {
1154 1.95.4.1 itohy printf("usb task thread %s didn't die\n",
1155 1.95.4.1 itohy taskq->name);
1156 1.95.4.1 itohy }
1157 1.95.4.1 itohy }
1158 1.95.4.1 itohy }
1159 1.95.4.1 itohy
1160 1.49 augustss #ifdef USB_USE_SOFTINTR
1161 1.49 augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1162 1.49 augustss if (sc->sc_bus->soft != NULL) {
1163 1.49 augustss softintr_disestablish(sc->sc_bus->soft);
1164 1.49 augustss sc->sc_bus->soft = NULL;
1165 1.49 augustss }
1166 1.49 augustss #else
1167 1.49 augustss callout_stop(&sc->sc_bus->softi);
1168 1.49 augustss #endif
1169 1.49 augustss #endif
1170 1.38 augustss
1171 1.87 christos ue = usb_alloc_event();
1172 1.87 christos ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
1173 1.87 christos usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
1174 1.38 augustss
1175 1.7 augustss return (0);
1176 1.7 augustss }
1177 1.92 pavel
1178 1.95.4.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
1179 1.92 pavel Static void
1180 1.92 pavel usb_copy_old_devinfo(struct usb_device_info_old *uo,
1181 1.92 pavel const struct usb_device_info *ue)
1182 1.92 pavel {
1183 1.92 pavel const unsigned char *p;
1184 1.92 pavel unsigned char *q;
1185 1.92 pavel int i, n;
1186 1.92 pavel
1187 1.92 pavel uo->udi_bus = ue->udi_bus;
1188 1.92 pavel uo->udi_addr = ue->udi_addr;
1189 1.92 pavel uo->udi_cookie = ue->udi_cookie;
1190 1.92 pavel for (i = 0, p = (const unsigned char *)ue->udi_product,
1191 1.92 pavel q = (unsigned char *)uo->udi_product;
1192 1.92 pavel *p && i < USB_MAX_STRING_LEN - 1; p++) {
1193 1.92 pavel if (*p < 0x80)
1194 1.92 pavel q[i++] = *p;
1195 1.92 pavel else {
1196 1.92 pavel q[i++] = '?';
1197 1.92 pavel if ((*p & 0xe0) == 0xe0)
1198 1.92 pavel p++;
1199 1.92 pavel p++;
1200 1.92 pavel }
1201 1.92 pavel }
1202 1.92 pavel q[i] = 0;
1203 1.92 pavel
1204 1.92 pavel for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
1205 1.92 pavel *p && i < USB_MAX_STRING_LEN - 1; p++) {
1206 1.92 pavel if (* p < 0x80)
1207 1.92 pavel q[i++] = *p;
1208 1.92 pavel else {
1209 1.92 pavel q[i++] = '?';
1210 1.92 pavel p++;
1211 1.92 pavel if ((*p & 0xe0) == 0xe0)
1212 1.92 pavel p++;
1213 1.92 pavel }
1214 1.92 pavel }
1215 1.92 pavel q[i] = 0;
1216 1.92 pavel
1217 1.92 pavel memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
1218 1.92 pavel
1219 1.92 pavel uo->udi_productNo = ue->udi_productNo;
1220 1.92 pavel uo->udi_vendorNo = ue->udi_vendorNo;
1221 1.92 pavel uo->udi_releaseNo = ue->udi_releaseNo;
1222 1.92 pavel uo->udi_class = ue->udi_class;
1223 1.92 pavel uo->udi_subclass = ue->udi_subclass;
1224 1.92 pavel uo->udi_protocol = ue->udi_protocol;
1225 1.92 pavel uo->udi_config = ue->udi_config;
1226 1.92 pavel uo->udi_speed = ue->udi_speed;
1227 1.92 pavel uo->udi_power = ue->udi_power;
1228 1.92 pavel uo->udi_nports = ue->udi_nports;
1229 1.92 pavel
1230 1.92 pavel for (n=0; n<USB_MAX_DEVNAMES; n++)
1231 1.92 pavel memcpy(uo->udi_devnames[n],
1232 1.92 pavel ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
1233 1.92 pavel memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
1234 1.92 pavel }
1235 1.92 pavel #endif
1236 1.95.4.1 itohy
1237 1.95.4.1 itohy #if defined(__FreeBSD__)
1238 1.95.4.1 itohy Static void
1239 1.95.4.1 itohy usb_child_detached(device_t self, device_t child)
1240 1.95.4.1 itohy {
1241 1.95.4.1 itohy struct usb_softc *sc = device_get_softc(self);
1242 1.95.4.1 itohy
1243 1.95.4.1 itohy /* XXX, should check it is the right device. */
1244 1.95.4.1 itohy sc->sc_port.device = NULL;
1245 1.95.4.1 itohy }
1246 1.95.4.1 itohy
1247 1.95.4.1 itohy /* Explore USB busses at the end of device configuration. */
1248 1.95.4.1 itohy Static void
1249 1.95.4.1 itohy usb_cold_explore(void *arg)
1250 1.95.4.1 itohy {
1251 1.95.4.1 itohy struct usb_softc *sc;
1252 1.95.4.1 itohy
1253 1.95.4.1 itohy USB_KASSERT2(cold || TAILQ_EMPTY(&usb_coldexplist),
1254 1.95.4.1 itohy ("usb_cold_explore: busses to explore when !cold"));
1255 1.95.4.1 itohy while (!TAILQ_EMPTY(&usb_coldexplist)) {
1256 1.95.4.1 itohy sc = TAILQ_FIRST(&usb_coldexplist);
1257 1.95.4.1 itohy TAILQ_REMOVE(&usb_coldexplist, sc, sc_coldexplist);
1258 1.95.4.1 itohy
1259 1.95.4.1 itohy sc->sc_bus->use_polling++;
1260 1.95.4.1 itohy sc->sc_port.device->hub->explore(sc->sc_bus->root_hub);
1261 1.95.4.1 itohy sc->sc_bus->use_polling--;
1262 1.95.4.1 itohy }
1263 1.95.4.1 itohy }
1264 1.95.4.1 itohy
1265 1.95.4.1 itohy DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
1266 1.95.4.1 itohy DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
1267 1.95.4.1 itohy DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);
1268 1.95.4.1 itohy DRIVER_MODULE(usb, slhci, usb_driver, usb_devclass, 0, 0);
1269 1.95.4.1 itohy SYSINIT(usb_cold_explore, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE,
1270 1.95.4.1 itohy usb_cold_explore, NULL);
1271 1.95.4.1 itohy #endif
1272