appletouch.c 27 KB

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