Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.1
      1 /*	$NetBSD: ums.c,v 1.1 1998/07/12 19:52:00 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Author: Lennart Augustsson <augustss (at) carlstedt.se>
      8  *         Carlstedt Research & Technology
      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 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/malloc.h>
     44 #include <sys/device.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/tty.h>
     47 #include <sys/file.h>
     48 #include <sys/select.h>
     49 #include <sys/proc.h>
     50 #include <sys/vnode.h>
     51 #include <sys/device.h>
     52 #include <sys/poll.h>
     53 
     54 #include <machine/mouse.h>
     55 
     56 #include <dev/usb/usb.h>
     57 #include <dev/usb/usbhid.h>
     58 
     59 #include <dev/usb/usbdi.h>
     60 #include <dev/usb/usbdi_util.h>
     61 #include <dev/usb/usbdevs.h>
     62 #include <dev/usb/usb_quirks.h>
     63 #include <dev/usb/hid.h>
     64 
     65 #ifdef USB_DEBUG
     66 #define DPRINTF(x)	if (umsdebug) printf x
     67 #define DPRINTFN(n,x)	if (umsdebug>(n)) printf x
     68 int	umsdebug = 0;
     69 #else
     70 #define DPRINTF(x)
     71 #define DPRINTFN(n,x)
     72 #endif
     73 
     74 #define PS2LBUTMASK 0x01
     75 #define PS2RBUTMASK 0x02
     76 #define PS2MBUTMASK 0x04
     77 #define PS2BUTMASK 0x0f
     78 
     79 struct ums_softc {
     80 	struct device sc_dev;		/* base device */
     81 	usbd_interface_handle sc_iface;	/* interface */
     82 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
     83 	int sc_ep_addr;
     84 
     85 	u_char *sc_ibuf;
     86 	u_int8_t sc_iid;
     87 	int sc_isize;
     88 	struct hid_location sc_loc_x, sc_loc_y,
     89 		sc_loc_btn1, sc_loc_btn2, sc_loc_btn3;
     90 
     91 	struct clist sc_q;
     92 	struct selinfo sc_rsel;
     93 	u_char sc_state;	/* mouse driver state */
     94 #define	UMS_OPEN	0x01	/* device is open */
     95 #define	UMS_ASLP	0x02	/* waiting for mouse data */
     96 #define UMS_NEEDCLEAR	0x04	/* needs clearing endpoint stall */
     97 	u_char sc_status;	/* mouse button status */
     98 	int sc_x, sc_y;		/* accumulated motion in the X,Y axis */
     99 	int sc_disconnected;	/* device is gone */
    100 };
    101 
    102 #define	UMSUNIT(dev)	(minor(dev))
    103 #define	UMS_CHUNK	128	/* chunk size for read */
    104 #define	UMS_BSIZE	1020	/* buffer size */
    105 
    106 int ums_match __P((struct device *, struct cfdata *, void *));
    107 void ums_attach __P((struct device *, struct device *, void *));
    108 
    109 int umsopen __P((dev_t, int, int, struct proc *));
    110 int umsclose __P((dev_t, int, int, struct proc *p));
    111 int umsread __P((dev_t, struct uio *uio, int));
    112 int umsioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
    113 int umspoll __P((dev_t, int, struct proc *));
    114 void ums_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
    115 void ums_disco __P((void *));
    116 
    117 extern struct cfdriver ums_cd;
    118 
    119 struct cfattach ums_ca = {
    120 	sizeof(struct ums_softc), ums_match, ums_attach
    121 };
    122 
    123 int
    124 ums_match(parent, match, aux)
    125 	struct device *parent;
    126 	struct cfdata *match;
    127 	void *aux;
    128 {
    129 	struct usb_attach_arg *uaa = aux;
    130 	usb_interface_descriptor_t *id;
    131 	int size, ret;
    132 	void *desc;
    133 	usbd_status r;
    134 
    135 	if (!uaa->iface)
    136 		return (UMATCH_NONE);
    137 	id = usbd_get_interface_descriptor(uaa->iface);
    138 	if (id->bInterfaceClass != UCLASS_HID)
    139 		return (UMATCH_NONE);
    140 
    141 	r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
    142 	if (r != USBD_NORMAL_COMPLETION)
    143 		return (UMATCH_NONE);
    144 
    145 	if (hid_is_collection(desc, size,
    146 			      HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
    147 		ret = UMATCH_IFACECLASS;
    148 	else
    149 		ret = UMATCH_NONE;
    150 
    151 	free(desc, M_TEMP);
    152 	return (ret);
    153 }
    154 
    155 void
    156 ums_attach(parent, self, aux)
    157 	struct device *parent;
    158 	struct device *self;
    159 	void *aux;
    160 {
    161 	struct ums_softc *sc = (struct ums_softc *)self;
    162 	struct usb_attach_arg *uaa = aux;
    163 	usbd_interface_handle iface = uaa->iface;
    164 	usb_interface_descriptor_t *id;
    165 	usb_endpoint_descriptor_t *ed;
    166 	int size;
    167 	void *desc;
    168 	usbd_status r;
    169 	char devinfo[1024];
    170 
    171 	sc->sc_disconnected = 1;
    172 	sc->sc_iface = iface;
    173 	id = usbd_get_interface_descriptor(iface);
    174 	usbd_devinfo(uaa->device, 0, devinfo);
    175 	printf(": %s (interface class %d/%d)\n", devinfo,
    176 	       id->bInterfaceClass, id->bInterfaceSubClass);
    177 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    178 	if (!ed) {
    179 		printf("%s: could not read endpoint descriptor\n",
    180 		       sc->sc_dev.dv_xname);
    181 		return;
    182 	}
    183 
    184 	DPRINTFN(10,("ums_attach: \
    185 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
    186 	       ed->bLength, ed->bDescriptorType, ed->bEndpointAddress & UE_ADDR,
    187 	       ed->bEndpointAddress & UE_IN ? "in" : "out",
    188 	       ed->bmAttributes & UE_XFERTYPE,
    189 	       UGETW(ed->wMaxPacketSize), ed->bInterval));
    190 
    191 	if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
    192 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    193 		printf("%s: unexpected endpoint\n",
    194 		       sc->sc_dev.dv_xname);
    195 		return;
    196 	}
    197 
    198 	r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
    199 	if (r != USBD_NORMAL_COMPLETION)
    200 		return;
    201 	if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
    202 		       hid_input, &sc->sc_loc_x) == 0) {
    203 		printf("%s: mouse has no X report\n", sc->sc_dev.dv_xname);
    204 		return;
    205 	}
    206 	if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
    207 		       hid_input, &sc->sc_loc_y) == 0) {
    208 		printf("%s: mouse has no Y report\n", sc->sc_dev.dv_xname);
    209 		return;
    210 	}
    211 	hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 1),
    212 		   hid_input, &sc->sc_loc_btn1);
    213 	hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 2),
    214 		   hid_input, &sc->sc_loc_btn2);
    215 	hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 3),
    216 		   hid_input, &sc->sc_loc_btn3);
    217 	DPRINTF(("ums_attach: sc=%p\n", sc));
    218 	DPRINTF(("ums_attach: X  %d/%d\n",
    219 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    220 	DPRINTF(("ums_attach: Y  %d/%d\n",
    221 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    222 	DPRINTF(("ums_attach: B1 %d/%d\n",
    223 		 sc->sc_loc_btn1.pos,sc->sc_loc_btn1.size));
    224 	DPRINTF(("ums_attach: B2 %d/%d\n",
    225 		 sc->sc_loc_btn2.pos,sc->sc_loc_btn2.size));
    226 	DPRINTF(("ums_attach: B3 %d/%d\n",
    227 		 sc->sc_loc_btn3.pos,sc->sc_loc_btn3.size));
    228 
    229 	sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
    230 	DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
    231 	sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_NOWAIT);
    232 	if (!sc->sc_ibuf)
    233 		return;
    234 
    235 	sc->sc_ep_addr = ed->bEndpointAddress;
    236 	sc->sc_disconnected = 0;
    237 	free(desc, M_TEMP);
    238 }
    239 
    240 void
    241 ums_disco(p)
    242 	void *p;
    243 {
    244 	struct ums_softc *sc = p;
    245 	sc->sc_disconnected = 1;
    246 }
    247 
    248 void
    249 ums_intr(reqh, addr, status)
    250 	usbd_request_handle reqh;
    251 	usbd_private_handle addr;
    252 	usbd_status status;
    253 {
    254 	struct ums_softc *sc = addr;
    255 	u_char *ibuf;
    256 	char dx, dy;
    257 	u_char buttons, changed;
    258 	u_char buffer[5];
    259 
    260 	DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
    261 	DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
    262 		     sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
    263 
    264 	if (status == USBD_CANCELLED)
    265 		return;
    266 
    267 	if (status != USBD_NORMAL_COMPLETION) {
    268 		DPRINTF(("ums_intr: status=%d\n", status));
    269 		sc->sc_state |= UMS_NEEDCLEAR;
    270 		return;
    271 	}
    272 
    273 	ibuf = sc->sc_ibuf;
    274 	if (sc->sc_iid) {
    275 		if (*ibuf++ != sc->sc_iid)
    276 			return;
    277 	}
    278 	DPRINTF(("ums_attach: X  %d/%d\n",
    279 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    280 	dx =  hid_get_data(ibuf, &sc->sc_loc_x);
    281 	dy = -hid_get_data(ibuf, &sc->sc_loc_y);
    282 	buttons = 0;
    283 	if (hid_get_data(ibuf, &sc->sc_loc_btn1)) buttons |= PS2LBUTMASK;
    284 	if (hid_get_data(ibuf, &sc->sc_loc_btn2)) buttons |= PS2RBUTMASK;
    285 	if (hid_get_data(ibuf, &sc->sc_loc_btn3)) buttons |= PS2MBUTMASK;
    286 
    287 	buttons = ((buttons & PS2LBUTMASK) << 2) |
    288 		  ((buttons & (PS2RBUTMASK | PS2MBUTMASK)) >> 1);
    289 	changed = ((buttons ^ sc->sc_status) & BUTSTATMASK) << 3;
    290 	sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    291 
    292 	if (dx || dy || changed) {
    293 		/* Update accumulated movements. */
    294 		sc->sc_x += dx;
    295 		sc->sc_y += dy;
    296 
    297 		/* Add this event to the queue. */
    298 		buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
    299 		buffer[1] = dx;
    300 		buffer[2] = dy;
    301 		buffer[3] = buffer[4] = 0;
    302 		(void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
    303 
    304 		if (sc->sc_state & UMS_ASLP) {
    305 			sc->sc_state &= ~UMS_ASLP;
    306 			DPRINTFN(5, ("ums_intr: waking %p\n", sc));
    307 			wakeup((caddr_t)sc);
    308 		}
    309 		selwakeup(&sc->sc_rsel);
    310 	}
    311 }
    312 
    313 int
    314 umsopen(dev, flag, mode, p)
    315 	dev_t dev;
    316 	int flag;
    317 	int mode;
    318 	struct proc *p;
    319 {
    320 	int unit = UMSUNIT(dev);
    321 	struct ums_softc *sc;
    322 	usbd_status r;
    323 
    324 	if (unit >= ums_cd.cd_ndevs)
    325 		return ENXIO;
    326 	sc = ums_cd.cd_devs[unit];
    327 	if (!sc)
    328 		return ENXIO;
    329 
    330 	DPRINTF(("umsopen: sc=%p, disco=%d\n", sc, sc->sc_disconnected));
    331 
    332 	if (sc->sc_disconnected)
    333 		return (EIO);
    334 
    335 	if (sc->sc_state & UMS_OPEN)
    336 		return EBUSY;
    337 
    338 	if (clalloc(&sc->sc_q, UMS_BSIZE, 0) == -1)
    339 		return ENOMEM;
    340 
    341 	sc->sc_state |= UMS_OPEN;
    342 	sc->sc_status = 0;
    343 
    344 	/* Set up interrupt pipe. */
    345 	r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    346 				USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
    347 				sc->sc_ibuf, sc->sc_isize, ums_intr);
    348 	if (r != USBD_NORMAL_COMPLETION) {
    349 		DPRINTF(("umsopen: usbd_open_pipe_intr failed, error=%d\n",r));
    350 		sc->sc_state &= ~UMS_OPEN;
    351 		clfree(&sc->sc_q);
    352 		return (EIO);
    353 	}
    354 	usbd_set_disco(sc->sc_intrpipe, ums_disco, sc);
    355 	return 0;
    356 }
    357 
    358 int
    359 umsclose(dev, flag, mode, p)
    360 	dev_t dev;
    361 	int flag;
    362 	int mode;
    363 	struct proc *p;
    364 {
    365 	struct ums_softc *sc = ums_cd.cd_devs[UMSUNIT(dev)];
    366 
    367 	if (sc->sc_disconnected)
    368 		return (EIO);
    369 
    370 	DPRINTF(("umsclose: sc=%p\n", sc));
    371 
    372 	/* Disable interrupts. */
    373 	usbd_abort_pipe(sc->sc_intrpipe);
    374 	usbd_close_pipe(sc->sc_intrpipe);
    375 
    376 	sc->sc_state &= ~UMS_OPEN;
    377 
    378 	clfree(&sc->sc_q);
    379 
    380 	return 0;
    381 }
    382 
    383 int
    384 umsread(dev, uio, flag)
    385 	dev_t dev;
    386 	struct uio *uio;
    387 	int flag;
    388 {
    389 	struct ums_softc *sc = ums_cd.cd_devs[UMSUNIT(dev)];
    390 	int s;
    391 	int error = 0;
    392 	size_t length;
    393 	u_char buffer[UMS_CHUNK];
    394 
    395 	/* Block until mouse activity occured. */
    396 
    397 	if (sc->sc_disconnected)
    398 		return (EIO);
    399 
    400 	s = spltty();
    401 	while (sc->sc_q.c_cc == 0) {
    402 		if (flag & IO_NDELAY) {
    403 			splx(s);
    404 			return EWOULDBLOCK;
    405 		}
    406 		sc->sc_state |= UMS_ASLP;
    407 		DPRINTFN(5, ("umsread: sleep on %p\n", sc));
    408 		error = tsleep((caddr_t)sc, PZERO | PCATCH, "umsrea", 0);
    409 		DPRINTFN(5, ("umsread: woke, error=%d\n", error));
    410 		if (error) {
    411 			sc->sc_state &= ~UMS_ASLP;
    412 			splx(s);
    413 			return error;
    414 		}
    415 		if (sc->sc_state & UMS_NEEDCLEAR) {
    416 			DPRINTFN(-1,("umsread: clearing stall\n"));
    417 			sc->sc_state &= ~UMS_NEEDCLEAR;
    418 			usbd_clear_endpoint_stall(sc->sc_intrpipe);
    419 		}
    420 	}
    421 	splx(s);
    422 
    423 	/* Transfer as many chunks as possible. */
    424 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    425 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    426 		if (length > sizeof(buffer))
    427 			length = sizeof(buffer);
    428 
    429 		/* Remove a small chunk from the input queue. */
    430 		(void) q_to_b(&sc->sc_q, buffer, length);
    431 		DPRINTFN(5, ("umsread: got %d chars\n", length));
    432 
    433 		/* Copy the data to the user process. */
    434 		if ((error = uiomove(buffer, length, uio)) != 0)
    435 			break;
    436 	}
    437 
    438 	return error;
    439 }
    440 
    441 int
    442 umsioctl(dev, cmd, addr, flag, p)
    443 	dev_t dev;
    444 	u_long cmd;
    445 	caddr_t addr;
    446 	int flag;
    447 	struct proc *p;
    448 {
    449 	struct ums_softc *sc = ums_cd.cd_devs[UMSUNIT(dev)];
    450 
    451 	if (sc->sc_disconnected)
    452 		return (EIO);
    453 
    454 	return EINVAL;
    455 }
    456 
    457 int
    458 umspoll(dev, events, p)
    459 	dev_t dev;
    460 	int events;
    461 	struct proc *p;
    462 {
    463 	struct ums_softc *sc = ums_cd.cd_devs[UMSUNIT(dev)];
    464 	int revents = 0;
    465 	int s;
    466 
    467 	if (sc->sc_disconnected)
    468 		return (EIO);
    469 
    470 	s = spltty();
    471 	if (events & (POLLIN | POLLRDNORM))
    472 		if (sc->sc_q.c_cc > 0)
    473 			revents |= events & (POLLIN | POLLRDNORM);
    474 		else
    475 			selrecord(p, &sc->sc_rsel);
    476 
    477 	splx(s);
    478 	return (revents);
    479 }
    480