uhidev.c revision 1.14.2.5 1 1.14.2.5 skrll /* $NetBSD: uhidev.c,v 1.14.2.5 2005/03/04 16:50:55 skrll Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 2001 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.1 augustss * Carlstedt Research & Technology.
10 1.1 augustss *
11 1.1 augustss * Redistribution and use in source and binary forms, with or without
12 1.1 augustss * modification, are permitted provided that the following conditions
13 1.1 augustss * are met:
14 1.1 augustss * 1. Redistributions of source code must retain the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer.
16 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 augustss * notice, this list of conditions and the following disclaimer in the
18 1.1 augustss * documentation and/or other materials provided with the distribution.
19 1.1 augustss * 3. All advertising materials mentioning features or use of this software
20 1.1 augustss * must display the following acknowledgement:
21 1.1 augustss * This product includes software developed by the NetBSD
22 1.1 augustss * Foundation, Inc. and its contributors.
23 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 augustss * contributors may be used to endorse or promote products derived
25 1.1 augustss * from this software without specific prior written permission.
26 1.1 augustss *
27 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
38 1.1 augustss */
39 1.1 augustss
40 1.1 augustss /*
41 1.14 augustss * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
42 1.1 augustss */
43 1.1 augustss
44 1.14.2.1 skrll #include <sys/cdefs.h>
45 1.14.2.5 skrll __KERNEL_RCSID(0, "$NetBSD: uhidev.c,v 1.14.2.5 2005/03/04 16:50:55 skrll Exp $");
46 1.14.2.1 skrll
47 1.1 augustss #include <sys/param.h>
48 1.1 augustss #include <sys/systm.h>
49 1.1 augustss #include <sys/kernel.h>
50 1.1 augustss #include <sys/malloc.h>
51 1.1 augustss #include <sys/signalvar.h>
52 1.1 augustss #include <sys/device.h>
53 1.1 augustss #include <sys/ioctl.h>
54 1.1 augustss #include <sys/conf.h>
55 1.1 augustss
56 1.1 augustss #include <dev/usb/usb.h>
57 1.1 augustss #include <dev/usb/usbhid.h>
58 1.1 augustss
59 1.1 augustss #include <dev/usb/usbdevs.h>
60 1.1 augustss #include <dev/usb/usbdi.h>
61 1.1 augustss #include <dev/usb/usbdi_util.h>
62 1.1 augustss #include <dev/usb/hid.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.1 augustss
67 1.1 augustss /* Report descriptor for broken Wacom Graphire */
68 1.1 augustss #include <dev/usb/ugraphire_rdesc.h>
69 1.1 augustss
70 1.14.2.2 skrll #include "locators.h"
71 1.14.2.2 skrll
72 1.1 augustss #ifdef UHIDEV_DEBUG
73 1.1 augustss #define DPRINTF(x) if (uhidevdebug) logprintf x
74 1.1 augustss #define DPRINTFN(n,x) if (uhidevdebug>(n)) logprintf x
75 1.1 augustss int uhidevdebug = 0;
76 1.1 augustss #else
77 1.1 augustss #define DPRINTF(x)
78 1.1 augustss #define DPRINTFN(n,x)
79 1.1 augustss #endif
80 1.1 augustss
81 1.1 augustss Static void uhidev_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
82 1.1 augustss
83 1.1 augustss Static int uhidev_maxrepid(void *buf, int len);
84 1.1 augustss Static int uhidevprint(void *aux, const char *pnp);
85 1.14.2.2 skrll Static int uhidevsubmatch(struct device *parent, struct cfdata *cf,
86 1.14.2.2 skrll const locdesc_t *, void *aux);
87 1.1 augustss
88 1.1 augustss USB_DECLARE_DRIVER(uhidev);
89 1.1 augustss
90 1.1 augustss USB_MATCH(uhidev)
91 1.1 augustss {
92 1.1 augustss USB_MATCH_START(uhidev, uaa);
93 1.1 augustss usb_interface_descriptor_t *id;
94 1.6 augustss
95 1.1 augustss if (uaa->iface == NULL)
96 1.1 augustss return (UMATCH_NONE);
97 1.1 augustss id = usbd_get_interface_descriptor(uaa->iface);
98 1.1 augustss if (id == NULL || id->bInterfaceClass != UICLASS_HID)
99 1.1 augustss return (UMATCH_NONE);
100 1.1 augustss if (uaa->matchlvl)
101 1.1 augustss return (uaa->matchlvl);
102 1.1 augustss return (UMATCH_IFACECLASS_GENERIC);
103 1.1 augustss }
104 1.1 augustss
105 1.1 augustss USB_ATTACH(uhidev)
106 1.1 augustss {
107 1.1 augustss USB_ATTACH_START(uhidev, sc, uaa);
108 1.1 augustss usbd_interface_handle iface = uaa->iface;
109 1.1 augustss usb_interface_descriptor_t *id;
110 1.1 augustss usb_endpoint_descriptor_t *ed;
111 1.1 augustss struct uhidev_attach_arg uha;
112 1.1 augustss struct uhidev *dev;
113 1.1 augustss int size, nrepid, repid, repsz;
114 1.2 augustss int repsizes[256];
115 1.1 augustss void *desc;
116 1.14.2.1 skrll const void *descptr;
117 1.1 augustss usbd_status err;
118 1.1 augustss char devinfo[1024];
119 1.14.2.2 skrll int help[2];
120 1.14.2.2 skrll locdesc_t *ldesc = (void *)help; /* XXX */
121 1.6 augustss
122 1.1 augustss sc->sc_udev = uaa->device;
123 1.1 augustss sc->sc_iface = iface;
124 1.1 augustss id = usbd_get_interface_descriptor(iface);
125 1.14.2.1 skrll usbd_devinfo(uaa->device, 0, devinfo, sizeof(devinfo));
126 1.1 augustss USB_ATTACH_SETUP;
127 1.1 augustss printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
128 1.1 augustss devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
129 1.1 augustss
130 1.1 augustss (void)usbd_set_idle(iface, 0, 0);
131 1.1 augustss #if 0
132 1.1 augustss
133 1.1 augustss qflags = usbd_get_quirks(sc->sc_udev)->uq_flags;
134 1.1 augustss if ((qflags & UQ_NO_SET_PROTO) == 0 &&
135 1.1 augustss id->bInterfaceSubClass != UISUBCLASS_BOOT)
136 1.1 augustss (void)usbd_set_protocol(iface, 1);
137 1.1 augustss #endif
138 1.1 augustss
139 1.1 augustss ed = usbd_interface2endpoint_descriptor(iface, 0);
140 1.1 augustss if (ed == NULL) {
141 1.1 augustss printf("%s: could not read endpoint descriptor\n",
142 1.1 augustss USBDEVNAME(sc->sc_dev));
143 1.1 augustss sc->sc_dying = 1;
144 1.1 augustss USB_ATTACH_ERROR_RETURN;
145 1.1 augustss }
146 1.1 augustss
147 1.1 augustss DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
148 1.1 augustss "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
149 1.1 augustss " bInterval=%d\n",
150 1.6 augustss ed->bLength, ed->bDescriptorType,
151 1.1 augustss ed->bEndpointAddress & UE_ADDR,
152 1.1 augustss UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
153 1.1 augustss ed->bmAttributes & UE_XFERTYPE,
154 1.1 augustss UGETW(ed->wMaxPacketSize), ed->bInterval));
155 1.1 augustss
156 1.1 augustss if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
157 1.1 augustss (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
158 1.1 augustss printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
159 1.1 augustss sc->sc_dying = 1;
160 1.1 augustss USB_ATTACH_ERROR_RETURN;
161 1.1 augustss }
162 1.1 augustss
163 1.1 augustss sc->sc_ep_addr = ed->bEndpointAddress;
164 1.1 augustss
165 1.1 augustss /* XXX need to extend this */
166 1.14.2.1 skrll descptr = NULL;
167 1.14.2.1 skrll if (uaa->vendor == USB_VENDOR_WACOM) {
168 1.14.2.1 skrll static uByte reportbuf[] = {2, 2, 2};
169 1.14.2.1 skrll
170 1.1 augustss /* The report descriptor for the Wacom Graphire is broken. */
171 1.14.2.1 skrll switch (uaa->product) {
172 1.14.2.1 skrll case USB_PRODUCT_WACOM_GRAPHIRE:
173 1.14.2.1 skrll size = sizeof uhid_graphire_report_descr;
174 1.14.2.1 skrll descptr = uhid_graphire_report_descr;
175 1.14.2.1 skrll break;
176 1.14.2.1 skrll
177 1.14.2.1 skrll case USB_PRODUCT_WACOM_GRAPHIRE3_4X5: /* The 6x8 too? */
178 1.14.2.1 skrll /*
179 1.14.2.1 skrll * The Graphire3 needs 0x0202 to be written to
180 1.14.2.1 skrll * feature report ID 2 before it'll start
181 1.14.2.1 skrll * returning digitizer data.
182 1.14.2.1 skrll */
183 1.14.2.1 skrll usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2,
184 1.14.2.1 skrll &reportbuf, sizeof reportbuf);
185 1.14.2.1 skrll
186 1.14.2.1 skrll size = sizeof uhid_graphire3_4x5_report_descr;
187 1.14.2.1 skrll descptr = uhid_graphire3_4x5_report_descr;
188 1.14.2.1 skrll break;
189 1.14.2.1 skrll default:
190 1.14.2.1 skrll /* Keep descriptor */
191 1.14.2.1 skrll break;
192 1.14.2.1 skrll }
193 1.14.2.1 skrll }
194 1.14.2.1 skrll
195 1.14.2.1 skrll if (descptr) {
196 1.1 augustss desc = malloc(size, M_USBDEV, M_NOWAIT);
197 1.1 augustss if (desc == NULL)
198 1.1 augustss err = USBD_NOMEM;
199 1.1 augustss else {
200 1.1 augustss err = USBD_NORMAL_COMPLETION;
201 1.14.2.1 skrll memcpy(desc, descptr, size);
202 1.1 augustss }
203 1.1 augustss } else {
204 1.1 augustss desc = NULL;
205 1.14.2.1 skrll err = usbd_read_report_desc(uaa->iface, &desc, &size,
206 1.14.2.1 skrll M_USBDEV);
207 1.1 augustss }
208 1.1 augustss if (err) {
209 1.1 augustss printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
210 1.1 augustss sc->sc_dying = 1;
211 1.1 augustss USB_ATTACH_ERROR_RETURN;
212 1.1 augustss }
213 1.6 augustss
214 1.1 augustss sc->sc_repdesc = desc;
215 1.1 augustss sc->sc_repdesc_size = size;
216 1.1 augustss
217 1.1 augustss uha.uaa = uaa;
218 1.1 augustss nrepid = uhidev_maxrepid(desc, size);
219 1.1 augustss if (nrepid < 0)
220 1.1 augustss USB_ATTACH_SUCCESS_RETURN;
221 1.1 augustss if (nrepid > 0)
222 1.1 augustss printf("%s: %d report ids\n", USBDEVNAME(sc->sc_dev), nrepid);
223 1.1 augustss nrepid++;
224 1.1 augustss sc->sc_subdevs = malloc(nrepid * sizeof(device_ptr_t),
225 1.5 augustss M_USBDEV, M_NOWAIT | M_ZERO);
226 1.1 augustss if (sc->sc_subdevs == NULL) {
227 1.1 augustss printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
228 1.1 augustss USB_ATTACH_ERROR_RETURN;
229 1.1 augustss }
230 1.1 augustss sc->sc_nrepid = nrepid;
231 1.1 augustss sc->sc_isize = 0;
232 1.1 augustss
233 1.1 augustss usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
234 1.1 augustss USBDEV(sc->sc_dev));
235 1.1 augustss
236 1.1 augustss for (repid = 0; repid < nrepid; repid++) {
237 1.1 augustss repsz = hid_report_size(desc, size, hid_input, repid);
238 1.1 augustss DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
239 1.2 augustss repsizes[repid] = repsz;
240 1.1 augustss if (repsz > 0) {
241 1.1 augustss if (repsz > sc->sc_isize)
242 1.1 augustss sc->sc_isize = repsz;
243 1.1 augustss }
244 1.1 augustss }
245 1.1 augustss sc->sc_isize += nrepid != 1; /* space for report ID */
246 1.1 augustss DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
247 1.1 augustss
248 1.1 augustss uha.parent = sc;
249 1.1 augustss for (repid = 0; repid < nrepid; repid++) {
250 1.1 augustss DPRINTF(("uhidev_match: try repid=%d\n", repid));
251 1.1 augustss if (hid_report_size(desc, size, hid_input, repid) == 0 &&
252 1.1 augustss hid_report_size(desc, size, hid_output, repid) == 0 &&
253 1.1 augustss hid_report_size(desc, size, hid_feature, repid) == 0) {
254 1.5 augustss ; /* already NULL in sc->sc_subdevs[repid] */
255 1.1 augustss } else {
256 1.1 augustss uha.reportid = repid;
257 1.14.2.2 skrll ldesc->len = 1;
258 1.14.2.2 skrll ldesc->locs[UHIDBUSCF_REPORTID] = repid;
259 1.14.2.2 skrll
260 1.14.2.2 skrll dev = (struct uhidev *)config_found_sm_loc(self,
261 1.14.2.2 skrll "uhidbus", ldesc, &uha,
262 1.14.2.2 skrll uhidevprint, uhidevsubmatch);
263 1.1 augustss sc->sc_subdevs[repid] = dev;
264 1.4 augustss if (dev != NULL) {
265 1.4 augustss dev->sc_in_rep_size = repsizes[repid];
266 1.1 augustss #ifdef DIAGNOSTIC
267 1.1 augustss DPRINTF(("uhidev_match: repid=%d dev=%p\n",
268 1.1 augustss repid, dev));
269 1.1 augustss if (dev->sc_intr == NULL) {
270 1.1 augustss printf("%s: sc_intr == NULL\n",
271 1.1 augustss USBDEVNAME(sc->sc_dev));
272 1.1 augustss USB_ATTACH_ERROR_RETURN;
273 1.1 augustss }
274 1.4 augustss #endif
275 1.9 dan #if NRND > 0
276 1.14.2.5 skrll rnd_attach_source(&dev->rnd_source,
277 1.14.2.5 skrll USBDEVNAME(dev->sc_dev),
278 1.9 dan RND_TYPE_TTY, 0);
279 1.9 dan #endif
280 1.1 augustss }
281 1.1 augustss }
282 1.1 augustss }
283 1.1 augustss
284 1.1 augustss USB_ATTACH_SUCCESS_RETURN;
285 1.1 augustss }
286 1.1 augustss
287 1.1 augustss int
288 1.1 augustss uhidev_maxrepid(void *buf, int len)
289 1.1 augustss {
290 1.1 augustss struct hid_data *d;
291 1.1 augustss struct hid_item h;
292 1.1 augustss int maxid;
293 1.1 augustss
294 1.1 augustss maxid = -1;
295 1.1 augustss h.report_ID = 0;
296 1.1 augustss for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
297 1.1 augustss if (h.report_ID > maxid)
298 1.1 augustss maxid = h.report_ID;
299 1.1 augustss hid_end_parse(d);
300 1.1 augustss return (maxid);
301 1.1 augustss }
302 1.1 augustss
303 1.1 augustss int
304 1.1 augustss uhidevprint(void *aux, const char *pnp)
305 1.1 augustss {
306 1.1 augustss struct uhidev_attach_arg *uha = aux;
307 1.1 augustss
308 1.1 augustss if (pnp)
309 1.12 thorpej aprint_normal("uhid at %s", pnp);
310 1.1 augustss if (uha->reportid != 0)
311 1.12 thorpej aprint_normal(" reportid %d", uha->reportid);
312 1.1 augustss return (UNCONF);
313 1.1 augustss }
314 1.1 augustss
315 1.1 augustss int
316 1.14.2.2 skrll uhidevsubmatch(struct device *parent, struct cfdata *cf,
317 1.14.2.2 skrll const locdesc_t *ldesc, void *aux)
318 1.1 augustss {
319 1.1 augustss struct uhidev_attach_arg *uha = aux;
320 1.1 augustss
321 1.14.2.2 skrll if (cf->cf_loc[UHIDBUSCF_REPORTID] != UHIDBUSCF_REPORTID_DEFAULT &&
322 1.14.2.2 skrll cf->cf_loc[UHIDBUSCF_REPORTID] != ldesc->locs[UHIDBUSCF_REPORTID])
323 1.1 augustss return (0);
324 1.14.2.2 skrll
325 1.14.2.2 skrll if (cf->cf_loc[UHIDBUSCF_REPORTID] == ldesc->locs[UHIDBUSCF_REPORTID])
326 1.1 augustss uha->matchlvl = UMATCH_VENDOR_PRODUCT;
327 1.1 augustss else
328 1.1 augustss uha->matchlvl = 0;
329 1.8 thorpej return (config_match(parent, cf, aux));
330 1.1 augustss }
331 1.1 augustss
332 1.1 augustss int
333 1.1 augustss uhidev_activate(device_ptr_t self, enum devact act)
334 1.1 augustss {
335 1.1 augustss struct uhidev_softc *sc = (struct uhidev_softc *)self;
336 1.1 augustss int i, rv;
337 1.1 augustss
338 1.1 augustss switch (act) {
339 1.1 augustss case DVACT_ACTIVATE:
340 1.1 augustss return (EOPNOTSUPP);
341 1.1 augustss
342 1.1 augustss case DVACT_DEACTIVATE:
343 1.1 augustss rv = 0;
344 1.1 augustss for (i = 0; i < sc->sc_nrepid; i++)
345 1.1 augustss if (sc->sc_subdevs[i] != NULL)
346 1.1 augustss rv |= config_deactivate(
347 1.1 augustss &sc->sc_subdevs[i]->sc_dev);
348 1.1 augustss sc->sc_dying = 1;
349 1.1 augustss break;
350 1.14.2.1 skrll default:
351 1.14.2.1 skrll rv = 0;
352 1.14.2.1 skrll break;
353 1.1 augustss }
354 1.1 augustss return (rv);
355 1.1 augustss }
356 1.1 augustss
357 1.1 augustss USB_DETACH(uhidev)
358 1.1 augustss {
359 1.1 augustss USB_DETACH_START(uhidev, sc);
360 1.1 augustss int i, rv;
361 1.1 augustss
362 1.1 augustss DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
363 1.1 augustss
364 1.1 augustss sc->sc_dying = 1;
365 1.1 augustss if (sc->sc_intrpipe != NULL)
366 1.1 augustss usbd_abort_pipe(sc->sc_intrpipe);
367 1.1 augustss
368 1.1 augustss if (sc->sc_repdesc != NULL)
369 1.1 augustss free(sc->sc_repdesc, M_USBDEV);
370 1.1 augustss
371 1.1 augustss rv = 0;
372 1.1 augustss for (i = 0; i < sc->sc_nrepid; i++) {
373 1.1 augustss if (sc->sc_subdevs[i] != NULL) {
374 1.9 dan #if NRND > 0
375 1.9 dan rnd_detach_source(&sc->sc_subdevs[i]->rnd_source);
376 1.9 dan #endif
377 1.1 augustss rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
378 1.1 augustss sc->sc_subdevs[i] = NULL;
379 1.1 augustss }
380 1.1 augustss }
381 1.1 augustss
382 1.1 augustss usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
383 1.1 augustss USBDEV(sc->sc_dev));
384 1.1 augustss
385 1.1 augustss return (rv);
386 1.1 augustss }
387 1.1 augustss
388 1.1 augustss void
389 1.1 augustss uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
390 1.1 augustss {
391 1.1 augustss struct uhidev_softc *sc = addr;
392 1.1 augustss struct uhidev *scd;
393 1.1 augustss u_char *p;
394 1.1 augustss u_int rep;
395 1.1 augustss u_int32_t cc;
396 1.1 augustss
397 1.1 augustss usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
398 1.1 augustss
399 1.1 augustss #ifdef UHIDEV_DEBUG
400 1.1 augustss if (uhidevdebug > 5) {
401 1.1 augustss u_int32_t i;
402 1.6 augustss
403 1.1 augustss DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
404 1.1 augustss DPRINTF(("uhidev_intr: data ="));
405 1.1 augustss for (i = 0; i < cc; i++)
406 1.1 augustss DPRINTF((" %02x", sc->sc_ibuf[i]));
407 1.1 augustss DPRINTF(("\n"));
408 1.1 augustss }
409 1.1 augustss #endif
410 1.1 augustss
411 1.1 augustss if (status == USBD_CANCELLED)
412 1.1 augustss return;
413 1.1 augustss
414 1.1 augustss if (status != USBD_NORMAL_COMPLETION) {
415 1.3 augustss DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
416 1.3 augustss status));
417 1.1 augustss usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
418 1.1 augustss return;
419 1.1 augustss }
420 1.1 augustss
421 1.1 augustss p = sc->sc_ibuf;
422 1.1 augustss if (sc->sc_nrepid != 1)
423 1.1 augustss rep = *p++, cc--;
424 1.1 augustss else
425 1.1 augustss rep = 0;
426 1.1 augustss if (rep >= sc->sc_nrepid) {
427 1.1 augustss printf("uhidev_intr: bad repid %d\n", rep);
428 1.1 augustss return;
429 1.1 augustss }
430 1.1 augustss scd = sc->sc_subdevs[rep];
431 1.1 augustss DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
432 1.1 augustss rep, scd, scd ? scd->sc_state : 0));
433 1.1 augustss if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
434 1.1 augustss return;
435 1.14.2.4 skrll if (scd->sc_in_rep_size != cc) {
436 1.14.2.4 skrll printf("%s: bad input length %d != %d\n",
437 1.14.2.4 skrll USBDEVNAME(sc->sc_dev), scd->sc_in_rep_size, cc);
438 1.14.2.4 skrll return;
439 1.14.2.4 skrll }
440 1.9 dan #if NRND > 0
441 1.10 fair rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
442 1.2 augustss #endif
443 1.1 augustss scd->sc_intr(scd, p, cc);
444 1.1 augustss }
445 1.1 augustss
446 1.1 augustss void
447 1.1 augustss uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
448 1.1 augustss {
449 1.1 augustss *desc = sc->sc_repdesc;
450 1.1 augustss *size = sc->sc_repdesc_size;
451 1.1 augustss }
452 1.1 augustss
453 1.1 augustss int
454 1.1 augustss uhidev_open(struct uhidev *scd)
455 1.1 augustss {
456 1.1 augustss struct uhidev_softc *sc = scd->sc_parent;
457 1.1 augustss usbd_status err;
458 1.1 augustss
459 1.1 augustss DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
460 1.1 augustss scd->sc_state, sc->sc_refcnt));
461 1.1 augustss
462 1.1 augustss if (scd->sc_state & UHIDEV_OPEN)
463 1.1 augustss return (EBUSY);
464 1.1 augustss scd->sc_state |= UHIDEV_OPEN;
465 1.1 augustss if (sc->sc_refcnt++)
466 1.1 augustss return (0);
467 1.1 augustss
468 1.1 augustss if (sc->sc_isize == 0)
469 1.1 augustss return (0);
470 1.1 augustss
471 1.1 augustss sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
472 1.1 augustss
473 1.1 augustss /* Set up interrupt pipe. */
474 1.1 augustss DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
475 1.1 augustss sc->sc_ep_addr));
476 1.6 augustss err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
477 1.6 augustss USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
478 1.1 augustss sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
479 1.1 augustss if (err) {
480 1.1 augustss DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
481 1.1 augustss "error=%d\n",err));
482 1.1 augustss free(sc->sc_ibuf, M_USBDEV);
483 1.1 augustss scd->sc_state &= ~UHIDEV_OPEN;
484 1.1 augustss sc->sc_refcnt = 0;
485 1.1 augustss sc->sc_intrpipe = NULL;
486 1.1 augustss return (EIO);
487 1.1 augustss }
488 1.1 augustss return (0);
489 1.1 augustss }
490 1.1 augustss
491 1.1 augustss void
492 1.1 augustss uhidev_close(struct uhidev *scd)
493 1.1 augustss {
494 1.1 augustss struct uhidev_softc *sc = scd->sc_parent;
495 1.1 augustss
496 1.1 augustss if (!(scd->sc_state & UHIDEV_OPEN))
497 1.1 augustss return;
498 1.1 augustss scd->sc_state &= ~UHIDEV_OPEN;
499 1.1 augustss if (--sc->sc_refcnt)
500 1.1 augustss return;
501 1.1 augustss DPRINTF(("uhidev_close: close pipe\n"));
502 1.1 augustss
503 1.1 augustss /* Disable interrupts. */
504 1.1 augustss if (sc->sc_intrpipe != NULL) {
505 1.1 augustss usbd_abort_pipe(sc->sc_intrpipe);
506 1.1 augustss usbd_close_pipe(sc->sc_intrpipe);
507 1.1 augustss sc->sc_intrpipe = NULL;
508 1.1 augustss }
509 1.1 augustss
510 1.1 augustss if (sc->sc_ibuf != NULL) {
511 1.1 augustss free(sc->sc_ibuf, M_USBDEV);
512 1.1 augustss sc->sc_ibuf = NULL;
513 1.1 augustss }
514 1.1 augustss }
515 1.1 augustss
516 1.1 augustss usbd_status
517 1.1 augustss uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
518 1.1 augustss {
519 1.13 dsainty char *buf;
520 1.13 dsainty usbd_status retstat;
521 1.13 dsainty
522 1.13 dsainty if (scd->sc_report_id == 0)
523 1.13 dsainty return usbd_set_report(scd->sc_parent->sc_iface, type,
524 1.13 dsainty scd->sc_report_id, data, len);
525 1.13 dsainty
526 1.13 dsainty buf = malloc(len + 1, M_TEMP, M_WAITOK);
527 1.13 dsainty buf[0] = scd->sc_report_id;
528 1.13 dsainty memcpy(buf+1, data, len);
529 1.13 dsainty
530 1.13 dsainty retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
531 1.14.2.1 skrll scd->sc_report_id, buf, len + 1);
532 1.13 dsainty
533 1.13 dsainty free(buf, M_TEMP);
534 1.1 augustss
535 1.13 dsainty return retstat;
536 1.1 augustss }
537 1.1 augustss
538 1.1 augustss void
539 1.1 augustss uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
540 1.1 augustss {
541 1.1 augustss /* XXX */
542 1.1 augustss char buf[100];
543 1.1 augustss if (scd->sc_report_id) {
544 1.1 augustss buf[0] = scd->sc_report_id;
545 1.1 augustss memcpy(buf+1, data, len);
546 1.1 augustss len++;
547 1.1 augustss data = buf;
548 1.1 augustss }
549 1.1 augustss
550 1.6 augustss usbd_set_report_async(scd->sc_parent->sc_iface, type,
551 1.1 augustss scd->sc_report_id, data, len);
552 1.1 augustss }
553 1.1 augustss
554 1.1 augustss usbd_status
555 1.1 augustss uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
556 1.1 augustss {
557 1.6 augustss return usbd_get_report(scd->sc_parent->sc_iface, type,
558 1.1 augustss scd->sc_report_id, data, len);
559 1.1 augustss }
560