bcm5974.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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; /* left/right origin? */
  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 input_dev *input; /* input dev */
  139. struct bcm5974_config cfg; /* device configuration */
  140. struct mutex pm_mutex; /* serialize access to open/suspend */
  141. int opened; /* 1: opened, 0: closed */
  142. struct urb *bt_urb; /* button usb request block */
  143. struct bt_data *bt_data; /* button transferred data */
  144. struct urb *tp_urb; /* trackpad usb request block */
  145. struct tp_data *tp_data; /* trackpad transferred data */
  146. };
  147. /* logical dimensions */
  148. #define DIM_PRESSURE 256 /* maximum finger pressure */
  149. #define DIM_WIDTH 16 /* maximum finger width */
  150. #define DIM_X 1280 /* maximum trackpad x value */
  151. #define DIM_Y 800 /* maximum trackpad y value */
  152. /* logical signal quality */
  153. #define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
  154. #define SN_WIDTH 100 /* width signal-to-noise ratio */
  155. #define SN_COORD 250 /* coordinate signal-to-noise ratio */
  156. /* device constants */
  157. static const struct bcm5974_config bcm5974_config_table[] = {
  158. {
  159. USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
  160. USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
  161. USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
  162. 0x84, sizeof(struct bt_data),
  163. 0x81, sizeof(struct tp_data),
  164. { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
  165. { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
  166. { DIM_X, DIM_X / SN_COORD, -4824, 5342 },
  167. { DIM_Y, DIM_Y / SN_COORD, -172, 5820 }
  168. },
  169. {
  170. USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
  171. USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
  172. USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
  173. 0x84, sizeof(struct bt_data),
  174. 0x81, sizeof(struct tp_data),
  175. { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
  176. { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
  177. { DIM_X, DIM_X / SN_COORD, -4824, 4824 },
  178. { DIM_Y, DIM_Y / SN_COORD, -172, 4290 }
  179. },
  180. {}
  181. };
  182. /* return the device-specific configuration by device */
  183. static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
  184. {
  185. u16 id = le16_to_cpu(udev->descriptor.idProduct);
  186. const struct bcm5974_config *cfg;
  187. for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
  188. if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
  189. return cfg;
  190. return bcm5974_config_table;
  191. }
  192. /* convert 16-bit little endian to signed integer */
  193. static inline int raw2int(__le16 x)
  194. {
  195. return (signed short)le16_to_cpu(x);
  196. }
  197. /* scale device data to logical dimensions (asserts devmin < devmax) */
  198. static inline int int2scale(const struct bcm5974_param *p, int x)
  199. {
  200. return x * p->dim / (p->devmax - p->devmin);
  201. }
  202. /* all logical value ranges are [0,dim). */
  203. static inline int int2bound(const struct bcm5974_param *p, int x)
  204. {
  205. int s = int2scale(p, x);
  206. return clamp_val(s, 0, p->dim - 1);
  207. }
  208. /* setup which logical events to report */
  209. static void setup_events_to_report(struct input_dev *input_dev,
  210. const struct bcm5974_config *cfg)
  211. {
  212. __set_bit(EV_ABS, input_dev->evbit);
  213. input_set_abs_params(input_dev, ABS_PRESSURE,
  214. 0, cfg->p.dim, cfg->p.fuzz, 0);
  215. input_set_abs_params(input_dev, ABS_TOOL_WIDTH,
  216. 0, cfg->w.dim, cfg->w.fuzz, 0);
  217. input_set_abs_params(input_dev, ABS_X,
  218. 0, cfg->x.dim, cfg->x.fuzz, 0);
  219. input_set_abs_params(input_dev, ABS_Y,
  220. 0, cfg->y.dim, cfg->y.fuzz, 0);
  221. __set_bit(EV_KEY, input_dev->evbit);
  222. __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
  223. __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
  224. __set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
  225. __set_bit(BTN_LEFT, input_dev->keybit);
  226. }
  227. /* report button data as logical button state */
  228. static int report_bt_state(struct bcm5974 *dev, int size)
  229. {
  230. if (size != sizeof(struct bt_data))
  231. return -EIO;
  232. input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
  233. input_sync(dev->input);
  234. return 0;
  235. }
  236. /* report trackpad data as logical trackpad state */
  237. static int report_tp_state(struct bcm5974 *dev, int size)
  238. {
  239. const struct bcm5974_config *c = &dev->cfg;
  240. const struct tp_finger *f = dev->tp_data->finger;
  241. struct input_dev *input = dev->input;
  242. const int fingers = (size - 26) / 28;
  243. int p = 0, w, x, y, n = 0;
  244. if (size < 26 || (size - 26) % 28 != 0)
  245. return -EIO;
  246. if (fingers) {
  247. p = raw2int(f->force_major);
  248. w = raw2int(f->size_major);
  249. x = raw2int(f->abs_x);
  250. y = raw2int(f->abs_y);
  251. n = p > 0 ? fingers : 0;
  252. dprintk(9,
  253. "bcm5974: p: %+05d w: %+05d x: %+05d y: %+05d n: %d\n",
  254. p, w, x, y, n);
  255. input_report_abs(input, ABS_TOOL_WIDTH, int2bound(&c->w, w));
  256. input_report_abs(input, ABS_X, int2bound(&c->x, x - c->x.devmin));
  257. input_report_abs(input, ABS_Y, int2bound(&c->y, c->y.devmax - y));
  258. }
  259. input_report_abs(input, ABS_PRESSURE, int2bound(&c->p, p));
  260. input_report_key(input, BTN_TOOL_FINGER, n == 1);
  261. input_report_key(input, BTN_TOOL_DOUBLETAP, n == 2);
  262. input_report_key(input, BTN_TOOL_TRIPLETAP, n > 2);
  263. input_sync(input);
  264. return 0;
  265. }
  266. /* Wellspring initialization constants */
  267. #define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID 1
  268. #define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID 9
  269. #define BCM5974_WELLSPRING_MODE_REQUEST_VALUE 0x300
  270. #define BCM5974_WELLSPRING_MODE_REQUEST_INDEX 0
  271. #define BCM5974_WELLSPRING_MODE_VENDOR_VALUE 0x01
  272. static int bcm5974_wellspring_mode(struct bcm5974 *dev)
  273. {
  274. char *data = kmalloc(8, GFP_KERNEL);
  275. int retval = 0, size;
  276. if (!data) {
  277. err("bcm5974: out of memory");
  278. retval = -ENOMEM;
  279. goto out;
  280. }
  281. /* read configuration */
  282. size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  283. BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
  284. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  285. BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
  286. BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
  287. if (size != 8) {
  288. err("bcm5974: could not read from device");
  289. retval = -EIO;
  290. goto out;
  291. }
  292. /* apply the mode switch */
  293. data[0] = BCM5974_WELLSPRING_MODE_VENDOR_VALUE;
  294. /* write configuration */
  295. size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  296. BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
  297. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  298. BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
  299. BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
  300. if (size != 8) {
  301. err("bcm5974: could not write to device");
  302. retval = -EIO;
  303. goto out;
  304. }
  305. dprintk(2, "bcm5974: switched to wellspring mode.\n");
  306. out:
  307. kfree(data);
  308. return retval;
  309. }
  310. static void bcm5974_irq_button(struct urb *urb)
  311. {
  312. struct bcm5974 *dev = urb->context;
  313. int error;
  314. switch (urb->status) {
  315. case 0:
  316. break;
  317. case -EOVERFLOW:
  318. case -ECONNRESET:
  319. case -ENOENT:
  320. case -ESHUTDOWN:
  321. dbg("bcm5974: button urb shutting down: %d", urb->status);
  322. return;
  323. default:
  324. dbg("bcm5974: button urb status: %d", urb->status);
  325. goto exit;
  326. }
  327. if (report_bt_state(dev, dev->bt_urb->actual_length))
  328. dprintk(1, "bcm5974: bad button package, length: %d\n",
  329. dev->bt_urb->actual_length);
  330. exit:
  331. error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
  332. if (error)
  333. err("bcm5974: button urb failed: %d", error);
  334. }
  335. static void bcm5974_irq_trackpad(struct urb *urb)
  336. {
  337. struct bcm5974 *dev = urb->context;
  338. int error;
  339. switch (urb->status) {
  340. case 0:
  341. break;
  342. case -EOVERFLOW:
  343. case -ECONNRESET:
  344. case -ENOENT:
  345. case -ESHUTDOWN:
  346. dbg("bcm5974: trackpad urb shutting down: %d", urb->status);
  347. return;
  348. default:
  349. dbg("bcm5974: trackpad urb status: %d", urb->status);
  350. goto exit;
  351. }
  352. /* control response ignored */
  353. if (dev->tp_urb->actual_length == 2)
  354. goto exit;
  355. if (report_tp_state(dev, dev->tp_urb->actual_length))
  356. dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
  357. dev->tp_urb->actual_length);
  358. exit:
  359. error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
  360. if (error)
  361. err("bcm5974: trackpad urb failed: %d", error);
  362. }
  363. /*
  364. * The Wellspring trackpad, like many recent Apple trackpads, share
  365. * the usb device with the keyboard. Since keyboards are usually
  366. * handled by the HID system, the device ends up being handled by two
  367. * modules. Setting up the device therefore becomes slightly
  368. * complicated. To enable multitouch features, a mode switch is
  369. * required, which is usually applied via the control interface of the
  370. * device. It can be argued where this switch should take place. In
  371. * some drivers, like appletouch, the switch is made during
  372. * probe. However, the hid module may also alter the state of the
  373. * device, resulting in trackpad malfunction under certain
  374. * circumstances. To get around this problem, there is at least one
  375. * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
  376. * recieve a reset_resume request rather than the normal resume.
  377. * Since the implementation of reset_resume is equal to mode switch
  378. * plus start_traffic, it seems easier to always do the switch when
  379. * starting traffic on the device.
  380. */
  381. static int bcm5974_start_traffic(struct bcm5974 *dev)
  382. {
  383. if (bcm5974_wellspring_mode(dev)) {
  384. dprintk(1, "bcm5974: mode switch failed\n");
  385. goto error;
  386. }
  387. if (usb_submit_urb(dev->bt_urb, GFP_KERNEL))
  388. goto error;
  389. if (usb_submit_urb(dev->tp_urb, GFP_KERNEL))
  390. goto err_kill_bt;
  391. return 0;
  392. err_kill_bt:
  393. usb_kill_urb(dev->bt_urb);
  394. error:
  395. return -EIO;
  396. }
  397. static void bcm5974_pause_traffic(struct bcm5974 *dev)
  398. {
  399. usb_kill_urb(dev->tp_urb);
  400. usb_kill_urb(dev->bt_urb);
  401. }
  402. /*
  403. * The code below implements open/close and manual suspend/resume.
  404. * All functions may be called in random order.
  405. *
  406. * Opening a suspended device fails with EACCES - permission denied.
  407. *
  408. * Failing a resume leaves the device resumed but closed.
  409. */
  410. static int bcm5974_open(struct input_dev *input)
  411. {
  412. struct bcm5974 *dev = input_get_drvdata(input);
  413. int error;
  414. mutex_lock(&dev->pm_mutex);
  415. error = bcm5974_start_traffic(dev);
  416. if (!error)
  417. dev->opened = 1;
  418. mutex_unlock(&dev->pm_mutex);
  419. return error;
  420. }
  421. static void bcm5974_close(struct input_dev *input)
  422. {
  423. struct bcm5974 *dev = input_get_drvdata(input);
  424. mutex_lock(&dev->pm_mutex);
  425. bcm5974_pause_traffic(dev);
  426. dev->opened = 0;
  427. mutex_unlock(&dev->pm_mutex);
  428. }
  429. static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
  430. {
  431. struct bcm5974 *dev = usb_get_intfdata(iface);
  432. mutex_lock(&dev->pm_mutex);
  433. if (dev->opened)
  434. bcm5974_pause_traffic(dev);
  435. mutex_unlock(&dev->pm_mutex);
  436. return 0;
  437. }
  438. static int bcm5974_resume(struct usb_interface *iface)
  439. {
  440. struct bcm5974 *dev = usb_get_intfdata(iface);
  441. int error = 0;
  442. mutex_lock(&dev->pm_mutex);
  443. if (dev->opened)
  444. error = bcm5974_start_traffic(dev);
  445. mutex_unlock(&dev->pm_mutex);
  446. return error;
  447. }
  448. static int bcm5974_probe(struct usb_interface *iface,
  449. const struct usb_device_id *id)
  450. {
  451. struct usb_device *udev = interface_to_usbdev(iface);
  452. const struct bcm5974_config *cfg;
  453. struct bcm5974 *dev;
  454. struct input_dev *input_dev;
  455. int error = -ENOMEM;
  456. /* find the product index */
  457. cfg = bcm5974_get_config(udev);
  458. /* allocate memory for our device state and initialize it */
  459. dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
  460. input_dev = input_allocate_device();
  461. if (!dev || !input_dev) {
  462. err("bcm5974: out of memory");
  463. goto err_free_devs;
  464. }
  465. dev->udev = udev;
  466. dev->input = input_dev;
  467. dev->cfg = *cfg;
  468. mutex_init(&dev->pm_mutex);
  469. /* setup urbs */
  470. dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
  471. if (!dev->bt_urb)
  472. goto err_free_devs;
  473. dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
  474. if (!dev->tp_urb)
  475. goto err_free_bt_urb;
  476. dev->bt_data = usb_buffer_alloc(dev->udev,
  477. dev->cfg.bt_datalen, GFP_KERNEL,
  478. &dev->bt_urb->transfer_dma);
  479. if (!dev->bt_data)
  480. goto err_free_urb;
  481. dev->tp_data = usb_buffer_alloc(dev->udev,
  482. dev->cfg.tp_datalen, GFP_KERNEL,
  483. &dev->tp_urb->transfer_dma);
  484. if (!dev->tp_data)
  485. goto err_free_bt_buffer;
  486. usb_fill_int_urb(dev->bt_urb, udev,
  487. usb_rcvintpipe(udev, cfg->bt_ep),
  488. dev->bt_data, dev->cfg.bt_datalen,
  489. bcm5974_irq_button, dev, 1);
  490. usb_fill_int_urb(dev->tp_urb, udev,
  491. usb_rcvintpipe(udev, cfg->tp_ep),
  492. dev->tp_data, dev->cfg.tp_datalen,
  493. bcm5974_irq_trackpad, dev, 1);
  494. /* create bcm5974 device */
  495. usb_make_path(udev, dev->phys, sizeof(dev->phys));
  496. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  497. input_dev->name = "bcm5974";
  498. input_dev->phys = dev->phys;
  499. usb_to_input_id(dev->udev, &input_dev->id);
  500. input_dev->dev.parent = &iface->dev;
  501. input_set_drvdata(input_dev, dev);
  502. input_dev->open = bcm5974_open;
  503. input_dev->close = bcm5974_close;
  504. setup_events_to_report(input_dev, cfg);
  505. error = input_register_device(dev->input);
  506. if (error)
  507. goto err_free_buffer;
  508. /* save our data pointer in this interface device */
  509. usb_set_intfdata(iface, dev);
  510. return 0;
  511. err_free_buffer:
  512. usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
  513. dev->tp_data, dev->tp_urb->transfer_dma);
  514. err_free_bt_buffer:
  515. usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
  516. dev->bt_data, dev->bt_urb->transfer_dma);
  517. err_free_urb:
  518. usb_free_urb(dev->tp_urb);
  519. err_free_bt_urb:
  520. usb_free_urb(dev->bt_urb);
  521. err_free_devs:
  522. usb_set_intfdata(iface, NULL);
  523. input_free_device(input_dev);
  524. kfree(dev);
  525. return error;
  526. }
  527. static void bcm5974_disconnect(struct usb_interface *iface)
  528. {
  529. struct bcm5974 *dev = usb_get_intfdata(iface);
  530. usb_set_intfdata(iface, NULL);
  531. input_unregister_device(dev->input);
  532. usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
  533. dev->tp_data, dev->tp_urb->transfer_dma);
  534. usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
  535. dev->bt_data, dev->bt_urb->transfer_dma);
  536. usb_free_urb(dev->tp_urb);
  537. usb_free_urb(dev->bt_urb);
  538. kfree(dev);
  539. }
  540. static struct usb_driver bcm5974_driver = {
  541. .name = "bcm5974",
  542. .probe = bcm5974_probe,
  543. .disconnect = bcm5974_disconnect,
  544. .suspend = bcm5974_suspend,
  545. .resume = bcm5974_resume,
  546. .reset_resume = bcm5974_resume,
  547. .id_table = bcm5974_table,
  548. };
  549. static int __init bcm5974_init(void)
  550. {
  551. return usb_register(&bcm5974_driver);
  552. }
  553. static void __exit bcm5974_exit(void)
  554. {
  555. usb_deregister(&bcm5974_driver);
  556. }
  557. module_init(bcm5974_init);
  558. module_exit(bcm5974_exit);