appletouch.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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 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. case -ECONNRESET:
  413. case -ENOENT:
  414. case -ESHUTDOWN:
  415. /* This urb is terminated, clean up */
  416. dev_dbg(&intf->dev,
  417. "atp_complete: urb shutting down with status: %d\n",
  418. urb->status);
  419. return ATP_URB_STATUS_ERROR_FATAL;
  420. default:
  421. dev_dbg(&intf->dev,
  422. "atp_complete: nonzero urb status received: %d\n",
  423. urb->status);
  424. return ATP_URB_STATUS_ERROR;
  425. }
  426. /* drop incomplete datasets */
  427. if (dev->urb->actual_length != dev->info->datalen) {
  428. dprintk("appletouch: incomplete data package"
  429. " (first byte: %d, length: %d).\n",
  430. dev->data[0], dev->urb->actual_length);
  431. return ATP_URB_STATUS_ERROR;
  432. }
  433. return ATP_URB_STATUS_SUCCESS;
  434. }
  435. static void atp_detect_size(struct atp *dev)
  436. {
  437. int i;
  438. /* 17" Powerbooks have extra X sensors */
  439. for (i = dev->info->xsensors; i < ATP_XSENSORS; i++) {
  440. if (dev->xy_cur[i]) {
  441. dev_info(&dev->intf->dev,
  442. "appletouch: 17\" model detected.\n");
  443. input_set_abs_params(dev->input, ABS_X, 0,
  444. (dev->info->xsensors_17 - 1) *
  445. dev->info->xfact - 1,
  446. dev->info->fuzz, 0);
  447. break;
  448. }
  449. }
  450. }
  451. /*
  452. * USB interrupt callback functions
  453. */
  454. /* Interrupt function for older touchpads: FOUNTAIN/GEYSER1/GEYSER2 */
  455. static void atp_complete_geyser_1_2(struct urb *urb)
  456. {
  457. int x, y, x_z, y_z, x_f, y_f;
  458. int retval, i, j;
  459. int key, fingers;
  460. struct atp *dev = urb->context;
  461. int status = atp_status_check(urb);
  462. if (status == ATP_URB_STATUS_ERROR_FATAL)
  463. return;
  464. else if (status == ATP_URB_STATUS_ERROR)
  465. goto exit;
  466. /* reorder the sensors values */
  467. if (dev->info == &geyser2_info) {
  468. memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
  469. /*
  470. * The values are laid out like this:
  471. * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
  472. * '-' is an unused value.
  473. */
  474. /* read X values */
  475. for (i = 0, j = 19; i < 20; i += 2, j += 3) {
  476. dev->xy_cur[i] = dev->data[j];
  477. dev->xy_cur[i + 1] = dev->data[j + 1];
  478. }
  479. /* read Y values */
  480. for (i = 0, j = 1; i < 9; i += 2, j += 3) {
  481. dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
  482. dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
  483. }
  484. } else {
  485. for (i = 0; i < 8; i++) {
  486. /* X values */
  487. dev->xy_cur[i + 0] = dev->data[5 * i + 2];
  488. dev->xy_cur[i + 8] = dev->data[5 * i + 4];
  489. dev->xy_cur[i + 16] = dev->data[5 * i + 42];
  490. if (i < 2)
  491. dev->xy_cur[i + 24] = dev->data[5 * i + 44];
  492. /* Y values */
  493. dev->xy_cur[ATP_XSENSORS + i] = dev->data[5 * i + 1];
  494. dev->xy_cur[ATP_XSENSORS + i + 8] = dev->data[5 * i + 3];
  495. }
  496. }
  497. dbg_dump("sample", dev->xy_cur);
  498. if (!dev->valid) {
  499. /* first sample */
  500. dev->valid = true;
  501. dev->x_old = dev->y_old = -1;
  502. /* Store first sample */
  503. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  504. /* Perform size detection, if not done already */
  505. if (unlikely(!dev->size_detect_done)) {
  506. atp_detect_size(dev);
  507. dev->size_detect_done = 1;
  508. goto exit;
  509. }
  510. }
  511. for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
  512. /* accumulate the change */
  513. signed char change = dev->xy_old[i] - dev->xy_cur[i];
  514. dev->xy_acc[i] -= change;
  515. /* prevent down drifting */
  516. if (dev->xy_acc[i] < 0)
  517. dev->xy_acc[i] = 0;
  518. }
  519. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  520. dbg_dump("accumulator", dev->xy_acc);
  521. x = atp_calculate_abs(dev, 0, ATP_XSENSORS,
  522. dev->info->xfact, &x_z, &x_f);
  523. y = atp_calculate_abs(dev, ATP_XSENSORS, ATP_YSENSORS,
  524. dev->info->yfact, &y_z, &y_f);
  525. key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
  526. fingers = max(x_f, y_f);
  527. if (x && y && fingers == dev->fingers_old) {
  528. if (dev->x_old != -1) {
  529. x = (dev->x_old * 7 + x) >> 3;
  530. y = (dev->y_old * 7 + y) >> 3;
  531. dev->x_old = x;
  532. dev->y_old = y;
  533. if (debug > 1)
  534. printk(KERN_DEBUG "appletouch: "
  535. "X: %3d Y: %3d Xz: %3d Yz: %3d\n",
  536. x, y, x_z, y_z);
  537. input_report_key(dev->input, BTN_TOUCH, 1);
  538. input_report_abs(dev->input, ABS_X, x);
  539. input_report_abs(dev->input, ABS_Y, y);
  540. input_report_abs(dev->input, ABS_PRESSURE,
  541. min(ATP_PRESSURE, x_z + y_z));
  542. atp_report_fingers(dev->input, fingers);
  543. }
  544. dev->x_old = x;
  545. dev->y_old = y;
  546. } else if (!x && !y) {
  547. dev->x_old = dev->y_old = -1;
  548. dev->fingers_old = 0;
  549. input_report_key(dev->input, BTN_TOUCH, 0);
  550. input_report_abs(dev->input, ABS_PRESSURE, 0);
  551. atp_report_fingers(dev->input, 0);
  552. /* reset the accumulator on release */
  553. memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
  554. }
  555. if (fingers != dev->fingers_old)
  556. dev->x_old = dev->y_old = -1;
  557. dev->fingers_old = fingers;
  558. input_report_key(dev->input, BTN_LEFT, key);
  559. input_sync(dev->input);
  560. exit:
  561. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  562. if (retval)
  563. dev_err(&dev->intf->dev,
  564. "atp_complete: usb_submit_urb failed with result %d\n",
  565. retval);
  566. }
  567. /* Interrupt function for older touchpads: GEYSER3/GEYSER4 */
  568. static void atp_complete_geyser_3_4(struct urb *urb)
  569. {
  570. int x, y, x_z, y_z, x_f, y_f;
  571. int retval, i, j;
  572. int key, fingers;
  573. struct atp *dev = urb->context;
  574. int status = atp_status_check(urb);
  575. if (status == ATP_URB_STATUS_ERROR_FATAL)
  576. return;
  577. else if (status == ATP_URB_STATUS_ERROR)
  578. goto exit;
  579. /* Reorder the sensors values:
  580. *
  581. * The values are laid out like this:
  582. * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
  583. * '-' is an unused value.
  584. */
  585. /* read X values */
  586. for (i = 0, j = 19; i < 20; i += 2, j += 3) {
  587. dev->xy_cur[i] = dev->data[j + 1];
  588. dev->xy_cur[i + 1] = dev->data[j + 2];
  589. }
  590. /* read Y values */
  591. for (i = 0, j = 1; i < 9; i += 2, j += 3) {
  592. dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
  593. dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
  594. }
  595. dbg_dump("sample", dev->xy_cur);
  596. /* Just update the base values (i.e. touchpad in untouched state) */
  597. if (dev->data[dev->info->datalen - 1] & ATP_STATUS_BASE_UPDATE) {
  598. dprintk("appletouch: updated base values\n");
  599. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  600. goto exit;
  601. }
  602. for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
  603. /* calculate the change */
  604. dev->xy_acc[i] = dev->xy_cur[i] - dev->xy_old[i];
  605. /* this is a round-robin value, so couple with that */
  606. if (dev->xy_acc[i] > 127)
  607. dev->xy_acc[i] -= 256;
  608. if (dev->xy_acc[i] < -127)
  609. dev->xy_acc[i] += 256;
  610. /* prevent down drifting */
  611. if (dev->xy_acc[i] < 0)
  612. dev->xy_acc[i] = 0;
  613. }
  614. dbg_dump("accumulator", dev->xy_acc);
  615. x = atp_calculate_abs(dev, 0, ATP_XSENSORS,
  616. dev->info->xfact, &x_z, &x_f);
  617. y = atp_calculate_abs(dev, ATP_XSENSORS, ATP_YSENSORS,
  618. dev->info->yfact, &y_z, &y_f);
  619. key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
  620. fingers = max(x_f, y_f);
  621. if (x && y && fingers == dev->fingers_old) {
  622. if (dev->x_old != -1) {
  623. x = (dev->x_old * 7 + x) >> 3;
  624. y = (dev->y_old * 7 + y) >> 3;
  625. dev->x_old = x;
  626. dev->y_old = y;
  627. if (debug > 1)
  628. printk(KERN_DEBUG "appletouch: X: %3d Y: %3d "
  629. "Xz: %3d Yz: %3d\n",
  630. x, y, x_z, y_z);
  631. input_report_key(dev->input, BTN_TOUCH, 1);
  632. input_report_abs(dev->input, ABS_X, x);
  633. input_report_abs(dev->input, ABS_Y, y);
  634. input_report_abs(dev->input, ABS_PRESSURE,
  635. min(ATP_PRESSURE, x_z + y_z));
  636. atp_report_fingers(dev->input, fingers);
  637. }
  638. dev->x_old = x;
  639. dev->y_old = y;
  640. } else if (!x && !y) {
  641. dev->x_old = dev->y_old = -1;
  642. dev->fingers_old = 0;
  643. input_report_key(dev->input, BTN_TOUCH, 0);
  644. input_report_abs(dev->input, ABS_PRESSURE, 0);
  645. atp_report_fingers(dev->input, 0);
  646. /* reset the accumulator on release */
  647. memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
  648. }
  649. if (fingers != dev->fingers_old)
  650. dev->x_old = dev->y_old = -1;
  651. dev->fingers_old = fingers;
  652. input_report_key(dev->input, BTN_LEFT, key);
  653. input_sync(dev->input);
  654. /*
  655. * Geysers 3/4 will continue to send packets continually after
  656. * the first touch unless reinitialised. Do so if it's been
  657. * idle for a while in order to avoid waking the kernel up
  658. * several hundred times a second.
  659. */
  660. /*
  661. * Button must not be pressed when entering suspend,
  662. * otherwise we will never release the button.
  663. */
  664. if (!x && !y && !key) {
  665. dev->idlecount++;
  666. if (dev->idlecount == 10) {
  667. dev->x_old = dev->y_old = -1;
  668. dev->idlecount = 0;
  669. schedule_work(&dev->work);
  670. /* Don't resubmit urb here, wait for reinit */
  671. return;
  672. }
  673. } else
  674. dev->idlecount = 0;
  675. exit:
  676. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  677. if (retval)
  678. dev_err(&dev->intf->dev,
  679. "atp_complete: usb_submit_urb failed with result %d\n",
  680. retval);
  681. }
  682. static int atp_open(struct input_dev *input)
  683. {
  684. struct atp *dev = input_get_drvdata(input);
  685. if (usb_submit_urb(dev->urb, GFP_ATOMIC))
  686. return -EIO;
  687. dev->open = 1;
  688. return 0;
  689. }
  690. static void atp_close(struct input_dev *input)
  691. {
  692. struct atp *dev = input_get_drvdata(input);
  693. usb_kill_urb(dev->urb);
  694. cancel_work_sync(&dev->work);
  695. dev->open = 0;
  696. }
  697. static int atp_handle_geyser(struct atp *dev)
  698. {
  699. if (dev->info != &fountain_info) {
  700. /* switch to raw sensor mode */
  701. if (atp_geyser_init(dev))
  702. return -EIO;
  703. dev_info(&dev->intf->dev, "Geyser mode initialized.\n");
  704. }
  705. return 0;
  706. }
  707. static int atp_probe(struct usb_interface *iface,
  708. const struct usb_device_id *id)
  709. {
  710. struct atp *dev;
  711. struct input_dev *input_dev;
  712. struct usb_device *udev = interface_to_usbdev(iface);
  713. struct usb_host_interface *iface_desc;
  714. struct usb_endpoint_descriptor *endpoint;
  715. int int_in_endpointAddr = 0;
  716. int i, error = -ENOMEM;
  717. const struct atp_info *info = (const struct atp_info *)id->driver_info;
  718. /* set up the endpoint information */
  719. /* use only the first interrupt-in endpoint */
  720. iface_desc = iface->cur_altsetting;
  721. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  722. endpoint = &iface_desc->endpoint[i].desc;
  723. if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
  724. /* we found an interrupt in endpoint */
  725. int_in_endpointAddr = endpoint->bEndpointAddress;
  726. break;
  727. }
  728. }
  729. if (!int_in_endpointAddr) {
  730. dev_err(&iface->dev, "Could not find int-in endpoint\n");
  731. return -EIO;
  732. }
  733. /* allocate memory for our device state and initialize it */
  734. dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
  735. input_dev = input_allocate_device();
  736. if (!dev || !input_dev) {
  737. dev_err(&iface->dev, "Out of memory\n");
  738. goto err_free_devs;
  739. }
  740. dev->udev = udev;
  741. dev->intf = iface;
  742. dev->input = input_dev;
  743. dev->info = info;
  744. dev->overflow_warned = false;
  745. dev->urb = usb_alloc_urb(0, GFP_KERNEL);
  746. if (!dev->urb)
  747. goto err_free_devs;
  748. dev->data = usb_alloc_coherent(dev->udev, dev->info->datalen, GFP_KERNEL,
  749. &dev->urb->transfer_dma);
  750. if (!dev->data)
  751. goto err_free_urb;
  752. usb_fill_int_urb(dev->urb, udev,
  753. usb_rcvintpipe(udev, int_in_endpointAddr),
  754. dev->data, dev->info->datalen,
  755. dev->info->callback, dev, 1);
  756. error = atp_handle_geyser(dev);
  757. if (error)
  758. goto err_free_buffer;
  759. usb_make_path(udev, dev->phys, sizeof(dev->phys));
  760. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  761. input_dev->name = "appletouch";
  762. input_dev->phys = dev->phys;
  763. usb_to_input_id(dev->udev, &input_dev->id);
  764. input_dev->dev.parent = &iface->dev;
  765. input_set_drvdata(input_dev, dev);
  766. input_dev->open = atp_open;
  767. input_dev->close = atp_close;
  768. set_bit(EV_ABS, input_dev->evbit);
  769. input_set_abs_params(input_dev, ABS_X, 0,
  770. (dev->info->xsensors - 1) * dev->info->xfact - 1,
  771. dev->info->fuzz, 0);
  772. input_set_abs_params(input_dev, ABS_Y, 0,
  773. (dev->info->ysensors - 1) * dev->info->yfact - 1,
  774. dev->info->fuzz, 0);
  775. input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
  776. set_bit(EV_KEY, input_dev->evbit);
  777. set_bit(BTN_TOUCH, input_dev->keybit);
  778. set_bit(BTN_TOOL_FINGER, input_dev->keybit);
  779. set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
  780. set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
  781. set_bit(BTN_LEFT, input_dev->keybit);
  782. error = input_register_device(dev->input);
  783. if (error)
  784. goto err_free_buffer;
  785. /* save our data pointer in this interface device */
  786. usb_set_intfdata(iface, dev);
  787. INIT_WORK(&dev->work, atp_reinit);
  788. return 0;
  789. err_free_buffer:
  790. usb_free_coherent(dev->udev, dev->info->datalen,
  791. dev->data, dev->urb->transfer_dma);
  792. err_free_urb:
  793. usb_free_urb(dev->urb);
  794. err_free_devs:
  795. usb_set_intfdata(iface, NULL);
  796. kfree(dev);
  797. input_free_device(input_dev);
  798. return error;
  799. }
  800. static void atp_disconnect(struct usb_interface *iface)
  801. {
  802. struct atp *dev = usb_get_intfdata(iface);
  803. usb_set_intfdata(iface, NULL);
  804. if (dev) {
  805. usb_kill_urb(dev->urb);
  806. input_unregister_device(dev->input);
  807. usb_free_coherent(dev->udev, dev->info->datalen,
  808. dev->data, dev->urb->transfer_dma);
  809. usb_free_urb(dev->urb);
  810. kfree(dev);
  811. }
  812. dev_info(&iface->dev, "input: appletouch disconnected\n");
  813. }
  814. static int atp_recover(struct atp *dev)
  815. {
  816. int error;
  817. error = atp_handle_geyser(dev);
  818. if (error)
  819. return error;
  820. if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
  821. return -EIO;
  822. return 0;
  823. }
  824. static int atp_suspend(struct usb_interface *iface, pm_message_t message)
  825. {
  826. struct atp *dev = usb_get_intfdata(iface);
  827. usb_kill_urb(dev->urb);
  828. return 0;
  829. }
  830. static int atp_resume(struct usb_interface *iface)
  831. {
  832. struct atp *dev = usb_get_intfdata(iface);
  833. if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
  834. return -EIO;
  835. return 0;
  836. }
  837. static int atp_reset_resume(struct usb_interface *iface)
  838. {
  839. struct atp *dev = usb_get_intfdata(iface);
  840. return atp_recover(dev);
  841. }
  842. static struct usb_driver atp_driver = {
  843. .name = "appletouch",
  844. .probe = atp_probe,
  845. .disconnect = atp_disconnect,
  846. .suspend = atp_suspend,
  847. .resume = atp_resume,
  848. .reset_resume = atp_reset_resume,
  849. .id_table = atp_table,
  850. };
  851. module_usb_driver(atp_driver);