appletouch.c 21 KB

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