bcm5974.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /*
  2. * Apple USB BCM5974 (Macbook Air and Penryn Macbook Pro) multitouch driver
  3. *
  4. * Copyright (C) 2008 Henrik Rydberg (rydberg@euromail.se)
  5. *
  6. * The USB initialization and package decoding was made by
  7. * Scott Shawcroft as part of the touchd user-space driver project:
  8. * Copyright (C) 2008 Scott Shawcroft (scott.shawcroft@gmail.com)
  9. *
  10. * The BCM5974 driver is based on the appletouch driver:
  11. * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
  12. * Copyright (C) 2005 Johannes Berg (johannes@sipsolutions.net)
  13. * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
  14. * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de)
  15. * Copyright (C) 2005 Peter Osterlund (petero2@telia.com)
  16. * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch)
  17. * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch)
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/errno.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/module.h>
  39. #include <linux/usb/input.h>
  40. #include <linux/hid.h>
  41. #include <linux/mutex.h>
  42. #define USB_VENDOR_ID_APPLE 0x05ac
  43. /* MacbookAir, aka wellspring */
  44. #define USB_DEVICE_ID_APPLE_WELLSPRING_ANSI 0x0223
  45. #define USB_DEVICE_ID_APPLE_WELLSPRING_ISO 0x0224
  46. #define USB_DEVICE_ID_APPLE_WELLSPRING_JIS 0x0225
  47. /* MacbookProPenryn, aka wellspring2 */
  48. #define USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI 0x0230
  49. #define USB_DEVICE_ID_APPLE_WELLSPRING2_ISO 0x0231
  50. #define USB_DEVICE_ID_APPLE_WELLSPRING2_JIS 0x0232
  51. /* Macbook5,1 (unibody), aka wellspring3 */
  52. #define USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI 0x0236
  53. #define USB_DEVICE_ID_APPLE_WELLSPRING3_ISO 0x0237
  54. #define USB_DEVICE_ID_APPLE_WELLSPRING3_JIS 0x0238
  55. #define BCM5974_DEVICE(prod) { \
  56. .match_flags = (USB_DEVICE_ID_MATCH_DEVICE | \
  57. USB_DEVICE_ID_MATCH_INT_CLASS | \
  58. USB_DEVICE_ID_MATCH_INT_PROTOCOL), \
  59. .idVendor = USB_VENDOR_ID_APPLE, \
  60. .idProduct = (prod), \
  61. .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
  62. .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE \
  63. }
  64. /* table of devices that work with this driver */
  65. static const struct usb_device_id bcm5974_table[] = {
  66. /* MacbookAir1.1 */
  67. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
  68. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
  69. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
  70. /* MacbookProPenryn */
  71. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
  72. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
  73. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
  74. /* Macbook5,1 */
  75. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI),
  76. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ISO),
  77. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_JIS),
  78. /* Terminating entry */
  79. {}
  80. };
  81. MODULE_DEVICE_TABLE(usb, bcm5974_table);
  82. MODULE_AUTHOR("Henrik Rydberg");
  83. MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
  84. MODULE_LICENSE("GPL");
  85. #define dprintk(level, format, a...)\
  86. { if (debug >= level) printk(KERN_DEBUG format, ##a); }
  87. static int debug = 1;
  88. module_param(debug, int, 0644);
  89. MODULE_PARM_DESC(debug, "Activate debugging output");
  90. /* button data structure */
  91. struct bt_data {
  92. u8 unknown1; /* constant */
  93. u8 button; /* left button */
  94. u8 rel_x; /* relative x coordinate */
  95. u8 rel_y; /* relative y coordinate */
  96. };
  97. /* trackpad header types */
  98. enum tp_type {
  99. TYPE1, /* plain trackpad */
  100. TYPE2 /* button integrated in trackpad */
  101. };
  102. /* trackpad finger data offsets, le16-aligned */
  103. #define FINGER_TYPE1 (13 * sizeof(__le16))
  104. #define FINGER_TYPE2 (15 * sizeof(__le16))
  105. /* trackpad button data offsets */
  106. #define BUTTON_TYPE2 15
  107. /* list of device capability bits */
  108. #define HAS_INTEGRATED_BUTTON 1
  109. /* trackpad finger structure, le16-aligned */
  110. struct tp_finger {
  111. __le16 origin; /* zero when switching track finger */
  112. __le16 abs_x; /* absolute x coodinate */
  113. __le16 abs_y; /* absolute y coodinate */
  114. __le16 rel_x; /* relative x coodinate */
  115. __le16 rel_y; /* relative y coodinate */
  116. __le16 size_major; /* finger size, major axis? */
  117. __le16 size_minor; /* finger size, minor axis? */
  118. __le16 orientation; /* 16384 when point, else 15 bit angle */
  119. __le16 force_major; /* trackpad force, major axis? */
  120. __le16 force_minor; /* trackpad force, minor axis? */
  121. __le16 unused[3]; /* zeros */
  122. __le16 multi; /* one finger: varies, more fingers: constant */
  123. } __attribute__((packed,aligned(2)));
  124. /* trackpad finger data size, empirically at least ten fingers */
  125. #define SIZEOF_FINGER sizeof(struct tp_finger)
  126. #define SIZEOF_ALL_FINGERS (16 * SIZEOF_FINGER)
  127. #define MAX_FINGER_ORIENTATION 16384
  128. /* device-specific parameters */
  129. struct bcm5974_param {
  130. int dim; /* logical dimension */
  131. int fuzz; /* logical noise value */
  132. int devmin; /* device minimum reading */
  133. int devmax; /* device maximum reading */
  134. };
  135. /* device-specific configuration */
  136. struct bcm5974_config {
  137. int ansi, iso, jis; /* the product id of this device */
  138. int caps; /* device capability bitmask */
  139. int bt_ep; /* the endpoint of the button interface */
  140. int bt_datalen; /* data length of the button interface */
  141. int tp_ep; /* the endpoint of the trackpad interface */
  142. enum tp_type tp_type; /* type of trackpad interface */
  143. int tp_offset; /* offset to trackpad finger data */
  144. int tp_datalen; /* data length of the trackpad interface */
  145. struct bcm5974_param p; /* finger pressure limits */
  146. struct bcm5974_param w; /* finger width limits */
  147. struct bcm5974_param x; /* horizontal limits */
  148. struct bcm5974_param y; /* vertical limits */
  149. };
  150. /* logical device structure */
  151. struct bcm5974 {
  152. char phys[64];
  153. struct usb_device *udev; /* usb device */
  154. struct usb_interface *intf; /* our interface */
  155. struct input_dev *input; /* input dev */
  156. struct bcm5974_config cfg; /* device configuration */
  157. struct mutex pm_mutex; /* serialize access to open/suspend */
  158. int opened; /* 1: opened, 0: closed */
  159. struct urb *bt_urb; /* button usb request block */
  160. struct bt_data *bt_data; /* button transferred data */
  161. struct urb *tp_urb; /* trackpad usb request block */
  162. u8 *tp_data; /* trackpad transferred data */
  163. int fingers; /* number of fingers on trackpad */
  164. };
  165. /* logical dimensions */
  166. #define DIM_PRESSURE 256 /* maximum finger pressure */
  167. #define DIM_WIDTH 16 /* maximum finger width */
  168. #define DIM_X 1280 /* maximum trackpad x value */
  169. #define DIM_Y 800 /* maximum trackpad y value */
  170. /* logical signal quality */
  171. #define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
  172. #define SN_WIDTH 100 /* width signal-to-noise ratio */
  173. #define SN_COORD 250 /* coordinate signal-to-noise ratio */
  174. /* pressure thresholds */
  175. #define PRESSURE_LOW (2 * DIM_PRESSURE / SN_PRESSURE)
  176. #define PRESSURE_HIGH (3 * PRESSURE_LOW)
  177. /* device constants */
  178. static const struct bcm5974_config bcm5974_config_table[] = {
  179. {
  180. USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
  181. USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
  182. USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
  183. 0,
  184. 0x84, sizeof(struct bt_data),
  185. 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
  186. { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
  187. { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
  188. { DIM_X, DIM_X / SN_COORD, -4824, 5342 },
  189. { DIM_Y, DIM_Y / SN_COORD, -172, 5820 }
  190. },
  191. {
  192. USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
  193. USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
  194. USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
  195. 0,
  196. 0x84, sizeof(struct bt_data),
  197. 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
  198. { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
  199. { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
  200. { DIM_X, DIM_X / SN_COORD, -4824, 4824 },
  201. { DIM_Y, DIM_Y / SN_COORD, -172, 4290 }
  202. },
  203. {
  204. USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI,
  205. USB_DEVICE_ID_APPLE_WELLSPRING3_ISO,
  206. USB_DEVICE_ID_APPLE_WELLSPRING3_JIS,
  207. HAS_INTEGRATED_BUTTON,
  208. 0x84, sizeof(struct bt_data),
  209. 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
  210. { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
  211. { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
  212. { DIM_X, DIM_X / SN_COORD, -4460, 5166 },
  213. { DIM_Y, DIM_Y / SN_COORD, -75, 6700 }
  214. },
  215. {}
  216. };
  217. /* return the device-specific configuration by device */
  218. static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
  219. {
  220. u16 id = le16_to_cpu(udev->descriptor.idProduct);
  221. const struct bcm5974_config *cfg;
  222. for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
  223. if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
  224. return cfg;
  225. return bcm5974_config_table;
  226. }
  227. /* convert 16-bit little endian to signed integer */
  228. static inline int raw2int(__le16 x)
  229. {
  230. return (signed short)le16_to_cpu(x);
  231. }
  232. /* scale device data to logical dimensions (asserts devmin < devmax) */
  233. static inline int int2scale(const struct bcm5974_param *p, int x)
  234. {
  235. return x * p->dim / (p->devmax - p->devmin);
  236. }
  237. /* all logical value ranges are [0,dim). */
  238. static inline int int2bound(const struct bcm5974_param *p, int x)
  239. {
  240. int s = int2scale(p, x);
  241. return clamp_val(s, 0, p->dim - 1);
  242. }
  243. /* setup which logical events to report */
  244. static void setup_events_to_report(struct input_dev *input_dev,
  245. const struct bcm5974_config *cfg)
  246. {
  247. __set_bit(EV_ABS, input_dev->evbit);
  248. input_set_abs_params(input_dev, ABS_PRESSURE,
  249. 0, cfg->p.dim, cfg->p.fuzz, 0);
  250. input_set_abs_params(input_dev, ABS_TOOL_WIDTH,
  251. 0, cfg->w.dim, cfg->w.fuzz, 0);
  252. input_set_abs_params(input_dev, ABS_X,
  253. 0, cfg->x.dim, cfg->x.fuzz, 0);
  254. input_set_abs_params(input_dev, ABS_Y,
  255. 0, cfg->y.dim, cfg->y.fuzz, 0);
  256. /* finger touch area */
  257. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
  258. cfg->w.devmin, cfg->w.devmax, 0, 0);
  259. input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
  260. cfg->w.devmin, cfg->w.devmax, 0, 0);
  261. /* finger approach area */
  262. input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR,
  263. cfg->w.devmin, cfg->w.devmax, 0, 0);
  264. input_set_abs_params(input_dev, ABS_MT_WIDTH_MINOR,
  265. cfg->w.devmin, cfg->w.devmax, 0, 0);
  266. /* finger orientation */
  267. input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
  268. -MAX_FINGER_ORIENTATION,
  269. MAX_FINGER_ORIENTATION, 0, 0);
  270. /* finger position */
  271. input_set_abs_params(input_dev, ABS_MT_POSITION_X,
  272. cfg->x.devmin, cfg->x.devmax, 0, 0);
  273. input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
  274. cfg->y.devmin, cfg->y.devmax, 0, 0);
  275. __set_bit(EV_KEY, input_dev->evbit);
  276. __set_bit(BTN_TOUCH, input_dev->keybit);
  277. __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
  278. __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
  279. __set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
  280. __set_bit(BTN_TOOL_QUADTAP, input_dev->keybit);
  281. __set_bit(BTN_LEFT, input_dev->keybit);
  282. }
  283. /* report button data as logical button state */
  284. static int report_bt_state(struct bcm5974 *dev, int size)
  285. {
  286. if (size != sizeof(struct bt_data))
  287. return -EIO;
  288. dprintk(7,
  289. "bcm5974: button data: %x %x %x %x\n",
  290. dev->bt_data->unknown1, dev->bt_data->button,
  291. dev->bt_data->rel_x, dev->bt_data->rel_y);
  292. input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
  293. input_sync(dev->input);
  294. return 0;
  295. }
  296. static void report_finger_data(struct input_dev *input,
  297. const struct bcm5974_config *cfg,
  298. const struct tp_finger *f)
  299. {
  300. input_report_abs(input, ABS_MT_TOUCH_MAJOR, raw2int(f->force_major));
  301. input_report_abs(input, ABS_MT_TOUCH_MINOR, raw2int(f->force_minor));
  302. input_report_abs(input, ABS_MT_WIDTH_MAJOR, raw2int(f->size_major));
  303. input_report_abs(input, ABS_MT_WIDTH_MINOR, raw2int(f->size_minor));
  304. input_report_abs(input, ABS_MT_ORIENTATION,
  305. MAX_FINGER_ORIENTATION - raw2int(f->orientation));
  306. input_report_abs(input, ABS_MT_POSITION_X, raw2int(f->abs_x));
  307. input_report_abs(input, ABS_MT_POSITION_Y,
  308. cfg->y.devmin + cfg->y.devmax - raw2int(f->abs_y));
  309. input_mt_sync(input);
  310. }
  311. /* report trackpad data as logical trackpad state */
  312. static int report_tp_state(struct bcm5974 *dev, int size)
  313. {
  314. const struct bcm5974_config *c = &dev->cfg;
  315. const struct tp_finger *f;
  316. struct input_dev *input = dev->input;
  317. int raw_p, raw_w, raw_x, raw_y, raw_n, i;
  318. int ptest, origin, ibt = 0, nmin = 0, nmax = 0;
  319. int abs_p = 0, abs_w = 0, abs_x = 0, abs_y = 0;
  320. if (size < c->tp_offset || (size - c->tp_offset) % SIZEOF_FINGER != 0)
  321. return -EIO;
  322. /* finger data, le16-aligned */
  323. f = (const struct tp_finger *)(dev->tp_data + c->tp_offset);
  324. raw_n = (size - c->tp_offset) / SIZEOF_FINGER;
  325. /* always track the first finger; when detached, start over */
  326. if (raw_n) {
  327. /* report raw trackpad data */
  328. for (i = 0; i < raw_n; i++)
  329. report_finger_data(input, c, &f[i]);
  330. raw_p = raw2int(f->force_major);
  331. raw_w = raw2int(f->size_major);
  332. raw_x = raw2int(f->abs_x);
  333. raw_y = raw2int(f->abs_y);
  334. dprintk(9,
  335. "bcm5974: "
  336. "raw: p: %+05d w: %+05d x: %+05d y: %+05d n: %d\n",
  337. raw_p, raw_w, raw_x, raw_y, raw_n);
  338. ptest = int2bound(&c->p, raw_p);
  339. origin = raw2int(f->origin);
  340. /* set the integrated button if applicable */
  341. if (c->tp_type == TYPE2)
  342. ibt = raw2int(dev->tp_data[BUTTON_TYPE2]);
  343. /* while tracking finger still valid, count all fingers */
  344. if (ptest > PRESSURE_LOW && origin) {
  345. abs_p = ptest;
  346. abs_w = int2bound(&c->w, raw_w);
  347. abs_x = int2bound(&c->x, raw_x - c->x.devmin);
  348. abs_y = int2bound(&c->y, c->y.devmax - raw_y);
  349. while (raw_n--) {
  350. ptest = int2bound(&c->p,
  351. raw2int(f->force_major));
  352. if (ptest > PRESSURE_LOW)
  353. nmax++;
  354. if (ptest > PRESSURE_HIGH)
  355. nmin++;
  356. f++;
  357. }
  358. }
  359. }
  360. if (dev->fingers < nmin)
  361. dev->fingers = nmin;
  362. if (dev->fingers > nmax)
  363. dev->fingers = nmax;
  364. input_report_key(input, BTN_TOUCH, dev->fingers > 0);
  365. input_report_key(input, BTN_TOOL_FINGER, dev->fingers == 1);
  366. input_report_key(input, BTN_TOOL_DOUBLETAP, dev->fingers == 2);
  367. input_report_key(input, BTN_TOOL_TRIPLETAP, dev->fingers == 3);
  368. input_report_key(input, BTN_TOOL_QUADTAP, dev->fingers > 3);
  369. input_report_abs(input, ABS_PRESSURE, abs_p);
  370. input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
  371. if (abs_p) {
  372. input_report_abs(input, ABS_X, abs_x);
  373. input_report_abs(input, ABS_Y, abs_y);
  374. dprintk(8,
  375. "bcm5974: abs: p: %+05d w: %+05d x: %+05d y: %+05d "
  376. "nmin: %d nmax: %d n: %d ibt: %d\n", abs_p, abs_w,
  377. abs_x, abs_y, nmin, nmax, dev->fingers, ibt);
  378. }
  379. /* type 2 reports button events via ibt only */
  380. if (c->tp_type == TYPE2)
  381. input_report_key(input, BTN_LEFT, ibt);
  382. input_sync(input);
  383. return 0;
  384. }
  385. /* Wellspring initialization constants */
  386. #define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID 1
  387. #define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID 9
  388. #define BCM5974_WELLSPRING_MODE_REQUEST_VALUE 0x300
  389. #define BCM5974_WELLSPRING_MODE_REQUEST_INDEX 0
  390. #define BCM5974_WELLSPRING_MODE_VENDOR_VALUE 0x01
  391. #define BCM5974_WELLSPRING_MODE_NORMAL_VALUE 0x08
  392. static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
  393. {
  394. char *data = kmalloc(8, GFP_KERNEL);
  395. int retval = 0, size;
  396. if (!data) {
  397. err("bcm5974: out of memory");
  398. retval = -ENOMEM;
  399. goto out;
  400. }
  401. /* read configuration */
  402. size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  403. BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
  404. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  405. BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
  406. BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
  407. if (size != 8) {
  408. err("bcm5974: could not read from device");
  409. retval = -EIO;
  410. goto out;
  411. }
  412. /* apply the mode switch */
  413. data[0] = on ?
  414. BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
  415. BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
  416. /* write configuration */
  417. size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  418. BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
  419. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  420. BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
  421. BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
  422. if (size != 8) {
  423. err("bcm5974: could not write to device");
  424. retval = -EIO;
  425. goto out;
  426. }
  427. dprintk(2, "bcm5974: switched to %s mode.\n",
  428. on ? "wellspring" : "normal");
  429. out:
  430. kfree(data);
  431. return retval;
  432. }
  433. static void bcm5974_irq_button(struct urb *urb)
  434. {
  435. struct bcm5974 *dev = urb->context;
  436. int error;
  437. switch (urb->status) {
  438. case 0:
  439. break;
  440. case -EOVERFLOW:
  441. case -ECONNRESET:
  442. case -ENOENT:
  443. case -ESHUTDOWN:
  444. dbg("bcm5974: button urb shutting down: %d", urb->status);
  445. return;
  446. default:
  447. dbg("bcm5974: button urb status: %d", urb->status);
  448. goto exit;
  449. }
  450. if (report_bt_state(dev, dev->bt_urb->actual_length))
  451. dprintk(1, "bcm5974: bad button package, length: %d\n",
  452. dev->bt_urb->actual_length);
  453. exit:
  454. error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
  455. if (error)
  456. err("bcm5974: button urb failed: %d", error);
  457. }
  458. static void bcm5974_irq_trackpad(struct urb *urb)
  459. {
  460. struct bcm5974 *dev = urb->context;
  461. int error;
  462. switch (urb->status) {
  463. case 0:
  464. break;
  465. case -EOVERFLOW:
  466. case -ECONNRESET:
  467. case -ENOENT:
  468. case -ESHUTDOWN:
  469. dbg("bcm5974: trackpad urb shutting down: %d", urb->status);
  470. return;
  471. default:
  472. dbg("bcm5974: trackpad urb status: %d", urb->status);
  473. goto exit;
  474. }
  475. /* control response ignored */
  476. if (dev->tp_urb->actual_length == 2)
  477. goto exit;
  478. if (report_tp_state(dev, dev->tp_urb->actual_length))
  479. dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
  480. dev->tp_urb->actual_length);
  481. exit:
  482. error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
  483. if (error)
  484. err("bcm5974: trackpad urb failed: %d", error);
  485. }
  486. /*
  487. * The Wellspring trackpad, like many recent Apple trackpads, share
  488. * the usb device with the keyboard. Since keyboards are usually
  489. * handled by the HID system, the device ends up being handled by two
  490. * modules. Setting up the device therefore becomes slightly
  491. * complicated. To enable multitouch features, a mode switch is
  492. * required, which is usually applied via the control interface of the
  493. * device. It can be argued where this switch should take place. In
  494. * some drivers, like appletouch, the switch is made during
  495. * probe. However, the hid module may also alter the state of the
  496. * device, resulting in trackpad malfunction under certain
  497. * circumstances. To get around this problem, there is at least one
  498. * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
  499. * recieve a reset_resume request rather than the normal resume.
  500. * Since the implementation of reset_resume is equal to mode switch
  501. * plus start_traffic, it seems easier to always do the switch when
  502. * starting traffic on the device.
  503. */
  504. static int bcm5974_start_traffic(struct bcm5974 *dev)
  505. {
  506. if (bcm5974_wellspring_mode(dev, true)) {
  507. dprintk(1, "bcm5974: mode switch failed\n");
  508. goto error;
  509. }
  510. if (usb_submit_urb(dev->bt_urb, GFP_KERNEL))
  511. goto error;
  512. if (usb_submit_urb(dev->tp_urb, GFP_KERNEL))
  513. goto err_kill_bt;
  514. return 0;
  515. err_kill_bt:
  516. usb_kill_urb(dev->bt_urb);
  517. error:
  518. return -EIO;
  519. }
  520. static void bcm5974_pause_traffic(struct bcm5974 *dev)
  521. {
  522. usb_kill_urb(dev->tp_urb);
  523. usb_kill_urb(dev->bt_urb);
  524. bcm5974_wellspring_mode(dev, false);
  525. }
  526. /*
  527. * The code below implements open/close and manual suspend/resume.
  528. * All functions may be called in random order.
  529. *
  530. * Opening a suspended device fails with EACCES - permission denied.
  531. *
  532. * Failing a resume leaves the device resumed but closed.
  533. */
  534. static int bcm5974_open(struct input_dev *input)
  535. {
  536. struct bcm5974 *dev = input_get_drvdata(input);
  537. int error;
  538. error = usb_autopm_get_interface(dev->intf);
  539. if (error)
  540. return error;
  541. mutex_lock(&dev->pm_mutex);
  542. error = bcm5974_start_traffic(dev);
  543. if (!error)
  544. dev->opened = 1;
  545. mutex_unlock(&dev->pm_mutex);
  546. if (error)
  547. usb_autopm_put_interface(dev->intf);
  548. return error;
  549. }
  550. static void bcm5974_close(struct input_dev *input)
  551. {
  552. struct bcm5974 *dev = input_get_drvdata(input);
  553. mutex_lock(&dev->pm_mutex);
  554. bcm5974_pause_traffic(dev);
  555. dev->opened = 0;
  556. mutex_unlock(&dev->pm_mutex);
  557. usb_autopm_put_interface(dev->intf);
  558. }
  559. static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
  560. {
  561. struct bcm5974 *dev = usb_get_intfdata(iface);
  562. mutex_lock(&dev->pm_mutex);
  563. if (dev->opened)
  564. bcm5974_pause_traffic(dev);
  565. mutex_unlock(&dev->pm_mutex);
  566. return 0;
  567. }
  568. static int bcm5974_resume(struct usb_interface *iface)
  569. {
  570. struct bcm5974 *dev = usb_get_intfdata(iface);
  571. int error = 0;
  572. mutex_lock(&dev->pm_mutex);
  573. if (dev->opened)
  574. error = bcm5974_start_traffic(dev);
  575. mutex_unlock(&dev->pm_mutex);
  576. return error;
  577. }
  578. static int bcm5974_probe(struct usb_interface *iface,
  579. const struct usb_device_id *id)
  580. {
  581. struct usb_device *udev = interface_to_usbdev(iface);
  582. const struct bcm5974_config *cfg;
  583. struct bcm5974 *dev;
  584. struct input_dev *input_dev;
  585. int error = -ENOMEM;
  586. /* find the product index */
  587. cfg = bcm5974_get_config(udev);
  588. /* allocate memory for our device state and initialize it */
  589. dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
  590. input_dev = input_allocate_device();
  591. if (!dev || !input_dev) {
  592. err("bcm5974: out of memory");
  593. goto err_free_devs;
  594. }
  595. dev->udev = udev;
  596. dev->intf = iface;
  597. dev->input = input_dev;
  598. dev->cfg = *cfg;
  599. mutex_init(&dev->pm_mutex);
  600. /* setup urbs */
  601. dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
  602. if (!dev->bt_urb)
  603. goto err_free_devs;
  604. dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
  605. if (!dev->tp_urb)
  606. goto err_free_bt_urb;
  607. dev->bt_data = usb_buffer_alloc(dev->udev,
  608. dev->cfg.bt_datalen, GFP_KERNEL,
  609. &dev->bt_urb->transfer_dma);
  610. if (!dev->bt_data)
  611. goto err_free_urb;
  612. dev->tp_data = usb_buffer_alloc(dev->udev,
  613. dev->cfg.tp_datalen, GFP_KERNEL,
  614. &dev->tp_urb->transfer_dma);
  615. if (!dev->tp_data)
  616. goto err_free_bt_buffer;
  617. usb_fill_int_urb(dev->bt_urb, udev,
  618. usb_rcvintpipe(udev, cfg->bt_ep),
  619. dev->bt_data, dev->cfg.bt_datalen,
  620. bcm5974_irq_button, dev, 1);
  621. usb_fill_int_urb(dev->tp_urb, udev,
  622. usb_rcvintpipe(udev, cfg->tp_ep),
  623. dev->tp_data, dev->cfg.tp_datalen,
  624. bcm5974_irq_trackpad, dev, 1);
  625. /* create bcm5974 device */
  626. usb_make_path(udev, dev->phys, sizeof(dev->phys));
  627. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  628. input_dev->name = "bcm5974";
  629. input_dev->phys = dev->phys;
  630. usb_to_input_id(dev->udev, &input_dev->id);
  631. /* report driver capabilities via the version field */
  632. input_dev->id.version = cfg->caps;
  633. input_dev->dev.parent = &iface->dev;
  634. input_set_drvdata(input_dev, dev);
  635. input_dev->open = bcm5974_open;
  636. input_dev->close = bcm5974_close;
  637. setup_events_to_report(input_dev, cfg);
  638. error = input_register_device(dev->input);
  639. if (error)
  640. goto err_free_buffer;
  641. /* save our data pointer in this interface device */
  642. usb_set_intfdata(iface, dev);
  643. return 0;
  644. err_free_buffer:
  645. usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
  646. dev->tp_data, dev->tp_urb->transfer_dma);
  647. err_free_bt_buffer:
  648. usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
  649. dev->bt_data, dev->bt_urb->transfer_dma);
  650. err_free_urb:
  651. usb_free_urb(dev->tp_urb);
  652. err_free_bt_urb:
  653. usb_free_urb(dev->bt_urb);
  654. err_free_devs:
  655. usb_set_intfdata(iface, NULL);
  656. input_free_device(input_dev);
  657. kfree(dev);
  658. return error;
  659. }
  660. static void bcm5974_disconnect(struct usb_interface *iface)
  661. {
  662. struct bcm5974 *dev = usb_get_intfdata(iface);
  663. usb_set_intfdata(iface, NULL);
  664. input_unregister_device(dev->input);
  665. usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
  666. dev->tp_data, dev->tp_urb->transfer_dma);
  667. usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
  668. dev->bt_data, dev->bt_urb->transfer_dma);
  669. usb_free_urb(dev->tp_urb);
  670. usb_free_urb(dev->bt_urb);
  671. kfree(dev);
  672. }
  673. static struct usb_driver bcm5974_driver = {
  674. .name = "bcm5974",
  675. .probe = bcm5974_probe,
  676. .disconnect = bcm5974_disconnect,
  677. .suspend = bcm5974_suspend,
  678. .resume = bcm5974_resume,
  679. .id_table = bcm5974_table,
  680. .supports_autosuspend = 1,
  681. };
  682. static int __init bcm5974_init(void)
  683. {
  684. return usb_register(&bcm5974_driver);
  685. }
  686. static void __exit bcm5974_exit(void)
  687. {
  688. usb_deregister(&bcm5974_driver);
  689. }
  690. module_init(bcm5974_init);
  691. module_exit(bcm5974_exit);