appletouch.c 24 KB

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