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