appletouch.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Apple USB Touchpad (for post-February 2005 PowerBooks and MacBooks) driver
  4. *
  5. * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2005-2008 Johannes Berg (johannes@sipsolutions.net)
  7. * Copyright (C) 2005-2008 Stelian Pop (stelian@popies.net)
  8. * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de)
  9. * Copyright (C) 2005 Peter Osterlund (petero2@telia.com)
  10. * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch)
  11. * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch)
  12. * Copyright (C) 2007-2008 Sven Anders (anders@anduras.de)
  13. *
  14. * Thanks to Alex Harper <basilisk@foobox.net> for his inputs.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <linux/usb/input.h>
  21. /*
  22. * Note: We try to keep the touchpad aspect ratio while still doing only
  23. * simple arithmetics:
  24. * 0 <= x <= (xsensors - 1) * xfact
  25. * 0 <= y <= (ysensors - 1) * yfact
  26. */
  27. struct atp_info {
  28. int xsensors; /* number of X sensors */
  29. int xsensors_17; /* 17" models have more sensors */
  30. int ysensors; /* number of Y sensors */
  31. int xfact; /* X multiplication factor */
  32. int yfact; /* Y multiplication factor */
  33. int datalen; /* size of USB transfers */
  34. void (*callback)(struct urb *); /* callback function */
  35. int fuzz; /* fuzz touchpad generates */
  36. };
  37. static void atp_complete_geyser_1_2(struct urb *urb);
  38. static void atp_complete_geyser_3_4(struct urb *urb);
  39. static const struct atp_info fountain_info = {
  40. .xsensors = 16,
  41. .xsensors_17 = 26,
  42. .ysensors = 16,
  43. .xfact = 64,
  44. .yfact = 43,
  45. .datalen = 81,
  46. .callback = atp_complete_geyser_1_2,
  47. .fuzz = 16,
  48. };
  49. static const struct atp_info geyser1_info = {
  50. .xsensors = 16,
  51. .xsensors_17 = 26,
  52. .ysensors = 16,
  53. .xfact = 64,
  54. .yfact = 43,
  55. .datalen = 81,
  56. .callback = atp_complete_geyser_1_2,
  57. .fuzz = 16,
  58. };
  59. static const struct atp_info geyser2_info = {
  60. .xsensors = 15,
  61. .xsensors_17 = 20,
  62. .ysensors = 9,
  63. .xfact = 64,
  64. .yfact = 43,
  65. .datalen = 64,
  66. .callback = atp_complete_geyser_1_2,
  67. .fuzz = 0,
  68. };
  69. static const struct atp_info geyser3_info = {
  70. .xsensors = 20,
  71. .ysensors = 10,
  72. .xfact = 64,
  73. .yfact = 64,
  74. .datalen = 64,
  75. .callback = atp_complete_geyser_3_4,
  76. .fuzz = 0,
  77. };
  78. static const struct atp_info geyser4_info = {
  79. .xsensors = 20,
  80. .ysensors = 10,
  81. .xfact = 64,
  82. .yfact = 64,
  83. .datalen = 64,
  84. .callback = atp_complete_geyser_3_4,
  85. .fuzz = 0,
  86. };
  87. #define ATP_DEVICE(prod, info) \
  88. { \
  89. .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
  90. USB_DEVICE_ID_MATCH_INT_CLASS | \
  91. USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
  92. .idVendor = 0x05ac, /* Apple */ \
  93. .idProduct = (prod), \
  94. .bInterfaceClass = 0x03, \
  95. .bInterfaceProtocol = 0x02, \
  96. .driver_info = (unsigned long) &info, \
  97. }
  98. /*
  99. * Table of devices (Product IDs) that work with this driver.
  100. * (The names come from Info.plist in AppleUSBTrackpad.kext,
  101. * According to Info.plist Geyser IV is the same as Geyser III.)
  102. */
  103. static const struct usb_device_id atp_table[] = {
  104. /* PowerBooks Feb 2005, iBooks G4 */
  105. ATP_DEVICE(0x020e, fountain_info), /* FOUNTAIN ANSI */
  106. ATP_DEVICE(0x020f, fountain_info), /* FOUNTAIN ISO */
  107. ATP_DEVICE(0x030a, fountain_info), /* FOUNTAIN TP ONLY */
  108. ATP_DEVICE(0x030b, geyser1_info), /* GEYSER 1 TP ONLY */
  109. /* PowerBooks Oct 2005 */
  110. ATP_DEVICE(0x0214, geyser2_info), /* GEYSER 2 ANSI */
  111. ATP_DEVICE(0x0215, geyser2_info), /* GEYSER 2 ISO */
  112. ATP_DEVICE(0x0216, geyser2_info), /* GEYSER 2 JIS */
  113. /* Core Duo MacBook & MacBook Pro */
  114. ATP_DEVICE(0x0217, geyser3_info), /* GEYSER 3 ANSI */
  115. ATP_DEVICE(0x0218, geyser3_info), /* GEYSER 3 ISO */
  116. ATP_DEVICE(0x0219, geyser3_info), /* GEYSER 3 JIS */
  117. /* Core2 Duo MacBook & MacBook Pro */
  118. ATP_DEVICE(0x021a, geyser4_info), /* GEYSER 4 ANSI */
  119. ATP_DEVICE(0x021b, geyser4_info), /* GEYSER 4 ISO */
  120. ATP_DEVICE(0x021c, geyser4_info), /* GEYSER 4 JIS */
  121. /* Core2 Duo MacBook3,1 */
  122. ATP_DEVICE(0x0229, geyser4_info), /* GEYSER 4 HF ANSI */
  123. ATP_DEVICE(0x022a, geyser4_info), /* GEYSER 4 HF ISO */
  124. ATP_DEVICE(0x022b, geyser4_info), /* GEYSER 4 HF JIS */
  125. /* Terminating entry */
  126. { }
  127. };
  128. MODULE_DEVICE_TABLE(usb, atp_table);
  129. /* maximum number of sensors */
  130. #define ATP_XSENSORS 26
  131. #define ATP_YSENSORS 16
  132. /*
  133. * The largest possible bank of sensors with additional buffer of 4 extra values
  134. * on either side, for an array of smoothed sensor values.
  135. */
  136. #define ATP_SMOOTHSIZE 34
  137. /* maximum pressure this driver will report */
  138. #define ATP_PRESSURE 300
  139. /*
  140. * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
  141. * ignored.
  142. */
  143. #define ATP_THRESHOLD 5
  144. /*
  145. * How far we'll bitshift our sensor values before averaging them. Mitigates
  146. * rounding errors.
  147. */
  148. #define ATP_SCALE 12
  149. /* Geyser initialization constants */
  150. #define ATP_GEYSER_MODE_READ_REQUEST_ID 1
  151. #define ATP_GEYSER_MODE_WRITE_REQUEST_ID 9
  152. #define ATP_GEYSER_MODE_REQUEST_VALUE 0x300
  153. #define ATP_GEYSER_MODE_REQUEST_INDEX 0
  154. #define ATP_GEYSER_MODE_VENDOR_VALUE 0x04
  155. /**
  156. * enum atp_status_bits - status bit meanings
  157. *
  158. * These constants represent the meaning of the status bits.
  159. * (only Geyser 3/4)
  160. *
  161. * @ATP_STATUS_BUTTON: The button was pressed
  162. * @ATP_STATUS_BASE_UPDATE: Update of the base values (untouched pad)
  163. * @ATP_STATUS_FROM_RESET: Reset previously performed
  164. */
  165. enum atp_status_bits {
  166. ATP_STATUS_BUTTON = BIT(0),
  167. ATP_STATUS_BASE_UPDATE = BIT(2),
  168. ATP_STATUS_FROM_RESET = BIT(4),
  169. };
  170. /* Structure to hold all of our device specific stuff */
  171. struct atp {
  172. char phys[64];
  173. struct usb_device *udev; /* usb device */
  174. struct usb_interface *intf; /* usb interface */
  175. struct urb *urb; /* usb request block */
  176. u8 *data; /* transferred data */
  177. struct input_dev *input; /* input dev */
  178. const struct atp_info *info; /* touchpad model */
  179. bool open;
  180. bool valid; /* are the samples valid? */
  181. bool size_detect_done;
  182. bool overflow_warned;
  183. int fingers_old; /* last reported finger count */
  184. int x_old; /* last reported x/y, */
  185. int y_old; /* used for smoothing */
  186. signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
  187. signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
  188. int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
  189. int smooth[ATP_SMOOTHSIZE];
  190. int smooth_tmp[ATP_SMOOTHSIZE];
  191. int idlecount; /* number of empty packets */
  192. struct work_struct work;
  193. };
  194. #define dbg_dump(msg, tab) \
  195. if (debug > 1) { \
  196. int __i; \
  197. printk(KERN_DEBUG "appletouch: %s", msg); \
  198. for (__i = 0; __i < ATP_XSENSORS + ATP_YSENSORS; __i++) \
  199. printk(" %02x", tab[__i]); \
  200. printk("\n"); \
  201. }
  202. #define dprintk(format, a...) \
  203. do { \
  204. if (debug) \
  205. printk(KERN_DEBUG format, ##a); \
  206. } while (0)
  207. MODULE_AUTHOR("Johannes Berg");
  208. MODULE_AUTHOR("Stelian Pop");
  209. MODULE_AUTHOR("Frank Arnold");
  210. MODULE_AUTHOR("Michael Hanselmann");
  211. MODULE_AUTHOR("Sven Anders");
  212. MODULE_DESCRIPTION("Apple PowerBook and MacBook USB touchpad driver");
  213. MODULE_LICENSE("GPL");
  214. /*
  215. * Make the threshold a module parameter
  216. */
  217. static int threshold = ATP_THRESHOLD;
  218. module_param(threshold, int, 0644);
  219. MODULE_PARM_DESC(threshold, "Discard any change in data from a sensor"
  220. " (the trackpad has many of these sensors)"
  221. " less than this value.");
  222. static int debug;
  223. module_param(debug, int, 0644);
  224. MODULE_PARM_DESC(debug, "Activate debugging output");
  225. /*
  226. * By default newer Geyser devices send standard USB HID mouse
  227. * packets (Report ID 2). This code changes device mode, so it
  228. * sends raw sensor reports (Report ID 5).
  229. */
  230. static int atp_geyser_init(struct atp *dev)
  231. {
  232. struct usb_device *udev = dev->udev;
  233. char *data;
  234. int size;
  235. int i;
  236. int ret;
  237. data = kmalloc(8, GFP_KERNEL);
  238. if (!data) {
  239. dev_err(&dev->intf->dev, "Out of memory\n");
  240. return -ENOMEM;
  241. }
  242. size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  243. ATP_GEYSER_MODE_READ_REQUEST_ID,
  244. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  245. ATP_GEYSER_MODE_REQUEST_VALUE,
  246. ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000);
  247. if (size != 8) {
  248. dprintk("atp_geyser_init: read error\n");
  249. for (i = 0; i < 8; i++)
  250. dprintk("appletouch[%d]: %d\n", i, data[i]);
  251. dev_err(&dev->intf->dev, "Failed to read mode from device.\n");
  252. ret = -EIO;
  253. goto out_free;
  254. }
  255. /* Apply the mode switch */
  256. data[0] = ATP_GEYSER_MODE_VENDOR_VALUE;
  257. size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  258. ATP_GEYSER_MODE_WRITE_REQUEST_ID,
  259. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  260. ATP_GEYSER_MODE_REQUEST_VALUE,
  261. ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000);
  262. if (size != 8) {
  263. dprintk("atp_geyser_init: write error\n");
  264. for (i = 0; i < 8; i++)
  265. dprintk("appletouch[%d]: %d\n", i, data[i]);
  266. dev_err(&dev->intf->dev, "Failed to request geyser raw mode\n");
  267. ret = -EIO;
  268. goto out_free;
  269. }
  270. ret = 0;
  271. out_free:
  272. kfree(data);
  273. return ret;
  274. }
  275. /*
  276. * Reinitialise the device. This usually stops stream of empty packets
  277. * coming from it.
  278. */
  279. static void atp_reinit(struct work_struct *work)
  280. {
  281. struct atp *dev = container_of(work, struct atp, work);
  282. int retval;
  283. dprintk("appletouch: putting appletouch to sleep (reinit)\n");
  284. atp_geyser_init(dev);
  285. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  286. if (retval)
  287. dev_err(&dev->intf->dev,
  288. "atp_reinit: usb_submit_urb failed with error %d\n",
  289. retval);
  290. }
  291. static int atp_calculate_abs(struct atp *dev, int offset, int nb_sensors,
  292. int fact, int *z, int *fingers)
  293. {
  294. int i, pass;
  295. /*
  296. * Use offset to point xy_sensors at the first value in dev->xy_acc
  297. * for whichever dimension we're looking at this particular go-round.
  298. */
  299. int *xy_sensors = dev->xy_acc + offset;
  300. /* values to calculate mean */
  301. int pcum = 0, psum = 0;
  302. int is_increasing = 0;
  303. *fingers = 0;
  304. for (i = 0; i < nb_sensors; i++) {
  305. if (xy_sensors[i] < threshold) {
  306. if (is_increasing)
  307. is_increasing = 0;
  308. /*
  309. * Makes the finger detection more versatile. For example,
  310. * two fingers with no gap will be detected. Also, my
  311. * tests show it less likely to have intermittent loss
  312. * of multiple finger readings while moving around (scrolling).
  313. *
  314. * Changes the multiple finger detection to counting humps on
  315. * sensors (transitions from nonincreasing to increasing)
  316. * instead of counting transitions from low sensors (no
  317. * finger reading) to high sensors (finger above
  318. * sensor)
  319. *
  320. * - Jason Parekh <jasonparekh@gmail.com>
  321. */
  322. } else if (i < 1 ||
  323. (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) {
  324. (*fingers)++;
  325. is_increasing = 1;
  326. } else if (i > 0 && (xy_sensors[i - 1] - xy_sensors[i] > threshold)) {
  327. is_increasing = 0;
  328. }
  329. }
  330. if (*fingers < 1) /* No need to continue if no fingers are found. */
  331. return 0;
  332. /*
  333. * Use a smoothed version of sensor data for movement calculations, to
  334. * combat noise without needing to rely so heavily on a threshold.
  335. * This improves tracking.
  336. *
  337. * The smoothed array is bigger than the original so that the smoothing
  338. * doesn't result in edge values being truncated.
  339. */
  340. memset(dev->smooth, 0, 4 * sizeof(dev->smooth[0]));
  341. /* Pull base values, scaled up to help avoid truncation errors. */
  342. for (i = 0; i < nb_sensors; i++)
  343. dev->smooth[i + 4] = xy_sensors[i] << ATP_SCALE;
  344. memset(&dev->smooth[nb_sensors + 4], 0, 4 * sizeof(dev->smooth[0]));
  345. for (pass = 0; pass < 4; pass++) {
  346. /* Handle edge. */
  347. dev->smooth_tmp[0] = (dev->smooth[0] + dev->smooth[1]) / 2;
  348. /* Average values with neighbors. */
  349. for (i = 1; i < nb_sensors + 7; i++)
  350. dev->smooth_tmp[i] = (dev->smooth[i - 1] +
  351. dev->smooth[i] * 2 +
  352. dev->smooth[i + 1]) / 4;
  353. /* Handle other edge. */
  354. dev->smooth_tmp[i] = (dev->smooth[i - 1] + dev->smooth[i]) / 2;
  355. memcpy(dev->smooth, dev->smooth_tmp, sizeof(dev->smooth));
  356. }
  357. for (i = 0; i < nb_sensors + 8; i++) {
  358. /*
  359. * Skip values if they're small enough to be truncated to 0
  360. * by scale. Mostly noise.
  361. */
  362. if ((dev->smooth[i] >> ATP_SCALE) > 0) {
  363. pcum += dev->smooth[i] * i;
  364. psum += dev->smooth[i];
  365. }
  366. }
  367. if (psum > 0) {
  368. *z = psum >> ATP_SCALE; /* Scale down pressure output. */
  369. return pcum * fact / psum;
  370. }
  371. return 0;
  372. }
  373. static inline void atp_report_fingers(struct input_dev *input, int fingers)
  374. {
  375. input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
  376. input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
  377. input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
  378. }
  379. /* Check URB status and for correct length of data package */
  380. #define ATP_URB_STATUS_SUCCESS 0
  381. #define ATP_URB_STATUS_ERROR 1
  382. #define ATP_URB_STATUS_ERROR_FATAL 2
  383. static int atp_status_check(struct urb *urb)
  384. {
  385. struct atp *dev = urb->context;
  386. struct usb_interface *intf = dev->intf;
  387. switch (urb->status) {
  388. case 0:
  389. /* success */
  390. break;
  391. case -EOVERFLOW:
  392. if (!dev->overflow_warned) {
  393. dev_warn(&intf->dev,
  394. "appletouch: OVERFLOW with data length %d, actual length is %d\n",
  395. dev->info->datalen, dev->urb->actual_length);
  396. dev->overflow_warned = true;
  397. }
  398. fallthrough;
  399. case -ECONNRESET:
  400. case -ENOENT:
  401. case -ESHUTDOWN:
  402. /* This urb is terminated, clean up */
  403. dev_dbg(&intf->dev,
  404. "atp_complete: urb shutting down with status: %d\n",
  405. urb->status);
  406. return ATP_URB_STATUS_ERROR_FATAL;
  407. default:
  408. dev_dbg(&intf->dev,
  409. "atp_complete: nonzero urb status received: %d\n",
  410. urb->status);
  411. return ATP_URB_STATUS_ERROR;
  412. }
  413. /* drop incomplete datasets */
  414. if (dev->urb->actual_length != dev->info->datalen) {
  415. dprintk("appletouch: incomplete data package"
  416. " (first byte: %d, length: %d).\n",
  417. dev->data[0], dev->urb->actual_length);
  418. return ATP_URB_STATUS_ERROR;
  419. }
  420. return ATP_URB_STATUS_SUCCESS;
  421. }
  422. static void atp_detect_size(struct atp *dev)
  423. {
  424. int i;
  425. /* 17" Powerbooks have extra X sensors */
  426. for (i = dev->info->xsensors; i < ATP_XSENSORS; i++) {
  427. if (dev->xy_cur[i]) {
  428. dev_info(&dev->intf->dev,
  429. "appletouch: 17\" model detected.\n");
  430. input_set_abs_params(dev->input, ABS_X, 0,
  431. (dev->info->xsensors_17 - 1) *
  432. dev->info->xfact - 1,
  433. dev->info->fuzz, 0);
  434. break;
  435. }
  436. }
  437. }
  438. /*
  439. * USB interrupt callback functions
  440. */
  441. /* Interrupt function for older touchpads: FOUNTAIN/GEYSER1/GEYSER2 */
  442. static void atp_complete_geyser_1_2(struct urb *urb)
  443. {
  444. int x, y, x_z, y_z, x_f, y_f;
  445. int retval, i, j;
  446. int key, fingers;
  447. struct atp *dev = urb->context;
  448. int status = atp_status_check(urb);
  449. if (status == ATP_URB_STATUS_ERROR_FATAL)
  450. return;
  451. else if (status == ATP_URB_STATUS_ERROR)
  452. goto exit;
  453. /* reorder the sensors values */
  454. if (dev->info == &geyser2_info) {
  455. memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
  456. /*
  457. * The values are laid out like this:
  458. * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
  459. * '-' is an unused value.
  460. */
  461. /* read X values */
  462. for (i = 0, j = 19; i < 20; i += 2, j += 3) {
  463. dev->xy_cur[i] = dev->data[j];
  464. dev->xy_cur[i + 1] = dev->data[j + 1];
  465. }
  466. /* read Y values */
  467. for (i = 0, j = 1; i < 9; i += 2, j += 3) {
  468. dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
  469. dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
  470. }
  471. } else {
  472. for (i = 0; i < 8; i++) {
  473. /* X values */
  474. dev->xy_cur[i + 0] = dev->data[5 * i + 2];
  475. dev->xy_cur[i + 8] = dev->data[5 * i + 4];
  476. dev->xy_cur[i + 16] = dev->data[5 * i + 42];
  477. if (i < 2)
  478. dev->xy_cur[i + 24] = dev->data[5 * i + 44];
  479. /* Y values */
  480. dev->xy_cur[ATP_XSENSORS + i] = dev->data[5 * i + 1];
  481. dev->xy_cur[ATP_XSENSORS + i + 8] = dev->data[5 * i + 3];
  482. }
  483. }
  484. dbg_dump("sample", dev->xy_cur);
  485. if (!dev->valid) {
  486. /* first sample */
  487. dev->valid = true;
  488. dev->x_old = dev->y_old = -1;
  489. /* Store first sample */
  490. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  491. /* Perform size detection, if not done already */
  492. if (unlikely(!dev->size_detect_done)) {
  493. atp_detect_size(dev);
  494. dev->size_detect_done = true;
  495. goto exit;
  496. }
  497. }
  498. for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
  499. /* accumulate the change */
  500. signed char change = dev->xy_old[i] - dev->xy_cur[i];
  501. dev->xy_acc[i] -= change;
  502. /* prevent down drifting */
  503. if (dev->xy_acc[i] < 0)
  504. dev->xy_acc[i] = 0;
  505. }
  506. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  507. dbg_dump("accumulator", dev->xy_acc);
  508. x = atp_calculate_abs(dev, 0, ATP_XSENSORS,
  509. dev->info->xfact, &x_z, &x_f);
  510. y = atp_calculate_abs(dev, ATP_XSENSORS, ATP_YSENSORS,
  511. dev->info->yfact, &y_z, &y_f);
  512. key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
  513. fingers = max(x_f, y_f);
  514. if (x && y && fingers == dev->fingers_old) {
  515. if (dev->x_old != -1) {
  516. x = (dev->x_old * 7 + x) >> 3;
  517. y = (dev->y_old * 7 + y) >> 3;
  518. dev->x_old = x;
  519. dev->y_old = y;
  520. if (debug > 1)
  521. printk(KERN_DEBUG "appletouch: "
  522. "X: %3d Y: %3d Xz: %3d Yz: %3d\n",
  523. x, y, x_z, y_z);
  524. input_report_key(dev->input, BTN_TOUCH, 1);
  525. input_report_abs(dev->input, ABS_X, x);
  526. input_report_abs(dev->input, ABS_Y, y);
  527. input_report_abs(dev->input, ABS_PRESSURE,
  528. min(ATP_PRESSURE, x_z + y_z));
  529. atp_report_fingers(dev->input, fingers);
  530. }
  531. dev->x_old = x;
  532. dev->y_old = y;
  533. } else if (!x && !y) {
  534. dev->x_old = dev->y_old = -1;
  535. dev->fingers_old = 0;
  536. input_report_key(dev->input, BTN_TOUCH, 0);
  537. input_report_abs(dev->input, ABS_PRESSURE, 0);
  538. atp_report_fingers(dev->input, 0);
  539. /* reset the accumulator on release */
  540. memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
  541. }
  542. if (fingers != dev->fingers_old)
  543. dev->x_old = dev->y_old = -1;
  544. dev->fingers_old = fingers;
  545. input_report_key(dev->input, BTN_LEFT, key);
  546. input_sync(dev->input);
  547. exit:
  548. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  549. if (retval)
  550. dev_err(&dev->intf->dev,
  551. "atp_complete: usb_submit_urb failed with result %d\n",
  552. retval);
  553. }
  554. /* Interrupt function for older touchpads: GEYSER3/GEYSER4 */
  555. static void atp_complete_geyser_3_4(struct urb *urb)
  556. {
  557. int x, y, x_z, y_z, x_f, y_f;
  558. int retval, i, j;
  559. int key, fingers;
  560. struct atp *dev = urb->context;
  561. int status = atp_status_check(urb);
  562. if (status == ATP_URB_STATUS_ERROR_FATAL)
  563. return;
  564. else if (status == ATP_URB_STATUS_ERROR)
  565. goto exit;
  566. /* Reorder the sensors values:
  567. *
  568. * The values are laid out like this:
  569. * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
  570. * '-' is an unused value.
  571. */
  572. /* read X values */
  573. for (i = 0, j = 19; i < 20; i += 2, j += 3) {
  574. dev->xy_cur[i] = dev->data[j + 1];
  575. dev->xy_cur[i + 1] = dev->data[j + 2];
  576. }
  577. /* read Y values */
  578. for (i = 0, j = 1; i < 9; i += 2, j += 3) {
  579. dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
  580. dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
  581. }
  582. dbg_dump("sample", dev->xy_cur);
  583. /* Just update the base values (i.e. touchpad in untouched state) */
  584. if (dev->data[dev->info->datalen - 1] & ATP_STATUS_BASE_UPDATE) {
  585. dprintk("appletouch: updated base values\n");
  586. memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
  587. goto exit;
  588. }
  589. for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
  590. /* calculate the change */
  591. dev->xy_acc[i] = dev->xy_cur[i] - dev->xy_old[i];
  592. /* this is a round-robin value, so couple with that */
  593. if (dev->xy_acc[i] > 127)
  594. dev->xy_acc[i] -= 256;
  595. if (dev->xy_acc[i] < -127)
  596. dev->xy_acc[i] += 256;
  597. /* prevent down drifting */
  598. if (dev->xy_acc[i] < 0)
  599. dev->xy_acc[i] = 0;
  600. }
  601. dbg_dump("accumulator", dev->xy_acc);
  602. x = atp_calculate_abs(dev, 0, ATP_XSENSORS,
  603. dev->info->xfact, &x_z, &x_f);
  604. y = atp_calculate_abs(dev, ATP_XSENSORS, ATP_YSENSORS,
  605. dev->info->yfact, &y_z, &y_f);
  606. key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
  607. fingers = max(x_f, y_f);
  608. if (x && y && fingers == dev->fingers_old) {
  609. if (dev->x_old != -1) {
  610. x = (dev->x_old * 7 + x) >> 3;
  611. y = (dev->y_old * 7 + y) >> 3;
  612. dev->x_old = x;
  613. dev->y_old = y;
  614. if (debug > 1)
  615. printk(KERN_DEBUG "appletouch: X: %3d Y: %3d "
  616. "Xz: %3d Yz: %3d\n",
  617. x, y, x_z, y_z);
  618. input_report_key(dev->input, BTN_TOUCH, 1);
  619. input_report_abs(dev->input, ABS_X, x);
  620. input_report_abs(dev->input, ABS_Y, y);
  621. input_report_abs(dev->input, ABS_PRESSURE,
  622. min(ATP_PRESSURE, x_z + y_z));
  623. atp_report_fingers(dev->input, fingers);
  624. }
  625. dev->x_old = x;
  626. dev->y_old = y;
  627. } else if (!x && !y) {
  628. dev->x_old = dev->y_old = -1;
  629. dev->fingers_old = 0;
  630. input_report_key(dev->input, BTN_TOUCH, 0);
  631. input_report_abs(dev->input, ABS_PRESSURE, 0);
  632. atp_report_fingers(dev->input, 0);
  633. /* reset the accumulator on release */
  634. memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
  635. }
  636. if (fingers != dev->fingers_old)
  637. dev->x_old = dev->y_old = -1;
  638. dev->fingers_old = fingers;
  639. input_report_key(dev->input, BTN_LEFT, key);
  640. input_sync(dev->input);
  641. /*
  642. * Geysers 3/4 will continue to send packets continually after
  643. * the first touch unless reinitialised. Do so if it's been
  644. * idle for a while in order to avoid waking the kernel up
  645. * several hundred times a second.
  646. */
  647. /*
  648. * Button must not be pressed when entering suspend,
  649. * otherwise we will never release the button.
  650. */
  651. if (!x && !y && !key) {
  652. dev->idlecount++;
  653. if (dev->idlecount == 10) {
  654. dev->x_old = dev->y_old = -1;
  655. dev->idlecount = 0;
  656. schedule_work(&dev->work);
  657. /* Don't resubmit urb here, wait for reinit */
  658. return;
  659. }
  660. } else
  661. dev->idlecount = 0;
  662. exit:
  663. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  664. if (retval)
  665. dev_err(&dev->intf->dev,
  666. "atp_complete: usb_submit_urb failed with result %d\n",
  667. retval);
  668. }
  669. static int atp_open(struct input_dev *input)
  670. {
  671. struct atp *dev = input_get_drvdata(input);
  672. if (usb_submit_urb(dev->urb, GFP_KERNEL))
  673. return -EIO;
  674. dev->open = true;
  675. return 0;
  676. }
  677. static void atp_close(struct input_dev *input)
  678. {
  679. struct atp *dev = input_get_drvdata(input);
  680. usb_kill_urb(dev->urb);
  681. cancel_work_sync(&dev->work);
  682. dev->open = false;
  683. }
  684. static int atp_handle_geyser(struct atp *dev)
  685. {
  686. if (dev->info != &fountain_info) {
  687. /* switch to raw sensor mode */
  688. if (atp_geyser_init(dev))
  689. return -EIO;
  690. dev_info(&dev->intf->dev, "Geyser mode initialized.\n");
  691. }
  692. return 0;
  693. }
  694. static int atp_probe(struct usb_interface *iface,
  695. const struct usb_device_id *id)
  696. {
  697. struct atp *dev;
  698. struct input_dev *input_dev;
  699. struct usb_device *udev = interface_to_usbdev(iface);
  700. struct usb_host_interface *iface_desc;
  701. struct usb_endpoint_descriptor *endpoint;
  702. int int_in_endpointAddr = 0;
  703. int i, error = -ENOMEM;
  704. const struct atp_info *info = (const struct atp_info *)id->driver_info;
  705. /* set up the endpoint information */
  706. /* use only the first interrupt-in endpoint */
  707. iface_desc = iface->cur_altsetting;
  708. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  709. endpoint = &iface_desc->endpoint[i].desc;
  710. if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
  711. /* we found an interrupt in endpoint */
  712. int_in_endpointAddr = endpoint->bEndpointAddress;
  713. break;
  714. }
  715. }
  716. if (!int_in_endpointAddr) {
  717. dev_err(&iface->dev, "Could not find int-in endpoint\n");
  718. return -EIO;
  719. }
  720. /* allocate memory for our device state and initialize it */
  721. dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
  722. input_dev = input_allocate_device();
  723. if (!dev || !input_dev) {
  724. dev_err(&iface->dev, "Out of memory\n");
  725. goto err_free_devs;
  726. }
  727. dev->udev = udev;
  728. dev->intf = iface;
  729. dev->input = input_dev;
  730. dev->info = info;
  731. dev->overflow_warned = false;
  732. dev->urb = usb_alloc_urb(0, GFP_KERNEL);
  733. if (!dev->urb)
  734. goto err_free_devs;
  735. dev->data = usb_alloc_coherent(dev->udev, dev->info->datalen, GFP_KERNEL,
  736. &dev->urb->transfer_dma);
  737. if (!dev->data)
  738. goto err_free_urb;
  739. usb_fill_int_urb(dev->urb, udev,
  740. usb_rcvintpipe(udev, int_in_endpointAddr),
  741. dev->data, dev->info->datalen,
  742. dev->info->callback, dev, 1);
  743. error = atp_handle_geyser(dev);
  744. if (error)
  745. goto err_free_buffer;
  746. usb_make_path(udev, dev->phys, sizeof(dev->phys));
  747. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  748. input_dev->name = "appletouch";
  749. input_dev->phys = dev->phys;
  750. usb_to_input_id(dev->udev, &input_dev->id);
  751. input_dev->dev.parent = &iface->dev;
  752. input_set_drvdata(input_dev, dev);
  753. input_dev->open = atp_open;
  754. input_dev->close = atp_close;
  755. set_bit(EV_ABS, input_dev->evbit);
  756. input_set_abs_params(input_dev, ABS_X, 0,
  757. (dev->info->xsensors - 1) * dev->info->xfact - 1,
  758. dev->info->fuzz, 0);
  759. input_set_abs_params(input_dev, ABS_Y, 0,
  760. (dev->info->ysensors - 1) * dev->info->yfact - 1,
  761. dev->info->fuzz, 0);
  762. input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
  763. set_bit(EV_KEY, input_dev->evbit);
  764. set_bit(BTN_TOUCH, input_dev->keybit);
  765. set_bit(BTN_TOOL_FINGER, input_dev->keybit);
  766. set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
  767. set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
  768. set_bit(BTN_LEFT, input_dev->keybit);
  769. INIT_WORK(&dev->work, atp_reinit);
  770. error = input_register_device(dev->input);
  771. if (error)
  772. goto err_free_buffer;
  773. /* save our data pointer in this interface device */
  774. usb_set_intfdata(iface, dev);
  775. return 0;
  776. err_free_buffer:
  777. usb_free_coherent(dev->udev, dev->info->datalen,
  778. dev->data, dev->urb->transfer_dma);
  779. err_free_urb:
  780. usb_free_urb(dev->urb);
  781. err_free_devs:
  782. usb_set_intfdata(iface, NULL);
  783. kfree(dev);
  784. input_free_device(input_dev);
  785. return error;
  786. }
  787. static void atp_disconnect(struct usb_interface *iface)
  788. {
  789. struct atp *dev = usb_get_intfdata(iface);
  790. usb_set_intfdata(iface, NULL);
  791. if (dev) {
  792. usb_kill_urb(dev->urb);
  793. input_unregister_device(dev->input);
  794. usb_free_coherent(dev->udev, dev->info->datalen,
  795. dev->data, dev->urb->transfer_dma);
  796. usb_free_urb(dev->urb);
  797. kfree(dev);
  798. }
  799. dev_info(&iface->dev, "input: appletouch disconnected\n");
  800. }
  801. static int atp_recover(struct atp *dev)
  802. {
  803. int error;
  804. error = atp_handle_geyser(dev);
  805. if (error)
  806. return error;
  807. if (dev->open && usb_submit_urb(dev->urb, GFP_KERNEL))
  808. return -EIO;
  809. return 0;
  810. }
  811. static int atp_suspend(struct usb_interface *iface, pm_message_t message)
  812. {
  813. struct atp *dev = usb_get_intfdata(iface);
  814. usb_kill_urb(dev->urb);
  815. return 0;
  816. }
  817. static int atp_resume(struct usb_interface *iface)
  818. {
  819. struct atp *dev = usb_get_intfdata(iface);
  820. if (dev->open && usb_submit_urb(dev->urb, GFP_KERNEL))
  821. return -EIO;
  822. return 0;
  823. }
  824. static int atp_reset_resume(struct usb_interface *iface)
  825. {
  826. struct atp *dev = usb_get_intfdata(iface);
  827. return atp_recover(dev);
  828. }
  829. static struct usb_driver atp_driver = {
  830. .name = "appletouch",
  831. .probe = atp_probe,
  832. .disconnect = atp_disconnect,
  833. .suspend = atp_suspend,
  834. .resume = atp_resume,
  835. .reset_resume = atp_reset_resume,
  836. .id_table = atp_table,
  837. };
  838. module_usb_driver(atp_driver);