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