bcm5974.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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. struct work_struct mode_reset_work;
  268. unsigned long last_mode_reset;
  269. };
  270. /* trackpad finger block data, le16-aligned */
  271. static const struct tp_finger *get_tp_finger(const struct bcm5974 *dev, int i)
  272. {
  273. const struct bcm5974_config *c = &dev->cfg;
  274. u8 *f_base = dev->tp_data + c->tp_header + c->tp_delta;
  275. return (const struct tp_finger *)(f_base + i * c->tp_fsize);
  276. }
  277. #define DATAFORMAT(type) \
  278. type, \
  279. HEADER_##type, \
  280. HEADER_##type + (MAX_FINGERS) * (FSIZE_##type), \
  281. BUTTON_##type, \
  282. FSIZE_##type, \
  283. DELTA_##type, \
  284. USBMSG_##type
  285. /* logical signal quality */
  286. #define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
  287. #define SN_WIDTH 25 /* width signal-to-noise ratio */
  288. #define SN_COORD 250 /* coordinate signal-to-noise ratio */
  289. #define SN_ORIENT 10 /* orientation signal-to-noise ratio */
  290. /* device constants */
  291. static const struct bcm5974_config bcm5974_config_table[] = {
  292. {
  293. USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
  294. USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
  295. USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
  296. 0,
  297. 0x84, sizeof(struct bt_data),
  298. 0x81, DATAFORMAT(TYPE1),
  299. { SN_PRESSURE, 0, 256 },
  300. { SN_WIDTH, 0, 2048 },
  301. { SN_COORD, -4824, 5342 },
  302. { SN_COORD, -172, 5820 },
  303. { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
  304. },
  305. {
  306. USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
  307. USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
  308. USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
  309. 0,
  310. 0x84, sizeof(struct bt_data),
  311. 0x81, DATAFORMAT(TYPE1),
  312. { SN_PRESSURE, 0, 256 },
  313. { SN_WIDTH, 0, 2048 },
  314. { SN_COORD, -4824, 4824 },
  315. { SN_COORD, -172, 4290 },
  316. { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
  317. },
  318. {
  319. USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI,
  320. USB_DEVICE_ID_APPLE_WELLSPRING3_ISO,
  321. USB_DEVICE_ID_APPLE_WELLSPRING3_JIS,
  322. HAS_INTEGRATED_BUTTON,
  323. 0x84, sizeof(struct bt_data),
  324. 0x81, DATAFORMAT(TYPE2),
  325. { SN_PRESSURE, 0, 300 },
  326. { SN_WIDTH, 0, 2048 },
  327. { SN_COORD, -4460, 5166 },
  328. { SN_COORD, -75, 6700 },
  329. { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
  330. },
  331. {
  332. USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI,
  333. USB_DEVICE_ID_APPLE_WELLSPRING4_ISO,
  334. USB_DEVICE_ID_APPLE_WELLSPRING4_JIS,
  335. HAS_INTEGRATED_BUTTON,
  336. 0x84, sizeof(struct bt_data),
  337. 0x81, DATAFORMAT(TYPE2),
  338. { SN_PRESSURE, 0, 300 },
  339. { SN_WIDTH, 0, 2048 },
  340. { SN_COORD, -4620, 5140 },
  341. { SN_COORD, -150, 6600 },
  342. { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
  343. },
  344. {
  345. USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI,
  346. USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO,
  347. USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS,
  348. HAS_INTEGRATED_BUTTON,
  349. 0x84, sizeof(struct bt_data),
  350. 0x81, DATAFORMAT(TYPE2),
  351. { SN_PRESSURE, 0, 300 },
  352. { SN_WIDTH, 0, 2048 },
  353. { SN_COORD, -4616, 5112 },
  354. { SN_COORD, -142, 5234 },
  355. { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
  356. },
  357. {
  358. USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI,
  359. USB_DEVICE_ID_APPLE_WELLSPRING5_ISO,
  360. USB_DEVICE_ID_APPLE_WELLSPRING5_JIS,
  361. HAS_INTEGRATED_BUTTON,
  362. 0x84, sizeof(struct bt_data),
  363. 0x81, DATAFORMAT(TYPE2),
  364. { SN_PRESSURE, 0, 300 },
  365. { SN_WIDTH, 0, 2048 },
  366. { SN_COORD, -4415, 5050 },
  367. { SN_COORD, -55, 6680 },
  368. { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
  369. },
  370. {
  371. USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI,
  372. USB_DEVICE_ID_APPLE_WELLSPRING6_ISO,
  373. USB_DEVICE_ID_APPLE_WELLSPRING6_JIS,
  374. HAS_INTEGRATED_BUTTON,
  375. 0x84, sizeof(struct bt_data),
  376. 0x81, DATAFORMAT(TYPE2),
  377. { SN_PRESSURE, 0, 300 },
  378. { SN_WIDTH, 0, 2048 },
  379. { SN_COORD, -4620, 5140 },
  380. { SN_COORD, -150, 6600 },
  381. { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
  382. },
  383. {
  384. USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI,
  385. USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO,
  386. USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS,
  387. HAS_INTEGRATED_BUTTON,
  388. 0x84, sizeof(struct bt_data),
  389. 0x81, DATAFORMAT(TYPE2),
  390. { SN_PRESSURE, 0, 300 },
  391. { SN_WIDTH, 0, 2048 },
  392. { SN_COORD, -4750, 5280 },
  393. { SN_COORD, -150, 6730 },
  394. { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
  395. },
  396. {
  397. USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI,
  398. USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO,
  399. USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS,
  400. HAS_INTEGRATED_BUTTON,
  401. 0x84, sizeof(struct bt_data),
  402. 0x81, DATAFORMAT(TYPE2),
  403. { SN_PRESSURE, 0, 300 },
  404. { SN_WIDTH, 0, 2048 },
  405. { SN_COORD, -4620, 5140 },
  406. { SN_COORD, -150, 6600 },
  407. { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
  408. },
  409. {
  410. USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI,
  411. USB_DEVICE_ID_APPLE_WELLSPRING7_ISO,
  412. USB_DEVICE_ID_APPLE_WELLSPRING7_JIS,
  413. HAS_INTEGRATED_BUTTON,
  414. 0x84, sizeof(struct bt_data),
  415. 0x81, DATAFORMAT(TYPE2),
  416. { SN_PRESSURE, 0, 300 },
  417. { SN_WIDTH, 0, 2048 },
  418. { SN_COORD, -4750, 5280 },
  419. { SN_COORD, -150, 6730 },
  420. { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
  421. },
  422. {
  423. USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI,
  424. USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO,
  425. USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS,
  426. HAS_INTEGRATED_BUTTON,
  427. 0x84, sizeof(struct bt_data),
  428. 0x81, DATAFORMAT(TYPE2),
  429. { SN_PRESSURE, 0, 300 },
  430. { SN_WIDTH, 0, 2048 },
  431. { SN_COORD, -4750, 5280 },
  432. { SN_COORD, -150, 6730 },
  433. { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
  434. },
  435. {
  436. USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI,
  437. USB_DEVICE_ID_APPLE_WELLSPRING8_ISO,
  438. USB_DEVICE_ID_APPLE_WELLSPRING8_JIS,
  439. HAS_INTEGRATED_BUTTON,
  440. 0, sizeof(struct bt_data),
  441. 0x83, DATAFORMAT(TYPE3),
  442. { SN_PRESSURE, 0, 300 },
  443. { SN_WIDTH, 0, 2048 },
  444. { SN_COORD, -4620, 5140 },
  445. { SN_COORD, -150, 6600 },
  446. { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
  447. },
  448. {
  449. USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI,
  450. USB_DEVICE_ID_APPLE_WELLSPRING9_ISO,
  451. USB_DEVICE_ID_APPLE_WELLSPRING9_JIS,
  452. HAS_INTEGRATED_BUTTON,
  453. 0, sizeof(struct bt_data),
  454. 0x83, DATAFORMAT(TYPE4),
  455. { SN_PRESSURE, 0, 300 },
  456. { SN_WIDTH, 0, 2048 },
  457. { SN_COORD, -4828, 5345 },
  458. { SN_COORD, -203, 6803 },
  459. { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
  460. },
  461. {}
  462. };
  463. /* return the device-specific configuration by device */
  464. static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
  465. {
  466. u16 id = le16_to_cpu(udev->descriptor.idProduct);
  467. const struct bcm5974_config *cfg;
  468. for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
  469. if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
  470. return cfg;
  471. return bcm5974_config_table;
  472. }
  473. /* convert 16-bit little endian to signed integer */
  474. static inline int raw2int(__le16 x)
  475. {
  476. return (signed short)le16_to_cpu(x);
  477. }
  478. static void set_abs(struct input_dev *input, unsigned int code,
  479. const struct bcm5974_param *p)
  480. {
  481. int fuzz = p->snratio ? (p->max - p->min) / p->snratio : 0;
  482. input_set_abs_params(input, code, p->min, p->max, fuzz, 0);
  483. }
  484. /* setup which logical events to report */
  485. static void setup_events_to_report(struct input_dev *input_dev,
  486. const struct bcm5974_config *cfg)
  487. {
  488. __set_bit(EV_ABS, input_dev->evbit);
  489. /* for synaptics only */
  490. input_set_abs_params(input_dev, ABS_PRESSURE, 0, 256, 5, 0);
  491. input_set_abs_params(input_dev, ABS_TOOL_WIDTH, 0, 16, 0, 0);
  492. /* finger touch area */
  493. set_abs(input_dev, ABS_MT_TOUCH_MAJOR, &cfg->w);
  494. set_abs(input_dev, ABS_MT_TOUCH_MINOR, &cfg->w);
  495. /* finger approach area */
  496. set_abs(input_dev, ABS_MT_WIDTH_MAJOR, &cfg->w);
  497. set_abs(input_dev, ABS_MT_WIDTH_MINOR, &cfg->w);
  498. /* finger orientation */
  499. set_abs(input_dev, ABS_MT_ORIENTATION, &cfg->o);
  500. /* finger position */
  501. set_abs(input_dev, ABS_MT_POSITION_X, &cfg->x);
  502. set_abs(input_dev, ABS_MT_POSITION_Y, &cfg->y);
  503. __set_bit(EV_KEY, input_dev->evbit);
  504. __set_bit(BTN_LEFT, input_dev->keybit);
  505. if (cfg->caps & HAS_INTEGRATED_BUTTON)
  506. __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
  507. input_mt_init_slots(input_dev, MAX_FINGERS,
  508. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK);
  509. }
  510. /* report button data as logical button state */
  511. static int report_bt_state(struct bcm5974 *dev, int size)
  512. {
  513. if (size != sizeof(struct bt_data))
  514. return -EIO;
  515. dprintk(7,
  516. "bcm5974: button data: %x %x %x %x\n",
  517. dev->bt_data->unknown1, dev->bt_data->button,
  518. dev->bt_data->rel_x, dev->bt_data->rel_y);
  519. input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
  520. input_sync(dev->input);
  521. return 0;
  522. }
  523. static void report_finger_data(struct input_dev *input, int slot,
  524. const struct input_mt_pos *pos,
  525. const struct tp_finger *f)
  526. {
  527. input_mt_slot(input, slot);
  528. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  529. input_report_abs(input, ABS_MT_TOUCH_MAJOR,
  530. raw2int(f->touch_major) << 1);
  531. input_report_abs(input, ABS_MT_TOUCH_MINOR,
  532. raw2int(f->touch_minor) << 1);
  533. input_report_abs(input, ABS_MT_WIDTH_MAJOR,
  534. raw2int(f->tool_major) << 1);
  535. input_report_abs(input, ABS_MT_WIDTH_MINOR,
  536. raw2int(f->tool_minor) << 1);
  537. input_report_abs(input, ABS_MT_ORIENTATION,
  538. MAX_FINGER_ORIENTATION - raw2int(f->orientation));
  539. input_report_abs(input, ABS_MT_POSITION_X, pos->x);
  540. input_report_abs(input, ABS_MT_POSITION_Y, pos->y);
  541. }
  542. static void report_synaptics_data(struct input_dev *input,
  543. const struct bcm5974_config *cfg,
  544. const struct tp_finger *f, int raw_n)
  545. {
  546. int abs_p = 0, abs_w = 0;
  547. if (raw_n) {
  548. int p = raw2int(f->touch_major);
  549. int w = raw2int(f->tool_major);
  550. if (p > 0 && raw2int(f->origin)) {
  551. abs_p = clamp_val(256 * p / cfg->p.max, 0, 255);
  552. abs_w = clamp_val(16 * w / cfg->w.max, 0, 15);
  553. }
  554. }
  555. input_report_abs(input, ABS_PRESSURE, abs_p);
  556. input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
  557. }
  558. /* report trackpad data as logical trackpad state */
  559. static int report_tp_state(struct bcm5974 *dev, int size)
  560. {
  561. const struct bcm5974_config *c = &dev->cfg;
  562. const struct tp_finger *f;
  563. struct input_dev *input = dev->input;
  564. int raw_n, i, n = 0;
  565. if (size < c->tp_header || (size - c->tp_header) % c->tp_fsize != 0)
  566. return -EIO;
  567. raw_n = (size - c->tp_header) / c->tp_fsize;
  568. for (i = 0; i < raw_n; i++) {
  569. f = get_tp_finger(dev, i);
  570. if (raw2int(f->touch_major) == 0)
  571. continue;
  572. dev->pos[n].x = raw2int(f->abs_x);
  573. dev->pos[n].y = c->y.min + c->y.max - raw2int(f->abs_y);
  574. dev->index[n++] = f;
  575. }
  576. input_mt_assign_slots(input, dev->slots, dev->pos, n, 0);
  577. for (i = 0; i < n; i++)
  578. report_finger_data(input, dev->slots[i],
  579. &dev->pos[i], dev->index[i]);
  580. input_mt_sync_frame(input);
  581. report_synaptics_data(input, c, get_tp_finger(dev, 0), raw_n);
  582. /* later types report button events via integrated button only */
  583. if (c->caps & HAS_INTEGRATED_BUTTON) {
  584. int ibt = raw2int(dev->tp_data[c->tp_button]);
  585. input_report_key(input, BTN_LEFT, ibt);
  586. }
  587. input_sync(input);
  588. return 0;
  589. }
  590. static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
  591. {
  592. const struct bcm5974_config *c = &dev->cfg;
  593. int retval = 0, size;
  594. char *data;
  595. /* Type 3 does not require a mode switch */
  596. if (c->tp_type == TYPE3)
  597. return 0;
  598. data = kmalloc(c->um_size, GFP_KERNEL);
  599. if (!data) {
  600. dev_err(&dev->intf->dev, "out of memory\n");
  601. retval = -ENOMEM;
  602. goto out;
  603. }
  604. /* read configuration */
  605. size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  606. BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
  607. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  608. c->um_req_val, c->um_req_idx, data, c->um_size, 5000);
  609. if (size != c->um_size) {
  610. dev_err(&dev->intf->dev, "could not read from device\n");
  611. retval = -EIO;
  612. goto out;
  613. }
  614. /* apply the mode switch */
  615. data[c->um_switch_idx] = on ? c->um_switch_on : c->um_switch_off;
  616. /* write configuration */
  617. size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  618. BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
  619. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  620. c->um_req_val, c->um_req_idx, data, c->um_size, 5000);
  621. if (size != c->um_size) {
  622. dev_err(&dev->intf->dev, "could not write to device\n");
  623. retval = -EIO;
  624. goto out;
  625. }
  626. dprintk(2, "bcm5974: switched to %s mode.\n",
  627. on ? "wellspring" : "normal");
  628. out:
  629. kfree(data);
  630. return retval;
  631. }
  632. /*
  633. * Mode switches sent before the control response are ignored.
  634. * Fixing this state requires switching to normal mode and waiting
  635. * about 1ms before switching back to wellspring mode.
  636. */
  637. static void bcm5974_mode_reset_work(struct work_struct *work)
  638. {
  639. struct bcm5974 *dev = container_of(work, struct bcm5974, mode_reset_work);
  640. int error;
  641. guard(mutex)(&dev->pm_mutex);
  642. dev->last_mode_reset = jiffies;
  643. error = bcm5974_wellspring_mode(dev, false);
  644. if (error) {
  645. dev_err(&dev->intf->dev, "reset to normal mode failed\n");
  646. return;
  647. }
  648. fsleep(1000);
  649. error = bcm5974_wellspring_mode(dev, true);
  650. if (error)
  651. dev_err(&dev->intf->dev, "mode switch after reset failed\n");
  652. }
  653. static void bcm5974_irq_button(struct urb *urb)
  654. {
  655. struct bcm5974 *dev = urb->context;
  656. struct usb_interface *intf = dev->intf;
  657. int error;
  658. switch (urb->status) {
  659. case 0:
  660. break;
  661. case -EOVERFLOW:
  662. case -ECONNRESET:
  663. case -ENOENT:
  664. case -ESHUTDOWN:
  665. dev_dbg(&intf->dev, "button urb shutting down: %d\n",
  666. urb->status);
  667. return;
  668. default:
  669. dev_dbg(&intf->dev, "button urb status: %d\n", urb->status);
  670. goto exit;
  671. }
  672. if (report_bt_state(dev, dev->bt_urb->actual_length))
  673. dprintk(1, "bcm5974: bad button package, length: %d\n",
  674. dev->bt_urb->actual_length);
  675. exit:
  676. error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
  677. if (error)
  678. dev_err(&intf->dev, "button urb failed: %d\n", error);
  679. }
  680. static void bcm5974_irq_trackpad(struct urb *urb)
  681. {
  682. struct bcm5974 *dev = urb->context;
  683. struct usb_interface *intf = dev->intf;
  684. int error;
  685. switch (urb->status) {
  686. case 0:
  687. break;
  688. case -EOVERFLOW:
  689. case -ECONNRESET:
  690. case -ENOENT:
  691. case -ESHUTDOWN:
  692. dev_dbg(&intf->dev, "trackpad urb shutting down: %d\n",
  693. urb->status);
  694. return;
  695. default:
  696. dev_dbg(&intf->dev, "trackpad urb status: %d\n", urb->status);
  697. goto exit;
  698. }
  699. /* control response ignored */
  700. if (dev->tp_urb->actual_length == 2)
  701. goto exit;
  702. if (report_tp_state(dev, dev->tp_urb->actual_length)) {
  703. dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
  704. dev->tp_urb->actual_length);
  705. /*
  706. * Receiving a HID packet means we aren't in wellspring mode.
  707. * If we haven't tried a reset in the last second, try now.
  708. */
  709. if (dev->tp_urb->actual_length == 8 &&
  710. time_after(jiffies, dev->last_mode_reset + msecs_to_jiffies(1000))) {
  711. schedule_work(&dev->mode_reset_work);
  712. }
  713. }
  714. exit:
  715. error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
  716. if (error)
  717. dev_err(&intf->dev, "trackpad urb failed: %d\n", error);
  718. }
  719. /*
  720. * The Wellspring trackpad, like many recent Apple trackpads, share
  721. * the usb device with the keyboard. Since keyboards are usually
  722. * handled by the HID system, the device ends up being handled by two
  723. * modules. Setting up the device therefore becomes slightly
  724. * complicated. To enable multitouch features, a mode switch is
  725. * required, which is usually applied via the control interface of the
  726. * device. It can be argued where this switch should take place. In
  727. * some drivers, like appletouch, the switch is made during
  728. * probe. However, the hid module may also alter the state of the
  729. * device, resulting in trackpad malfunction under certain
  730. * circumstances. To get around this problem, there is at least one
  731. * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
  732. * receive a reset_resume request rather than the normal resume.
  733. * Since the implementation of reset_resume is equal to mode switch
  734. * plus start_traffic, it seems easier to always do the switch when
  735. * starting traffic on the device.
  736. */
  737. static int bcm5974_start_traffic(struct bcm5974 *dev)
  738. {
  739. int error;
  740. error = bcm5974_wellspring_mode(dev, true);
  741. if (error) {
  742. dprintk(1, "bcm5974: mode switch failed\n");
  743. goto err_out;
  744. }
  745. if (dev->bt_urb) {
  746. error = usb_submit_urb(dev->bt_urb, GFP_KERNEL);
  747. if (error)
  748. goto err_reset_mode;
  749. }
  750. error = usb_submit_urb(dev->tp_urb, GFP_KERNEL);
  751. if (error)
  752. goto err_kill_bt;
  753. return 0;
  754. err_kill_bt:
  755. usb_kill_urb(dev->bt_urb);
  756. err_reset_mode:
  757. bcm5974_wellspring_mode(dev, false);
  758. err_out:
  759. return error;
  760. }
  761. static void bcm5974_pause_traffic(struct bcm5974 *dev)
  762. {
  763. usb_kill_urb(dev->tp_urb);
  764. usb_kill_urb(dev->bt_urb);
  765. bcm5974_wellspring_mode(dev, false);
  766. }
  767. /*
  768. * The code below implements open/close and manual suspend/resume.
  769. * All functions may be called in random order.
  770. *
  771. * Opening a suspended device fails with EACCES - permission denied.
  772. *
  773. * Failing a resume leaves the device resumed but closed.
  774. */
  775. static int bcm5974_open(struct input_dev *input)
  776. {
  777. struct bcm5974 *dev = input_get_drvdata(input);
  778. int error;
  779. error = usb_autopm_get_interface(dev->intf);
  780. if (error)
  781. return error;
  782. scoped_guard(mutex, &dev->pm_mutex) {
  783. error = bcm5974_start_traffic(dev);
  784. if (!error)
  785. dev->opened = 1;
  786. }
  787. if (error)
  788. usb_autopm_put_interface(dev->intf);
  789. return error;
  790. }
  791. static void bcm5974_close(struct input_dev *input)
  792. {
  793. struct bcm5974 *dev = input_get_drvdata(input);
  794. scoped_guard(mutex, &dev->pm_mutex) {
  795. bcm5974_pause_traffic(dev);
  796. dev->opened = 0;
  797. }
  798. usb_autopm_put_interface(dev->intf);
  799. }
  800. static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
  801. {
  802. struct bcm5974 *dev = usb_get_intfdata(iface);
  803. guard(mutex)(&dev->pm_mutex);
  804. if (dev->opened)
  805. bcm5974_pause_traffic(dev);
  806. return 0;
  807. }
  808. static int bcm5974_resume(struct usb_interface *iface)
  809. {
  810. struct bcm5974 *dev = usb_get_intfdata(iface);
  811. guard(mutex)(&dev->pm_mutex);
  812. if (dev->opened)
  813. return bcm5974_start_traffic(dev);
  814. return 0;
  815. }
  816. static int bcm5974_probe(struct usb_interface *iface,
  817. const struct usb_device_id *id)
  818. {
  819. struct usb_device *udev = interface_to_usbdev(iface);
  820. const struct bcm5974_config *cfg;
  821. struct bcm5974 *dev;
  822. struct input_dev *input_dev;
  823. int error = -ENOMEM;
  824. /* find the product index */
  825. cfg = bcm5974_get_config(udev);
  826. /* allocate memory for our device state and initialize it */
  827. dev = kzalloc_obj(*dev);
  828. input_dev = input_allocate_device();
  829. if (!dev || !input_dev) {
  830. dev_err(&iface->dev, "out of memory\n");
  831. goto err_free_devs;
  832. }
  833. dev->udev = udev;
  834. dev->intf = iface;
  835. dev->input = input_dev;
  836. dev->cfg = *cfg;
  837. INIT_WORK(&dev->mode_reset_work, bcm5974_mode_reset_work);
  838. mutex_init(&dev->pm_mutex);
  839. /* setup urbs */
  840. if (cfg->tp_type == TYPE1) {
  841. dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
  842. if (!dev->bt_urb)
  843. goto err_free_devs;
  844. }
  845. dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
  846. if (!dev->tp_urb)
  847. goto err_free_bt_urb;
  848. if (dev->bt_urb) {
  849. dev->bt_data = usb_alloc_coherent(dev->udev,
  850. dev->cfg.bt_datalen, GFP_KERNEL,
  851. &dev->bt_urb->transfer_dma);
  852. if (!dev->bt_data)
  853. goto err_free_urb;
  854. }
  855. dev->tp_data = usb_alloc_coherent(dev->udev,
  856. dev->cfg.tp_datalen, GFP_KERNEL,
  857. &dev->tp_urb->transfer_dma);
  858. if (!dev->tp_data)
  859. goto err_free_bt_buffer;
  860. if (dev->bt_urb) {
  861. usb_fill_int_urb(dev->bt_urb, udev,
  862. usb_rcvintpipe(udev, cfg->bt_ep),
  863. dev->bt_data, dev->cfg.bt_datalen,
  864. bcm5974_irq_button, dev, 1);
  865. dev->bt_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  866. }
  867. usb_fill_int_urb(dev->tp_urb, udev,
  868. usb_rcvintpipe(udev, cfg->tp_ep),
  869. dev->tp_data, dev->cfg.tp_datalen,
  870. bcm5974_irq_trackpad, dev, 1);
  871. dev->tp_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  872. /* create bcm5974 device */
  873. usb_make_path(udev, dev->phys, sizeof(dev->phys));
  874. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  875. input_dev->name = "bcm5974";
  876. input_dev->phys = dev->phys;
  877. usb_to_input_id(dev->udev, &input_dev->id);
  878. /* report driver capabilities via the version field */
  879. input_dev->id.version = cfg->caps;
  880. input_dev->dev.parent = &iface->dev;
  881. input_set_drvdata(input_dev, dev);
  882. input_dev->open = bcm5974_open;
  883. input_dev->close = bcm5974_close;
  884. setup_events_to_report(input_dev, cfg);
  885. error = input_register_device(dev->input);
  886. if (error)
  887. goto err_free_buffer;
  888. /* save our data pointer in this interface device */
  889. usb_set_intfdata(iface, dev);
  890. return 0;
  891. err_free_buffer:
  892. usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
  893. dev->tp_data, dev->tp_urb->transfer_dma);
  894. err_free_bt_buffer:
  895. if (dev->bt_urb)
  896. usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
  897. dev->bt_data, dev->bt_urb->transfer_dma);
  898. err_free_urb:
  899. usb_free_urb(dev->tp_urb);
  900. err_free_bt_urb:
  901. usb_free_urb(dev->bt_urb);
  902. err_free_devs:
  903. usb_set_intfdata(iface, NULL);
  904. input_free_device(input_dev);
  905. kfree(dev);
  906. return error;
  907. }
  908. static void bcm5974_disconnect(struct usb_interface *iface)
  909. {
  910. struct bcm5974 *dev = usb_get_intfdata(iface);
  911. disable_work_sync(&dev->mode_reset_work);
  912. usb_set_intfdata(iface, NULL);
  913. input_unregister_device(dev->input);
  914. usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
  915. dev->tp_data, dev->tp_urb->transfer_dma);
  916. if (dev->bt_urb)
  917. usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
  918. dev->bt_data, dev->bt_urb->transfer_dma);
  919. usb_free_urb(dev->tp_urb);
  920. usb_free_urb(dev->bt_urb);
  921. kfree(dev);
  922. }
  923. static struct usb_driver bcm5974_driver = {
  924. .name = "bcm5974",
  925. .probe = bcm5974_probe,
  926. .disconnect = bcm5974_disconnect,
  927. .suspend = bcm5974_suspend,
  928. .resume = bcm5974_resume,
  929. .id_table = bcm5974_table,
  930. .supports_autosuspend = 1,
  931. };
  932. module_usb_driver(bcm5974_driver);