1 /* $Id: pbms.c,v 1.9 2009/11/05 05:37:30 dyoung Exp $ */
4 * Copyright (c) 2005, Johan Wallén
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * 3. The name of the copyright holder may not be used to endorse or
20 * promote products derived from this software without specific
21 * prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * The pbms driver provides support for the trackpad on new (post
38 * February 2005) Apple PowerBooks (and iBooks?) that are not standard
43 * The protocol (that is, the interpretation of the data generated by
44 * the trackpad) is taken from the Linux appletouch driver version
45 * 0.08 by Johannes Berg, Stelian Pop and Frank Arnold. The method
46 * used to detect fingers on the trackpad is also taken from that
51 * To add support for other devices using the same protocol, add an
52 * entry to the pbms_devices table below. See the comments for
53 * pbms_devices and struct pbms_devs.
59 * The driver transfers continuously 81 byte events. The last byte is
60 * 1 if the button is pressed, and is 0 otherwise. Of the remaining
61 * bytes, 26 + 16 = 42 are sensors detecting pressure in the X or
62 * horizontal, and Y or vertical directions, respectively. On 12 and
63 * 15 inch PowerBooks, only the 16 first sensors in the X-direction
64 * are used. In the X-direction, the sensors correspond to byte
67 * 2, 7, 12, 17, 22, 27, 32, 37, 4, 9, 14, 19, 24, 29, 34, 39, 42,
68 * 47, 52, 57, 62, 67, 72, 77, 44 and 49;
70 * in the Y direction, the sensors correspond to byte positions
72 * 1, 6, 11, 16, 21, 26, 31, 36, 3, 8, 13, 18, 23, 28, 33 and 38.
74 * The change in the sensor values over time is more interesting than
75 * their absolute values: if the pressure increases, we know that the
76 * finger has just moved there.
78 * We keep track of the previous sample (of sensor values in the X and
79 * Y directions) and the accumulated change for each sensor. When we
80 * receive a new sample, we add the difference of the new sensor value
81 * and the old value to the accumulated change. If the accumulator
82 * becomes negative, we set it to zero. The effect is that the
83 * accumulator is large for sensors whose pressure has recently
84 * increased. If there is little change in pressure (or if the
85 * pressure decreases), the accumulator drifts back to zero.
87 * Since there is some fluctuations, we ignore accumulator values
88 * below a threshold. The raw finger position is computed as a
89 * weighted average of the other sensors (the weights are the
90 * accumulated changes).
92 * For smoothing, we keep track of the previous raw finger position,
93 * and the virtual position reported to wsmouse. The new raw position
94 * is computed as a weighted average of the old raw position and the
95 * computed raw position. Since this still generates some noise, we
96 * compute a new virtual position as a weighted average of the previous
97 * virtual position and the new raw position. The weights are
98 * controlled by the raw change and a noise parameter. The position
99 * is reported as a relative position.
105 * Add support for other drivers of the same type.
107 * Add support for tapping and two-finger scrolling? The
108 * implementation already detects two fingers, so this should be
111 * Implement some of the mouse ioctls?
113 * Take care of the XXXs.
117 #include <sys/cdefs.h>
119 #include <sys/param.h>
120 #include <sys/device.h>
121 #include <sys/errno.h>
123 #include <sys/ioctl.h>
124 #include <sys/systm.h>
127 #include <dev/usb/usb.h>
128 #include <dev/usb/usbdi.h>
129 #include <dev/usb/usbdevs.h>
130 #include <dev/usb/uhidev.h>
132 #include <dev/wscons/wsconsio.h>
133 #include <dev/wscons/wsmousevar.h>
140 /* The amount of data transfered by the USB device. */
141 #define PBMS_DATA_LEN 81
143 /* The maximum number of sensors. */
144 #define PBMS_X_SENSORS 26
145 #define PBMS_Y_SENSORS 16
146 #define PBMS_SENSORS (PBMS_X_SENSORS + PBMS_Y_SENSORS)
149 * Parameters for supported devices. For generality, these parameters
150 * can be different for each device. The meanings of the parameters
153 * desc: A printable description used for dmesg output.
155 * noise: Amount of noise in the computed position. This controls
156 * how large a change must be to get reported, and how
157 * large enough changes are smoothed. A good value can
158 * probably only be found experimentally, but something around
161 * product: The product ID of the trackpad.
164 * threshold: Accumulated changes less than this are ignored. A good
165 * value could be determined experimentally, but 5 is a
168 * vendor: The vendor ID. Currently USB_VENDOR_APPLE for all devices.
170 * x_factor: Factor used in computations with X-coordinates. If the
171 * x-resolution of the display is x, this should be
172 * (x + 1) / (x_sensors - 1). Other values work fine, but
173 * then the aspect ratio is not necessarily kept.
175 * x_sensors: The number of sensors in the X-direction.
177 * y_factor: As x_factors, but for Y-coordinates.
179 * y_sensors: The number of sensors in the Y-direction.
183 const char *descr
; /* Description of the driver (for dmesg). */
184 int noise
; /* Amount of noise in the computed position. */
185 int threshold
; /* Changes less than this are ignored. */
186 int x_factor
; /* Factor used in computation with X-coordinates. */
187 int x_sensors
; /* The number of X-sensors. */
188 int y_factor
; /* Factor used in computation with Y-coordinates. */
189 int y_sensors
; /* The number of Y-sensors. */
190 uint16_t product
; /* Product ID. */
191 uint16_t vendor
; /* The vendor ID. */
194 /* Devices supported by this driver. */
195 static struct pbms_dev pbms_devices
[] =
197 #define POWERBOOK_TOUCHPAD(inches, prod, x_fact, x_sens, y_fact) \
199 .descr = #inches " inch PowerBook Trackpad", \
200 .vendor = USB_VENDOR_APPLE, \
204 .x_factor = (x_fact), \
205 .x_sensors = (x_sens), \
206 .y_factor = (y_fact), \
209 /* 12 inch PowerBooks */
210 POWERBOOK_TOUCHPAD(12, 0x030a, 69, 16, 52), /* XXX Not tested. */
211 /* 15 inch PowerBooks */
212 POWERBOOK_TOUCHPAD(15, 0x020e, 85, 16, 57), /* XXX Not tested. */
213 POWERBOOK_TOUCHPAD(15, 0x020f, 85, 16, 57),
214 POWERBOOK_TOUCHPAD(15, 0x0215, 90, 15, 107),
215 /* 17 inch PowerBooks */
216 POWERBOOK_TOUCHPAD(17, 0x020d, 71, 26, 68) /* XXX Not tested. */
217 #undef POWERBOOK_TOUCHPAD
220 /* The number of supported devices. */
221 #define PBMS_NUM_DEVICES (sizeof(pbms_devices) / sizeof(pbms_devices[0]))
225 * Types and prototypes.
231 struct uhidev sc_hdev
; /* USB parent (got the struct device). */
234 int sc_acc
[PBMS_SENSORS
]; /* Accumulated sensor values. */
235 unsigned char sc_prev
[PBMS_SENSORS
]; /* Previous sample. */
236 unsigned char sc_sample
[PBMS_SENSORS
]; /* Current sample. */
237 device_t sc_wsmousedev
; /* WSMouse device. */
238 int sc_noise
; /* Amount of noise. */
239 int sc_theshold
; /* Threshold value. */
240 int sc_x
; /* Virtual position in horizontal
241 * direction (wsmouse position). */
242 int sc_x_factor
; /* X-coordinate factor. */
243 int sc_x_raw
; /* X-position of finger on trackpad. */
244 int sc_x_sensors
; /* Number of X-sensors. */
245 int sc_y
; /* Virtual position in vertical direction
246 * (wsmouse position). */
247 int sc_y_factor
; /* Y-coordinate factor. */
248 int sc_y_raw
; /* Y-position of finger on trackpad. */
249 int sc_y_sensors
; /* Number of Y-sensors. */
250 uint32_t sc_buttons
; /* Button state. */
251 uint32_t sc_status
; /* Status flags. */
252 #define PBMS_ENABLED 1 /* Is the device enabled? */
253 #define PBMS_DYING 2 /* Is the device dying? */
254 #define PBMS_VALID 4 /* Is the previous sample valid? */
258 /* Static function prototypes. */
259 static void pbms_intr(struct uhidev
*, void *, unsigned int);
260 static int pbms_enable(void *);
261 static void pbms_disable(void *);
262 static int pbms_ioctl(void *, unsigned long, void *, int, struct lwp
*);
263 static void reorder_sample(struct pbms_softc
*, unsigned char *, unsigned char *);
264 static int compute_delta(struct pbms_softc
*, int *, int *, int *, uint32_t *);
265 static int detect_pos(int *, int, int, int, int *, int *);
266 static int smooth_pos(int, int, int);
268 /* Access methods for wsmouse. */
269 const struct wsmouse_accessops pbms_accessops
= {
275 /* This take cares also of the basic device registration. */
276 int pbms_match(device_t
, cfdata_t
, void *);
277 void pbms_attach(device_t
, device_t
, void *);
278 int pbms_detach(device_t
, int);
279 void pbms_childdet(device_t
, device_t
);
280 int pbms_activate(device_t
, enum devact
);
281 extern struct cfdriver pbms_cd
;
282 CFATTACH_DECL2_NEW(pbms
, sizeof(struct pbms_softc
), pbms_match
, pbms_attach
,
283 pbms_detach
, pbms_activate
, NULL
, pbms_childdet
);
290 /* Try to match the device at some uhidev. */
293 pbms_match(device_t parent
, cfdata_t match
, void *aux
)
295 struct uhidev_attach_arg
*uha
= aux
;
296 usb_device_descriptor_t
*udd
;
298 uint16_t vendor
, product
;
301 * We just check if the vendor and product IDs have the magic numbers
304 if ((udd
= usbd_get_device_descriptor(uha
->parent
->sc_udev
)) != NULL
) {
305 vendor
= UGETW(udd
->idVendor
);
306 product
= UGETW(udd
->idProduct
);
307 for (i
= 0; i
< PBMS_NUM_DEVICES
; i
++) {
308 if (vendor
== pbms_devices
[i
].vendor
&&
309 product
== pbms_devices
[i
].product
)
310 return UMATCH_IFACECLASS
;
317 /* Attach the device. */
320 pbms_attach(device_t parent
, device_t self
, void *aux
)
322 struct wsmousedev_attach_args a
;
323 struct uhidev_attach_arg
*uha
= aux
;
325 struct pbms_softc
*sc
= device_private(self
);
326 usb_device_descriptor_t
*udd
;
328 uint16_t vendor
, product
;
330 sc
->sc_hdev
.sc_intr
= pbms_intr
;
331 sc
->sc_hdev
.sc_parent
= uha
->parent
;
332 sc
->sc_hdev
.sc_report_id
= uha
->reportid
;
335 sc
->sc_datalen
= PBMS_DATA_LEN
;
337 /* Fill in device-specific parameters. */
338 if ((udd
= usbd_get_device_descriptor(uha
->parent
->sc_udev
)) != NULL
) {
339 product
= UGETW(udd
->idProduct
);
340 vendor
= UGETW(udd
->idVendor
);
341 for (i
= 0; i
< PBMS_NUM_DEVICES
; i
++) {
342 pd
= &pbms_devices
[i
];
343 if (product
== pd
->product
&& vendor
== pd
->vendor
) {
344 printf(": %s\n", pd
->descr
);
345 sc
->sc_noise
= pd
->noise
;
346 sc
->sc_theshold
= pd
->threshold
;
347 sc
->sc_x_factor
= pd
->x_factor
;
348 sc
->sc_x_sensors
= pd
->x_sensors
;
349 sc
->sc_y_factor
= pd
->y_factor
;
350 sc
->sc_y_sensors
= pd
->y_sensors
;
351 if (product
== 0x0215) {
354 sc
->sc_y_sensors
= 9;
360 KASSERT(0 <= sc
->sc_x_sensors
&& sc
->sc_x_sensors
<= PBMS_X_SENSORS
);
361 KASSERT(0 <= sc
->sc_y_sensors
&& sc
->sc_y_sensors
<= PBMS_Y_SENSORS
);
365 a
.accessops
= &pbms_accessops
;
368 sc
->sc_wsmousedev
= config_found(self
, &a
, wsmousedevprint
);
370 USB_ATTACH_SUCCESS_RETURN
;
374 /* Detach the device. */
377 pbms_childdet(device_t self
, device_t child
)
379 struct pbms_softc
*sc
= device_private(self
);
381 if (sc
->sc_wsmousedev
== child
)
382 sc
->sc_wsmousedev
= NULL
;
386 pbms_detach(device_t self
, int flags
)
388 /* XXX This could not possibly be sufficient! */
389 return config_detach_children(self
, flags
);
393 /* Activate the device. */
396 pbms_activate(device_t self
, enum devact act
)
398 struct pbms_softc
*sc
= device_private(self
);
400 if (act
!= DVACT_DEACTIVATE
)
403 sc
->sc_status
|= PBMS_DYING
;
408 /* Enable the device. */
413 struct pbms_softc
*sc
= v
;
415 /* Check that we are not detaching or already enabled. */
416 if (sc
->sc_status
& PBMS_DYING
)
418 if (sc
->sc_status
& PBMS_ENABLED
)
421 sc
->sc_status
|= PBMS_ENABLED
;
422 sc
->sc_status
&= ~PBMS_VALID
;
424 memset(sc
->sc_sample
, 0, sizeof(sc
->sc_sample
));
426 return uhidev_open(&sc
->sc_hdev
);
430 /* Disable the device. */
433 pbms_disable(void *v
)
435 struct pbms_softc
*sc
= v
;
437 if (!(sc
->sc_status
& PBMS_ENABLED
))
440 sc
->sc_status
&= ~PBMS_ENABLED
;
441 uhidev_close(&sc
->sc_hdev
);
445 /* XXX ioctl not implemented. */
448 pbms_ioctl(void *v
, unsigned long cmd
, void *data
, int flag
, struct lwp
*p
)
455 * Interrupts & pointer movement.
459 /* Handle interrupts. */
462 pbms_intr(struct uhidev
*addr
, void *ibuf
, unsigned int len
)
464 struct pbms_softc
*sc
= (struct pbms_softc
*)addr
;
466 int dx
, dy
, dz
, i
, s
;
469 /* Ignore incomplete data packets. */
470 if (len
!= sc
->sc_datalen
)
476 for (i
= 0; i
< len
; i
++)
477 printf(" %d", data
[i
]);
481 /* The last byte is 1 if the button is pressed and 0 otherwise. */
482 buttons
= !!data
[sc
->sc_datalen
- 1];
484 /* Everything below assumes that the sample is reordered. */
485 reorder_sample(sc
, sc
->sc_sample
, data
);
487 /* Is this the first sample? */
488 if (!(sc
->sc_status
& PBMS_VALID
)) {
489 sc
->sc_status
|= PBMS_VALID
;
490 sc
->sc_x
= sc
->sc_y
= -1;
491 sc
->sc_x_raw
= sc
->sc_y_raw
= -1;
492 memcpy(sc
->sc_prev
, sc
->sc_sample
, sizeof(sc
->sc_prev
));
493 memset(sc
->sc_acc
, 0, sizeof(sc
->sc_acc
));
496 /* Accumulate the sensor change while keeping it nonnegative. */
497 for (i
= 0; i
< PBMS_SENSORS
; i
++) {
499 (signed char) (sc
->sc_sample
[i
] - sc
->sc_prev
[i
]);
500 if (sc
->sc_acc
[i
] < 0)
503 memcpy(sc
->sc_prev
, sc
->sc_sample
, sizeof(sc
->sc_prev
));
505 /* Compute change. */
507 if (!compute_delta(sc
, &dx
, &dy
, &dz
, &buttons
))
510 /* Report to wsmouse. */
511 if ((dx
!= 0 || dy
!= 0 || dz
!= 0 || buttons
!= sc
->sc_buttons
) &&
512 sc
->sc_wsmousedev
!= NULL
) {
514 wsmouse_input(sc
->sc_wsmousedev
, buttons
, dx
, -dy
, dz
, 0,
515 WSMOUSE_INPUT_DELTA
);
518 sc
->sc_buttons
= buttons
;
523 * Reorder the sensor values so that all the X-sensors are before the
524 * Y-sensors in the natural order. Note that this might have to be
525 * rewritten if PBMS_X_SENSORS or PBMS_Y_SENSORS change.
529 reorder_sample(struct pbms_softc
*sc
, unsigned char *to
, unsigned char *from
)
533 if (sc
->is_geyser2
) {
536 memset(to
, 0, PBMS_SENSORS
);
537 for (i
= 0, j
= 19; i
< 20; i
+= 2, j
+= 3) {
539 to
[i
+ 1] = from
[j
+ 1];
541 for (i
= 0, j
= 1; i
< 9; i
+= 2, j
+= 3) {
542 to
[PBMS_X_SENSORS
+ i
] = from
[j
];
543 to
[PBMS_X_SENSORS
+ i
+ 1] = from
[j
+ 1];
546 for (i
= 0; i
< 8; i
++) {
548 to
[i
] = from
[5 * i
+ 2];
549 to
[i
+ 8] = from
[5 * i
+ 4];
550 to
[i
+ 16] = from
[5 * i
+ 42];
553 * XXX This seems to introduce random ventical jumps, so
554 * we ignore these sensors until we figure out their meaning.
557 to
[i
+ 24] = from
[5 * i
+ 44];
560 to
[i
+ 26] = from
[5 * i
+ 1];
561 to
[i
+ 34] = from
[5 * i
+ 3];
568 * Compute the change in x, y and z direction, update the button state
569 * (to simulate more than one button, scrolling etc.), and update the
570 * history. Note that dx, dy, dz and buttons are modified only if
571 * corresponding pressure is detected and should thus be initialised
572 * before the call. Return 0 on error.
575 /* XXX Could we report something useful in dz? */
578 compute_delta(struct pbms_softc
*sc
, int *dx
, int *dy
, int *dz
,
581 int x_det
, y_det
, x_raw
, y_raw
, x_fingers
, y_fingers
, fingers
, x
, y
;
583 x_det
= detect_pos(sc
->sc_acc
, sc
->sc_x_sensors
, sc
->sc_theshold
,
584 sc
->sc_x_factor
, &x_raw
, &x_fingers
);
585 y_det
= detect_pos(sc
->sc_acc
+ PBMS_X_SENSORS
, sc
->sc_y_sensors
,
586 sc
->sc_theshold
, sc
->sc_y_factor
,
588 fingers
= max(x_fingers
, y_fingers
);
590 /* Check the number of fingers and if we have detected a position. */
592 /* More than one finger detected, resetting. */
593 memset(sc
->sc_acc
, 0, sizeof(sc
->sc_acc
));
594 sc
->sc_x_raw
= sc
->sc_y_raw
= sc
->sc_x
= sc
->sc_y
= -1;
596 } else if (x_det
== 0 && y_det
== 0) {
597 /* No position detected, resetting. */
598 memset(sc
->sc_acc
, 0, sizeof(sc
->sc_acc
));
599 sc
->sc_x_raw
= sc
->sc_y_raw
= sc
->sc_x
= sc
->sc_y
= -1;
600 } else if (x_det
> 0 && y_det
> 0) {
601 /* Smooth position. */
602 if (sc
->sc_x_raw
>= 0) {
603 sc
->sc_x_raw
= (3 * sc
->sc_x_raw
+ x_raw
) / 4;
604 sc
->sc_y_raw
= (3 * sc
->sc_y_raw
+ y_raw
) / 4;
606 * Compute virtual position and change if we already
607 * have a decent position.
610 x
= smooth_pos(sc
->sc_x
, sc
->sc_x_raw
,
612 y
= smooth_pos(sc
->sc_y
, sc
->sc_y_raw
,
619 /* Initialise virtual position. */
620 sc
->sc_x
= sc
->sc_x_raw
;
621 sc
->sc_y
= sc
->sc_y_raw
;
624 /* Initialise raw position. */
625 sc
->sc_x_raw
= x_raw
;
626 sc
->sc_y_raw
= y_raw
;
634 * Compute the new smoothed position from the previous smoothed position
635 * and the raw position.
639 smooth_pos(int pos_old
, int pos_raw
, int noise
)
643 delta
= pos_raw
- pos_old
;
646 /* Too small changes are ignored. */
649 /* A bit larger changes are smoothed. */
652 else if (ad
< 2 * noise
)
655 return pos_old
+ delta
;
660 * Detect the position of the finger. Returns the total pressure.
661 * The position is returned in pos_ret and the number of fingers
662 * is returned in fingers_ret. The position returned in pos_ret
663 * is in [0, (n_sensors - 1) * factor - 1].
667 detect_pos(int *sensors
, int n_sensors
, int threshold
, int fact
,
668 int *pos_ret
, int *fingers_ret
)
673 * Compute the number of fingers, total pressure, and weighted
674 * position of the fingers.
678 for (i
= 0; i
< n_sensors
; i
++) {
679 if (sensors
[i
] >= threshold
) {
680 if (i
== 0 || sensors
[i
- 1] < threshold
)
688 *pos_ret
= w
* fact
/ s
;