appletouch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Apple USB Touchpad (for post-February 2005 PowerBooks) driver
  3. *
  4. * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
  5. * Copyright (C) 2005 Johannes Berg (johannes@sipsolutions.net)
  6. * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
  7. * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de)
  8. * Copyright (C) 2005 Peter Osterlund (petero2@telia.com)
  9. *
  10. * Thanks to Alex Harper <basilisk@foobox.net> for his inputs.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. */
  27. #include <linux/config.h>
  28. #include <linux/kernel.h>
  29. #include <linux/errno.h>
  30. #include <linux/init.h>
  31. #include <linux/slab.h>
  32. #include <linux/module.h>
  33. #include <linux/usb.h>
  34. #include <linux/input.h>
  35. #include <linux/usb_input.h>
  36. /* Apple has powerbooks which have the keyboard with different Product IDs */
  37. #define APPLE_VENDOR_ID 0x05AC
  38. #define ATP_DEVICE(prod) \
  39. .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
  40. USB_DEVICE_ID_MATCH_INT_CLASS | \
  41. USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
  42. .idVendor = APPLE_VENDOR_ID, \
  43. .idProduct = (prod), \
  44. .bInterfaceClass = 0x03, \
  45. .bInterfaceProtocol = 0x02
  46. /* table of devices that work with this driver */
  47. static struct usb_device_id atp_table [] = {
  48. { ATP_DEVICE(0x020E) },
  49. { ATP_DEVICE(0x020F) },
  50. { ATP_DEVICE(0x030A) },
  51. { ATP_DEVICE(0x030B) },
  52. { } /* Terminating entry */
  53. };
  54. MODULE_DEVICE_TABLE (usb, atp_table);
  55. /* size of a USB urb transfer */
  56. #define ATP_DATASIZE 81
  57. /*
  58. * number of sensors. Note that only 16 instead of 26 X (horizontal)
  59. * sensors exist on 12" and 15" PowerBooks. All models have 16 Y
  60. * (vertical) sensors.
  61. */
  62. #define ATP_XSENSORS 26
  63. #define ATP_YSENSORS 16
  64. /* amount of fuzz this touchpad generates */
  65. #define ATP_FUZZ 16
  66. /* maximum pressure this driver will report */
  67. #define ATP_PRESSURE 300
  68. /*
  69. * multiplication factor for the X and Y coordinates.
  70. * We try to keep the touchpad aspect ratio while still doing only simple
  71. * arithmetics.
  72. * The factors below give coordinates like:
  73. * 0 <= x < 960 on 12" and 15" Powerbooks
  74. * 0 <= x < 1600 on 17" Powerbooks
  75. * 0 <= y < 646
  76. */
  77. #define ATP_XFACT 64
  78. #define ATP_YFACT 43
  79. /*
  80. * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
  81. * ignored.
  82. */
  83. #define ATP_THRESHOLD 5
  84. /* Structure to hold all of our device specific stuff */
  85. struct atp {
  86. struct usb_device * udev; /* usb device */
  87. struct urb * urb; /* usb request block */
  88. signed char * data; /* transferred data */
  89. int open; /* non-zero if opened */
  90. struct input_dev input; /* input dev */
  91. int valid; /* are the sensors valid ? */
  92. int x_old; /* last reported x/y, */
  93. int y_old; /* used for smoothing */
  94. /* current value of the sensors */
  95. signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
  96. /* last value of the sensors */
  97. signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
  98. /* accumulated sensors */
  99. int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
  100. };
  101. #define dbg_dump(msg, tab) \
  102. if (debug > 1) { \
  103. int i; \
  104. printk("appletouch: %s %lld", msg, (long long)jiffies); \
  105. for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) \
  106. printk(" %02x", tab[i]); \
  107. printk("\n"); \
  108. }
  109. #define dprintk(format, a...) \
  110. do { \
  111. if (debug) printk(format, ##a); \
  112. } while (0)
  113. MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold");
  114. MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver");
  115. MODULE_LICENSE("GPL");
  116. static int debug = 1;
  117. module_param(debug, int, 0644);
  118. MODULE_PARM_DESC(debug, "Activate debugging output");
  119. static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
  120. int *z, int *fingers)
  121. {
  122. int i;
  123. /* values to calculate mean */
  124. int pcum = 0, psum = 0;
  125. *fingers = 0;
  126. for (i = 0; i < nb_sensors; i++) {
  127. if (xy_sensors[i] < ATP_THRESHOLD)
  128. continue;
  129. if ((i - 1 < 0) || (xy_sensors[i - 1] < ATP_THRESHOLD))
  130. (*fingers)++;
  131. pcum += xy_sensors[i] * i;
  132. psum += xy_sensors[i];
  133. }
  134. if (psum > 0) {
  135. *z = psum;
  136. return pcum * fact / psum;
  137. }
  138. return 0;
  139. }
  140. static inline void atp_report_fingers(struct input_dev *input, int fingers)
  141. {
  142. input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
  143. input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
  144. input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
  145. }
  146. static void atp_complete(struct urb* urb, struct pt_regs* regs)
  147. {
  148. int x, y, x_z, y_z, x_f, y_f;
  149. int retval, i;
  150. struct atp *dev = urb->context;
  151. switch (urb->status) {
  152. case 0:
  153. /* success */
  154. break;
  155. case -ECONNRESET:
  156. case -ENOENT:
  157. case -ESHUTDOWN:
  158. /* This urb is terminated, clean up */
  159. dbg("%s - urb shutting down with status: %d",
  160. __FUNCTION__, urb->status);
  161. return;
  162. default:
  163. dbg("%s - nonzero urb status received: %d",
  164. __FUNCTION__, urb->status);
  165. goto exit;
  166. }
  167. /* drop incomplete datasets */
  168. if (dev->urb->actual_length != ATP_DATASIZE) {
  169. dprintk("appletouch: incomplete data package.\n");
  170. goto exit;
  171. }
  172. /* reorder the sensors values */
  173. for (i = 0; i < 8; i++) {
  174. /* X values */
  175. dev->xy_cur[i ] = dev->data[5 * i + 2];
  176. dev->xy_cur[i + 8] = dev->data[5 * i + 4];
  177. dev->xy_cur[i + 16] = dev->data[5 * i + 42];
  178. if (i < 2)
  179. dev->xy_cur[i + 24] = dev->data[5 * i + 44];
  180. /* Y values */
  181. dev->xy_cur[i + 26] = dev->data[5 * i + 1];
  182. dev->xy_cur[i + 34] = dev->data[5 * i + 3];
  183. }
  184. dbg_dump("sample", dev->xy_cur);
  185. if (!dev->valid) {
  186. /* first sample */
  187. dev->valid = 1;
  188. dev->x_old = dev->y_old = -1;
  189. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  190. /* 17" Powerbooks have 10 extra X sensors */
  191. for (i = 16; i < ATP_XSENSORS; i++)
  192. if (dev->xy_cur[i]) {
  193. printk("appletouch: 17\" model detected.\n");
  194. input_set_abs_params(&dev->input, ABS_X, 0,
  195. (ATP_XSENSORS - 1) *
  196. ATP_XFACT - 1,
  197. ATP_FUZZ, 0);
  198. break;
  199. }
  200. goto exit;
  201. }
  202. for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
  203. /* accumulate the change */
  204. signed char change = dev->xy_old[i] - dev->xy_cur[i];
  205. dev->xy_acc[i] -= change;
  206. /* prevent down drifting */
  207. if (dev->xy_acc[i] < 0)
  208. dev->xy_acc[i] = 0;
  209. }
  210. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  211. dbg_dump("accumulator", dev->xy_acc);
  212. x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
  213. ATP_XFACT, &x_z, &x_f);
  214. y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
  215. ATP_YFACT, &y_z, &y_f);
  216. if (x && y) {
  217. if (dev->x_old != -1) {
  218. x = (dev->x_old * 3 + x) >> 2;
  219. y = (dev->y_old * 3 + y) >> 2;
  220. dev->x_old = x;
  221. dev->y_old = y;
  222. if (debug > 1)
  223. printk("appletouch: X: %3d Y: %3d "
  224. "Xz: %3d Yz: %3d\n",
  225. x, y, x_z, y_z);
  226. input_report_key(&dev->input, BTN_TOUCH, 1);
  227. input_report_abs(&dev->input, ABS_X, x);
  228. input_report_abs(&dev->input, ABS_Y, y);
  229. input_report_abs(&dev->input, ABS_PRESSURE,
  230. min(ATP_PRESSURE, x_z + y_z));
  231. atp_report_fingers(&dev->input, max(x_f, y_f));
  232. }
  233. dev->x_old = x;
  234. dev->y_old = y;
  235. }
  236. else if (!x && !y) {
  237. dev->x_old = dev->y_old = -1;
  238. input_report_key(&dev->input, BTN_TOUCH, 0);
  239. input_report_abs(&dev->input, ABS_PRESSURE, 0);
  240. atp_report_fingers(&dev->input, 0);
  241. /* reset the accumulator on release */
  242. memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
  243. }
  244. input_report_key(&dev->input, BTN_LEFT, !!dev->data[80]);
  245. input_sync(&dev->input);
  246. exit:
  247. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  248. if (retval) {
  249. err("%s - usb_submit_urb failed with result %d",
  250. __FUNCTION__, retval);
  251. }
  252. }
  253. static int atp_open(struct input_dev *input)
  254. {
  255. struct atp *dev = input->private;
  256. if (usb_submit_urb(dev->urb, GFP_ATOMIC))
  257. return -EIO;
  258. dev->open = 1;
  259. return 0;
  260. }
  261. static void atp_close(struct input_dev *input)
  262. {
  263. struct atp *dev = input->private;
  264. usb_kill_urb(dev->urb);
  265. dev->open = 0;
  266. }
  267. static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id)
  268. {
  269. struct atp *dev = NULL;
  270. struct usb_host_interface *iface_desc;
  271. struct usb_endpoint_descriptor *endpoint;
  272. int int_in_endpointAddr = 0;
  273. int i, retval = -ENOMEM;
  274. /* allocate memory for our device state and initialize it */
  275. dev = kmalloc(sizeof(struct atp), GFP_KERNEL);
  276. if (dev == NULL) {
  277. err("Out of memory");
  278. goto err_kmalloc;
  279. }
  280. memset(dev, 0, sizeof(struct atp));
  281. dev->udev = interface_to_usbdev(iface);
  282. /* set up the endpoint information */
  283. /* use only the first interrupt-in endpoint */
  284. iface_desc = iface->cur_altsetting;
  285. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  286. endpoint = &iface_desc->endpoint[i].desc;
  287. if (!int_in_endpointAddr &&
  288. (endpoint->bEndpointAddress & USB_DIR_IN) &&
  289. ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  290. == USB_ENDPOINT_XFER_INT)) {
  291. /* we found an interrupt in endpoint */
  292. int_in_endpointAddr = endpoint->bEndpointAddress;
  293. break;
  294. }
  295. }
  296. if (!int_in_endpointAddr) {
  297. retval = -EIO;
  298. err("Could not find int-in endpoint");
  299. goto err_endpoint;
  300. }
  301. /* save our data pointer in this interface device */
  302. usb_set_intfdata(iface, dev);
  303. dev->urb = usb_alloc_urb(0, GFP_KERNEL);
  304. if (!dev->urb) {
  305. retval = -ENOMEM;
  306. goto err_usballoc;
  307. }
  308. dev->data = usb_buffer_alloc(dev->udev, ATP_DATASIZE, GFP_KERNEL,
  309. &dev->urb->transfer_dma);
  310. if (!dev->data) {
  311. retval = -ENOMEM;
  312. goto err_usbbufalloc;
  313. }
  314. usb_fill_int_urb(dev->urb, dev->udev,
  315. usb_rcvintpipe(dev->udev, int_in_endpointAddr),
  316. dev->data, ATP_DATASIZE, atp_complete, dev, 1);
  317. init_input_dev(&dev->input);
  318. dev->input.name = "appletouch";
  319. dev->input.dev = &iface->dev;
  320. dev->input.private = dev;
  321. dev->input.open = atp_open;
  322. dev->input.close = atp_close;
  323. usb_to_input_id(dev->udev, &dev->input.id);
  324. set_bit(EV_ABS, dev->input.evbit);
  325. /*
  326. * 12" and 15" Powerbooks only have 16 x sensors,
  327. * 17" models are detected later.
  328. */
  329. input_set_abs_params(&dev->input, ABS_X, 0,
  330. (16 - 1) * ATP_XFACT - 1, ATP_FUZZ, 0);
  331. input_set_abs_params(&dev->input, ABS_Y, 0,
  332. (ATP_YSENSORS - 1) * ATP_YFACT - 1, ATP_FUZZ, 0);
  333. input_set_abs_params(&dev->input, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
  334. set_bit(EV_KEY, dev->input.evbit);
  335. set_bit(BTN_TOUCH, dev->input.keybit);
  336. set_bit(BTN_TOOL_FINGER, dev->input.keybit);
  337. set_bit(BTN_TOOL_DOUBLETAP, dev->input.keybit);
  338. set_bit(BTN_TOOL_TRIPLETAP, dev->input.keybit);
  339. set_bit(BTN_LEFT, dev->input.keybit);
  340. input_register_device(&dev->input);
  341. printk(KERN_INFO "input: appletouch connected\n");
  342. return 0;
  343. err_usbbufalloc:
  344. usb_free_urb(dev->urb);
  345. err_usballoc:
  346. usb_set_intfdata(iface, NULL);
  347. err_endpoint:
  348. kfree(dev);
  349. err_kmalloc:
  350. return retval;
  351. }
  352. static void atp_disconnect(struct usb_interface *iface)
  353. {
  354. struct atp *dev = usb_get_intfdata(iface);
  355. usb_set_intfdata(iface, NULL);
  356. if (dev) {
  357. usb_kill_urb(dev->urb);
  358. input_unregister_device(&dev->input);
  359. usb_free_urb(dev->urb);
  360. usb_buffer_free(dev->udev, ATP_DATASIZE,
  361. dev->data, dev->urb->transfer_dma);
  362. kfree(dev);
  363. }
  364. printk(KERN_INFO "input: appletouch disconnected\n");
  365. }
  366. static int atp_suspend(struct usb_interface *iface, pm_message_t message)
  367. {
  368. struct atp *dev = usb_get_intfdata(iface);
  369. usb_kill_urb(dev->urb);
  370. dev->valid = 0;
  371. return 0;
  372. }
  373. static int atp_resume(struct usb_interface *iface)
  374. {
  375. struct atp *dev = usb_get_intfdata(iface);
  376. if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
  377. return -EIO;
  378. return 0;
  379. }
  380. static struct usb_driver atp_driver = {
  381. .owner = THIS_MODULE,
  382. .name = "appletouch",
  383. .probe = atp_probe,
  384. .disconnect = atp_disconnect,
  385. .suspend = atp_suspend,
  386. .resume = atp_resume,
  387. .id_table = atp_table,
  388. };
  389. static int __init atp_init(void)
  390. {
  391. return usb_register(&atp_driver);
  392. }
  393. static void __exit atp_exit(void)
  394. {
  395. usb_deregister(&atp_driver);
  396. }
  397. module_init(atp_init);
  398. module_exit(atp_exit);