appletouch.c 14 KB

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