appletouch.c 24 KB

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