appletouch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. char phys[64];
  87. struct usb_device * udev; /* usb device */
  88. struct urb * urb; /* usb request block */
  89. signed char * data; /* transferred data */
  90. int open; /* non-zero if opened */
  91. struct input_dev *input; /* input dev */
  92. int valid; /* are the sensors valid ? */
  93. int x_old; /* last reported x/y, */
  94. int y_old; /* used for smoothing */
  95. /* current value of the sensors */
  96. signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
  97. /* last value of the sensors */
  98. signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
  99. /* accumulated sensors */
  100. int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
  101. };
  102. #define dbg_dump(msg, tab) \
  103. if (debug > 1) { \
  104. int i; \
  105. printk("appletouch: %s %lld", msg, (long long)jiffies); \
  106. for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) \
  107. printk(" %02x", tab[i]); \
  108. printk("\n"); \
  109. }
  110. #define dprintk(format, a...) \
  111. do { \
  112. if (debug) printk(format, ##a); \
  113. } while (0)
  114. MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold");
  115. MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver");
  116. MODULE_LICENSE("GPL");
  117. static int debug = 1;
  118. module_param(debug, int, 0644);
  119. MODULE_PARM_DESC(debug, "Activate debugging output");
  120. static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
  121. int *z, int *fingers)
  122. {
  123. int i;
  124. /* values to calculate mean */
  125. int pcum = 0, psum = 0;
  126. *fingers = 0;
  127. for (i = 0; i < nb_sensors; i++) {
  128. if (xy_sensors[i] < ATP_THRESHOLD)
  129. continue;
  130. if ((i - 1 < 0) || (xy_sensors[i - 1] < ATP_THRESHOLD))
  131. (*fingers)++;
  132. pcum += xy_sensors[i] * i;
  133. psum += xy_sensors[i];
  134. }
  135. if (psum > 0) {
  136. *z = psum;
  137. return pcum * fact / psum;
  138. }
  139. return 0;
  140. }
  141. static inline void atp_report_fingers(struct input_dev *input, int fingers)
  142. {
  143. input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
  144. input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
  145. input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
  146. }
  147. static void atp_complete(struct urb* urb, struct pt_regs* regs)
  148. {
  149. int x, y, x_z, y_z, x_f, y_f;
  150. int retval, i;
  151. struct atp *dev = urb->context;
  152. switch (urb->status) {
  153. case 0:
  154. /* success */
  155. break;
  156. case -ECONNRESET:
  157. case -ENOENT:
  158. case -ESHUTDOWN:
  159. /* This urb is terminated, clean up */
  160. dbg("%s - urb shutting down with status: %d",
  161. __FUNCTION__, urb->status);
  162. return;
  163. default:
  164. dbg("%s - nonzero urb status received: %d",
  165. __FUNCTION__, urb->status);
  166. goto exit;
  167. }
  168. /* drop incomplete datasets */
  169. if (dev->urb->actual_length != ATP_DATASIZE) {
  170. dprintk("appletouch: incomplete data package.\n");
  171. goto exit;
  172. }
  173. /* reorder the sensors values */
  174. for (i = 0; i < 8; i++) {
  175. /* X values */
  176. dev->xy_cur[i ] = dev->data[5 * i + 2];
  177. dev->xy_cur[i + 8] = dev->data[5 * i + 4];
  178. dev->xy_cur[i + 16] = dev->data[5 * i + 42];
  179. if (i < 2)
  180. dev->xy_cur[i + 24] = dev->data[5 * i + 44];
  181. /* Y values */
  182. dev->xy_cur[i + 26] = dev->data[5 * i + 1];
  183. dev->xy_cur[i + 34] = dev->data[5 * i + 3];
  184. }
  185. dbg_dump("sample", dev->xy_cur);
  186. if (!dev->valid) {
  187. /* first sample */
  188. dev->valid = 1;
  189. dev->x_old = dev->y_old = -1;
  190. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  191. /* 17" Powerbooks have 10 extra X sensors */
  192. for (i = 16; i < ATP_XSENSORS; i++)
  193. if (dev->xy_cur[i]) {
  194. printk("appletouch: 17\" model detected.\n");
  195. input_set_abs_params(dev->input, ABS_X, 0,
  196. (ATP_XSENSORS - 1) *
  197. ATP_XFACT - 1,
  198. ATP_FUZZ, 0);
  199. break;
  200. }
  201. goto exit;
  202. }
  203. for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
  204. /* accumulate the change */
  205. signed char change = dev->xy_old[i] - dev->xy_cur[i];
  206. dev->xy_acc[i] -= change;
  207. /* prevent down drifting */
  208. if (dev->xy_acc[i] < 0)
  209. dev->xy_acc[i] = 0;
  210. }
  211. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  212. dbg_dump("accumulator", dev->xy_acc);
  213. x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
  214. ATP_XFACT, &x_z, &x_f);
  215. y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
  216. ATP_YFACT, &y_z, &y_f);
  217. if (x && y) {
  218. if (dev->x_old != -1) {
  219. x = (dev->x_old * 3 + x) >> 2;
  220. y = (dev->y_old * 3 + y) >> 2;
  221. dev->x_old = x;
  222. dev->y_old = y;
  223. if (debug > 1)
  224. printk("appletouch: X: %3d Y: %3d "
  225. "Xz: %3d Yz: %3d\n",
  226. x, y, x_z, y_z);
  227. input_report_key(dev->input, BTN_TOUCH, 1);
  228. input_report_abs(dev->input, ABS_X, x);
  229. input_report_abs(dev->input, ABS_Y, y);
  230. input_report_abs(dev->input, ABS_PRESSURE,
  231. min(ATP_PRESSURE, x_z + y_z));
  232. atp_report_fingers(dev->input, max(x_f, y_f));
  233. }
  234. dev->x_old = x;
  235. dev->y_old = y;
  236. }
  237. else if (!x && !y) {
  238. dev->x_old = dev->y_old = -1;
  239. input_report_key(dev->input, BTN_TOUCH, 0);
  240. input_report_abs(dev->input, ABS_PRESSURE, 0);
  241. atp_report_fingers(dev->input, 0);
  242. /* reset the accumulator on release */
  243. memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
  244. }
  245. input_report_key(dev->input, BTN_LEFT, !!dev->data[80]);
  246. input_sync(dev->input);
  247. exit:
  248. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  249. if (retval) {
  250. err("%s - usb_submit_urb failed with result %d",
  251. __FUNCTION__, retval);
  252. }
  253. }
  254. static int atp_open(struct input_dev *input)
  255. {
  256. struct atp *dev = input->private;
  257. if (usb_submit_urb(dev->urb, GFP_ATOMIC))
  258. return -EIO;
  259. dev->open = 1;
  260. return 0;
  261. }
  262. static void atp_close(struct input_dev *input)
  263. {
  264. struct atp *dev = input->private;
  265. usb_kill_urb(dev->urb);
  266. dev->open = 0;
  267. }
  268. static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id)
  269. {
  270. struct atp *dev;
  271. struct input_dev *input_dev;
  272. struct usb_device *udev = interface_to_usbdev(iface);
  273. struct usb_host_interface *iface_desc;
  274. struct usb_endpoint_descriptor *endpoint;
  275. int int_in_endpointAddr = 0;
  276. int i, retval = -ENOMEM;
  277. /* set up the endpoint information */
  278. /* use only the first interrupt-in endpoint */
  279. iface_desc = iface->cur_altsetting;
  280. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  281. endpoint = &iface_desc->endpoint[i].desc;
  282. if (!int_in_endpointAddr &&
  283. (endpoint->bEndpointAddress & USB_DIR_IN) &&
  284. ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  285. == USB_ENDPOINT_XFER_INT)) {
  286. /* we found an interrupt in endpoint */
  287. int_in_endpointAddr = endpoint->bEndpointAddress;
  288. break;
  289. }
  290. }
  291. if (!int_in_endpointAddr) {
  292. err("Could not find int-in endpoint");
  293. return -EIO;
  294. }
  295. /* allocate memory for our device state and initialize it */
  296. dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
  297. input_dev = input_allocate_device();
  298. if (!dev || !input_dev) {
  299. err("Out of memory");
  300. goto err_free_devs;
  301. }
  302. dev->udev = udev;
  303. dev->input = input_dev;
  304. dev->urb = usb_alloc_urb(0, GFP_KERNEL);
  305. if (!dev->urb) {
  306. retval = -ENOMEM;
  307. goto err_free_devs;
  308. }
  309. dev->data = usb_buffer_alloc(dev->udev, ATP_DATASIZE, GFP_KERNEL,
  310. &dev->urb->transfer_dma);
  311. if (!dev->data) {
  312. retval = -ENOMEM;
  313. goto err_free_urb;
  314. }
  315. usb_fill_int_urb(dev->urb, udev,
  316. usb_rcvintpipe(udev, int_in_endpointAddr),
  317. dev->data, ATP_DATASIZE, atp_complete, dev, 1);
  318. usb_make_path(udev, dev->phys, sizeof(dev->phys));
  319. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  320. input_dev->name = "appletouch";
  321. input_dev->phys = dev->phys;
  322. usb_to_input_id(dev->udev, &input_dev->id);
  323. input_dev->cdev.dev = &iface->dev;
  324. input_dev->private = dev;
  325. input_dev->open = atp_open;
  326. input_dev->close = atp_close;
  327. set_bit(EV_ABS, input_dev->evbit);
  328. /*
  329. * 12" and 15" Powerbooks only have 16 x sensors,
  330. * 17" models are detected later.
  331. */
  332. input_set_abs_params(input_dev, ABS_X, 0,
  333. (16 - 1) * ATP_XFACT - 1, ATP_FUZZ, 0);
  334. input_set_abs_params(input_dev, ABS_Y, 0,
  335. (ATP_YSENSORS - 1) * ATP_YFACT - 1, ATP_FUZZ, 0);
  336. input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
  337. set_bit(EV_KEY, input_dev->evbit);
  338. set_bit(BTN_TOUCH, input_dev->keybit);
  339. set_bit(BTN_TOOL_FINGER, input_dev->keybit);
  340. set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
  341. set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
  342. set_bit(BTN_LEFT, input_dev->keybit);
  343. input_register_device(dev->input);
  344. /* save our data pointer in this interface device */
  345. usb_set_intfdata(iface, dev);
  346. return 0;
  347. err_free_urb:
  348. usb_free_urb(dev->urb);
  349. err_free_devs:
  350. usb_set_intfdata(iface, NULL);
  351. kfree(dev);
  352. input_free_device(input_dev);
  353. return retval;
  354. }
  355. static void atp_disconnect(struct usb_interface *iface)
  356. {
  357. struct atp *dev = usb_get_intfdata(iface);
  358. usb_set_intfdata(iface, NULL);
  359. if (dev) {
  360. usb_kill_urb(dev->urb);
  361. input_unregister_device(dev->input);
  362. usb_free_urb(dev->urb);
  363. usb_buffer_free(dev->udev, ATP_DATASIZE,
  364. dev->data, dev->urb->transfer_dma);
  365. kfree(dev);
  366. }
  367. printk(KERN_INFO "input: appletouch disconnected\n");
  368. }
  369. static int atp_suspend(struct usb_interface *iface, pm_message_t message)
  370. {
  371. struct atp *dev = usb_get_intfdata(iface);
  372. usb_kill_urb(dev->urb);
  373. dev->valid = 0;
  374. return 0;
  375. }
  376. static int atp_resume(struct usb_interface *iface)
  377. {
  378. struct atp *dev = usb_get_intfdata(iface);
  379. if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
  380. return -EIO;
  381. return 0;
  382. }
  383. static struct usb_driver atp_driver = {
  384. .name = "appletouch",
  385. .probe = atp_probe,
  386. .disconnect = atp_disconnect,
  387. .suspend = atp_suspend,
  388. .resume = atp_resume,
  389. .id_table = atp_table,
  390. };
  391. static int __init atp_init(void)
  392. {
  393. return usb_register(&atp_driver);
  394. }
  395. static void __exit atp_exit(void)
  396. {
  397. usb_deregister(&atp_driver);
  398. }
  399. module_init(atp_init);
  400. module_exit(atp_exit);