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