uhidev.c revision 1.90 1 1.90 riastrad /* $NetBSD: uhidev.c,v 1.90 2022/03/28 12:44:17 riastradh Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.56 mrg * Copyright (c) 2001, 2012 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.1 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at
9 1.56 mrg * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna.com.au).
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 *
20 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
31 1.1 augustss */
32 1.1 augustss
33 1.1 augustss /*
34 1.14 augustss * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
35 1.1 augustss */
36 1.15 lukem
37 1.15 lukem #include <sys/cdefs.h>
38 1.90 riastrad __KERNEL_RCSID(0, "$NetBSD: uhidev.c,v 1.90 2022/03/28 12:44:17 riastradh Exp $");
39 1.66 jakllsch
40 1.66 jakllsch #ifdef _KERNEL_OPT
41 1.66 jakllsch #include "opt_usb.h"
42 1.66 jakllsch #endif
43 1.1 augustss
44 1.1 augustss #include <sys/param.h>
45 1.79 riastrad #include <sys/types.h>
46 1.79 riastrad
47 1.89 riastrad #include <sys/atomic.h>
48 1.79 riastrad #include <sys/conf.h>
49 1.79 riastrad #include <sys/device.h>
50 1.79 riastrad #include <sys/ioctl.h>
51 1.1 augustss #include <sys/kernel.h>
52 1.65 skrll #include <sys/kmem.h>
53 1.79 riastrad #include <sys/lwp.h>
54 1.79 riastrad #include <sys/rndsource.h>
55 1.1 augustss #include <sys/signalvar.h>
56 1.79 riastrad #include <sys/systm.h>
57 1.89 riastrad #include <sys/xcall.h>
58 1.1 augustss
59 1.1 augustss #include <dev/usb/usb.h>
60 1.1 augustss #include <dev/usb/usbhid.h>
61 1.1 augustss
62 1.1 augustss #include <dev/usb/usbdevs.h>
63 1.1 augustss #include <dev/usb/usbdi.h>
64 1.1 augustss #include <dev/usb/usbdi_util.h>
65 1.1 augustss #include <dev/usb/usb_quirks.h>
66 1.1 augustss
67 1.1 augustss #include <dev/usb/uhidev.h>
68 1.73 bouyer #include <dev/hid/hid.h>
69 1.1 augustss
70 1.1 augustss /* Report descriptor for broken Wacom Graphire */
71 1.1 augustss #include <dev/usb/ugraphire_rdesc.h>
72 1.51 jmcneill /* Report descriptor for game controllers in "XInput" mode */
73 1.51 jmcneill #include <dev/usb/xinput_rdesc.h>
74 1.62 jmcneill /* Report descriptor for Xbox One controllers */
75 1.62 jmcneill #include <dev/usb/x1input_rdesc.h>
76 1.1 augustss
77 1.22 drochner #include "locators.h"
78 1.22 drochner
79 1.85 riastrad struct uhidev_softc {
80 1.85 riastrad device_t sc_dev; /* base device */
81 1.85 riastrad struct usbd_device *sc_udev;
82 1.85 riastrad struct usbd_interface *sc_iface; /* interface */
83 1.85 riastrad int sc_iep_addr;
84 1.85 riastrad int sc_oep_addr;
85 1.85 riastrad u_int sc_isize;
86 1.85 riastrad
87 1.85 riastrad int sc_repdesc_size;
88 1.85 riastrad void *sc_repdesc;
89 1.85 riastrad
90 1.85 riastrad u_int sc_nrepid;
91 1.90 riastrad struct uhidev {
92 1.90 riastrad struct uhidev_softc *sc_parent;
93 1.90 riastrad device_t sc_dev;
94 1.90 riastrad void (*sc_intr)(void *, void *, u_int);
95 1.90 riastrad void *sc_cookie;
96 1.90 riastrad krndsource_t sc_rndsource;
97 1.90 riastrad int sc_in_rep_size;
98 1.90 riastrad uint8_t sc_report_id;
99 1.90 riastrad uint8_t sc_state;
100 1.90 riastrad #define UHIDEV_OPEN 0x01 /* device is open */
101 1.90 riastrad #define UHIDEV_STOPPED 0x02 /* xfers are stopped */
102 1.90 riastrad } *sc_subdevs;
103 1.85 riastrad
104 1.85 riastrad kmutex_t sc_lock;
105 1.85 riastrad kcondvar_t sc_cv;
106 1.85 riastrad
107 1.85 riastrad /* Read/written under sc_lock. */
108 1.85 riastrad struct lwp *sc_writelock;
109 1.85 riastrad struct lwp *sc_configlock;
110 1.85 riastrad int sc_refcnt;
111 1.85 riastrad int sc_writereportid;
112 1.86 riastrad int sc_stopreportid;
113 1.85 riastrad u_char sc_dying;
114 1.85 riastrad
115 1.85 riastrad /*
116 1.85 riastrad * - Read under sc_lock, provided sc_refcnt > 0.
117 1.85 riastrad * - Written under sc_configlock only when transitioning to and
118 1.85 riastrad * from sc_refcnt = 0.
119 1.85 riastrad */
120 1.85 riastrad u_char *sc_ibuf;
121 1.85 riastrad struct usbd_pipe *sc_ipipe; /* input interrupt pipe */
122 1.85 riastrad struct usbd_pipe *sc_opipe; /* output interrupt pipe */
123 1.85 riastrad struct usbd_xfer *sc_oxfer; /* write request */
124 1.85 riastrad usbd_callback sc_writecallback; /* async write request callback */
125 1.85 riastrad void *sc_writecookie;
126 1.85 riastrad
127 1.85 riastrad u_int sc_flags;
128 1.85 riastrad #define UHIDEV_F_XB1 0x0001 /* Xbox 1 controller */
129 1.85 riastrad };
130 1.85 riastrad
131 1.1 augustss #ifdef UHIDEV_DEBUG
132 1.48 dyoung #define DPRINTF(x) if (uhidevdebug) printf x
133 1.48 dyoung #define DPRINTFN(n,x) if (uhidevdebug>(n)) printf x
134 1.1 augustss int uhidevdebug = 0;
135 1.1 augustss #else
136 1.1 augustss #define DPRINTF(x)
137 1.1 augustss #define DPRINTFN(n,x)
138 1.1 augustss #endif
139 1.1 augustss
140 1.87 riastrad static void uhidev_intr(struct usbd_xfer *, void *, usbd_status);
141 1.1 augustss
142 1.87 riastrad static int uhidev_maxrepid(void *, int);
143 1.87 riastrad static int uhidevprint(void *, const char *);
144 1.1 augustss
145 1.76 maxv static int uhidev_match(device_t, cfdata_t, void *);
146 1.76 maxv static void uhidev_attach(device_t, device_t, void *);
147 1.76 maxv static void uhidev_childdet(device_t, device_t);
148 1.76 maxv static int uhidev_detach(device_t, int);
149 1.76 maxv static int uhidev_activate(device_t, enum devact);
150 1.75 mrg
151 1.41 cube CFATTACH_DECL2_NEW(uhidev, sizeof(struct uhidev_softc), uhidev_match,
152 1.39 dyoung uhidev_attach, uhidev_detach, uhidev_activate, NULL, uhidev_childdet);
153 1.1 augustss
154 1.76 maxv static int
155 1.48 dyoung uhidev_match(device_t parent, cfdata_t match, void *aux)
156 1.1 augustss {
157 1.65 skrll struct usbif_attach_arg *uiaa = aux;
158 1.6 augustss
159 1.51 jmcneill /* Game controllers in "XInput" mode */
160 1.65 skrll if (USBIF_IS_XINPUT(uiaa))
161 1.51 jmcneill return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
162 1.62 jmcneill /* Xbox One controllers */
163 1.69 skrll if (USBIF_IS_X1INPUT(uiaa) && uiaa->uiaa_ifaceno == 0)
164 1.62 jmcneill return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
165 1.62 jmcneill
166 1.65 skrll if (uiaa->uiaa_class != UICLASS_HID)
167 1.65 skrll return UMATCH_NONE;
168 1.65 skrll if (usbd_get_quirks(uiaa->uiaa_device)->uq_flags & UQ_HID_IGNORE)
169 1.65 skrll return UMATCH_NONE;
170 1.65 skrll return UMATCH_IFACECLASS_GENERIC;
171 1.1 augustss }
172 1.1 augustss
173 1.76 maxv static void
174 1.48 dyoung uhidev_attach(device_t parent, device_t self, void *aux)
175 1.1 augustss {
176 1.48 dyoung struct uhidev_softc *sc = device_private(self);
177 1.65 skrll struct usbif_attach_arg *uiaa = aux;
178 1.65 skrll struct usbd_interface *iface = uiaa->uiaa_iface;
179 1.1 augustss usb_interface_descriptor_t *id;
180 1.1 augustss usb_endpoint_descriptor_t *ed;
181 1.1 augustss struct uhidev_attach_arg uha;
182 1.42 drochner device_t dev;
183 1.44 rafal int maxinpktsize, size, nrepid, repid, repsz;
184 1.32 christos int *repsizes;
185 1.25 skrll int i;
186 1.19 jdolecek void *desc;
187 1.20 augustss const void *descptr;
188 1.1 augustss usbd_status err;
189 1.26 augustss char *devinfop;
190 1.28 drochner int locs[UHIDBUSCF_NLOCS];
191 1.6 augustss
192 1.41 cube sc->sc_dev = self;
193 1.65 skrll sc->sc_udev = uiaa->uiaa_device;
194 1.1 augustss sc->sc_iface = iface;
195 1.43 plunky
196 1.43 plunky aprint_naive("\n");
197 1.43 plunky aprint_normal("\n");
198 1.43 plunky
199 1.65 skrll mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
200 1.79 riastrad cv_init(&sc->sc_cv, "uhidev");
201 1.79 riastrad sc->sc_writelock = NULL;
202 1.79 riastrad sc->sc_configlock = NULL;
203 1.86 riastrad sc->sc_refcnt = 0;
204 1.86 riastrad sc->sc_writereportid = -1;
205 1.86 riastrad sc->sc_stopreportid = -1;
206 1.86 riastrad sc->sc_dying = false;
207 1.56 mrg
208 1.1 augustss id = usbd_get_interface_descriptor(iface);
209 1.26 augustss
210 1.65 skrll devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
211 1.41 cube aprint_normal_dev(self, "%s, iclass %d/%d\n",
212 1.26 augustss devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
213 1.26 augustss usbd_devinfo_free(devinfop);
214 1.1 augustss
215 1.38 jmcneill if (!pmf_device_register(self, NULL, NULL))
216 1.38 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
217 1.38 jmcneill
218 1.72 ryoon if (uiaa->uiaa_vendor == USB_VENDOR_WACOM) {
219 1.72 ryoon if (uiaa->uiaa_product == USB_PRODUCT_WACOM_XD0912U) {
220 1.72 ryoon /*
221 1.72 ryoon * Wacom Intuos2 (XD-0912-U) requires longer idle time to
222 1.72 ryoon * initialize the device with 0x0202.
223 1.72 ryoon */
224 1.72 ryoon DELAY(500000);
225 1.72 ryoon }
226 1.72 ryoon }
227 1.1 augustss (void)usbd_set_idle(iface, 0, 0);
228 1.1 augustss
229 1.82 jakllsch #if 0
230 1.82 jakllsch /*
231 1.82 jakllsch * HID 1.11 says we should do this, but the device firmware is
232 1.82 jakllsch * supposed to come up in Report Protocol after reset anyway, and
233 1.82 jakllsch * apparently explicitly requesting it confuses some devices.
234 1.82 jakllsch */
235 1.82 jakllsch if ((usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_NO_SET_PROTO) == 0 &&
236 1.82 jakllsch id->bInterfaceSubClass == UISUBCLASS_BOOT)
237 1.1 augustss (void)usbd_set_protocol(iface, 1);
238 1.82 jakllsch #endif
239 1.1 augustss
240 1.44 rafal maxinpktsize = 0;
241 1.25 skrll sc->sc_iep_addr = sc->sc_oep_addr = -1;
242 1.25 skrll for (i = 0; i < id->bNumEndpoints; i++) {
243 1.25 skrll ed = usbd_interface2endpoint_descriptor(iface, i);
244 1.25 skrll if (ed == NULL) {
245 1.41 cube aprint_error_dev(self,
246 1.41 cube "could not read endpoint descriptor\n");
247 1.25 skrll sc->sc_dying = 1;
248 1.48 dyoung return;
249 1.25 skrll }
250 1.25 skrll
251 1.25 skrll DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
252 1.25 skrll "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
253 1.25 skrll " bInterval=%d\n",
254 1.25 skrll ed->bLength, ed->bDescriptorType,
255 1.25 skrll ed->bEndpointAddress & UE_ADDR,
256 1.25 skrll UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
257 1.25 skrll ed->bmAttributes & UE_XFERTYPE,
258 1.25 skrll UGETW(ed->wMaxPacketSize), ed->bInterval));
259 1.25 skrll
260 1.25 skrll if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
261 1.25 skrll (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
262 1.44 rafal maxinpktsize = UGETW(ed->wMaxPacketSize);
263 1.25 skrll sc->sc_iep_addr = ed->bEndpointAddress;
264 1.25 skrll } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
265 1.25 skrll (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
266 1.25 skrll sc->sc_oep_addr = ed->bEndpointAddress;
267 1.25 skrll } else {
268 1.41 cube aprint_verbose_dev(self, "endpoint %d: ignored\n", i);
269 1.25 skrll }
270 1.1 augustss }
271 1.1 augustss
272 1.25 skrll /*
273 1.25 skrll * Check that we found an input interrupt endpoint. The output interrupt
274 1.25 skrll * endpoint is optional
275 1.25 skrll */
276 1.25 skrll if (sc->sc_iep_addr == -1) {
277 1.41 cube aprint_error_dev(self, "no input interrupt endpoint\n");
278 1.1 augustss sc->sc_dying = 1;
279 1.48 dyoung return;
280 1.1 augustss }
281 1.1 augustss
282 1.1 augustss /* XXX need to extend this */
283 1.20 augustss descptr = NULL;
284 1.65 skrll if (uiaa->uiaa_vendor == USB_VENDOR_WACOM) {
285 1.72 ryoon static uByte reportbuf[3];
286 1.17 augustss
287 1.1 augustss /* The report descriptor for the Wacom Graphire is broken. */
288 1.65 skrll switch (uiaa->uiaa_product) {
289 1.33 ghen case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
290 1.33 ghen case USB_PRODUCT_WACOM_GRAPHIRE3_6X8:
291 1.33 ghen case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: /* The 6x8 too? */
292 1.17 augustss /*
293 1.17 augustss * The Graphire3 needs 0x0202 to be written to
294 1.17 augustss * feature report ID 2 before it'll start
295 1.17 augustss * returning digitizer data.
296 1.17 augustss */
297 1.72 ryoon reportbuf[0] = 0x02;
298 1.72 ryoon reportbuf[1] = 0x02;
299 1.65 skrll usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 2,
300 1.72 ryoon &reportbuf, 2);
301 1.17 augustss
302 1.65 skrll size = sizeof(uhid_graphire3_4x5_report_descr);
303 1.17 augustss descptr = uhid_graphire3_4x5_report_descr;
304 1.17 augustss break;
305 1.72 ryoon case USB_PRODUCT_WACOM_GRAPHIRE:
306 1.72 ryoon case USB_PRODUCT_WACOM_GRAPHIRE2:
307 1.72 ryoon case USB_PRODUCT_WACOM_XD0912U:
308 1.72 ryoon case USB_PRODUCT_WACOM_CTH690K0:
309 1.72 ryoon reportbuf[0] = 0x02;
310 1.72 ryoon reportbuf[1] = 0x02;
311 1.72 ryoon usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 2,
312 1.72 ryoon &reportbuf, 2);
313 1.72 ryoon break;
314 1.20 augustss default:
315 1.20 augustss /* Keep descriptor */
316 1.20 augustss break;
317 1.17 augustss }
318 1.17 augustss }
319 1.65 skrll if (USBIF_IS_XINPUT(uiaa)) {
320 1.65 skrll size = sizeof(uhid_xinput_report_descr);
321 1.51 jmcneill descptr = uhid_xinput_report_descr;
322 1.51 jmcneill }
323 1.65 skrll if (USBIF_IS_X1INPUT(uiaa)) {
324 1.62 jmcneill sc->sc_flags |= UHIDEV_F_XB1;
325 1.65 skrll size = sizeof(uhid_x1input_report_descr);
326 1.62 jmcneill descptr = uhid_x1input_report_descr;
327 1.62 jmcneill }
328 1.17 augustss
329 1.17 augustss if (descptr) {
330 1.65 skrll desc = kmem_alloc(size, KM_SLEEP);
331 1.70 chs err = USBD_NORMAL_COMPLETION;
332 1.70 chs memcpy(desc, descptr, size);
333 1.1 augustss } else {
334 1.1 augustss desc = NULL;
335 1.65 skrll err = usbd_read_report_desc(uiaa->uiaa_iface, &desc, &size);
336 1.1 augustss }
337 1.1 augustss if (err) {
338 1.41 cube aprint_error_dev(self, "no report descriptor\n");
339 1.1 augustss sc->sc_dying = 1;
340 1.48 dyoung return;
341 1.1 augustss }
342 1.6 augustss
343 1.65 skrll if (uiaa->uiaa_vendor == USB_VENDOR_HOSIDEN &&
344 1.65 skrll uiaa->uiaa_product == USB_PRODUCT_HOSIDEN_PPP) {
345 1.27 augustss static uByte reportbuf[] = { 1 };
346 1.27 augustss /*
347 1.58 skrll * This device was sold by Konami with its ParaParaParadise
348 1.27 augustss * game for PlayStation2. It needs to be "turned on"
349 1.27 augustss * before it will send any reports.
350 1.27 augustss */
351 1.27 augustss
352 1.65 skrll usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 0,
353 1.65 skrll &reportbuf, sizeof(reportbuf));
354 1.27 augustss }
355 1.27 augustss
356 1.65 skrll if (uiaa->uiaa_vendor == USB_VENDOR_LOGITECH &&
357 1.65 skrll uiaa->uiaa_product == USB_PRODUCT_LOGITECH_CBT44 && size == 0xb1) {
358 1.47 jakllsch uint8_t *data = desc;
359 1.47 jakllsch /*
360 1.47 jakllsch * This device has a odd USAGE_MINIMUM value that would
361 1.47 jakllsch * cause the multimedia keys to have their usage number
362 1.47 jakllsch * shifted up one usage. Adjust so the usages are sane.
363 1.47 jakllsch */
364 1.47 jakllsch
365 1.47 jakllsch if (data[0x56] == 0x19 && data[0x57] == 0x01 &&
366 1.47 jakllsch data[0x58] == 0x2a && data[0x59] == 0x8c)
367 1.47 jakllsch data[0x57] = 0x00;
368 1.47 jakllsch }
369 1.47 jakllsch
370 1.52 aymeric /*
371 1.52 aymeric * Enable the Six Axis and DualShock 3 controllers.
372 1.52 aymeric * See http://ps3.jim.sh/sixaxis/usb/
373 1.52 aymeric */
374 1.65 skrll if (uiaa->uiaa_vendor == USB_VENDOR_SONY &&
375 1.65 skrll uiaa->uiaa_product == USB_PRODUCT_SONY_PS3CONTROLLER) {
376 1.52 aymeric usb_device_request_t req;
377 1.52 aymeric char data[17];
378 1.52 aymeric int actlen;
379 1.52 aymeric
380 1.52 aymeric req.bmRequestType = UT_READ_CLASS_INTERFACE;
381 1.52 aymeric req.bRequest = 1;
382 1.52 aymeric USETW(req.wValue, 0x3f2);
383 1.52 aymeric USETW(req.wIndex, 0);
384 1.65 skrll USETW(req.wLength, sizeof(data));
385 1.52 aymeric
386 1.52 aymeric usbd_do_request_flags(sc->sc_udev, &req, data,
387 1.52 aymeric USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
388 1.52 aymeric }
389 1.52 aymeric
390 1.1 augustss sc->sc_repdesc = desc;
391 1.1 augustss sc->sc_repdesc_size = size;
392 1.1 augustss
393 1.65 skrll uha.uiaa = uiaa;
394 1.1 augustss nrepid = uhidev_maxrepid(desc, size);
395 1.1 augustss if (nrepid < 0)
396 1.48 dyoung return;
397 1.1 augustss if (nrepid > 0)
398 1.41 cube aprint_normal_dev(self, "%d report ids\n", nrepid);
399 1.1 augustss nrepid++;
400 1.65 skrll repsizes = kmem_alloc(nrepid * sizeof(*repsizes), KM_SLEEP);
401 1.79 riastrad sc->sc_subdevs = kmem_zalloc(nrepid * sizeof(sc->sc_subdevs[0]),
402 1.65 skrll KM_SLEEP);
403 1.44 rafal
404 1.44 rafal /* Just request max packet size for the interrupt pipe */
405 1.44 rafal sc->sc_isize = maxinpktsize;
406 1.1 augustss sc->sc_nrepid = nrepid;
407 1.1 augustss
408 1.68 msaitoh usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
409 1.1 augustss
410 1.1 augustss for (repid = 0; repid < nrepid; repid++) {
411 1.1 augustss repsz = hid_report_size(desc, size, hid_input, repid);
412 1.1 augustss DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
413 1.2 augustss repsizes[repid] = repsz;
414 1.1 augustss }
415 1.44 rafal
416 1.1 augustss DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
417 1.1 augustss
418 1.1 augustss for (repid = 0; repid < nrepid; repid++) {
419 1.90 riastrad struct uhidev *scd = &sc->sc_subdevs[repid];
420 1.90 riastrad
421 1.90 riastrad scd->sc_parent = sc;
422 1.90 riastrad scd->sc_report_id = repid;
423 1.90 riastrad scd->sc_in_rep_size = repsizes[repid];
424 1.90 riastrad
425 1.1 augustss DPRINTF(("uhidev_match: try repid=%d\n", repid));
426 1.1 augustss if (hid_report_size(desc, size, hid_input, repid) == 0 &&
427 1.1 augustss hid_report_size(desc, size, hid_output, repid) == 0 &&
428 1.1 augustss hid_report_size(desc, size, hid_feature, repid) == 0) {
429 1.5 augustss ; /* already NULL in sc->sc_subdevs[repid] */
430 1.1 augustss } else {
431 1.90 riastrad uha.parent = scd;
432 1.1 augustss uha.reportid = repid;
433 1.28 drochner locs[UHIDBUSCF_REPORTID] = repid;
434 1.22 drochner
435 1.80 thorpej dev = config_found(self, &uha, uhidevprint,
436 1.81 thorpej CFARGS(.submatch = config_stdsubmatch,
437 1.81 thorpej .locators = locs));
438 1.90 riastrad sc->sc_subdevs[repid].sc_dev = dev;
439 1.90 riastrad if (dev == NULL)
440 1.90 riastrad continue;
441 1.90 riastrad /*
442 1.90 riastrad * XXXSMP -- could be detached in the middle of
443 1.90 riastrad * sleeping for allocation in rnd_attach_source
444 1.90 riastrad */
445 1.90 riastrad rnd_attach_source(&scd->sc_rndsource,
446 1.90 riastrad device_xname(dev), RND_TYPE_TTY, RND_FLAG_DEFAULT);
447 1.1 augustss }
448 1.1 augustss }
449 1.65 skrll kmem_free(repsizes, nrepid * sizeof(*repsizes));
450 1.1 augustss
451 1.48 dyoung return;
452 1.1 augustss }
453 1.1 augustss
454 1.87 riastrad static int
455 1.1 augustss uhidev_maxrepid(void *buf, int len)
456 1.1 augustss {
457 1.1 augustss struct hid_data *d;
458 1.1 augustss struct hid_item h;
459 1.1 augustss int maxid;
460 1.1 augustss
461 1.1 augustss maxid = -1;
462 1.1 augustss h.report_ID = 0;
463 1.1 augustss for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
464 1.74 jakllsch if ((int)h.report_ID > maxid)
465 1.1 augustss maxid = h.report_ID;
466 1.1 augustss hid_end_parse(d);
467 1.65 skrll return maxid;
468 1.1 augustss }
469 1.1 augustss
470 1.87 riastrad static int
471 1.1 augustss uhidevprint(void *aux, const char *pnp)
472 1.1 augustss {
473 1.1 augustss struct uhidev_attach_arg *uha = aux;
474 1.1 augustss
475 1.1 augustss if (pnp)
476 1.12 thorpej aprint_normal("uhid at %s", pnp);
477 1.1 augustss if (uha->reportid != 0)
478 1.12 thorpej aprint_normal(" reportid %d", uha->reportid);
479 1.65 skrll return UNCONF;
480 1.1 augustss }
481 1.1 augustss
482 1.76 maxv static int
483 1.39 dyoung uhidev_activate(device_t self, enum devact act)
484 1.1 augustss {
485 1.39 dyoung struct uhidev_softc *sc = device_private(self);
486 1.1 augustss
487 1.1 augustss switch (act) {
488 1.1 augustss case DVACT_DEACTIVATE:
489 1.1 augustss sc->sc_dying = 1;
490 1.45 dyoung return 0;
491 1.16 christos default:
492 1.45 dyoung return EOPNOTSUPP;
493 1.1 augustss }
494 1.1 augustss }
495 1.1 augustss
496 1.76 maxv static void
497 1.39 dyoung uhidev_childdet(device_t self, device_t child)
498 1.39 dyoung {
499 1.39 dyoung int i;
500 1.39 dyoung struct uhidev_softc *sc = device_private(self);
501 1.39 dyoung
502 1.39 dyoung for (i = 0; i < sc->sc_nrepid; i++) {
503 1.90 riastrad if (sc->sc_subdevs[i].sc_dev == child)
504 1.39 dyoung break;
505 1.39 dyoung }
506 1.39 dyoung KASSERT(i < sc->sc_nrepid);
507 1.90 riastrad sc->sc_subdevs[i].sc_dev = NULL;
508 1.90 riastrad /*
509 1.90 riastrad * XXXSMP -- could be reattached in the middle of sleeping for
510 1.90 riastrad * lock on sources to delete this in rnd_attach_source
511 1.90 riastrad *
512 1.90 riastrad * (Actually this can't happen right now because there's no
513 1.90 riastrad * rescan method, but if there were, it could.)
514 1.90 riastrad */
515 1.90 riastrad rnd_detach_source(&sc->sc_subdevs[i].sc_rndsource);
516 1.39 dyoung }
517 1.39 dyoung
518 1.76 maxv static int
519 1.48 dyoung uhidev_detach(device_t self, int flags)
520 1.1 augustss {
521 1.48 dyoung struct uhidev_softc *sc = device_private(self);
522 1.90 riastrad int rv;
523 1.1 augustss
524 1.1 augustss DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
525 1.1 augustss
526 1.79 riastrad /* Notify that we are going away. */
527 1.79 riastrad mutex_enter(&sc->sc_lock);
528 1.1 augustss sc->sc_dying = 1;
529 1.79 riastrad cv_broadcast(&sc->sc_cv);
530 1.79 riastrad mutex_exit(&sc->sc_lock);
531 1.1 augustss
532 1.79 riastrad /*
533 1.79 riastrad * Try to detach all our children. If anything fails, bail.
534 1.79 riastrad * Failure can happen if this is from drvctl -d; of course, if
535 1.79 riastrad * this is a USB device being yanked, flags will have
536 1.79 riastrad * DETACH_FORCE and the children will not have the option of
537 1.79 riastrad * refusing detachment.
538 1.79 riastrad */
539 1.90 riastrad rv = config_detach_children(self, flags);
540 1.90 riastrad if (rv) {
541 1.90 riastrad mutex_enter(&sc->sc_lock);
542 1.90 riastrad sc->sc_dying = 0;
543 1.90 riastrad mutex_exit(&sc->sc_lock);
544 1.90 riastrad return rv;
545 1.1 augustss }
546 1.1 augustss
547 1.79 riastrad KASSERTMSG(sc->sc_refcnt == 0,
548 1.79 riastrad "%s: %d refs remain", device_xname(sc->sc_dev), sc->sc_refcnt);
549 1.79 riastrad KASSERT(sc->sc_opipe == NULL);
550 1.79 riastrad KASSERT(sc->sc_ipipe == NULL);
551 1.79 riastrad KASSERT(sc->sc_ibuf == NULL);
552 1.79 riastrad
553 1.79 riastrad if (sc->sc_repdesc != NULL) {
554 1.79 riastrad kmem_free(sc->sc_repdesc, sc->sc_repdesc_size);
555 1.79 riastrad sc->sc_repdesc = NULL;
556 1.79 riastrad }
557 1.79 riastrad if (sc->sc_subdevs != NULL) {
558 1.79 riastrad int nrepid = sc->sc_nrepid;
559 1.79 riastrad kmem_free(sc->sc_subdevs, nrepid * sizeof(sc->sc_subdevs[0]));
560 1.79 riastrad sc->sc_subdevs = NULL;
561 1.79 riastrad }
562 1.79 riastrad
563 1.68 msaitoh usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
564 1.1 augustss
565 1.38 jmcneill pmf_device_deregister(self);
566 1.79 riastrad KASSERT(sc->sc_configlock == NULL);
567 1.79 riastrad KASSERT(sc->sc_writelock == NULL);
568 1.79 riastrad cv_destroy(&sc->sc_cv);
569 1.56 mrg mutex_destroy(&sc->sc_lock);
570 1.38 jmcneill
571 1.65 skrll return rv;
572 1.1 augustss }
573 1.1 augustss
574 1.87 riastrad static void
575 1.65 skrll uhidev_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
576 1.1 augustss {
577 1.1 augustss struct uhidev_softc *sc = addr;
578 1.1 augustss struct uhidev *scd;
579 1.1 augustss u_char *p;
580 1.1 augustss u_int rep;
581 1.65 skrll uint32_t cc;
582 1.1 augustss
583 1.1 augustss usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
584 1.1 augustss
585 1.1 augustss #ifdef UHIDEV_DEBUG
586 1.1 augustss if (uhidevdebug > 5) {
587 1.65 skrll uint32_t i;
588 1.6 augustss
589 1.1 augustss DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
590 1.1 augustss DPRINTF(("uhidev_intr: data ="));
591 1.1 augustss for (i = 0; i < cc; i++)
592 1.1 augustss DPRINTF((" %02x", sc->sc_ibuf[i]));
593 1.1 augustss DPRINTF(("\n"));
594 1.1 augustss }
595 1.1 augustss #endif
596 1.1 augustss
597 1.1 augustss if (status == USBD_CANCELLED)
598 1.1 augustss return;
599 1.1 augustss
600 1.1 augustss if (status != USBD_NORMAL_COMPLETION) {
601 1.48 dyoung DPRINTF(("%s: interrupt status=%d\n", device_xname(sc->sc_dev),
602 1.3 augustss status));
603 1.25 skrll usbd_clear_endpoint_stall_async(sc->sc_ipipe);
604 1.1 augustss return;
605 1.1 augustss }
606 1.1 augustss
607 1.1 augustss p = sc->sc_ibuf;
608 1.1 augustss if (sc->sc_nrepid != 1)
609 1.1 augustss rep = *p++, cc--;
610 1.1 augustss else
611 1.1 augustss rep = 0;
612 1.1 augustss if (rep >= sc->sc_nrepid) {
613 1.1 augustss printf("uhidev_intr: bad repid %d\n", rep);
614 1.1 augustss return;
615 1.1 augustss }
616 1.90 riastrad scd = &sc->sc_subdevs[rep];
617 1.77 christos DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=%#x\n",
618 1.90 riastrad rep, scd, scd->sc_state));
619 1.89 riastrad if (!(atomic_load_acquire(&scd->sc_state) & UHIDEV_OPEN))
620 1.1 augustss return;
621 1.46 jakllsch #ifdef UHIDEV_DEBUG
622 1.46 jakllsch if (scd->sc_in_rep_size != cc) {
623 1.46 jakllsch DPRINTF(("%s: expected %d bytes, got %d\n",
624 1.48 dyoung device_xname(sc->sc_dev), scd->sc_in_rep_size, cc));
625 1.46 jakllsch }
626 1.46 jakllsch #endif
627 1.46 jakllsch if (cc == 0) {
628 1.46 jakllsch DPRINTF(("%s: 0-length input ignored\n",
629 1.48 dyoung device_xname(sc->sc_dev)));
630 1.23 augustss return;
631 1.23 augustss }
632 1.90 riastrad rnd_add_uint32(&scd->sc_rndsource, (uintptr_t)(sc->sc_ibuf));
633 1.90 riastrad scd->sc_intr(scd->sc_cookie, p, cc);
634 1.1 augustss }
635 1.1 augustss
636 1.1 augustss void
637 1.90 riastrad uhidev_get_report_desc(struct uhidev *scd, void **desc, int *size)
638 1.1 augustss {
639 1.90 riastrad struct uhidev_softc *sc = scd->sc_parent;
640 1.90 riastrad
641 1.1 augustss *desc = sc->sc_repdesc;
642 1.1 augustss *size = sc->sc_repdesc_size;
643 1.1 augustss }
644 1.1 augustss
645 1.79 riastrad static int
646 1.79 riastrad uhidev_config_enter(struct uhidev_softc *sc)
647 1.79 riastrad {
648 1.79 riastrad int error;
649 1.79 riastrad
650 1.79 riastrad KASSERT(mutex_owned(&sc->sc_lock));
651 1.79 riastrad
652 1.79 riastrad for (;;) {
653 1.79 riastrad if (sc->sc_dying)
654 1.79 riastrad return ENXIO;
655 1.79 riastrad if (sc->sc_configlock == NULL)
656 1.79 riastrad break;
657 1.79 riastrad error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
658 1.79 riastrad if (error)
659 1.79 riastrad return error;
660 1.79 riastrad }
661 1.79 riastrad
662 1.79 riastrad sc->sc_configlock = curlwp;
663 1.79 riastrad return 0;
664 1.79 riastrad }
665 1.79 riastrad
666 1.79 riastrad static void
667 1.79 riastrad uhidev_config_enter_nointr(struct uhidev_softc *sc)
668 1.79 riastrad {
669 1.79 riastrad
670 1.79 riastrad KASSERT(mutex_owned(&sc->sc_lock));
671 1.79 riastrad
672 1.79 riastrad while (sc->sc_configlock)
673 1.79 riastrad cv_wait(&sc->sc_cv, &sc->sc_lock);
674 1.79 riastrad sc->sc_configlock = curlwp;
675 1.79 riastrad }
676 1.79 riastrad
677 1.79 riastrad static void
678 1.79 riastrad uhidev_config_exit(struct uhidev_softc *sc)
679 1.79 riastrad {
680 1.79 riastrad
681 1.79 riastrad KASSERT(mutex_owned(&sc->sc_lock));
682 1.79 riastrad KASSERTMSG(sc->sc_configlock == curlwp, "%s: migrated from %p to %p",
683 1.79 riastrad device_xname(sc->sc_dev), curlwp, sc->sc_configlock);
684 1.79 riastrad
685 1.79 riastrad sc->sc_configlock = NULL;
686 1.79 riastrad cv_broadcast(&sc->sc_cv);
687 1.79 riastrad }
688 1.79 riastrad
689 1.79 riastrad /*
690 1.79 riastrad * uhidev_open_pipes(sc)
691 1.79 riastrad *
692 1.79 riastrad * Ensure the pipes of the softc are open. Caller must hold
693 1.79 riastrad * sc_lock, which may be released and reacquired.
694 1.79 riastrad */
695 1.79 riastrad static int
696 1.79 riastrad uhidev_open_pipes(struct uhidev_softc *sc)
697 1.1 augustss {
698 1.1 augustss usbd_status err;
699 1.25 skrll int error;
700 1.1 augustss
701 1.79 riastrad KASSERT(mutex_owned(&sc->sc_lock));
702 1.79 riastrad
703 1.79 riastrad /* If the device is dying, refuse. */
704 1.79 riastrad if (sc->sc_dying)
705 1.79 riastrad return ENXIO;
706 1.1 augustss
707 1.79 riastrad /*
708 1.79 riastrad * If the pipes are already open, just increment the reference
709 1.79 riastrad * count, or fail if it would overflow.
710 1.79 riastrad */
711 1.79 riastrad if (sc->sc_refcnt) {
712 1.79 riastrad if (sc->sc_refcnt == INT_MAX)
713 1.79 riastrad return EBUSY;
714 1.79 riastrad sc->sc_refcnt++;
715 1.65 skrll return 0;
716 1.60 skrll }
717 1.1 augustss
718 1.79 riastrad /*
719 1.79 riastrad * If there's no input data to prepare, don't bother with the
720 1.79 riastrad * pipes. We assume any device that does output also does
721 1.79 riastrad * input; if you have a device where this is wrong, then
722 1.79 riastrad * uhidev_write will fail gracefully (it checks sc->sc_opipe),
723 1.79 riastrad * and you can use that device to test the changes needed to
724 1.79 riastrad * open the output pipe here.
725 1.79 riastrad */
726 1.1 augustss if (sc->sc_isize == 0)
727 1.65 skrll return 0;
728 1.1 augustss
729 1.79 riastrad /*
730 1.87 riastrad * Lock the configuration and release sc_lock -- we may sleep
731 1.87 riastrad * to allocate. If someone else got in first, we're done;
732 1.79 riastrad * otherwise open the pipes.
733 1.79 riastrad */
734 1.79 riastrad error = uhidev_config_enter(sc);
735 1.79 riastrad if (error)
736 1.79 riastrad goto out;
737 1.79 riastrad if (sc->sc_refcnt) {
738 1.79 riastrad if (sc->sc_refcnt == INT_MAX) {
739 1.79 riastrad error = EBUSY;
740 1.79 riastrad } else {
741 1.79 riastrad sc->sc_refcnt++;
742 1.79 riastrad error = 0;
743 1.79 riastrad }
744 1.79 riastrad goto out0;
745 1.79 riastrad }
746 1.79 riastrad mutex_exit(&sc->sc_lock);
747 1.79 riastrad
748 1.79 riastrad /* Allocate an input buffer. */
749 1.65 skrll sc->sc_ibuf = kmem_alloc(sc->sc_isize, KM_SLEEP);
750 1.1 augustss
751 1.25 skrll /* Set up input interrupt pipe. */
752 1.79 riastrad DPRINTF(("%s: isize=%d, ep=0x%02x\n", __func__, sc->sc_isize,
753 1.25 skrll sc->sc_iep_addr));
754 1.58 skrll
755 1.25 skrll err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
756 1.25 skrll USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
757 1.1 augustss sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
758 1.25 skrll if (err != USBD_NORMAL_COMPLETION) {
759 1.1 augustss DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
760 1.25 skrll "error=%d\n", err));
761 1.25 skrll error = EIO;
762 1.25 skrll goto out1;
763 1.25 skrll }
764 1.25 skrll
765 1.25 skrll /*
766 1.25 skrll * Set up output interrupt pipe if an output interrupt endpoint
767 1.25 skrll * exists.
768 1.25 skrll */
769 1.25 skrll if (sc->sc_oep_addr != -1) {
770 1.78 christos DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
771 1.25 skrll
772 1.25 skrll err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
773 1.25 skrll 0, &sc->sc_opipe);
774 1.25 skrll
775 1.25 skrll if (err != USBD_NORMAL_COMPLETION) {
776 1.25 skrll DPRINTF(("uhidev_open: usbd_open_pipe failed, "
777 1.25 skrll "error=%d\n", err));
778 1.25 skrll error = EIO;
779 1.25 skrll goto out2;
780 1.25 skrll }
781 1.25 skrll DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
782 1.25 skrll
783 1.65 skrll error = usbd_create_xfer(sc->sc_opipe, UHIDEV_OSIZE, 0, 0,
784 1.65 skrll &sc->sc_oxfer);
785 1.65 skrll if (error) {
786 1.25 skrll DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
787 1.25 skrll goto out3;
788 1.25 skrll }
789 1.62 jmcneill
790 1.62 jmcneill if (sc->sc_flags & UHIDEV_F_XB1) {
791 1.62 jmcneill uint8_t init_data[] = { 0x05, 0x20 };
792 1.62 jmcneill int init_data_len = sizeof(init_data);
793 1.62 jmcneill err = usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
794 1.65 skrll USBD_NO_TIMEOUT, init_data, &init_data_len);
795 1.62 jmcneill if (err != USBD_NORMAL_COMPLETION) {
796 1.62 jmcneill DPRINTF(("uhidev_open: xb1 init failed, "
797 1.62 jmcneill "error=%d\n", err));
798 1.62 jmcneill error = EIO;
799 1.62 jmcneill goto out4;
800 1.62 jmcneill }
801 1.62 jmcneill }
802 1.1 augustss }
803 1.58 skrll
804 1.79 riastrad /* Success! */
805 1.79 riastrad mutex_enter(&sc->sc_lock);
806 1.79 riastrad KASSERTMSG(sc->sc_refcnt == 0, "%d refs spuriously acquired",
807 1.79 riastrad sc->sc_refcnt);
808 1.79 riastrad sc->sc_refcnt++;
809 1.79 riastrad goto out0;
810 1.79 riastrad
811 1.79 riastrad out4: if (sc->sc_oxfer) {
812 1.79 riastrad usbd_abort_pipe(sc->sc_opipe);
813 1.65 skrll usbd_destroy_xfer(sc->sc_oxfer);
814 1.79 riastrad sc->sc_oxfer = NULL;
815 1.79 riastrad }
816 1.79 riastrad out3: if (sc->sc_opipe) {
817 1.79 riastrad usbd_close_pipe(sc->sc_opipe);
818 1.79 riastrad sc->sc_opipe = NULL;
819 1.79 riastrad }
820 1.79 riastrad out2: if (sc->sc_ipipe) {
821 1.79 riastrad usbd_abort_pipe(sc->sc_ipipe);
822 1.79 riastrad usbd_close_pipe(sc->sc_ipipe);
823 1.79 riastrad sc->sc_ipipe = NULL;
824 1.79 riastrad }
825 1.79 riastrad out1: kmem_free(sc->sc_ibuf, sc->sc_isize);
826 1.79 riastrad sc->sc_ibuf = NULL;
827 1.56 mrg mutex_enter(&sc->sc_lock);
828 1.79 riastrad out0: KASSERT(mutex_owned(&sc->sc_lock));
829 1.79 riastrad uhidev_config_exit(sc);
830 1.79 riastrad out: KASSERT(mutex_owned(&sc->sc_lock));
831 1.25 skrll return error;
832 1.1 augustss }
833 1.1 augustss
834 1.79 riastrad static void
835 1.79 riastrad uhidev_close_pipes(struct uhidev_softc *sc)
836 1.63 mrg {
837 1.63 mrg
838 1.79 riastrad KASSERT(mutex_owned(&sc->sc_lock));
839 1.79 riastrad KASSERTMSG(sc->sc_refcnt > 0, "%s: refcnt fouled: %d",
840 1.79 riastrad device_xname(sc->sc_dev), sc->sc_refcnt);
841 1.79 riastrad
842 1.79 riastrad /* If this isn't the last reference, just decrement. */
843 1.79 riastrad if (sc->sc_refcnt > 1) {
844 1.79 riastrad sc->sc_refcnt--;
845 1.79 riastrad return;
846 1.79 riastrad }
847 1.79 riastrad
848 1.79 riastrad /*
849 1.79 riastrad * Lock the configuration and release sc_lock so we may sleep
850 1.79 riastrad * to free memory. We're not waiting for anyone to allocate or
851 1.79 riastrad * free anything.
852 1.79 riastrad */
853 1.79 riastrad uhidev_config_enter_nointr(sc);
854 1.79 riastrad
855 1.79 riastrad /*
856 1.79 riastrad * If someone else acquired a reference while we were waiting
857 1.79 riastrad * for the config lock, nothing more for us to do.
858 1.79 riastrad */
859 1.79 riastrad if (sc->sc_refcnt > 1) {
860 1.79 riastrad sc->sc_refcnt--;
861 1.79 riastrad uhidev_config_exit(sc);
862 1.79 riastrad return;
863 1.79 riastrad }
864 1.79 riastrad
865 1.79 riastrad /*
866 1.79 riastrad * We're the last reference and committed to closing the pipes.
867 1.79 riastrad * Decrement the reference count before we release the lock --
868 1.79 riastrad * access to the pipes is allowed as long as the reference
869 1.79 riastrad * count is positive, so this forces all new opens to wait
870 1.79 riastrad * until the config lock is released.
871 1.79 riastrad */
872 1.79 riastrad KASSERTMSG(sc->sc_refcnt == 1, "%s: refcnt fouled: %d",
873 1.79 riastrad device_xname(sc->sc_dev), sc->sc_refcnt);
874 1.79 riastrad sc->sc_refcnt--;
875 1.79 riastrad mutex_exit(&sc->sc_lock);
876 1.79 riastrad
877 1.79 riastrad if (sc->sc_oxfer) {
878 1.63 mrg usbd_abort_pipe(sc->sc_opipe);
879 1.79 riastrad usbd_destroy_xfer(sc->sc_oxfer);
880 1.79 riastrad sc->sc_oxfer = NULL;
881 1.79 riastrad }
882 1.79 riastrad if (sc->sc_opipe) {
883 1.63 mrg usbd_close_pipe(sc->sc_opipe);
884 1.63 mrg sc->sc_opipe = NULL;
885 1.63 mrg }
886 1.79 riastrad if (sc->sc_ipipe) {
887 1.63 mrg usbd_abort_pipe(sc->sc_ipipe);
888 1.63 mrg usbd_close_pipe(sc->sc_ipipe);
889 1.63 mrg sc->sc_ipipe = NULL;
890 1.63 mrg }
891 1.79 riastrad kmem_free(sc->sc_ibuf, sc->sc_isize);
892 1.79 riastrad sc->sc_ibuf = NULL;
893 1.79 riastrad
894 1.79 riastrad mutex_enter(&sc->sc_lock);
895 1.79 riastrad uhidev_config_exit(sc);
896 1.79 riastrad KASSERTMSG(sc->sc_refcnt == 0, "%s: refcnt fouled: %d",
897 1.79 riastrad device_xname(sc->sc_dev), sc->sc_refcnt);
898 1.79 riastrad }
899 1.79 riastrad
900 1.79 riastrad int
901 1.90 riastrad uhidev_open(struct uhidev *scd, void (*intr)(void *, void *, u_int),
902 1.90 riastrad void *cookie)
903 1.79 riastrad {
904 1.79 riastrad struct uhidev_softc *sc = scd->sc_parent;
905 1.79 riastrad int error;
906 1.79 riastrad
907 1.79 riastrad mutex_enter(&sc->sc_lock);
908 1.79 riastrad
909 1.79 riastrad DPRINTF(("uhidev_open(%s, report %d = %s): state=%x refcnt=%d\n",
910 1.79 riastrad device_xname(sc->sc_dev),
911 1.79 riastrad scd->sc_report_id,
912 1.79 riastrad device_xname(scd->sc_dev),
913 1.79 riastrad scd->sc_state,
914 1.79 riastrad sc->sc_refcnt));
915 1.63 mrg
916 1.79 riastrad /* Mark the report id open. This is an exclusive lock. */
917 1.79 riastrad if (scd->sc_state & UHIDEV_OPEN) {
918 1.79 riastrad error = EBUSY;
919 1.88 riastrad goto out;
920 1.63 mrg }
921 1.90 riastrad scd->sc_intr = intr;
922 1.90 riastrad scd->sc_cookie = cookie;
923 1.89 riastrad atomic_store_release(&scd->sc_state, scd->sc_state | UHIDEV_OPEN);
924 1.79 riastrad
925 1.79 riastrad /* Open the pipes which are shared by all report ids. */
926 1.79 riastrad error = uhidev_open_pipes(sc);
927 1.79 riastrad if (error)
928 1.88 riastrad goto out;
929 1.79 riastrad
930 1.79 riastrad /* Success! */
931 1.88 riastrad error = 0;
932 1.79 riastrad
933 1.88 riastrad out: if (error) {
934 1.88 riastrad KASSERTMSG(scd->sc_state & UHIDEV_OPEN,
935 1.88 riastrad "%s: report id %d: closed while opening",
936 1.88 riastrad device_xname(sc->sc_dev), scd->sc_report_id);
937 1.89 riastrad atomic_store_relaxed(&scd->sc_state,
938 1.89 riastrad scd->sc_state & ~UHIDEV_OPEN);
939 1.88 riastrad }
940 1.88 riastrad mutex_exit(&sc->sc_lock);
941 1.79 riastrad return error;
942 1.63 mrg }
943 1.63 mrg
944 1.86 riastrad /*
945 1.86 riastrad * uhidev_stop(scd)
946 1.86 riastrad *
947 1.86 riastrad * Make all current and future output reports or xfers by scd to
948 1.86 riastrad * the output pipe to fail. Caller must then ensure no more will
949 1.86 riastrad * be submitted and then call uhidev_close.
950 1.86 riastrad *
951 1.86 riastrad * Side effect: If uhidev_write was in progress for this scd,
952 1.86 riastrad * blocks all other uhidev_writes until uhidev_close on this scd.
953 1.86 riastrad *
954 1.86 riastrad * May sleep but only for a short duration to wait for USB
955 1.86 riastrad * transfer completion callbacks to run.
956 1.86 riastrad */
957 1.63 mrg void
958 1.79 riastrad uhidev_stop(struct uhidev *scd)
959 1.1 augustss {
960 1.1 augustss struct uhidev_softc *sc = scd->sc_parent;
961 1.1 augustss
962 1.56 mrg mutex_enter(&sc->sc_lock);
963 1.86 riastrad
964 1.86 riastrad /* Prevent further writes on this report from starting. */
965 1.89 riastrad atomic_store_relaxed(&scd->sc_state, scd->sc_state | UHIDEV_STOPPED);
966 1.86 riastrad
967 1.86 riastrad /* If there's no output pipe at all, nothing to do. */
968 1.86 riastrad if (sc->sc_opipe == NULL)
969 1.86 riastrad goto out;
970 1.86 riastrad
971 1.86 riastrad /*
972 1.86 riastrad * If there's no write on this report in progress, nothing to
973 1.86 riastrad * do -- any subsequent attempts will be prevented by
974 1.86 riastrad * UHIDEV_STOPPED.
975 1.86 riastrad */
976 1.86 riastrad if (sc->sc_writereportid != scd->sc_report_id)
977 1.86 riastrad goto out;
978 1.86 riastrad
979 1.86 riastrad /*
980 1.86 riastrad * Caller must wait for uhidev_open to succeed before calling
981 1.86 riastrad * uhidev_write, and must wait for all uhidev_writes to return
982 1.86 riastrad * before calling uhidev_close, so neither on can be in flight
983 1.86 riastrad * right now.
984 1.86 riastrad *
985 1.86 riastrad * Suspend the pipe, but hold up uhidev_write from any report
986 1.86 riastrad * until we confirm this one has finished. We will resume the
987 1.86 riastrad * pipe only after all uhidev_writes on this report have
988 1.86 riastrad * finished -- when the caller calls uhidev_close.
989 1.86 riastrad */
990 1.86 riastrad KASSERTMSG(sc->sc_stopreportid == -1, "%d", sc->sc_stopreportid);
991 1.86 riastrad sc->sc_stopreportid = scd->sc_report_id;
992 1.56 mrg mutex_exit(&sc->sc_lock);
993 1.56 mrg
994 1.86 riastrad usbd_suspend_pipe(sc->sc_opipe);
995 1.86 riastrad
996 1.86 riastrad mutex_enter(&sc->sc_lock);
997 1.86 riastrad KASSERT(sc->sc_stopreportid == scd->sc_report_id);
998 1.86 riastrad sc->sc_stopreportid = scd->sc_report_id;
999 1.86 riastrad cv_broadcast(&sc->sc_cv);
1000 1.86 riastrad out: mutex_exit(&sc->sc_lock);
1001 1.79 riastrad }
1002 1.79 riastrad
1003 1.86 riastrad /*
1004 1.86 riastrad * uhidev_close(scd)
1005 1.86 riastrad *
1006 1.86 riastrad * Close a uhidev previously opened with uhidev_open. If writes
1007 1.86 riastrad * had been stopped with uhidev_stop, allow writes at other report
1008 1.86 riastrad * ids again.
1009 1.86 riastrad */
1010 1.79 riastrad void
1011 1.79 riastrad uhidev_close(struct uhidev *scd)
1012 1.79 riastrad {
1013 1.79 riastrad struct uhidev_softc *sc = scd->sc_parent;
1014 1.1 augustss
1015 1.79 riastrad mutex_enter(&sc->sc_lock);
1016 1.58 skrll
1017 1.79 riastrad DPRINTF(("uhidev_close(%s, report %d = %s): state=%x refcnt=%d\n",
1018 1.79 riastrad device_xname(sc->sc_dev),
1019 1.79 riastrad scd->sc_report_id,
1020 1.79 riastrad device_xname(scd->sc_dev),
1021 1.79 riastrad scd->sc_state,
1022 1.79 riastrad sc->sc_refcnt));
1023 1.79 riastrad
1024 1.79 riastrad KASSERTMSG(scd->sc_state & UHIDEV_OPEN,
1025 1.79 riastrad "%s: report id %d: unpaired close",
1026 1.79 riastrad device_xname(sc->sc_dev), scd->sc_report_id);
1027 1.86 riastrad
1028 1.86 riastrad /*
1029 1.86 riastrad * If the caller had issued uhidev_stop to interrupt a write
1030 1.86 riastrad * for this report, then resume the pipe now that no further
1031 1.86 riastrad * uhidev_write on the same report is possible, and wake anyone
1032 1.86 riastrad * trying to write on other reports.
1033 1.86 riastrad */
1034 1.86 riastrad if (sc->sc_stopreportid == scd->sc_report_id) {
1035 1.86 riastrad KASSERT(scd->sc_state & UHIDEV_STOPPED);
1036 1.86 riastrad mutex_exit(&sc->sc_lock);
1037 1.86 riastrad
1038 1.86 riastrad usbd_resume_pipe(sc->sc_opipe);
1039 1.86 riastrad
1040 1.86 riastrad mutex_enter(&sc->sc_lock);
1041 1.86 riastrad KASSERT(sc->sc_stopreportid == scd->sc_report_id);
1042 1.86 riastrad KASSERT(scd->sc_state & UHIDEV_STOPPED);
1043 1.86 riastrad sc->sc_stopreportid = -1;
1044 1.86 riastrad cv_broadcast(&sc->sc_cv);
1045 1.86 riastrad }
1046 1.86 riastrad
1047 1.86 riastrad /*
1048 1.86 riastrad * Close our reference to the pipes, and mark our report as no
1049 1.86 riastrad * longer open. If it was stopped, clear that too -- drivers
1050 1.86 riastrad * are forbidden from issuing writes after uhidev_close anyway.
1051 1.86 riastrad */
1052 1.86 riastrad KASSERT(scd->sc_state & UHIDEV_OPEN);
1053 1.79 riastrad uhidev_close_pipes(sc);
1054 1.86 riastrad KASSERT(scd->sc_state & UHIDEV_OPEN);
1055 1.89 riastrad atomic_store_relaxed(&scd->sc_state,
1056 1.89 riastrad scd->sc_state & ~(UHIDEV_OPEN | UHIDEV_STOPPED));
1057 1.65 skrll
1058 1.89 riastrad /*
1059 1.89 riastrad * Make sure the next uhidev_intr (which runs in softint, like
1060 1.89 riastrad * XC_HIGHPRI) notices that UHIDEV_OPEN is cleared, and wait
1061 1.89 riastrad * for any current one to finish, in case the pipe is still
1062 1.89 riastrad * open for other report ids.
1063 1.89 riastrad *
1064 1.89 riastrad * We must drop the lock while doing this, because
1065 1.89 riastrad * uhidev_write_callback takes the lock in softint context and
1066 1.89 riastrad * it could deadlock with the xcall softint.
1067 1.90 riastrad *
1068 1.90 riastrad * It is safe to drop the lock now before zeroing sc_intr and
1069 1.90 riastrad * sc_cookie because the driver is obligated not to reopen
1070 1.90 riastrad * until after uhidev_close returns.
1071 1.89 riastrad */
1072 1.79 riastrad mutex_exit(&sc->sc_lock);
1073 1.89 riastrad xc_barrier(XC_HIGHPRI);
1074 1.90 riastrad mutex_enter(&sc->sc_lock);
1075 1.90 riastrad KASSERT((scd->sc_state & UHIDEV_OPEN) == 0);
1076 1.90 riastrad scd->sc_intr = NULL;
1077 1.90 riastrad scd->sc_cookie = NULL;
1078 1.90 riastrad
1079 1.90 riastrad mutex_exit(&sc->sc_lock);
1080 1.1 augustss }
1081 1.1 augustss
1082 1.1 augustss usbd_status
1083 1.1 augustss uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
1084 1.1 augustss {
1085 1.13 dsainty char *buf;
1086 1.13 dsainty usbd_status retstat;
1087 1.13 dsainty
1088 1.13 dsainty if (scd->sc_report_id == 0)
1089 1.13 dsainty return usbd_set_report(scd->sc_parent->sc_iface, type,
1090 1.13 dsainty scd->sc_report_id, data, len);
1091 1.13 dsainty
1092 1.65 skrll buf = kmem_alloc(len + 1, KM_SLEEP);
1093 1.13 dsainty buf[0] = scd->sc_report_id;
1094 1.13 dsainty memcpy(buf+1, data, len);
1095 1.13 dsainty
1096 1.13 dsainty retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
1097 1.18 dsainty scd->sc_report_id, buf, len + 1);
1098 1.13 dsainty
1099 1.65 skrll kmem_free(buf, len + 1);
1100 1.1 augustss
1101 1.13 dsainty return retstat;
1102 1.1 augustss }
1103 1.1 augustss
1104 1.1 augustss usbd_status
1105 1.1 augustss uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
1106 1.1 augustss {
1107 1.6 augustss return usbd_get_report(scd->sc_parent->sc_iface, type,
1108 1.1 augustss scd->sc_report_id, data, len);
1109 1.1 augustss }
1110 1.25 skrll
1111 1.25 skrll usbd_status
1112 1.83 riastrad uhidev_write(struct uhidev *scd, void *data, int len)
1113 1.25 skrll {
1114 1.83 riastrad struct uhidev_softc *sc = scd->sc_parent;
1115 1.79 riastrad usbd_status err;
1116 1.25 skrll
1117 1.25 skrll DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
1118 1.25 skrll
1119 1.25 skrll if (sc->sc_opipe == NULL)
1120 1.25 skrll return USBD_INVAL;
1121 1.25 skrll
1122 1.79 riastrad mutex_enter(&sc->sc_lock);
1123 1.79 riastrad KASSERT(sc->sc_refcnt);
1124 1.79 riastrad for (;;) {
1125 1.79 riastrad if (sc->sc_dying) {
1126 1.79 riastrad err = USBD_IOERROR;
1127 1.79 riastrad goto out;
1128 1.79 riastrad }
1129 1.86 riastrad if (scd->sc_state & UHIDEV_STOPPED) {
1130 1.86 riastrad err = USBD_CANCELLED;
1131 1.86 riastrad goto out;
1132 1.86 riastrad }
1133 1.86 riastrad if (sc->sc_writelock == NULL && sc->sc_stopreportid == -1)
1134 1.79 riastrad break;
1135 1.79 riastrad if (cv_wait_sig(&sc->sc_cv, &sc->sc_lock)) {
1136 1.79 riastrad err = USBD_INTERRUPTED;
1137 1.79 riastrad goto out;
1138 1.79 riastrad }
1139 1.79 riastrad }
1140 1.79 riastrad sc->sc_writelock = curlwp;
1141 1.83 riastrad sc->sc_writereportid = scd->sc_report_id;
1142 1.79 riastrad mutex_exit(&sc->sc_lock);
1143 1.79 riastrad
1144 1.25 skrll #ifdef UHIDEV_DEBUG
1145 1.25 skrll if (uhidevdebug > 50) {
1146 1.25 skrll
1147 1.65 skrll uint32_t i;
1148 1.65 skrll uint8_t *d = data;
1149 1.25 skrll
1150 1.25 skrll DPRINTF(("uhidev_write: data ="));
1151 1.25 skrll for (i = 0; i < len; i++)
1152 1.25 skrll DPRINTF((" %02x", d[i]));
1153 1.25 skrll DPRINTF(("\n"));
1154 1.25 skrll }
1155 1.25 skrll #endif
1156 1.79 riastrad err = usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
1157 1.79 riastrad USBD_NO_TIMEOUT, data, &len);
1158 1.79 riastrad
1159 1.79 riastrad mutex_enter(&sc->sc_lock);
1160 1.79 riastrad KASSERT(sc->sc_refcnt);
1161 1.79 riastrad KASSERTMSG(sc->sc_writelock == curlwp, "%s: migrated from %p to %p",
1162 1.79 riastrad device_xname(sc->sc_dev), curlwp, sc->sc_writelock);
1163 1.83 riastrad KASSERTMSG(sc->sc_writereportid == scd->sc_report_id,
1164 1.83 riastrad "%s: changed write report ids from %d to %d",
1165 1.83 riastrad device_xname(sc->sc_dev), scd->sc_report_id, sc->sc_writereportid);
1166 1.83 riastrad sc->sc_writereportid = -1;
1167 1.79 riastrad sc->sc_writelock = NULL;
1168 1.79 riastrad cv_broadcast(&sc->sc_cv);
1169 1.79 riastrad out: mutex_exit(&sc->sc_lock);
1170 1.79 riastrad return err;
1171 1.25 skrll }
1172 1.84 riastrad
1173 1.84 riastrad static void
1174 1.84 riastrad uhidev_write_callback(struct usbd_xfer *xfer, void *cookie, usbd_status err)
1175 1.84 riastrad {
1176 1.84 riastrad struct uhidev_softc *sc = cookie;
1177 1.84 riastrad usbd_callback writecallback;
1178 1.84 riastrad void *writecookie;
1179 1.84 riastrad
1180 1.84 riastrad if (err) {
1181 1.84 riastrad if (err != USBD_CANCELLED)
1182 1.84 riastrad usbd_clear_endpoint_stall_async(sc->sc_opipe);
1183 1.84 riastrad }
1184 1.84 riastrad
1185 1.84 riastrad mutex_enter(&sc->sc_lock);
1186 1.84 riastrad KASSERT(sc->sc_writelock == (void *)1);
1187 1.84 riastrad writecallback = sc->sc_writecallback;
1188 1.84 riastrad writecookie = sc->sc_writecookie;
1189 1.84 riastrad sc->sc_writereportid = -1;
1190 1.84 riastrad sc->sc_writelock = NULL;
1191 1.84 riastrad sc->sc_writecallback = NULL;
1192 1.84 riastrad sc->sc_writecookie = NULL;
1193 1.84 riastrad cv_broadcast(&sc->sc_cv);
1194 1.84 riastrad mutex_exit(&sc->sc_lock);
1195 1.84 riastrad
1196 1.84 riastrad (*writecallback)(xfer, writecookie, err);
1197 1.84 riastrad }
1198 1.84 riastrad
1199 1.84 riastrad usbd_status
1200 1.84 riastrad uhidev_write_async(struct uhidev *scd, void *data, int len, int flags,
1201 1.84 riastrad int timo, usbd_callback writecallback, void *writecookie)
1202 1.84 riastrad {
1203 1.84 riastrad struct uhidev_softc *sc = scd->sc_parent;
1204 1.84 riastrad usbd_status err;
1205 1.84 riastrad
1206 1.84 riastrad DPRINTF(("%s: data=%p, len=%d\n", __func__, data, len));
1207 1.84 riastrad
1208 1.84 riastrad if (sc->sc_opipe == NULL)
1209 1.84 riastrad return USBD_INVAL;
1210 1.84 riastrad
1211 1.84 riastrad mutex_enter(&sc->sc_lock);
1212 1.84 riastrad KASSERT(sc->sc_refcnt);
1213 1.84 riastrad if (sc->sc_dying) {
1214 1.84 riastrad err = USBD_IOERROR;
1215 1.84 riastrad goto out;
1216 1.84 riastrad }
1217 1.86 riastrad if (scd->sc_state & UHIDEV_STOPPED) {
1218 1.86 riastrad err = USBD_CANCELLED;
1219 1.86 riastrad goto out;
1220 1.86 riastrad }
1221 1.86 riastrad if (sc->sc_writelock != NULL || sc->sc_stopreportid != -1) {
1222 1.84 riastrad err = USBD_IN_USE;
1223 1.84 riastrad goto out;
1224 1.84 riastrad }
1225 1.84 riastrad sc->sc_writelock = (void *)1; /* XXX no lwp to attribute async xfer */
1226 1.84 riastrad sc->sc_writereportid = scd->sc_report_id;
1227 1.84 riastrad sc->sc_writecallback = writecallback;
1228 1.84 riastrad sc->sc_writecookie = writecookie;
1229 1.84 riastrad usbd_setup_xfer(sc->sc_oxfer, sc, data, len, flags, timo,
1230 1.84 riastrad uhidev_write_callback);
1231 1.84 riastrad err = usbd_transfer(sc->sc_oxfer);
1232 1.84 riastrad switch (err) {
1233 1.84 riastrad case USBD_IN_PROGRESS:
1234 1.84 riastrad break;
1235 1.84 riastrad case USBD_NORMAL_COMPLETION:
1236 1.84 riastrad panic("unexpected normal completion of async xfer under lock");
1237 1.84 riastrad default: /* error */
1238 1.84 riastrad sc->sc_writelock = NULL;
1239 1.84 riastrad sc->sc_writereportid = -1;
1240 1.84 riastrad sc->sc_writecallback = NULL;
1241 1.84 riastrad sc->sc_writecookie = NULL;
1242 1.84 riastrad cv_broadcast(&sc->sc_cv);
1243 1.84 riastrad }
1244 1.84 riastrad out: mutex_exit(&sc->sc_lock);
1245 1.84 riastrad return err;
1246 1.84 riastrad }
1247