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