2 * drivers/input/tablet/wacom_sys.c
4 * USB Wacom tablet support - system specific code
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.
15 #include "wacom_wac.h"
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
39 struct hid_descriptor {
40 struct usb_descriptor_header header;
45 __le16 wDescriptorLength;
46 } __attribute__ ((packed));
48 /* defines to get/set USB message */
49 #define USB_REQ_GET_REPORT 0x01
50 #define USB_REQ_SET_REPORT 0x09
51 #define WAC_HID_FEATURE_REPORT 0x03
53 static int usb_get_report(struct usb_interface *intf, unsigned char type,
54 unsigned char id, void *buf, int size)
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,
63 static int usb_set_report(struct usb_interface *intf, unsigned char type,
64 unsigned char id, void *buf, int size)
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,
73 static struct input_dev * get_input_dev(struct wacom_combo *wcombo)
75 return wcombo->wacom->dev;
78 static void wacom_sys_irq(struct urb *urb)
80 struct wacom *wacom = urb->context;
81 struct wacom_combo wcombo;
84 switch (urb->status) {
91 /* this urb is terminated, clean up */
92 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
95 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
102 if (wacom_wac_irq(wacom->wacom_wac, (void *)&wcombo))
103 input_sync(get_input_dev(&wcombo));
106 usb_mark_last_busy(wacom->usbdev);
107 retval = usb_submit_urb (urb, GFP_ATOMIC);
109 err ("%s - usb_submit_urb failed with result %d",
113 void wacom_report_key(void *wcombo, unsigned int key_type, int key_data)
115 input_report_key(get_input_dev((struct wacom_combo *)wcombo), key_type, key_data);
118 void wacom_report_abs(void *wcombo, unsigned int abs_type, int abs_data)
120 input_report_abs(get_input_dev((struct wacom_combo *)wcombo), abs_type, abs_data);
123 void wacom_report_rel(void *wcombo, unsigned int rel_type, int rel_data)
125 input_report_rel(get_input_dev((struct wacom_combo *)wcombo), rel_type, rel_data);
128 void wacom_input_event(void *wcombo, unsigned int type, unsigned int code, int value)
130 input_event(get_input_dev((struct wacom_combo *)wcombo), type, code, value);
133 __u16 wacom_be16_to_cpu(unsigned char *data)
136 value = be16_to_cpu(*(__be16 *) data);
140 __u16 wacom_le16_to_cpu(unsigned char *data)
143 value = le16_to_cpu(*(__le16 *) data);
147 void wacom_input_sync(void *wcombo)
149 input_sync(get_input_dev((struct wacom_combo *)wcombo));
152 static int wacom_open(struct input_dev *dev)
154 struct wacom *wacom = input_get_drvdata(dev);
156 mutex_lock(&wacom->lock);
158 wacom->irq->dev = wacom->usbdev;
160 if (usb_autopm_get_interface(wacom->intf) < 0) {
161 mutex_unlock(&wacom->lock);
165 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
166 usb_autopm_put_interface(wacom->intf);
167 mutex_unlock(&wacom->lock);
172 wacom->intf->needs_remote_wakeup = 1;
174 mutex_unlock(&wacom->lock);
178 static void wacom_close(struct input_dev *dev)
180 struct wacom *wacom = input_get_drvdata(dev);
182 mutex_lock(&wacom->lock);
183 usb_kill_urb(wacom->irq);
185 wacom->intf->needs_remote_wakeup = 0;
186 mutex_unlock(&wacom->lock);
189 void input_dev_mo(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
191 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_1) |
193 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
196 void input_dev_g4(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
198 input_dev->evbit[0] |= BIT_MASK(EV_MSC);
199 input_dev->mscbit[0] |= BIT_MASK(MSC_SERIAL);
200 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_FINGER);
201 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_0) |
205 void input_dev_g(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
207 input_dev->evbit[0] |= BIT_MASK(EV_REL);
208 input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);
209 input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_LEFT) |
210 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
211 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_RUBBER) |
212 BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_STYLUS) |
213 BIT_MASK(BTN_TOOL_MOUSE) | BIT_MASK(BTN_STYLUS2);
214 input_set_abs_params(input_dev, ABS_DISTANCE, 0, wacom_wac->features->distance_max, 0, 0);
217 void input_dev_i3s(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
219 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_FINGER);
220 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_0) |
221 BIT_MASK(BTN_1) | BIT_MASK(BTN_2) | BIT_MASK(BTN_3);
222 input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
223 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
226 void input_dev_i3(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
228 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_4) |
229 BIT_MASK(BTN_5) | BIT_MASK(BTN_6) | BIT_MASK(BTN_7);
230 input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
233 void input_dev_i4s(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
235 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_FINGER);
236 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_0) | BIT_MASK(BTN_1) | BIT_MASK(BTN_2) | BIT_MASK(BTN_3);
237 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_4) | BIT_MASK(BTN_5) | BIT_MASK(BTN_6);
238 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
241 void input_dev_i4(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
243 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_7) | BIT_MASK(BTN_8);
246 void input_dev_bee(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
248 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_8) | BIT_MASK(BTN_9);
251 void input_dev_i(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
253 input_dev->evbit[0] |= BIT_MASK(EV_MSC) | BIT_MASK(EV_REL);
254 input_dev->mscbit[0] |= BIT_MASK(MSC_SERIAL);
255 input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);
256 input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_LEFT) |
257 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE) |
258 BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
259 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_RUBBER) |
260 BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_STYLUS) |
261 BIT_MASK(BTN_TOOL_MOUSE) | BIT_MASK(BTN_TOOL_BRUSH) |
262 BIT_MASK(BTN_TOOL_PENCIL) | BIT_MASK(BTN_TOOL_AIRBRUSH) |
263 BIT_MASK(BTN_TOOL_LENS) | BIT_MASK(BTN_STYLUS2);
264 input_set_abs_params(input_dev, ABS_DISTANCE, 0, wacom_wac->features->distance_max, 0, 0);
265 input_set_abs_params(input_dev, ABS_WHEEL, 0, 1023, 0, 0);
266 input_set_abs_params(input_dev, ABS_TILT_X, 0, 127, 0, 0);
267 input_set_abs_params(input_dev, ABS_TILT_Y, 0, 127, 0, 0);
268 input_set_abs_params(input_dev, ABS_RZ, -900, 899, 0, 0);
269 input_set_abs_params(input_dev, ABS_THROTTLE, -1023, 1023, 0, 0);
272 void input_dev_pl(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
274 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_PEN) |
275 BIT_MASK(BTN_STYLUS) | BIT_MASK(BTN_STYLUS2);
278 void input_dev_pt(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
280 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_RUBBER);
283 void input_dev_tpc(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
285 if (wacom_wac->features->device_type == BTN_TOOL_DOUBLETAP ||
286 wacom_wac->features->device_type == BTN_TOOL_TRIPLETAP) {
287 input_set_abs_params(input_dev, ABS_RX, 0, wacom_wac->features->x_phy, 0, 0);
288 input_set_abs_params(input_dev, ABS_RY, 0, wacom_wac->features->y_phy, 0, 0);
289 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_DOUBLETAP);
293 void input_dev_tpc2fg(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
295 if (wacom_wac->features->device_type == BTN_TOOL_TRIPLETAP) {
296 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_TRIPLETAP);
297 input_dev->evbit[0] |= BIT_MASK(EV_MSC);
298 input_dev->mscbit[0] |= BIT_MASK(MSC_SERIAL);
302 static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
303 struct wacom_features *features)
305 struct usb_device *dev = interface_to_usbdev(intf);
307 /* result has to be defined as int for some devices */
309 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
310 unsigned char *report;
312 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
316 /* retrive report descriptors */
318 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
319 USB_REQ_GET_DESCRIPTOR,
320 USB_RECIP_INTERFACE | USB_DIR_IN,
321 HID_DEVICET_REPORT << 8,
322 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
324 hid_desc->wDescriptorLength,
326 } while (result < 0 && limit++ < 5);
328 /* No need to parse the Descriptor. It isn't an error though */
332 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
336 switch (report[i + 1]) {
337 case HID_USAGE_PAGE_DIGITIZER:
338 usage = WCM_DIGITIZER;
342 case HID_USAGE_PAGE_DESKTOP:
350 switch (report[i + 1]) {
352 if (usage == WCM_DESKTOP) {
354 features->device_type = BTN_TOOL_DOUBLETAP;
355 if (features->type == TABLETPC2FG) {
356 /* need to reset back */
357 features->pktlen = WACOM_PKGLEN_TPC2FG;
358 features->device_type = BTN_TOOL_TRIPLETAP;
361 wacom_le16_to_cpu(&report[i + 3]);
363 wacom_le16_to_cpu(&report[i + 6]);
364 features->unit = report[i + 9];
365 features->unitExpo = report[i + 11];
368 /* penabled only accepts exact bytes of data */
369 if (features->type == TABLETPC2FG)
370 features->pktlen = WACOM_PKGLEN_PENABLED;
371 features->device_type = BTN_TOOL_PEN;
373 wacom_le16_to_cpu(&report[i + 3]);
376 } else if (usage == WCM_DIGITIZER) {
377 /* max pressure isn't reported
378 features->pressure_max = (unsigned short)
379 (report[i+4] << 8 | report[i + 3]);
381 features->pressure_max = 255;
387 if (usage == WCM_DESKTOP) {
389 features->device_type = BTN_TOOL_DOUBLETAP;
390 if (features->type == TABLETPC2FG) {
391 /* need to reset back */
392 features->pktlen = WACOM_PKGLEN_TPC2FG;
393 features->device_type = BTN_TOOL_TRIPLETAP;
395 wacom_le16_to_cpu(&report[i + 3]);
397 wacom_le16_to_cpu(&report[i + 6]);
403 wacom_le16_to_cpu(&report[i + 3]);
407 /* penabled only accepts exact bytes of data */
408 if (features->type == TABLETPC2FG)
409 features->pktlen = WACOM_PKGLEN_PENABLED;
410 features->device_type = BTN_TOOL_PEN;
412 wacom_le16_to_cpu(&report[i + 3]);
418 case HID_USAGE_FINGER:
423 case HID_USAGE_STYLUS:
428 case HID_USAGE_UNDEFINED:
429 if (usage == WCM_DESKTOP && finger) /* capacity */
430 features->pressure_max =
431 wacom_le16_to_cpu(&report[i + 3]);
438 /* reset UsagePage and Finger */
450 static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
452 unsigned char *rep_data;
453 int limit = 0, report_id = 2;
456 rep_data = kmalloc(2, GFP_KERNEL);
460 /* ask to report tablet data if it is 2FGT or not a Tablet PC */
461 if (features->device_type == BTN_TOOL_TRIPLETAP) {
466 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
467 report_id, rep_data, 2);
469 error = usb_get_report(intf,
470 WAC_HID_FEATURE_REPORT, report_id,
472 } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
473 } else if (features->type != TABLETPC && features->type != TABLETPC2FG) {
477 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
478 report_id, rep_data, 2);
480 error = usb_get_report(intf,
481 WAC_HID_FEATURE_REPORT, report_id,
483 } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
488 return error < 0 ? error : 0;
491 static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
492 struct wacom_features *features)
495 struct usb_host_interface *interface = intf->cur_altsetting;
496 struct hid_descriptor *hid_desc;
498 /* default device to penabled */
499 features->device_type = BTN_TOOL_PEN;
501 /* only Tablet PCs need to retrieve the info */
502 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG))
505 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
506 if (usb_get_extra_descriptor(&interface->endpoint[0],
507 HID_DEVICET_REPORT, &hid_desc)) {
508 printk("wacom: can not retrieve extra class descriptor\n");
513 error = wacom_parse_hid(intf, hid_desc, features);
517 /* touch device found but size is not defined. use default */
518 if (features->device_type == BTN_TOOL_DOUBLETAP && !features->x_max) {
519 features->x_max = 1023;
520 features->y_max = 1023;
527 static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
529 struct usb_device *dev = interface_to_usbdev(intf);
530 struct usb_endpoint_descriptor *endpoint;
532 struct wacom_wac *wacom_wac;
533 struct wacom_features *features;
534 struct input_dev *input_dev;
537 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
538 wacom_wac = kzalloc(sizeof(struct wacom_wac), GFP_KERNEL);
539 input_dev = input_allocate_device();
540 if (!wacom || !input_dev || !wacom_wac)
543 wacom_wac->data = usb_buffer_alloc(dev, WACOM_PKGLEN_MAX, GFP_KERNEL, &wacom->data_dma);
544 if (!wacom_wac->data)
547 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
552 wacom->dev = input_dev;
554 mutex_init(&wacom->lock);
555 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
556 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
558 wacom_wac->features = features = get_wacom_feature(id);
559 BUG_ON(features->pktlen > WACOM_PKGLEN_MAX);
561 input_dev->name = wacom_wac->features->name;
562 wacom->wacom_wac = wacom_wac;
563 usb_to_input_id(dev, &input_dev->id);
565 input_dev->dev.parent = &intf->dev;
567 input_set_drvdata(input_dev, wacom);
569 input_dev->open = wacom_open;
570 input_dev->close = wacom_close;
572 endpoint = &intf->cur_altsetting->endpoint[0].desc;
574 /* Retrieve the physical and logical size for OEM devices */
575 error = wacom_retrieve_hid_descriptor(intf, features);
579 input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
580 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOUCH);
582 input_set_abs_params(input_dev, ABS_X, 0, features->x_max, 4, 0);
583 input_set_abs_params(input_dev, ABS_Y, 0, features->y_max, 4, 0);
584 input_set_abs_params(input_dev, ABS_PRESSURE, 0, features->pressure_max, 0, 0);
585 input_dev->absbit[BIT_WORD(ABS_MISC)] |= BIT_MASK(ABS_MISC);
587 wacom_init_input_dev(input_dev, wacom_wac);
589 usb_fill_int_urb(wacom->irq, dev,
590 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
591 wacom_wac->data, features->pktlen,
592 wacom_sys_irq, wacom, endpoint->bInterval);
593 wacom->irq->transfer_dma = wacom->data_dma;
594 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
596 error = input_register_device(wacom->dev);
600 /* Note that if query fails it is not a hard failure */
601 wacom_query_tablet_data(intf, features);
603 usb_set_intfdata(intf, wacom);
606 fail3: usb_free_urb(wacom->irq);
607 fail2: usb_buffer_free(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
608 fail1: input_free_device(input_dev);
614 static void wacom_disconnect(struct usb_interface *intf)
616 struct wacom *wacom = usb_get_intfdata(intf);
618 usb_set_intfdata(intf, NULL);
620 usb_kill_urb(wacom->irq);
621 input_unregister_device(wacom->dev);
622 usb_free_urb(wacom->irq);
623 usb_buffer_free(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
624 wacom->wacom_wac->data, wacom->data_dma);
625 kfree(wacom->wacom_wac);
629 static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
631 struct wacom *wacom = usb_get_intfdata(intf);
633 mutex_lock(&wacom->lock);
634 usb_kill_urb(wacom->irq);
635 mutex_unlock(&wacom->lock);
640 static int wacom_resume(struct usb_interface *intf)
642 struct wacom *wacom = usb_get_intfdata(intf);
643 struct wacom_features *features = wacom->wacom_wac->features;
646 mutex_lock(&wacom->lock);
648 rv = usb_submit_urb(wacom->irq, GFP_NOIO);
649 /* switch to wacom mode if needed */
650 if (!wacom_retrieve_hid_descriptor(intf, features))
651 wacom_query_tablet_data(intf, features);
654 mutex_unlock(&wacom->lock);
659 static int wacom_reset_resume(struct usb_interface *intf)
661 return wacom_resume(intf);
664 static struct usb_driver wacom_driver = {
666 .probe = wacom_probe,
667 .disconnect = wacom_disconnect,
668 .suspend = wacom_suspend,
669 .resume = wacom_resume,
670 .reset_resume = wacom_reset_resume,
671 .supports_autosuspend = 1,
674 static int __init wacom_init(void)
677 wacom_driver.id_table = get_device_table();
678 result = usb_register(&wacom_driver);
680 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
685 static void __exit wacom_exit(void)
687 usb_deregister(&wacom_driver);
690 module_init(wacom_init);
691 module_exit(wacom_exit);