appletouch.c 24 KB

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