xref: /netbsd-src/sys/dev/usb/uep.c (revision 4b896b232495b7a9b8b94a1cf1e21873296d53b8)
1 /*	$NetBSD: uep.c,v 1.1 2004/05/24 23:48:36 tsarna Exp $	*/
2 
3 /*
4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Tyler C. Sarna (tsarna@netbsd.org).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	  This product includes software developed by the NetBSD
21  *	  Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: uep.c,v 1.1 2004/05/24 23:48:36 tsarna Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/device.h>
47 #include <sys/ioctl.h>
48 #include <sys/vnode.h>
49 
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usbdi_util.h>
53 #include <dev/usb/usbdevs.h>
54 #include <dev/usb/usb_quirks.h>
55 
56 #include <dev/wscons/wsconsio.h>
57 #include <dev/wscons/wsmousevar.h>
58 
59 struct uep_softc {
60 	USBBASEDEVICE sc_dev;
61 	usbd_device_handle sc_udev;	/* device */
62 	usbd_interface_handle sc_iface;	/* interface */
63 	int sc_iface_number;
64 
65 	int			sc_intr_number; /* interrupt number */
66 	usbd_pipe_handle	sc_intr_pipe;	/* interrupt pipe */
67 	u_char			*sc_ibuf;
68 	int			sc_isize;
69 
70 	device_ptr_t		sc_wsmousedev;	/* wsmouse device */
71 
72 	u_char sc_enabled;
73 	u_char sc_dying;
74 };
75 
76 Static void uep_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
77 
78 Static int	uep_enable(void *);
79 Static void	uep_disable(void *);
80 Static int	uep_ioctl(void *, u_long, caddr_t, int, usb_proc_ptr);
81 
82 const struct wsmouse_accessops uep_accessops = {
83 	uep_enable,
84 	uep_ioctl,
85 	uep_disable,
86 };
87 
88 USB_DECLARE_DRIVER(uep);
89 
90 USB_MATCH(uep)
91 {
92 	USB_MATCH_START(uep, uaa);
93 
94 	if (uaa->iface == NULL)
95 		return UMATCH_NONE;
96 
97 	if ((uaa->vendor == USB_VENDOR_EGALAX) && (
98 		(uaa->product == USB_PRODUCT_EGALAX_TPANEL)
99 		|| (uaa->product == USB_PRODUCT_EGALAX_TPANEL2)
100 	))
101 		return UMATCH_VENDOR_PRODUCT;
102 
103 	if ((uaa->vendor == USB_VENDOR_EGALAX2)
104 	&&  (uaa->product == USB_PRODUCT_EGALAX2_TPANEL))
105 		return UMATCH_VENDOR_PRODUCT;
106 
107 
108 	return UMATCH_NONE;
109 }
110 
111 USB_ATTACH(uep)
112 {
113 	USB_ATTACH_START(uep, sc, uaa);
114 	usbd_device_handle dev = uaa->device;
115 	usb_config_descriptor_t *cdesc;
116 	usb_interface_descriptor_t *id;
117 	usb_endpoint_descriptor_t *ed;
118 	struct wsmousedev_attach_args a;
119 	char devinfo[1024];
120 	usbd_status err;
121 	int i, found;
122 
123 	usbd_devinfo(dev, 0, devinfo, sizeof(devinfo));
124 
125 	USB_ATTACH_SETUP;
126 
127 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
128 
129 	sc->sc_udev = dev;
130 	sc->sc_intr_number = -1;
131 	sc->sc_intr_pipe = NULL;
132 	sc->sc_enabled = sc->sc_isize = 0;
133 
134 	/* Move the device into the configured state. */
135 	err = usbd_set_config_index(dev, 0, 1);
136 	if (err) {
137 		printf("\n%s: failed to set configuration, err=%s\n",
138 			USBDEVNAME(sc->sc_dev),  usbd_errstr(err));
139 		sc->sc_dying = 1;
140 		USB_ATTACH_ERROR_RETURN;
141 	}
142 
143 	/* get the config descriptor */
144 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
145 	if (cdesc == NULL) {
146 		printf("%s: failed to get configuration descriptor\n",
147 			USBDEVNAME(sc->sc_dev));
148 		sc->sc_dying = 1;
149 		USB_ATTACH_ERROR_RETURN;
150 	}
151 
152 	/* get the interface */
153 	err = usbd_device2interface_handle(dev, 0, &sc->sc_iface);
154 	if (err) {
155 		printf("\n%s: failed to get interface, err=%s\n",
156 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
157 		sc->sc_dying = 1;
158 		USB_ATTACH_ERROR_RETURN;
159 	}
160 
161 	/* Find the interrupt endpoint */
162 	id = usbd_get_interface_descriptor(sc->sc_iface);
163 	sc->sc_iface_number = id->bInterfaceNumber;
164 	found = 0;
165 
166 	for (i = 0; i < id->bNumEndpoints; i++) {
167 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
168 		if (ed == NULL) {
169 			printf("%s: no endpoint descriptor for %d\n",
170 				USBDEVNAME(sc->sc_dev), i);
171 			sc->sc_dying = 1;
172 			USB_ATTACH_ERROR_RETURN;
173 		}
174 
175 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
176 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
177 			sc->sc_intr_number = ed->bEndpointAddress;
178 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
179 		}
180 	}
181 
182 	if (sc->sc_intr_number== -1) {
183 		printf("%s: Could not find interrupt in\n",
184 			USBDEVNAME(sc->sc_dev));
185 		sc->sc_dying = 1;
186 		USB_ATTACH_ERROR_RETURN;
187 	}
188 
189 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
190 			   USBDEV(sc->sc_dev));
191 
192 	a.accessops = &uep_accessops;
193 	a.accesscookie = sc;
194 
195 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
196 
197 	USB_ATTACH_SUCCESS_RETURN;
198 }
199 
200 USB_DETACH(uep)
201 {
202 	USB_DETACH_START(uep, sc);
203 	int rv = 0;
204 
205 	if (sc->sc_intr_pipe != NULL) {
206 		usbd_abort_pipe(sc->sc_intr_pipe);
207 		usbd_close_pipe(sc->sc_intr_pipe);
208 		sc->sc_intr_pipe = NULL;
209 	}
210 
211 	sc->sc_dying = 1;
212 
213 	if (sc->sc_wsmousedev != NULL) {
214 		rv = config_detach(sc->sc_wsmousedev, flags);
215 		sc->sc_wsmousedev = NULL;
216 	}
217 
218 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
219 			   USBDEV(sc->sc_dev));
220 
221 	return rv;
222 }
223 
224 int
225 uep_activate(device_ptr_t self, enum devact act)
226 {
227 	struct uep_softc *sc = (struct uep_softc *)self;
228 	int rv = 0;
229 
230 	switch (act) {
231 	case DVACT_ACTIVATE:
232 		return EOPNOTSUPP;
233 
234 	case DVACT_DEACTIVATE:
235 		if (sc->sc_wsmousedev != NULL)
236 			rv = config_deactivate(sc->sc_wsmousedev);
237 		sc->sc_dying = 1;
238 		break;
239 	}
240 
241 	return rv;
242 }
243 
244 Static int
245 uep_enable(void *v)
246 {
247 	struct uep_softc *sc = v;
248 	int err;
249 
250 	if (sc->sc_dying)
251 		return EIO;
252 
253 	if (sc->sc_enabled)
254 		return EBUSY;
255 
256 	if (sc->sc_isize == 0)
257 		return 0;
258 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
259 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
260 		USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_ibuf,
261 		sc->sc_isize, uep_intr, USBD_DEFAULT_INTERVAL);
262 	if (err) {
263 		free(sc->sc_ibuf, M_USBDEV);
264 		sc->sc_intr_pipe = NULL;
265 		return EIO;
266 	}
267 
268 	sc->sc_enabled = 1;
269 
270 	return 0;
271 }
272 
273 Static void
274 uep_disable(void *v)
275 {
276 	struct uep_softc *sc = v;
277 
278 	if (!sc->sc_enabled) {
279 		printf("uep_disable: already disabled!\n");
280 		return;
281 	}
282 
283 	/* Disable interrupts. */
284 	if (sc->sc_intr_pipe != NULL) {
285 		usbd_abort_pipe(sc->sc_intr_pipe);
286 		usbd_close_pipe(sc->sc_intr_pipe);
287 		sc->sc_intr_pipe = NULL;
288 	}
289 
290 	if (sc->sc_ibuf != NULL) {
291 		free(sc->sc_ibuf, M_USBDEV);
292 		sc->sc_ibuf = NULL;
293 	}
294 
295 	sc->sc_enabled = 0;
296 }
297 
298 Static int
299 uep_ioctl(void *v, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
300 {
301 	switch (cmd) {
302 	case WSMOUSEIO_GTYPE:
303 		*(u_int *)data = WSMOUSE_TYPE_TPANEL;
304 		return (0);
305 	}
306 
307 	return EPASSTHROUGH;
308 }
309 
310 void
311 uep_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
312 {
313 	struct uep_softc *sc = addr;
314 	u_char *p = sc->sc_ibuf;
315 	u_int32_t len;
316 	int x = 0, y = 0, s;
317 
318 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
319 
320 	if (status == USBD_CANCELLED)
321 		return;
322 
323 	if (status != USBD_NORMAL_COMPLETION) {
324 		printf("%s: status %d\n", USBDEVNAME(sc->sc_dev), status);
325 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
326 		return;
327 	}
328 
329 	if (len != 5) {
330 #if 0
331 		printf("%s: bad input length %d != %d\n",
332 			USBDEVNAME(sc->sc_dev), len, sc->sc_isize);
333 #endif
334 		return;
335 	}
336 
337 	if ((p[0] & 0xFE) != 0x80) {
338 		printf("%s: bad input packet format\n", USBDEVNAME(sc->sc_dev));
339 		return;
340 	}
341 
342 	if (sc->sc_wsmousedev != NULL) {
343 		x = (p[3] << 7) | p[4];
344 		y = (p[1] << 7) | p[2];
345 
346 #if 0
347 		printf("%s: b=%d x=%d y=%d\n", USBDEVNAME(sc->sc_dev),
348 			p[0] & 0x01, x, y);
349 #endif
350 
351 		s = spltty();
352 		wsmouse_input(sc->sc_wsmousedev, p[0] & 0x01, x, y, 0,
353 			WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
354 		splx(s);
355 	}
356 }
357