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