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 usb_device *udev)
  228. {
  229. char *data;
  230. int size;
  231. int i;
  232. int ret;
  233. data = kmalloc(8, GFP_KERNEL);
  234. if (!data) {
  235. err("Out of memory");
  236. return -ENOMEM;
  237. }
  238. size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  239. ATP_GEYSER_MODE_READ_REQUEST_ID,
  240. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  241. ATP_GEYSER_MODE_REQUEST_VALUE,
  242. ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000);
  243. if (size != 8) {
  244. dprintk("atp_geyser_init: read error\n");
  245. for (i = 0; i < 8; i++)
  246. dprintk("appletouch[%d]: %d\n", i, data[i]);
  247. err("Failed to read mode from device.");
  248. ret = -EIO;
  249. goto out_free;
  250. }
  251. /* Apply the mode switch */
  252. data[0] = ATP_GEYSER_MODE_VENDOR_VALUE;
  253. size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  254. ATP_GEYSER_MODE_WRITE_REQUEST_ID,
  255. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  256. ATP_GEYSER_MODE_REQUEST_VALUE,
  257. ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000);
  258. if (size != 8) {
  259. dprintk("atp_geyser_init: write error\n");
  260. for (i = 0; i < 8; i++)
  261. dprintk("appletouch[%d]: %d\n", i, data[i]);
  262. err("Failed to request geyser raw mode");
  263. ret = -EIO;
  264. goto out_free;
  265. }
  266. ret = 0;
  267. out_free:
  268. kfree(data);
  269. return ret;
  270. }
  271. /*
  272. * Reinitialise the device. This usually stops stream of empty packets
  273. * coming from it.
  274. */
  275. static void atp_reinit(struct work_struct *work)
  276. {
  277. struct atp *dev = container_of(work, struct atp, work);
  278. struct usb_device *udev = dev->udev;
  279. int retval;
  280. dprintk("appletouch: putting appletouch to sleep (reinit)\n");
  281. atp_geyser_init(udev);
  282. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  283. if (retval)
  284. err("atp_reinit: usb_submit_urb failed with error %d",
  285. retval);
  286. }
  287. static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
  288. int *z, int *fingers)
  289. {
  290. int i;
  291. /* values to calculate mean */
  292. int pcum = 0, psum = 0;
  293. int is_increasing = 0;
  294. *fingers = 0;
  295. for (i = 0; i < nb_sensors; i++) {
  296. if (xy_sensors[i] < threshold) {
  297. if (is_increasing)
  298. is_increasing = 0;
  299. continue;
  300. }
  301. /*
  302. * Makes the finger detection more versatile. For example,
  303. * two fingers with no gap will be detected. Also, my
  304. * tests show it less likely to have intermittent loss
  305. * of multiple finger readings while moving around (scrolling).
  306. *
  307. * Changes the multiple finger detection to counting humps on
  308. * sensors (transitions from nonincreasing to increasing)
  309. * instead of counting transitions from low sensors (no
  310. * finger reading) to high sensors (finger above
  311. * sensor)
  312. *
  313. * - Jason Parekh <jasonparekh@gmail.com>
  314. */
  315. if (i < 1 ||
  316. (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) {
  317. (*fingers)++;
  318. is_increasing = 1;
  319. } else if (i > 0 && (xy_sensors[i - 1] - xy_sensors[i] > threshold)) {
  320. is_increasing = 0;
  321. }
  322. /*
  323. * Subtracts threshold so a high sensor that just passes the
  324. * threshold won't skew the calculated absolute coordinate.
  325. * Fixes an issue where slowly moving the mouse would
  326. * occasionally jump a number of pixels (slowly moving the
  327. * finger makes this issue most apparent.)
  328. */
  329. pcum += (xy_sensors[i] - threshold) * i;
  330. psum += (xy_sensors[i] - threshold);
  331. }
  332. if (psum > 0) {
  333. *z = psum;
  334. return pcum * fact / psum;
  335. }
  336. return 0;
  337. }
  338. static inline void atp_report_fingers(struct input_dev *input, int fingers)
  339. {
  340. input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
  341. input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
  342. input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
  343. }
  344. /* Check URB status and for correct length of data package */
  345. #define ATP_URB_STATUS_SUCCESS 0
  346. #define ATP_URB_STATUS_ERROR 1
  347. #define ATP_URB_STATUS_ERROR_FATAL 2
  348. static int atp_status_check(struct urb *urb)
  349. {
  350. struct atp *dev = urb->context;
  351. switch (urb->status) {
  352. case 0:
  353. /* success */
  354. break;
  355. case -EOVERFLOW:
  356. if (!dev->overflow_warned) {
  357. printk(KERN_WARNING "appletouch: OVERFLOW with data "
  358. "length %d, actual length is %d\n",
  359. dev->info->datalen, dev->urb->actual_length);
  360. dev->overflow_warned = true;
  361. }
  362. case -ECONNRESET:
  363. case -ENOENT:
  364. case -ESHUTDOWN:
  365. /* This urb is terminated, clean up */
  366. dbg("atp_complete: urb shutting down with status: %d",
  367. urb->status);
  368. return ATP_URB_STATUS_ERROR_FATAL;
  369. default:
  370. dbg("atp_complete: nonzero urb status received: %d",
  371. urb->status);
  372. return ATP_URB_STATUS_ERROR;
  373. }
  374. /* drop incomplete datasets */
  375. if (dev->urb->actual_length != dev->info->datalen) {
  376. dprintk("appletouch: incomplete data package"
  377. " (first byte: %d, length: %d).\n",
  378. dev->data[0], dev->urb->actual_length);
  379. return ATP_URB_STATUS_ERROR;
  380. }
  381. return ATP_URB_STATUS_SUCCESS;
  382. }
  383. static void atp_detect_size(struct atp *dev)
  384. {
  385. int i;
  386. /* 17" Powerbooks have extra X sensors */
  387. for (i = dev->info->xsensors; i < ATP_XSENSORS; i++) {
  388. if (dev->xy_cur[i]) {
  389. printk(KERN_INFO "appletouch: 17\" model detected.\n");
  390. input_set_abs_params(dev->input, ABS_X, 0,
  391. (dev->info->xsensors_17 - 1) *
  392. dev->info->xfact - 1,
  393. ATP_FUZZ, 0);
  394. break;
  395. }
  396. }
  397. }
  398. /*
  399. * USB interrupt callback functions
  400. */
  401. /* Interrupt function for older touchpads: FOUNTAIN/GEYSER1/GEYSER2 */
  402. static void atp_complete_geyser_1_2(struct urb *urb)
  403. {
  404. int x, y, x_z, y_z, x_f, y_f;
  405. int retval, i, j;
  406. int key;
  407. struct atp *dev = urb->context;
  408. int status = atp_status_check(urb);
  409. if (status == ATP_URB_STATUS_ERROR_FATAL)
  410. return;
  411. else if (status == ATP_URB_STATUS_ERROR)
  412. goto exit;
  413. /* reorder the sensors values */
  414. if (dev->info == &geyser2_info) {
  415. memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
  416. /*
  417. * The values are laid out like this:
  418. * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
  419. * '-' is an unused value.
  420. */
  421. /* read X values */
  422. for (i = 0, j = 19; i < 20; i += 2, j += 3) {
  423. dev->xy_cur[i] = dev->data[j];
  424. dev->xy_cur[i + 1] = dev->data[j + 1];
  425. }
  426. /* read Y values */
  427. for (i = 0, j = 1; i < 9; i += 2, j += 3) {
  428. dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
  429. dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
  430. }
  431. } else {
  432. for (i = 0; i < 8; i++) {
  433. /* X values */
  434. dev->xy_cur[i + 0] = dev->data[5 * i + 2];
  435. dev->xy_cur[i + 8] = dev->data[5 * i + 4];
  436. dev->xy_cur[i + 16] = dev->data[5 * i + 42];
  437. if (i < 2)
  438. dev->xy_cur[i + 24] = dev->data[5 * i + 44];
  439. /* Y values */
  440. dev->xy_cur[ATP_XSENSORS + i] = dev->data[5 * i + 1];
  441. dev->xy_cur[ATP_XSENSORS + i + 8] = dev->data[5 * i + 3];
  442. }
  443. }
  444. dbg_dump("sample", dev->xy_cur);
  445. if (!dev->valid) {
  446. /* first sample */
  447. dev->valid = true;
  448. dev->x_old = dev->y_old = -1;
  449. /* Store first sample */
  450. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  451. /* Perform size detection, if not done already */
  452. if (unlikely(!dev->size_detect_done)) {
  453. atp_detect_size(dev);
  454. dev->size_detect_done = 1;
  455. goto exit;
  456. }
  457. }
  458. for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
  459. /* accumulate the change */
  460. signed char change = dev->xy_old[i] - dev->xy_cur[i];
  461. dev->xy_acc[i] -= change;
  462. /* prevent down drifting */
  463. if (dev->xy_acc[i] < 0)
  464. dev->xy_acc[i] = 0;
  465. }
  466. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  467. dbg_dump("accumulator", dev->xy_acc);
  468. x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
  469. dev->info->xfact, &x_z, &x_f);
  470. y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
  471. dev->info->yfact, &y_z, &y_f);
  472. key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
  473. if (x && y) {
  474. if (dev->x_old != -1) {
  475. x = (dev->x_old * 3 + x) >> 2;
  476. y = (dev->y_old * 3 + y) >> 2;
  477. dev->x_old = x;
  478. dev->y_old = y;
  479. if (debug > 1)
  480. printk(KERN_DEBUG "appletouch: "
  481. "X: %3d Y: %3d Xz: %3d Yz: %3d\n",
  482. x, y, x_z, y_z);
  483. input_report_key(dev->input, BTN_TOUCH, 1);
  484. input_report_abs(dev->input, ABS_X, x);
  485. input_report_abs(dev->input, ABS_Y, y);
  486. input_report_abs(dev->input, ABS_PRESSURE,
  487. min(ATP_PRESSURE, x_z + y_z));
  488. atp_report_fingers(dev->input, max(x_f, y_f));
  489. }
  490. dev->x_old = x;
  491. dev->y_old = y;
  492. } else if (!x && !y) {
  493. dev->x_old = dev->y_old = -1;
  494. input_report_key(dev->input, BTN_TOUCH, 0);
  495. input_report_abs(dev->input, ABS_PRESSURE, 0);
  496. atp_report_fingers(dev->input, 0);
  497. /* reset the accumulator on release */
  498. memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
  499. }
  500. input_report_key(dev->input, BTN_LEFT, key);
  501. input_sync(dev->input);
  502. exit:
  503. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  504. if (retval)
  505. err("atp_complete: usb_submit_urb failed with result %d",
  506. retval);
  507. }
  508. /* Interrupt function for older touchpads: GEYSER3/GEYSER4 */
  509. static void atp_complete_geyser_3_4(struct urb *urb)
  510. {
  511. int x, y, x_z, y_z, x_f, y_f;
  512. int retval, i, j;
  513. int key;
  514. struct atp *dev = urb->context;
  515. int status = atp_status_check(urb);
  516. if (status == ATP_URB_STATUS_ERROR_FATAL)
  517. return;
  518. else if (status == ATP_URB_STATUS_ERROR)
  519. goto exit;
  520. /* Reorder the sensors values:
  521. *
  522. * The values are laid out like this:
  523. * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
  524. * '-' is an unused value.
  525. */
  526. /* read X values */
  527. for (i = 0, j = 19; i < 20; i += 2, j += 3) {
  528. dev->xy_cur[i] = dev->data[j + 1];
  529. dev->xy_cur[i + 1] = dev->data[j + 2];
  530. }
  531. /* read Y values */
  532. for (i = 0, j = 1; i < 9; i += 2, j += 3) {
  533. dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
  534. dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
  535. }
  536. dbg_dump("sample", dev->xy_cur);
  537. /* Just update the base values (i.e. touchpad in untouched state) */
  538. if (dev->data[dev->info->datalen - 1] & ATP_STATUS_BASE_UPDATE) {
  539. dprintk("appletouch: updated base values\n");
  540. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  541. goto exit;
  542. }
  543. for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
  544. /* calculate the change */
  545. dev->xy_acc[i] = dev->xy_cur[i] - dev->xy_old[i];
  546. /* this is a round-robin value, so couple with that */
  547. if (dev->xy_acc[i] > 127)
  548. dev->xy_acc[i] -= 256;
  549. if (dev->xy_acc[i] < -127)
  550. dev->xy_acc[i] += 256;
  551. /* prevent down drifting */
  552. if (dev->xy_acc[i] < 0)
  553. dev->xy_acc[i] = 0;
  554. }
  555. dbg_dump("accumulator", dev->xy_acc);
  556. x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
  557. dev->info->xfact, &x_z, &x_f);
  558. y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
  559. dev->info->yfact, &y_z, &y_f);
  560. key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
  561. if (x && y) {
  562. if (dev->x_old != -1) {
  563. x = (dev->x_old * 3 + x) >> 2;
  564. y = (dev->y_old * 3 + y) >> 2;
  565. dev->x_old = x;
  566. dev->y_old = y;
  567. if (debug > 1)
  568. printk(KERN_DEBUG "appletouch: X: %3d Y: %3d "
  569. "Xz: %3d Yz: %3d\n",
  570. x, y, x_z, y_z);
  571. input_report_key(dev->input, BTN_TOUCH, 1);
  572. input_report_abs(dev->input, ABS_X, x);
  573. input_report_abs(dev->input, ABS_Y, y);
  574. input_report_abs(dev->input, ABS_PRESSURE,
  575. min(ATP_PRESSURE, x_z + y_z));
  576. atp_report_fingers(dev->input, max(x_f, y_f));
  577. }
  578. dev->x_old = x;
  579. dev->y_old = y;
  580. } else if (!x && !y) {
  581. dev->x_old = dev->y_old = -1;
  582. input_report_key(dev->input, BTN_TOUCH, 0);
  583. input_report_abs(dev->input, ABS_PRESSURE, 0);
  584. atp_report_fingers(dev->input, 0);
  585. /* reset the accumulator on release */
  586. memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
  587. }
  588. input_report_key(dev->input, BTN_LEFT, key);
  589. input_sync(dev->input);
  590. /*
  591. * Geysers 3/4 will continue to send packets continually after
  592. * the first touch unless reinitialised. Do so if it's been
  593. * idle for a while in order to avoid waking the kernel up
  594. * several hundred times a second.
  595. */
  596. /*
  597. * Button must not be pressed when entering suspend,
  598. * otherwise we will never release the button.
  599. */
  600. if (!x && !y && !key) {
  601. dev->idlecount++;
  602. if (dev->idlecount == 10) {
  603. dev->x_old = dev->y_old = -1;
  604. dev->idlecount = 0;
  605. schedule_work(&dev->work);
  606. /* Don't resubmit urb here, wait for reinit */
  607. return;
  608. }
  609. } else
  610. dev->idlecount = 0;
  611. exit:
  612. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  613. if (retval)
  614. err("atp_complete: usb_submit_urb failed with result %d",
  615. retval);
  616. }
  617. static int atp_open(struct input_dev *input)
  618. {
  619. struct atp *dev = input_get_drvdata(input);
  620. if (usb_submit_urb(dev->urb, GFP_ATOMIC))
  621. return -EIO;
  622. dev->open = 1;
  623. return 0;
  624. }
  625. static void atp_close(struct input_dev *input)
  626. {
  627. struct atp *dev = input_get_drvdata(input);
  628. usb_kill_urb(dev->urb);
  629. cancel_work_sync(&dev->work);
  630. dev->open = 0;
  631. }
  632. static int atp_handle_geyser(struct atp *dev)
  633. {
  634. struct usb_device *udev = dev->udev;
  635. if (dev->info != &fountain_info) {
  636. /* switch to raw sensor mode */
  637. if (atp_geyser_init(udev))
  638. return -EIO;
  639. printk(KERN_INFO "appletouch: Geyser mode initialized.\n");
  640. }
  641. return 0;
  642. }
  643. static int atp_probe(struct usb_interface *iface,
  644. const struct usb_device_id *id)
  645. {
  646. struct atp *dev;
  647. struct input_dev *input_dev;
  648. struct usb_device *udev = interface_to_usbdev(iface);
  649. struct usb_host_interface *iface_desc;
  650. struct usb_endpoint_descriptor *endpoint;
  651. int int_in_endpointAddr = 0;
  652. int i, error = -ENOMEM;
  653. const struct atp_info *info = (const struct atp_info *)id->driver_info;
  654. /* set up the endpoint information */
  655. /* use only the first interrupt-in endpoint */
  656. iface_desc = iface->cur_altsetting;
  657. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  658. endpoint = &iface_desc->endpoint[i].desc;
  659. if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
  660. /* we found an interrupt in endpoint */
  661. int_in_endpointAddr = endpoint->bEndpointAddress;
  662. break;
  663. }
  664. }
  665. if (!int_in_endpointAddr) {
  666. err("Could not find int-in endpoint");
  667. return -EIO;
  668. }
  669. /* allocate memory for our device state and initialize it */
  670. dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
  671. input_dev = input_allocate_device();
  672. if (!dev || !input_dev) {
  673. err("Out of memory");
  674. goto err_free_devs;
  675. }
  676. dev->udev = udev;
  677. dev->input = input_dev;
  678. dev->info = info;
  679. dev->overflow_warned = false;
  680. dev->urb = usb_alloc_urb(0, GFP_KERNEL);
  681. if (!dev->urb)
  682. goto err_free_devs;
  683. dev->data = usb_alloc_coherent(dev->udev, dev->info->datalen, GFP_KERNEL,
  684. &dev->urb->transfer_dma);
  685. if (!dev->data)
  686. goto err_free_urb;
  687. usb_fill_int_urb(dev->urb, udev,
  688. usb_rcvintpipe(udev, int_in_endpointAddr),
  689. dev->data, dev->info->datalen,
  690. dev->info->callback, dev, 1);
  691. error = atp_handle_geyser(dev);
  692. if (error)
  693. goto err_free_buffer;
  694. usb_make_path(udev, dev->phys, sizeof(dev->phys));
  695. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  696. input_dev->name = "appletouch";
  697. input_dev->phys = dev->phys;
  698. usb_to_input_id(dev->udev, &input_dev->id);
  699. input_dev->dev.parent = &iface->dev;
  700. input_set_drvdata(input_dev, dev);
  701. input_dev->open = atp_open;
  702. input_dev->close = atp_close;
  703. set_bit(EV_ABS, input_dev->evbit);
  704. input_set_abs_params(input_dev, ABS_X, 0,
  705. (dev->info->xsensors - 1) * dev->info->xfact - 1,
  706. ATP_FUZZ, 0);
  707. input_set_abs_params(input_dev, ABS_Y, 0,
  708. (dev->info->ysensors - 1) * dev->info->yfact - 1,
  709. ATP_FUZZ, 0);
  710. input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
  711. set_bit(EV_KEY, input_dev->evbit);
  712. set_bit(BTN_TOUCH, input_dev->keybit);
  713. set_bit(BTN_TOOL_FINGER, input_dev->keybit);
  714. set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
  715. set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
  716. set_bit(BTN_LEFT, input_dev->keybit);
  717. error = input_register_device(dev->input);
  718. if (error)
  719. goto err_free_buffer;
  720. /* save our data pointer in this interface device */
  721. usb_set_intfdata(iface, dev);
  722. INIT_WORK(&dev->work, atp_reinit);
  723. return 0;
  724. err_free_buffer:
  725. usb_free_coherent(dev->udev, dev->info->datalen,
  726. dev->data, dev->urb->transfer_dma);
  727. err_free_urb:
  728. usb_free_urb(dev->urb);
  729. err_free_devs:
  730. usb_set_intfdata(iface, NULL);
  731. kfree(dev);
  732. input_free_device(input_dev);
  733. return error;
  734. }
  735. static void atp_disconnect(struct usb_interface *iface)
  736. {
  737. struct atp *dev = usb_get_intfdata(iface);
  738. usb_set_intfdata(iface, NULL);
  739. if (dev) {
  740. usb_kill_urb(dev->urb);
  741. input_unregister_device(dev->input);
  742. usb_free_coherent(dev->udev, dev->info->datalen,
  743. dev->data, dev->urb->transfer_dma);
  744. usb_free_urb(dev->urb);
  745. kfree(dev);
  746. }
  747. printk(KERN_INFO "input: appletouch disconnected\n");
  748. }
  749. static int atp_recover(struct atp *dev)
  750. {
  751. int error;
  752. error = atp_handle_geyser(dev);
  753. if (error)
  754. return error;
  755. if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
  756. return -EIO;
  757. return 0;
  758. }
  759. static int atp_suspend(struct usb_interface *iface, pm_message_t message)
  760. {
  761. struct atp *dev = usb_get_intfdata(iface);
  762. usb_kill_urb(dev->urb);
  763. return 0;
  764. }
  765. static int atp_resume(struct usb_interface *iface)
  766. {
  767. struct atp *dev = usb_get_intfdata(iface);
  768. if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
  769. return -EIO;
  770. return 0;
  771. }
  772. static int atp_reset_resume(struct usb_interface *iface)
  773. {
  774. struct atp *dev = usb_get_intfdata(iface);
  775. return atp_recover(dev);
  776. }
  777. static struct usb_driver atp_driver = {
  778. .name = "appletouch",
  779. .probe = atp_probe,
  780. .disconnect = atp_disconnect,
  781. .suspend = atp_suspend,
  782. .resume = atp_resume,
  783. .reset_resume = atp_reset_resume,
  784. .id_table = atp_table,
  785. };
  786. module_usb_driver(atp_driver);