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
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 void wacom_sys_irq(struct urb *urb)
75 struct wacom *wacom = urb->context;
78 switch (urb->status) {
85 /* this urb is terminated, clean up */
86 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
89 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
93 wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
96 usb_mark_last_busy(wacom->usbdev);
97 retval = usb_submit_urb(urb, GFP_ATOMIC);
99 err ("%s - usb_submit_urb failed with result %d",
103 static int wacom_open(struct input_dev *dev)
105 struct wacom *wacom = input_get_drvdata(dev);
108 if (usb_autopm_get_interface(wacom->intf) < 0)
111 mutex_lock(&wacom->lock);
113 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
119 wacom->intf->needs_remote_wakeup = 1;
122 mutex_unlock(&wacom->lock);
123 usb_autopm_put_interface(wacom->intf);
127 static void wacom_close(struct input_dev *dev)
129 struct wacom *wacom = input_get_drvdata(dev);
132 autopm_error = usb_autopm_get_interface(wacom->intf);
134 mutex_lock(&wacom->lock);
135 usb_kill_urb(wacom->irq);
137 wacom->intf->needs_remote_wakeup = 0;
138 mutex_unlock(&wacom->lock);
141 usb_autopm_put_interface(wacom->intf);
144 static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
145 struct wacom_features *features)
147 struct usb_device *dev = interface_to_usbdev(intf);
149 /* result has to be defined as int for some devices */
151 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
152 unsigned char *report;
154 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
158 /* retrive report descriptors */
160 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
161 USB_REQ_GET_DESCRIPTOR,
162 USB_RECIP_INTERFACE | USB_DIR_IN,
163 HID_DEVICET_REPORT << 8,
164 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
166 hid_desc->wDescriptorLength,
168 } while (result < 0 && limit++ < 5);
170 /* No need to parse the Descriptor. It isn't an error though */
174 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
178 switch (report[i + 1]) {
179 case HID_USAGE_PAGE_DIGITIZER:
180 usage = WCM_DIGITIZER;
184 case HID_USAGE_PAGE_DESKTOP:
192 switch (report[i + 1]) {
194 if (usage == WCM_DESKTOP) {
196 features->device_type = BTN_TOOL_DOUBLETAP;
197 if (features->type == TABLETPC2FG) {
198 /* need to reset back */
199 features->pktlen = WACOM_PKGLEN_TPC2FG;
200 features->device_type = BTN_TOOL_TRIPLETAP;
202 if (features->type == BAMBOO_PT) {
203 /* need to reset back */
204 features->pktlen = WACOM_PKGLEN_BBTOUCH;
205 features->device_type = BTN_TOOL_TRIPLETAP;
207 get_unaligned_le16(&report[i + 5]);
209 get_unaligned_le16(&report[i + 8]);
213 get_unaligned_le16(&report[i + 3]);
215 get_unaligned_le16(&report[i + 6]);
216 features->unit = report[i + 9];
217 features->unitExpo = report[i + 11];
221 /* penabled only accepts exact bytes of data */
222 if (features->type == TABLETPC2FG)
223 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
224 if (features->type == BAMBOO_PT)
225 features->pktlen = WACOM_PKGLEN_BBFUN;
226 features->device_type = BTN_TOOL_PEN;
228 get_unaligned_le16(&report[i + 3]);
231 } else if (usage == WCM_DIGITIZER) {
232 /* max pressure isn't reported
233 features->pressure_max = (unsigned short)
234 (report[i+4] << 8 | report[i + 3]);
236 features->pressure_max = 255;
242 if (usage == WCM_DESKTOP) {
244 features->device_type = BTN_TOOL_DOUBLETAP;
245 if (features->type == TABLETPC2FG) {
246 /* need to reset back */
247 features->pktlen = WACOM_PKGLEN_TPC2FG;
248 features->device_type = BTN_TOOL_TRIPLETAP;
250 get_unaligned_le16(&report[i + 3]);
252 get_unaligned_le16(&report[i + 6]);
254 } else if (features->type == BAMBOO_PT) {
255 /* need to reset back */
256 features->pktlen = WACOM_PKGLEN_BBTOUCH;
257 features->device_type = BTN_TOOL_TRIPLETAP;
259 get_unaligned_le16(&report[i + 3]);
261 get_unaligned_le16(&report[i + 6]);
267 get_unaligned_le16(&report[i + 3]);
271 /* penabled only accepts exact bytes of data */
272 if (features->type == TABLETPC2FG)
273 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
274 if (features->type == BAMBOO_PT)
275 features->pktlen = WACOM_PKGLEN_BBFUN;
276 features->device_type = BTN_TOOL_PEN;
278 get_unaligned_le16(&report[i + 3]);
284 case HID_USAGE_FINGER:
289 case HID_USAGE_STYLUS:
294 case HID_USAGE_UNDEFINED:
295 if (usage == WCM_DESKTOP && finger) /* capacity */
296 features->pressure_max =
297 get_unaligned_le16(&report[i + 3]);
304 /* reset UsagePage and Finger */
316 static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
318 unsigned char *rep_data;
319 int limit = 0, report_id = 2;
322 rep_data = kmalloc(2, GFP_KERNEL);
326 /* ask to report tablet data if it is 2FGT Tablet PC or
328 if (features->type == TABLETPC2FG) {
333 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
334 report_id, rep_data, 2);
336 error = usb_get_report(intf,
337 WAC_HID_FEATURE_REPORT, report_id,
339 } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
340 } else if (features->type != TABLETPC) {
344 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
345 report_id, rep_data, 2);
347 error = usb_get_report(intf,
348 WAC_HID_FEATURE_REPORT, report_id,
350 } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
355 return error < 0 ? error : 0;
358 static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
359 struct wacom_features *features)
362 struct usb_host_interface *interface = intf->cur_altsetting;
363 struct hid_descriptor *hid_desc;
365 /* default features */
366 features->device_type = BTN_TOOL_PEN;
367 features->x_fuzz = 4;
368 features->y_fuzz = 4;
369 features->pressure_fuzz = 0;
370 features->distance_fuzz = 0;
372 /* only Tablet PCs and Bamboo P&T need to retrieve the info */
373 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
374 (features->type != BAMBOO_PT))
377 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
378 if (usb_get_extra_descriptor(&interface->endpoint[0],
379 HID_DEVICET_REPORT, &hid_desc)) {
380 printk("wacom: can not retrieve extra class descriptor\n");
385 error = wacom_parse_hid(intf, hid_desc, features);
393 struct wacom_usbdev_data {
394 struct list_head list;
396 struct usb_device *dev;
397 struct wacom_shared shared;
400 static LIST_HEAD(wacom_udev_list);
401 static DEFINE_MUTEX(wacom_udev_list_lock);
403 static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
405 struct wacom_usbdev_data *data;
407 list_for_each_entry(data, &wacom_udev_list, list) {
408 if (data->dev == dev) {
409 kref_get(&data->kref);
417 static int wacom_add_shared_data(struct wacom_wac *wacom,
418 struct usb_device *dev)
420 struct wacom_usbdev_data *data;
423 mutex_lock(&wacom_udev_list_lock);
425 data = wacom_get_usbdev_data(dev);
427 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
433 kref_init(&data->kref);
435 list_add_tail(&data->list, &wacom_udev_list);
438 wacom->shared = &data->shared;
441 mutex_unlock(&wacom_udev_list_lock);
445 static void wacom_release_shared_data(struct kref *kref)
447 struct wacom_usbdev_data *data =
448 container_of(kref, struct wacom_usbdev_data, kref);
450 mutex_lock(&wacom_udev_list_lock);
451 list_del(&data->list);
452 mutex_unlock(&wacom_udev_list_lock);
457 static void wacom_remove_shared_data(struct wacom_wac *wacom)
459 struct wacom_usbdev_data *data;
462 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
463 kref_put(&data->kref, wacom_release_shared_data);
464 wacom->shared = NULL;
468 static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
470 struct usb_device *dev = interface_to_usbdev(intf);
471 struct usb_endpoint_descriptor *endpoint;
473 struct wacom_wac *wacom_wac;
474 struct wacom_features *features;
475 struct input_dev *input_dev;
478 if (!id->driver_info)
481 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
482 input_dev = input_allocate_device();
483 if (!wacom || !input_dev) {
488 wacom_wac = &wacom->wacom_wac;
489 wacom_wac->features = *((struct wacom_features *)id->driver_info);
490 features = &wacom_wac->features;
491 if (features->pktlen > WACOM_PKGLEN_MAX) {
496 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
497 GFP_KERNEL, &wacom->data_dma);
498 if (!wacom_wac->data) {
503 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
511 mutex_init(&wacom->lock);
512 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
513 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
515 wacom_wac->input = input_dev;
517 endpoint = &intf->cur_altsetting->endpoint[0].desc;
519 /* Retrieve the physical and logical size for OEM devices */
520 error = wacom_retrieve_hid_descriptor(intf, features);
524 wacom_setup_device_quirks(features);
526 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
528 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
529 /* Append the device type to the name */
530 strlcat(wacom_wac->name,
531 features->device_type == BTN_TOOL_PEN ?
533 sizeof(wacom_wac->name));
535 error = wacom_add_shared_data(wacom_wac, dev);
540 input_dev->name = wacom_wac->name;
541 input_dev->dev.parent = &intf->dev;
542 input_dev->open = wacom_open;
543 input_dev->close = wacom_close;
544 usb_to_input_id(dev, &input_dev->id);
545 input_set_drvdata(input_dev, wacom);
547 wacom_setup_input_capabilities(input_dev, wacom_wac);
549 usb_fill_int_urb(wacom->irq, dev,
550 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
551 wacom_wac->data, features->pktlen,
552 wacom_sys_irq, wacom, endpoint->bInterval);
553 wacom->irq->transfer_dma = wacom->data_dma;
554 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
556 error = input_register_device(input_dev);
560 /* Note that if query fails it is not a hard failure */
561 wacom_query_tablet_data(intf, features);
563 usb_set_intfdata(intf, wacom);
566 fail4: wacom_remove_shared_data(wacom_wac);
567 fail3: usb_free_urb(wacom->irq);
568 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
569 fail1: input_free_device(input_dev);
574 static void wacom_disconnect(struct usb_interface *intf)
576 struct wacom *wacom = usb_get_intfdata(intf);
578 usb_set_intfdata(intf, NULL);
580 usb_kill_urb(wacom->irq);
581 input_unregister_device(wacom->wacom_wac.input);
582 usb_free_urb(wacom->irq);
583 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
584 wacom->wacom_wac.data, wacom->data_dma);
585 wacom_remove_shared_data(&wacom->wacom_wac);
589 static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
591 struct wacom *wacom = usb_get_intfdata(intf);
593 mutex_lock(&wacom->lock);
594 usb_kill_urb(wacom->irq);
595 mutex_unlock(&wacom->lock);
600 static int wacom_resume(struct usb_interface *intf)
602 struct wacom *wacom = usb_get_intfdata(intf);
603 struct wacom_features *features = &wacom->wacom_wac.features;
606 mutex_lock(&wacom->lock);
608 /* switch to wacom mode first */
609 wacom_query_tablet_data(intf, features);
612 rv = usb_submit_urb(wacom->irq, GFP_NOIO);
616 mutex_unlock(&wacom->lock);
621 static int wacom_reset_resume(struct usb_interface *intf)
623 return wacom_resume(intf);
626 static struct usb_driver wacom_driver = {
628 .id_table = wacom_ids,
629 .probe = wacom_probe,
630 .disconnect = wacom_disconnect,
631 .suspend = wacom_suspend,
632 .resume = wacom_resume,
633 .reset_resume = wacom_reset_resume,
634 .supports_autosuspend = 1,
637 static int __init wacom_init(void)
641 result = usb_register(&wacom_driver);
643 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
648 static void __exit wacom_exit(void)
650 usb_deregister(&wacom_driver);
653 module_init(wacom_init);
654 module_exit(wacom_exit);