Input: add Pegasus Notetaker tablet driver
[deliverable/linux.git] / drivers / input / tablet / pegasus_notetaker.c
CommitLineData
1afca2b6
MK
1/*
2 * Pegasus Mobile Notetaker Pen input tablet driver
3 *
4 * Copyright (c) 2016 Martin Kepplinger <martink@posteo.de>
5 */
6
7/*
8 * request packet (control endpoint):
9 * |-------------------------------------|
10 * | Report ID | Nr of bytes | command |
11 * | (1 byte) | (1 byte) | (n bytes) |
12 * |-------------------------------------|
13 * | 0x02 | n | |
14 * |-------------------------------------|
15 *
16 * data packet after set xy mode command, 0x80 0xb5 0x02 0x01
17 * and pen is in range:
18 *
19 * byte byte name value (bits)
20 * --------------------------------------------
21 * 0 status 0 1 0 0 0 0 X X
22 * 1 color 0 0 0 0 H 0 S T
23 * 2 X low
24 * 3 X high
25 * 4 Y low
26 * 5 Y high
27 *
28 * X X battery state:
29 * no state reported 0x00
30 * battery low 0x01
31 * battery good 0x02
32 *
33 * H Hovering
34 * S Switch 1 (pen button)
35 * T Tip
36 */
37
38#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/input.h>
41#include <linux/usb/input.h>
42#include <linux/slab.h>
43
44/* USB HID defines */
45#define USB_REQ_GET_REPORT 0x01
46#define USB_REQ_SET_REPORT 0x09
47
48#define USB_VENDOR_ID_PEGASUSTECH 0x0e20
49#define USB_DEVICE_ID_PEGASUS_NOTETAKER_EN100 0x0101
50
51/* device specific defines */
52#define NOTETAKER_REPORT_ID 0x02
53#define NOTETAKER_SET_CMD 0x80
54#define NOTETAKER_SET_MODE 0xb5
55
56#define NOTETAKER_LED_MOUSE 0x02
57#define PEN_MODE_XY 0x01
58
59#define SPECIAL_COMMAND 0x80
60#define BUTTON_PRESSED 0xb5
61#define COMMAND_VERSION 0xa9
62
63/* in xy data packet */
64#define BATTERY_NO_REPORT 0x40
65#define BATTERY_LOW 0x41
66#define BATTERY_GOOD 0x42
67#define PEN_BUTTON_PRESSED BIT(1)
68#define PEN_TIP BIT(0)
69
70struct pegasus {
71 unsigned char *data;
72 u8 data_len;
73 dma_addr_t data_dma;
74 struct input_dev *dev;
75 struct usb_device *usbdev;
76 struct usb_interface *intf;
77 struct urb *irq;
78 char name[128];
79 char phys[64];
80 struct work_struct init;
81};
82
83static void pegasus_control_msg(struct pegasus *pegasus, u8 *data, int len)
84{
85 const int sizeof_buf = len + 2;
86 int result;
87 u8 *cmd_buf;
88
89 cmd_buf = kmalloc(sizeof_buf, GFP_KERNEL);
90 if (!cmd_buf)
91 return;
92
93 cmd_buf[0] = NOTETAKER_REPORT_ID;
94 cmd_buf[1] = len;
95 memcpy(cmd_buf + 2, data, len);
96
97 result = usb_control_msg(pegasus->usbdev,
98 usb_sndctrlpipe(pegasus->usbdev, 0),
99 USB_REQ_SET_REPORT,
100 USB_TYPE_VENDOR | USB_DIR_OUT,
101 0, 0, cmd_buf, sizeof_buf,
102 USB_CTRL_SET_TIMEOUT);
103
104 if (result != sizeof_buf)
105 dev_err(&pegasus->usbdev->dev, "control msg error\n");
106
107 kfree(cmd_buf);
108}
109
110static void pegasus_set_mode(struct pegasus *pegasus, u8 mode, u8 led)
111{
112 u8 cmd[] = { NOTETAKER_SET_CMD, NOTETAKER_SET_MODE, led, mode };
113
114 pegasus_control_msg(pegasus, cmd, sizeof(cmd));
115}
116
117static void pegasus_parse_packet(struct pegasus *pegasus)
118{
119 unsigned char *data = pegasus->data;
120 struct input_dev *dev = pegasus->dev;
121 u16 x, y;
122
123 switch (data[0]) {
124 case SPECIAL_COMMAND:
125 /* device button pressed */
126 if (data[1] == BUTTON_PRESSED)
127 schedule_work(&pegasus->init);
128
129 break;
130
131 /* xy data */
132 case BATTERY_LOW:
133 dev_warn_once(&dev->dev, "Pen battery low\n");
134 /* fall through */
135
136 case BATTERY_NO_REPORT:
137 case BATTERY_GOOD:
138 x = le16_to_cpup((__le16 *)&data[2]);
139 y = le16_to_cpup((__le16 *)&data[4]);
140
141 /* pen-up event */
142 if (x == 0 && y == 0)
143 break;
144
145 input_report_key(dev, BTN_TOUCH, data[1] & PEN_TIP);
146 input_report_key(dev, BTN_RIGHT, data[1] & PEN_BUTTON_PRESSED);
147 input_report_key(dev, BTN_TOOL_PEN, 1);
148 input_report_abs(dev, ABS_X, (s16)x);
149 input_report_abs(dev, ABS_Y, y);
150
151 input_sync(dev);
152 break;
153
154 default:
155 dev_warn_once(&pegasus->usbdev->dev,
156 "unknown answer from device\n");
157 }
158}
159
160static void pegasus_irq(struct urb *urb)
161{
162 struct pegasus *pegasus = urb->context;
163 struct usb_device *dev = pegasus->usbdev;
164 int retval;
165
166 switch (urb->status) {
167 case 0:
168 pegasus_parse_packet(pegasus);
169 usb_mark_last_busy(pegasus->usbdev);
170 break;
171
172 case -ECONNRESET:
173 case -ENOENT:
174 case -ESHUTDOWN:
175 dev_err(&dev->dev, "%s - urb shutting down with status: %d",
176 __func__, urb->status);
177 return;
178
179 default:
180 dev_err(&dev->dev, "%s - nonzero urb status received: %d",
181 __func__, urb->status);
182 break;
183 }
184
185 retval = usb_submit_urb(urb, GFP_ATOMIC);
186 if (retval)
187 dev_err(&dev->dev, "%s - usb_submit_urb failed with result %d",
188 __func__, retval);
189}
190
191static void pegasus_init(struct work_struct *work)
192{
193 struct pegasus *pegasus = container_of(work, struct pegasus, init);
194
195 pegasus_set_mode(pegasus, PEN_MODE_XY, NOTETAKER_LED_MOUSE);
196}
197
198static int pegasus_open(struct input_dev *dev)
199{
200 struct pegasus *pegasus = input_get_drvdata(dev);
201 int retval;
202
203 retval = usb_autopm_get_interface(pegasus->intf);
204 if (retval)
205 return retval;
206
207 pegasus->irq->dev = pegasus->usbdev;
208 if (usb_submit_urb(pegasus->irq, GFP_KERNEL))
209 retval = -EIO;
210
211 pegasus_set_mode(pegasus, PEN_MODE_XY, NOTETAKER_LED_MOUSE);
212
213 usb_autopm_put_interface(pegasus->intf);
214
215 return retval;
216}
217
218static void pegasus_close(struct input_dev *dev)
219{
220 struct pegasus *pegasus = input_get_drvdata(dev);
221 int autopm_error;
222
223 autopm_error = usb_autopm_get_interface(pegasus->intf);
224 usb_kill_urb(pegasus->irq);
225 cancel_work_sync(&pegasus->init);
226
227 if (!autopm_error)
228 usb_autopm_put_interface(pegasus->intf);
229}
230
231static int pegasus_probe(struct usb_interface *intf,
232 const struct usb_device_id *id)
233{
234 struct usb_device *dev = interface_to_usbdev(intf);
235 struct usb_endpoint_descriptor *endpoint;
236 struct pegasus *pegasus;
237 struct input_dev *input_dev;
238 int error;
239 int pipe;
240
241 /* We control interface 0 */
242 if (intf->cur_altsetting->desc.bInterfaceNumber >= 1)
243 return -ENODEV;
244
245 /* Sanity check that the device has an endpoint */
246 if (intf->altsetting[0].desc.bNumEndpoints < 1) {
247 dev_err(&intf->dev, "Invalid number of endpoints\n");
248 return -EINVAL;
249 }
250
251 endpoint = &intf->cur_altsetting->endpoint[0].desc;
252
253 pegasus = kzalloc(sizeof(*pegasus), GFP_KERNEL);
254 input_dev = input_allocate_device();
255 if (!pegasus || !input_dev) {
256 error = -ENOMEM;
257 goto err_free_mem;
258 }
259
260 pegasus->usbdev = dev;
261 pegasus->dev = input_dev;
262 pegasus->intf = intf;
263
264 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
265 pegasus->data_len = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
266
267 pegasus->data = usb_alloc_coherent(dev, pegasus->data_len, GFP_KERNEL,
268 &pegasus->data_dma);
269 if (!pegasus->data) {
270 error = -ENOMEM;
271 goto err_free_mem;
272 }
273
274 pegasus->irq = usb_alloc_urb(0, GFP_KERNEL);
275 if (!pegasus->irq) {
276 error = -ENOMEM;
277 goto err_free_dma;
278 }
279
280 usb_fill_int_urb(pegasus->irq, dev, pipe,
281 pegasus->data, pegasus->data_len,
282 pegasus_irq, pegasus, endpoint->bInterval);
283
284 pegasus->irq->transfer_dma = pegasus->data_dma;
285 pegasus->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
286
287 if (dev->manufacturer)
288 strlcpy(pegasus->name, dev->manufacturer,
289 sizeof(pegasus->name));
290
291 if (dev->product) {
292 if (dev->manufacturer)
293 strlcat(pegasus->name, " ", sizeof(pegasus->name));
294 strlcat(pegasus->name, dev->product, sizeof(pegasus->name));
295 }
296
297 if (!strlen(pegasus->name))
298 snprintf(pegasus->name, sizeof(pegasus->name),
299 "USB Pegasus Device %04x:%04x",
300 le16_to_cpu(dev->descriptor.idVendor),
301 le16_to_cpu(dev->descriptor.idProduct));
302
303 usb_make_path(dev, pegasus->phys, sizeof(pegasus->phys));
304 strlcat(pegasus->phys, "/input0", sizeof(pegasus->phys));
305
306 INIT_WORK(&pegasus->init, pegasus_init);
307
308 usb_set_intfdata(intf, pegasus);
309
310 input_dev->name = pegasus->name;
311 input_dev->phys = pegasus->phys;
312 usb_to_input_id(dev, &input_dev->id);
313 input_dev->dev.parent = &intf->dev;
314
315 input_set_drvdata(input_dev, pegasus);
316
317 input_dev->open = pegasus_open;
318 input_dev->close = pegasus_close;
319
320 __set_bit(EV_ABS, input_dev->evbit);
321 __set_bit(EV_KEY, input_dev->evbit);
322
323 __set_bit(ABS_X, input_dev->absbit);
324 __set_bit(ABS_Y, input_dev->absbit);
325
326 __set_bit(BTN_TOUCH, input_dev->keybit);
327 __set_bit(BTN_RIGHT, input_dev->keybit);
328 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
329
330 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
331 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
332
333 input_set_abs_params(input_dev, ABS_X, -1500, 1500, 8, 0);
334 input_set_abs_params(input_dev, ABS_Y, 1600, 3000, 8, 0);
335
336 error = input_register_device(pegasus->dev);
337 if (error)
338 goto err_free_urb;
339
340 return 0;
341
342err_free_urb:
343 usb_free_urb(pegasus->irq);
344err_free_dma:
345 usb_free_coherent(dev, pegasus->data_len,
346 pegasus->data, pegasus->data_dma);
347err_free_mem:
348 input_free_device(input_dev);
349 kfree(pegasus);
350 usb_set_intfdata(intf, NULL);
351
352 return error;
353}
354
355static void pegasus_disconnect(struct usb_interface *intf)
356{
357 struct pegasus *pegasus = usb_get_intfdata(intf);
358
359 input_unregister_device(pegasus->dev);
360
361 usb_free_urb(pegasus->irq);
362 usb_free_coherent(interface_to_usbdev(intf),
363 pegasus->data_len, pegasus->data,
364 pegasus->data_dma);
365
366 kfree(pegasus);
367 usb_set_intfdata(intf, NULL);
368}
369
370static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
371{
372 struct pegasus *pegasus = usb_get_intfdata(intf);
373
374 mutex_lock(&pegasus->dev->mutex);
375 usb_kill_urb(pegasus->irq);
376 mutex_unlock(&pegasus->dev->mutex);
377
378 return 0;
379}
380
381static int pegasus_resume(struct usb_interface *intf)
382{
383 struct pegasus *pegasus = usb_get_intfdata(intf);
384 int retval = 0;
385
386 mutex_lock(&pegasus->dev->mutex);
387 if (pegasus->dev->users && usb_submit_urb(pegasus->irq, GFP_NOIO) < 0)
388 retval = -EIO;
389 mutex_unlock(&pegasus->dev->mutex);
390
391 return retval;
392}
393
394static int pegasus_reset_resume(struct usb_interface *intf)
395{
396 return pegasus_resume(intf);
397}
398
399static const struct usb_device_id pegasus_ids[] = {
400 { USB_DEVICE(USB_VENDOR_ID_PEGASUSTECH,
401 USB_DEVICE_ID_PEGASUS_NOTETAKER_EN100) },
402 { }
403};
404MODULE_DEVICE_TABLE(usb, pegasus_ids);
405
406static struct usb_driver pegasus_driver = {
407 .name = "pegasus_notetaker",
408 .probe = pegasus_probe,
409 .disconnect = pegasus_disconnect,
410 .suspend = pegasus_suspend,
411 .resume = pegasus_resume,
412 .reset_resume = pegasus_reset_resume,
413 .id_table = pegasus_ids,
414 .supports_autosuspend = 1,
415};
416
417module_usb_driver(pegasus_driver);
418
419MODULE_AUTHOR("Martin Kepplinger <martink@posteo.de>");
420MODULE_DESCRIPTION("Pegasus Mobile Notetaker Pen tablet driver");
421MODULE_LICENSE("GPL");
This page took 0.355111 seconds and 5 git commands to generate.