appletouch.c 25 KB

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