appletouch.c 26 KB

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