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.
14 #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
52 #define WAC_HID_FEATURE_REPORT 0x03
53 #define WAC_MSG_RETRIES 5
55 #define WAC_CMD_LED_CONTROL 0x20
56 #define WAC_CMD_ICON_START 0x21
57 #define WAC_CMD_ICON_XFER 0x23
58 #define WAC_CMD_RETRIES 10
60 static int wacom_get_report(struct usb_interface *intf, u8 type, u8 id,
61 void *buf, size_t size, unsigned int retries)
63 struct usb_device *dev = interface_to_usbdev(intf);
67 retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
69 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
71 intf->altsetting[0].desc.bInterfaceNumber,
73 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
78 static int wacom_set_report(struct usb_interface *intf, u8 type, u8 id,
79 void *buf, size_t size, unsigned int retries)
81 struct usb_device *dev = interface_to_usbdev(intf);
85 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
87 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
89 intf->altsetting[0].desc.bInterfaceNumber,
91 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
96 static void wacom_sys_irq(struct urb *urb)
98 struct wacom *wacom = urb->context;
101 switch (urb->status) {
108 /* this urb is terminated, clean up */
109 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
112 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
116 wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
119 usb_mark_last_busy(wacom->usbdev);
120 retval = usb_submit_urb(urb, GFP_ATOMIC);
122 err ("%s - usb_submit_urb failed with result %d",
126 static int wacom_open(struct input_dev *dev)
128 struct wacom *wacom = input_get_drvdata(dev);
131 if (usb_autopm_get_interface(wacom->intf) < 0)
134 mutex_lock(&wacom->lock);
136 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
142 wacom->intf->needs_remote_wakeup = 1;
145 mutex_unlock(&wacom->lock);
146 usb_autopm_put_interface(wacom->intf);
150 static void wacom_close(struct input_dev *dev)
152 struct wacom *wacom = input_get_drvdata(dev);
155 autopm_error = usb_autopm_get_interface(wacom->intf);
157 mutex_lock(&wacom->lock);
158 usb_kill_urb(wacom->irq);
160 wacom->intf->needs_remote_wakeup = 0;
161 mutex_unlock(&wacom->lock);
164 usb_autopm_put_interface(wacom->intf);
167 static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
168 struct wacom_features *features)
170 struct usb_device *dev = interface_to_usbdev(intf);
172 /* result has to be defined as int for some devices */
174 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
175 unsigned char *report;
177 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
181 /* retrive report descriptors */
183 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
184 USB_REQ_GET_DESCRIPTOR,
185 USB_RECIP_INTERFACE | USB_DIR_IN,
186 HID_DEVICET_REPORT << 8,
187 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
189 hid_desc->wDescriptorLength,
191 } while (result < 0 && limit++ < WAC_MSG_RETRIES);
193 /* No need to parse the Descriptor. It isn't an error though */
197 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
201 switch (report[i + 1]) {
202 case HID_USAGE_PAGE_DIGITIZER:
203 usage = WCM_DIGITIZER;
207 case HID_USAGE_PAGE_DESKTOP:
215 switch (report[i + 1]) {
217 if (usage == WCM_DESKTOP) {
219 features->device_type = BTN_TOOL_FINGER;
220 if (features->type == TABLETPC2FG) {
221 /* need to reset back */
222 features->pktlen = WACOM_PKGLEN_TPC2FG;
223 features->device_type = BTN_TOOL_DOUBLETAP;
225 if (features->type == BAMBOO_PT) {
226 /* need to reset back */
227 features->pktlen = WACOM_PKGLEN_BBTOUCH;
228 features->device_type = BTN_TOOL_DOUBLETAP;
230 get_unaligned_le16(&report[i + 5]);
232 get_unaligned_le16(&report[i + 8]);
236 get_unaligned_le16(&report[i + 3]);
238 get_unaligned_le16(&report[i + 6]);
239 features->unit = report[i + 9];
240 features->unitExpo = report[i + 11];
244 /* penabled only accepts exact bytes of data */
245 if (features->type == TABLETPC2FG)
246 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
247 if (features->type == BAMBOO_PT)
248 features->pktlen = WACOM_PKGLEN_BBFUN;
249 features->device_type = BTN_TOOL_PEN;
251 get_unaligned_le16(&report[i + 3]);
254 } else if (usage == WCM_DIGITIZER) {
255 /* max pressure isn't reported
256 features->pressure_max = (unsigned short)
257 (report[i+4] << 8 | report[i + 3]);
259 features->pressure_max = 255;
265 if (usage == WCM_DESKTOP) {
267 features->device_type = BTN_TOOL_FINGER;
268 if (features->type == TABLETPC2FG) {
269 /* need to reset back */
270 features->pktlen = WACOM_PKGLEN_TPC2FG;
271 features->device_type = BTN_TOOL_DOUBLETAP;
273 get_unaligned_le16(&report[i + 3]);
275 get_unaligned_le16(&report[i + 6]);
277 } else if (features->type == BAMBOO_PT) {
278 /* need to reset back */
279 features->pktlen = WACOM_PKGLEN_BBTOUCH;
280 features->device_type = BTN_TOOL_DOUBLETAP;
282 get_unaligned_le16(&report[i + 3]);
284 get_unaligned_le16(&report[i + 6]);
290 get_unaligned_le16(&report[i + 3]);
294 /* penabled only accepts exact bytes of data */
295 if (features->type == TABLETPC2FG)
296 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
297 if (features->type == BAMBOO_PT)
298 features->pktlen = WACOM_PKGLEN_BBFUN;
299 features->device_type = BTN_TOOL_PEN;
301 get_unaligned_le16(&report[i + 3]);
307 case HID_USAGE_FINGER:
312 case HID_USAGE_STYLUS:
317 case HID_USAGE_UNDEFINED:
318 if (usage == WCM_DESKTOP && finger) /* capacity */
319 features->pressure_max =
320 get_unaligned_le16(&report[i + 3]);
327 /* reset UsagePage and Finger */
339 static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
341 unsigned char *rep_data;
342 int limit = 0, report_id = 2;
345 rep_data = kmalloc(4, GFP_KERNEL);
349 /* ask to report tablet data if it is MT Tablet PC or
351 if (features->type == TABLETPC2FG) {
358 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
359 report_id, rep_data, 4, 1);
361 error = wacom_get_report(intf,
362 WAC_HID_FEATURE_REPORT,
363 report_id, rep_data, 4, 1);
364 } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
365 } else if (features->type != TABLETPC) {
369 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
370 report_id, rep_data, 2, 1);
372 error = wacom_get_report(intf,
373 WAC_HID_FEATURE_REPORT,
374 report_id, rep_data, 2, 1);
375 } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
380 return error < 0 ? error : 0;
383 static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
384 struct wacom_features *features)
387 struct usb_host_interface *interface = intf->cur_altsetting;
388 struct hid_descriptor *hid_desc;
390 /* default features */
391 features->device_type = BTN_TOOL_PEN;
392 features->x_fuzz = 4;
393 features->y_fuzz = 4;
394 features->pressure_fuzz = 0;
395 features->distance_fuzz = 0;
397 /* only Tablet PCs and Bamboo P&T need to retrieve the info */
398 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
399 (features->type != BAMBOO_PT))
402 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
403 if (usb_get_extra_descriptor(&interface->endpoint[0],
404 HID_DEVICET_REPORT, &hid_desc)) {
405 printk("wacom: can not retrieve extra class descriptor\n");
410 error = wacom_parse_hid(intf, hid_desc, features);
418 struct wacom_usbdev_data {
419 struct list_head list;
421 struct usb_device *dev;
422 struct wacom_shared shared;
425 static LIST_HEAD(wacom_udev_list);
426 static DEFINE_MUTEX(wacom_udev_list_lock);
428 static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
430 struct wacom_usbdev_data *data;
432 list_for_each_entry(data, &wacom_udev_list, list) {
433 if (data->dev == dev) {
434 kref_get(&data->kref);
442 static int wacom_add_shared_data(struct wacom_wac *wacom,
443 struct usb_device *dev)
445 struct wacom_usbdev_data *data;
448 mutex_lock(&wacom_udev_list_lock);
450 data = wacom_get_usbdev_data(dev);
452 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
458 kref_init(&data->kref);
460 list_add_tail(&data->list, &wacom_udev_list);
463 wacom->shared = &data->shared;
466 mutex_unlock(&wacom_udev_list_lock);
470 static void wacom_release_shared_data(struct kref *kref)
472 struct wacom_usbdev_data *data =
473 container_of(kref, struct wacom_usbdev_data, kref);
475 mutex_lock(&wacom_udev_list_lock);
476 list_del(&data->list);
477 mutex_unlock(&wacom_udev_list_lock);
482 static void wacom_remove_shared_data(struct wacom_wac *wacom)
484 struct wacom_usbdev_data *data;
487 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
488 kref_put(&data->kref, wacom_release_shared_data);
489 wacom->shared = NULL;
493 static int wacom_led_control(struct wacom *wacom)
498 buf = kzalloc(9, GFP_KERNEL);
502 buf[0] = WAC_CMD_LED_CONTROL;
503 buf[1] = wacom->led.select >= 0 ? wacom->led.select | 4 : 0;
504 buf[2] = wacom->led.llv;
505 buf[3] = wacom->led.hlv;
506 buf[4] = wacom->led.img_lum;
508 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
509 buf, 9, WAC_CMD_RETRIES);
515 static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
520 buf = kzalloc(259, GFP_KERNEL);
524 /* Send 'start' command */
525 buf[0] = WAC_CMD_ICON_START;
527 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
528 buf, 2, WAC_CMD_RETRIES);
532 buf[0] = WAC_CMD_ICON_XFER;
533 buf[1] = button_id & 0x07;
534 for (i = 0; i < 4; i++) {
536 memcpy(buf + 3, img + i * 256, 256);
538 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
539 buf, 259, WAC_CMD_RETRIES);
545 buf[0] = WAC_CMD_ICON_START;
547 wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
548 buf, 2, WAC_CMD_RETRIES);
555 static ssize_t wacom_led_select_store(struct device *dev,
556 struct device_attribute *attr,
557 const char *buf, size_t count)
559 struct wacom *wacom = dev_get_drvdata(dev);
563 err = kstrtouint(buf, 10, &id);
567 mutex_lock(&wacom->lock);
569 wacom->led.select = id;
570 err = wacom_led_control(wacom);
572 mutex_unlock(&wacom->lock);
574 return err < 0 ? err : count;
577 static DEVICE_ATTR(status_led_select, S_IWUSR, NULL, wacom_led_select_store);
579 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
580 const char *buf, size_t count)
585 err = kstrtouint(buf, 10, &value);
589 mutex_lock(&wacom->lock);
591 *dest = value & 0x7f;
592 err = wacom_led_control(wacom);
594 mutex_unlock(&wacom->lock);
596 return err < 0 ? err : count;
599 #define DEVICE_LUMINANCE_ATTR(name, field) \
600 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
601 struct device_attribute *attr, const char *buf, size_t count) \
603 struct wacom *wacom = dev_get_drvdata(dev); \
605 return wacom_luminance_store(wacom, &wacom->led.field, \
608 static DEVICE_ATTR(name##_luminance, S_IWUSR, \
609 NULL, wacom_##name##_luminance_store)
611 DEVICE_LUMINANCE_ATTR(status0, llv);
612 DEVICE_LUMINANCE_ATTR(status1, hlv);
613 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
615 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
616 const char *buf, size_t count)
618 struct wacom *wacom = dev_get_drvdata(dev);
624 mutex_lock(&wacom->lock);
626 err = wacom_led_putimage(wacom, button_id, buf);
628 mutex_unlock(&wacom->lock);
630 return err < 0 ? err : count;
633 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
634 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
635 struct device_attribute *attr, const char *buf, size_t count) \
637 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
639 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
640 NULL, wacom_btnimg##BUTTON_ID##_store)
642 DEVICE_BTNIMG_ATTR(0);
643 DEVICE_BTNIMG_ATTR(1);
644 DEVICE_BTNIMG_ATTR(2);
645 DEVICE_BTNIMG_ATTR(3);
646 DEVICE_BTNIMG_ATTR(4);
647 DEVICE_BTNIMG_ATTR(5);
648 DEVICE_BTNIMG_ATTR(6);
649 DEVICE_BTNIMG_ATTR(7);
651 static struct attribute *wacom_led_attrs[] = {
652 &dev_attr_status0_luminance.attr,
653 &dev_attr_status1_luminance.attr,
654 &dev_attr_status_led_select.attr,
655 &dev_attr_buttons_luminance.attr,
656 &dev_attr_button0_rawimg.attr,
657 &dev_attr_button1_rawimg.attr,
658 &dev_attr_button2_rawimg.attr,
659 &dev_attr_button3_rawimg.attr,
660 &dev_attr_button4_rawimg.attr,
661 &dev_attr_button5_rawimg.attr,
662 &dev_attr_button6_rawimg.attr,
663 &dev_attr_button7_rawimg.attr,
667 static struct attribute_group wacom_led_attr_group = {
669 .attrs = wacom_led_attrs,
672 static int wacom_initialize_leds(struct wacom *wacom)
676 if (wacom->wacom_wac.features.type >= INTUOS4 &&
677 wacom->wacom_wac.features.type <= INTUOS4L) {
679 /* Initialize default values */
680 wacom->led.select = 0;
683 wacom->led.img_lum = 10;
684 wacom_led_control(wacom);
686 error = sysfs_create_group(&wacom->intf->dev.kobj,
687 &wacom_led_attr_group);
689 dev_err(&wacom->intf->dev,
690 "cannot create sysfs group err: %d\n", error);
698 static void wacom_destroy_leds(struct wacom *wacom)
700 if (wacom->wacom_wac.features.type >= INTUOS4 &&
701 wacom->wacom_wac.features.type <= INTUOS4L) {
702 sysfs_remove_group(&wacom->intf->dev.kobj,
703 &wacom_led_attr_group);
707 static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
709 struct usb_device *dev = interface_to_usbdev(intf);
710 struct usb_endpoint_descriptor *endpoint;
712 struct wacom_wac *wacom_wac;
713 struct wacom_features *features;
714 struct input_dev *input_dev;
717 if (!id->driver_info)
720 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
721 input_dev = input_allocate_device();
722 if (!wacom || !input_dev) {
727 wacom_wac = &wacom->wacom_wac;
728 wacom_wac->features = *((struct wacom_features *)id->driver_info);
729 features = &wacom_wac->features;
730 if (features->pktlen > WACOM_PKGLEN_MAX) {
735 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
736 GFP_KERNEL, &wacom->data_dma);
737 if (!wacom_wac->data) {
742 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
750 mutex_init(&wacom->lock);
751 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
752 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
754 wacom_wac->input = input_dev;
756 endpoint = &intf->cur_altsetting->endpoint[0].desc;
758 /* Retrieve the physical and logical size for OEM devices */
759 error = wacom_retrieve_hid_descriptor(intf, features);
763 wacom_setup_device_quirks(features);
765 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
767 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
768 /* Append the device type to the name */
769 strlcat(wacom_wac->name,
770 features->device_type == BTN_TOOL_PEN ?
772 sizeof(wacom_wac->name));
774 error = wacom_add_shared_data(wacom_wac, dev);
779 input_dev->name = wacom_wac->name;
780 input_dev->dev.parent = &intf->dev;
781 input_dev->open = wacom_open;
782 input_dev->close = wacom_close;
783 usb_to_input_id(dev, &input_dev->id);
784 input_set_drvdata(input_dev, wacom);
786 wacom_setup_input_capabilities(input_dev, wacom_wac);
788 usb_fill_int_urb(wacom->irq, dev,
789 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
790 wacom_wac->data, features->pktlen,
791 wacom_sys_irq, wacom, endpoint->bInterval);
792 wacom->irq->transfer_dma = wacom->data_dma;
793 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
795 error = wacom_initialize_leds(wacom);
799 error = input_register_device(input_dev);
803 /* Note that if query fails it is not a hard failure */
804 wacom_query_tablet_data(intf, features);
806 usb_set_intfdata(intf, wacom);
809 fail5: wacom_destroy_leds(wacom);
810 fail4: wacom_remove_shared_data(wacom_wac);
811 fail3: usb_free_urb(wacom->irq);
812 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
813 fail1: input_free_device(input_dev);
818 static void wacom_disconnect(struct usb_interface *intf)
820 struct wacom *wacom = usb_get_intfdata(intf);
822 usb_set_intfdata(intf, NULL);
824 usb_kill_urb(wacom->irq);
825 input_unregister_device(wacom->wacom_wac.input);
826 wacom_destroy_leds(wacom);
827 usb_free_urb(wacom->irq);
828 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
829 wacom->wacom_wac.data, wacom->data_dma);
830 wacom_remove_shared_data(&wacom->wacom_wac);
834 static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
836 struct wacom *wacom = usb_get_intfdata(intf);
838 mutex_lock(&wacom->lock);
839 usb_kill_urb(wacom->irq);
840 mutex_unlock(&wacom->lock);
845 static int wacom_resume(struct usb_interface *intf)
847 struct wacom *wacom = usb_get_intfdata(intf);
848 struct wacom_features *features = &wacom->wacom_wac.features;
851 mutex_lock(&wacom->lock);
853 /* switch to wacom mode first */
854 wacom_query_tablet_data(intf, features);
855 wacom_led_control(wacom);
857 if (wacom->open && usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
860 mutex_unlock(&wacom->lock);
865 static int wacom_reset_resume(struct usb_interface *intf)
867 return wacom_resume(intf);
870 static struct usb_driver wacom_driver = {
872 .id_table = wacom_ids,
873 .probe = wacom_probe,
874 .disconnect = wacom_disconnect,
875 .suspend = wacom_suspend,
876 .resume = wacom_resume,
877 .reset_resume = wacom_reset_resume,
878 .supports_autosuspend = 1,
881 static int __init wacom_init(void)
885 result = usb_register(&wacom_driver);
887 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
892 static void __exit wacom_exit(void)
894 usb_deregister(&wacom_driver);
897 module_init(wacom_init);
898 module_exit(wacom_exit);