bcm5974.c 31 KB

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