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
52 #define WAC_MSG_RETRIES 5
54 static int usb_get_report(struct usb_interface
*intf
, unsigned char type
,
55 unsigned char id
, void *buf
, int size
)
57 return usb_control_msg(interface_to_usbdev(intf
),
58 usb_rcvctrlpipe(interface_to_usbdev(intf
), 0),
59 USB_REQ_GET_REPORT
, USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
60 (type
<< 8) + id
, intf
->altsetting
[0].desc
.bInterfaceNumber
,
64 static int usb_set_report(struct usb_interface
*intf
, unsigned char type
,
65 unsigned char id
, void *buf
, int size
)
67 return usb_control_msg(interface_to_usbdev(intf
),
68 usb_sndctrlpipe(interface_to_usbdev(intf
), 0),
69 USB_REQ_SET_REPORT
, USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
70 (type
<< 8) + id
, intf
->altsetting
[0].desc
.bInterfaceNumber
,
74 static void wacom_sys_irq(struct urb
*urb
)
76 struct wacom
*wacom
= urb
->context
;
79 switch (urb
->status
) {
86 /* this urb is terminated, clean up */
87 dbg("%s - urb shutting down with status: %d", __func__
, urb
->status
);
90 dbg("%s - nonzero urb status received: %d", __func__
, urb
->status
);
94 wacom_wac_irq(&wacom
->wacom_wac
, urb
->actual_length
);
97 usb_mark_last_busy(wacom
->usbdev
);
98 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
100 err ("%s - usb_submit_urb failed with result %d",
104 static int wacom_open(struct input_dev
*dev
)
106 struct wacom
*wacom
= input_get_drvdata(dev
);
109 if (usb_autopm_get_interface(wacom
->intf
) < 0)
112 mutex_lock(&wacom
->lock
);
114 if (usb_submit_urb(wacom
->irq
, GFP_KERNEL
)) {
120 wacom
->intf
->needs_remote_wakeup
= 1;
123 mutex_unlock(&wacom
->lock
);
124 usb_autopm_put_interface(wacom
->intf
);
128 static void wacom_close(struct input_dev
*dev
)
130 struct wacom
*wacom
= input_get_drvdata(dev
);
133 autopm_error
= usb_autopm_get_interface(wacom
->intf
);
135 mutex_lock(&wacom
->lock
);
136 usb_kill_urb(wacom
->irq
);
138 wacom
->intf
->needs_remote_wakeup
= 0;
139 mutex_unlock(&wacom
->lock
);
142 usb_autopm_put_interface(wacom
->intf
);
145 static int wacom_parse_hid(struct usb_interface
*intf
, struct hid_descriptor
*hid_desc
,
146 struct wacom_features
*features
)
148 struct usb_device
*dev
= interface_to_usbdev(intf
);
150 /* result has to be defined as int for some devices */
152 int i
= 0, usage
= WCM_UNDEFINED
, finger
= 0, pen
= 0;
153 unsigned char *report
;
155 report
= kzalloc(hid_desc
->wDescriptorLength
, GFP_KERNEL
);
159 /* retrive report descriptors */
161 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
162 USB_REQ_GET_DESCRIPTOR
,
163 USB_RECIP_INTERFACE
| USB_DIR_IN
,
164 HID_DEVICET_REPORT
<< 8,
165 intf
->altsetting
[0].desc
.bInterfaceNumber
, /* interface */
167 hid_desc
->wDescriptorLength
,
169 } while (result
< 0 && limit
++ < WAC_MSG_RETRIES
);
171 /* No need to parse the Descriptor. It isn't an error though */
175 for (i
= 0; i
< hid_desc
->wDescriptorLength
; i
++) {
179 switch (report
[i
+ 1]) {
180 case HID_USAGE_PAGE_DIGITIZER
:
181 usage
= WCM_DIGITIZER
;
185 case HID_USAGE_PAGE_DESKTOP
:
193 switch (report
[i
+ 1]) {
195 if (usage
== WCM_DESKTOP
) {
197 features
->device_type
= BTN_TOOL_FINGER
;
198 if (features
->type
== TABLETPC2FG
) {
199 /* need to reset back */
200 features
->pktlen
= WACOM_PKGLEN_TPC2FG
;
201 features
->device_type
= BTN_TOOL_DOUBLETAP
;
203 if (features
->type
== BAMBOO_PT
) {
204 /* need to reset back */
205 features
->pktlen
= WACOM_PKGLEN_BBTOUCH
;
206 features
->device_type
= BTN_TOOL_DOUBLETAP
;
208 get_unaligned_le16(&report
[i
+ 5]);
210 get_unaligned_le16(&report
[i
+ 8]);
214 get_unaligned_le16(&report
[i
+ 3]);
216 get_unaligned_le16(&report
[i
+ 6]);
217 features
->unit
= report
[i
+ 9];
218 features
->unitExpo
= report
[i
+ 11];
222 /* penabled only accepts exact bytes of data */
223 if (features
->type
== TABLETPC2FG
)
224 features
->pktlen
= WACOM_PKGLEN_GRAPHIRE
;
225 if (features
->type
== BAMBOO_PT
)
226 features
->pktlen
= WACOM_PKGLEN_BBFUN
;
227 features
->device_type
= BTN_TOOL_PEN
;
229 get_unaligned_le16(&report
[i
+ 3]);
232 } else if (usage
== WCM_DIGITIZER
) {
233 /* max pressure isn't reported
234 features->pressure_max = (unsigned short)
235 (report[i+4] << 8 | report[i + 3]);
237 features
->pressure_max
= 255;
243 if (usage
== WCM_DESKTOP
) {
245 features
->device_type
= BTN_TOOL_FINGER
;
246 if (features
->type
== TABLETPC2FG
) {
247 /* need to reset back */
248 features
->pktlen
= WACOM_PKGLEN_TPC2FG
;
249 features
->device_type
= BTN_TOOL_DOUBLETAP
;
251 get_unaligned_le16(&report
[i
+ 3]);
253 get_unaligned_le16(&report
[i
+ 6]);
255 } else if (features
->type
== BAMBOO_PT
) {
256 /* need to reset back */
257 features
->pktlen
= WACOM_PKGLEN_BBTOUCH
;
258 features
->device_type
= BTN_TOOL_DOUBLETAP
;
260 get_unaligned_le16(&report
[i
+ 3]);
262 get_unaligned_le16(&report
[i
+ 6]);
268 get_unaligned_le16(&report
[i
+ 3]);
272 /* penabled only accepts exact bytes of data */
273 if (features
->type
== TABLETPC2FG
)
274 features
->pktlen
= WACOM_PKGLEN_GRAPHIRE
;
275 if (features
->type
== BAMBOO_PT
)
276 features
->pktlen
= WACOM_PKGLEN_BBFUN
;
277 features
->device_type
= BTN_TOOL_PEN
;
279 get_unaligned_le16(&report
[i
+ 3]);
285 case HID_USAGE_FINGER
:
290 case HID_USAGE_STYLUS
:
295 case HID_USAGE_UNDEFINED
:
296 if (usage
== WCM_DESKTOP
&& finger
) /* capacity */
297 features
->pressure_max
=
298 get_unaligned_le16(&report
[i
+ 3]);
305 /* reset UsagePage and Finger */
317 static int wacom_query_tablet_data(struct usb_interface
*intf
, struct wacom_features
*features
)
319 unsigned char *rep_data
;
320 int limit
= 0, report_id
= 2;
323 rep_data
= kmalloc(4, GFP_KERNEL
);
327 /* ask to report tablet data if it is MT Tablet PC or
329 if (features
->type
== TABLETPC2FG
) {
336 error
= usb_set_report(intf
, WAC_HID_FEATURE_REPORT
,
337 report_id
, rep_data
, 4);
339 error
= usb_get_report(intf
,
340 WAC_HID_FEATURE_REPORT
, report_id
,
342 } while ((error
< 0 || rep_data
[1] != 4) && limit
++ < WAC_MSG_RETRIES
);
343 } else if (features
->type
!= TABLETPC
) {
347 error
= usb_set_report(intf
, WAC_HID_FEATURE_REPORT
,
348 report_id
, rep_data
, 2);
350 error
= usb_get_report(intf
,
351 WAC_HID_FEATURE_REPORT
, report_id
,
353 } while ((error
< 0 || rep_data
[1] != 2) && limit
++ < WAC_MSG_RETRIES
);
358 return error
< 0 ? error
: 0;
361 static int wacom_retrieve_hid_descriptor(struct usb_interface
*intf
,
362 struct wacom_features
*features
)
365 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
366 struct hid_descriptor
*hid_desc
;
368 /* default features */
369 features
->device_type
= BTN_TOOL_PEN
;
370 features
->x_fuzz
= 4;
371 features
->y_fuzz
= 4;
372 features
->pressure_fuzz
= 0;
373 features
->distance_fuzz
= 0;
375 /* only Tablet PCs and Bamboo P&T need to retrieve the info */
376 if ((features
->type
!= TABLETPC
) && (features
->type
!= TABLETPC2FG
) &&
377 (features
->type
!= BAMBOO_PT
))
380 if (usb_get_extra_descriptor(interface
, HID_DEVICET_HID
, &hid_desc
)) {
381 if (usb_get_extra_descriptor(&interface
->endpoint
[0],
382 HID_DEVICET_REPORT
, &hid_desc
)) {
383 printk("wacom: can not retrieve extra class descriptor\n");
388 error
= wacom_parse_hid(intf
, hid_desc
, features
);
396 struct wacom_usbdev_data
{
397 struct list_head list
;
399 struct usb_device
*dev
;
400 struct wacom_shared shared
;
403 static LIST_HEAD(wacom_udev_list
);
404 static DEFINE_MUTEX(wacom_udev_list_lock
);
406 static struct wacom_usbdev_data
*wacom_get_usbdev_data(struct usb_device
*dev
)
408 struct wacom_usbdev_data
*data
;
410 list_for_each_entry(data
, &wacom_udev_list
, list
) {
411 if (data
->dev
== dev
) {
412 kref_get(&data
->kref
);
420 static int wacom_add_shared_data(struct wacom_wac
*wacom
,
421 struct usb_device
*dev
)
423 struct wacom_usbdev_data
*data
;
426 mutex_lock(&wacom_udev_list_lock
);
428 data
= wacom_get_usbdev_data(dev
);
430 data
= kzalloc(sizeof(struct wacom_usbdev_data
), GFP_KERNEL
);
436 kref_init(&data
->kref
);
438 list_add_tail(&data
->list
, &wacom_udev_list
);
441 wacom
->shared
= &data
->shared
;
444 mutex_unlock(&wacom_udev_list_lock
);
448 static void wacom_release_shared_data(struct kref
*kref
)
450 struct wacom_usbdev_data
*data
=
451 container_of(kref
, struct wacom_usbdev_data
, kref
);
453 mutex_lock(&wacom_udev_list_lock
);
454 list_del(&data
->list
);
455 mutex_unlock(&wacom_udev_list_lock
);
460 static void wacom_remove_shared_data(struct wacom_wac
*wacom
)
462 struct wacom_usbdev_data
*data
;
465 data
= container_of(wacom
->shared
, struct wacom_usbdev_data
, shared
);
466 kref_put(&data
->kref
, wacom_release_shared_data
);
467 wacom
->shared
= NULL
;
471 static int wacom_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
473 struct usb_device
*dev
= interface_to_usbdev(intf
);
474 struct usb_endpoint_descriptor
*endpoint
;
476 struct wacom_wac
*wacom_wac
;
477 struct wacom_features
*features
;
478 struct input_dev
*input_dev
;
481 if (!id
->driver_info
)
484 wacom
= kzalloc(sizeof(struct wacom
), GFP_KERNEL
);
485 input_dev
= input_allocate_device();
486 if (!wacom
|| !input_dev
) {
491 wacom_wac
= &wacom
->wacom_wac
;
492 wacom_wac
->features
= *((struct wacom_features
*)id
->driver_info
);
493 features
= &wacom_wac
->features
;
494 if (features
->pktlen
> WACOM_PKGLEN_MAX
) {
499 wacom_wac
->data
= usb_alloc_coherent(dev
, WACOM_PKGLEN_MAX
,
500 GFP_KERNEL
, &wacom
->data_dma
);
501 if (!wacom_wac
->data
) {
506 wacom
->irq
= usb_alloc_urb(0, GFP_KERNEL
);
514 mutex_init(&wacom
->lock
);
515 usb_make_path(dev
, wacom
->phys
, sizeof(wacom
->phys
));
516 strlcat(wacom
->phys
, "/input0", sizeof(wacom
->phys
));
518 wacom_wac
->input
= input_dev
;
520 endpoint
= &intf
->cur_altsetting
->endpoint
[0].desc
;
522 /* Retrieve the physical and logical size for OEM devices */
523 error
= wacom_retrieve_hid_descriptor(intf
, features
);
527 wacom_setup_device_quirks(features
);
529 strlcpy(wacom_wac
->name
, features
->name
, sizeof(wacom_wac
->name
));
531 if (features
->quirks
& WACOM_QUIRK_MULTI_INPUT
) {
532 /* Append the device type to the name */
533 strlcat(wacom_wac
->name
,
534 features
->device_type
== BTN_TOOL_PEN
?
536 sizeof(wacom_wac
->name
));
538 error
= wacom_add_shared_data(wacom_wac
, dev
);
543 input_dev
->name
= wacom_wac
->name
;
544 input_dev
->dev
.parent
= &intf
->dev
;
545 input_dev
->open
= wacom_open
;
546 input_dev
->close
= wacom_close
;
547 usb_to_input_id(dev
, &input_dev
->id
);
548 input_set_drvdata(input_dev
, wacom
);
550 wacom_setup_input_capabilities(input_dev
, wacom_wac
);
552 usb_fill_int_urb(wacom
->irq
, dev
,
553 usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
),
554 wacom_wac
->data
, features
->pktlen
,
555 wacom_sys_irq
, wacom
, endpoint
->bInterval
);
556 wacom
->irq
->transfer_dma
= wacom
->data_dma
;
557 wacom
->irq
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
559 error
= input_register_device(input_dev
);
563 /* Note that if query fails it is not a hard failure */
564 wacom_query_tablet_data(intf
, features
);
566 usb_set_intfdata(intf
, wacom
);
569 fail4
: wacom_remove_shared_data(wacom_wac
);
570 fail3
: usb_free_urb(wacom
->irq
);
571 fail2
: usb_free_coherent(dev
, WACOM_PKGLEN_MAX
, wacom_wac
->data
, wacom
->data_dma
);
572 fail1
: input_free_device(input_dev
);
577 static void wacom_disconnect(struct usb_interface
*intf
)
579 struct wacom
*wacom
= usb_get_intfdata(intf
);
581 usb_set_intfdata(intf
, NULL
);
583 usb_kill_urb(wacom
->irq
);
584 input_unregister_device(wacom
->wacom_wac
.input
);
585 usb_free_urb(wacom
->irq
);
586 usb_free_coherent(interface_to_usbdev(intf
), WACOM_PKGLEN_MAX
,
587 wacom
->wacom_wac
.data
, wacom
->data_dma
);
588 wacom_remove_shared_data(&wacom
->wacom_wac
);
592 static int wacom_suspend(struct usb_interface
*intf
, pm_message_t message
)
594 struct wacom
*wacom
= usb_get_intfdata(intf
);
596 mutex_lock(&wacom
->lock
);
597 usb_kill_urb(wacom
->irq
);
598 mutex_unlock(&wacom
->lock
);
603 static int wacom_resume(struct usb_interface
*intf
)
605 struct wacom
*wacom
= usb_get_intfdata(intf
);
606 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
609 mutex_lock(&wacom
->lock
);
611 /* switch to wacom mode first */
612 wacom_query_tablet_data(intf
, features
);
615 rv
= usb_submit_urb(wacom
->irq
, GFP_NOIO
);
619 mutex_unlock(&wacom
->lock
);
624 static int wacom_reset_resume(struct usb_interface
*intf
)
626 return wacom_resume(intf
);
629 static struct usb_driver wacom_driver
= {
631 .id_table
= wacom_ids
,
632 .probe
= wacom_probe
,
633 .disconnect
= wacom_disconnect
,
634 .suspend
= wacom_suspend
,
635 .resume
= wacom_resume
,
636 .reset_resume
= wacom_reset_resume
,
637 .supports_autosuspend
= 1,
640 static int __init
wacom_init(void)
644 result
= usb_register(&wacom_driver
);
646 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
":"
651 static void __exit
wacom_exit(void)
653 usb_deregister(&wacom_driver
);
656 module_init(wacom_init
);
657 module_exit(wacom_exit
);