bcm5974.c 29 KB

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