31 #include <linux/kernel.h>
32 #include <linux/errno.h>
34 #include <linux/slab.h>
35 #include <linux/module.h>
54 static void atp_complete_geyser_1_2(
struct urb *
urb);
55 static void atp_complete_geyser_3_4(
struct urb *
urb);
57 static const struct atp_info fountain_info = {
64 .callback = atp_complete_geyser_1_2,
67 static const struct atp_info geyser1_info = {
74 .callback = atp_complete_geyser_1_2,
77 static const struct atp_info geyser2_info = {
84 .callback = atp_complete_geyser_1_2,
87 static const struct atp_info geyser3_info = {
93 .callback = atp_complete_geyser_3_4,
96 static const struct atp_info geyser4_info = {
102 .callback = atp_complete_geyser_3_4,
105 #define ATP_DEVICE(prod, info) \
107 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
108 USB_DEVICE_ID_MATCH_INT_CLASS | \
109 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
110 .idVendor = 0x05ac, \
111 .idProduct = (prod), \
112 .bInterfaceClass = 0x03, \
113 .bInterfaceProtocol = 0x02, \
114 .driver_info = (unsigned long) &info, \
156 #define ATP_XSENSORS 26
157 #define ATP_YSENSORS 16
163 #define ATP_PRESSURE 300
169 #define ATP_THRESHOLD 5
172 #define ATP_GEYSER_MODE_READ_REQUEST_ID 1
173 #define ATP_GEYSER_MODE_WRITE_REQUEST_ID 9
174 #define ATP_GEYSER_MODE_REQUEST_VALUE 0x300
175 #define ATP_GEYSER_MODE_REQUEST_INDEX 0
176 #define ATP_GEYSER_MODE_VENDOR_VALUE 0x04
216 #define dbg_dump(msg, tab) \
219 printk(KERN_DEBUG "appletouch: %s", msg); \
220 for (__i = 0; __i < ATP_XSENSORS + ATP_YSENSORS; __i++) \
221 printk(" %02x", tab[__i]); \
225 #define dprintk(format, a...) \
228 printk(KERN_DEBUG format, ##a); \
245 " (the trackpad has many of these sensors)"
246 " less than this value.");
257 static int atp_geyser_init(
struct atp *
dev)
259 struct usb_device *
udev = dev->
udev;
278 dprintk(
"atp_geyser_init: read error\n");
279 for (i = 0; i < 8; i++)
280 dprintk(
"appletouch[%d]: %d\n", i, data[i]);
282 dev_err(&dev->
intf->dev,
"Failed to read mode from device.\n");
297 dprintk(
"atp_geyser_init: write error\n");
298 for (i = 0; i < 8; i++)
299 dprintk(
"appletouch[%d]: %d\n", i, data[i]);
301 dev_err(&dev->
intf->dev,
"Failed to request geyser raw mode\n");
320 dprintk(
"appletouch: putting appletouch to sleep (reinit)\n");
321 atp_geyser_init(dev);
326 "atp_reinit: usb_submit_urb failed with error %d\n",
330 static int atp_calculate_abs(
int *xy_sensors,
int nb_sensors,
int fact,
331 int *z,
int *fingers)
335 int pcum = 0, psum = 0;
336 int is_increasing = 0;
340 for (i = 0; i < nb_sensors; i++) {
363 (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) {
366 }
else if (i > 0 && (xy_sensors[i - 1] - xy_sensors[i] >
threshold)) {
383 return pcum * fact / psum;
389 static inline void atp_report_fingers(
struct input_dev *
input,
int fingers)
398 #define ATP_URB_STATUS_SUCCESS 0
399 #define ATP_URB_STATUS_ERROR 1
400 #define ATP_URB_STATUS_ERROR_FATAL 2
402 static int atp_status_check(
struct urb *
urb)
404 struct atp *dev = urb->context;
407 switch (urb->status) {
414 "appletouch: OVERFLOW with data length %d, actual length is %d\n",
415 dev->
info->datalen, dev->
urb->actual_length);
423 "atp_complete: urb shutting down with status: %d\n",
429 "atp_complete: nonzero urb status received: %d\n",
435 if (dev->
urb->actual_length != dev->
info->datalen) {
436 dprintk(
"appletouch: incomplete data package"
437 " (first byte: %d, length: %d).\n",
438 dev->
data[0], dev->
urb->actual_length);
445 static void atp_detect_size(
struct atp *dev)
454 "appletouch: 17\" model detected.\n");
457 (dev->
info->xsensors_17 - 1) *
458 dev->
info->xfact - 1,
471 static void atp_complete_geyser_1_2(
struct urb *
urb)
473 int x,
y, x_z, y_z, x_f, y_f;
476 struct atp *dev = urb->context;
477 int status = atp_status_check(urb);
485 if (dev->
info == &geyser2_info) {
495 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
501 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
506 for (i = 0; i < 8; i++) {
532 atp_detect_size(dev);
553 dev->
info->xfact, &x_z, &x_f);
555 dev->
info->yfact, &y_z, &y_f);
559 if (dev->
x_old != -1) {
560 x = (dev->
x_old * 3 +
x) >> 2;
561 y = (dev->
y_old * 3 +
y) >> 2;
567 "X: %3d Y: %3d Xz: %3d Yz: %3d\n",
575 atp_report_fingers(dev->
input,
max(x_f, y_f));
580 }
else if (!x && !y) {
585 atp_report_fingers(dev->
input, 0);
592 input_sync(dev->
input);
598 "atp_complete: usb_submit_urb failed with result %d\n",
604 static void atp_complete_geyser_3_4(
struct urb *urb)
606 int x,
y, x_z, y_z, x_f, y_f;
609 struct atp *dev = urb->context;
610 int status = atp_status_check(urb);
625 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
630 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
640 dprintk(
"appletouch: updated base values\n");
654 if (dev->
xy_acc[i] < -127)
665 dev->
info->xfact, &x_z, &x_f);
667 dev->
info->yfact, &y_z, &y_f);
671 if (dev->
x_old != -1) {
672 x = (dev->
x_old * 3 +
x) >> 2;
673 y = (dev->
y_old * 3 +
y) >> 2;
687 atp_report_fingers(dev->
input,
max(x_f, y_f));
692 }
else if (!x && !y) {
697 atp_report_fingers(dev->
input, 0);
704 input_sync(dev->
input);
717 if (!x && !y && !key) {
733 "atp_complete: usb_submit_urb failed with result %d\n",
737 static int atp_open(
struct input_dev *
input)
739 struct atp *dev = input_get_drvdata(input);
748 static void atp_close(
struct input_dev *input)
750 struct atp *dev = input_get_drvdata(input);
757 static int atp_handle_geyser(
struct atp *dev)
759 if (dev->
info != &fountain_info) {
761 if (atp_geyser_init(dev))
764 dev_info(&dev->
intf->dev,
"Geyser mode initialized.\n");
774 struct input_dev *input_dev;
775 struct usb_device *udev = interface_to_usbdev(iface);
776 struct usb_host_interface *iface_desc;
778 int int_in_endpointAddr = 0;
784 iface_desc = iface->cur_altsetting;
785 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
786 endpoint = &iface_desc->endpoint[
i].desc;
787 if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
793 if (!int_in_endpointAddr) {
794 dev_err(&iface->dev,
"Could not find int-in endpoint\n");
800 input_dev = input_allocate_device();
801 if (!dev || !input_dev) {
802 dev_err(&iface->dev,
"Out of memory\n");
808 dev->
input = input_dev;
817 &dev->
urb->transfer_dma);
821 usb_fill_int_urb(dev->
urb, udev,
822 usb_rcvintpipe(udev, int_in_endpointAddr),
824 dev->
info->callback, dev, 1);
826 error = atp_handle_geyser(dev);
828 goto err_free_buffer;
830 usb_make_path(udev, dev->
phys,
sizeof(dev->
phys));
833 input_dev->name =
"appletouch";
834 input_dev->phys = dev->
phys;
835 usb_to_input_id(dev->
udev, &input_dev->id);
836 input_dev->dev.parent = &iface->dev;
838 input_set_drvdata(input_dev, dev);
840 input_dev->open = atp_open;
841 input_dev->close = atp_close;
845 input_set_abs_params(input_dev,
ABS_X, 0,
846 (dev->
info->xsensors - 1) * dev->
info->xfact - 1,
848 input_set_abs_params(input_dev,
ABS_Y, 0,
849 (dev->
info->ysensors - 1) * dev->
info->yfact - 1,
860 error = input_register_device(dev->
input);
862 goto err_free_buffer;
865 usb_set_intfdata(iface, dev);
873 dev->
data, dev->
urb->transfer_dma);
877 usb_set_intfdata(iface,
NULL);
879 input_free_device(input_dev);
885 struct atp *dev = usb_get_intfdata(iface);
887 usb_set_intfdata(iface,
NULL);
890 input_unregister_device(dev->
input);
892 dev->
data, dev->
urb->transfer_dma);
896 dev_info(&iface->dev,
"input: appletouch disconnected\n");
899 static int atp_recover(
struct atp *dev)
903 error = atp_handle_geyser(dev);
915 struct atp *dev = usb_get_intfdata(iface);
923 struct atp *dev = usb_get_intfdata(iface);
933 struct atp *dev = usb_get_intfdata(iface);
935 return atp_recover(dev);
938 static struct usb_driver atp_driver = {
939 .name =
"appletouch",
941 .disconnect = atp_disconnect,
942 .suspend = atp_suspend,
943 .resume = atp_resume,
944 .reset_resume = atp_reset_resume,
945 .id_table = atp_table,