xref: /openbsd-src/sys/dev/usb/ubcmtp.c (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
1 /*	$OpenBSD: ubcmtp.c,v 1.19 2019/01/13 14:30:16 mpi Exp $ */
2 
3 /*
4  * Copyright (c) 2013-2014, joshua stein <jcs@openbsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the copyright holder may not be used to endorse or
16  *    promote products derived from this software without specific
17  *    prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Apple USB multitouch trackpad (Broadcom BCM5974) driver
34  *
35  * Protocol info/magic from bcm5974 Linux driver by Henrik Rydberg, et al.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/device.h>
40 #include <sys/errno.h>
41 #include <sys/malloc.h>
42 
43 #include <sys/ioctl.h>
44 #include <sys/systm.h>
45 #include <sys/tty.h>
46 
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdevs.h>
50 #include <dev/usb/uhidev.h>
51 #include <dev/usb/usbhid.h>
52 
53 #include <dev/wscons/wsconsio.h>
54 #include <dev/wscons/wsmousevar.h>
55 
56 /* #define UBCMTP_DEBUG */
57 
58 #ifdef UBCMTP_DEBUG
59 #define DPRINTF(x...)	do { printf(x); } while (0);
60 #else
61 #define DPRINTF(x...)
62 #endif
63 
64 /* magic to switch device from HID (default) mode into raw */
65 #define UBCMTP_WELLSPRING_MODE_RAW	0x01
66 #define UBCMTP_WELLSPRING_MODE_HID	0x08
67 #define UBCMTP_WELLSPRING_MODE_LEN	8
68 #define UBCMTP_WELLSPRING9_MODE_RAW	0x01
69 #define UBCMTP_WELLSPRING9_MODE_HID	0x00
70 #define UBCMTP_WELLSPRING9_MODE_LEN	2
71 
72 struct ubcmtp_button {
73 	uint8_t		unused;
74 	uint8_t		button;
75 	uint8_t		rel_x;
76 	uint8_t		rel_y;
77 };
78 
79 struct ubcmtp_finger {
80 	uint16_t	origin;
81 	uint16_t	abs_x;
82 	uint16_t	abs_y;
83 	uint16_t	rel_x;
84 	uint16_t	rel_y;
85 	uint16_t	tool_major;
86 	uint16_t	tool_minor;
87 	uint16_t	orientation;
88 	uint16_t	touch_major;
89 	uint16_t	touch_minor;
90 	uint16_t	unused[2];
91 	uint16_t	pressure;
92 	uint16_t	multi;
93 } __packed __attribute((aligned(2)));
94 
95 #define UBCMTP_MAX_FINGERS	16
96 #define UBCMTP_ALL_FINGER_SIZE	(UBCMTP_MAX_FINGERS * sizeof(struct ubcmtp_finger))
97 
98 #define UBCMTP_TYPE1		1
99 #define UBCMTP_TYPE1_TPOFF	(13 * sizeof(uint16_t))
100 #define UBCMTP_TYPE1_TPLEN	UBCMTP_TYPE1_TPOFF + UBCMTP_ALL_FINGER_SIZE
101 #define UBCMTP_TYPE1_TPIFACE	1
102 #define UBCMTP_TYPE1_BTIFACE	2
103 
104 #define UBCMTP_TYPE2		2
105 #define UBCMTP_TYPE2_TPOFF	(15 * sizeof(uint16_t))
106 #define UBCMTP_TYPE2_TPLEN	UBCMTP_TYPE2_TPOFF + UBCMTP_ALL_FINGER_SIZE
107 #define UBCMTP_TYPE2_TPIFACE	1
108 #define UBCMTP_TYPE2_BTOFF	15
109 
110 #define UBCMTP_TYPE3		3
111 #define UBCMTP_TYPE3_TPOFF	(19 * sizeof(uint16_t))
112 #define UBCMTP_TYPE3_TPLEN	UBCMTP_TYPE3_TPOFF + UBCMTP_ALL_FINGER_SIZE
113 #define UBCMTP_TYPE3_TPIFACE	2
114 #define UBCMTP_TYPE3_BTOFF	23
115 
116 #define UBCMTP_TYPE4		4
117 #define UBCMTP_TYPE4_TPOFF	(24 * sizeof(uint16_t))
118 #define UBCMTP_TYPE4_TPLEN	UBCMTP_TYPE4_TPOFF + UBCMTP_ALL_FINGER_SIZE
119 #define UBCMTP_TYPE4_TPIFACE	2
120 #define UBCMTP_TYPE4_BTOFF	31
121 
122 #define UBCMTP_FINGER_ORIENT	16384
123 #define UBCMTP_SN_PRESSURE	45
124 #define UBCMTP_SN_WIDTH		25
125 #define UBCMTP_SN_COORD		250
126 #define UBCMTP_SN_ORIENT	10
127 
128 /* Identify clickpads in ubcmtp_configure. */
129 #define IS_CLICKPAD(ubcmtp_type) (ubcmtp_type != UBCMTP_TYPE1)
130 
131 /* Use a constant, synaptics-compatible pressure value for now. */
132 #define DEFAULT_PRESSURE	40
133 
134 struct ubcmtp_limit {
135 	int limit;
136 	int min;
137 	int max;
138 };
139 
140 struct ubcmtp_dev {
141 	int vendor;			/* vendor */
142 	int ansi, iso, jis;		/* 3 types of product */
143 	int type;			/* 1 (normal) or 2 (integrated btn) */
144 	struct ubcmtp_limit l_pressure;	/* finger pressure */
145 	struct ubcmtp_limit l_width;	/* finger width */
146 	struct ubcmtp_limit l_x;
147 	struct ubcmtp_limit l_y;
148 	struct ubcmtp_limit l_orientation;
149 };
150 
151 static struct ubcmtp_dev ubcmtp_devices[] = {
152 	/* type 1 devices with separate buttons */
153 	{
154 		USB_VENDOR_APPLE,
155 		/* MacbookAir */
156 		USB_PRODUCT_APPLE_WELLSPRING_ANSI,
157 		USB_PRODUCT_APPLE_WELLSPRING_ISO,
158 		USB_PRODUCT_APPLE_WELLSPRING_JIS,
159 		UBCMTP_TYPE1,
160 		{ UBCMTP_SN_PRESSURE, 0, 256 },
161 		{ UBCMTP_SN_WIDTH, 0, 2048 },
162 		{ UBCMTP_SN_COORD, -4824, 5342 },
163 		{ UBCMTP_SN_COORD, -172, 5820 },
164 		{ UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT },
165 	},
166 	{
167 		USB_VENDOR_APPLE,
168 		/* MacbookProPenryn */
169 		USB_PRODUCT_APPLE_WELLSPRING2_ANSI,
170 		USB_PRODUCT_APPLE_WELLSPRING2_ISO,
171 		USB_PRODUCT_APPLE_WELLSPRING2_JIS,
172 		UBCMTP_TYPE1,
173 		{ UBCMTP_SN_PRESSURE, 0, 256 },
174 		{ UBCMTP_SN_WIDTH, 0, 2048 },
175 		{ UBCMTP_SN_COORD, -4824, 4824 },
176 		{ UBCMTP_SN_COORD, -172, 4290 },
177 		{ UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT },
178 	},
179 	/* type 2 devices with integrated buttons */
180 	{
181 		USB_VENDOR_APPLE,
182 		/* Macbook5,1 */
183 		USB_PRODUCT_APPLE_WELLSPRING3_ANSI,
184 		USB_PRODUCT_APPLE_WELLSPRING3_ISO,
185 		USB_PRODUCT_APPLE_WELLSPRING3_JIS,
186 		UBCMTP_TYPE2,
187 		{ UBCMTP_SN_PRESSURE, 0, 300 },
188 		{ UBCMTP_SN_WIDTH, 0, 2048 },
189 		{ UBCMTP_SN_COORD, -4460, 5166 },
190 		{ UBCMTP_SN_COORD, -75, 6700 },
191 		{ UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT },
192 	},
193 	{
194 		USB_VENDOR_APPLE,
195 		/* MacbookAir3,1 */
196 		USB_PRODUCT_APPLE_WELLSPRING4A_ANSI,
197 		USB_PRODUCT_APPLE_WELLSPRING4A_ISO,
198 		USB_PRODUCT_APPLE_WELLSPRING4A_JIS,
199 		UBCMTP_TYPE2,
200 		{ UBCMTP_SN_PRESSURE, 0, 300 },
201 		{ UBCMTP_SN_WIDTH, 0, 2048 },
202 		{ UBCMTP_SN_COORD, -4616, 5112 },
203 		{ UBCMTP_SN_COORD, -142, 5234 },
204 		{ UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT },
205 	},
206 	{
207 		USB_VENDOR_APPLE,
208 		/* MacbookAir3,2 */
209 		USB_PRODUCT_APPLE_WELLSPRING4_ANSI,
210 		USB_PRODUCT_APPLE_WELLSPRING4_ISO,
211 		USB_PRODUCT_APPLE_WELLSPRING4_JIS,
212 		UBCMTP_TYPE2,
213 		{ UBCMTP_SN_PRESSURE, 0, 300 },
214 		{ UBCMTP_SN_WIDTH, 0, 2048 },
215 		{ UBCMTP_SN_COORD, -4620, 5140 },
216 		{ UBCMTP_SN_COORD, -150, 6600 },
217 		{ UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT },
218 	},
219 	{
220 		USB_VENDOR_APPLE,
221 		/* Macbook8 */
222 		USB_PRODUCT_APPLE_WELLSPRING5_ANSI,
223 		USB_PRODUCT_APPLE_WELLSPRING5_ISO,
224 		USB_PRODUCT_APPLE_WELLSPRING5_JIS,
225 		UBCMTP_TYPE2,
226 		{ UBCMTP_SN_PRESSURE, 0, 300 },
227 		{ UBCMTP_SN_WIDTH, 0, 2048 },
228 		{ UBCMTP_SN_COORD, -4415, 5050 },
229 		{ UBCMTP_SN_COORD, -55, 6680 },
230 		{ UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT },
231 	},
232 	{
233 		USB_VENDOR_APPLE,
234 		/* Macbook8,2 */
235 		USB_PRODUCT_APPLE_WELLSPRING5A_ANSI,
236 		USB_PRODUCT_APPLE_WELLSPRING5A_ISO,
237 		USB_PRODUCT_APPLE_WELLSPRING5A_JIS,
238 		UBCMTP_TYPE2,
239 		{ UBCMTP_SN_PRESSURE, 0, 300 },
240 		{ UBCMTP_SN_WIDTH, 0, 2048 },
241 		{ UBCMTP_SN_COORD, -4750, 5280 },
242 		{ UBCMTP_SN_COORD, -150, 6730 },
243 		{ UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT },
244 	},
245 	{
246 		USB_VENDOR_APPLE,
247 		/* MacbookAir4,2 */
248 		USB_PRODUCT_APPLE_WELLSPRING6_ANSI,
249 		USB_PRODUCT_APPLE_WELLSPRING6_ISO,
250 		USB_PRODUCT_APPLE_WELLSPRING6_JIS,
251 		UBCMTP_TYPE2,
252 		{ UBCMTP_SN_PRESSURE, 0, 300 },
253 		{ UBCMTP_SN_WIDTH, 0, 2048 },
254 		{ UBCMTP_SN_COORD, -4620, 5140 },
255 		{ UBCMTP_SN_COORD, -150, 6600 },
256 		{ UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT },
257 	},
258 	{
259 		USB_VENDOR_APPLE,
260 		/* MacbookAir4,1 */
261 		USB_PRODUCT_APPLE_WELLSPRING6A_ANSI,
262 		USB_PRODUCT_APPLE_WELLSPRING6A_ISO,
263 		USB_PRODUCT_APPLE_WELLSPRING6A_JIS,
264 		UBCMTP_TYPE2,
265 		{ UBCMTP_SN_PRESSURE, 0, 300 },
266 		{ UBCMTP_SN_WIDTH, 0, 2048 },
267 		{ UBCMTP_SN_COORD, -4620, 5140 },
268 		{ UBCMTP_SN_COORD, -150, 6600 },
269 		{ UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT },
270 	},
271 	{
272 		USB_VENDOR_APPLE,
273 		/* MacbookPro10,1 */
274 		USB_PRODUCT_APPLE_WELLSPRING7_ANSI,
275 		USB_PRODUCT_APPLE_WELLSPRING7_ISO,
276 		USB_PRODUCT_APPLE_WELLSPRING7_JIS,
277 		UBCMTP_TYPE2,
278 		{ UBCMTP_SN_PRESSURE, 0, 300 },
279 		{ UBCMTP_SN_WIDTH, 0, 2048 },
280 		{ UBCMTP_SN_COORD, -4750, 5280 },
281 		{ UBCMTP_SN_COORD, -150, 6730 },
282 		{ UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT },
283 	},
284 	{
285 		USB_VENDOR_APPLE,
286 		/* MacbookPro10,2 */
287 		USB_PRODUCT_APPLE_WELLSPRING7A_ANSI,
288 		USB_PRODUCT_APPLE_WELLSPRING7A_ISO,
289 		USB_PRODUCT_APPLE_WELLSPRING7A_JIS,
290 		UBCMTP_TYPE2,
291 		{ UBCMTP_SN_PRESSURE, 0, 300 },
292 		{ UBCMTP_SN_WIDTH, 0, 2048 },
293 		{ UBCMTP_SN_COORD, -4750, 5280 },
294 		{ UBCMTP_SN_COORD, -150, 6730 },
295 		{ UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT },
296 	},
297 	{
298 		USB_VENDOR_APPLE,
299 		/* MacbookAir6,1 */
300 		USB_PRODUCT_APPLE_WELLSPRING8_ANSI,
301 		USB_PRODUCT_APPLE_WELLSPRING8_ISO,
302 		USB_PRODUCT_APPLE_WELLSPRING8_JIS,
303 		UBCMTP_TYPE3,
304 		{ UBCMTP_SN_PRESSURE, 0, 300 },
305 		{ UBCMTP_SN_WIDTH, 0, 2048 },
306 		{ UBCMTP_SN_COORD, -4620, 5140 },
307 		{ UBCMTP_SN_COORD, -150, 6600 },
308 		{ UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT },
309 	},
310 	{
311 		USB_VENDOR_APPLE,
312 		/* MacbookPro12,1 */
313 		USB_PRODUCT_APPLE_WELLSPRING9_ANSI,
314 		USB_PRODUCT_APPLE_WELLSPRING9_ISO,
315 		USB_PRODUCT_APPLE_WELLSPRING9_JIS,
316 		UBCMTP_TYPE4,
317 		{ UBCMTP_SN_PRESSURE, 0, 300 },
318 		{ UBCMTP_SN_WIDTH, 0, 2048 },
319 		{ UBCMTP_SN_COORD, -4828, 5345 },
320 		{ UBCMTP_SN_COORD, -203, 6803 },
321 		{ UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT },
322 	},
323 };
324 
325 struct ubcmtp_softc {
326 	struct device		sc_dev;		/* base device */
327 
328 	struct ubcmtp_dev	*dev_type;
329 
330 	struct uhidev		sc_hdev;
331 	struct usbd_device	*sc_udev;
332 	struct device		*sc_wsmousedev;
333 
334 	struct usbd_interface	*sc_tp_iface;	/* trackpad interface */
335 	struct usbd_pipe	*sc_tp_pipe;	/* trackpad pipe */
336 	int			sc_tp_epaddr;	/* endpoint addr */
337 	int			tp_maxlen;	/* max size of tp data */
338 	int			tp_offset;	/* finger offset into data */
339 	uint8_t			*tp_pkt;
340 
341 	struct usbd_interface	*sc_bt_iface;	/* button interface */
342 	struct usbd_pipe	*sc_bt_pipe;	/* button pipe */
343 	int			sc_bt_epaddr;	/* endpoint addr */
344 	int			bt_maxlen;	/* max size of button data */
345 	uint8_t			*bt_pkt;
346 
347 	uint32_t		sc_status;
348 #define UBCMTP_ENABLED		1
349 
350 	struct mtpoint		frame[UBCMTP_MAX_FINGERS];
351 	int			contacts;
352 	int			btn;
353 };
354 
355 int	ubcmtp_enable(void *);
356 void	ubcmtp_disable(void *);
357 int	ubcmtp_ioctl(void *, unsigned long, caddr_t, int, struct proc *);
358 int	ubcmtp_raw_mode(struct ubcmtp_softc *, int);
359 int	ubcmtp_setup_pipes(struct ubcmtp_softc *);
360 void	ubcmtp_bt_intr(struct usbd_xfer *, void *, usbd_status);
361 void	ubcmtp_tp_intr(struct usbd_xfer *, void *, usbd_status);
362 
363 int	ubcmtp_match(struct device *, void *, void *);
364 void	ubcmtp_attach(struct device *, struct device *, void *);
365 int	ubcmtp_detach(struct device *, int);
366 int	ubcmtp_activate(struct device *, int);
367 int	ubcmtp_configure(struct ubcmtp_softc *);
368 
369 const struct wsmouse_accessops ubcmtp_accessops = {
370 	ubcmtp_enable,
371 	ubcmtp_ioctl,
372 	ubcmtp_disable,
373 };
374 
375 struct cfdriver ubcmtp_cd = {
376 	NULL, "ubcmtp", DV_DULL
377 };
378 
379 const struct cfattach ubcmtp_ca = {
380 	sizeof(struct ubcmtp_softc), ubcmtp_match, ubcmtp_attach, ubcmtp_detach,
381 	ubcmtp_activate,
382 };
383 
384 int
385 ubcmtp_match(struct device *parent, void *match, void *aux)
386 {
387 	struct usb_attach_arg *uaa = aux;
388 	usb_interface_descriptor_t *id;
389 	int i;
390 
391 	if (uaa->iface == NULL)
392 		return (UMATCH_NONE);
393 
394 	for (i = 0; i < nitems(ubcmtp_devices); i++) {
395 		if (uaa->vendor == ubcmtp_devices[i].vendor && (
396 		    uaa->product == ubcmtp_devices[i].ansi ||
397 		    uaa->product == ubcmtp_devices[i].iso ||
398 		    uaa->product == ubcmtp_devices[i].jis)) {
399 			if (uaa->nifaces < 2)
400 				return (UMATCH_NONE);
401 			if ((ubcmtp_devices[i].type != UBCMTP_TYPE2) &&
402 			    (uaa->nifaces < 3))
403 				return (UMATCH_NONE);
404 
405 			/*
406 			 * The USB keyboard/mouse device will have one keyboard
407 			 * HID and two mouse HIDs, though only one will have a
408 			 * protocol of mouse -- we only want to take control of
409 			 * that one.
410 			 */
411 			id = usbd_get_interface_descriptor(uaa->iface);
412 			if (id->bInterfaceProtocol == UIPROTO_BOOT_MOUSE)
413 				return (UMATCH_VENDOR_PRODUCT_CONF_IFACE);
414 		}
415 	}
416 
417 	return (UMATCH_NONE);
418 }
419 
420 void
421 ubcmtp_attach(struct device *parent, struct device *self, void *aux)
422 {
423 	struct ubcmtp_softc *sc = (struct ubcmtp_softc *)self;
424 	struct usb_attach_arg *uaa = aux;
425 	struct usbd_device *dev = uaa->device;
426 	struct wsmousedev_attach_args a;
427 	usb_device_descriptor_t *udd;
428 	int i;
429 
430 	sc->sc_udev = uaa->device;
431 	sc->sc_status = 0;
432 
433 	if ((udd = usbd_get_device_descriptor(dev)) == NULL) {
434 		printf("ubcmtp: failed getting device descriptor\n");
435 		return;
436 	}
437 
438 	for (i = 0; i < nitems(ubcmtp_devices); i++) {
439 		if (uaa->vendor == ubcmtp_devices[i].vendor && (
440 		    uaa->product == ubcmtp_devices[i].ansi ||
441 		    uaa->product == ubcmtp_devices[i].iso ||
442 		    uaa->product == ubcmtp_devices[i].jis)) {
443 			sc->dev_type = &ubcmtp_devices[i];
444 			DPRINTF("%s: attached to 0x%x/0x%x type %d\n",
445 			    sc->sc_dev.dv_xname, uaa->vendor, uaa->product,
446 			    sc->dev_type->type);
447 			break;
448 		}
449 	}
450 
451 	if (sc->dev_type == NULL) {
452 		/* how did we match then? */
453 		printf("%s: failed looking up device in table\n",
454 		    sc->sc_dev.dv_xname);
455 		return;
456 	}
457 
458 	switch (sc->dev_type->type) {
459 	case UBCMTP_TYPE1:
460 		sc->tp_maxlen = UBCMTP_TYPE1_TPLEN;
461 		sc->tp_offset = UBCMTP_TYPE1_TPOFF;
462 		sc->sc_tp_iface = uaa->ifaces[UBCMTP_TYPE1_TPIFACE];
463 		usbd_claim_iface(sc->sc_udev, UBCMTP_TYPE1_TPIFACE);
464 
465 		/* button offsets */
466 		sc->bt_maxlen = sizeof(struct ubcmtp_button);
467 		sc->sc_bt_iface = uaa->ifaces[UBCMTP_TYPE1_BTIFACE];
468 		usbd_claim_iface(sc->sc_udev, UBCMTP_TYPE1_BTIFACE);
469 		break;
470 
471 	case UBCMTP_TYPE2:
472 		sc->tp_maxlen = UBCMTP_TYPE2_TPLEN;
473 		sc->tp_offset = UBCMTP_TYPE2_TPOFF;
474 		sc->sc_tp_iface = uaa->ifaces[UBCMTP_TYPE2_TPIFACE];
475 		usbd_claim_iface(sc->sc_udev, UBCMTP_TYPE2_TPIFACE);
476 		break;
477 
478 	case UBCMTP_TYPE3:
479 		sc->tp_maxlen = UBCMTP_TYPE3_TPLEN;
480 		sc->tp_offset = UBCMTP_TYPE3_TPOFF;
481 		sc->sc_tp_iface = uaa->ifaces[UBCMTP_TYPE3_TPIFACE];
482 		usbd_claim_iface(sc->sc_udev, UBCMTP_TYPE3_TPIFACE);
483 		break;
484 
485 	case UBCMTP_TYPE4:
486 		sc->tp_maxlen = UBCMTP_TYPE4_TPLEN;
487 		sc->tp_offset = UBCMTP_TYPE4_TPOFF;
488 		sc->sc_tp_iface = uaa->ifaces[UBCMTP_TYPE4_TPIFACE];
489 		usbd_claim_iface(sc->sc_udev, UBCMTP_TYPE4_TPIFACE);
490 		break;
491 	}
492 
493 	a.accessops = &ubcmtp_accessops;
494 	a.accesscookie = sc;
495 
496 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
497 	if (sc->sc_wsmousedev != NULL && ubcmtp_configure(sc))
498 		ubcmtp_disable(sc);
499 }
500 
501 int
502 ubcmtp_detach(struct device *self, int flags)
503 {
504 	struct ubcmtp_softc *sc = (struct ubcmtp_softc *)self;
505 	int ret = 0;
506 
507 	if (sc->sc_wsmousedev != NULL)
508 		ret = config_detach(sc->sc_wsmousedev, flags);
509 
510 	return (ret);
511 }
512 
513 int
514 ubcmtp_activate(struct device *self, int act)
515 {
516 	struct ubcmtp_softc *sc = (struct ubcmtp_softc *)self;
517 	int rv = 0;
518 
519 	if (act == DVACT_DEACTIVATE) {
520 		if (sc->sc_wsmousedev != NULL)
521 			rv = config_deactivate(sc->sc_wsmousedev);
522 		usbd_deactivate(sc->sc_udev);
523 	}
524 
525 	return (rv);
526 }
527 
528 int
529 ubcmtp_configure(struct ubcmtp_softc *sc)
530 {
531 	struct wsmousehw *hw = wsmouse_get_hw(sc->sc_wsmousedev);
532 
533 	hw->type = WSMOUSE_TYPE_TOUCHPAD;
534 	hw->hw_type = (IS_CLICKPAD(sc->dev_type->type)
535 	    ? WSMOUSEHW_CLICKPAD : WSMOUSEHW_TOUCHPAD);
536 	hw->x_min = sc->dev_type->l_x.min;
537 	hw->x_max = sc->dev_type->l_x.max;
538 	hw->y_min = sc->dev_type->l_y.min;
539 	hw->y_max = sc->dev_type->l_y.max;
540 	hw->mt_slots = UBCMTP_MAX_FINGERS;
541 	hw->flags = WSMOUSEHW_MT_TRACKING;
542 
543 	return wsmouse_configure(sc->sc_wsmousedev, NULL, 0);
544 }
545 
546 int
547 ubcmtp_enable(void *v)
548 {
549 	struct ubcmtp_softc *sc = v;
550 
551 	if (sc->sc_status & UBCMTP_ENABLED)
552 		return (EBUSY);
553 
554 	if (usbd_is_dying(sc->sc_udev))
555 		return (EIO);
556 
557 	if (ubcmtp_raw_mode(sc, 1) != 0) {
558 		printf("%s: failed to enter raw mode\n", sc->sc_dev.dv_xname);
559 		return (1);
560 	}
561 
562 	if (ubcmtp_setup_pipes(sc) == 0) {
563 		sc->sc_status |= UBCMTP_ENABLED;
564 		return (0);
565 	} else
566 		return (1);
567 }
568 
569 void
570 ubcmtp_disable(void *v)
571 {
572 	struct ubcmtp_softc *sc = v;
573 
574 	if (usbd_is_dying(sc->sc_udev) || !(sc->sc_status & UBCMTP_ENABLED))
575 		return;
576 
577 	sc->sc_status &= ~UBCMTP_ENABLED;
578 
579 	ubcmtp_raw_mode(sc, 0);
580 
581 	if (sc->sc_tp_pipe != NULL) {
582 		usbd_abort_pipe(sc->sc_tp_pipe);
583 		usbd_close_pipe(sc->sc_tp_pipe);
584 		sc->sc_tp_pipe = NULL;
585 	}
586 	if (sc->sc_bt_pipe != NULL) {
587 		usbd_abort_pipe(sc->sc_bt_pipe);
588 		usbd_close_pipe(sc->sc_bt_pipe);
589 		sc->sc_bt_pipe = NULL;
590 	}
591 
592 	if (sc->tp_pkt != NULL) {
593 		free(sc->tp_pkt, M_USBDEV, sc->tp_maxlen);
594 		sc->tp_pkt = NULL;
595 	}
596 	if (sc->bt_pkt != NULL) {
597 		free(sc->bt_pkt, M_USBDEV, sc->bt_maxlen);
598 		sc->bt_pkt = NULL;
599 	}
600 }
601 
602 int
603 ubcmtp_ioctl(void *v, unsigned long cmd, caddr_t data, int flag, struct proc *p)
604 {
605 	struct ubcmtp_softc *sc = v;
606 	struct wsmouse_calibcoords *wsmc = (struct wsmouse_calibcoords *)data;
607 	int wsmode;
608 
609 	DPRINTF("%s: in %s with cmd 0x%lx\n", sc->sc_dev.dv_xname, __func__,
610 	    cmd);
611 
612 	switch (cmd) {
613 	case WSMOUSEIO_GTYPE: {
614 		struct wsmousehw *hw = wsmouse_get_hw(sc->sc_wsmousedev);
615 		*(u_int *)data = hw->type;
616 		break;
617 	}
618 
619 	case WSMOUSEIO_GCALIBCOORDS:
620 		wsmc->minx = sc->dev_type->l_x.min;
621 		wsmc->maxx = sc->dev_type->l_x.max;
622 		wsmc->miny = sc->dev_type->l_y.min;
623 		wsmc->maxy = sc->dev_type->l_y.max;
624 		wsmc->swapxy = 0;
625 		wsmc->resx = 0;
626 		wsmc->resy = 0;
627 		break;
628 
629 	case WSMOUSEIO_SETMODE:
630 		wsmode = *(u_int *)data;
631 		if (wsmode != WSMOUSE_COMPAT && wsmode != WSMOUSE_NATIVE) {
632 			printf("%s: invalid mode %d\n", sc->sc_dev.dv_xname,
633 			    wsmode);
634 			return (EINVAL);
635 		}
636 		wsmouse_set_mode(sc->sc_wsmousedev, wsmode);
637 
638 		DPRINTF("%s: changing mode to %s\n",
639 		    sc->sc_dev.dv_xname, (wsmode == WSMOUSE_COMPAT ? "compat" :
640 		    "native"));
641 
642 		break;
643 
644 	default:
645 		return (-1);
646 	}
647 
648 	return (0);
649 }
650 
651 int
652 ubcmtp_raw_mode(struct ubcmtp_softc *sc, int enable)
653 {
654 	usb_device_request_t r;
655 	usbd_status err;
656 	uint8_t buf[8];
657 
658 	/* type 3 has no raw mode */
659 	if (sc->dev_type->type == UBCMTP_TYPE3)
660 		return (0);
661 
662 	r.bRequest = UR_GET_REPORT;
663 	r.bmRequestType = UT_READ_CLASS_INTERFACE;
664 	if (sc->dev_type->type < UBCMTP_TYPE4) {
665 		USETW2(r.wValue, UHID_FEATURE_REPORT, 0);
666 		USETW(r.wIndex, 0);
667 		USETW(r.wLength, UBCMTP_WELLSPRING_MODE_LEN);
668 	} else {
669 		USETW2(r.wValue, UHID_FEATURE_REPORT, 2);
670 		USETW(r.wIndex, 2);
671 		USETW(r.wLength, UBCMTP_WELLSPRING9_MODE_LEN);
672 	}
673 
674 	err = usbd_do_request(sc->sc_udev, &r, buf);
675 	if (err != USBD_NORMAL_COMPLETION) {
676 		printf("%s: %s: failed to get feature report\n",
677 		    sc->sc_dev.dv_xname, __func__);
678 		return (err);
679 	}
680 
681 	/* toggle magic byte and write everything back */
682 	if (sc->dev_type->type < UBCMTP_TYPE4)
683 		buf[0] = (enable ? UBCMTP_WELLSPRING_MODE_RAW :
684 		    UBCMTP_WELLSPRING_MODE_HID);
685 	else
686 		buf[1] = (enable ? UBCMTP_WELLSPRING9_MODE_RAW :
687 		    UBCMTP_WELLSPRING9_MODE_HID);
688 
689 	r.bRequest = UR_SET_REPORT;
690 	r.bmRequestType = UT_WRITE_CLASS_INTERFACE;
691 	if (sc->dev_type->type < UBCMTP_TYPE4) {
692 		USETW2(r.wValue, UHID_FEATURE_REPORT, 0);
693 		USETW(r.wIndex, 0);
694 		USETW(r.wLength, UBCMTP_WELLSPRING_MODE_LEN);
695 	} else {
696 		USETW2(r.wValue, UHID_FEATURE_REPORT, 2);
697 		USETW(r.wIndex, 2);
698 		USETW(r.wLength, UBCMTP_WELLSPRING9_MODE_LEN);
699 	}
700 
701 	err = usbd_do_request(sc->sc_udev, &r, buf);
702 	if (err != USBD_NORMAL_COMPLETION) {
703 		printf("%s: %s: failed to toggle raw mode\n",
704 		    sc->sc_dev.dv_xname, __func__);
705 		return (err);
706 	}
707 
708 	return (0);
709 }
710 
711 int
712 ubcmtp_setup_pipes(struct ubcmtp_softc *sc)
713 {
714 	usbd_status err;
715 	usb_endpoint_descriptor_t *ed;
716 
717 	if (sc->dev_type->type == UBCMTP_TYPE1) {
718 		/* setup physical button pipe */
719 
720 		ed = usbd_interface2endpoint_descriptor(sc->sc_bt_iface, 0);
721 		if (ed == NULL) {
722 			printf("%s: failed getting button endpoint descriptor\n",
723 			    sc->sc_dev.dv_xname);
724 			goto fail1;
725 		}
726 		sc->sc_bt_epaddr = ed->bEndpointAddress;
727 		sc->bt_pkt = malloc(sc->bt_maxlen, M_USBDEV, M_WAITOK);
728 		if (sc->bt_pkt == NULL)
729 			goto fail1;
730 
731 		DPRINTF("%s: button iface at 0x%x, max size %d\n",
732 		    sc->sc_dev.dv_xname, sc->sc_bt_epaddr, sc->bt_maxlen);
733 
734 		err = usbd_open_pipe_intr(sc->sc_bt_iface, sc->sc_bt_epaddr,
735 		    USBD_SHORT_XFER_OK, &sc->sc_bt_pipe, sc, sc->bt_pkt,
736 		    sc->bt_maxlen, ubcmtp_bt_intr, USBD_DEFAULT_INTERVAL);
737 		if (err != USBD_NORMAL_COMPLETION) {
738 			printf("%s: failed opening button pipe\n",
739 			    sc->sc_dev.dv_xname);
740 			goto fail1;
741 		}
742 	}
743 
744 	/* setup trackpad data pipe */
745 
746 	ed = usbd_interface2endpoint_descriptor(sc->sc_tp_iface, 0);
747 	if (ed == NULL) {
748 		printf("%s: failed getting trackpad data endpoint descriptor\n",
749 		    sc->sc_dev.dv_xname);
750 		goto fail2;
751 	}
752 	sc->sc_tp_epaddr = ed->bEndpointAddress;
753 	sc->tp_pkt = malloc(sc->tp_maxlen, M_USBDEV, M_WAITOK);
754 	if (sc->tp_pkt == NULL)
755 		goto fail2;
756 
757 	DPRINTF("%s: trackpad data iface at 0x%x, max size %d\n",
758 	    sc->sc_dev.dv_xname, sc->sc_tp_epaddr, sc->tp_maxlen);
759 
760 	err = usbd_open_pipe_intr(sc->sc_tp_iface, sc->sc_tp_epaddr,
761 	    USBD_SHORT_XFER_OK, &sc->sc_tp_pipe, sc, sc->tp_pkt, sc->tp_maxlen,
762 	    ubcmtp_tp_intr, USBD_DEFAULT_INTERVAL);
763 	if (err != USBD_NORMAL_COMPLETION) {
764 		printf("%s: error opening trackpad data pipe\n",
765 		    sc->sc_dev.dv_xname);
766 		goto fail2;
767 	}
768 
769 	return (0);
770 
771 fail2:
772 	if (sc->sc_tp_pipe != NULL) {
773 		usbd_abort_pipe(sc->sc_tp_pipe);
774 		usbd_close_pipe(sc->sc_tp_pipe);
775 	}
776 	if (sc->tp_pkt != NULL)
777 		free(sc->tp_pkt, M_USBDEV, sc->tp_maxlen);
778 fail1:
779 	if (sc->sc_bt_pipe != NULL) {
780 		usbd_abort_pipe(sc->sc_bt_pipe);
781 		usbd_close_pipe(sc->sc_bt_pipe);
782 	}
783 	if (sc->bt_pkt != NULL)
784 		free(sc->bt_pkt, M_USBDEV, sc->bt_maxlen);
785 
786 	return (1);
787 }
788 
789 void
790 ubcmtp_tp_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
791 {
792 	struct ubcmtp_softc *sc = priv;
793 	struct ubcmtp_finger *pkt;
794 	u_int32_t pktlen;
795 	int i, s, btn, contacts, fingers;
796 
797 	if (usbd_is_dying(sc->sc_udev) || !(sc->sc_status & UBCMTP_ENABLED))
798 		return;
799 
800 	if (status != USBD_NORMAL_COMPLETION) {
801 		DPRINTF("%s: %s with status 0x%x\n", sc->sc_dev.dv_xname,
802 		    __func__, status);
803 
804 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
805 			return;
806 		if (status == USBD_STALLED)
807 			usbd_clear_endpoint_stall_async(sc->sc_tp_pipe);
808 		return;
809 	}
810 
811 	usbd_get_xfer_status(xfer, NULL, NULL, &pktlen, NULL);
812 
813 	if (sc->tp_pkt == NULL || pktlen < sc->tp_offset)
814 		return;
815 
816 	pkt = (struct ubcmtp_finger *)(sc->tp_pkt + sc->tp_offset);
817 	fingers = (pktlen - sc->tp_offset) / sizeof(struct ubcmtp_finger);
818 
819 	contacts = 0;
820 	for (i = 0; i < fingers; i++) {
821 		if ((int16_t)letoh16(pkt[i].touch_major) == 0)
822 			continue; /* finger lifted */
823 		sc->frame[contacts].x = (int16_t)letoh16(pkt[i].abs_x);
824 		sc->frame[contacts].y = (int16_t)letoh16(pkt[i].abs_y);
825 		sc->frame[contacts].pressure = DEFAULT_PRESSURE;
826 		contacts++;
827 	}
828 
829 	btn = sc->btn;
830 	if (sc->dev_type->type == UBCMTP_TYPE2)
831 		sc->btn = !!((int16_t)letoh16(sc->tp_pkt[UBCMTP_TYPE2_BTOFF]));
832 	else if (sc->dev_type->type == UBCMTP_TYPE3)
833 		sc->btn = !!((int16_t)letoh16(sc->tp_pkt[UBCMTP_TYPE3_BTOFF]));
834 	else if (sc->dev_type->type == UBCMTP_TYPE4)
835 		sc->btn = !!((int16_t)letoh16(sc->tp_pkt[UBCMTP_TYPE4_BTOFF]));
836 
837 	if (contacts || sc->contacts || sc->btn != btn) {
838 		sc->contacts = contacts;
839 		s = spltty();
840 		wsmouse_buttons(sc->sc_wsmousedev, sc->btn);
841 		wsmouse_mtframe(sc->sc_wsmousedev, sc->frame, contacts);
842 		wsmouse_input_sync(sc->sc_wsmousedev);
843 		splx(s);
844 	}
845 }
846 
847 /* hardware button interrupt */
848 void
849 ubcmtp_bt_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
850 {
851 	struct ubcmtp_softc *sc = priv;
852 	struct ubcmtp_button *pkt;
853 	u_int32_t len;
854 
855 	if (usbd_is_dying(sc->sc_udev) || !(sc->sc_status & UBCMTP_ENABLED))
856 		return;
857 
858 	if (status != USBD_NORMAL_COMPLETION) {
859 		DPRINTF("%s: %s with status 0x%x\n", sc->sc_dev.dv_xname,
860 		    __func__, status);
861 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
862 			return;
863 		if (status == USBD_STALLED)
864 			usbd_clear_endpoint_stall_async(sc->sc_tp_pipe);
865 		return;
866 	}
867 
868 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
869 
870 	if (sc->bt_pkt == NULL || len < sizeof(struct ubcmtp_button))
871 		return;
872 
873 	pkt = (struct ubcmtp_button *)(sc->bt_pkt);
874 
875 	DPRINTF("%s: button interrupt (%d, %d, %d, %d)", sc->sc_dev.dv_xname,
876 	    pkt->unused, pkt->button, pkt->rel_x, pkt->rel_y);
877 
878 	if (pkt->button != sc->btn) {
879 		sc->btn = pkt->button;
880 		wsmouse_buttons(sc->sc_wsmousedev, sc->btn);
881 		wsmouse_input_sync(sc->sc_wsmousedev);
882 	}
883 }
884