Input: wacom - get rid of input event wrappers
[linux-block.git] / drivers / input / tablet / wacom_sys.c
CommitLineData
3bea733a 1/*
4104d13f 2 * drivers/input/tablet/wacom_sys.c
3bea733a 3 *
232f5693 4 * USB Wacom tablet support - system specific code
3bea733a
PC
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
3bea733a 14#include "wacom_wac.h"
51269fe8 15#include "wacom.h"
3bea733a 16
545f4e99
PC
17/* defines to get HID report descriptor */
18#define HID_DEVICET_HID (USB_TYPE_CLASS | 0x01)
19#define HID_DEVICET_REPORT (USB_TYPE_CLASS | 0x02)
20#define HID_USAGE_UNDEFINED 0x00
21#define HID_USAGE_PAGE 0x05
22#define HID_USAGE_PAGE_DIGITIZER 0x0d
23#define HID_USAGE_PAGE_DESKTOP 0x01
24#define HID_USAGE 0x09
25#define HID_USAGE_X 0x30
26#define HID_USAGE_Y 0x31
27#define HID_USAGE_X_TILT 0x3d
28#define HID_USAGE_Y_TILT 0x3e
29#define HID_USAGE_FINGER 0x22
30#define HID_USAGE_STYLUS 0x20
31#define HID_COLLECTION 0xc0
32
33enum {
34 WCM_UNDEFINED = 0,
35 WCM_DESKTOP,
36 WCM_DIGITIZER,
37};
38
39struct hid_descriptor {
40 struct usb_descriptor_header header;
41 __le16 bcdHID;
42 u8 bCountryCode;
43 u8 bNumDescriptors;
44 u8 bDescriptorType;
45 __le16 wDescriptorLength;
46} __attribute__ ((packed));
47
48/* defines to get/set USB message */
3bea733a
PC
49#define USB_REQ_GET_REPORT 0x01
50#define USB_REQ_SET_REPORT 0x09
545f4e99 51#define WAC_HID_FEATURE_REPORT 0x03
3bea733a
PC
52
53static int usb_get_report(struct usb_interface *intf, unsigned char type,
54 unsigned char id, void *buf, int size)
55{
56 return usb_control_msg(interface_to_usbdev(intf),
57 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
58 USB_REQ_GET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
59 (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
60 buf, size, 100);
61}
62
63static int usb_set_report(struct usb_interface *intf, unsigned char type,
64 unsigned char id, void *buf, int size)
65{
66 return usb_control_msg(interface_to_usbdev(intf),
67 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
68 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
69 (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
70 buf, size, 1000);
71}
72
73static struct input_dev * get_input_dev(struct wacom_combo *wcombo)
74{
8da23fc1 75 return wcombo->wacom->wacom_wac.input;
3bea733a
PC
76}
77
54c9b226 78static void wacom_sys_irq(struct urb *urb)
3bea733a
PC
79{
80 struct wacom *wacom = urb->context;
81 struct wacom_combo wcombo;
82 int retval;
83
84 switch (urb->status) {
85 case 0:
86 /* success */
87 break;
88 case -ECONNRESET:
89 case -ENOENT:
90 case -ESHUTDOWN:
91 /* this urb is terminated, clean up */
ea3e6c59 92 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
3bea733a
PC
93 return;
94 default:
ea3e6c59 95 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
3bea733a
PC
96 goto exit;
97 }
98
99 wcombo.wacom = wacom;
100 wcombo.urb = urb;
3bea733a 101
73a97f4f 102 if (wacom_wac_irq(&wacom->wacom_wac, &wcombo))
3bea733a
PC
103 input_sync(get_input_dev(&wcombo));
104
105 exit:
e7224094 106 usb_mark_last_busy(wacom->usbdev);
73a97f4f 107 retval = usb_submit_urb(urb, GFP_ATOMIC);
3bea733a
PC
108 if (retval)
109 err ("%s - usb_submit_urb failed with result %d",
ea3e6c59 110 __func__, retval);
3bea733a
PC
111}
112
3bea733a
PC
113__u16 wacom_be16_to_cpu(unsigned char *data)
114{
115 __u16 value;
116 value = be16_to_cpu(*(__be16 *) data);
117 return value;
118}
119
120__u16 wacom_le16_to_cpu(unsigned char *data)
121{
122 __u16 value;
8d32e3ae 123 value = le16_to_cpu(*(__le16 *) data);
3bea733a
PC
124 return value;
125}
126
3bea733a
PC
127static int wacom_open(struct input_dev *dev)
128{
7791bdae 129 struct wacom *wacom = input_get_drvdata(dev);
3bea733a 130
e7224094
ON
131 mutex_lock(&wacom->lock);
132
3bea733a 133 wacom->irq->dev = wacom->usbdev;
e7224094
ON
134
135 if (usb_autopm_get_interface(wacom->intf) < 0) {
136 mutex_unlock(&wacom->lock);
3bea733a 137 return -EIO;
e7224094
ON
138 }
139
140 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
141 usb_autopm_put_interface(wacom->intf);
142 mutex_unlock(&wacom->lock);
143 return -EIO;
144 }
145
51269fe8 146 wacom->open = true;
e7224094 147 wacom->intf->needs_remote_wakeup = 1;
3bea733a 148
e7224094 149 mutex_unlock(&wacom->lock);
3bea733a
PC
150 return 0;
151}
152
153static void wacom_close(struct input_dev *dev)
154{
7791bdae 155 struct wacom *wacom = input_get_drvdata(dev);
3bea733a 156
e7224094 157 mutex_lock(&wacom->lock);
3bea733a 158 usb_kill_urb(wacom->irq);
51269fe8 159 wacom->open = false;
e7224094
ON
160 wacom->intf->needs_remote_wakeup = 0;
161 mutex_unlock(&wacom->lock);
3bea733a
PC
162}
163
545f4e99 164static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
ec67bbed 165 struct wacom_features *features)
545f4e99
PC
166{
167 struct usb_device *dev = interface_to_usbdev(intf);
ec67bbed
PC
168 char limit = 0;
169 /* result has to be defined as int for some devices */
170 int result = 0;
545f4e99
PC
171 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
172 unsigned char *report;
173
174 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
175 if (!report)
176 return -ENOMEM;
177
178 /* retrive report descriptors */
179 do {
180 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
181 USB_REQ_GET_DESCRIPTOR,
182 USB_RECIP_INTERFACE | USB_DIR_IN,
183 HID_DEVICET_REPORT << 8,
184 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
185 report,
186 hid_desc->wDescriptorLength,
187 5000); /* 5 secs */
188 } while (result < 0 && limit++ < 5);
189
384318ec 190 /* No need to parse the Descriptor. It isn't an error though */
545f4e99
PC
191 if (result < 0)
192 goto out;
193
194 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
195
196 switch (report[i]) {
197 case HID_USAGE_PAGE:
198 switch (report[i + 1]) {
199 case HID_USAGE_PAGE_DIGITIZER:
200 usage = WCM_DIGITIZER;
201 i++;
202 break;
203
204 case HID_USAGE_PAGE_DESKTOP:
205 usage = WCM_DESKTOP;
206 i++;
207 break;
208 }
209 break;
210
211 case HID_USAGE:
212 switch (report[i + 1]) {
213 case HID_USAGE_X:
214 if (usage == WCM_DESKTOP) {
215 if (finger) {
ec67bbed
PC
216 features->device_type = BTN_TOOL_DOUBLETAP;
217 if (features->type == TABLETPC2FG) {
218 /* need to reset back */
219 features->pktlen = WACOM_PKGLEN_TPC2FG;
220 features->device_type = BTN_TOOL_TRIPLETAP;
221 }
545f4e99 222 features->x_max =
ec67bbed
PC
223 wacom_le16_to_cpu(&report[i + 3]);
224 features->x_phy =
545f4e99 225 wacom_le16_to_cpu(&report[i + 6]);
ec67bbed
PC
226 features->unit = report[i + 9];
227 features->unitExpo = report[i + 11];
228 i += 12;
545f4e99 229 } else if (pen) {
ec67bbed
PC
230 /* penabled only accepts exact bytes of data */
231 if (features->type == TABLETPC2FG)
57e413d9 232 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
ec67bbed 233 features->device_type = BTN_TOOL_PEN;
545f4e99
PC
234 features->x_max =
235 wacom_le16_to_cpu(&report[i + 3]);
236 i += 4;
237 }
238 } else if (usage == WCM_DIGITIZER) {
239 /* max pressure isn't reported
240 features->pressure_max = (unsigned short)
241 (report[i+4] << 8 | report[i + 3]);
242 */
243 features->pressure_max = 255;
244 i += 4;
245 }
246 break;
247
248 case HID_USAGE_Y:
ec67bbed
PC
249 if (usage == WCM_DESKTOP) {
250 if (finger) {
251 features->device_type = BTN_TOOL_DOUBLETAP;
252 if (features->type == TABLETPC2FG) {
253 /* need to reset back */
254 features->pktlen = WACOM_PKGLEN_TPC2FG;
255 features->device_type = BTN_TOOL_TRIPLETAP;
256 features->y_max =
257 wacom_le16_to_cpu(&report[i + 3]);
258 features->y_phy =
259 wacom_le16_to_cpu(&report[i + 6]);
260 i += 7;
261 } else {
262 features->y_max =
263 features->x_max;
264 features->y_phy =
265 wacom_le16_to_cpu(&report[i + 3]);
266 i += 4;
267 }
268 } else if (pen) {
269 /* penabled only accepts exact bytes of data */
270 if (features->type == TABLETPC2FG)
57e413d9 271 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
ec67bbed
PC
272 features->device_type = BTN_TOOL_PEN;
273 features->y_max =
274 wacom_le16_to_cpu(&report[i + 3]);
275 i += 4;
276 }
277 }
545f4e99
PC
278 break;
279
280 case HID_USAGE_FINGER:
281 finger = 1;
282 i++;
283 break;
284
285 case HID_USAGE_STYLUS:
286 pen = 1;
287 i++;
288 break;
289
290 case HID_USAGE_UNDEFINED:
291 if (usage == WCM_DESKTOP && finger) /* capacity */
292 features->pressure_max =
293 wacom_le16_to_cpu(&report[i + 3]);
294 i += 4;
295 break;
296 }
297 break;
298
299 case HID_COLLECTION:
ec67bbed 300 /* reset UsagePage and Finger */
545f4e99
PC
301 finger = usage = 0;
302 break;
303 }
304 }
305
545f4e99 306 out:
384318ec 307 result = 0;
545f4e99
PC
308 kfree(report);
309 return result;
310}
311
ec67bbed 312static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
3b7307c2
DT
313{
314 unsigned char *rep_data;
ec67bbed
PC
315 int limit = 0, report_id = 2;
316 int error = -ENOMEM;
3b7307c2
DT
317
318 rep_data = kmalloc(2, GFP_KERNEL);
319 if (!rep_data)
ec67bbed
PC
320 return error;
321
322 /* ask to report tablet data if it is 2FGT or not a Tablet PC */
323 if (features->device_type == BTN_TOOL_TRIPLETAP) {
324 do {
325 rep_data[0] = 3;
326 rep_data[1] = 4;
327 report_id = 3;
328 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
329 report_id, rep_data, 2);
330 if (error >= 0)
331 error = usb_get_report(intf,
332 WAC_HID_FEATURE_REPORT, report_id,
333 rep_data, 3);
334 } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
335 } else if (features->type != TABLETPC && features->type != TABLETPC2FG) {
336 do {
337 rep_data[0] = 2;
338 rep_data[1] = 2;
339 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
340 report_id, rep_data, 2);
341 if (error >= 0)
342 error = usb_get_report(intf,
343 WAC_HID_FEATURE_REPORT, report_id,
344 rep_data, 2);
345 } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
346 }
3b7307c2
DT
347
348 kfree(rep_data);
349
350 return error < 0 ? error : 0;
351}
352
ec67bbed
PC
353static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
354 struct wacom_features *features)
355{
356 int error = 0;
357 struct usb_host_interface *interface = intf->cur_altsetting;
358 struct hid_descriptor *hid_desc;
359
360 /* default device to penabled */
361 features->device_type = BTN_TOOL_PEN;
362
363 /* only Tablet PCs need to retrieve the info */
364 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG))
365 goto out;
366
367 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
368 if (usb_get_extra_descriptor(&interface->endpoint[0],
369 HID_DEVICET_REPORT, &hid_desc)) {
370 printk("wacom: can not retrieve extra class descriptor\n");
371 error = 1;
372 goto out;
373 }
374 }
375 error = wacom_parse_hid(intf, hid_desc, features);
376 if (error)
377 goto out;
378
379 /* touch device found but size is not defined. use default */
380 if (features->device_type == BTN_TOOL_DOUBLETAP && !features->x_max) {
381 features->x_max = 1023;
382 features->y_max = 1023;
383 }
384
385 out:
386 return error;
387}
388
4492efff
PC
389struct wacom_usbdev_data {
390 struct list_head list;
391 struct kref kref;
392 struct usb_device *dev;
393 struct wacom_shared shared;
394};
395
396static LIST_HEAD(wacom_udev_list);
397static DEFINE_MUTEX(wacom_udev_list_lock);
398
399static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
400{
401 struct wacom_usbdev_data *data;
402
403 list_for_each_entry(data, &wacom_udev_list, list) {
404 if (data->dev == dev) {
405 kref_get(&data->kref);
406 return data;
407 }
408 }
409
410 return NULL;
411}
412
413static int wacom_add_shared_data(struct wacom_wac *wacom,
414 struct usb_device *dev)
415{
416 struct wacom_usbdev_data *data;
417 int retval = 0;
418
419 mutex_lock(&wacom_udev_list_lock);
420
421 data = wacom_get_usbdev_data(dev);
422 if (!data) {
423 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
424 if (!data) {
425 retval = -ENOMEM;
426 goto out;
427 }
428
429 kref_init(&data->kref);
430 data->dev = dev;
431 list_add_tail(&data->list, &wacom_udev_list);
432 }
433
434 wacom->shared = &data->shared;
435
436out:
437 mutex_unlock(&wacom_udev_list_lock);
438 return retval;
439}
440
441static void wacom_release_shared_data(struct kref *kref)
442{
443 struct wacom_usbdev_data *data =
444 container_of(kref, struct wacom_usbdev_data, kref);
445
446 mutex_lock(&wacom_udev_list_lock);
447 list_del(&data->list);
448 mutex_unlock(&wacom_udev_list_lock);
449
450 kfree(data);
451}
452
453static void wacom_remove_shared_data(struct wacom_wac *wacom)
454{
455 struct wacom_usbdev_data *data;
456
457 if (wacom->shared) {
458 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
459 kref_put(&data->kref, wacom_release_shared_data);
460 wacom->shared = NULL;
461 }
462}
463
3bea733a
PC
464static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
465{
466 struct usb_device *dev = interface_to_usbdev(intf);
467 struct usb_endpoint_descriptor *endpoint;
468 struct wacom *wacom;
469 struct wacom_wac *wacom_wac;
e33da8a5 470 struct wacom_features *features;
3bea733a 471 struct input_dev *input_dev;
e33da8a5 472 int error;
3bea733a 473
e33da8a5 474 if (!id->driver_info)
b036f6fb
BB
475 return -EINVAL;
476
3bea733a 477 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
3bea733a 478 input_dev = input_allocate_device();
51269fe8 479 if (!wacom || !input_dev) {
e33da8a5
JC
480 error = -ENOMEM;
481 goto fail1;
482 }
483
51269fe8 484 wacom_wac = &wacom->wacom_wac;
e33da8a5
JC
485 wacom_wac->features = *((struct wacom_features *)id->driver_info);
486 features = &wacom_wac->features;
487 if (features->pktlen > WACOM_PKGLEN_MAX) {
488 error = -EINVAL;
3bea733a 489 goto fail1;
e33da8a5 490 }
3bea733a 491
e33da8a5
JC
492 wacom_wac->data = usb_buffer_alloc(dev, WACOM_PKGLEN_MAX,
493 GFP_KERNEL, &wacom->data_dma);
494 if (!wacom_wac->data) {
495 error = -ENOMEM;
3bea733a 496 goto fail1;
e33da8a5 497 }
3bea733a
PC
498
499 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
e33da8a5
JC
500 if (!wacom->irq) {
501 error = -ENOMEM;
3bea733a 502 goto fail2;
e33da8a5 503 }
3bea733a
PC
504
505 wacom->usbdev = dev;
e7224094
ON
506 wacom->intf = intf;
507 mutex_init(&wacom->lock);
3bea733a
PC
508 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
509 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
510
8da23fc1 511 wacom_wac->input = input_dev;
3bea733a 512
545f4e99
PC
513 endpoint = &intf->cur_altsetting->endpoint[0].desc;
514
ec67bbed
PC
515 /* Retrieve the physical and logical size for OEM devices */
516 error = wacom_retrieve_hid_descriptor(intf, features);
517 if (error)
518 goto fail2;
545f4e99 519
49b764ae
PC
520 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
521
522 if (features->type == TABLETPC || features->type == TABLETPC2FG) {
523 /* Append the device type to the name */
524 strlcat(wacom_wac->name,
525 features->device_type == BTN_TOOL_PEN ?
526 " Pen" : " Finger",
527 sizeof(wacom_wac->name));
4492efff
PC
528
529 error = wacom_add_shared_data(wacom_wac, dev);
530 if (error)
531 goto fail3;
49b764ae
PC
532 }
533
534 input_dev->name = wacom_wac->name;
8da23fc1
DT
535 input_dev->name = wacom_wac->name;
536 input_dev->dev.parent = &intf->dev;
537 input_dev->open = wacom_open;
538 input_dev->close = wacom_close;
539 usb_to_input_id(dev, &input_dev->id);
540 input_set_drvdata(input_dev, wacom);
e33da8a5 541
8da23fc1 542 wacom_setup_input_capabilities(input_dev, wacom_wac);
3bea733a 543
3bea733a
PC
544 usb_fill_int_urb(wacom->irq, dev,
545 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
ec67bbed 546 wacom_wac->data, features->pktlen,
8d32e3ae 547 wacom_sys_irq, wacom, endpoint->bInterval);
3bea733a
PC
548 wacom->irq->transfer_dma = wacom->data_dma;
549 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
550
8da23fc1 551 error = input_register_device(input_dev);
5014186d 552 if (error)
4492efff 553 goto fail4;
3bea733a 554
ec67bbed
PC
555 /* Note that if query fails it is not a hard failure */
556 wacom_query_tablet_data(intf, features);
3bea733a
PC
557
558 usb_set_intfdata(intf, wacom);
559 return 0;
560
4492efff 561 fail4: wacom_remove_shared_data(wacom_wac);
5014186d 562 fail3: usb_free_urb(wacom->irq);
ec67bbed 563 fail2: usb_buffer_free(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
5014186d 564 fail1: input_free_device(input_dev);
3bea733a 565 kfree(wacom);
5014186d 566 return error;
3bea733a
PC
567}
568
569static void wacom_disconnect(struct usb_interface *intf)
570{
e7224094 571 struct wacom *wacom = usb_get_intfdata(intf);
3bea733a
PC
572
573 usb_set_intfdata(intf, NULL);
e7224094
ON
574
575 usb_kill_urb(wacom->irq);
8da23fc1 576 input_unregister_device(wacom->wacom_wac.input);
e7224094 577 usb_free_urb(wacom->irq);
ec67bbed 578 usb_buffer_free(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
51269fe8
DT
579 wacom->wacom_wac.data, wacom->data_dma);
580 wacom_remove_shared_data(&wacom->wacom_wac);
e7224094
ON
581 kfree(wacom);
582}
583
584static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
585{
586 struct wacom *wacom = usb_get_intfdata(intf);
587
588 mutex_lock(&wacom->lock);
589 usb_kill_urb(wacom->irq);
590 mutex_unlock(&wacom->lock);
591
592 return 0;
593}
594
595static int wacom_resume(struct usb_interface *intf)
596{
597 struct wacom *wacom = usb_get_intfdata(intf);
51269fe8 598 struct wacom_features *features = &wacom->wacom_wac.features;
e7224094
ON
599 int rv;
600
601 mutex_lock(&wacom->lock);
38101475
PC
602
603 /* switch to wacom mode first */
604 wacom_query_tablet_data(intf, features);
605
606 if (wacom->open)
e7224094 607 rv = usb_submit_urb(wacom->irq, GFP_NOIO);
38101475 608 else
e7224094 609 rv = 0;
38101475 610
e7224094
ON
611 mutex_unlock(&wacom->lock);
612
613 return rv;
614}
615
616static int wacom_reset_resume(struct usb_interface *intf)
617{
618 return wacom_resume(intf);
3bea733a
PC
619}
620
621static struct usb_driver wacom_driver = {
622 .name = "wacom",
b036f6fb 623 .id_table = wacom_ids,
3bea733a
PC
624 .probe = wacom_probe,
625 .disconnect = wacom_disconnect,
e7224094
ON
626 .suspend = wacom_suspend,
627 .resume = wacom_resume,
628 .reset_resume = wacom_reset_resume,
629 .supports_autosuspend = 1,
3bea733a
PC
630};
631
632static int __init wacom_init(void)
633{
634 int result;
b036f6fb 635
3bea733a
PC
636 result = usb_register(&wacom_driver);
637 if (result == 0)
899ef6e7
GKH
638 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
639 DRIVER_DESC "\n");
3bea733a
PC
640 return result;
641}
642
643static void __exit wacom_exit(void)
644{
645 usb_deregister(&wacom_driver);
646}
647
648module_init(wacom_init);
649module_exit(wacom_exit);