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