blob: 2c66913cf5a2457c2f2b1c9b01ce211e30814fa3 [file] [log] [blame]
Jan Steinhoff8491ee12012-02-03 00:21:31 -08001/*
2 * USB Synaptics device driver
3 *
4 * Copyright (c) 2002 Rob Miller (rob@inpharmatica . co . uk)
5 * Copyright (c) 2003 Ron Lee (ron@debian.org)
6 * cPad driver for kernel 2.4
7 *
8 * Copyright (c) 2004 Jan Steinhoff (cpad@jan-steinhoff . de)
9 * Copyright (c) 2004 Ron Lee (ron@debian.org)
10 * rewritten for kernel 2.6
11 *
12 * cPad display character device part is not included. It can be found at
13 * http://jan-steinhoff.de/linux/synaptics-usb.html
14 *
15 * Bases on: usb_skeleton.c v2.2 by Greg Kroah-Hartman
16 * drivers/hid/usbhid/usbmouse.c by Vojtech Pavlik
17 * drivers/input/mouse/synaptics.c by Peter Osterlund
18 *
19 * This program is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU General Public License as published by the Free
21 * Software Foundation; either version 2 of the License, or (at your option)
22 * any later version.
23 *
24 * Trademarks are the property of their respective owners.
25 */
26
27/*
28 * There are three different types of Synaptics USB devices: Touchpads,
29 * touchsticks (or trackpoints), and touchscreens. Touchpads are well supported
30 * by this driver, touchstick support has not been tested much yet, and
31 * touchscreens have not been tested at all.
32 *
33 * Up to three alternate settings are possible:
34 * setting 0: one int endpoint for relative movement (used by usbhid.ko)
35 * setting 1: one int endpoint for absolute finger position
36 * setting 2 (cPad only): one int endpoint for absolute finger position and
37 * two bulk endpoints for the display (in/out)
38 * This driver uses setting 1.
39 */
40
41#include <linux/kernel.h>
Jan Steinhoff8491ee12012-02-03 00:21:31 -080042#include <linux/slab.h>
43#include <linux/module.h>
44#include <linux/moduleparam.h>
45#include <linux/usb.h>
46#include <linux/input.h>
47#include <linux/usb/input.h>
48
49#define USB_VENDOR_ID_SYNAPTICS 0x06cb
50#define USB_DEVICE_ID_SYNAPTICS_TP 0x0001 /* Synaptics USB TouchPad */
51#define USB_DEVICE_ID_SYNAPTICS_INT_TP 0x0002 /* Integrated USB TouchPad */
52#define USB_DEVICE_ID_SYNAPTICS_CPAD 0x0003 /* Synaptics cPad */
53#define USB_DEVICE_ID_SYNAPTICS_TS 0x0006 /* Synaptics TouchScreen */
54#define USB_DEVICE_ID_SYNAPTICS_STICK 0x0007 /* Synaptics USB Styk */
55#define USB_DEVICE_ID_SYNAPTICS_WP 0x0008 /* Synaptics USB WheelPad */
56#define USB_DEVICE_ID_SYNAPTICS_COMP_TP 0x0009 /* Composite USB TouchPad */
57#define USB_DEVICE_ID_SYNAPTICS_WTP 0x0010 /* Wireless TouchPad */
58#define USB_DEVICE_ID_SYNAPTICS_DPAD 0x0013 /* DisplayPad */
59
60#define SYNUSB_TOUCHPAD (1 << 0)
61#define SYNUSB_STICK (1 << 1)
62#define SYNUSB_TOUCHSCREEN (1 << 2)
63#define SYNUSB_AUXDISPLAY (1 << 3) /* For cPad */
64#define SYNUSB_COMBO (1 << 4) /* Composite device (TP + stick) */
65#define SYNUSB_IO_ALWAYS (1 << 5)
66
67#define USB_DEVICE_SYNAPTICS(prod, kind) \
68 USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, \
69 USB_DEVICE_ID_SYNAPTICS_##prod), \
70 .driver_info = (kind),
71
72#define SYNUSB_RECV_SIZE 8
73
74#define XMIN_NOMINAL 1472
75#define XMAX_NOMINAL 5472
76#define YMIN_NOMINAL 1408
77#define YMAX_NOMINAL 4448
78
79struct synusb {
80 struct usb_device *udev;
81 struct usb_interface *intf;
82 struct urb *urb;
83 unsigned char *data;
84
Marcus Folkessonb8a7cc42018-03-17 10:49:46 -070085 /* serialize access to open/suspend */
86 struct mutex pm_mutex;
87
Jan Steinhoff8491ee12012-02-03 00:21:31 -080088 /* input device related data structures */
89 struct input_dev *input;
90 char name[128];
91 char phys[64];
92
93 /* characteristics of the device */
94 unsigned long flags;
95};
96
97static void synusb_report_buttons(struct synusb *synusb)
98{
99 struct input_dev *input_dev = synusb->input;
100
101 input_report_key(input_dev, BTN_LEFT, synusb->data[1] & 0x04);
102 input_report_key(input_dev, BTN_RIGHT, synusb->data[1] & 0x01);
103 input_report_key(input_dev, BTN_MIDDLE, synusb->data[1] & 0x02);
104}
105
106static void synusb_report_stick(struct synusb *synusb)
107{
108 struct input_dev *input_dev = synusb->input;
109 int x, y;
110 unsigned int pressure;
111
112 pressure = synusb->data[6];
113 x = (s16)(be16_to_cpup((__be16 *)&synusb->data[2]) << 3) >> 7;
114 y = (s16)(be16_to_cpup((__be16 *)&synusb->data[4]) << 3) >> 7;
115
116 if (pressure > 0) {
117 input_report_rel(input_dev, REL_X, x);
118 input_report_rel(input_dev, REL_Y, -y);
119 }
120
121 input_report_abs(input_dev, ABS_PRESSURE, pressure);
122
123 synusb_report_buttons(synusb);
124
125 input_sync(input_dev);
126}
127
128static void synusb_report_touchpad(struct synusb *synusb)
129{
130 struct input_dev *input_dev = synusb->input;
131 unsigned int num_fingers, tool_width;
132 unsigned int x, y;
133 unsigned int pressure, w;
134
135 pressure = synusb->data[6];
136 x = be16_to_cpup((__be16 *)&synusb->data[2]);
137 y = be16_to_cpup((__be16 *)&synusb->data[4]);
138 w = synusb->data[0] & 0x0f;
139
140 if (pressure > 0) {
141 num_fingers = 1;
142 tool_width = 5;
143 switch (w) {
144 case 0 ... 1:
145 num_fingers = 2 + w;
146 break;
147
148 case 2: /* pen, pretend its a finger */
149 break;
150
151 case 4 ... 15:
152 tool_width = w;
153 break;
154 }
155 } else {
156 num_fingers = 0;
157 tool_width = 0;
158 }
159
160 /*
161 * Post events
162 * BTN_TOUCH has to be first as mousedev relies on it when doing
163 * absolute -> relative conversion
164 */
165
166 if (pressure > 30)
167 input_report_key(input_dev, BTN_TOUCH, 1);
168 if (pressure < 25)
169 input_report_key(input_dev, BTN_TOUCH, 0);
170
171 if (num_fingers > 0) {
172 input_report_abs(input_dev, ABS_X, x);
173 input_report_abs(input_dev, ABS_Y,
174 YMAX_NOMINAL + YMIN_NOMINAL - y);
175 }
176
177 input_report_abs(input_dev, ABS_PRESSURE, pressure);
178 input_report_abs(input_dev, ABS_TOOL_WIDTH, tool_width);
179
180 input_report_key(input_dev, BTN_TOOL_FINGER, num_fingers == 1);
181 input_report_key(input_dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
182 input_report_key(input_dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
183
184 synusb_report_buttons(synusb);
185 if (synusb->flags & SYNUSB_AUXDISPLAY)
186 input_report_key(input_dev, BTN_MIDDLE, synusb->data[1] & 0x08);
187
188 input_sync(input_dev);
189}
190
191static void synusb_irq(struct urb *urb)
192{
193 struct synusb *synusb = urb->context;
194 int error;
195
196 /* Check our status in case we need to bail out early. */
197 switch (urb->status) {
198 case 0:
199 usb_mark_last_busy(synusb->udev);
200 break;
201
202 /* Device went away so don't keep trying to read from it. */
203 case -ECONNRESET:
204 case -ENOENT:
205 case -ESHUTDOWN:
206 return;
207
208 default:
209 goto resubmit;
210 break;
211 }
212
213 if (synusb->flags & SYNUSB_STICK)
214 synusb_report_stick(synusb);
215 else
216 synusb_report_touchpad(synusb);
217
218resubmit:
219 error = usb_submit_urb(urb, GFP_ATOMIC);
220 if (error && error != -EPERM)
221 dev_err(&synusb->intf->dev,
222 "%s - usb_submit_urb failed with result: %d",
223 __func__, error);
224}
225
226static struct usb_endpoint_descriptor *
227synusb_get_in_endpoint(struct usb_host_interface *iface)
228{
229
230 struct usb_endpoint_descriptor *endpoint;
231 int i;
232
233 for (i = 0; i < iface->desc.bNumEndpoints; ++i) {
234 endpoint = &iface->endpoint[i].desc;
235
236 if (usb_endpoint_is_int_in(endpoint)) {
237 /* we found our interrupt in endpoint */
238 return endpoint;
239 }
240 }
241
242 return NULL;
243}
244
245static int synusb_open(struct input_dev *dev)
246{
247 struct synusb *synusb = input_get_drvdata(dev);
248 int retval;
249
250 retval = usb_autopm_get_interface(synusb->intf);
251 if (retval) {
252 dev_err(&synusb->intf->dev,
253 "%s - usb_autopm_get_interface failed, error: %d\n",
254 __func__, retval);
255 return retval;
256 }
257
Marcus Folkessonb8a7cc42018-03-17 10:49:46 -0700258 mutex_lock(&synusb->pm_mutex);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800259 retval = usb_submit_urb(synusb->urb, GFP_KERNEL);
260 if (retval) {
261 dev_err(&synusb->intf->dev,
262 "%s - usb_submit_urb failed, error: %d\n",
263 __func__, retval);
264 retval = -EIO;
265 goto out;
266 }
267
268 synusb->intf->needs_remote_wakeup = 1;
269
270out:
Marcus Folkessonb8a7cc42018-03-17 10:49:46 -0700271 mutex_unlock(&synusb->pm_mutex);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800272 usb_autopm_put_interface(synusb->intf);
273 return retval;
274}
275
276static void synusb_close(struct input_dev *dev)
277{
278 struct synusb *synusb = input_get_drvdata(dev);
279 int autopm_error;
280
281 autopm_error = usb_autopm_get_interface(synusb->intf);
282
Marcus Folkessonb8a7cc42018-03-17 10:49:46 -0700283 mutex_lock(&synusb->pm_mutex);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800284 usb_kill_urb(synusb->urb);
285 synusb->intf->needs_remote_wakeup = 0;
Marcus Folkessonb8a7cc42018-03-17 10:49:46 -0700286 mutex_unlock(&synusb->pm_mutex);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800287
288 if (!autopm_error)
289 usb_autopm_put_interface(synusb->intf);
290}
291
292static int synusb_probe(struct usb_interface *intf,
293 const struct usb_device_id *id)
294{
295 struct usb_device *udev = interface_to_usbdev(intf);
296 struct usb_endpoint_descriptor *ep;
297 struct synusb *synusb;
298 struct input_dev *input_dev;
299 unsigned int intf_num = intf->cur_altsetting->desc.bInterfaceNumber;
300 unsigned int altsetting = min(intf->num_altsetting, 1U);
301 int error;
302
303 error = usb_set_interface(udev, intf_num, altsetting);
304 if (error) {
305 dev_err(&udev->dev,
306 "Can not set alternate setting to %i, error: %i",
307 altsetting, error);
308 return error;
309 }
310
311 ep = synusb_get_in_endpoint(intf->cur_altsetting);
312 if (!ep)
313 return -ENODEV;
314
315 synusb = kzalloc(sizeof(*synusb), GFP_KERNEL);
316 input_dev = input_allocate_device();
317 if (!synusb || !input_dev) {
318 error = -ENOMEM;
319 goto err_free_mem;
320 }
321
322 synusb->udev = udev;
323 synusb->intf = intf;
324 synusb->input = input_dev;
Marcus Folkessonb8a7cc42018-03-17 10:49:46 -0700325 mutex_init(&synusb->pm_mutex);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800326
327 synusb->flags = id->driver_info;
328 if (synusb->flags & SYNUSB_COMBO) {
329 /*
330 * This is a combo device, we need to set proper
331 * capability, depending on the interface.
332 */
333 synusb->flags |= intf_num == 1 ?
334 SYNUSB_STICK : SYNUSB_TOUCHPAD;
335 }
336
337 synusb->urb = usb_alloc_urb(0, GFP_KERNEL);
338 if (!synusb->urb) {
339 error = -ENOMEM;
340 goto err_free_mem;
341 }
342
343 synusb->data = usb_alloc_coherent(udev, SYNUSB_RECV_SIZE, GFP_KERNEL,
344 &synusb->urb->transfer_dma);
345 if (!synusb->data) {
346 error = -ENOMEM;
347 goto err_free_urb;
348 }
349
350 usb_fill_int_urb(synusb->urb, udev,
351 usb_rcvintpipe(udev, ep->bEndpointAddress),
352 synusb->data, SYNUSB_RECV_SIZE,
353 synusb_irq, synusb,
354 ep->bInterval);
355 synusb->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
356
357 if (udev->manufacturer)
358 strlcpy(synusb->name, udev->manufacturer,
359 sizeof(synusb->name));
360
361 if (udev->product) {
362 if (udev->manufacturer)
363 strlcat(synusb->name, " ", sizeof(synusb->name));
364 strlcat(synusb->name, udev->product, sizeof(synusb->name));
365 }
366
367 if (!strlen(synusb->name))
368 snprintf(synusb->name, sizeof(synusb->name),
369 "USB Synaptics Device %04x:%04x",
370 le16_to_cpu(udev->descriptor.idVendor),
371 le16_to_cpu(udev->descriptor.idProduct));
372
373 if (synusb->flags & SYNUSB_STICK)
Bob Rossf2bb26b2012-07-06 11:34:47 -0700374 strlcat(synusb->name, " (Stick)", sizeof(synusb->name));
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800375
376 usb_make_path(udev, synusb->phys, sizeof(synusb->phys));
377 strlcat(synusb->phys, "/input0", sizeof(synusb->phys));
378
379 input_dev->name = synusb->name;
380 input_dev->phys = synusb->phys;
381 usb_to_input_id(udev, &input_dev->id);
382 input_dev->dev.parent = &synusb->intf->dev;
383
384 if (!(synusb->flags & SYNUSB_IO_ALWAYS)) {
385 input_dev->open = synusb_open;
386 input_dev->close = synusb_close;
387 }
388
389 input_set_drvdata(input_dev, synusb);
390
391 __set_bit(EV_ABS, input_dev->evbit);
392 __set_bit(EV_KEY, input_dev->evbit);
393
394 if (synusb->flags & SYNUSB_STICK) {
395 __set_bit(EV_REL, input_dev->evbit);
396 __set_bit(REL_X, input_dev->relbit);
397 __set_bit(REL_Y, input_dev->relbit);
Hans de Goede76113922014-09-08 14:42:12 -0700398 __set_bit(INPUT_PROP_POINTING_STICK, input_dev->propbit);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800399 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 127, 0, 0);
400 } else {
401 input_set_abs_params(input_dev, ABS_X,
402 XMIN_NOMINAL, XMAX_NOMINAL, 0, 0);
403 input_set_abs_params(input_dev, ABS_Y,
404 YMIN_NOMINAL, YMAX_NOMINAL, 0, 0);
405 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
406 input_set_abs_params(input_dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
407 __set_bit(BTN_TOUCH, input_dev->keybit);
408 __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
409 __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
410 __set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
411 }
412
Hans de Goede01d4cd52014-09-08 14:44:05 -0700413 if (synusb->flags & SYNUSB_TOUCHSCREEN)
414 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
415 else
416 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
417
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800418 __set_bit(BTN_LEFT, input_dev->keybit);
419 __set_bit(BTN_RIGHT, input_dev->keybit);
420 __set_bit(BTN_MIDDLE, input_dev->keybit);
421
422 usb_set_intfdata(intf, synusb);
423
424 if (synusb->flags & SYNUSB_IO_ALWAYS) {
425 error = synusb_open(input_dev);
426 if (error)
427 goto err_free_dma;
428 }
429
430 error = input_register_device(input_dev);
431 if (error) {
432 dev_err(&udev->dev,
433 "Failed to register input device, error %d\n",
434 error);
435 goto err_stop_io;
436 }
437
438 return 0;
439
440err_stop_io:
441 if (synusb->flags & SYNUSB_IO_ALWAYS)
442 synusb_close(synusb->input);
443err_free_dma:
444 usb_free_coherent(udev, SYNUSB_RECV_SIZE, synusb->data,
445 synusb->urb->transfer_dma);
446err_free_urb:
447 usb_free_urb(synusb->urb);
448err_free_mem:
449 input_free_device(input_dev);
450 kfree(synusb);
451 usb_set_intfdata(intf, NULL);
452
453 return error;
454}
455
456static void synusb_disconnect(struct usb_interface *intf)
457{
458 struct synusb *synusb = usb_get_intfdata(intf);
459 struct usb_device *udev = interface_to_usbdev(intf);
460
461 if (synusb->flags & SYNUSB_IO_ALWAYS)
462 synusb_close(synusb->input);
463
464 input_unregister_device(synusb->input);
465
466 usb_free_coherent(udev, SYNUSB_RECV_SIZE, synusb->data,
467 synusb->urb->transfer_dma);
468 usb_free_urb(synusb->urb);
469 kfree(synusb);
470
471 usb_set_intfdata(intf, NULL);
472}
473
474static int synusb_suspend(struct usb_interface *intf, pm_message_t message)
475{
476 struct synusb *synusb = usb_get_intfdata(intf);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800477
Marcus Folkessonb8a7cc42018-03-17 10:49:46 -0700478 mutex_lock(&synusb->pm_mutex);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800479 usb_kill_urb(synusb->urb);
Marcus Folkessonb8a7cc42018-03-17 10:49:46 -0700480 mutex_unlock(&synusb->pm_mutex);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800481
482 return 0;
483}
484
485static int synusb_resume(struct usb_interface *intf)
486{
487 struct synusb *synusb = usb_get_intfdata(intf);
488 struct input_dev *input_dev = synusb->input;
489 int retval = 0;
490
Marcus Folkessonb8a7cc42018-03-17 10:49:46 -0700491 mutex_lock(&synusb->pm_mutex);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800492
493 if ((input_dev->users || (synusb->flags & SYNUSB_IO_ALWAYS)) &&
494 usb_submit_urb(synusb->urb, GFP_NOIO) < 0) {
495 retval = -EIO;
496 }
497
Marcus Folkessonb8a7cc42018-03-17 10:49:46 -0700498 mutex_unlock(&synusb->pm_mutex);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800499
500 return retval;
501}
502
503static int synusb_pre_reset(struct usb_interface *intf)
504{
505 struct synusb *synusb = usb_get_intfdata(intf);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800506
Marcus Folkessonb8a7cc42018-03-17 10:49:46 -0700507 mutex_lock(&synusb->pm_mutex);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800508 usb_kill_urb(synusb->urb);
509
510 return 0;
511}
512
513static int synusb_post_reset(struct usb_interface *intf)
514{
515 struct synusb *synusb = usb_get_intfdata(intf);
516 struct input_dev *input_dev = synusb->input;
517 int retval = 0;
518
519 if ((input_dev->users || (synusb->flags & SYNUSB_IO_ALWAYS)) &&
520 usb_submit_urb(synusb->urb, GFP_NOIO) < 0) {
521 retval = -EIO;
522 }
523
Marcus Folkessonb8a7cc42018-03-17 10:49:46 -0700524 mutex_unlock(&synusb->pm_mutex);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800525
526 return retval;
527}
528
529static int synusb_reset_resume(struct usb_interface *intf)
530{
531 return synusb_resume(intf);
532}
533
Arvind Yadav3cafe412017-08-07 19:44:37 -0700534static const struct usb_device_id synusb_idtable[] = {
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800535 { USB_DEVICE_SYNAPTICS(TP, SYNUSB_TOUCHPAD) },
536 { USB_DEVICE_SYNAPTICS(INT_TP, SYNUSB_TOUCHPAD) },
537 { USB_DEVICE_SYNAPTICS(CPAD,
538 SYNUSB_TOUCHPAD | SYNUSB_AUXDISPLAY | SYNUSB_IO_ALWAYS) },
539 { USB_DEVICE_SYNAPTICS(TS, SYNUSB_TOUCHSCREEN) },
540 { USB_DEVICE_SYNAPTICS(STICK, SYNUSB_STICK) },
541 { USB_DEVICE_SYNAPTICS(WP, SYNUSB_TOUCHPAD) },
542 { USB_DEVICE_SYNAPTICS(COMP_TP, SYNUSB_COMBO) },
543 { USB_DEVICE_SYNAPTICS(WTP, SYNUSB_TOUCHPAD) },
544 { USB_DEVICE_SYNAPTICS(DPAD, SYNUSB_TOUCHPAD) },
545 { }
546};
547MODULE_DEVICE_TABLE(usb, synusb_idtable);
548
549static struct usb_driver synusb_driver = {
550 .name = "synaptics_usb",
551 .probe = synusb_probe,
552 .disconnect = synusb_disconnect,
553 .id_table = synusb_idtable,
554 .suspend = synusb_suspend,
555 .resume = synusb_resume,
556 .pre_reset = synusb_pre_reset,
557 .post_reset = synusb_post_reset,
558 .reset_resume = synusb_reset_resume,
559 .supports_autosuspend = 1,
560};
561
Dmitry Torokhov4a533832012-03-16 23:05:44 -0700562module_usb_driver(synusb_driver);
Jan Steinhoff8491ee12012-02-03 00:21:31 -0800563
564MODULE_AUTHOR("Rob Miller <rob@inpharmatica.co.uk>, "
565 "Ron Lee <ron@debian.org>, "
566 "Jan Steinhoff <cpad@jan-steinhoff.de>");
567MODULE_DESCRIPTION("Synaptics USB device driver");
568MODULE_LICENSE("GPL");