bcm5974.c 29 KB

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