bcm5974.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. #define BCM5974_DEVICE(prod) { \
  52. .match_flags = (USB_DEVICE_ID_MATCH_DEVICE | \
  53. USB_DEVICE_ID_MATCH_INT_CLASS | \
  54. USB_DEVICE_ID_MATCH_INT_PROTOCOL), \
  55. .idVendor = USB_VENDOR_ID_APPLE, \
  56. .idProduct = (prod), \
  57. .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
  58. .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE \
  59. }
  60. /* table of devices that work with this driver */
  61. static const struct usb_device_id bcm5974_table[] = {
  62. /* MacbookAir1.1 */
  63. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
  64. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
  65. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
  66. /* MacbookProPenryn */
  67. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
  68. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
  69. BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
  70. /* Terminating entry */
  71. {}
  72. };
  73. MODULE_DEVICE_TABLE(usb, bcm5974_table);
  74. MODULE_AUTHOR("Henrik Rydberg");
  75. MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
  76. MODULE_LICENSE("GPL");
  77. #define dprintk(level, format, a...)\
  78. { if (debug >= level) printk(KERN_DEBUG format, ##a); }
  79. static int debug = 1;
  80. module_param(debug, int, 0644);
  81. MODULE_PARM_DESC(debug, "Activate debugging output");
  82. /* button data structure */
  83. struct bt_data {
  84. u8 unknown1; /* constant */
  85. u8 button; /* left button */
  86. u8 rel_x; /* relative x coordinate */
  87. u8 rel_y; /* relative y coordinate */
  88. };
  89. /* trackpad header structure */
  90. struct tp_header {
  91. u8 unknown1[16]; /* constants, timers, etc */
  92. u8 fingers; /* number of fingers on trackpad */
  93. u8 unknown2[9]; /* constants, timers, etc */
  94. };
  95. /* trackpad finger structure */
  96. struct tp_finger {
  97. __le16 origin; /* zero when switching track finger */
  98. __le16 abs_x; /* absolute x coodinate */
  99. __le16 abs_y; /* absolute y coodinate */
  100. __le16 rel_x; /* relative x coodinate */
  101. __le16 rel_y; /* relative y coodinate */
  102. __le16 size_major; /* finger size, major axis? */
  103. __le16 size_minor; /* finger size, minor axis? */
  104. __le16 orientation; /* 16384 when point, else 15 bit angle */
  105. __le16 force_major; /* trackpad force, major axis? */
  106. __le16 force_minor; /* trackpad force, minor axis? */
  107. __le16 unused[3]; /* zeros */
  108. __le16 multi; /* one finger: varies, more fingers: constant */
  109. };
  110. /* trackpad data structure, empirically at least ten fingers */
  111. struct tp_data {
  112. struct tp_header header;
  113. struct tp_finger finger[16];
  114. };
  115. /* device-specific parameters */
  116. struct bcm5974_param {
  117. int dim; /* logical dimension */
  118. int fuzz; /* logical noise value */
  119. int devmin; /* device minimum reading */
  120. int devmax; /* device maximum reading */
  121. };
  122. /* device-specific configuration */
  123. struct bcm5974_config {
  124. int ansi, iso, jis; /* the product id of this device */
  125. int bt_ep; /* the endpoint of the button interface */
  126. int bt_datalen; /* data length of the button interface */
  127. int tp_ep; /* the endpoint of the trackpad interface */
  128. int tp_datalen; /* data length of the trackpad interface */
  129. struct bcm5974_param p; /* finger pressure limits */
  130. struct bcm5974_param w; /* finger width limits */
  131. struct bcm5974_param x; /* horizontal limits */
  132. struct bcm5974_param y; /* vertical limits */
  133. };
  134. /* logical device structure */
  135. struct bcm5974 {
  136. char phys[64];
  137. struct usb_device *udev; /* usb device */
  138. struct usb_interface *intf; /* our interface */
  139. struct input_dev *input; /* input dev */
  140. struct bcm5974_config cfg; /* device configuration */
  141. struct mutex pm_mutex; /* serialize access to open/suspend */
  142. int opened; /* 1: opened, 0: closed */
  143. struct urb *bt_urb; /* button usb request block */
  144. struct bt_data *bt_data; /* button transferred data */
  145. struct urb *tp_urb; /* trackpad usb request block */
  146. struct tp_data *tp_data; /* trackpad transferred data */
  147. int fingers; /* number of fingers on trackpad */
  148. };
  149. /* logical dimensions */
  150. #define DIM_PRESSURE 256 /* maximum finger pressure */
  151. #define DIM_WIDTH 16 /* maximum finger width */
  152. #define DIM_X 1280 /* maximum trackpad x value */
  153. #define DIM_Y 800 /* maximum trackpad y value */
  154. /* logical signal quality */
  155. #define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
  156. #define SN_WIDTH 100 /* width signal-to-noise ratio */
  157. #define SN_COORD 250 /* coordinate signal-to-noise ratio */
  158. /* pressure thresholds */
  159. #define PRESSURE_LOW (2 * DIM_PRESSURE / SN_PRESSURE)
  160. #define PRESSURE_HIGH (3 * PRESSURE_LOW)
  161. /* device constants */
  162. static const struct bcm5974_config bcm5974_config_table[] = {
  163. {
  164. USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
  165. USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
  166. USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
  167. 0x84, sizeof(struct bt_data),
  168. 0x81, sizeof(struct tp_data),
  169. { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
  170. { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
  171. { DIM_X, DIM_X / SN_COORD, -4824, 5342 },
  172. { DIM_Y, DIM_Y / SN_COORD, -172, 5820 }
  173. },
  174. {
  175. USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
  176. USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
  177. USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
  178. 0x84, sizeof(struct bt_data),
  179. 0x81, sizeof(struct tp_data),
  180. { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
  181. { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
  182. { DIM_X, DIM_X / SN_COORD, -4824, 4824 },
  183. { DIM_Y, DIM_Y / SN_COORD, -172, 4290 }
  184. },
  185. {}
  186. };
  187. /* return the device-specific configuration by device */
  188. static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
  189. {
  190. u16 id = le16_to_cpu(udev->descriptor.idProduct);
  191. const struct bcm5974_config *cfg;
  192. for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
  193. if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
  194. return cfg;
  195. return bcm5974_config_table;
  196. }
  197. /* convert 16-bit little endian to signed integer */
  198. static inline int raw2int(__le16 x)
  199. {
  200. return (signed short)le16_to_cpu(x);
  201. }
  202. /* scale device data to logical dimensions (asserts devmin < devmax) */
  203. static inline int int2scale(const struct bcm5974_param *p, int x)
  204. {
  205. return x * p->dim / (p->devmax - p->devmin);
  206. }
  207. /* all logical value ranges are [0,dim). */
  208. static inline int int2bound(const struct bcm5974_param *p, int x)
  209. {
  210. int s = int2scale(p, x);
  211. return clamp_val(s, 0, p->dim - 1);
  212. }
  213. /* setup which logical events to report */
  214. static void setup_events_to_report(struct input_dev *input_dev,
  215. const struct bcm5974_config *cfg)
  216. {
  217. __set_bit(EV_ABS, input_dev->evbit);
  218. input_set_abs_params(input_dev, ABS_PRESSURE,
  219. 0, cfg->p.dim, cfg->p.fuzz, 0);
  220. input_set_abs_params(input_dev, ABS_TOOL_WIDTH,
  221. 0, cfg->w.dim, cfg->w.fuzz, 0);
  222. input_set_abs_params(input_dev, ABS_X,
  223. 0, cfg->x.dim, cfg->x.fuzz, 0);
  224. input_set_abs_params(input_dev, ABS_Y,
  225. 0, cfg->y.dim, cfg->y.fuzz, 0);
  226. __set_bit(EV_KEY, input_dev->evbit);
  227. __set_bit(BTN_TOUCH, input_dev->keybit);
  228. __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
  229. __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
  230. __set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
  231. __set_bit(BTN_LEFT, input_dev->keybit);
  232. }
  233. /* report button data as logical button state */
  234. static int report_bt_state(struct bcm5974 *dev, int size)
  235. {
  236. if (size != sizeof(struct bt_data))
  237. return -EIO;
  238. input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
  239. input_sync(dev->input);
  240. return 0;
  241. }
  242. /* report trackpad data as logical trackpad state */
  243. static int report_tp_state(struct bcm5974 *dev, int size)
  244. {
  245. const struct bcm5974_config *c = &dev->cfg;
  246. const struct tp_finger *f = dev->tp_data->finger;
  247. struct input_dev *input = dev->input;
  248. const int fingers = (size - 26) / 28;
  249. int raw_p, raw_w, raw_x, raw_y;
  250. int ptest = 0, origin = 0, nmin = 0, nmax = 0;
  251. int abs_p = 0, abs_w = 0, abs_x = 0, abs_y = 0;
  252. if (size < 26 || (size - 26) % 28 != 0)
  253. return -EIO;
  254. /* always track the first finger; when detached, start over */
  255. if (fingers) {
  256. raw_p = raw2int(f->force_major);
  257. raw_w = raw2int(f->size_major);
  258. raw_x = raw2int(f->abs_x);
  259. raw_y = raw2int(f->abs_y);
  260. dprintk(9,
  261. "bcm5974: raw: p: %+05d w: %+05d x: %+05d y: %+05d\n",
  262. raw_p, raw_w, raw_x, raw_y);
  263. ptest = int2bound(&c->p, raw_p);
  264. origin = raw2int(f->origin);
  265. }
  266. /* while tracking finger still valid, count all fingers */
  267. if (ptest > PRESSURE_LOW && origin) {
  268. abs_p = ptest;
  269. abs_w = int2bound(&c->w, raw_w);
  270. abs_x = int2bound(&c->x, raw_x - c->x.devmin);
  271. abs_y = int2bound(&c->y, c->y.devmax - raw_y);
  272. for (; f != dev->tp_data->finger + fingers; f++) {
  273. ptest = int2bound(&c->p, raw2int(f->force_major));
  274. if (ptest > PRESSURE_LOW)
  275. nmax++;
  276. if (ptest > PRESSURE_HIGH)
  277. nmin++;
  278. }
  279. }
  280. if (dev->fingers < nmin)
  281. dev->fingers = nmin;
  282. if (dev->fingers > nmax)
  283. dev->fingers = nmax;
  284. input_report_key(input, BTN_TOUCH, dev->fingers > 0);
  285. input_report_key(input, BTN_TOOL_FINGER, dev->fingers == 1);
  286. input_report_key(input, BTN_TOOL_DOUBLETAP, dev->fingers == 2);
  287. input_report_key(input, BTN_TOOL_TRIPLETAP, dev->fingers > 2);
  288. input_report_abs(input, ABS_PRESSURE, abs_p);
  289. input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
  290. if (abs_p) {
  291. input_report_abs(input, ABS_X, abs_x);
  292. input_report_abs(input, ABS_Y, abs_y);
  293. dprintk(8,
  294. "bcm5974: abs: p: %+05d w: %+05d x: %+05d y: %+05d "
  295. "nmin: %d nmax: %d n: %d\n",
  296. abs_p, abs_w, abs_x, abs_y, nmin, nmax, dev->fingers);
  297. }
  298. input_sync(input);
  299. return 0;
  300. }
  301. /* Wellspring initialization constants */
  302. #define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID 1
  303. #define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID 9
  304. #define BCM5974_WELLSPRING_MODE_REQUEST_VALUE 0x300
  305. #define BCM5974_WELLSPRING_MODE_REQUEST_INDEX 0
  306. #define BCM5974_WELLSPRING_MODE_VENDOR_VALUE 0x01
  307. static int bcm5974_wellspring_mode(struct bcm5974 *dev)
  308. {
  309. char *data = kmalloc(8, GFP_KERNEL);
  310. int retval = 0, size;
  311. if (!data) {
  312. err("bcm5974: out of memory");
  313. retval = -ENOMEM;
  314. goto out;
  315. }
  316. /* read configuration */
  317. size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  318. BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
  319. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  320. BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
  321. BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
  322. if (size != 8) {
  323. err("bcm5974: could not read from device");
  324. retval = -EIO;
  325. goto out;
  326. }
  327. /* apply the mode switch */
  328. data[0] = BCM5974_WELLSPRING_MODE_VENDOR_VALUE;
  329. /* write configuration */
  330. size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  331. BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
  332. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  333. BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
  334. BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
  335. if (size != 8) {
  336. err("bcm5974: could not write to device");
  337. retval = -EIO;
  338. goto out;
  339. }
  340. dprintk(2, "bcm5974: switched to wellspring mode.\n");
  341. out:
  342. kfree(data);
  343. return retval;
  344. }
  345. static void bcm5974_irq_button(struct urb *urb)
  346. {
  347. struct bcm5974 *dev = urb->context;
  348. int error;
  349. switch (urb->status) {
  350. case 0:
  351. break;
  352. case -EOVERFLOW:
  353. case -ECONNRESET:
  354. case -ENOENT:
  355. case -ESHUTDOWN:
  356. dbg("bcm5974: button urb shutting down: %d", urb->status);
  357. return;
  358. default:
  359. dbg("bcm5974: button urb status: %d", urb->status);
  360. goto exit;
  361. }
  362. if (report_bt_state(dev, dev->bt_urb->actual_length))
  363. dprintk(1, "bcm5974: bad button package, length: %d\n",
  364. dev->bt_urb->actual_length);
  365. exit:
  366. error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
  367. if (error)
  368. err("bcm5974: button urb failed: %d", error);
  369. }
  370. static void bcm5974_irq_trackpad(struct urb *urb)
  371. {
  372. struct bcm5974 *dev = urb->context;
  373. int error;
  374. switch (urb->status) {
  375. case 0:
  376. break;
  377. case -EOVERFLOW:
  378. case -ECONNRESET:
  379. case -ENOENT:
  380. case -ESHUTDOWN:
  381. dbg("bcm5974: trackpad urb shutting down: %d", urb->status);
  382. return;
  383. default:
  384. dbg("bcm5974: trackpad urb status: %d", urb->status);
  385. goto exit;
  386. }
  387. /* control response ignored */
  388. if (dev->tp_urb->actual_length == 2)
  389. goto exit;
  390. if (report_tp_state(dev, dev->tp_urb->actual_length))
  391. dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
  392. dev->tp_urb->actual_length);
  393. exit:
  394. error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
  395. if (error)
  396. err("bcm5974: trackpad urb failed: %d", error);
  397. }
  398. /*
  399. * The Wellspring trackpad, like many recent Apple trackpads, share
  400. * the usb device with the keyboard. Since keyboards are usually
  401. * handled by the HID system, the device ends up being handled by two
  402. * modules. Setting up the device therefore becomes slightly
  403. * complicated. To enable multitouch features, a mode switch is
  404. * required, which is usually applied via the control interface of the
  405. * device. It can be argued where this switch should take place. In
  406. * some drivers, like appletouch, the switch is made during
  407. * probe. However, the hid module may also alter the state of the
  408. * device, resulting in trackpad malfunction under certain
  409. * circumstances. To get around this problem, there is at least one
  410. * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
  411. * recieve a reset_resume request rather than the normal resume.
  412. * Since the implementation of reset_resume is equal to mode switch
  413. * plus start_traffic, it seems easier to always do the switch when
  414. * starting traffic on the device.
  415. */
  416. static int bcm5974_start_traffic(struct bcm5974 *dev)
  417. {
  418. if (bcm5974_wellspring_mode(dev)) {
  419. dprintk(1, "bcm5974: mode switch failed\n");
  420. goto error;
  421. }
  422. if (usb_submit_urb(dev->bt_urb, GFP_KERNEL))
  423. goto error;
  424. if (usb_submit_urb(dev->tp_urb, GFP_KERNEL))
  425. goto err_kill_bt;
  426. return 0;
  427. err_kill_bt:
  428. usb_kill_urb(dev->bt_urb);
  429. error:
  430. return -EIO;
  431. }
  432. static void bcm5974_pause_traffic(struct bcm5974 *dev)
  433. {
  434. usb_kill_urb(dev->tp_urb);
  435. usb_kill_urb(dev->bt_urb);
  436. }
  437. /*
  438. * The code below implements open/close and manual suspend/resume.
  439. * All functions may be called in random order.
  440. *
  441. * Opening a suspended device fails with EACCES - permission denied.
  442. *
  443. * Failing a resume leaves the device resumed but closed.
  444. */
  445. static int bcm5974_open(struct input_dev *input)
  446. {
  447. struct bcm5974 *dev = input_get_drvdata(input);
  448. int error;
  449. error = usb_autopm_get_interface(dev->intf);
  450. if (error)
  451. return error;
  452. mutex_lock(&dev->pm_mutex);
  453. error = bcm5974_start_traffic(dev);
  454. if (!error)
  455. dev->opened = 1;
  456. mutex_unlock(&dev->pm_mutex);
  457. if (error)
  458. usb_autopm_put_interface(dev->intf);
  459. return error;
  460. }
  461. static void bcm5974_close(struct input_dev *input)
  462. {
  463. struct bcm5974 *dev = input_get_drvdata(input);
  464. mutex_lock(&dev->pm_mutex);
  465. bcm5974_pause_traffic(dev);
  466. dev->opened = 0;
  467. mutex_unlock(&dev->pm_mutex);
  468. usb_autopm_put_interface(dev->intf);
  469. }
  470. static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
  471. {
  472. struct bcm5974 *dev = usb_get_intfdata(iface);
  473. mutex_lock(&dev->pm_mutex);
  474. if (dev->opened)
  475. bcm5974_pause_traffic(dev);
  476. mutex_unlock(&dev->pm_mutex);
  477. return 0;
  478. }
  479. static int bcm5974_resume(struct usb_interface *iface)
  480. {
  481. struct bcm5974 *dev = usb_get_intfdata(iface);
  482. int error = 0;
  483. mutex_lock(&dev->pm_mutex);
  484. if (dev->opened)
  485. error = bcm5974_start_traffic(dev);
  486. mutex_unlock(&dev->pm_mutex);
  487. return error;
  488. }
  489. static int bcm5974_probe(struct usb_interface *iface,
  490. const struct usb_device_id *id)
  491. {
  492. struct usb_device *udev = interface_to_usbdev(iface);
  493. const struct bcm5974_config *cfg;
  494. struct bcm5974 *dev;
  495. struct input_dev *input_dev;
  496. int error = -ENOMEM;
  497. /* find the product index */
  498. cfg = bcm5974_get_config(udev);
  499. /* allocate memory for our device state and initialize it */
  500. dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
  501. input_dev = input_allocate_device();
  502. if (!dev || !input_dev) {
  503. err("bcm5974: out of memory");
  504. goto err_free_devs;
  505. }
  506. dev->udev = udev;
  507. dev->intf = iface;
  508. dev->input = input_dev;
  509. dev->cfg = *cfg;
  510. mutex_init(&dev->pm_mutex);
  511. /* setup urbs */
  512. dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
  513. if (!dev->bt_urb)
  514. goto err_free_devs;
  515. dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
  516. if (!dev->tp_urb)
  517. goto err_free_bt_urb;
  518. dev->bt_data = usb_buffer_alloc(dev->udev,
  519. dev->cfg.bt_datalen, GFP_KERNEL,
  520. &dev->bt_urb->transfer_dma);
  521. if (!dev->bt_data)
  522. goto err_free_urb;
  523. dev->tp_data = usb_buffer_alloc(dev->udev,
  524. dev->cfg.tp_datalen, GFP_KERNEL,
  525. &dev->tp_urb->transfer_dma);
  526. if (!dev->tp_data)
  527. goto err_free_bt_buffer;
  528. usb_fill_int_urb(dev->bt_urb, udev,
  529. usb_rcvintpipe(udev, cfg->bt_ep),
  530. dev->bt_data, dev->cfg.bt_datalen,
  531. bcm5974_irq_button, dev, 1);
  532. usb_fill_int_urb(dev->tp_urb, udev,
  533. usb_rcvintpipe(udev, cfg->tp_ep),
  534. dev->tp_data, dev->cfg.tp_datalen,
  535. bcm5974_irq_trackpad, dev, 1);
  536. /* create bcm5974 device */
  537. usb_make_path(udev, dev->phys, sizeof(dev->phys));
  538. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  539. input_dev->name = "bcm5974";
  540. input_dev->phys = dev->phys;
  541. usb_to_input_id(dev->udev, &input_dev->id);
  542. input_dev->dev.parent = &iface->dev;
  543. input_set_drvdata(input_dev, dev);
  544. input_dev->open = bcm5974_open;
  545. input_dev->close = bcm5974_close;
  546. setup_events_to_report(input_dev, cfg);
  547. error = input_register_device(dev->input);
  548. if (error)
  549. goto err_free_buffer;
  550. /* save our data pointer in this interface device */
  551. usb_set_intfdata(iface, dev);
  552. return 0;
  553. err_free_buffer:
  554. usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
  555. dev->tp_data, dev->tp_urb->transfer_dma);
  556. err_free_bt_buffer:
  557. usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
  558. dev->bt_data, dev->bt_urb->transfer_dma);
  559. err_free_urb:
  560. usb_free_urb(dev->tp_urb);
  561. err_free_bt_urb:
  562. usb_free_urb(dev->bt_urb);
  563. err_free_devs:
  564. usb_set_intfdata(iface, NULL);
  565. input_free_device(input_dev);
  566. kfree(dev);
  567. return error;
  568. }
  569. static void bcm5974_disconnect(struct usb_interface *iface)
  570. {
  571. struct bcm5974 *dev = usb_get_intfdata(iface);
  572. usb_set_intfdata(iface, NULL);
  573. input_unregister_device(dev->input);
  574. usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
  575. dev->tp_data, dev->tp_urb->transfer_dma);
  576. usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
  577. dev->bt_data, dev->bt_urb->transfer_dma);
  578. usb_free_urb(dev->tp_urb);
  579. usb_free_urb(dev->bt_urb);
  580. kfree(dev);
  581. }
  582. static struct usb_driver bcm5974_driver = {
  583. .name = "bcm5974",
  584. .probe = bcm5974_probe,
  585. .disconnect = bcm5974_disconnect,
  586. .suspend = bcm5974_suspend,
  587. .resume = bcm5974_resume,
  588. .reset_resume = bcm5974_resume,
  589. .id_table = bcm5974_table,
  590. .supports_autosuspend = 1,
  591. };
  592. static int __init bcm5974_init(void)
  593. {
  594. return usb_register(&bcm5974_driver);
  595. }
  596. static void __exit bcm5974_exit(void)
  597. {
  598. usb_deregister(&bcm5974_driver);
  599. }
  600. module_init(bcm5974_init);
  601. module_exit(bcm5974_exit);