appletouch.c 21 KB

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