appletouch.c 22 KB

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