usb.c revision 1.95.4.3 1 1.95.4.2 itohy /* $NetBSD: usb.c,v 1.95.4.3 2007/06/22 10:12:24 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.3 2007/06/22 10:12:24 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.95.4.3 itohy USB_KASSERT(queue > USB_TASKQ_IDLE && queue <= USB_NUM_TASKQS);
426 1.95.4.3 itohy
427 1.51 augustss s = splusb();
428 1.95.4.3 itohy taskq = &usb_taskq[queue - 1];
429 1.95.4.3 itohy if (task->queue == USB_TASKQ_IDLE) {
430 1.58 augustss DPRINTFN(2,("usb_add_task: task=%p\n", task));
431 1.90 joerg TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
432 1.90 joerg task->queue = queue;
433 1.62 augustss } else {
434 1.58 augustss DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
435 1.62 augustss }
436 1.90 joerg wakeup(&taskq->tasks);
437 1.51 augustss splx(s);
438 1.51 augustss }
439 1.51 augustss
440 1.51 augustss void
441 1.91 christos usb_rem_task(usbd_device_handle dev, struct usb_task *task)
442 1.53 augustss {
443 1.90 joerg struct usb_taskq *taskq;
444 1.53 augustss int s;
445 1.53 augustss
446 1.53 augustss s = splusb();
447 1.95.4.3 itohy if (task->queue != USB_TASKQ_IDLE) {
448 1.95.4.3 itohy taskq = &usb_taskq[task->queue - 1];
449 1.90 joerg TAILQ_REMOVE(&taskq->tasks, task, next);
450 1.95.4.3 itohy task->queue = USB_TASKQ_IDLE;
451 1.53 augustss }
452 1.53 augustss splx(s);
453 1.53 augustss }
454 1.53 augustss
455 1.53 augustss void
456 1.45 augustss usb_event_thread(void *arg)
457 1.13 augustss {
458 1.95.4.1 itohy static int newthread_wchan;
459 1.13 augustss struct usb_softc *sc = arg;
460 1.13 augustss
461 1.95.4.1 itohy #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
462 1.95.4.1 itohy mtx_lock(&Giant);
463 1.95.4.1 itohy #endif
464 1.95.4.1 itohy
465 1.27 augustss DPRINTF(("usb_event_thread: start\n"));
466 1.40 augustss
467 1.60 augustss /*
468 1.60 augustss * In case this controller is a companion controller to an
469 1.60 augustss * EHCI controller we need to wait until the EHCI controller
470 1.95.4.1 itohy * has grabbed the port. What we do here is wait until no new
471 1.95.4.1 itohy * USB threads have been created in a while. XXX we actually
472 1.95.4.1 itohy * just want to wait for the PCI slot to be fully scanned.
473 1.95.4.1 itohy *
474 1.95.4.1 itohy * Note that when you `kldload usb' it actually attaches the
475 1.95.4.1 itohy * devices in order that the drivers appear in the kld, not the
476 1.95.4.1 itohy * normal PCI order, since the addition of each driver within
477 1.95.4.1 itohy * usb.ko (ohci, ehci etc.) causes a separate PCI bus re-scan.
478 1.60 augustss */
479 1.95.4.1 itohy wakeup(&newthread_wchan);
480 1.95.4.1 itohy for (;;) {
481 1.95.4.1 itohy if (tsleep(&newthread_wchan , PWAIT, "usbets", hz * 4) != 0)
482 1.95.4.1 itohy break;
483 1.95.4.1 itohy }
484 1.60 augustss
485 1.40 augustss /* Make sure first discover does something. */
486 1.40 augustss sc->sc_bus->needs_explore = 1;
487 1.51 augustss usb_discover(sc);
488 1.51 augustss config_pending_decr();
489 1.27 augustss
490 1.22 augustss while (!sc->sc_dying) {
491 1.58 augustss #ifdef USB_DEBUG
492 1.58 augustss if (usb_noexplore < 2)
493 1.58 augustss #endif
494 1.58 augustss usb_discover(sc);
495 1.58 augustss #ifdef USB_DEBUG
496 1.58 augustss (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
497 1.58 augustss usb_noexplore ? 0 : hz * 60);
498 1.58 augustss #else
499 1.58 augustss (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
500 1.58 augustss hz * 60);
501 1.58 augustss #endif
502 1.58 augustss DPRINTFN(2,("usb_event_thread: woke up\n"));
503 1.13 augustss }
504 1.50 augustss sc->sc_event_thread = NULL;
505 1.13 augustss
506 1.13 augustss /* In case parent is waiting for us to exit. */
507 1.13 augustss wakeup(sc);
508 1.13 augustss
509 1.27 augustss DPRINTF(("usb_event_thread: exit\n"));
510 1.13 augustss kthread_exit(0);
511 1.13 augustss }
512 1.13 augustss
513 1.58 augustss void
514 1.90 joerg usb_task_thread(void *arg)
515 1.58 augustss {
516 1.58 augustss struct usb_task *task;
517 1.90 joerg struct usb_taskq *taskq;
518 1.58 augustss int s;
519 1.58 augustss
520 1.95.4.1 itohy #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
521 1.95.4.1 itohy mtx_lock(&Giant);
522 1.95.4.1 itohy #endif
523 1.95.4.1 itohy
524 1.90 joerg taskq = arg;
525 1.90 joerg DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
526 1.58 augustss
527 1.58 augustss s = splusb();
528 1.95.4.1 itohy
529 1.95.4.1 itohy while (usb_nbusses > 0) {
530 1.90 joerg task = TAILQ_FIRST(&taskq->tasks);
531 1.58 augustss if (task == NULL) {
532 1.90 joerg tsleep(&taskq->tasks, PWAIT, "usbtsk", 0);
533 1.90 joerg task = TAILQ_FIRST(&taskq->tasks);
534 1.58 augustss }
535 1.58 augustss DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
536 1.58 augustss if (task != NULL) {
537 1.90 joerg TAILQ_REMOVE(&taskq->tasks, task, next);
538 1.95.4.3 itohy task->queue = USB_TASKQ_IDLE;
539 1.58 augustss splx(s);
540 1.58 augustss task->fun(task->arg);
541 1.58 augustss s = splusb();
542 1.58 augustss }
543 1.58 augustss }
544 1.95.4.1 itohy
545 1.95.4.1 itohy splx(s);
546 1.95.4.1 itohy
547 1.95.4.1 itohy taskq->taskcreated = 0;
548 1.95.4.1 itohy wakeup(&taskq->taskcreated);
549 1.95.4.1 itohy
550 1.95.4.1 itohy DPRINTF(("usb_event_thread: exit\n"));
551 1.95.4.1 itohy kthread_exit(0);
552 1.58 augustss }
553 1.58 augustss
554 1.95.4.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
555 1.1 augustss int
556 1.91 christos usbctlprint(void *aux, const char *pnp)
557 1.1 augustss {
558 1.1 augustss /* only "usb"es can attach to host controllers */
559 1.1 augustss if (pnp)
560 1.77 thorpej aprint_normal("usb at %s", pnp);
561 1.1 augustss
562 1.1 augustss return (UNCONF);
563 1.1 augustss }
564 1.28 augustss #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
565 1.7 augustss
566 1.1 augustss int
567 1.95.4.1 itohy usbopen(usb_cdev_t dev, int flag, int mode, usb_proc_ptr p)
568 1.1 augustss {
569 1.95.4.1 itohy int unit = USBUNIT(dev);
570 1.26 augustss struct usb_softc *sc;
571 1.26 augustss
572 1.26 augustss if (unit == USB_DEV_MINOR) {
573 1.26 augustss if (usb_dev_open)
574 1.26 augustss return (EBUSY);
575 1.26 augustss usb_dev_open = 1;
576 1.26 augustss usb_async_proc = 0;
577 1.26 augustss return (0);
578 1.26 augustss }
579 1.26 augustss
580 1.26 augustss USB_GET_SC_OPEN(usb, unit, sc);
581 1.1 augustss
582 1.22 augustss if (sc->sc_dying)
583 1.22 augustss return (EIO);
584 1.1 augustss
585 1.1 augustss return (0);
586 1.1 augustss }
587 1.1 augustss
588 1.1 augustss int
589 1.95.4.1 itohy usbread(usb_cdev_t dev, struct uio *uio, int flag)
590 1.26 augustss {
591 1.92 pavel struct usb_event *ue;
592 1.95.4.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
593 1.93 christos struct usb_event_old *ueo = NULL; /* XXXGCC */
594 1.95.4.1 itohy int useold = 0;
595 1.92 pavel #endif
596 1.95.4.1 itohy int s, error, n;
597 1.26 augustss
598 1.95.4.1 itohy if (USBUNIT(dev) != USB_DEV_MINOR)
599 1.26 augustss return (ENXIO);
600 1.26 augustss
601 1.92 pavel switch (uio->uio_resid) {
602 1.95.4.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
603 1.92 pavel case sizeof(struct usb_event_old):
604 1.92 pavel ueo = malloc(sizeof(struct usb_event_old), M_USBDEV,
605 1.92 pavel M_WAITOK|M_ZERO);
606 1.92 pavel useold = 1;
607 1.92 pavel /* FALLTHRU */
608 1.92 pavel #endif
609 1.92 pavel case sizeof(struct usb_event):
610 1.92 pavel ue = usb_alloc_event();
611 1.92 pavel break;
612 1.92 pavel default:
613 1.26 augustss return (EINVAL);
614 1.92 pavel }
615 1.26 augustss
616 1.26 augustss error = 0;
617 1.26 augustss s = splusb();
618 1.26 augustss for (;;) {
619 1.87 christos n = usb_get_next_event(ue);
620 1.26 augustss if (n != 0)
621 1.26 augustss break;
622 1.95.4.1 itohy if (flag &
623 1.95.4.1 itohy #ifdef __FreeBSD__
624 1.95.4.1 itohy O_NONBLOCK
625 1.95.4.1 itohy #else
626 1.95.4.1 itohy IO_NDELAY
627 1.95.4.1 itohy #endif
628 1.95.4.1 itohy ) {
629 1.26 augustss error = EWOULDBLOCK;
630 1.26 augustss break;
631 1.26 augustss }
632 1.26 augustss error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
633 1.26 augustss if (error)
634 1.26 augustss break;
635 1.26 augustss }
636 1.26 augustss splx(s);
637 1.92 pavel if (!error) {
638 1.95.4.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
639 1.92 pavel if (useold) { /* copy fields to old struct */
640 1.92 pavel ueo->ue_type = ue->ue_type;
641 1.92 pavel memcpy(&ueo->ue_time, &ue->ue_time,
642 1.92 pavel sizeof(struct timespec));
643 1.92 pavel switch (ue->ue_type) {
644 1.92 pavel case USB_EVENT_DEVICE_ATTACH:
645 1.92 pavel case USB_EVENT_DEVICE_DETACH:
646 1.92 pavel usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
647 1.92 pavel break;
648 1.92 pavel
649 1.92 pavel case USB_EVENT_CTRLR_ATTACH:
650 1.92 pavel case USB_EVENT_CTRLR_DETACH:
651 1.92 pavel ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
652 1.92 pavel break;
653 1.92 pavel
654 1.92 pavel case USB_EVENT_DRIVER_ATTACH:
655 1.92 pavel case USB_EVENT_DRIVER_DETACH:
656 1.92 pavel ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
657 1.92 pavel memcpy(ueo->u.ue_driver.ue_devname,
658 1.92 pavel ue->u.ue_driver.ue_devname,
659 1.92 pavel sizeof(ue->u.ue_driver.ue_devname));
660 1.92 pavel break;
661 1.92 pavel default:
662 1.92 pavel ;
663 1.92 pavel }
664 1.92 pavel
665 1.92 pavel error = uiomove((void *)ueo, uio->uio_resid, uio);
666 1.92 pavel } else
667 1.92 pavel #endif
668 1.92 pavel error = uiomove((void *)ue, uio->uio_resid, uio);
669 1.92 pavel }
670 1.87 christos usb_free_event(ue);
671 1.95.4.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
672 1.92 pavel if (useold)
673 1.92 pavel free(ueo, M_USBDEV);
674 1.92 pavel #endif
675 1.26 augustss
676 1.26 augustss return (error);
677 1.26 augustss }
678 1.26 augustss
679 1.26 augustss int
680 1.95.4.1 itohy usbclose(usb_cdev_t dev, int flag, int mode, usb_proc_ptr p)
681 1.1 augustss {
682 1.95.4.1 itohy int unit = USBUNIT(dev);
683 1.26 augustss
684 1.26 augustss if (unit == USB_DEV_MINOR) {
685 1.26 augustss usb_async_proc = 0;
686 1.26 augustss usb_dev_open = 0;
687 1.26 augustss }
688 1.26 augustss
689 1.1 augustss return (0);
690 1.1 augustss }
691 1.1 augustss
692 1.1 augustss int
693 1.95.4.2 itohy usbioctl(usb_cdev_t devt, u_long cmd, usb_ioctlarg_t data, int flag,
694 1.95.4.2 itohy usb_proc_ptr p)
695 1.1 augustss {
696 1.26 augustss struct usb_softc *sc;
697 1.95.4.1 itohy int unit = USBUNIT(devt);
698 1.26 augustss
699 1.26 augustss if (unit == USB_DEV_MINOR) {
700 1.26 augustss switch (cmd) {
701 1.26 augustss case FIONBIO:
702 1.26 augustss /* All handled in the upper FS layer. */
703 1.26 augustss return (0);
704 1.66 augustss
705 1.26 augustss case FIOASYNC:
706 1.26 augustss if (*(int *)data)
707 1.95.4.1 itohy #if defined(__NetBSD__) && __NetBSD_Version__ >= 399001400
708 1.95.4.1 itohy usb_async_proc = p->l_proc;
709 1.95.4.1 itohy #elif defined(__FreeBSD__) && __FreeBSD_version >= 500000
710 1.95.4.1 itohy usb_async_proc = p->td_proc;
711 1.95.4.1 itohy #else
712 1.95.4.1 itohy usb_async_proc = p;
713 1.95.4.1 itohy #endif
714 1.26 augustss else
715 1.26 augustss usb_async_proc = 0;
716 1.26 augustss return (0);
717 1.26 augustss
718 1.26 augustss default:
719 1.26 augustss return (EINVAL);
720 1.26 augustss }
721 1.26 augustss }
722 1.26 augustss
723 1.26 augustss USB_GET_SC(usb, unit, sc);
724 1.1 augustss
725 1.22 augustss if (sc->sc_dying)
726 1.22 augustss return (EIO);
727 1.22 augustss
728 1.1 augustss switch (cmd) {
729 1.95.4.1 itohy #if defined(USB_DEBUG)
730 1.1 augustss case USB_SETDEBUG:
731 1.69 augustss if (!(flag & FWRITE))
732 1.69 augustss return (EBADF);
733 1.30 augustss usbdebug = ((*(int *)data) & 0x000000ff);
734 1.76 dsainty #if defined(UHCI_DEBUG) && NUHCI > 0
735 1.30 augustss uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
736 1.30 augustss #endif
737 1.76 dsainty #if defined(OHCI_DEBUG) && NOHCI > 0
738 1.30 augustss ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
739 1.30 augustss #endif
740 1.1 augustss break;
741 1.56 augustss #endif /* USB_DEBUG */
742 1.95.4.1 itohy #if defined(__FreeBSD__)
743 1.95.4.1 itohy /* This part should be deleted */
744 1.95.4.1 itohy case USB_DISCOVER:
745 1.95.4.1 itohy break;
746 1.95.4.1 itohy #endif
747 1.1 augustss case USB_REQUEST:
748 1.1 augustss {
749 1.1 augustss struct usb_ctl_request *ur = (void *)data;
750 1.68 christos int len = UGETW(ur->ucr_request.wLength);
751 1.1 augustss struct iovec iov;
752 1.1 augustss struct uio uio;
753 1.1 augustss void *ptr = 0;
754 1.68 christos int addr = ur->ucr_addr;
755 1.29 augustss usbd_status err;
756 1.1 augustss int error = 0;
757 1.69 augustss
758 1.69 augustss if (!(flag & FWRITE))
759 1.69 augustss return (EBADF);
760 1.1 augustss
761 1.9 augustss DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
762 1.1 augustss if (len < 0 || len > 32768)
763 1.9 augustss return (EINVAL);
764 1.66 augustss if (addr < 0 || addr >= USB_MAX_DEVICES ||
765 1.1 augustss sc->sc_bus->devices[addr] == 0)
766 1.9 augustss return (EINVAL);
767 1.1 augustss if (len != 0) {
768 1.95.4.2 itohy iov.iov_base = (void *)ur->ucr_data;
769 1.1 augustss iov.iov_len = len;
770 1.1 augustss uio.uio_iov = &iov;
771 1.1 augustss uio.uio_iovcnt = 1;
772 1.1 augustss uio.uio_resid = len;
773 1.1 augustss uio.uio_offset = 0;
774 1.1 augustss uio.uio_rw =
775 1.68 christos ur->ucr_request.bmRequestType & UT_READ ?
776 1.1 augustss UIO_READ : UIO_WRITE;
777 1.95.4.2 itohy USB_UIO_SET_PROC(&uio, p);
778 1.1 augustss ptr = malloc(len, M_TEMP, M_WAITOK);
779 1.1 augustss if (uio.uio_rw == UIO_WRITE) {
780 1.1 augustss error = uiomove(ptr, len, &uio);
781 1.1 augustss if (error)
782 1.1 augustss goto ret;
783 1.1 augustss }
784 1.1 augustss }
785 1.29 augustss err = usbd_do_request_flags(sc->sc_bus->devices[addr],
786 1.68 christos &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
787 1.67 augustss USBD_DEFAULT_TIMEOUT);
788 1.29 augustss if (err) {
789 1.1 augustss error = EIO;
790 1.1 augustss goto ret;
791 1.1 augustss }
792 1.1 augustss if (len != 0) {
793 1.1 augustss if (uio.uio_rw == UIO_READ) {
794 1.1 augustss error = uiomove(ptr, len, &uio);
795 1.1 augustss if (error)
796 1.1 augustss goto ret;
797 1.1 augustss }
798 1.1 augustss }
799 1.1 augustss ret:
800 1.1 augustss if (ptr)
801 1.1 augustss free(ptr, M_TEMP);
802 1.1 augustss return (error);
803 1.1 augustss }
804 1.1 augustss
805 1.1 augustss case USB_DEVICEINFO:
806 1.93 christos {
807 1.93 christos usbd_device_handle dev;
808 1.93 christos struct usb_device_info *di = (void *)data;
809 1.93 christos int addr = di->udi_addr;
810 1.93 christos
811 1.93 christos if (addr < 1 || addr >= USB_MAX_DEVICES)
812 1.93 christos return EINVAL;
813 1.93 christos if ((dev = sc->sc_bus->devices[addr]) == NULL)
814 1.93 christos return ENXIO;
815 1.93 christos usbd_fill_deviceinfo(dev, di, 1);
816 1.93 christos break;
817 1.93 christos }
818 1.93 christos
819 1.95.4.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
820 1.92 pavel case USB_DEVICEINFO_OLD:
821 1.1 augustss {
822 1.30 augustss usbd_device_handle dev;
823 1.93 christos struct usb_device_info_old *di = (void *)data;
824 1.93 christos int addr = di->udi_addr;
825 1.1 augustss
826 1.1 augustss if (addr < 1 || addr >= USB_MAX_DEVICES)
827 1.93 christos return EINVAL;
828 1.93 christos if ((dev = sc->sc_bus->devices[addr]) == NULL)
829 1.93 christos return ENXIO;
830 1.93 christos usbd_fill_deviceinfo_old(dev, di, 1);
831 1.1 augustss break;
832 1.1 augustss }
833 1.93 christos #endif
834 1.2 augustss
835 1.2 augustss case USB_DEVICESTATS:
836 1.2 augustss *(struct usb_device_stats *)data = sc->sc_bus->stats;
837 1.2 augustss break;
838 1.1 augustss
839 1.1 augustss default:
840 1.26 augustss return (EINVAL);
841 1.1 augustss }
842 1.1 augustss return (0);
843 1.1 augustss }
844 1.1 augustss
845 1.1 augustss int
846 1.95.4.1 itohy usbpoll(usb_cdev_t dev, int events, usb_proc_ptr p)
847 1.1 augustss {
848 1.26 augustss int revents, mask, s;
849 1.95.4.1 itohy int unit = USBUNIT(dev);
850 1.1 augustss
851 1.95.4.1 itohy if (unit == USB_DEV_MINOR) {
852 1.30 augustss revents = 0;
853 1.30 augustss mask = POLLIN | POLLRDNORM;
854 1.66 augustss
855 1.30 augustss s = splusb();
856 1.30 augustss if (events & mask && usb_nevents > 0)
857 1.30 augustss revents |= events & mask;
858 1.30 augustss if (revents == 0 && events & mask)
859 1.95.4.1 itohy selrecord(p, &usb_selevent);
860 1.30 augustss splx(s);
861 1.66 augustss
862 1.30 augustss return (revents);
863 1.30 augustss } else {
864 1.82 ws return (0);
865 1.1 augustss }
866 1.1 augustss }
867 1.1 augustss
868 1.95.4.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
869 1.74 jdolecek static void
870 1.74 jdolecek filt_usbrdetach(struct knote *kn)
871 1.74 jdolecek {
872 1.74 jdolecek int s;
873 1.74 jdolecek
874 1.74 jdolecek s = splusb();
875 1.75 christos SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
876 1.74 jdolecek splx(s);
877 1.74 jdolecek }
878 1.74 jdolecek
879 1.74 jdolecek static int
880 1.91 christos filt_usbread(struct knote *kn, long hint)
881 1.74 jdolecek {
882 1.74 jdolecek
883 1.74 jdolecek if (usb_nevents == 0)
884 1.74 jdolecek return (0);
885 1.74 jdolecek
886 1.74 jdolecek kn->kn_data = sizeof(struct usb_event);
887 1.74 jdolecek return (1);
888 1.74 jdolecek }
889 1.74 jdolecek
890 1.74 jdolecek static const struct filterops usbread_filtops =
891 1.74 jdolecek { 1, NULL, filt_usbrdetach, filt_usbread };
892 1.74 jdolecek
893 1.74 jdolecek int
894 1.74 jdolecek usbkqfilter(dev_t dev, struct knote *kn)
895 1.74 jdolecek {
896 1.74 jdolecek struct klist *klist;
897 1.74 jdolecek int s;
898 1.74 jdolecek
899 1.74 jdolecek switch (kn->kn_filter) {
900 1.74 jdolecek case EVFILT_READ:
901 1.74 jdolecek if (minor(dev) != USB_DEV_MINOR)
902 1.74 jdolecek return (1);
903 1.75 christos klist = &usb_selevent.sel_klist;
904 1.74 jdolecek kn->kn_fop = &usbread_filtops;
905 1.74 jdolecek break;
906 1.74 jdolecek
907 1.74 jdolecek default:
908 1.74 jdolecek return (1);
909 1.74 jdolecek }
910 1.74 jdolecek
911 1.74 jdolecek kn->kn_hook = NULL;
912 1.74 jdolecek
913 1.74 jdolecek s = splusb();
914 1.74 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
915 1.74 jdolecek splx(s);
916 1.74 jdolecek
917 1.74 jdolecek return (0);
918 1.74 jdolecek }
919 1.95.4.1 itohy #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
920 1.74 jdolecek
921 1.25 augustss /* Explore device tree from the root. */
922 1.51 augustss Static void
923 1.51 augustss usb_discover(void *v)
924 1.1 augustss {
925 1.51 augustss struct usb_softc *sc = v;
926 1.95.4.1 itohy /* splxxx should be changed to mutexes for preemption safety some day */
927 1.95.4.1 itohy int s;
928 1.51 augustss
929 1.51 augustss DPRINTFN(2,("usb_discover\n"));
930 1.51 augustss #ifdef USB_DEBUG
931 1.51 augustss if (usb_noexplore > 1)
932 1.51 augustss return;
933 1.51 augustss #endif
934 1.95.4.1 itohy
935 1.66 augustss /*
936 1.25 augustss * We need mutual exclusion while traversing the device tree,
937 1.25 augustss * but this is guaranteed since this function is only called
938 1.25 augustss * from the event thread for the controller.
939 1.25 augustss */
940 1.95.4.1 itohy s = splusb();
941 1.30 augustss while (sc->sc_bus->needs_explore && !sc->sc_dying) {
942 1.13 augustss sc->sc_bus->needs_explore = 0;
943 1.95.4.1 itohy splx(s);
944 1.13 augustss sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
945 1.95.4.1 itohy s = splusb();
946 1.30 augustss }
947 1.95.4.1 itohy splx(s);
948 1.1 augustss }
949 1.1 augustss
950 1.1 augustss void
951 1.51 augustss usb_needs_explore(usbd_device_handle dev)
952 1.1 augustss {
953 1.51 augustss DPRINTFN(2,("usb_needs_explore\n"));
954 1.51 augustss dev->bus->needs_explore = 1;
955 1.58 augustss wakeup(&dev->bus->needs_explore);
956 1.1 augustss }
957 1.7 augustss
958 1.81 joff void
959 1.81 joff usb_needs_reattach(usbd_device_handle dev)
960 1.81 joff {
961 1.81 joff DPRINTFN(2,("usb_needs_reattach\n"));
962 1.81 joff dev->powersrc->reattach = 1;
963 1.81 joff dev->bus->needs_explore = 1;
964 1.81 joff wakeup(&dev->bus->needs_explore);
965 1.81 joff }
966 1.81 joff
967 1.26 augustss /* Called at splusb() */
968 1.26 augustss int
969 1.45 augustss usb_get_next_event(struct usb_event *ue)
970 1.26 augustss {
971 1.26 augustss struct usb_event_q *ueq;
972 1.26 augustss
973 1.26 augustss if (usb_nevents <= 0)
974 1.26 augustss return (0);
975 1.95.4.1 itohy ueq = TAILQ_FIRST(&usb_events);
976 1.65 augustss #ifdef DIAGNOSTIC
977 1.65 augustss if (ueq == NULL) {
978 1.65 augustss printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
979 1.65 augustss usb_nevents = 0;
980 1.65 augustss return (0);
981 1.65 augustss }
982 1.65 augustss #endif
983 1.83 drochner if (ue)
984 1.83 drochner *ue = ueq->ue;
985 1.95.4.1 itohy TAILQ_REMOVE(&usb_events, ueq, next);
986 1.87 christos usb_free_event((struct usb_event *)(void *)ueq);
987 1.26 augustss usb_nevents--;
988 1.26 augustss return (1);
989 1.26 augustss }
990 1.26 augustss
991 1.26 augustss void
992 1.45 augustss usbd_add_dev_event(int type, usbd_device_handle udev)
993 1.38 augustss {
994 1.87 christos struct usb_event *ue = usb_alloc_event();
995 1.38 augustss
996 1.87 christos usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
997 1.87 christos usb_add_event(type, ue);
998 1.38 augustss }
999 1.38 augustss
1000 1.38 augustss void
1001 1.45 augustss usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
1002 1.38 augustss {
1003 1.87 christos struct usb_event *ue = usb_alloc_event();
1004 1.87 christos
1005 1.87 christos ue->u.ue_driver.ue_cookie = udev->cookie;
1006 1.87 christos strncpy(ue->u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
1007 1.87 christos sizeof ue->u.ue_driver.ue_devname);
1008 1.87 christos usb_add_event(type, ue);
1009 1.87 christos }
1010 1.87 christos
1011 1.87 christos Static struct usb_event *
1012 1.87 christos usb_alloc_event(void)
1013 1.87 christos {
1014 1.87 christos /* Yes, this is right; we allocate enough so that we can use it later */
1015 1.87 christos return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO);
1016 1.87 christos }
1017 1.38 augustss
1018 1.87 christos Static void
1019 1.87 christos usb_free_event(struct usb_event *uep)
1020 1.87 christos {
1021 1.87 christos free(uep, M_USBDEV);
1022 1.38 augustss }
1023 1.38 augustss
1024 1.42 augustss Static void
1025 1.45 augustss usb_add_event(int type, struct usb_event *uep)
1026 1.26 augustss {
1027 1.26 augustss struct usb_event_q *ueq;
1028 1.26 augustss struct timeval thetime;
1029 1.26 augustss int s;
1030 1.26 augustss
1031 1.38 augustss microtime(&thetime);
1032 1.38 augustss /* Don't want to wait here inside splusb() */
1033 1.87 christos ueq = (struct usb_event_q *)(void *)uep;
1034 1.38 augustss ueq->ue = *uep;
1035 1.38 augustss ueq->ue.ue_type = type;
1036 1.38 augustss TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
1037 1.38 augustss
1038 1.26 augustss s = splusb();
1039 1.95.4.1 itohy if (USB_EVENT_IS_DETACH(type)) {
1040 1.95.4.1 itohy struct usb_event_q *ueqi, *ueqi_next;
1041 1.95.4.1 itohy
1042 1.95.4.1 itohy for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
1043 1.95.4.1 itohy ueqi_next = TAILQ_NEXT(ueqi, next);
1044 1.95.4.1 itohy if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
1045 1.95.4.1 itohy uep->u.ue_device.udi_cookie.cookie) {
1046 1.95.4.1 itohy TAILQ_REMOVE(&usb_events, ueqi, next);
1047 1.95.4.1 itohy free(ueqi, M_USBDEV);
1048 1.95.4.1 itohy usb_nevents--;
1049 1.95.4.1 itohy ueqi_next = TAILQ_FIRST(&usb_events);
1050 1.95.4.1 itohy }
1051 1.95.4.1 itohy }
1052 1.95.4.1 itohy }
1053 1.95.4.1 itohy if (usb_nevents >= USB_MAX_EVENTS) {
1054 1.26 augustss /* Too many queued events, drop an old one. */
1055 1.26 augustss DPRINTFN(-1,("usb: event dropped\n"));
1056 1.83 drochner (void)usb_get_next_event(0);
1057 1.26 augustss }
1058 1.95.4.1 itohy TAILQ_INSERT_TAIL(&usb_events, ueq, next);
1059 1.95.4.1 itohy usb_nevents++;
1060 1.26 augustss wakeup(&usb_events);
1061 1.95.4.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
1062 1.74 jdolecek selnotify(&usb_selevent, 0);
1063 1.95.4.1 itohy #elif defined(__FreeBSD__)
1064 1.95.4.1 itohy selwakeuppri(&usb_selevent, PZERO);
1065 1.95.4.1 itohy #endif
1066 1.94 ad if (usb_async_proc != NULL) {
1067 1.95.4.1 itohy USB_PROC_LOCK(usb_async_proc);
1068 1.26 augustss psignal(usb_async_proc, SIGIO);
1069 1.95.4.1 itohy USB_PROC_UNLOCK(usb_async_proc);
1070 1.94 ad }
1071 1.26 augustss splx(s);
1072 1.39 augustss }
1073 1.60 augustss
1074 1.39 augustss void
1075 1.49 augustss usb_schedsoftintr(usbd_bus_handle bus)
1076 1.39 augustss {
1077 1.54 augustss DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
1078 1.49 augustss #ifdef USB_USE_SOFTINTR
1079 1.49 augustss if (bus->use_polling) {
1080 1.49 augustss bus->methods->soft_intr(bus);
1081 1.49 augustss } else {
1082 1.49 augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1083 1.49 augustss softintr_schedule(bus->soft);
1084 1.49 augustss #else
1085 1.49 augustss if (!callout_pending(&bus->softi))
1086 1.49 augustss callout_reset(&bus->softi, 0, bus->methods->soft_intr,
1087 1.49 augustss bus);
1088 1.49 augustss #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
1089 1.49 augustss }
1090 1.49 augustss #else
1091 1.39 augustss bus->methods->soft_intr(bus);
1092 1.56 augustss #endif /* USB_USE_SOFTINTR */
1093 1.26 augustss }
1094 1.26 augustss
1095 1.95.4.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
1096 1.7 augustss int
1097 1.45 augustss usb_activate(device_ptr_t self, enum devact act)
1098 1.7 augustss {
1099 1.22 augustss struct usb_softc *sc = (struct usb_softc *)self;
1100 1.25 augustss usbd_device_handle dev = sc->sc_port.device;
1101 1.25 augustss int i, rv = 0;
1102 1.22 augustss
1103 1.22 augustss switch (act) {
1104 1.22 augustss case DVACT_ACTIVATE:
1105 1.22 augustss return (EOPNOTSUPP);
1106 1.22 augustss
1107 1.22 augustss case DVACT_DEACTIVATE:
1108 1.22 augustss sc->sc_dying = 1;
1109 1.62 augustss if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
1110 1.25 augustss for (i = 0; dev->subdevs[i]; i++)
1111 1.25 augustss rv |= config_deactivate(dev->subdevs[i]);
1112 1.25 augustss }
1113 1.22 augustss break;
1114 1.22 augustss }
1115 1.22 augustss return (rv);
1116 1.13 augustss }
1117 1.95.4.1 itohy #endif
1118 1.7 augustss
1119 1.95.4.1 itohy USB_DETACH(usb)
1120 1.13 augustss {
1121 1.95.4.1 itohy USB_DETACH_START(usb, sc);
1122 1.87 christos struct usb_event *ue;
1123 1.95.4.1 itohy struct usb_taskq *taskq;
1124 1.95.4.1 itohy int i;
1125 1.22 augustss
1126 1.27 augustss DPRINTF(("usb_detach: start\n"));
1127 1.27 augustss
1128 1.22 augustss sc->sc_dying = 1;
1129 1.22 augustss
1130 1.22 augustss /* Make all devices disconnect. */
1131 1.62 augustss if (sc->sc_port.device != NULL)
1132 1.27 augustss usb_disconnect_port(&sc->sc_port, self);
1133 1.22 augustss
1134 1.22 augustss /* Kill off event thread. */
1135 1.62 augustss if (sc->sc_event_thread != NULL) {
1136 1.58 augustss wakeup(&sc->sc_bus->needs_explore);
1137 1.22 augustss if (tsleep(sc, PWAIT, "usbdet", hz * 60))
1138 1.22 augustss printf("%s: event thread didn't die\n",
1139 1.22 augustss USBDEVNAME(sc->sc_dev));
1140 1.27 augustss DPRINTF(("usb_detach: event thread dead\n"));
1141 1.22 augustss }
1142 1.22 augustss
1143 1.95.4.1 itohy #ifdef __FreeBSD__
1144 1.95.4.1 itohy destroy_dev(sc->sc_usbdev);
1145 1.95.4.1 itohy #endif
1146 1.95.4.1 itohy if (--usb_nbusses == 0) {
1147 1.95.4.1 itohy #ifdef __FreeBSD__
1148 1.95.4.1 itohy destroy_dev(usb_dev);
1149 1.95.4.1 itohy usb_dev = NULL;
1150 1.95.4.1 itohy #endif
1151 1.95.4.1 itohy for (i = 0; i < USB_NUM_TASKQS; i++) {
1152 1.95.4.1 itohy taskq = &usb_taskq[i];
1153 1.95.4.1 itohy wakeup(&taskq->tasks);
1154 1.95.4.1 itohy if (tsleep(&taskq->taskcreated, PWAIT, "usbtdt",
1155 1.95.4.1 itohy hz * 60)) {
1156 1.95.4.1 itohy printf("usb task thread %s didn't die\n",
1157 1.95.4.1 itohy taskq->name);
1158 1.95.4.1 itohy }
1159 1.95.4.1 itohy }
1160 1.95.4.1 itohy }
1161 1.95.4.1 itohy
1162 1.49 augustss #ifdef USB_USE_SOFTINTR
1163 1.49 augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1164 1.49 augustss if (sc->sc_bus->soft != NULL) {
1165 1.49 augustss softintr_disestablish(sc->sc_bus->soft);
1166 1.49 augustss sc->sc_bus->soft = NULL;
1167 1.49 augustss }
1168 1.49 augustss #else
1169 1.49 augustss callout_stop(&sc->sc_bus->softi);
1170 1.49 augustss #endif
1171 1.49 augustss #endif
1172 1.38 augustss
1173 1.87 christos ue = usb_alloc_event();
1174 1.87 christos ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
1175 1.87 christos usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
1176 1.38 augustss
1177 1.7 augustss return (0);
1178 1.7 augustss }
1179 1.92 pavel
1180 1.95.4.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
1181 1.92 pavel Static void
1182 1.92 pavel usb_copy_old_devinfo(struct usb_device_info_old *uo,
1183 1.92 pavel const struct usb_device_info *ue)
1184 1.92 pavel {
1185 1.92 pavel const unsigned char *p;
1186 1.92 pavel unsigned char *q;
1187 1.92 pavel int i, n;
1188 1.92 pavel
1189 1.92 pavel uo->udi_bus = ue->udi_bus;
1190 1.92 pavel uo->udi_addr = ue->udi_addr;
1191 1.92 pavel uo->udi_cookie = ue->udi_cookie;
1192 1.92 pavel for (i = 0, p = (const unsigned char *)ue->udi_product,
1193 1.92 pavel q = (unsigned char *)uo->udi_product;
1194 1.92 pavel *p && i < USB_MAX_STRING_LEN - 1; p++) {
1195 1.92 pavel if (*p < 0x80)
1196 1.92 pavel q[i++] = *p;
1197 1.92 pavel else {
1198 1.92 pavel q[i++] = '?';
1199 1.92 pavel if ((*p & 0xe0) == 0xe0)
1200 1.92 pavel p++;
1201 1.92 pavel p++;
1202 1.92 pavel }
1203 1.92 pavel }
1204 1.92 pavel q[i] = 0;
1205 1.92 pavel
1206 1.92 pavel for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
1207 1.92 pavel *p && i < USB_MAX_STRING_LEN - 1; p++) {
1208 1.92 pavel if (* p < 0x80)
1209 1.92 pavel q[i++] = *p;
1210 1.92 pavel else {
1211 1.92 pavel q[i++] = '?';
1212 1.92 pavel p++;
1213 1.92 pavel if ((*p & 0xe0) == 0xe0)
1214 1.92 pavel p++;
1215 1.92 pavel }
1216 1.92 pavel }
1217 1.92 pavel q[i] = 0;
1218 1.92 pavel
1219 1.92 pavel memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
1220 1.92 pavel
1221 1.92 pavel uo->udi_productNo = ue->udi_productNo;
1222 1.92 pavel uo->udi_vendorNo = ue->udi_vendorNo;
1223 1.92 pavel uo->udi_releaseNo = ue->udi_releaseNo;
1224 1.92 pavel uo->udi_class = ue->udi_class;
1225 1.92 pavel uo->udi_subclass = ue->udi_subclass;
1226 1.92 pavel uo->udi_protocol = ue->udi_protocol;
1227 1.92 pavel uo->udi_config = ue->udi_config;
1228 1.92 pavel uo->udi_speed = ue->udi_speed;
1229 1.92 pavel uo->udi_power = ue->udi_power;
1230 1.92 pavel uo->udi_nports = ue->udi_nports;
1231 1.92 pavel
1232 1.92 pavel for (n=0; n<USB_MAX_DEVNAMES; n++)
1233 1.92 pavel memcpy(uo->udi_devnames[n],
1234 1.92 pavel ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
1235 1.92 pavel memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
1236 1.92 pavel }
1237 1.92 pavel #endif
1238 1.95.4.1 itohy
1239 1.95.4.1 itohy #if defined(__FreeBSD__)
1240 1.95.4.1 itohy Static void
1241 1.95.4.1 itohy usb_child_detached(device_t self, device_t child)
1242 1.95.4.1 itohy {
1243 1.95.4.1 itohy struct usb_softc *sc = device_get_softc(self);
1244 1.95.4.1 itohy
1245 1.95.4.1 itohy /* XXX, should check it is the right device. */
1246 1.95.4.1 itohy sc->sc_port.device = NULL;
1247 1.95.4.1 itohy }
1248 1.95.4.1 itohy
1249 1.95.4.1 itohy /* Explore USB busses at the end of device configuration. */
1250 1.95.4.1 itohy Static void
1251 1.95.4.1 itohy usb_cold_explore(void *arg)
1252 1.95.4.1 itohy {
1253 1.95.4.1 itohy struct usb_softc *sc;
1254 1.95.4.1 itohy
1255 1.95.4.1 itohy USB_KASSERT2(cold || TAILQ_EMPTY(&usb_coldexplist),
1256 1.95.4.1 itohy ("usb_cold_explore: busses to explore when !cold"));
1257 1.95.4.1 itohy while (!TAILQ_EMPTY(&usb_coldexplist)) {
1258 1.95.4.1 itohy sc = TAILQ_FIRST(&usb_coldexplist);
1259 1.95.4.1 itohy TAILQ_REMOVE(&usb_coldexplist, sc, sc_coldexplist);
1260 1.95.4.1 itohy
1261 1.95.4.1 itohy sc->sc_bus->use_polling++;
1262 1.95.4.1 itohy sc->sc_port.device->hub->explore(sc->sc_bus->root_hub);
1263 1.95.4.1 itohy sc->sc_bus->use_polling--;
1264 1.95.4.1 itohy }
1265 1.95.4.1 itohy }
1266 1.95.4.1 itohy
1267 1.95.4.1 itohy DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
1268 1.95.4.1 itohy DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
1269 1.95.4.1 itohy DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);
1270 1.95.4.1 itohy DRIVER_MODULE(usb, slhci, usb_driver, usb_devclass, 0, 0);
1271 1.95.4.1 itohy SYSINIT(usb_cold_explore, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE,
1272 1.95.4.1 itohy usb_cold_explore, NULL);
1273 1.95.4.1 itohy #endif
1274