appletouch.c 20 KB

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