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 
70 struct 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 
83 static int pegasus_control_msg(struct pegasus *pegasus, u8 *data, int len)
84 {
85 	const int sizeof_buf = len + 2;
86 	int result;
87 	int error;
88 	u8 *cmd_buf;
89 
90 	cmd_buf = kmalloc(sizeof_buf, GFP_KERNEL);
91 	if (!cmd_buf)
92 		return -ENOMEM;
93 
94 	cmd_buf[0] = NOTETAKER_REPORT_ID;
95 	cmd_buf[1] = len;
96 	memcpy(cmd_buf + 2, data, len);
97 
98 	result = usb_control_msg(pegasus->usbdev,
99 				 usb_sndctrlpipe(pegasus->usbdev, 0),
100 				 USB_REQ_SET_REPORT,
101 				 USB_TYPE_VENDOR | USB_DIR_OUT,
102 				 0, 0, cmd_buf, sizeof_buf,
103 				 USB_CTRL_SET_TIMEOUT);
104 
105 	kfree(cmd_buf);
106 
107 	if (unlikely(result != sizeof_buf)) {
108 		error = result < 0 ? result : -EIO;
109 		dev_err(&pegasus->usbdev->dev, "control msg error: %d\n",
110 			error);
111 		return error;
112 	}
113 
114 	return 0;
115 }
116 
117 static int pegasus_set_mode(struct pegasus *pegasus, u8 mode, u8 led)
118 {
119 	u8 cmd[] = { NOTETAKER_SET_CMD, NOTETAKER_SET_MODE, led, mode };
120 
121 	return pegasus_control_msg(pegasus, cmd, sizeof(cmd));
122 }
123 
124 static void pegasus_parse_packet(struct pegasus *pegasus)
125 {
126 	unsigned char *data = pegasus->data;
127 	struct input_dev *dev = pegasus->dev;
128 	u16 x, y;
129 
130 	switch (data[0]) {
131 	case SPECIAL_COMMAND:
132 		/* device button pressed */
133 		if (data[1] == BUTTON_PRESSED)
134 			schedule_work(&pegasus->init);
135 
136 		break;
137 
138 	/* xy data */
139 	case BATTERY_LOW:
140 		dev_warn_once(&dev->dev, "Pen battery low\n");
141 		/* fall through */
142 
143 	case BATTERY_NO_REPORT:
144 	case BATTERY_GOOD:
145 		x = le16_to_cpup((__le16 *)&data[2]);
146 		y = le16_to_cpup((__le16 *)&data[4]);
147 
148 		/* pen-up event */
149 		if (x == 0 && y == 0)
150 			break;
151 
152 		input_report_key(dev, BTN_TOUCH, data[1] & PEN_TIP);
153 		input_report_key(dev, BTN_RIGHT, data[1] & PEN_BUTTON_PRESSED);
154 		input_report_key(dev, BTN_TOOL_PEN, 1);
155 		input_report_abs(dev, ABS_X, (s16)x);
156 		input_report_abs(dev, ABS_Y, y);
157 
158 		input_sync(dev);
159 		break;
160 
161 	default:
162 		dev_warn_once(&pegasus->usbdev->dev,
163 			      "unknown answer from device\n");
164 	}
165 }
166 
167 static void pegasus_irq(struct urb *urb)
168 {
169 	struct pegasus *pegasus = urb->context;
170 	struct usb_device *dev = pegasus->usbdev;
171 	int retval;
172 
173 	switch (urb->status) {
174 	case 0:
175 		pegasus_parse_packet(pegasus);
176 		usb_mark_last_busy(pegasus->usbdev);
177 		break;
178 
179 	case -ECONNRESET:
180 	case -ENOENT:
181 	case -ESHUTDOWN:
182 		dev_err(&dev->dev, "%s - urb shutting down with status: %d",
183 			__func__, urb->status);
184 		return;
185 
186 	default:
187 		dev_err(&dev->dev, "%s - nonzero urb status received: %d",
188 			__func__, urb->status);
189 		break;
190 	}
191 
192 	retval = usb_submit_urb(urb, GFP_ATOMIC);
193 	if (retval)
194 		dev_err(&dev->dev, "%s - usb_submit_urb failed with result %d",
195 			__func__, retval);
196 }
197 
198 static void pegasus_init(struct work_struct *work)
199 {
200 	struct pegasus *pegasus = container_of(work, struct pegasus, init);
201 	int error;
202 
203 	error = pegasus_set_mode(pegasus, PEN_MODE_XY, NOTETAKER_LED_MOUSE);
204 	if (error)
205 		dev_err(&pegasus->usbdev->dev, "pegasus_set_mode error: %d\n",
206 			error);
207 }
208 
209 static int pegasus_open(struct input_dev *dev)
210 {
211 	struct pegasus *pegasus = input_get_drvdata(dev);
212 	int error;
213 
214 	error = usb_autopm_get_interface(pegasus->intf);
215 	if (error)
216 		return error;
217 
218 	pegasus->irq->dev = pegasus->usbdev;
219 	if (usb_submit_urb(pegasus->irq, GFP_KERNEL)) {
220 		error = -EIO;
221 		goto err_autopm_put;
222 	}
223 
224 	error = pegasus_set_mode(pegasus, PEN_MODE_XY, NOTETAKER_LED_MOUSE);
225 	if (error)
226 		goto err_kill_urb;
227 
228 	return 0;
229 
230 err_kill_urb:
231 	usb_kill_urb(pegasus->irq);
232 	cancel_work_sync(&pegasus->init);
233 err_autopm_put:
234 	usb_autopm_put_interface(pegasus->intf);
235 	return error;
236 }
237 
238 static void pegasus_close(struct input_dev *dev)
239 {
240 	struct pegasus *pegasus = input_get_drvdata(dev);
241 
242 	usb_kill_urb(pegasus->irq);
243 	cancel_work_sync(&pegasus->init);
244 	usb_autopm_put_interface(pegasus->intf);
245 }
246 
247 static int pegasus_probe(struct usb_interface *intf,
248 			 const struct usb_device_id *id)
249 {
250 	struct usb_device *dev = interface_to_usbdev(intf);
251 	struct usb_endpoint_descriptor *endpoint;
252 	struct pegasus *pegasus;
253 	struct input_dev *input_dev;
254 	int error;
255 	int pipe;
256 
257 	/* We control interface 0 */
258 	if (intf->cur_altsetting->desc.bInterfaceNumber >= 1)
259 		return -ENODEV;
260 
261 	/* Sanity check that the device has an endpoint */
262 	if (intf->altsetting[0].desc.bNumEndpoints < 1) {
263 		dev_err(&intf->dev, "Invalid number of endpoints\n");
264 		return -EINVAL;
265 	}
266 
267 	endpoint = &intf->cur_altsetting->endpoint[0].desc;
268 
269 	pegasus = kzalloc(sizeof(*pegasus), GFP_KERNEL);
270 	input_dev = input_allocate_device();
271 	if (!pegasus || !input_dev) {
272 		error = -ENOMEM;
273 		goto err_free_mem;
274 	}
275 
276 	pegasus->usbdev = dev;
277 	pegasus->dev = input_dev;
278 	pegasus->intf = intf;
279 
280 	pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
281 	pegasus->data_len = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
282 
283 	pegasus->data = usb_alloc_coherent(dev, pegasus->data_len, GFP_KERNEL,
284 					   &pegasus->data_dma);
285 	if (!pegasus->data) {
286 		error = -ENOMEM;
287 		goto err_free_mem;
288 	}
289 
290 	pegasus->irq = usb_alloc_urb(0, GFP_KERNEL);
291 	if (!pegasus->irq) {
292 		error = -ENOMEM;
293 		goto err_free_dma;
294 	}
295 
296 	usb_fill_int_urb(pegasus->irq, dev, pipe,
297 			 pegasus->data, pegasus->data_len,
298 			 pegasus_irq, pegasus, endpoint->bInterval);
299 
300 	pegasus->irq->transfer_dma = pegasus->data_dma;
301 	pegasus->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
302 
303 	if (dev->manufacturer)
304 		strlcpy(pegasus->name, dev->manufacturer,
305 			sizeof(pegasus->name));
306 
307 	if (dev->product) {
308 		if (dev->manufacturer)
309 			strlcat(pegasus->name, " ", sizeof(pegasus->name));
310 		strlcat(pegasus->name, dev->product, sizeof(pegasus->name));
311 	}
312 
313 	if (!strlen(pegasus->name))
314 		snprintf(pegasus->name, sizeof(pegasus->name),
315 			 "USB Pegasus Device %04x:%04x",
316 			 le16_to_cpu(dev->descriptor.idVendor),
317 			 le16_to_cpu(dev->descriptor.idProduct));
318 
319 	usb_make_path(dev, pegasus->phys, sizeof(pegasus->phys));
320 	strlcat(pegasus->phys, "/input0", sizeof(pegasus->phys));
321 
322 	INIT_WORK(&pegasus->init, pegasus_init);
323 
324 	usb_set_intfdata(intf, pegasus);
325 
326 	input_dev->name = pegasus->name;
327 	input_dev->phys = pegasus->phys;
328 	usb_to_input_id(dev, &input_dev->id);
329 	input_dev->dev.parent = &intf->dev;
330 
331 	input_set_drvdata(input_dev, pegasus);
332 
333 	input_dev->open = pegasus_open;
334 	input_dev->close = pegasus_close;
335 
336 	__set_bit(EV_ABS, input_dev->evbit);
337 	__set_bit(EV_KEY, input_dev->evbit);
338 
339 	__set_bit(ABS_X, input_dev->absbit);
340 	__set_bit(ABS_Y, input_dev->absbit);
341 
342 	__set_bit(BTN_TOUCH, input_dev->keybit);
343 	__set_bit(BTN_RIGHT, input_dev->keybit);
344 	__set_bit(BTN_TOOL_PEN, input_dev->keybit);
345 
346 	__set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
347 	__set_bit(INPUT_PROP_POINTER, input_dev->propbit);
348 
349 	input_set_abs_params(input_dev, ABS_X, -1500, 1500, 8, 0);
350 	input_set_abs_params(input_dev, ABS_Y, 1600, 3000, 8, 0);
351 
352 	error = input_register_device(pegasus->dev);
353 	if (error)
354 		goto err_free_urb;
355 
356 	return 0;
357 
358 err_free_urb:
359 	usb_free_urb(pegasus->irq);
360 err_free_dma:
361 	usb_free_coherent(dev, pegasus->data_len,
362 			  pegasus->data, pegasus->data_dma);
363 err_free_mem:
364 	input_free_device(input_dev);
365 	kfree(pegasus);
366 	usb_set_intfdata(intf, NULL);
367 
368 	return error;
369 }
370 
371 static void pegasus_disconnect(struct usb_interface *intf)
372 {
373 	struct pegasus *pegasus = usb_get_intfdata(intf);
374 
375 	input_unregister_device(pegasus->dev);
376 
377 	usb_free_urb(pegasus->irq);
378 	usb_free_coherent(interface_to_usbdev(intf),
379 			  pegasus->data_len, pegasus->data,
380 			  pegasus->data_dma);
381 
382 	kfree(pegasus);
383 	usb_set_intfdata(intf, NULL);
384 }
385 
386 static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
387 {
388 	struct pegasus *pegasus = usb_get_intfdata(intf);
389 
390 	mutex_lock(&pegasus->dev->mutex);
391 	usb_kill_urb(pegasus->irq);
392 	cancel_work_sync(&pegasus->init);
393 	mutex_unlock(&pegasus->dev->mutex);
394 
395 	return 0;
396 }
397 
398 static int pegasus_resume(struct usb_interface *intf)
399 {
400 	struct pegasus *pegasus = usb_get_intfdata(intf);
401 	int retval = 0;
402 
403 	mutex_lock(&pegasus->dev->mutex);
404 	if (pegasus->dev->users && usb_submit_urb(pegasus->irq, GFP_NOIO) < 0)
405 		retval = -EIO;
406 	mutex_unlock(&pegasus->dev->mutex);
407 
408 	return retval;
409 }
410 
411 static int pegasus_reset_resume(struct usb_interface *intf)
412 {
413 	struct pegasus *pegasus = usb_get_intfdata(intf);
414 	int retval = 0;
415 
416 	mutex_lock(&pegasus->dev->mutex);
417 	if (pegasus->dev->users) {
418 		retval = pegasus_set_mode(pegasus, PEN_MODE_XY,
419 					  NOTETAKER_LED_MOUSE);
420 		if (!retval && usb_submit_urb(pegasus->irq, GFP_NOIO) < 0)
421 			retval = -EIO;
422 	}
423 	mutex_unlock(&pegasus->dev->mutex);
424 
425 	return retval;
426 }
427 
428 static const struct usb_device_id pegasus_ids[] = {
429 	{ USB_DEVICE(USB_VENDOR_ID_PEGASUSTECH,
430 		     USB_DEVICE_ID_PEGASUS_NOTETAKER_EN100) },
431 	{ }
432 };
433 MODULE_DEVICE_TABLE(usb, pegasus_ids);
434 
435 static struct usb_driver pegasus_driver = {
436 	.name		= "pegasus_notetaker",
437 	.probe		= pegasus_probe,
438 	.disconnect	= pegasus_disconnect,
439 	.suspend	= pegasus_suspend,
440 	.resume		= pegasus_resume,
441 	.reset_resume	= pegasus_reset_resume,
442 	.id_table	= pegasus_ids,
443 	.supports_autosuspend = 1,
444 };
445 
446 module_usb_driver(pegasus_driver);
447 
448 MODULE_AUTHOR("Martin Kepplinger <martink@posteo.de>");
449 MODULE_DESCRIPTION("Pegasus Mobile Notetaker Pen tablet driver");
450 MODULE_LICENSE("GPL");
451