appletouch.c 21 KB

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