uhidev.c revision 1.3.2.7 1 1.3.2.7 thorpej /* $NetBSD: uhidev.c,v 1.3.2.7 2003/01/03 17:08:17 thorpej 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 USB_ATTACH(uhidev)
100 1.3.2.2 nathanw {
101 1.3.2.2 nathanw USB_ATTACH_START(uhidev, sc, uaa);
102 1.3.2.2 nathanw usbd_interface_handle iface = uaa->iface;
103 1.3.2.2 nathanw usb_interface_descriptor_t *id;
104 1.3.2.2 nathanw usb_endpoint_descriptor_t *ed;
105 1.3.2.2 nathanw struct uhidev_attach_arg uha;
106 1.3.2.2 nathanw struct uhidev *dev;
107 1.3.2.2 nathanw int size, nrepid, repid, repsz;
108 1.3.2.2 nathanw int repsizes[256];
109 1.3.2.2 nathanw void *desc;
110 1.3.2.2 nathanw usbd_status err;
111 1.3.2.2 nathanw char devinfo[1024];
112 1.3.2.4 nathanw
113 1.3.2.2 nathanw sc->sc_udev = uaa->device;
114 1.3.2.2 nathanw sc->sc_iface = iface;
115 1.3.2.2 nathanw id = usbd_get_interface_descriptor(iface);
116 1.3.2.2 nathanw usbd_devinfo(uaa->device, 0, devinfo);
117 1.3.2.2 nathanw USB_ATTACH_SETUP;
118 1.3.2.2 nathanw printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
119 1.3.2.2 nathanw devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
120 1.3.2.2 nathanw
121 1.3.2.2 nathanw (void)usbd_set_idle(iface, 0, 0);
122 1.3.2.2 nathanw #if 0
123 1.3.2.2 nathanw
124 1.3.2.2 nathanw qflags = usbd_get_quirks(sc->sc_udev)->uq_flags;
125 1.3.2.2 nathanw if ((qflags & UQ_NO_SET_PROTO) == 0 &&
126 1.3.2.2 nathanw id->bInterfaceSubClass != UISUBCLASS_BOOT)
127 1.3.2.2 nathanw (void)usbd_set_protocol(iface, 1);
128 1.3.2.2 nathanw #endif
129 1.3.2.2 nathanw
130 1.3.2.2 nathanw ed = usbd_interface2endpoint_descriptor(iface, 0);
131 1.3.2.2 nathanw if (ed == NULL) {
132 1.3.2.2 nathanw printf("%s: could not read endpoint descriptor\n",
133 1.3.2.2 nathanw USBDEVNAME(sc->sc_dev));
134 1.3.2.2 nathanw sc->sc_dying = 1;
135 1.3.2.2 nathanw USB_ATTACH_ERROR_RETURN;
136 1.3.2.2 nathanw }
137 1.3.2.2 nathanw
138 1.3.2.2 nathanw DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
139 1.3.2.2 nathanw "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
140 1.3.2.2 nathanw " bInterval=%d\n",
141 1.3.2.4 nathanw ed->bLength, ed->bDescriptorType,
142 1.3.2.2 nathanw ed->bEndpointAddress & UE_ADDR,
143 1.3.2.2 nathanw UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
144 1.3.2.2 nathanw ed->bmAttributes & UE_XFERTYPE,
145 1.3.2.2 nathanw UGETW(ed->wMaxPacketSize), ed->bInterval));
146 1.3.2.2 nathanw
147 1.3.2.2 nathanw if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
148 1.3.2.2 nathanw (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
149 1.3.2.2 nathanw printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
150 1.3.2.2 nathanw sc->sc_dying = 1;
151 1.3.2.2 nathanw USB_ATTACH_ERROR_RETURN;
152 1.3.2.2 nathanw }
153 1.3.2.2 nathanw
154 1.3.2.2 nathanw sc->sc_ep_addr = ed->bEndpointAddress;
155 1.3.2.2 nathanw
156 1.3.2.2 nathanw /* XXX need to extend this */
157 1.3.2.2 nathanw if (uaa->vendor == USB_VENDOR_WACOM &&
158 1.3.2.2 nathanw uaa->product == USB_PRODUCT_WACOM_GRAPHIRE /* &&
159 1.3.2.2 nathanw uaa->revision == 0x???? */) { /* XXX should use revision */
160 1.3.2.2 nathanw /* The report descriptor for the Wacom Graphire is broken. */
161 1.3.2.2 nathanw size = sizeof uhid_graphire_report_descr;
162 1.3.2.2 nathanw desc = malloc(size, M_USBDEV, M_NOWAIT);
163 1.3.2.2 nathanw if (desc == NULL)
164 1.3.2.2 nathanw err = USBD_NOMEM;
165 1.3.2.2 nathanw else {
166 1.3.2.2 nathanw err = USBD_NORMAL_COMPLETION;
167 1.3.2.2 nathanw memcpy(desc, uhid_graphire_report_descr, size);
168 1.3.2.2 nathanw }
169 1.3.2.2 nathanw } else {
170 1.3.2.2 nathanw desc = NULL;
171 1.3.2.2 nathanw err = usbd_read_report_desc(uaa->iface, &desc, &size, M_USBDEV);
172 1.3.2.2 nathanw }
173 1.3.2.2 nathanw if (err) {
174 1.3.2.2 nathanw printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
175 1.3.2.2 nathanw sc->sc_dying = 1;
176 1.3.2.2 nathanw USB_ATTACH_ERROR_RETURN;
177 1.3.2.2 nathanw }
178 1.3.2.4 nathanw
179 1.3.2.2 nathanw sc->sc_repdesc = desc;
180 1.3.2.2 nathanw sc->sc_repdesc_size = size;
181 1.3.2.2 nathanw
182 1.3.2.2 nathanw uha.uaa = uaa;
183 1.3.2.2 nathanw nrepid = uhidev_maxrepid(desc, size);
184 1.3.2.2 nathanw if (nrepid < 0)
185 1.3.2.2 nathanw USB_ATTACH_SUCCESS_RETURN;
186 1.3.2.2 nathanw if (nrepid > 0)
187 1.3.2.2 nathanw printf("%s: %d report ids\n", USBDEVNAME(sc->sc_dev), nrepid);
188 1.3.2.2 nathanw nrepid++;
189 1.3.2.2 nathanw sc->sc_subdevs = malloc(nrepid * sizeof(device_ptr_t),
190 1.3.2.3 nathanw M_USBDEV, M_NOWAIT | M_ZERO);
191 1.3.2.2 nathanw if (sc->sc_subdevs == NULL) {
192 1.3.2.2 nathanw printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
193 1.3.2.2 nathanw USB_ATTACH_ERROR_RETURN;
194 1.3.2.2 nathanw }
195 1.3.2.2 nathanw sc->sc_nrepid = nrepid;
196 1.3.2.2 nathanw sc->sc_isize = 0;
197 1.3.2.2 nathanw
198 1.3.2.2 nathanw usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
199 1.3.2.2 nathanw USBDEV(sc->sc_dev));
200 1.3.2.2 nathanw
201 1.3.2.2 nathanw for (repid = 0; repid < nrepid; repid++) {
202 1.3.2.2 nathanw repsz = hid_report_size(desc, size, hid_input, repid);
203 1.3.2.2 nathanw DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
204 1.3.2.2 nathanw repsizes[repid] = repsz;
205 1.3.2.2 nathanw if (repsz > 0) {
206 1.3.2.2 nathanw if (repsz > sc->sc_isize)
207 1.3.2.2 nathanw sc->sc_isize = repsz;
208 1.3.2.2 nathanw }
209 1.3.2.2 nathanw }
210 1.3.2.2 nathanw sc->sc_isize += nrepid != 1; /* space for report ID */
211 1.3.2.2 nathanw DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
212 1.3.2.2 nathanw
213 1.3.2.2 nathanw uha.parent = sc;
214 1.3.2.2 nathanw for (repid = 0; repid < nrepid; repid++) {
215 1.3.2.2 nathanw DPRINTF(("uhidev_match: try repid=%d\n", repid));
216 1.3.2.2 nathanw if (hid_report_size(desc, size, hid_input, repid) == 0 &&
217 1.3.2.2 nathanw hid_report_size(desc, size, hid_output, repid) == 0 &&
218 1.3.2.2 nathanw hid_report_size(desc, size, hid_feature, repid) == 0) {
219 1.3.2.3 nathanw ; /* already NULL in sc->sc_subdevs[repid] */
220 1.3.2.2 nathanw } else {
221 1.3.2.2 nathanw uha.reportid = repid;
222 1.3.2.2 nathanw dev = (struct uhidev *)config_found_sm(self, &uha,
223 1.3.2.2 nathanw uhidevprint, uhidevsubmatch);
224 1.3.2.2 nathanw sc->sc_subdevs[repid] = dev;
225 1.3.2.2 nathanw if (dev != NULL) {
226 1.3.2.3 nathanw dev->sc_in_rep_size = repsizes[repid];
227 1.3.2.3 nathanw #ifdef DIAGNOSTIC
228 1.3.2.2 nathanw DPRINTF(("uhidev_match: repid=%d dev=%p\n",
229 1.3.2.2 nathanw repid, dev));
230 1.3.2.2 nathanw if (dev->sc_intr == NULL) {
231 1.3.2.2 nathanw printf("%s: sc_intr == NULL\n",
232 1.3.2.2 nathanw USBDEVNAME(sc->sc_dev));
233 1.3.2.2 nathanw USB_ATTACH_ERROR_RETURN;
234 1.3.2.2 nathanw }
235 1.3.2.2 nathanw #endif
236 1.3.2.5 nathanw #if NRND > 0
237 1.3.2.5 nathanw rnd_attach_source(&dev->rnd_source,
238 1.3.2.5 nathanw USBDEVNAME(dev->sc_dev),
239 1.3.2.5 nathanw RND_TYPE_TTY, 0);
240 1.3.2.5 nathanw #endif
241 1.3.2.3 nathanw }
242 1.3.2.2 nathanw }
243 1.3.2.2 nathanw }
244 1.3.2.2 nathanw
245 1.3.2.2 nathanw USB_ATTACH_SUCCESS_RETURN;
246 1.3.2.2 nathanw }
247 1.3.2.2 nathanw
248 1.3.2.2 nathanw int
249 1.3.2.2 nathanw uhidev_maxrepid(void *buf, int len)
250 1.3.2.2 nathanw {
251 1.3.2.2 nathanw struct hid_data *d;
252 1.3.2.2 nathanw struct hid_item h;
253 1.3.2.2 nathanw int maxid;
254 1.3.2.2 nathanw
255 1.3.2.2 nathanw maxid = -1;
256 1.3.2.2 nathanw h.report_ID = 0;
257 1.3.2.2 nathanw for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
258 1.3.2.2 nathanw if (h.report_ID > maxid)
259 1.3.2.2 nathanw maxid = h.report_ID;
260 1.3.2.2 nathanw hid_end_parse(d);
261 1.3.2.2 nathanw return (maxid);
262 1.3.2.2 nathanw }
263 1.3.2.2 nathanw
264 1.3.2.2 nathanw int
265 1.3.2.2 nathanw uhidevprint(void *aux, const char *pnp)
266 1.3.2.2 nathanw {
267 1.3.2.2 nathanw struct uhidev_attach_arg *uha = aux;
268 1.3.2.2 nathanw
269 1.3.2.2 nathanw if (pnp)
270 1.3.2.7 thorpej aprint_normal("uhid at %s", pnp);
271 1.3.2.2 nathanw if (uha->reportid != 0)
272 1.3.2.7 thorpej aprint_normal(" reportid %d", uha->reportid);
273 1.3.2.2 nathanw return (UNCONF);
274 1.3.2.2 nathanw }
275 1.3.2.2 nathanw
276 1.3.2.2 nathanw int
277 1.3.2.2 nathanw uhidevsubmatch(struct device *parent, struct cfdata *cf, void *aux)
278 1.3.2.2 nathanw {
279 1.3.2.2 nathanw struct uhidev_attach_arg *uha = aux;
280 1.3.2.2 nathanw
281 1.3.2.2 nathanw if (cf->uhidevcf_reportid != UHIDEV_UNK_REPORTID &&
282 1.3.2.2 nathanw cf->uhidevcf_reportid != uha->reportid)
283 1.3.2.2 nathanw return (0);
284 1.3.2.2 nathanw if (cf->uhidevcf_reportid == uha->reportid)
285 1.3.2.2 nathanw uha->matchlvl = UMATCH_VENDOR_PRODUCT;
286 1.3.2.2 nathanw else
287 1.3.2.2 nathanw uha->matchlvl = 0;
288 1.3.2.5 nathanw return (config_match(parent, cf, aux));
289 1.3.2.2 nathanw }
290 1.3.2.2 nathanw
291 1.3.2.2 nathanw int
292 1.3.2.2 nathanw uhidev_activate(device_ptr_t self, enum devact act)
293 1.3.2.2 nathanw {
294 1.3.2.2 nathanw struct uhidev_softc *sc = (struct uhidev_softc *)self;
295 1.3.2.2 nathanw int i, rv;
296 1.3.2.2 nathanw
297 1.3.2.2 nathanw switch (act) {
298 1.3.2.2 nathanw case DVACT_ACTIVATE:
299 1.3.2.2 nathanw return (EOPNOTSUPP);
300 1.3.2.2 nathanw
301 1.3.2.2 nathanw case DVACT_DEACTIVATE:
302 1.3.2.2 nathanw rv = 0;
303 1.3.2.2 nathanw for (i = 0; i < sc->sc_nrepid; i++)
304 1.3.2.2 nathanw if (sc->sc_subdevs[i] != NULL)
305 1.3.2.2 nathanw rv |= config_deactivate(
306 1.3.2.2 nathanw &sc->sc_subdevs[i]->sc_dev);
307 1.3.2.2 nathanw sc->sc_dying = 1;
308 1.3.2.2 nathanw break;
309 1.3.2.2 nathanw }
310 1.3.2.2 nathanw return (rv);
311 1.3.2.2 nathanw }
312 1.3.2.2 nathanw
313 1.3.2.2 nathanw USB_DETACH(uhidev)
314 1.3.2.2 nathanw {
315 1.3.2.2 nathanw USB_DETACH_START(uhidev, sc);
316 1.3.2.2 nathanw int i, rv;
317 1.3.2.2 nathanw
318 1.3.2.2 nathanw DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
319 1.3.2.2 nathanw
320 1.3.2.2 nathanw sc->sc_dying = 1;
321 1.3.2.2 nathanw if (sc->sc_intrpipe != NULL)
322 1.3.2.2 nathanw usbd_abort_pipe(sc->sc_intrpipe);
323 1.3.2.2 nathanw
324 1.3.2.2 nathanw if (sc->sc_repdesc != NULL)
325 1.3.2.2 nathanw free(sc->sc_repdesc, M_USBDEV);
326 1.3.2.2 nathanw
327 1.3.2.2 nathanw rv = 0;
328 1.3.2.2 nathanw for (i = 0; i < sc->sc_nrepid; i++) {
329 1.3.2.2 nathanw if (sc->sc_subdevs[i] != NULL) {
330 1.3.2.5 nathanw #if NRND > 0
331 1.3.2.5 nathanw rnd_detach_source(&sc->sc_subdevs[i]->rnd_source);
332 1.3.2.5 nathanw #endif
333 1.3.2.2 nathanw rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
334 1.3.2.2 nathanw sc->sc_subdevs[i] = NULL;
335 1.3.2.2 nathanw }
336 1.3.2.2 nathanw }
337 1.3.2.2 nathanw
338 1.3.2.2 nathanw usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
339 1.3.2.2 nathanw USBDEV(sc->sc_dev));
340 1.3.2.2 nathanw
341 1.3.2.2 nathanw return (rv);
342 1.3.2.2 nathanw }
343 1.3.2.2 nathanw
344 1.3.2.2 nathanw void
345 1.3.2.2 nathanw uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
346 1.3.2.2 nathanw {
347 1.3.2.2 nathanw struct uhidev_softc *sc = addr;
348 1.3.2.2 nathanw struct uhidev *scd;
349 1.3.2.2 nathanw u_char *p;
350 1.3.2.2 nathanw u_int rep;
351 1.3.2.2 nathanw u_int32_t cc;
352 1.3.2.2 nathanw
353 1.3.2.2 nathanw usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
354 1.3.2.2 nathanw
355 1.3.2.2 nathanw #ifdef UHIDEV_DEBUG
356 1.3.2.2 nathanw if (uhidevdebug > 5) {
357 1.3.2.2 nathanw u_int32_t i;
358 1.3.2.4 nathanw
359 1.3.2.2 nathanw DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
360 1.3.2.2 nathanw DPRINTF(("uhidev_intr: data ="));
361 1.3.2.2 nathanw for (i = 0; i < cc; i++)
362 1.3.2.2 nathanw DPRINTF((" %02x", sc->sc_ibuf[i]));
363 1.3.2.2 nathanw DPRINTF(("\n"));
364 1.3.2.2 nathanw }
365 1.3.2.2 nathanw #endif
366 1.3.2.2 nathanw
367 1.3.2.2 nathanw if (status == USBD_CANCELLED)
368 1.3.2.2 nathanw return;
369 1.3.2.2 nathanw
370 1.3.2.2 nathanw if (status != USBD_NORMAL_COMPLETION) {
371 1.3.2.2 nathanw DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
372 1.3.2.2 nathanw status));
373 1.3.2.2 nathanw usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
374 1.3.2.2 nathanw return;
375 1.3.2.2 nathanw }
376 1.3.2.2 nathanw
377 1.3.2.2 nathanw p = sc->sc_ibuf;
378 1.3.2.2 nathanw if (sc->sc_nrepid != 1)
379 1.3.2.2 nathanw rep = *p++, cc--;
380 1.3.2.2 nathanw else
381 1.3.2.2 nathanw rep = 0;
382 1.3.2.2 nathanw if (rep >= sc->sc_nrepid) {
383 1.3.2.2 nathanw printf("uhidev_intr: bad repid %d\n", rep);
384 1.3.2.2 nathanw return;
385 1.3.2.2 nathanw }
386 1.3.2.2 nathanw scd = sc->sc_subdevs[rep];
387 1.3.2.2 nathanw DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
388 1.3.2.2 nathanw rep, scd, scd ? scd->sc_state : 0));
389 1.3.2.2 nathanw if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
390 1.3.2.2 nathanw return;
391 1.3.2.2 nathanw #ifdef DIAGNOSTIC
392 1.3.2.2 nathanw if (scd->sc_in_rep_size != cc)
393 1.3.2.2 nathanw printf("%s: bad input length %d != %d\n",USBDEVNAME(sc->sc_dev),
394 1.3.2.2 nathanw scd->sc_in_rep_size, cc);
395 1.3.2.2 nathanw #endif
396 1.3.2.5 nathanw #if NRND > 0
397 1.3.2.5 nathanw rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
398 1.3.2.5 nathanw #endif
399 1.3.2.2 nathanw scd->sc_intr(scd, p, cc);
400 1.3.2.2 nathanw }
401 1.3.2.2 nathanw
402 1.3.2.2 nathanw void
403 1.3.2.2 nathanw uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
404 1.3.2.2 nathanw {
405 1.3.2.2 nathanw *desc = sc->sc_repdesc;
406 1.3.2.2 nathanw *size = sc->sc_repdesc_size;
407 1.3.2.2 nathanw }
408 1.3.2.2 nathanw
409 1.3.2.2 nathanw int
410 1.3.2.2 nathanw uhidev_open(struct uhidev *scd)
411 1.3.2.2 nathanw {
412 1.3.2.2 nathanw struct uhidev_softc *sc = scd->sc_parent;
413 1.3.2.2 nathanw usbd_status err;
414 1.3.2.2 nathanw
415 1.3.2.2 nathanw DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
416 1.3.2.2 nathanw scd->sc_state, sc->sc_refcnt));
417 1.3.2.2 nathanw
418 1.3.2.2 nathanw if (scd->sc_state & UHIDEV_OPEN)
419 1.3.2.2 nathanw return (EBUSY);
420 1.3.2.2 nathanw scd->sc_state |= UHIDEV_OPEN;
421 1.3.2.2 nathanw if (sc->sc_refcnt++)
422 1.3.2.2 nathanw return (0);
423 1.3.2.2 nathanw
424 1.3.2.2 nathanw if (sc->sc_isize == 0)
425 1.3.2.2 nathanw return (0);
426 1.3.2.2 nathanw
427 1.3.2.2 nathanw sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
428 1.3.2.2 nathanw
429 1.3.2.2 nathanw /* Set up interrupt pipe. */
430 1.3.2.2 nathanw DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
431 1.3.2.2 nathanw sc->sc_ep_addr));
432 1.3.2.4 nathanw err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
433 1.3.2.4 nathanw USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
434 1.3.2.2 nathanw sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
435 1.3.2.2 nathanw if (err) {
436 1.3.2.2 nathanw DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
437 1.3.2.2 nathanw "error=%d\n",err));
438 1.3.2.2 nathanw free(sc->sc_ibuf, M_USBDEV);
439 1.3.2.2 nathanw scd->sc_state &= ~UHIDEV_OPEN;
440 1.3.2.2 nathanw sc->sc_refcnt = 0;
441 1.3.2.2 nathanw sc->sc_intrpipe = NULL;
442 1.3.2.2 nathanw return (EIO);
443 1.3.2.2 nathanw }
444 1.3.2.2 nathanw return (0);
445 1.3.2.2 nathanw }
446 1.3.2.2 nathanw
447 1.3.2.2 nathanw void
448 1.3.2.2 nathanw uhidev_close(struct uhidev *scd)
449 1.3.2.2 nathanw {
450 1.3.2.2 nathanw struct uhidev_softc *sc = scd->sc_parent;
451 1.3.2.2 nathanw
452 1.3.2.2 nathanw if (!(scd->sc_state & UHIDEV_OPEN))
453 1.3.2.2 nathanw return;
454 1.3.2.2 nathanw scd->sc_state &= ~UHIDEV_OPEN;
455 1.3.2.2 nathanw if (--sc->sc_refcnt)
456 1.3.2.2 nathanw return;
457 1.3.2.2 nathanw DPRINTF(("uhidev_close: close pipe\n"));
458 1.3.2.2 nathanw
459 1.3.2.2 nathanw /* Disable interrupts. */
460 1.3.2.2 nathanw if (sc->sc_intrpipe != NULL) {
461 1.3.2.2 nathanw usbd_abort_pipe(sc->sc_intrpipe);
462 1.3.2.2 nathanw usbd_close_pipe(sc->sc_intrpipe);
463 1.3.2.2 nathanw sc->sc_intrpipe = NULL;
464 1.3.2.2 nathanw }
465 1.3.2.2 nathanw
466 1.3.2.2 nathanw if (sc->sc_ibuf != NULL) {
467 1.3.2.2 nathanw free(sc->sc_ibuf, M_USBDEV);
468 1.3.2.2 nathanw sc->sc_ibuf = NULL;
469 1.3.2.2 nathanw }
470 1.3.2.2 nathanw }
471 1.3.2.2 nathanw
472 1.3.2.2 nathanw usbd_status
473 1.3.2.2 nathanw uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
474 1.3.2.2 nathanw {
475 1.3.2.7 thorpej char *buf;
476 1.3.2.7 thorpej usbd_status retstat;
477 1.3.2.2 nathanw
478 1.3.2.7 thorpej if (scd->sc_report_id == 0)
479 1.3.2.7 thorpej return usbd_set_report(scd->sc_parent->sc_iface, type,
480 1.3.2.7 thorpej scd->sc_report_id, data, len);
481 1.3.2.7 thorpej
482 1.3.2.7 thorpej buf = malloc(len + 1, M_TEMP, M_WAITOK);
483 1.3.2.7 thorpej buf[0] = scd->sc_report_id;
484 1.3.2.7 thorpej memcpy(buf+1, data, len);
485 1.3.2.7 thorpej
486 1.3.2.7 thorpej retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
487 1.3.2.7 thorpej scd->sc_report_id, data, len + 1);
488 1.3.2.7 thorpej
489 1.3.2.7 thorpej free(buf, M_TEMP);
490 1.3.2.7 thorpej
491 1.3.2.7 thorpej return retstat;
492 1.3.2.2 nathanw }
493 1.3.2.2 nathanw
494 1.3.2.2 nathanw void
495 1.3.2.2 nathanw uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
496 1.3.2.2 nathanw {
497 1.3.2.2 nathanw /* XXX */
498 1.3.2.2 nathanw char buf[100];
499 1.3.2.2 nathanw if (scd->sc_report_id) {
500 1.3.2.2 nathanw buf[0] = scd->sc_report_id;
501 1.3.2.2 nathanw memcpy(buf+1, data, len);
502 1.3.2.2 nathanw len++;
503 1.3.2.2 nathanw data = buf;
504 1.3.2.2 nathanw }
505 1.3.2.2 nathanw
506 1.3.2.4 nathanw usbd_set_report_async(scd->sc_parent->sc_iface, type,
507 1.3.2.2 nathanw scd->sc_report_id, data, len);
508 1.3.2.2 nathanw }
509 1.3.2.2 nathanw
510 1.3.2.2 nathanw usbd_status
511 1.3.2.2 nathanw uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
512 1.3.2.2 nathanw {
513 1.3.2.4 nathanw return usbd_get_report(scd->sc_parent->sc_iface, type,
514 1.3.2.2 nathanw scd->sc_report_id, data, len);
515 1.3.2.2 nathanw }
516