1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * USB Synaptics device driver
5 * Copyright (c) 2002 Rob Miller (rob@inpharmatica . co . uk)
6 * Copyright (c) 2003 Ron Lee (ron@debian.org)
7 * cPad driver for kernel 2.4
9 * Copyright (c) 2004 Jan Steinhoff (cpad@jan-steinhoff . de)
10 * Copyright (c) 2004 Ron Lee (ron@debian.org)
11 * rewritten for kernel 2.6
13 * cPad display character device part is not included. It can be found at
14 * http://jan-steinhoff.de/linux/synaptics-usb.html
16 * Bases on: usb_skeleton.c v2.2 by Greg Kroah-Hartman
17 * drivers/hid/usbhid/usbmouse.c by Vojtech Pavlik
18 * drivers/input/mouse/synaptics.c by Peter Osterlund
20 * Trademarks are the property of their respective owners.
24 * There are three different types of Synaptics USB devices: Touchpads,
25 * touchsticks (or trackpoints), and touchscreens. Touchpads are well supported
26 * by this driver, touchstick support has not been tested much yet, and
27 * touchscreens have not been tested at all.
29 * Up to three alternate settings are possible:
30 * setting 0: one int endpoint for relative movement (used by usbhid.ko)
31 * setting 1: one int endpoint for absolute finger position
32 * setting 2 (cPad only): one int endpoint for absolute finger position and
33 * two bulk endpoints for the display (in/out)
34 * This driver uses setting 1.
37 #include <linux/kernel.h>
38 #include <linux/slab.h>
39 #include <linux/module.h>
40 #include <linux/moduleparam.h>
41 #include <linux/usb.h>
42 #include <linux/input.h>
43 #include <linux/usb/input.h>
45 #define USB_VENDOR_ID_SYNAPTICS 0x06cb
46 #define USB_DEVICE_ID_SYNAPTICS_TP 0x0001 /* Synaptics USB TouchPad */
47 #define USB_DEVICE_ID_SYNAPTICS_INT_TP 0x0002 /* Integrated USB TouchPad */
48 #define USB_DEVICE_ID_SYNAPTICS_CPAD 0x0003 /* Synaptics cPad */
49 #define USB_DEVICE_ID_SYNAPTICS_TS 0x0006 /* Synaptics TouchScreen */
50 #define USB_DEVICE_ID_SYNAPTICS_STICK 0x0007 /* Synaptics USB Styk */
51 #define USB_DEVICE_ID_SYNAPTICS_WP 0x0008 /* Synaptics USB WheelPad */
52 #define USB_DEVICE_ID_SYNAPTICS_COMP_TP 0x0009 /* Composite USB TouchPad */
53 #define USB_DEVICE_ID_SYNAPTICS_WTP 0x0010 /* Wireless TouchPad */
54 #define USB_DEVICE_ID_SYNAPTICS_DPAD 0x0013 /* DisplayPad */
56 #define SYNUSB_TOUCHPAD (1 << 0)
57 #define SYNUSB_STICK (1 << 1)
58 #define SYNUSB_TOUCHSCREEN (1 << 2)
59 #define SYNUSB_AUXDISPLAY (1 << 3) /* For cPad */
60 #define SYNUSB_COMBO (1 << 4) /* Composite device (TP + stick) */
61 #define SYNUSB_IO_ALWAYS (1 << 5)
63 #define USB_DEVICE_SYNAPTICS(prod, kind) \
64 USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, \
65 USB_DEVICE_ID_SYNAPTICS_##prod), \
66 .driver_info = (kind),
68 #define SYNUSB_RECV_SIZE 8
70 #define XMIN_NOMINAL 1472
71 #define XMAX_NOMINAL 5472
72 #define YMIN_NOMINAL 1408
73 #define YMAX_NOMINAL 4448
76 struct usb_device *udev;
77 struct usb_interface *intf;
81 /* serialize access to open/suspend */
82 struct mutex pm_mutex;
85 /* input device related data structures */
86 struct input_dev *input;
90 /* characteristics of the device */
94 static void synusb_report_buttons(struct synusb *synusb)
96 struct input_dev *input_dev = synusb->input;
98 input_report_key(input_dev, BTN_LEFT, synusb->data[1] & 0x04);
99 input_report_key(input_dev, BTN_RIGHT, synusb->data[1] & 0x01);
100 input_report_key(input_dev, BTN_MIDDLE, synusb->data[1] & 0x02);
103 static void synusb_report_stick(struct synusb *synusb)
105 struct input_dev *input_dev = synusb->input;
107 unsigned int pressure;
109 pressure = synusb->data[6];
110 x = (s16)(be16_to_cpup((__be16 *)&synusb->data[2]) << 3) >> 7;
111 y = (s16)(be16_to_cpup((__be16 *)&synusb->data[4]) << 3) >> 7;
114 input_report_rel(input_dev, REL_X, x);
115 input_report_rel(input_dev, REL_Y, -y);
118 input_report_abs(input_dev, ABS_PRESSURE, pressure);
120 synusb_report_buttons(synusb);
122 input_sync(input_dev);
125 static void synusb_report_touchpad(struct synusb *synusb)
127 struct input_dev *input_dev = synusb->input;
128 unsigned int num_fingers, tool_width;
130 unsigned int pressure, w;
132 pressure = synusb->data[6];
133 x = be16_to_cpup((__be16 *)&synusb->data[2]);
134 y = be16_to_cpup((__be16 *)&synusb->data[4]);
135 w = synusb->data[0] & 0x0f;
145 case 2: /* pen, pretend its a finger */
159 * BTN_TOUCH has to be first as mousedev relies on it when doing
160 * absolute -> relative conversion
164 input_report_key(input_dev, BTN_TOUCH, 1);
166 input_report_key(input_dev, BTN_TOUCH, 0);
168 if (num_fingers > 0) {
169 input_report_abs(input_dev, ABS_X, x);
170 input_report_abs(input_dev, ABS_Y,
171 YMAX_NOMINAL + YMIN_NOMINAL - y);
174 input_report_abs(input_dev, ABS_PRESSURE, pressure);
175 input_report_abs(input_dev, ABS_TOOL_WIDTH, tool_width);
177 input_report_key(input_dev, BTN_TOOL_FINGER, num_fingers == 1);
178 input_report_key(input_dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
179 input_report_key(input_dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
181 synusb_report_buttons(synusb);
182 if (synusb->flags & SYNUSB_AUXDISPLAY)
183 input_report_key(input_dev, BTN_MIDDLE, synusb->data[1] & 0x08);
185 input_sync(input_dev);
188 static void synusb_irq(struct urb *urb)
190 struct synusb *synusb = urb->context;
193 /* Check our status in case we need to bail out early. */
194 switch (urb->status) {
196 usb_mark_last_busy(synusb->udev);
199 /* Device went away so don't keep trying to read from it. */
210 if (synusb->flags & SYNUSB_STICK)
211 synusb_report_stick(synusb);
213 synusb_report_touchpad(synusb);
216 error = usb_submit_urb(urb, GFP_ATOMIC);
217 if (error && error != -EPERM)
218 dev_err(&synusb->intf->dev,
219 "%s - usb_submit_urb failed with result: %d",
223 static struct usb_endpoint_descriptor *
224 synusb_get_in_endpoint(struct usb_host_interface *iface)
227 struct usb_endpoint_descriptor *endpoint;
230 for (i = 0; i < iface->desc.bNumEndpoints; ++i) {
231 endpoint = &iface->endpoint[i].desc;
233 if (usb_endpoint_is_int_in(endpoint)) {
234 /* we found our interrupt in endpoint */
242 static int synusb_open(struct input_dev *dev)
244 struct synusb *synusb = input_get_drvdata(dev);
247 retval = usb_autopm_get_interface(synusb->intf);
249 dev_err(&synusb->intf->dev,
250 "%s - usb_autopm_get_interface failed, error: %d\n",
255 mutex_lock(&synusb->pm_mutex);
256 retval = usb_submit_urb(synusb->urb, GFP_KERNEL);
258 dev_err(&synusb->intf->dev,
259 "%s - usb_submit_urb failed, error: %d\n",
265 synusb->intf->needs_remote_wakeup = 1;
266 synusb->is_open = true;
269 mutex_unlock(&synusb->pm_mutex);
270 usb_autopm_put_interface(synusb->intf);
274 static void synusb_close(struct input_dev *dev)
276 struct synusb *synusb = input_get_drvdata(dev);
279 autopm_error = usb_autopm_get_interface(synusb->intf);
281 mutex_lock(&synusb->pm_mutex);
282 usb_kill_urb(synusb->urb);
283 synusb->intf->needs_remote_wakeup = 0;
284 synusb->is_open = false;
285 mutex_unlock(&synusb->pm_mutex);
288 usb_autopm_put_interface(synusb->intf);
291 static int synusb_probe(struct usb_interface *intf,
292 const struct usb_device_id *id)
294 struct usb_device *udev = interface_to_usbdev(intf);
295 struct usb_endpoint_descriptor *ep;
296 struct synusb *synusb;
297 struct input_dev *input_dev;
298 unsigned int intf_num = intf->cur_altsetting->desc.bInterfaceNumber;
299 unsigned int altsetting = min(intf->num_altsetting, 1U);
302 error = usb_set_interface(udev, intf_num, altsetting);
305 "Can not set alternate setting to %i, error: %i",
310 ep = synusb_get_in_endpoint(intf->cur_altsetting);
314 synusb = kzalloc(sizeof(*synusb), GFP_KERNEL);
315 input_dev = input_allocate_device();
316 if (!synusb || !input_dev) {
323 synusb->input = input_dev;
324 mutex_init(&synusb->pm_mutex);
326 synusb->flags = id->driver_info;
327 if (synusb->flags & SYNUSB_COMBO) {
329 * This is a combo device, we need to set proper
330 * capability, depending on the interface.
332 synusb->flags |= intf_num == 1 ?
333 SYNUSB_STICK : SYNUSB_TOUCHPAD;
336 synusb->urb = usb_alloc_urb(0, GFP_KERNEL);
342 synusb->data = usb_alloc_coherent(udev, SYNUSB_RECV_SIZE, GFP_KERNEL,
343 &synusb->urb->transfer_dma);
349 usb_fill_int_urb(synusb->urb, udev,
350 usb_rcvintpipe(udev, ep->bEndpointAddress),
351 synusb->data, SYNUSB_RECV_SIZE,
354 synusb->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
356 if (udev->manufacturer)
357 strlcpy(synusb->name, udev->manufacturer,
358 sizeof(synusb->name));
361 if (udev->manufacturer)
362 strlcat(synusb->name, " ", sizeof(synusb->name));
363 strlcat(synusb->name, udev->product, sizeof(synusb->name));
366 if (!strlen(synusb->name))
367 snprintf(synusb->name, sizeof(synusb->name),
368 "USB Synaptics Device %04x:%04x",
369 le16_to_cpu(udev->descriptor.idVendor),
370 le16_to_cpu(udev->descriptor.idProduct));
372 if (synusb->flags & SYNUSB_STICK)
373 strlcat(synusb->name, " (Stick)", sizeof(synusb->name));
375 usb_make_path(udev, synusb->phys, sizeof(synusb->phys));
376 strlcat(synusb->phys, "/input0", sizeof(synusb->phys));
378 input_dev->name = synusb->name;
379 input_dev->phys = synusb->phys;
380 usb_to_input_id(udev, &input_dev->id);
381 input_dev->dev.parent = &synusb->intf->dev;
383 if (!(synusb->flags & SYNUSB_IO_ALWAYS)) {
384 input_dev->open = synusb_open;
385 input_dev->close = synusb_close;
388 input_set_drvdata(input_dev, synusb);
390 __set_bit(EV_ABS, input_dev->evbit);
391 __set_bit(EV_KEY, input_dev->evbit);
393 if (synusb->flags & SYNUSB_STICK) {
394 __set_bit(EV_REL, input_dev->evbit);
395 __set_bit(REL_X, input_dev->relbit);
396 __set_bit(REL_Y, input_dev->relbit);
397 __set_bit(INPUT_PROP_POINTING_STICK, input_dev->propbit);
398 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 127, 0, 0);
400 input_set_abs_params(input_dev, ABS_X,
401 XMIN_NOMINAL, XMAX_NOMINAL, 0, 0);
402 input_set_abs_params(input_dev, ABS_Y,
403 YMIN_NOMINAL, YMAX_NOMINAL, 0, 0);
404 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
405 input_set_abs_params(input_dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
406 __set_bit(BTN_TOUCH, input_dev->keybit);
407 __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
408 __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
409 __set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
412 if (synusb->flags & SYNUSB_TOUCHSCREEN)
413 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
415 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
417 __set_bit(BTN_LEFT, input_dev->keybit);
418 __set_bit(BTN_RIGHT, input_dev->keybit);
419 __set_bit(BTN_MIDDLE, input_dev->keybit);
421 usb_set_intfdata(intf, synusb);
423 if (synusb->flags & SYNUSB_IO_ALWAYS) {
424 error = synusb_open(input_dev);
429 error = input_register_device(input_dev);
432 "Failed to register input device, error %d\n",
440 if (synusb->flags & SYNUSB_IO_ALWAYS)
441 synusb_close(synusb->input);
443 usb_free_coherent(udev, SYNUSB_RECV_SIZE, synusb->data,
444 synusb->urb->transfer_dma);
446 usb_free_urb(synusb->urb);
448 input_free_device(input_dev);
450 usb_set_intfdata(intf, NULL);
455 static void synusb_disconnect(struct usb_interface *intf)
457 struct synusb *synusb = usb_get_intfdata(intf);
458 struct usb_device *udev = interface_to_usbdev(intf);
460 if (synusb->flags & SYNUSB_IO_ALWAYS)
461 synusb_close(synusb->input);
463 input_unregister_device(synusb->input);
465 usb_free_coherent(udev, SYNUSB_RECV_SIZE, synusb->data,
466 synusb->urb->transfer_dma);
467 usb_free_urb(synusb->urb);
470 usb_set_intfdata(intf, NULL);
473 static int synusb_suspend(struct usb_interface *intf, pm_message_t message)
475 struct synusb *synusb = usb_get_intfdata(intf);
477 mutex_lock(&synusb->pm_mutex);
478 usb_kill_urb(synusb->urb);
479 mutex_unlock(&synusb->pm_mutex);
484 static int synusb_resume(struct usb_interface *intf)
486 struct synusb *synusb = usb_get_intfdata(intf);
489 mutex_lock(&synusb->pm_mutex);
491 if ((synusb->is_open || (synusb->flags & SYNUSB_IO_ALWAYS)) &&
492 usb_submit_urb(synusb->urb, GFP_NOIO) < 0) {
496 mutex_unlock(&synusb->pm_mutex);
501 static int synusb_pre_reset(struct usb_interface *intf)
503 struct synusb *synusb = usb_get_intfdata(intf);
505 mutex_lock(&synusb->pm_mutex);
506 usb_kill_urb(synusb->urb);
511 static int synusb_post_reset(struct usb_interface *intf)
513 struct synusb *synusb = usb_get_intfdata(intf);
516 if ((synusb->is_open || (synusb->flags & SYNUSB_IO_ALWAYS)) &&
517 usb_submit_urb(synusb->urb, GFP_NOIO) < 0) {
521 mutex_unlock(&synusb->pm_mutex);
526 static int synusb_reset_resume(struct usb_interface *intf)
528 return synusb_resume(intf);
531 static const struct usb_device_id synusb_idtable[] = {
532 { USB_DEVICE_SYNAPTICS(TP, SYNUSB_TOUCHPAD) },
533 { USB_DEVICE_SYNAPTICS(INT_TP, SYNUSB_TOUCHPAD) },
534 { USB_DEVICE_SYNAPTICS(CPAD,
535 SYNUSB_TOUCHPAD | SYNUSB_AUXDISPLAY | SYNUSB_IO_ALWAYS) },
536 { USB_DEVICE_SYNAPTICS(TS, SYNUSB_TOUCHSCREEN) },
537 { USB_DEVICE_SYNAPTICS(STICK, SYNUSB_STICK) },
538 { USB_DEVICE_SYNAPTICS(WP, SYNUSB_TOUCHPAD) },
539 { USB_DEVICE_SYNAPTICS(COMP_TP, SYNUSB_COMBO) },
540 { USB_DEVICE_SYNAPTICS(WTP, SYNUSB_TOUCHPAD) },
541 { USB_DEVICE_SYNAPTICS(DPAD, SYNUSB_TOUCHPAD) },
544 MODULE_DEVICE_TABLE(usb, synusb_idtable);
546 static struct usb_driver synusb_driver = {
547 .name = "synaptics_usb",
548 .probe = synusb_probe,
549 .disconnect = synusb_disconnect,
550 .id_table = synusb_idtable,
551 .suspend = synusb_suspend,
552 .resume = synusb_resume,
553 .pre_reset = synusb_pre_reset,
554 .post_reset = synusb_post_reset,
555 .reset_resume = synusb_reset_resume,
556 .supports_autosuspend = 1,
559 module_usb_driver(synusb_driver);
561 MODULE_AUTHOR("Rob Miller <rob@inpharmatica.co.uk>, "
562 "Ron Lee <ron@debian.org>, "
563 "Jan Steinhoff <cpad@jan-steinhoff.de>");
564 MODULE_DESCRIPTION("Synaptics USB device driver");
565 MODULE_LICENSE("GPL");