bcm5974.c 28 KB

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