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