bcm5974.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. * Apple USB BCM5974 (Macbook Air and Penryn Macbook Pro) multitouch driver
  3. *
  4. * Copyright (C) 2008 Henrik Rydberg (rydberg@euromail.se)
  5. *
  6. * The USB initialization and package decoding was made by
  7. * Scott Shawcroft as part of the touchd user-space driver project:
  8. * Copyright (C) 2008 Scott Shawcroft (scott.shawcroft@gmail.com)
  9. *
  10. * The BCM5974 driver is based on the appletouch driver:
  11. * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
  12. * Copyright (C) 2005 Johannes Berg (johannes@sipsolutions.net)
  13. * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
  14. * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de)
  15. * Copyright (C) 2005 Peter Osterlund (petero2@telia.com)
  16. * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch)
  17. * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch)
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/errno.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/module.h>
  39. #include <linux/usb/input.h>
  40. #include <linux/hid.h>
  41. #include <linux/mutex.h>
  42. #define USB_VENDOR_ID_APPLE 0x05ac
  43. /* MacbookAir, aka wellspring */
  44. #define USB_DEVICE_ID_APPLE_WELLSPRING_ANSI 0x0223
  45. #define USB_DEVICE_ID_APPLE_WELLSPRING_ISO 0x0224
  46. #define USB_DEVICE_ID_APPLE_WELLSPRING_JIS 0x0225
  47. /* MacbookProPenryn, aka wellspring2 */
  48. #define USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI 0x0230
  49. #define USB_DEVICE_ID_APPLE_WELLSPRING2_ISO 0x0231
  50. #define USB_DEVICE_ID_APPLE_WELLSPRING2_JIS 0x0232
  51. /* Macbook5,1 (unibody), aka wellspring3 */
  52. #define USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI 0x0236
  53. #define USB_DEVICE_ID_APPLE_WELLSPRING3_ISO 0x0237
  54. #define USB_DEVICE_ID_APPLE_WELLSPRING3_JIS 0x0238
  55. /* MacbookAir3,2 (unibody), aka wellspring5 */
  56. #define USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI 0x023f
  57. #define USB_DEVICE_ID_APPLE_WELLSPRING4_ISO 0x0240
  58. #define USB_DEVICE_ID_APPLE_WELLSPRING4_JIS 0x0241
  59. /* MacbookAir3,1 (unibody), aka wellspring4 */
  60. #define USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI 0x0242
  61. #define USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO 0x0243
  62. #define USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS 0x0244
  63. /* Macbook8 (unibody, March 2011) */
  64. #define USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI 0x0245
  65. #define USB_DEVICE_ID_APPLE_WELLSPRING5_ISO 0x0246
  66. #define USB_DEVICE_ID_APPLE_WELLSPRING5_JIS 0x0247
  67. #define BCM5974_DEVICE(prod) { \
  68. .match_flags = (USB_DEVICE_ID_MATCH_DEVICE | \
  69. USB_DEVICE_ID_MATCH_INT_CLASS | \
  70. USB_DEVICE_ID_MATCH_INT_PROTOCOL), \
  71. .idVendor = USB_VENDOR_ID_APPLE, \
  72. .idProduct = (prod), \
  73. .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
  74. .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE \
  75. }
  76. /* table of devices that work with this driver */
  77. static const struct usb_device_id bcm5974_table[] = {
  78. /* MacbookAir1.1 */
  79. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
  80. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
  81. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
  82. /* MacbookProPenryn */
  83. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
  84. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
  85. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
  86. /* Macbook5,1 */
  87. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI),
  88. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ISO),
  89. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_JIS),
  90. /* MacbookAir3,2 */
  91. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI),
  92. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_ISO),
  93. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_JIS),
  94. /* MacbookAir3,1 */
  95. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI),
  96. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO),
  97. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS),
  98. /* MacbookPro8 */
  99. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI),
  100. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_ISO),
  101. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_JIS),
  102. /* Terminating entry */
  103. {}
  104. };
  105. MODULE_DEVICE_TABLE(usb, bcm5974_table);
  106. MODULE_AUTHOR("Henrik Rydberg");
  107. MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
  108. MODULE_LICENSE("GPL");
  109. #define dprintk(level, format, a...)\
  110. { if (debug >= level) printk(KERN_DEBUG format, ##a); }
  111. static int debug = 1;
  112. module_param(debug, int, 0644);
  113. MODULE_PARM_DESC(debug, "Activate debugging output");
  114. /* button data structure */
  115. struct bt_data {
  116. u8 unknown1; /* constant */
  117. u8 button; /* left button */
  118. u8 rel_x; /* relative x coordinate */
  119. u8 rel_y; /* relative y coordinate */
  120. };
  121. /* trackpad header types */
  122. enum tp_type {
  123. TYPE1, /* plain trackpad */
  124. TYPE2 /* button integrated in trackpad */
  125. };
  126. /* trackpad finger data offsets, le16-aligned */
  127. #define FINGER_TYPE1 (13 * sizeof(__le16))
  128. #define FINGER_TYPE2 (15 * sizeof(__le16))
  129. /* trackpad button data offsets */
  130. #define BUTTON_TYPE2 15
  131. /* list of device capability bits */
  132. #define HAS_INTEGRATED_BUTTON 1
  133. /* trackpad finger structure, le16-aligned */
  134. struct tp_finger {
  135. __le16 origin; /* zero when switching track finger */
  136. __le16 abs_x; /* absolute x coodinate */
  137. __le16 abs_y; /* absolute y coodinate */
  138. __le16 rel_x; /* relative x coodinate */
  139. __le16 rel_y; /* relative y coodinate */
  140. __le16 size_major; /* finger size, major axis? */
  141. __le16 size_minor; /* finger size, minor axis? */
  142. __le16 orientation; /* 16384 when point, else 15 bit angle */
  143. __le16 force_major; /* trackpad force, major axis? */
  144. __le16 force_minor; /* trackpad force, minor axis? */
  145. __le16 unused[3]; /* zeros */
  146. __le16 multi; /* one finger: varies, more fingers: constant */
  147. } __attribute__((packed,aligned(2)));
  148. /* trackpad finger data size, empirically at least ten fingers */
  149. #define SIZEOF_FINGER sizeof(struct tp_finger)
  150. #define SIZEOF_ALL_FINGERS (16 * SIZEOF_FINGER)
  151. #define MAX_FINGER_ORIENTATION 16384
  152. /* device-specific parameters */
  153. struct bcm5974_param {
  154. int dim; /* logical dimension */
  155. int fuzz; /* logical noise value */
  156. int devmin; /* device minimum reading */
  157. int devmax; /* device maximum reading */
  158. };
  159. /* device-specific configuration */
  160. struct bcm5974_config {
  161. int ansi, iso, jis; /* the product id of this device */
  162. int caps; /* device capability bitmask */
  163. int bt_ep; /* the endpoint of the button interface */
  164. int bt_datalen; /* data length of the button interface */
  165. int tp_ep; /* the endpoint of the trackpad interface */
  166. enum tp_type tp_type; /* type of trackpad interface */
  167. int tp_offset; /* offset to trackpad finger data */
  168. int tp_datalen; /* data length of the trackpad interface */
  169. struct bcm5974_param p; /* finger pressure limits */
  170. struct bcm5974_param w; /* finger width limits */
  171. struct bcm5974_param x; /* horizontal limits */
  172. struct bcm5974_param y; /* vertical limits */
  173. };
  174. /* logical device structure */
  175. struct bcm5974 {
  176. char phys[64];
  177. struct usb_device *udev; /* usb device */
  178. struct usb_interface *intf; /* our interface */
  179. struct input_dev *input; /* input dev */
  180. struct bcm5974_config cfg; /* device configuration */
  181. struct mutex pm_mutex; /* serialize access to open/suspend */
  182. int opened; /* 1: opened, 0: closed */
  183. struct urb *bt_urb; /* button usb request block */
  184. struct bt_data *bt_data; /* button transferred data */
  185. struct urb *tp_urb; /* trackpad usb request block */
  186. u8 *tp_data; /* trackpad transferred data */
  187. int fingers; /* number of fingers on trackpad */
  188. };
  189. /* logical dimensions */
  190. #define DIM_PRESSURE 256 /* maximum finger pressure */
  191. #define DIM_WIDTH 16 /* maximum finger width */
  192. #define DIM_X 1280 /* maximum trackpad x value */
  193. #define DIM_Y 800 /* maximum trackpad y value */
  194. /* logical signal quality */
  195. #define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
  196. #define SN_WIDTH 100 /* width signal-to-noise ratio */
  197. #define SN_COORD 250 /* coordinate signal-to-noise ratio */
  198. /* pressure thresholds */
  199. #define PRESSURE_LOW (2 * DIM_PRESSURE / SN_PRESSURE)
  200. #define PRESSURE_HIGH (3 * PRESSURE_LOW)
  201. /* device constants */
  202. static const struct bcm5974_config bcm5974_config_table[] = {
  203. {
  204. USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
  205. USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
  206. USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
  207. 0,
  208. 0x84, sizeof(struct bt_data),
  209. 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
  210. { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
  211. { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
  212. { DIM_X, DIM_X / SN_COORD, -4824, 5342 },
  213. { DIM_Y, DIM_Y / SN_COORD, -172, 5820 }
  214. },
  215. {
  216. USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
  217. USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
  218. USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
  219. 0,
  220. 0x84, sizeof(struct bt_data),
  221. 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
  222. { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
  223. { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
  224. { DIM_X, DIM_X / SN_COORD, -4824, 4824 },
  225. { DIM_Y, DIM_Y / SN_COORD, -172, 4290 }
  226. },
  227. {
  228. USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI,
  229. USB_DEVICE_ID_APPLE_WELLSPRING3_ISO,
  230. USB_DEVICE_ID_APPLE_WELLSPRING3_JIS,
  231. HAS_INTEGRATED_BUTTON,
  232. 0x84, sizeof(struct bt_data),
  233. 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
  234. { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
  235. { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
  236. { DIM_X, DIM_X / SN_COORD, -4460, 5166 },
  237. { DIM_Y, DIM_Y / SN_COORD, -75, 6700 }
  238. },
  239. {
  240. USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI,
  241. USB_DEVICE_ID_APPLE_WELLSPRING4_ISO,
  242. USB_DEVICE_ID_APPLE_WELLSPRING4_JIS,
  243. HAS_INTEGRATED_BUTTON,
  244. 0x84, sizeof(struct bt_data),
  245. 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
  246. { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
  247. { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
  248. { DIM_X, DIM_X / SN_COORD, -4620, 5140 },
  249. { DIM_Y, DIM_Y / SN_COORD, -150, 6600 }
  250. },
  251. {
  252. USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI,
  253. USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO,
  254. USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS,
  255. HAS_INTEGRATED_BUTTON,
  256. 0x84, sizeof(struct bt_data),
  257. 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
  258. { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
  259. { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
  260. { DIM_X, DIM_X / SN_COORD, -4616, 5112 },
  261. { DIM_Y, DIM_Y / SN_COORD, -142, 5234 }
  262. },
  263. {
  264. USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI,
  265. USB_DEVICE_ID_APPLE_WELLSPRING5_ISO,
  266. USB_DEVICE_ID_APPLE_WELLSPRING5_JIS,
  267. HAS_INTEGRATED_BUTTON,
  268. 0x84, sizeof(struct bt_data),
  269. 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
  270. { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
  271. { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
  272. { DIM_X, DIM_X / SN_COORD, -4415, 5050 },
  273. { DIM_Y, DIM_Y / SN_COORD, -55, 6680 }
  274. },
  275. {}
  276. };
  277. /* return the device-specific configuration by device */
  278. static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
  279. {
  280. u16 id = le16_to_cpu(udev->descriptor.idProduct);
  281. const struct bcm5974_config *cfg;
  282. for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
  283. if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
  284. return cfg;
  285. return bcm5974_config_table;
  286. }
  287. /* convert 16-bit little endian to signed integer */
  288. static inline int raw2int(__le16 x)
  289. {
  290. return (signed short)le16_to_cpu(x);
  291. }
  292. /* scale device data to logical dimensions (asserts devmin < devmax) */
  293. static inline int int2scale(const struct bcm5974_param *p, int x)
  294. {
  295. return x * p->dim / (p->devmax - p->devmin);
  296. }
  297. /* all logical value ranges are [0,dim). */
  298. static inline int int2bound(const struct bcm5974_param *p, int x)
  299. {
  300. int s = int2scale(p, x);
  301. return clamp_val(s, 0, p->dim - 1);
  302. }
  303. /* setup which logical events to report */
  304. static void setup_events_to_report(struct input_dev *input_dev,
  305. const struct bcm5974_config *cfg)
  306. {
  307. __set_bit(EV_ABS, input_dev->evbit);
  308. input_set_abs_params(input_dev, ABS_PRESSURE,
  309. 0, cfg->p.dim, cfg->p.fuzz, 0);
  310. input_set_abs_params(input_dev, ABS_TOOL_WIDTH,
  311. 0, cfg->w.dim, cfg->w.fuzz, 0);
  312. input_set_abs_params(input_dev, ABS_X,
  313. 0, cfg->x.dim, cfg->x.fuzz, 0);
  314. input_set_abs_params(input_dev, ABS_Y,
  315. 0, cfg->y.dim, cfg->y.fuzz, 0);
  316. /* finger touch area */
  317. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
  318. cfg->w.devmin, cfg->w.devmax, 0, 0);
  319. input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
  320. cfg->w.devmin, cfg->w.devmax, 0, 0);
  321. /* finger approach area */
  322. input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR,
  323. cfg->w.devmin, cfg->w.devmax, 0, 0);
  324. input_set_abs_params(input_dev, ABS_MT_WIDTH_MINOR,
  325. cfg->w.devmin, cfg->w.devmax, 0, 0);
  326. /* finger orientation */
  327. input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
  328. -MAX_FINGER_ORIENTATION,
  329. MAX_FINGER_ORIENTATION, 0, 0);
  330. /* finger position */
  331. input_set_abs_params(input_dev, ABS_MT_POSITION_X,
  332. cfg->x.devmin, cfg->x.devmax, 0, 0);
  333. input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
  334. cfg->y.devmin, cfg->y.devmax, 0, 0);
  335. __set_bit(EV_KEY, input_dev->evbit);
  336. __set_bit(BTN_TOUCH, input_dev->keybit);
  337. __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
  338. __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
  339. __set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
  340. __set_bit(BTN_TOOL_QUADTAP, input_dev->keybit);
  341. __set_bit(BTN_LEFT, input_dev->keybit);
  342. input_set_events_per_packet(input_dev, 60);
  343. }
  344. /* report button data as logical button state */
  345. static int report_bt_state(struct bcm5974 *dev, int size)
  346. {
  347. if (size != sizeof(struct bt_data))
  348. return -EIO;
  349. dprintk(7,
  350. "bcm5974: button data: %x %x %x %x\n",
  351. dev->bt_data->unknown1, dev->bt_data->button,
  352. dev->bt_data->rel_x, dev->bt_data->rel_y);
  353. input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
  354. input_sync(dev->input);
  355. return 0;
  356. }
  357. static void report_finger_data(struct input_dev *input,
  358. const struct bcm5974_config *cfg,
  359. const struct tp_finger *f)
  360. {
  361. input_report_abs(input, ABS_MT_TOUCH_MAJOR,
  362. raw2int(f->force_major) << 1);
  363. input_report_abs(input, ABS_MT_TOUCH_MINOR,
  364. raw2int(f->force_minor) << 1);
  365. input_report_abs(input, ABS_MT_WIDTH_MAJOR,
  366. raw2int(f->size_major) << 1);
  367. input_report_abs(input, ABS_MT_WIDTH_MINOR,
  368. raw2int(f->size_minor) << 1);
  369. input_report_abs(input, ABS_MT_ORIENTATION,
  370. MAX_FINGER_ORIENTATION - raw2int(f->orientation));
  371. input_report_abs(input, ABS_MT_POSITION_X, raw2int(f->abs_x));
  372. input_report_abs(input, ABS_MT_POSITION_Y,
  373. cfg->y.devmin + cfg->y.devmax - raw2int(f->abs_y));
  374. input_mt_sync(input);
  375. }
  376. /* report trackpad data as logical trackpad state */
  377. static int report_tp_state(struct bcm5974 *dev, int size)
  378. {
  379. const struct bcm5974_config *c = &dev->cfg;
  380. const struct tp_finger *f;
  381. struct input_dev *input = dev->input;
  382. int raw_p, raw_w, raw_x, raw_y, raw_n, i;
  383. int ptest, origin, ibt = 0, nmin = 0, nmax = 0;
  384. int abs_p = 0, abs_w = 0, abs_x = 0, abs_y = 0;
  385. if (size < c->tp_offset || (size - c->tp_offset) % SIZEOF_FINGER != 0)
  386. return -EIO;
  387. /* finger data, le16-aligned */
  388. f = (const struct tp_finger *)(dev->tp_data + c->tp_offset);
  389. raw_n = (size - c->tp_offset) / SIZEOF_FINGER;
  390. /* always track the first finger; when detached, start over */
  391. if (raw_n) {
  392. /* report raw trackpad data */
  393. for (i = 0; i < raw_n; i++)
  394. report_finger_data(input, c, &f[i]);
  395. raw_p = raw2int(f->force_major);
  396. raw_w = raw2int(f->size_major);
  397. raw_x = raw2int(f->abs_x);
  398. raw_y = raw2int(f->abs_y);
  399. dprintk(9,
  400. "bcm5974: "
  401. "raw: p: %+05d w: %+05d x: %+05d y: %+05d n: %d\n",
  402. raw_p, raw_w, raw_x, raw_y, raw_n);
  403. ptest = int2bound(&c->p, raw_p);
  404. origin = raw2int(f->origin);
  405. /* while tracking finger still valid, count all fingers */
  406. if (ptest > PRESSURE_LOW && origin) {
  407. abs_p = ptest;
  408. abs_w = int2bound(&c->w, raw_w);
  409. abs_x = int2bound(&c->x, raw_x - c->x.devmin);
  410. abs_y = int2bound(&c->y, c->y.devmax - raw_y);
  411. while (raw_n--) {
  412. ptest = int2bound(&c->p,
  413. raw2int(f->force_major));
  414. if (ptest > PRESSURE_LOW)
  415. nmax++;
  416. if (ptest > PRESSURE_HIGH)
  417. nmin++;
  418. f++;
  419. }
  420. }
  421. }
  422. /* set the integrated button if applicable */
  423. if (c->tp_type == TYPE2)
  424. ibt = raw2int(dev->tp_data[BUTTON_TYPE2]);
  425. if (dev->fingers < nmin)
  426. dev->fingers = nmin;
  427. if (dev->fingers > nmax)
  428. dev->fingers = nmax;
  429. input_report_key(input, BTN_TOUCH, dev->fingers > 0);
  430. input_report_key(input, BTN_TOOL_FINGER, dev->fingers == 1);
  431. input_report_key(input, BTN_TOOL_DOUBLETAP, dev->fingers == 2);
  432. input_report_key(input, BTN_TOOL_TRIPLETAP, dev->fingers == 3);
  433. input_report_key(input, BTN_TOOL_QUADTAP, dev->fingers > 3);
  434. input_report_abs(input, ABS_PRESSURE, abs_p);
  435. input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
  436. if (abs_p) {
  437. input_report_abs(input, ABS_X, abs_x);
  438. input_report_abs(input, ABS_Y, abs_y);
  439. dprintk(8,
  440. "bcm5974: abs: p: %+05d w: %+05d x: %+05d y: %+05d "
  441. "nmin: %d nmax: %d n: %d ibt: %d\n", abs_p, abs_w,
  442. abs_x, abs_y, nmin, nmax, dev->fingers, ibt);
  443. }
  444. /* type 2 reports button events via ibt only */
  445. if (c->tp_type == TYPE2)
  446. input_report_key(input, BTN_LEFT, ibt);
  447. input_sync(input);
  448. return 0;
  449. }
  450. /* Wellspring initialization constants */
  451. #define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID 1
  452. #define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID 9
  453. #define BCM5974_WELLSPRING_MODE_REQUEST_VALUE 0x300
  454. #define BCM5974_WELLSPRING_MODE_REQUEST_INDEX 0
  455. #define BCM5974_WELLSPRING_MODE_VENDOR_VALUE 0x01
  456. #define BCM5974_WELLSPRING_MODE_NORMAL_VALUE 0x08
  457. static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
  458. {
  459. char *data = kmalloc(8, GFP_KERNEL);
  460. int retval = 0, size;
  461. if (!data) {
  462. err("bcm5974: out of memory");
  463. retval = -ENOMEM;
  464. goto out;
  465. }
  466. /* read configuration */
  467. size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  468. BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
  469. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  470. BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
  471. BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
  472. if (size != 8) {
  473. err("bcm5974: could not read from device");
  474. retval = -EIO;
  475. goto out;
  476. }
  477. /* apply the mode switch */
  478. data[0] = on ?
  479. BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
  480. BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
  481. /* write configuration */
  482. size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  483. BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
  484. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  485. BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
  486. BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
  487. if (size != 8) {
  488. err("bcm5974: could not write to device");
  489. retval = -EIO;
  490. goto out;
  491. }
  492. dprintk(2, "bcm5974: switched to %s mode.\n",
  493. on ? "wellspring" : "normal");
  494. out:
  495. kfree(data);
  496. return retval;
  497. }
  498. static void bcm5974_irq_button(struct urb *urb)
  499. {
  500. struct bcm5974 *dev = urb->context;
  501. int error;
  502. switch (urb->status) {
  503. case 0:
  504. break;
  505. case -EOVERFLOW:
  506. case -ECONNRESET:
  507. case -ENOENT:
  508. case -ESHUTDOWN:
  509. dbg("bcm5974: button urb shutting down: %d", urb->status);
  510. return;
  511. default:
  512. dbg("bcm5974: button urb status: %d", urb->status);
  513. goto exit;
  514. }
  515. if (report_bt_state(dev, dev->bt_urb->actual_length))
  516. dprintk(1, "bcm5974: bad button package, length: %d\n",
  517. dev->bt_urb->actual_length);
  518. exit:
  519. error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
  520. if (error)
  521. err("bcm5974: button urb failed: %d", error);
  522. }
  523. static void bcm5974_irq_trackpad(struct urb *urb)
  524. {
  525. struct bcm5974 *dev = urb->context;
  526. int error;
  527. switch (urb->status) {
  528. case 0:
  529. break;
  530. case -EOVERFLOW:
  531. case -ECONNRESET:
  532. case -ENOENT:
  533. case -ESHUTDOWN:
  534. dbg("bcm5974: trackpad urb shutting down: %d", urb->status);
  535. return;
  536. default:
  537. dbg("bcm5974: trackpad urb status: %d", urb->status);
  538. goto exit;
  539. }
  540. /* control response ignored */
  541. if (dev->tp_urb->actual_length == 2)
  542. goto exit;
  543. if (report_tp_state(dev, dev->tp_urb->actual_length))
  544. dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
  545. dev->tp_urb->actual_length);
  546. exit:
  547. error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
  548. if (error)
  549. err("bcm5974: trackpad urb failed: %d", error);
  550. }
  551. /*
  552. * The Wellspring trackpad, like many recent Apple trackpads, share
  553. * the usb device with the keyboard. Since keyboards are usually
  554. * handled by the HID system, the device ends up being handled by two
  555. * modules. Setting up the device therefore becomes slightly
  556. * complicated. To enable multitouch features, a mode switch is
  557. * required, which is usually applied via the control interface of the
  558. * device. It can be argued where this switch should take place. In
  559. * some drivers, like appletouch, the switch is made during
  560. * probe. However, the hid module may also alter the state of the
  561. * device, resulting in trackpad malfunction under certain
  562. * circumstances. To get around this problem, there is at least one
  563. * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
  564. * receive a reset_resume request rather than the normal resume.
  565. * Since the implementation of reset_resume is equal to mode switch
  566. * plus start_traffic, it seems easier to always do the switch when
  567. * starting traffic on the device.
  568. */
  569. static int bcm5974_start_traffic(struct bcm5974 *dev)
  570. {
  571. int error;
  572. error = bcm5974_wellspring_mode(dev, true);
  573. if (error) {
  574. dprintk(1, "bcm5974: mode switch failed\n");
  575. goto err_out;
  576. }
  577. error = usb_submit_urb(dev->bt_urb, GFP_KERNEL);
  578. if (error)
  579. goto err_reset_mode;
  580. error = usb_submit_urb(dev->tp_urb, GFP_KERNEL);
  581. if (error)
  582. goto err_kill_bt;
  583. return 0;
  584. err_kill_bt:
  585. usb_kill_urb(dev->bt_urb);
  586. err_reset_mode:
  587. bcm5974_wellspring_mode(dev, false);
  588. err_out:
  589. return error;
  590. }
  591. static void bcm5974_pause_traffic(struct bcm5974 *dev)
  592. {
  593. usb_kill_urb(dev->tp_urb);
  594. usb_kill_urb(dev->bt_urb);
  595. bcm5974_wellspring_mode(dev, false);
  596. }
  597. /*
  598. * The code below implements open/close and manual suspend/resume.
  599. * All functions may be called in random order.
  600. *
  601. * Opening a suspended device fails with EACCES - permission denied.
  602. *
  603. * Failing a resume leaves the device resumed but closed.
  604. */
  605. static int bcm5974_open(struct input_dev *input)
  606. {
  607. struct bcm5974 *dev = input_get_drvdata(input);
  608. int error;
  609. error = usb_autopm_get_interface(dev->intf);
  610. if (error)
  611. return error;
  612. mutex_lock(&dev->pm_mutex);
  613. error = bcm5974_start_traffic(dev);
  614. if (!error)
  615. dev->opened = 1;
  616. mutex_unlock(&dev->pm_mutex);
  617. if (error)
  618. usb_autopm_put_interface(dev->intf);
  619. return error;
  620. }
  621. static void bcm5974_close(struct input_dev *input)
  622. {
  623. struct bcm5974 *dev = input_get_drvdata(input);
  624. mutex_lock(&dev->pm_mutex);
  625. bcm5974_pause_traffic(dev);
  626. dev->opened = 0;
  627. mutex_unlock(&dev->pm_mutex);
  628. usb_autopm_put_interface(dev->intf);
  629. }
  630. static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
  631. {
  632. struct bcm5974 *dev = usb_get_intfdata(iface);
  633. mutex_lock(&dev->pm_mutex);
  634. if (dev->opened)
  635. bcm5974_pause_traffic(dev);
  636. mutex_unlock(&dev->pm_mutex);
  637. return 0;
  638. }
  639. static int bcm5974_resume(struct usb_interface *iface)
  640. {
  641. struct bcm5974 *dev = usb_get_intfdata(iface);
  642. int error = 0;
  643. mutex_lock(&dev->pm_mutex);
  644. if (dev->opened)
  645. error = bcm5974_start_traffic(dev);
  646. mutex_unlock(&dev->pm_mutex);
  647. return error;
  648. }
  649. static int bcm5974_probe(struct usb_interface *iface,
  650. const struct usb_device_id *id)
  651. {
  652. struct usb_device *udev = interface_to_usbdev(iface);
  653. const struct bcm5974_config *cfg;
  654. struct bcm5974 *dev;
  655. struct input_dev *input_dev;
  656. int error = -ENOMEM;
  657. /* find the product index */
  658. cfg = bcm5974_get_config(udev);
  659. /* allocate memory for our device state and initialize it */
  660. dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
  661. input_dev = input_allocate_device();
  662. if (!dev || !input_dev) {
  663. err("bcm5974: out of memory");
  664. goto err_free_devs;
  665. }
  666. dev->udev = udev;
  667. dev->intf = iface;
  668. dev->input = input_dev;
  669. dev->cfg = *cfg;
  670. mutex_init(&dev->pm_mutex);
  671. /* setup urbs */
  672. dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
  673. if (!dev->bt_urb)
  674. goto err_free_devs;
  675. dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
  676. if (!dev->tp_urb)
  677. goto err_free_bt_urb;
  678. dev->bt_data = usb_alloc_coherent(dev->udev,
  679. dev->cfg.bt_datalen, GFP_KERNEL,
  680. &dev->bt_urb->transfer_dma);
  681. if (!dev->bt_data)
  682. goto err_free_urb;
  683. dev->tp_data = usb_alloc_coherent(dev->udev,
  684. dev->cfg.tp_datalen, GFP_KERNEL,
  685. &dev->tp_urb->transfer_dma);
  686. if (!dev->tp_data)
  687. goto err_free_bt_buffer;
  688. usb_fill_int_urb(dev->bt_urb, udev,
  689. usb_rcvintpipe(udev, cfg->bt_ep),
  690. dev->bt_data, dev->cfg.bt_datalen,
  691. bcm5974_irq_button, dev, 1);
  692. usb_fill_int_urb(dev->tp_urb, udev,
  693. usb_rcvintpipe(udev, cfg->tp_ep),
  694. dev->tp_data, dev->cfg.tp_datalen,
  695. bcm5974_irq_trackpad, dev, 1);
  696. /* create bcm5974 device */
  697. usb_make_path(udev, dev->phys, sizeof(dev->phys));
  698. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  699. input_dev->name = "bcm5974";
  700. input_dev->phys = dev->phys;
  701. usb_to_input_id(dev->udev, &input_dev->id);
  702. /* report driver capabilities via the version field */
  703. input_dev->id.version = cfg->caps;
  704. input_dev->dev.parent = &iface->dev;
  705. input_set_drvdata(input_dev, dev);
  706. input_dev->open = bcm5974_open;
  707. input_dev->close = bcm5974_close;
  708. setup_events_to_report(input_dev, cfg);
  709. error = input_register_device(dev->input);
  710. if (error)
  711. goto err_free_buffer;
  712. /* save our data pointer in this interface device */
  713. usb_set_intfdata(iface, dev);
  714. return 0;
  715. err_free_buffer:
  716. usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
  717. dev->tp_data, dev->tp_urb->transfer_dma);
  718. err_free_bt_buffer:
  719. usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
  720. dev->bt_data, dev->bt_urb->transfer_dma);
  721. err_free_urb:
  722. usb_free_urb(dev->tp_urb);
  723. err_free_bt_urb:
  724. usb_free_urb(dev->bt_urb);
  725. err_free_devs:
  726. usb_set_intfdata(iface, NULL);
  727. input_free_device(input_dev);
  728. kfree(dev);
  729. return error;
  730. }
  731. static void bcm5974_disconnect(struct usb_interface *iface)
  732. {
  733. struct bcm5974 *dev = usb_get_intfdata(iface);
  734. usb_set_intfdata(iface, NULL);
  735. input_unregister_device(dev->input);
  736. usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
  737. dev->tp_data, dev->tp_urb->transfer_dma);
  738. usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
  739. dev->bt_data, dev->bt_urb->transfer_dma);
  740. usb_free_urb(dev->tp_urb);
  741. usb_free_urb(dev->bt_urb);
  742. kfree(dev);
  743. }
  744. static struct usb_driver bcm5974_driver = {
  745. .name = "bcm5974",
  746. .probe = bcm5974_probe,
  747. .disconnect = bcm5974_disconnect,
  748. .suspend = bcm5974_suspend,
  749. .resume = bcm5974_resume,
  750. .id_table = bcm5974_table,
  751. .supports_autosuspend = 1,
  752. };
  753. static int __init bcm5974_init(void)
  754. {
  755. return usb_register(&bcm5974_driver);
  756. }
  757. static void __exit bcm5974_exit(void)
  758. {
  759. usb_deregister(&bcm5974_driver);
  760. }
  761. module_init(bcm5974_init);
  762. module_exit(bcm5974_exit);