blob: aa577898e952b48c495f3214abd690a80106df99 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/kernel.h>
3#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/module.h>
David Brownellae0dadc2006-06-13 10:04:34 -07005#include <linux/usb/input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
8/*
Julia Lawall698c03b2018-01-16 16:56:54 -08009 * Pressure-threshold modules param code from Alex Perry <alex.perry@ieee.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Julia Lawall698c03b2018-01-16 16:56:54 -080012MODULE_AUTHOR("Josh Myer <josh@joshisanerd.com>");
13MODULE_DESCRIPTION("USB KB Gear JamStudio Tablet driver");
Greg Kroah-Hartman9f1d1ea2018-01-16 16:36:48 -080014MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#define USB_VENDOR_ID_KBGEAR 0x084e
17
18static int kb_pressure_click = 0x10;
19module_param(kb_pressure_click, int, 0);
20MODULE_PARM_DESC(kb_pressure_click, "pressure threshold for clicks");
21
22struct kbtab {
Dmitry Torokhov4ee1fc82007-05-03 00:54:54 -040023 unsigned char *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 dma_addr_t data_dma;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050025 struct input_dev *dev;
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -070026 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 struct urb *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 char phys[32];
29};
30
David Howells7d12e782006-10-05 14:55:46 +010031static void kbtab_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 struct kbtab *kbtab = urb->context;
34 unsigned char *data = kbtab->data;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050035 struct input_dev *dev = kbtab->dev;
Dmitry Torokhovf3192092010-04-20 22:15:44 -070036 int pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 int retval;
38
39 switch (urb->status) {
40 case 0:
41 /* success */
42 break;
43 case -ECONNRESET:
44 case -ENOENT:
45 case -ESHUTDOWN:
46 /* this urb is terminated, clean up */
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -070047 dev_dbg(&kbtab->intf->dev,
48 "%s - urb shutting down with status: %d\n",
Greg Kroah-Hartman6d0f7dc2012-05-01 21:33:20 -070049 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return;
51 default:
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -070052 dev_dbg(&kbtab->intf->dev,
53 "%s - nonzero urb status received: %d\n",
Greg Kroah-Hartman6d0f7dc2012-05-01 21:33:20 -070054 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 goto exit;
56 }
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 input_report_key(dev, BTN_TOOL_PEN, 1);
60
Dmitry Torokhovf3192092010-04-20 22:15:44 -070061 input_report_abs(dev, ABS_X, get_unaligned_le16(&data[1]));
62 input_report_abs(dev, ABS_Y, get_unaligned_le16(&data[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /*input_report_key(dev, BTN_TOUCH , data[0] & 0x01);*/
65 input_report_key(dev, BTN_RIGHT, data[0] & 0x02);
66
Dmitry Torokhovf3192092010-04-20 22:15:44 -070067 pressure = data[5];
68 if (kb_pressure_click == -1)
69 input_report_abs(dev, ABS_PRESSURE, pressure);
70 else
71 input_report_key(dev, BTN_LEFT, pressure > kb_pressure_click ? 1 : 0);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 input_sync(dev);
74
75 exit:
Dmitry Torokhovf3192092010-04-20 22:15:44 -070076 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (retval)
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -070078 dev_err(&kbtab->intf->dev,
Greg Kroah-Hartman202712c2012-04-25 14:48:41 -070079 "%s - usb_submit_urb failed with result %d\n",
80 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Arvind Yadav95ac4cb2017-08-07 19:45:14 -070083static const struct usb_device_id kbtab_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 { USB_DEVICE(USB_VENDOR_ID_KBGEAR, 0x1001), .driver_info = 0 },
85 { }
86};
87
88MODULE_DEVICE_TABLE(usb, kbtab_ids);
89
90static int kbtab_open(struct input_dev *dev)
91{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040092 struct kbtab *kbtab = input_get_drvdata(dev);
Oliver Neukum8f7292e2016-03-31 11:01:48 -070093 struct usb_device *udev = interface_to_usbdev(kbtab->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Oliver Neukum8f7292e2016-03-31 11:01:48 -070095 kbtab->irq->dev = udev;
Dmitry Torokhov65cde542005-05-29 02:29:38 -050096 if (usb_submit_urb(kbtab->irq, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 return 0;
100}
101
102static void kbtab_close(struct input_dev *dev)
103{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400104 struct kbtab *kbtab = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500106 usb_kill_urb(kbtab->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109static int kbtab_probe(struct usb_interface *intf, const struct usb_device_id *id)
110{
111 struct usb_device *dev = interface_to_usbdev(intf);
112 struct usb_endpoint_descriptor *endpoint;
113 struct kbtab *kbtab;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500114 struct input_dev *input_dev;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400115 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Johan Hovoldcb1b4942017-03-16 11:41:55 -0700117 if (intf->cur_altsetting->desc.bNumEndpoints < 1)
118 return -ENODEV;
119
Oliver Neukumc88090d2019-08-01 09:44:25 -0700120 endpoint = &intf->cur_altsetting->endpoint[0].desc;
121 if (!usb_endpoint_is_int_in(endpoint))
122 return -ENODEV;
123
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500124 kbtab = kzalloc(sizeof(struct kbtab), GFP_KERNEL);
125 input_dev = input_allocate_device();
126 if (!kbtab || !input_dev)
127 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Daniel Mack997ea582010-04-12 13:17:25 +0200129 kbtab->data = usb_alloc_coherent(dev, 8, GFP_KERNEL, &kbtab->data_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500130 if (!kbtab->data)
131 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 kbtab->irq = usb_alloc_urb(0, GFP_KERNEL);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500134 if (!kbtab->irq)
135 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -0700137 kbtab->intf = intf;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500138 kbtab->dev = input_dev;
139
140 usb_make_path(dev, kbtab->phys, sizeof(kbtab->phys));
141 strlcat(kbtab->phys, "/input0", sizeof(kbtab->phys));
142
143 input_dev->name = "KB Gear Tablet";
144 input_dev->phys = kbtab->phys;
145 usb_to_input_id(dev, &input_dev->id);
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400146 input_dev->dev.parent = &intf->dev;
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400147
148 input_set_drvdata(input_dev, kbtab);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500149
150 input_dev->open = kbtab_open;
151 input_dev->close = kbtab_close;
152
Dmitry Torokhovf3192092010-04-20 22:15:44 -0700153 input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
154 input_dev->keybit[BIT_WORD(BTN_LEFT)] |=
155 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
156 input_dev->keybit[BIT_WORD(BTN_DIGI)] |=
157 BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_TOUCH);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500158 input_set_abs_params(input_dev, ABS_X, 0, 0x2000, 4, 0);
Dmitry Torokhov19947542005-12-29 22:19:07 -0500159 input_set_abs_params(input_dev, ABS_Y, 0, 0x1750, 4, 0);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500160 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 0xff, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 usb_fill_int_urb(kbtab->irq, dev,
163 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
164 kbtab->data, 8,
165 kbtab_irq, kbtab, endpoint->bInterval);
166 kbtab->irq->transfer_dma = kbtab->data_dma;
167 kbtab->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
168
Dmitry Torokhov50141862007-04-12 01:33:39 -0400169 error = input_register_device(kbtab->dev);
170 if (error)
171 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 usb_set_intfdata(intf, kbtab);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500176
Dmitry Torokhov50141862007-04-12 01:33:39 -0400177 fail3: usb_free_urb(kbtab->irq);
Linus Torvalds7a9b1492010-05-20 21:26:12 -0700178 fail2: usb_free_coherent(dev, 8, kbtab->data, kbtab->data_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400179 fail1: input_free_device(input_dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500180 kfree(kbtab);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400181 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184static void kbtab_disconnect(struct usb_interface *intf)
185{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500186 struct kbtab *kbtab = usb_get_intfdata(intf);
Oliver Neukum8f7292e2016-03-31 11:01:48 -0700187 struct usb_device *udev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 usb_set_intfdata(intf, NULL);
Dmitry Torokhov331cb022010-04-20 22:06:53 -0700190
191 input_unregister_device(kbtab->dev);
192 usb_free_urb(kbtab->irq);
Oliver Neukum8f7292e2016-03-31 11:01:48 -0700193 usb_free_coherent(udev, 8, kbtab->data, kbtab->data_dma);
Dmitry Torokhov331cb022010-04-20 22:06:53 -0700194 kfree(kbtab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197static struct usb_driver kbtab_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .name = "kbtab",
199 .probe = kbtab_probe,
200 .disconnect = kbtab_disconnect,
201 .id_table = kbtab_ids,
202};
203
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -0800204module_usb_driver(kbtab_driver);