1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107#include <linux/completion.h>
108#include <linux/init.h>
109#include <linux/input.h>
110#include <linux/interrupt.h>
111#include <linux/kernel.h>
112#include <linux/module.h>
113#include <linux/serio.h>
114#include <linux/slab.h>
115#include <linux/string.h>
116
117MODULE_AUTHOR("Julian Squires <julian@cipht.net>, Hans de Goede <hdegoede@redhat.com>");
118MODULE_DESCRIPTION("Wacom protocol 4 serial tablet driver");
119MODULE_LICENSE("GPL");
120
121#define REQUEST_MODEL_AND_ROM_VERSION "~#"
122#define REQUEST_MAX_COORDINATES "~C\r"
123#define REQUEST_CONFIGURATION_STRING "~R\r"
124#define REQUEST_RESET_TO_PROTOCOL_IV "\r#"
125
126
127
128
129
130#define COMMAND_START_SENDING_PACKETS "ST\r"
131#define COMMAND_STOP_SENDING_PACKETS "SP\r"
132#define COMMAND_MULTI_MODE_INPUT "MU1\r"
133#define COMMAND_ORIGIN_IN_UPPER_LEFT "OC1\r"
134#define COMMAND_ENABLE_ALL_MACRO_BUTTONS "~M0\r"
135#define COMMAND_DISABLE_GROUP_1_MACRO_BUTTONS "~M1\r"
136#define COMMAND_TRANSMIT_AT_MAX_RATE "IT0\r"
137#define COMMAND_DISABLE_INCREMENTAL_MODE "IN0\r"
138#define COMMAND_ENABLE_CONTINUOUS_MODE "SR\r"
139#define COMMAND_ENABLE_PRESSURE_MODE "PH1\r"
140#define COMMAND_Z_FILTER "ZF1\r"
141
142
143#define PACKET_LENGTH 7
144#define DATA_SIZE 32
145
146
147#define F_COVERS_SCREEN 0x01
148#define F_HAS_STYLUS2 0x02
149#define F_HAS_SCROLLWHEEL 0x04
150
151
152#define STYLUS_DEVICE_ID 0x02
153#define CURSOR_DEVICE_ID 0x06
154#define ERASER_DEVICE_ID 0x0A
155
156enum { STYLUS = 1, ERASER, CURSOR };
157
158static const struct {
159 int device_id;
160 int input_id;
161} tools[] = {
162 { 0, 0 },
163 { STYLUS_DEVICE_ID, BTN_TOOL_PEN },
164 { ERASER_DEVICE_ID, BTN_TOOL_RUBBER },
165 { CURSOR_DEVICE_ID, BTN_TOOL_MOUSE },
166};
167
168struct wacom {
169 struct input_dev *dev;
170 struct completion cmd_done;
171 int result;
172 u8 expect;
173 u8 eraser_mask;
174 unsigned int extra_z_bits;
175 unsigned int flags;
176 unsigned int res_x, res_y;
177 unsigned int max_x, max_y;
178 unsigned int tool;
179 unsigned int idx;
180 u8 data[DATA_SIZE];
181 char phys[32];
182};
183
184enum {
185 MODEL_CINTIQ = 0x504C,
186 MODEL_CINTIQ2 = 0x4454,
187 MODEL_DIGITIZER_II = 0x5544,
188 MODEL_GRAPHIRE = 0x4554,
189 MODEL_PENPARTNER = 0x4354,
190 MODEL_ARTPAD_II = 0x4B54,
191};
192
193static void wacom_handle_model_response(struct wacom *wacom)
194{
195 int major_v, minor_v, r = 0;
196 char *p;
197
198 p = strrchr(wacom->data, 'V');
199 if (p)
200 r = sscanf(p + 1, "%u.%u", &major_v, &minor_v);
201 if (r != 2)
202 major_v = minor_v = 0;
203
204 switch (wacom->data[2] << 8 | wacom->data[3]) {
205 case MODEL_CINTIQ:
206 case MODEL_CINTIQ2:
207 if ((wacom->data[2] << 8 | wacom->data[3]) == MODEL_CINTIQ) {
208 wacom->dev->name = "Wacom Cintiq";
209 wacom->dev->id.version = MODEL_CINTIQ;
210 } else {
211 wacom->dev->name = "Wacom Cintiq II";
212 wacom->dev->id.version = MODEL_CINTIQ2;
213 }
214 wacom->res_x = 508;
215 wacom->res_y = 508;
216
217 switch (wacom->data[5] << 8 | wacom->data[6]) {
218 case 0x3731:
219 wacom->res_x = 2540;
220 wacom->res_y = 2540;
221
222 case 0x3535:
223 case 0x3830:
224 wacom->extra_z_bits = 2;
225 }
226
227 wacom->flags = F_COVERS_SCREEN;
228 break;
229
230 case MODEL_PENPARTNER:
231 wacom->dev->name = "Wacom Penpartner";
232 wacom->dev->id.version = MODEL_PENPARTNER;
233 wacom->res_x = 1000;
234 wacom->res_y = 1000;
235 break;
236
237 case MODEL_GRAPHIRE:
238 wacom->dev->name = "Wacom Graphire";
239 wacom->dev->id.version = MODEL_GRAPHIRE;
240 wacom->res_x = 1016;
241 wacom->res_y = 1016;
242 wacom->max_x = 5103;
243 wacom->max_y = 3711;
244 wacom->extra_z_bits = 2;
245 wacom->eraser_mask = 0x08;
246 wacom->flags = F_HAS_STYLUS2 | F_HAS_SCROLLWHEEL;
247 break;
248
249 case MODEL_ARTPAD_II:
250 case MODEL_DIGITIZER_II:
251 wacom->dev->name = "Wacom Digitizer II";
252 wacom->dev->id.version = MODEL_DIGITIZER_II;
253 if (major_v == 1 && minor_v <= 2)
254 wacom->extra_z_bits = 0;
255 break;
256
257 default:
258 dev_err(&wacom->dev->dev, "Unsupported Wacom model %s\n",
259 wacom->data);
260 wacom->result = -ENODEV;
261 return;
262 }
263
264 dev_info(&wacom->dev->dev, "%s tablet, version %u.%u\n",
265 wacom->dev->name, major_v, minor_v);
266}
267
268static void wacom_handle_configuration_response(struct wacom *wacom)
269{
270 int r, skip;
271
272 dev_dbg(&wacom->dev->dev, "Configuration string: %s\n", wacom->data);
273 r = sscanf(wacom->data, "~R%x,%u,%u,%u,%u", &skip, &skip, &skip,
274 &wacom->res_x, &wacom->res_y);
275 if (r != 5)
276 dev_warn(&wacom->dev->dev, "could not get resolution\n");
277}
278
279static void wacom_handle_coordinates_response(struct wacom *wacom)
280{
281 int r;
282
283 dev_dbg(&wacom->dev->dev, "Coordinates string: %s\n", wacom->data);
284 r = sscanf(wacom->data, "~C%u,%u", &wacom->max_x, &wacom->max_y);
285 if (r != 2)
286 dev_warn(&wacom->dev->dev, "could not get max coordinates\n");
287}
288
289static void wacom_handle_response(struct wacom *wacom)
290{
291 if (wacom->data[0] != '~' || wacom->data[1] != wacom->expect) {
292 dev_err(&wacom->dev->dev,
293 "Wacom got an unexpected response: %s\n", wacom->data);
294 wacom->result = -EIO;
295 } else {
296 wacom->result = 0;
297
298 switch (wacom->data[1]) {
299 case '#':
300 wacom_handle_model_response(wacom);
301 break;
302 case 'R':
303 wacom_handle_configuration_response(wacom);
304 break;
305 case 'C':
306 wacom_handle_coordinates_response(wacom);
307 break;
308 }
309 }
310
311 complete(&wacom->cmd_done);
312}
313
314static void wacom_handle_packet(struct wacom *wacom)
315{
316 u8 in_proximity_p, stylus_p, button;
317 unsigned int tool;
318 int x, y, z;
319
320 in_proximity_p = wacom->data[0] & 0x40;
321 stylus_p = wacom->data[0] & 0x20;
322 button = (wacom->data[3] & 0x78) >> 3;
323 x = (wacom->data[0] & 3) << 14 | wacom->data[1]<<7 | wacom->data[2];
324 y = (wacom->data[3] & 3) << 14 | wacom->data[4]<<7 | wacom->data[5];
325
326 if (in_proximity_p && stylus_p) {
327 z = wacom->data[6] & 0x7f;
328 if (wacom->extra_z_bits >= 1)
329 z = z << 1 | (wacom->data[3] & 0x4) >> 2;
330 if (wacom->extra_z_bits > 1)
331 z = z << 1 | (wacom->data[0] & 0x4) >> 2;
332 z = z ^ (0x40 << wacom->extra_z_bits);
333 } else {
334 z = -1;
335 }
336
337 if (stylus_p)
338 tool = (button & wacom->eraser_mask) ? ERASER : STYLUS;
339 else
340 tool = CURSOR;
341
342 if (tool != wacom->tool && wacom->tool != 0) {
343 input_report_key(wacom->dev, tools[wacom->tool].input_id, 0);
344 input_sync(wacom->dev);
345 }
346 wacom->tool = tool;
347
348 input_report_key(wacom->dev, tools[tool].input_id, in_proximity_p);
349 input_report_abs(wacom->dev, ABS_MISC,
350 in_proximity_p ? tools[tool].device_id : 0);
351 input_report_abs(wacom->dev, ABS_X, x);
352 input_report_abs(wacom->dev, ABS_Y, y);
353 input_report_abs(wacom->dev, ABS_PRESSURE, z);
354 if (stylus_p) {
355 input_report_key(wacom->dev, BTN_TOUCH, button & 1);
356 input_report_key(wacom->dev, BTN_STYLUS, button & 2);
357 input_report_key(wacom->dev, BTN_STYLUS2, button & 4);
358 } else {
359 input_report_key(wacom->dev, BTN_LEFT, button & 1);
360 input_report_key(wacom->dev, BTN_RIGHT, button & 2);
361 input_report_key(wacom->dev, BTN_MIDDLE, button & 4);
362
363 z = (wacom->data[6] & 0x30) >> 4;
364 if (wacom->data[6] & 0x40)
365 z = -z;
366 input_report_rel(wacom->dev, REL_WHEEL, z);
367 }
368 input_sync(wacom->dev);
369}
370
371static void wacom_clear_data_buf(struct wacom *wacom)
372{
373 memset(wacom->data, 0, DATA_SIZE);
374 wacom->idx = 0;
375}
376
377static irqreturn_t wacom_interrupt(struct serio *serio, unsigned char data,
378 unsigned int flags)
379{
380 struct wacom *wacom = serio_get_drvdata(serio);
381
382 if (data & 0x80)
383 wacom->idx = 0;
384
385
386
387
388
389
390
391
392
393
394 if (data == '\r' && !(wacom->data[0] & 0x80)) {
395 wacom_handle_response(wacom);
396 wacom_clear_data_buf(wacom);
397 return IRQ_HANDLED;
398 }
399
400
401 if (wacom->idx > (DATA_SIZE - 2)) {
402 dev_dbg(&wacom->dev->dev,
403 "throwing away %d bytes of garbage\n", wacom->idx);
404 wacom_clear_data_buf(wacom);
405 }
406 wacom->data[wacom->idx++] = data;
407
408 if (wacom->idx == PACKET_LENGTH && (wacom->data[0] & 0x80)) {
409 wacom_handle_packet(wacom);
410 wacom_clear_data_buf(wacom);
411 }
412
413 return IRQ_HANDLED;
414}
415
416static void wacom_disconnect(struct serio *serio)
417{
418 struct wacom *wacom = serio_get_drvdata(serio);
419
420 serio_close(serio);
421 serio_set_drvdata(serio, NULL);
422 input_unregister_device(wacom->dev);
423 kfree(wacom);
424}
425
426static int wacom_send(struct serio *serio, const u8 *command)
427{
428 int err = 0;
429
430 for (; !err && *command; command++)
431 err = serio_write(serio, *command);
432
433 return err;
434}
435
436static int wacom_send_setup_string(struct wacom *wacom, struct serio *serio)
437{
438 const u8 *cmd;
439
440 switch (wacom->dev->id.version) {
441 case MODEL_CINTIQ:
442 cmd = COMMAND_ORIGIN_IN_UPPER_LEFT
443 COMMAND_TRANSMIT_AT_MAX_RATE
444 COMMAND_ENABLE_CONTINUOUS_MODE
445 COMMAND_START_SENDING_PACKETS;
446 break;
447
448 case MODEL_PENPARTNER:
449 cmd = COMMAND_ENABLE_PRESSURE_MODE
450 COMMAND_START_SENDING_PACKETS;
451 break;
452
453 default:
454 cmd = COMMAND_MULTI_MODE_INPUT
455 COMMAND_ORIGIN_IN_UPPER_LEFT
456 COMMAND_ENABLE_ALL_MACRO_BUTTONS
457 COMMAND_DISABLE_GROUP_1_MACRO_BUTTONS
458 COMMAND_TRANSMIT_AT_MAX_RATE
459 COMMAND_DISABLE_INCREMENTAL_MODE
460 COMMAND_ENABLE_CONTINUOUS_MODE
461 COMMAND_Z_FILTER
462 COMMAND_START_SENDING_PACKETS;
463 break;
464 }
465
466 return wacom_send(serio, cmd);
467}
468
469static int wacom_send_and_wait(struct wacom *wacom, struct serio *serio,
470 const u8 *cmd, const char *desc)
471{
472 int err;
473 unsigned long u;
474
475 wacom->expect = cmd[1];
476 init_completion(&wacom->cmd_done);
477
478 err = wacom_send(serio, cmd);
479 if (err)
480 return err;
481
482 u = wait_for_completion_timeout(&wacom->cmd_done, HZ);
483 if (u == 0) {
484
485 wacom_handle_response(wacom);
486 }
487
488 wacom->expect = 0;
489 return wacom->result;
490}
491
492static int wacom_setup(struct wacom *wacom, struct serio *serio)
493{
494 int err;
495
496
497
498
499 err = wacom_send_and_wait(wacom, serio, REQUEST_MODEL_AND_ROM_VERSION,
500 "model and version");
501 if (err)
502 return err;
503
504 if (!(wacom->res_x && wacom->res_y)) {
505 err = wacom_send_and_wait(wacom, serio,
506 REQUEST_CONFIGURATION_STRING,
507 "configuration string");
508 if (err)
509 return err;
510 }
511
512 if (!(wacom->max_x && wacom->max_y)) {
513 err = wacom_send_and_wait(wacom, serio,
514 REQUEST_MAX_COORDINATES,
515 "coordinates string");
516 if (err)
517 return err;
518 }
519
520 return wacom_send_setup_string(wacom, serio);
521}
522
523static int wacom_connect(struct serio *serio, struct serio_driver *drv)
524{
525 struct wacom *wacom;
526 struct input_dev *input_dev;
527 int err = -ENOMEM;
528
529 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
530 input_dev = input_allocate_device();
531 if (!wacom || !input_dev)
532 goto free_device;
533
534 wacom->dev = input_dev;
535 wacom->extra_z_bits = 1;
536 wacom->eraser_mask = 0x04;
537 wacom->tool = wacom->idx = 0;
538 snprintf(wacom->phys, sizeof(wacom->phys), "%s/input0", serio->phys);
539 input_dev->phys = wacom->phys;
540 input_dev->id.bustype = BUS_RS232;
541 input_dev->id.vendor = SERIO_WACOM_IV;
542 input_dev->id.product = serio->id.extra;
543 input_dev->dev.parent = &serio->dev;
544
545 input_dev->evbit[0] =
546 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) | BIT_MASK(EV_REL);
547 set_bit(ABS_MISC, input_dev->absbit);
548 set_bit(BTN_TOOL_PEN, input_dev->keybit);
549 set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
550 set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
551 set_bit(BTN_TOUCH, input_dev->keybit);
552 set_bit(BTN_STYLUS, input_dev->keybit);
553 set_bit(BTN_LEFT, input_dev->keybit);
554 set_bit(BTN_RIGHT, input_dev->keybit);
555 set_bit(BTN_MIDDLE, input_dev->keybit);
556
557 serio_set_drvdata(serio, wacom);
558
559 err = serio_open(serio, drv);
560 if (err)
561 goto free_device;
562
563 err = wacom_setup(wacom, serio);
564 if (err)
565 goto close_serio;
566
567 set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
568 if (!(wacom->flags & F_COVERS_SCREEN))
569 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
570
571 if (wacom->flags & F_HAS_STYLUS2)
572 __set_bit(BTN_STYLUS2, input_dev->keybit);
573
574 if (wacom->flags & F_HAS_SCROLLWHEEL)
575 __set_bit(REL_WHEEL, input_dev->relbit);
576
577 input_abs_set_res(wacom->dev, ABS_X, wacom->res_x);
578 input_abs_set_res(wacom->dev, ABS_Y, wacom->res_y);
579 input_set_abs_params(wacom->dev, ABS_X, 0, wacom->max_x, 0, 0);
580 input_set_abs_params(wacom->dev, ABS_Y, 0, wacom->max_y, 0, 0);
581 input_set_abs_params(wacom->dev, ABS_PRESSURE, -1,
582 (1 << (7 + wacom->extra_z_bits)) - 1, 0, 0);
583
584 err = input_register_device(wacom->dev);
585 if (err)
586 goto close_serio;
587
588 return 0;
589
590close_serio:
591 serio_close(serio);
592free_device:
593 serio_set_drvdata(serio, NULL);
594 input_free_device(input_dev);
595 kfree(wacom);
596 return err;
597}
598
599static const struct serio_device_id wacom_serio_ids[] = {
600 {
601 .type = SERIO_RS232,
602 .proto = SERIO_WACOM_IV,
603 .id = SERIO_ANY,
604 .extra = SERIO_ANY,
605 },
606 { 0 }
607};
608
609MODULE_DEVICE_TABLE(serio, wacom_serio_ids);
610
611static struct serio_driver wacom_drv = {
612 .driver = {
613 .name = "wacom_serial4",
614 },
615 .description = "Wacom protocol 4 serial tablet driver",
616 .id_table = wacom_serio_ids,
617 .interrupt = wacom_interrupt,
618 .connect = wacom_connect,
619 .disconnect = wacom_disconnect,
620};
621
622module_serio_driver(wacom_drv);
623