appletouch.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*
  2. * Apple USB Touchpad (for post-February 2005 PowerBooks and MacBooks) driver
  3. *
  4. * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
  5. * Copyright (C) 2005-2008 Johannes Berg (johannes@sipsolutions.net)
  6. * Copyright (C) 2005-2008 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. * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch)
  11. * Copyright (C) 2007-2008 Sven Anders (anders@anduras.de)
  12. *
  13. * Thanks to Alex Harper <basilisk@foobox.net> for his inputs.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. *
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/errno.h>
  32. #include <linux/init.h>
  33. #include <linux/slab.h>
  34. #include <linux/module.h>
  35. #include <linux/usb/input.h>
  36. /*
  37. * Note: We try to keep the touchpad aspect ratio while still doing only
  38. * simple arithmetics:
  39. * 0 <= x <= (xsensors - 1) * xfact
  40. * 0 <= y <= (ysensors - 1) * yfact
  41. */
  42. struct atp_info {
  43. int xsensors; /* number of X sensors */
  44. int xsensors_17; /* 17" models have more sensors */
  45. int ysensors; /* number of Y sensors */
  46. int xfact; /* X multiplication factor */
  47. int yfact; /* Y multiplication factor */
  48. int datalen; /* size of USB transfers */
  49. void (*callback)(struct urb *); /* callback function */
  50. };
  51. static void atp_complete_geyser_1_2(struct urb *urb);
  52. static void atp_complete_geyser_3_4(struct urb *urb);
  53. static const struct atp_info fountain_info = {
  54. .xsensors = 16,
  55. .xsensors_17 = 26,
  56. .ysensors = 16,
  57. .xfact = 64,
  58. .yfact = 43,
  59. .datalen = 81,
  60. .callback = atp_complete_geyser_1_2,
  61. };
  62. static const struct atp_info geyser1_info = {
  63. .xsensors = 16,
  64. .xsensors_17 = 26,
  65. .ysensors = 16,
  66. .xfact = 64,
  67. .yfact = 43,
  68. .datalen = 81,
  69. .callback = atp_complete_geyser_1_2,
  70. };
  71. static const struct atp_info geyser2_info = {
  72. .xsensors = 15,
  73. .xsensors_17 = 20,
  74. .ysensors = 9,
  75. .xfact = 64,
  76. .yfact = 43,
  77. .datalen = 64,
  78. .callback = atp_complete_geyser_1_2,
  79. };
  80. static const struct atp_info geyser3_info = {
  81. .xsensors = 20,
  82. .ysensors = 10,
  83. .xfact = 64,
  84. .yfact = 64,
  85. .datalen = 64,
  86. .callback = atp_complete_geyser_3_4,
  87. };
  88. static const struct atp_info geyser4_info = {
  89. .xsensors = 20,
  90. .ysensors = 10,
  91. .xfact = 64,
  92. .yfact = 64,
  93. .datalen = 64,
  94. .callback = atp_complete_geyser_3_4,
  95. };
  96. #define ATP_DEVICE(prod, info) \
  97. { \
  98. .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
  99. USB_DEVICE_ID_MATCH_INT_CLASS | \
  100. USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
  101. .idVendor = 0x05ac, /* Apple */ \
  102. .idProduct = (prod), \
  103. .bInterfaceClass = 0x03, \
  104. .bInterfaceProtocol = 0x02, \
  105. .driver_info = (unsigned long) &info, \
  106. }
  107. /*
  108. * Table of devices (Product IDs) that work with this driver.
  109. * (The names come from Info.plist in AppleUSBTrackpad.kext,
  110. * According to Info.plist Geyser IV is the same as Geyser III.)
  111. */
  112. static struct usb_device_id atp_table[] = {
  113. /* PowerBooks Feb 2005, iBooks G4 */
  114. ATP_DEVICE(0x020e, fountain_info), /* FOUNTAIN ANSI */
  115. ATP_DEVICE(0x020f, fountain_info), /* FOUNTAIN ISO */
  116. ATP_DEVICE(0x030a, fountain_info), /* FOUNTAIN TP ONLY */
  117. ATP_DEVICE(0x030b, geyser1_info), /* GEYSER 1 TP ONLY */
  118. /* PowerBooks Oct 2005 */
  119. ATP_DEVICE(0x0214, geyser2_info), /* GEYSER 2 ANSI */
  120. ATP_DEVICE(0x0215, geyser2_info), /* GEYSER 2 ISO */
  121. ATP_DEVICE(0x0216, geyser2_info), /* GEYSER 2 JIS */
  122. /* Core Duo MacBook & MacBook Pro */
  123. ATP_DEVICE(0x0217, geyser3_info), /* GEYSER 3 ANSI */
  124. ATP_DEVICE(0x0218, geyser3_info), /* GEYSER 3 ISO */
  125. ATP_DEVICE(0x0219, geyser3_info), /* GEYSER 3 JIS */
  126. /* Core2 Duo MacBook & MacBook Pro */
  127. ATP_DEVICE(0x021a, geyser4_info), /* GEYSER 4 ANSI */
  128. ATP_DEVICE(0x021b, geyser4_info), /* GEYSER 4 ISO */
  129. ATP_DEVICE(0x021c, geyser4_info), /* GEYSER 4 JIS */
  130. /* Core2 Duo MacBook3,1 */
  131. ATP_DEVICE(0x0229, geyser4_info), /* GEYSER 4 HF ANSI */
  132. ATP_DEVICE(0x022a, geyser4_info), /* GEYSER 4 HF ISO */
  133. ATP_DEVICE(0x022b, geyser4_info), /* GEYSER 4 HF JIS */
  134. /* Terminating entry */
  135. { }
  136. };
  137. MODULE_DEVICE_TABLE(usb, atp_table);
  138. /* maximum number of sensors */
  139. #define ATP_XSENSORS 26
  140. #define ATP_YSENSORS 16
  141. /* amount of fuzz this touchpad generates */
  142. #define ATP_FUZZ 16
  143. /* maximum pressure this driver will report */
  144. #define ATP_PRESSURE 300
  145. /*
  146. * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
  147. * ignored.
  148. */
  149. #define ATP_THRESHOLD 5
  150. /* Geyser initialization constants */
  151. #define ATP_GEYSER_MODE_READ_REQUEST_ID 1
  152. #define ATP_GEYSER_MODE_WRITE_REQUEST_ID 9
  153. #define ATP_GEYSER_MODE_REQUEST_VALUE 0x300
  154. #define ATP_GEYSER_MODE_REQUEST_INDEX 0
  155. #define ATP_GEYSER_MODE_VENDOR_VALUE 0x04
  156. /**
  157. * enum atp_status_bits - status bit meanings
  158. *
  159. * These constants represent the meaning of the status bits.
  160. * (only Geyser 3/4)
  161. *
  162. * @ATP_STATUS_BUTTON: The button was pressed
  163. * @ATP_STATUS_BASE_UPDATE: Update of the base values (untouched pad)
  164. * @ATP_STATUS_FROM_RESET: Reset previously performed
  165. */
  166. enum atp_status_bits {
  167. ATP_STATUS_BUTTON = BIT(0),
  168. ATP_STATUS_BASE_UPDATE = BIT(2),
  169. ATP_STATUS_FROM_RESET = BIT(4),
  170. };
  171. /* Structure to hold all of our device specific stuff */
  172. struct atp {
  173. char phys[64];
  174. struct usb_device *udev; /* usb device */
  175. struct usb_interface *intf; /* usb interface */
  176. struct urb *urb; /* usb request block */
  177. u8 *data; /* transferred data */
  178. struct input_dev *input; /* input dev */
  179. const struct atp_info *info; /* touchpad model */
  180. bool open;
  181. bool valid; /* are the samples valid? */
  182. bool size_detect_done;
  183. bool overflow_warned;
  184. int x_old; /* last reported x/y, */
  185. int y_old; /* used for smoothing */
  186. signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
  187. signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
  188. int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
  189. int idlecount; /* number of empty packets */
  190. struct work_struct work;
  191. };
  192. #define dbg_dump(msg, tab) \
  193. if (debug > 1) { \
  194. int __i; \
  195. printk(KERN_DEBUG "appletouch: %s", msg); \
  196. for (__i = 0; __i < ATP_XSENSORS + ATP_YSENSORS; __i++) \
  197. printk(" %02x", tab[__i]); \
  198. printk("\n"); \
  199. }
  200. #define dprintk(format, a...) \
  201. do { \
  202. if (debug) \
  203. printk(KERN_DEBUG format, ##a); \
  204. } while (0)
  205. MODULE_AUTHOR("Johannes Berg");
  206. MODULE_AUTHOR("Stelian Pop");
  207. MODULE_AUTHOR("Frank Arnold");
  208. MODULE_AUTHOR("Michael Hanselmann");
  209. MODULE_AUTHOR("Sven Anders");
  210. MODULE_DESCRIPTION("Apple PowerBook and MacBook USB touchpad driver");
  211. MODULE_LICENSE("GPL");
  212. /*
  213. * Make the threshold a module parameter
  214. */
  215. static int threshold = ATP_THRESHOLD;
  216. module_param(threshold, int, 0644);
  217. MODULE_PARM_DESC(threshold, "Discard any change in data from a sensor"
  218. " (the trackpad has many of these sensors)"
  219. " less than this value.");
  220. static int debug;
  221. module_param(debug, int, 0644);
  222. MODULE_PARM_DESC(debug, "Activate debugging output");
  223. /*
  224. * By default newer Geyser devices send standard USB HID mouse
  225. * packets (Report ID 2). This code changes device mode, so it
  226. * sends raw sensor reports (Report ID 5).
  227. */
  228. static int atp_geyser_init(struct atp *dev)
  229. {
  230. struct usb_device *udev = dev->udev;
  231. char *data;
  232. int size;
  233. int i;
  234. int ret;
  235. data = kmalloc(8, GFP_KERNEL);
  236. if (!data) {
  237. dev_err(&dev->intf->dev, "Out of memory\n");
  238. return -ENOMEM;
  239. }
  240. size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  241. ATP_GEYSER_MODE_READ_REQUEST_ID,
  242. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  243. ATP_GEYSER_MODE_REQUEST_VALUE,
  244. ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000);
  245. if (size != 8) {
  246. dprintk("atp_geyser_init: read error\n");
  247. for (i = 0; i < 8; i++)
  248. dprintk("appletouch[%d]: %d\n", i, data[i]);
  249. dev_err(&dev->intf->dev, "Failed to read mode from device.\n");
  250. ret = -EIO;
  251. goto out_free;
  252. }
  253. /* Apply the mode switch */
  254. data[0] = ATP_GEYSER_MODE_VENDOR_VALUE;
  255. size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  256. ATP_GEYSER_MODE_WRITE_REQUEST_ID,
  257. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  258. ATP_GEYSER_MODE_REQUEST_VALUE,
  259. ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000);
  260. if (size != 8) {
  261. dprintk("atp_geyser_init: write error\n");
  262. for (i = 0; i < 8; i++)
  263. dprintk("appletouch[%d]: %d\n", i, data[i]);
  264. dev_err(&dev->intf->dev, "Failed to request geyser raw mode\n");
  265. ret = -EIO;
  266. goto out_free;
  267. }
  268. ret = 0;
  269. out_free:
  270. kfree(data);
  271. return ret;
  272. }
  273. /*
  274. * Reinitialise the device. This usually stops stream of empty packets
  275. * coming from it.
  276. */
  277. static void atp_reinit(struct work_struct *work)
  278. {
  279. struct atp *dev = container_of(work, struct atp, work);
  280. int retval;
  281. dprintk("appletouch: putting appletouch to sleep (reinit)\n");
  282. atp_geyser_init(dev);
  283. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  284. if (retval)
  285. dev_err(&dev->intf->dev,
  286. "atp_reinit: usb_submit_urb failed with error %d\n",
  287. retval);
  288. }
  289. static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
  290. int *z, int *fingers)
  291. {
  292. int i;
  293. /* values to calculate mean */
  294. int pcum = 0, psum = 0;
  295. int is_increasing = 0;
  296. *fingers = 0;
  297. for (i = 0; i < nb_sensors; i++) {
  298. if (xy_sensors[i] < threshold) {
  299. if (is_increasing)
  300. is_increasing = 0;
  301. continue;
  302. }
  303. /*
  304. * Makes the finger detection more versatile. For example,
  305. * two fingers with no gap will be detected. Also, my
  306. * tests show it less likely to have intermittent loss
  307. * of multiple finger readings while moving around (scrolling).
  308. *
  309. * Changes the multiple finger detection to counting humps on
  310. * sensors (transitions from nonincreasing to increasing)
  311. * instead of counting transitions from low sensors (no
  312. * finger reading) to high sensors (finger above
  313. * sensor)
  314. *
  315. * - Jason Parekh <jasonparekh@gmail.com>
  316. */
  317. if (i < 1 ||
  318. (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) {
  319. (*fingers)++;
  320. is_increasing = 1;
  321. } else if (i > 0 && (xy_sensors[i - 1] - xy_sensors[i] > threshold)) {
  322. is_increasing = 0;
  323. }
  324. /*
  325. * Subtracts threshold so a high sensor that just passes the
  326. * threshold won't skew the calculated absolute coordinate.
  327. * Fixes an issue where slowly moving the mouse would
  328. * occasionally jump a number of pixels (slowly moving the
  329. * finger makes this issue most apparent.)
  330. */
  331. pcum += (xy_sensors[i] - threshold) * i;
  332. psum += (xy_sensors[i] - threshold);
  333. }
  334. if (psum > 0) {
  335. *z = psum;
  336. return pcum * fact / psum;
  337. }
  338. return 0;
  339. }
  340. static inline void atp_report_fingers(struct input_dev *input, int fingers)
  341. {
  342. input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
  343. input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
  344. input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
  345. }
  346. /* Check URB status and for correct length of data package */
  347. #define ATP_URB_STATUS_SUCCESS 0
  348. #define ATP_URB_STATUS_ERROR 1
  349. #define ATP_URB_STATUS_ERROR_FATAL 2
  350. static int atp_status_check(struct urb *urb)
  351. {
  352. struct atp *dev = urb->context;
  353. struct usb_interface *intf = dev->intf;
  354. switch (urb->status) {
  355. case 0:
  356. /* success */
  357. break;
  358. case -EOVERFLOW:
  359. if (!dev->overflow_warned) {
  360. dev_warn(&intf->dev,
  361. "appletouch: OVERFLOW with data length %d, actual length is %d\n",
  362. dev->info->datalen, dev->urb->actual_length);
  363. dev->overflow_warned = true;
  364. }
  365. case -ECONNRESET:
  366. case -ENOENT:
  367. case -ESHUTDOWN:
  368. /* This urb is terminated, clean up */
  369. dev_dbg(&intf->dev,
  370. "atp_complete: urb shutting down with status: %d\n",
  371. urb->status);
  372. return ATP_URB_STATUS_ERROR_FATAL;
  373. default:
  374. dev_dbg(&intf->dev,
  375. "atp_complete: nonzero urb status received: %d\n",
  376. urb->status);
  377. return ATP_URB_STATUS_ERROR;
  378. }
  379. /* drop incomplete datasets */
  380. if (dev->urb->actual_length != dev->info->datalen) {
  381. dprintk("appletouch: incomplete data package"
  382. " (first byte: %d, length: %d).\n",
  383. dev->data[0], dev->urb->actual_length);
  384. return ATP_URB_STATUS_ERROR;
  385. }
  386. return ATP_URB_STATUS_SUCCESS;
  387. }
  388. static void atp_detect_size(struct atp *dev)
  389. {
  390. int i;
  391. /* 17" Powerbooks have extra X sensors */
  392. for (i = dev->info->xsensors; i < ATP_XSENSORS; i++) {
  393. if (dev->xy_cur[i]) {
  394. dev_info(&dev->intf->dev,
  395. "appletouch: 17\" model detected.\n");
  396. input_set_abs_params(dev->input, ABS_X, 0,
  397. (dev->info->xsensors_17 - 1) *
  398. dev->info->xfact - 1,
  399. ATP_FUZZ, 0);
  400. break;
  401. }
  402. }
  403. }
  404. /*
  405. * USB interrupt callback functions
  406. */
  407. /* Interrupt function for older touchpads: FOUNTAIN/GEYSER1/GEYSER2 */
  408. static void atp_complete_geyser_1_2(struct urb *urb)
  409. {
  410. int x, y, x_z, y_z, x_f, y_f;
  411. int retval, i, j;
  412. int key;
  413. struct atp *dev = urb->context;
  414. int status = atp_status_check(urb);
  415. if (status == ATP_URB_STATUS_ERROR_FATAL)
  416. return;
  417. else if (status == ATP_URB_STATUS_ERROR)
  418. goto exit;
  419. /* reorder the sensors values */
  420. if (dev->info == &geyser2_info) {
  421. memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
  422. /*
  423. * The values are laid out like this:
  424. * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
  425. * '-' is an unused value.
  426. */
  427. /* read X values */
  428. for (i = 0, j = 19; i < 20; i += 2, j += 3) {
  429. dev->xy_cur[i] = dev->data[j];
  430. dev->xy_cur[i + 1] = dev->data[j + 1];
  431. }
  432. /* read Y values */
  433. for (i = 0, j = 1; i < 9; i += 2, j += 3) {
  434. dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
  435. dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
  436. }
  437. } else {
  438. for (i = 0; i < 8; i++) {
  439. /* X values */
  440. dev->xy_cur[i + 0] = dev->data[5 * i + 2];
  441. dev->xy_cur[i + 8] = dev->data[5 * i + 4];
  442. dev->xy_cur[i + 16] = dev->data[5 * i + 42];
  443. if (i < 2)
  444. dev->xy_cur[i + 24] = dev->data[5 * i + 44];
  445. /* Y values */
  446. dev->xy_cur[ATP_XSENSORS + i] = dev->data[5 * i + 1];
  447. dev->xy_cur[ATP_XSENSORS + i + 8] = dev->data[5 * i + 3];
  448. }
  449. }
  450. dbg_dump("sample", dev->xy_cur);
  451. if (!dev->valid) {
  452. /* first sample */
  453. dev->valid = true;
  454. dev->x_old = dev->y_old = -1;
  455. /* Store first sample */
  456. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  457. /* Perform size detection, if not done already */
  458. if (unlikely(!dev->size_detect_done)) {
  459. atp_detect_size(dev);
  460. dev->size_detect_done = 1;
  461. goto exit;
  462. }
  463. }
  464. for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
  465. /* accumulate the change */
  466. signed char change = dev->xy_old[i] - dev->xy_cur[i];
  467. dev->xy_acc[i] -= change;
  468. /* prevent down drifting */
  469. if (dev->xy_acc[i] < 0)
  470. dev->xy_acc[i] = 0;
  471. }
  472. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  473. dbg_dump("accumulator", dev->xy_acc);
  474. x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
  475. dev->info->xfact, &x_z, &x_f);
  476. y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
  477. dev->info->yfact, &y_z, &y_f);
  478. key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
  479. if (x && y) {
  480. if (dev->x_old != -1) {
  481. x = (dev->x_old * 3 + x) >> 2;
  482. y = (dev->y_old * 3 + y) >> 2;
  483. dev->x_old = x;
  484. dev->y_old = y;
  485. if (debug > 1)
  486. printk(KERN_DEBUG "appletouch: "
  487. "X: %3d Y: %3d Xz: %3d Yz: %3d\n",
  488. x, y, x_z, y_z);
  489. input_report_key(dev->input, BTN_TOUCH, 1);
  490. input_report_abs(dev->input, ABS_X, x);
  491. input_report_abs(dev->input, ABS_Y, y);
  492. input_report_abs(dev->input, ABS_PRESSURE,
  493. min(ATP_PRESSURE, x_z + y_z));
  494. atp_report_fingers(dev->input, max(x_f, y_f));
  495. }
  496. dev->x_old = x;
  497. dev->y_old = y;
  498. } else if (!x && !y) {
  499. dev->x_old = dev->y_old = -1;
  500. input_report_key(dev->input, BTN_TOUCH, 0);
  501. input_report_abs(dev->input, ABS_PRESSURE, 0);
  502. atp_report_fingers(dev->input, 0);
  503. /* reset the accumulator on release */
  504. memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
  505. }
  506. input_report_key(dev->input, BTN_LEFT, key);
  507. input_sync(dev->input);
  508. exit:
  509. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  510. if (retval)
  511. dev_err(&dev->intf->dev,
  512. "atp_complete: usb_submit_urb failed with result %d\n",
  513. retval);
  514. }
  515. /* Interrupt function for older touchpads: GEYSER3/GEYSER4 */
  516. static void atp_complete_geyser_3_4(struct urb *urb)
  517. {
  518. int x, y, x_z, y_z, x_f, y_f;
  519. int retval, i, j;
  520. int key;
  521. struct atp *dev = urb->context;
  522. int status = atp_status_check(urb);
  523. if (status == ATP_URB_STATUS_ERROR_FATAL)
  524. return;
  525. else if (status == ATP_URB_STATUS_ERROR)
  526. goto exit;
  527. /* Reorder the sensors values:
  528. *
  529. * The values are laid out like this:
  530. * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
  531. * '-' is an unused value.
  532. */
  533. /* read X values */
  534. for (i = 0, j = 19; i < 20; i += 2, j += 3) {
  535. dev->xy_cur[i] = dev->data[j + 1];
  536. dev->xy_cur[i + 1] = dev->data[j + 2];
  537. }
  538. /* read Y values */
  539. for (i = 0, j = 1; i < 9; i += 2, j += 3) {
  540. dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
  541. dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
  542. }
  543. dbg_dump("sample", dev->xy_cur);
  544. /* Just update the base values (i.e. touchpad in untouched state) */
  545. if (dev->data[dev->info->datalen - 1] & ATP_STATUS_BASE_UPDATE) {
  546. dprintk("appletouch: updated base values\n");
  547. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  548. goto exit;
  549. }
  550. for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
  551. /* calculate the change */
  552. dev->xy_acc[i] = dev->xy_cur[i] - dev->xy_old[i];
  553. /* this is a round-robin value, so couple with that */
  554. if (dev->xy_acc[i] > 127)
  555. dev->xy_acc[i] -= 256;
  556. if (dev->xy_acc[i] < -127)
  557. dev->xy_acc[i] += 256;
  558. /* prevent down drifting */
  559. if (dev->xy_acc[i] < 0)
  560. dev->xy_acc[i] = 0;
  561. }
  562. dbg_dump("accumulator", dev->xy_acc);
  563. x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
  564. dev->info->xfact, &x_z, &x_f);
  565. y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
  566. dev->info->yfact, &y_z, &y_f);
  567. key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
  568. if (x && y) {
  569. if (dev->x_old != -1) {
  570. x = (dev->x_old * 3 + x) >> 2;
  571. y = (dev->y_old * 3 + y) >> 2;
  572. dev->x_old = x;
  573. dev->y_old = y;
  574. if (debug > 1)
  575. printk(KERN_DEBUG "appletouch: X: %3d Y: %3d "
  576. "Xz: %3d Yz: %3d\n",
  577. x, y, x_z, y_z);
  578. input_report_key(dev->input, BTN_TOUCH, 1);
  579. input_report_abs(dev->input, ABS_X, x);
  580. input_report_abs(dev->input, ABS_Y, y);
  581. input_report_abs(dev->input, ABS_PRESSURE,
  582. min(ATP_PRESSURE, x_z + y_z));
  583. atp_report_fingers(dev->input, max(x_f, y_f));
  584. }
  585. dev->x_old = x;
  586. dev->y_old = y;
  587. } else if (!x && !y) {
  588. dev->x_old = dev->y_old = -1;
  589. input_report_key(dev->input, BTN_TOUCH, 0);
  590. input_report_abs(dev->input, ABS_PRESSURE, 0);
  591. atp_report_fingers(dev->input, 0);
  592. /* reset the accumulator on release */
  593. memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
  594. }
  595. input_report_key(dev->input, BTN_LEFT, key);
  596. input_sync(dev->input);
  597. /*
  598. * Geysers 3/4 will continue to send packets continually after
  599. * the first touch unless reinitialised. Do so if it's been
  600. * idle for a while in order to avoid waking the kernel up
  601. * several hundred times a second.
  602. */
  603. /*
  604. * Button must not be pressed when entering suspend,
  605. * otherwise we will never release the button.
  606. */
  607. if (!x && !y && !key) {
  608. dev->idlecount++;
  609. if (dev->idlecount == 10) {
  610. dev->x_old = dev->y_old = -1;
  611. dev->idlecount = 0;
  612. schedule_work(&dev->work);
  613. /* Don't resubmit urb here, wait for reinit */
  614. return;
  615. }
  616. } else
  617. dev->idlecount = 0;
  618. exit:
  619. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  620. if (retval)
  621. dev_err(&dev->intf->dev,
  622. "atp_complete: usb_submit_urb failed with result %d\n",
  623. retval);
  624. }
  625. static int atp_open(struct input_dev *input)
  626. {
  627. struct atp *dev = input_get_drvdata(input);
  628. if (usb_submit_urb(dev->urb, GFP_ATOMIC))
  629. return -EIO;
  630. dev->open = 1;
  631. return 0;
  632. }
  633. static void atp_close(struct input_dev *input)
  634. {
  635. struct atp *dev = input_get_drvdata(input);
  636. usb_kill_urb(dev->urb);
  637. cancel_work_sync(&dev->work);
  638. dev->open = 0;
  639. }
  640. static int atp_handle_geyser(struct atp *dev)
  641. {
  642. if (dev->info != &fountain_info) {
  643. /* switch to raw sensor mode */
  644. if (atp_geyser_init(dev))
  645. return -EIO;
  646. dev_info(&dev->intf->dev, "Geyser mode initialized.\n");
  647. }
  648. return 0;
  649. }
  650. static int atp_probe(struct usb_interface *iface,
  651. const struct usb_device_id *id)
  652. {
  653. struct atp *dev;
  654. struct input_dev *input_dev;
  655. struct usb_device *udev = interface_to_usbdev(iface);
  656. struct usb_host_interface *iface_desc;
  657. struct usb_endpoint_descriptor *endpoint;
  658. int int_in_endpointAddr = 0;
  659. int i, error = -ENOMEM;
  660. const struct atp_info *info = (const struct atp_info *)id->driver_info;
  661. /* set up the endpoint information */
  662. /* use only the first interrupt-in endpoint */
  663. iface_desc = iface->cur_altsetting;
  664. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  665. endpoint = &iface_desc->endpoint[i].desc;
  666. if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
  667. /* we found an interrupt in endpoint */
  668. int_in_endpointAddr = endpoint->bEndpointAddress;
  669. break;
  670. }
  671. }
  672. if (!int_in_endpointAddr) {
  673. dev_err(&iface->dev, "Could not find int-in endpoint\n");
  674. return -EIO;
  675. }
  676. /* allocate memory for our device state and initialize it */
  677. dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
  678. input_dev = input_allocate_device();
  679. if (!dev || !input_dev) {
  680. dev_err(&iface->dev, "Out of memory\n");
  681. goto err_free_devs;
  682. }
  683. dev->udev = udev;
  684. dev->intf = iface;
  685. dev->input = input_dev;
  686. dev->info = info;
  687. dev->overflow_warned = false;
  688. dev->urb = usb_alloc_urb(0, GFP_KERNEL);
  689. if (!dev->urb)
  690. goto err_free_devs;
  691. dev->data = usb_alloc_coherent(dev->udev, dev->info->datalen, GFP_KERNEL,
  692. &dev->urb->transfer_dma);
  693. if (!dev->data)
  694. goto err_free_urb;
  695. usb_fill_int_urb(dev->urb, udev,
  696. usb_rcvintpipe(udev, int_in_endpointAddr),
  697. dev->data, dev->info->datalen,
  698. dev->info->callback, dev, 1);
  699. error = atp_handle_geyser(dev);
  700. if (error)
  701. goto err_free_buffer;
  702. usb_make_path(udev, dev->phys, sizeof(dev->phys));
  703. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  704. input_dev->name = "appletouch";
  705. input_dev->phys = dev->phys;
  706. usb_to_input_id(dev->udev, &input_dev->id);
  707. input_dev->dev.parent = &iface->dev;
  708. input_set_drvdata(input_dev, dev);
  709. input_dev->open = atp_open;
  710. input_dev->close = atp_close;
  711. set_bit(EV_ABS, input_dev->evbit);
  712. input_set_abs_params(input_dev, ABS_X, 0,
  713. (dev->info->xsensors - 1) * dev->info->xfact - 1,
  714. ATP_FUZZ, 0);
  715. input_set_abs_params(input_dev, ABS_Y, 0,
  716. (dev->info->ysensors - 1) * dev->info->yfact - 1,
  717. ATP_FUZZ, 0);
  718. input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
  719. set_bit(EV_KEY, input_dev->evbit);
  720. set_bit(BTN_TOUCH, input_dev->keybit);
  721. set_bit(BTN_TOOL_FINGER, input_dev->keybit);
  722. set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
  723. set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
  724. set_bit(BTN_LEFT, input_dev->keybit);
  725. error = input_register_device(dev->input);
  726. if (error)
  727. goto err_free_buffer;
  728. /* save our data pointer in this interface device */
  729. usb_set_intfdata(iface, dev);
  730. INIT_WORK(&dev->work, atp_reinit);
  731. return 0;
  732. err_free_buffer:
  733. usb_free_coherent(dev->udev, dev->info->datalen,
  734. dev->data, dev->urb->transfer_dma);
  735. err_free_urb:
  736. usb_free_urb(dev->urb);
  737. err_free_devs:
  738. usb_set_intfdata(iface, NULL);
  739. kfree(dev);
  740. input_free_device(input_dev);
  741. return error;
  742. }
  743. static void atp_disconnect(struct usb_interface *iface)
  744. {
  745. struct atp *dev = usb_get_intfdata(iface);
  746. usb_set_intfdata(iface, NULL);
  747. if (dev) {
  748. usb_kill_urb(dev->urb);
  749. input_unregister_device(dev->input);
  750. usb_free_coherent(dev->udev, dev->info->datalen,
  751. dev->data, dev->urb->transfer_dma);
  752. usb_free_urb(dev->urb);
  753. kfree(dev);
  754. }
  755. dev_info(&iface->dev, "input: appletouch disconnected\n");
  756. }
  757. static int atp_recover(struct atp *dev)
  758. {
  759. int error;
  760. error = atp_handle_geyser(dev);
  761. if (error)
  762. return error;
  763. if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
  764. return -EIO;
  765. return 0;
  766. }
  767. static int atp_suspend(struct usb_interface *iface, pm_message_t message)
  768. {
  769. struct atp *dev = usb_get_intfdata(iface);
  770. usb_kill_urb(dev->urb);
  771. return 0;
  772. }
  773. static int atp_resume(struct usb_interface *iface)
  774. {
  775. struct atp *dev = usb_get_intfdata(iface);
  776. if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
  777. return -EIO;
  778. return 0;
  779. }
  780. static int atp_reset_resume(struct usb_interface *iface)
  781. {
  782. struct atp *dev = usb_get_intfdata(iface);
  783. return atp_recover(dev);
  784. }
  785. static struct usb_driver atp_driver = {
  786. .name = "appletouch",
  787. .probe = atp_probe,
  788. .disconnect = atp_disconnect,
  789. .suspend = atp_suspend,
  790. .resume = atp_resume,
  791. .reset_resume = atp_reset_resume,
  792. .id_table = atp_table,
  793. };
  794. module_usb_driver(atp_driver);