Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 1 | /* |
Dmitry Torokhov | 4104d13 | 2007-05-07 16:16:29 -0400 | [diff] [blame] | 2 | * drivers/input/tablet/wacom_sys.c |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 3 | * |
Ping Cheng | 232f569 | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 4 | * USB Wacom tablet support - system specific code |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 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 | |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 14 | #include "wacom_wac.h" |
Dmitry Torokhov | 51269fe | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 15 | #include "wacom.h" |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 16 | |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 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 | |
| 33 | enum { |
| 34 | WCM_UNDEFINED = 0, |
| 35 | WCM_DESKTOP, |
| 36 | WCM_DIGITIZER, |
| 37 | }; |
| 38 | |
| 39 | struct 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 */ |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 49 | #define USB_REQ_GET_REPORT 0x01 |
| 50 | #define USB_REQ_SET_REPORT 0x09 |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 51 | #define WAC_HID_FEATURE_REPORT 0x03 |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 52 | |
| 53 | static 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 | |
| 63 | static 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 | |
Adrian Bunk | 54c9b226 | 2006-11-20 03:23:58 +0100 | [diff] [blame] | 73 | static void wacom_sys_irq(struct urb *urb) |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 74 | { |
| 75 | struct wacom *wacom = urb->context; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 76 | int retval; |
| 77 | |
| 78 | switch (urb->status) { |
| 79 | case 0: |
| 80 | /* success */ |
| 81 | break; |
| 82 | case -ECONNRESET: |
| 83 | case -ENOENT: |
| 84 | case -ESHUTDOWN: |
| 85 | /* this urb is terminated, clean up */ |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 86 | dbg("%s - urb shutting down with status: %d", __func__, urb->status); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 87 | return; |
| 88 | default: |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 89 | dbg("%s - nonzero urb status received: %d", __func__, urb->status); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 90 | goto exit; |
| 91 | } |
| 92 | |
Dmitry Torokhov | 95dd3b3 | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 93 | wacom_wac_irq(&wacom->wacom_wac, urb->actual_length); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 94 | |
| 95 | exit: |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 96 | usb_mark_last_busy(wacom->usbdev); |
Dmitry Torokhov | 73a97f4 | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 97 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 98 | if (retval) |
| 99 | err ("%s - usb_submit_urb failed with result %d", |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 100 | __func__, retval); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 103 | static int wacom_open(struct input_dev *dev) |
| 104 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 105 | struct wacom *wacom = input_get_drvdata(dev); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 106 | |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 107 | mutex_lock(&wacom->lock); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 108 | |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 109 | wacom->irq->dev = wacom->usbdev; |
| 110 | |
| 111 | if (usb_autopm_get_interface(wacom->intf) < 0) { |
| 112 | mutex_unlock(&wacom->lock); |
| 113 | return -EIO; |
| 114 | } |
| 115 | |
| 116 | if (usb_submit_urb(wacom->irq, GFP_KERNEL)) { |
| 117 | usb_autopm_put_interface(wacom->intf); |
| 118 | mutex_unlock(&wacom->lock); |
| 119 | return -EIO; |
| 120 | } |
| 121 | |
Dmitry Torokhov | 51269fe | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 122 | wacom->open = true; |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 123 | wacom->intf->needs_remote_wakeup = 1; |
| 124 | |
| 125 | mutex_unlock(&wacom->lock); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static void wacom_close(struct input_dev *dev) |
| 130 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 131 | struct wacom *wacom = input_get_drvdata(dev); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 132 | |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 133 | mutex_lock(&wacom->lock); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 134 | usb_kill_urb(wacom->irq); |
Dmitry Torokhov | 51269fe | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 135 | wacom->open = false; |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 136 | wacom->intf->needs_remote_wakeup = 0; |
| 137 | mutex_unlock(&wacom->lock); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 140 | static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc, |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 141 | struct wacom_features *features) |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 142 | { |
| 143 | struct usb_device *dev = interface_to_usbdev(intf); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 144 | char limit = 0; |
| 145 | /* result has to be defined as int for some devices */ |
| 146 | int result = 0; |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 147 | int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0; |
| 148 | unsigned char *report; |
| 149 | |
| 150 | report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL); |
| 151 | if (!report) |
| 152 | return -ENOMEM; |
| 153 | |
| 154 | /* retrive report descriptors */ |
| 155 | do { |
| 156 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 157 | USB_REQ_GET_DESCRIPTOR, |
| 158 | USB_RECIP_INTERFACE | USB_DIR_IN, |
| 159 | HID_DEVICET_REPORT << 8, |
| 160 | intf->altsetting[0].desc.bInterfaceNumber, /* interface */ |
| 161 | report, |
| 162 | hid_desc->wDescriptorLength, |
| 163 | 5000); /* 5 secs */ |
| 164 | } while (result < 0 && limit++ < 5); |
| 165 | |
Ping Cheng | 384318e | 2009-04-28 07:49:54 -0700 | [diff] [blame] | 166 | /* No need to parse the Descriptor. It isn't an error though */ |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 167 | if (result < 0) |
| 168 | goto out; |
| 169 | |
| 170 | for (i = 0; i < hid_desc->wDescriptorLength; i++) { |
| 171 | |
| 172 | switch (report[i]) { |
| 173 | case HID_USAGE_PAGE: |
| 174 | switch (report[i + 1]) { |
| 175 | case HID_USAGE_PAGE_DIGITIZER: |
| 176 | usage = WCM_DIGITIZER; |
| 177 | i++; |
| 178 | break; |
| 179 | |
| 180 | case HID_USAGE_PAGE_DESKTOP: |
| 181 | usage = WCM_DESKTOP; |
| 182 | i++; |
| 183 | break; |
| 184 | } |
| 185 | break; |
| 186 | |
| 187 | case HID_USAGE: |
| 188 | switch (report[i + 1]) { |
| 189 | case HID_USAGE_X: |
| 190 | if (usage == WCM_DESKTOP) { |
| 191 | if (finger) { |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 192 | features->device_type = BTN_TOOL_DOUBLETAP; |
| 193 | if (features->type == TABLETPC2FG) { |
| 194 | /* need to reset back */ |
| 195 | features->pktlen = WACOM_PKGLEN_TPC2FG; |
| 196 | features->device_type = BTN_TOOL_TRIPLETAP; |
| 197 | } |
Ping Cheng | 4a88081 | 2010-09-05 12:25:40 -0700 | [diff] [blame] | 198 | if (features->type == BAMBOO_PT) { |
| 199 | /* need to reset back */ |
| 200 | features->pktlen = WACOM_PKGLEN_BBTOUCH; |
| 201 | features->device_type = BTN_TOOL_TRIPLETAP; |
| 202 | features->x_phy = |
| 203 | get_unaligned_le16(&report[i + 5]); |
| 204 | features->x_max = |
| 205 | get_unaligned_le16(&report[i + 8]); |
| 206 | i += 15; |
| 207 | } else { |
| 208 | features->x_max = |
| 209 | get_unaligned_le16(&report[i + 3]); |
| 210 | features->x_phy = |
| 211 | get_unaligned_le16(&report[i + 6]); |
| 212 | features->unit = report[i + 9]; |
| 213 | features->unitExpo = report[i + 11]; |
| 214 | i += 12; |
| 215 | } |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 216 | } else if (pen) { |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 217 | /* penabled only accepts exact bytes of data */ |
| 218 | if (features->type == TABLETPC2FG) |
Ping Cheng | 57e413d | 2010-03-01 23:50:24 -0800 | [diff] [blame] | 219 | features->pktlen = WACOM_PKGLEN_GRAPHIRE; |
Ping Cheng | 4a88081 | 2010-09-05 12:25:40 -0700 | [diff] [blame] | 220 | if (features->type == BAMBOO_PT) |
| 221 | features->pktlen = WACOM_PKGLEN_BBFUN; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 222 | features->device_type = BTN_TOOL_PEN; |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 223 | features->x_max = |
Dmitry Torokhov | 252f776 | 2010-03-19 22:33:38 -0700 | [diff] [blame] | 224 | get_unaligned_le16(&report[i + 3]); |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 225 | i += 4; |
| 226 | } |
| 227 | } else if (usage == WCM_DIGITIZER) { |
| 228 | /* max pressure isn't reported |
| 229 | features->pressure_max = (unsigned short) |
| 230 | (report[i+4] << 8 | report[i + 3]); |
| 231 | */ |
| 232 | features->pressure_max = 255; |
| 233 | i += 4; |
| 234 | } |
| 235 | break; |
| 236 | |
| 237 | case HID_USAGE_Y: |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 238 | if (usage == WCM_DESKTOP) { |
| 239 | if (finger) { |
| 240 | features->device_type = BTN_TOOL_DOUBLETAP; |
| 241 | if (features->type == TABLETPC2FG) { |
| 242 | /* need to reset back */ |
| 243 | features->pktlen = WACOM_PKGLEN_TPC2FG; |
| 244 | features->device_type = BTN_TOOL_TRIPLETAP; |
| 245 | features->y_max = |
Dmitry Torokhov | 252f776 | 2010-03-19 22:33:38 -0700 | [diff] [blame] | 246 | get_unaligned_le16(&report[i + 3]); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 247 | features->y_phy = |
Dmitry Torokhov | 252f776 | 2010-03-19 22:33:38 -0700 | [diff] [blame] | 248 | get_unaligned_le16(&report[i + 6]); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 249 | i += 7; |
Ping Cheng | 4a88081 | 2010-09-05 12:25:40 -0700 | [diff] [blame] | 250 | } else if (features->type == BAMBOO_PT) { |
| 251 | /* need to reset back */ |
| 252 | features->pktlen = WACOM_PKGLEN_BBTOUCH; |
| 253 | features->device_type = BTN_TOOL_TRIPLETAP; |
| 254 | features->y_phy = |
| 255 | get_unaligned_le16(&report[i + 3]); |
| 256 | features->y_max = |
| 257 | get_unaligned_le16(&report[i + 6]); |
| 258 | i += 12; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 259 | } else { |
| 260 | features->y_max = |
| 261 | features->x_max; |
| 262 | features->y_phy = |
Dmitry Torokhov | 252f776 | 2010-03-19 22:33:38 -0700 | [diff] [blame] | 263 | get_unaligned_le16(&report[i + 3]); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 264 | i += 4; |
| 265 | } |
| 266 | } else if (pen) { |
| 267 | /* penabled only accepts exact bytes of data */ |
| 268 | if (features->type == TABLETPC2FG) |
Ping Cheng | 57e413d | 2010-03-01 23:50:24 -0800 | [diff] [blame] | 269 | features->pktlen = WACOM_PKGLEN_GRAPHIRE; |
Ping Cheng | 4a88081 | 2010-09-05 12:25:40 -0700 | [diff] [blame] | 270 | if (features->type == BAMBOO_PT) |
| 271 | features->pktlen = WACOM_PKGLEN_BBFUN; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 272 | features->device_type = BTN_TOOL_PEN; |
| 273 | features->y_max = |
Dmitry Torokhov | 252f776 | 2010-03-19 22:33:38 -0700 | [diff] [blame] | 274 | get_unaligned_le16(&report[i + 3]); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 275 | i += 4; |
| 276 | } |
| 277 | } |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 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 = |
Dmitry Torokhov | 252f776 | 2010-03-19 22:33:38 -0700 | [diff] [blame] | 293 | get_unaligned_le16(&report[i + 3]); |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 294 | i += 4; |
| 295 | break; |
| 296 | } |
| 297 | break; |
| 298 | |
| 299 | case HID_COLLECTION: |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 300 | /* reset UsagePage and Finger */ |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 301 | finger = usage = 0; |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 306 | out: |
Ping Cheng | 384318e | 2009-04-28 07:49:54 -0700 | [diff] [blame] | 307 | result = 0; |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 308 | kfree(report); |
| 309 | return result; |
| 310 | } |
| 311 | |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 312 | static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features) |
Dmitry Torokhov | 3b7307c | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 313 | { |
| 314 | unsigned char *rep_data; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 315 | int limit = 0, report_id = 2; |
| 316 | int error = -ENOMEM; |
Dmitry Torokhov | 3b7307c | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 317 | |
| 318 | rep_data = kmalloc(2, GFP_KERNEL); |
| 319 | if (!rep_data) |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 320 | return error; |
Dmitry Torokhov | 3b7307c | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 321 | |
Chris Bagwell | 3dc9f40 | 2010-09-12 00:08:40 -0700 | [diff] [blame^] | 322 | /* ask to report tablet data if it is 2FGT Tablet PC or |
| 323 | * not a Tablet PC */ |
| 324 | if (features->type == TABLETPC2FG) { |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 325 | do { |
| 326 | rep_data[0] = 3; |
| 327 | rep_data[1] = 4; |
| 328 | report_id = 3; |
| 329 | error = usb_set_report(intf, WAC_HID_FEATURE_REPORT, |
| 330 | report_id, rep_data, 2); |
| 331 | if (error >= 0) |
| 332 | error = usb_get_report(intf, |
| 333 | WAC_HID_FEATURE_REPORT, report_id, |
| 334 | rep_data, 3); |
| 335 | } while ((error < 0 || rep_data[1] != 4) && limit++ < 5); |
Chris Bagwell | 3dc9f40 | 2010-09-12 00:08:40 -0700 | [diff] [blame^] | 336 | } else if (features->type != TABLETPC) { |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 337 | do { |
| 338 | rep_data[0] = 2; |
| 339 | rep_data[1] = 2; |
| 340 | error = usb_set_report(intf, WAC_HID_FEATURE_REPORT, |
| 341 | report_id, rep_data, 2); |
| 342 | if (error >= 0) |
| 343 | error = usb_get_report(intf, |
| 344 | WAC_HID_FEATURE_REPORT, report_id, |
| 345 | rep_data, 2); |
| 346 | } while ((error < 0 || rep_data[1] != 2) && limit++ < 5); |
| 347 | } |
Dmitry Torokhov | 3b7307c | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 348 | |
| 349 | kfree(rep_data); |
| 350 | |
| 351 | return error < 0 ? error : 0; |
| 352 | } |
| 353 | |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 354 | static int wacom_retrieve_hid_descriptor(struct usb_interface *intf, |
| 355 | struct wacom_features *features) |
| 356 | { |
| 357 | int error = 0; |
| 358 | struct usb_host_interface *interface = intf->cur_altsetting; |
| 359 | struct hid_descriptor *hid_desc; |
| 360 | |
Henrik Rydberg | fed87e6 | 2010-09-05 12:25:11 -0700 | [diff] [blame] | 361 | /* default features */ |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 362 | features->device_type = BTN_TOOL_PEN; |
Henrik Rydberg | fed87e6 | 2010-09-05 12:25:11 -0700 | [diff] [blame] | 363 | features->x_fuzz = 4; |
| 364 | features->y_fuzz = 4; |
| 365 | features->pressure_fuzz = 0; |
| 366 | features->distance_fuzz = 0; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 367 | |
Chris Bagwell | 3dc9f40 | 2010-09-12 00:08:40 -0700 | [diff] [blame^] | 368 | /* only Tablet PCs and Bamboo P&T need to retrieve the info */ |
Ping Cheng | 4a88081 | 2010-09-05 12:25:40 -0700 | [diff] [blame] | 369 | if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) && |
| 370 | (features->type != BAMBOO_PT)) |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 371 | goto out; |
| 372 | |
| 373 | if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) { |
| 374 | if (usb_get_extra_descriptor(&interface->endpoint[0], |
| 375 | HID_DEVICET_REPORT, &hid_desc)) { |
| 376 | printk("wacom: can not retrieve extra class descriptor\n"); |
| 377 | error = 1; |
| 378 | goto out; |
| 379 | } |
| 380 | } |
| 381 | error = wacom_parse_hid(intf, hid_desc, features); |
| 382 | if (error) |
| 383 | goto out; |
| 384 | |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 385 | out: |
| 386 | return error; |
| 387 | } |
| 388 | |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 389 | struct wacom_usbdev_data { |
| 390 | struct list_head list; |
| 391 | struct kref kref; |
| 392 | struct usb_device *dev; |
| 393 | struct wacom_shared shared; |
| 394 | }; |
| 395 | |
| 396 | static LIST_HEAD(wacom_udev_list); |
| 397 | static DEFINE_MUTEX(wacom_udev_list_lock); |
| 398 | |
| 399 | static 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 | |
| 413 | static 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 | |
| 436 | out: |
| 437 | mutex_unlock(&wacom_udev_list_lock); |
| 438 | return retval; |
| 439 | } |
| 440 | |
| 441 | static 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 | |
| 453 | static 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 | |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 464 | static 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; |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 470 | struct wacom_features *features; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 471 | struct input_dev *input_dev; |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 472 | int error; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 473 | |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 474 | if (!id->driver_info) |
Bastian Blank | b036f6f | 2010-02-10 23:06:23 -0800 | [diff] [blame] | 475 | return -EINVAL; |
| 476 | |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 477 | wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 478 | input_dev = input_allocate_device(); |
Dmitry Torokhov | 51269fe | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 479 | if (!wacom || !input_dev) { |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 480 | error = -ENOMEM; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 481 | goto fail1; |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 482 | } |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 483 | |
Dmitry Torokhov | 51269fe | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 484 | wacom_wac = &wacom->wacom_wac; |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 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; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 489 | goto fail1; |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 492 | wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX, |
| 493 | GFP_KERNEL, &wacom->data_dma); |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 494 | if (!wacom_wac->data) { |
| 495 | error = -ENOMEM; |
| 496 | goto fail1; |
| 497 | } |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 498 | |
| 499 | wacom->irq = usb_alloc_urb(0, GFP_KERNEL); |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 500 | if (!wacom->irq) { |
| 501 | error = -ENOMEM; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 502 | goto fail2; |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 503 | } |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 504 | |
| 505 | wacom->usbdev = dev; |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 506 | wacom->intf = intf; |
| 507 | mutex_init(&wacom->lock); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 508 | usb_make_path(dev, wacom->phys, sizeof(wacom->phys)); |
| 509 | strlcat(wacom->phys, "/input0", sizeof(wacom->phys)); |
| 510 | |
Dmitry Torokhov | 8da23fc | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 511 | wacom_wac->input = input_dev; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 512 | |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 513 | endpoint = &intf->cur_altsetting->endpoint[0].desc; |
| 514 | |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 515 | /* Retrieve the physical and logical size for OEM devices */ |
| 516 | error = wacom_retrieve_hid_descriptor(intf, features); |
| 517 | if (error) |
| 518 | goto fail2; |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 519 | |
Henrik Rydberg | bc73dd3 | 2010-09-05 12:26:16 -0700 | [diff] [blame] | 520 | wacom_setup_device_quirks(features); |
| 521 | |
Ping Cheng | 49b764a | 2010-02-20 00:53:49 -0800 | [diff] [blame] | 522 | strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name)); |
| 523 | |
Henrik Rydberg | bc73dd3 | 2010-09-05 12:26:16 -0700 | [diff] [blame] | 524 | if (features->quirks & WACOM_QUIRK_MULTI_INPUT) { |
Ping Cheng | 49b764a | 2010-02-20 00:53:49 -0800 | [diff] [blame] | 525 | /* Append the device type to the name */ |
| 526 | strlcat(wacom_wac->name, |
| 527 | features->device_type == BTN_TOOL_PEN ? |
| 528 | " Pen" : " Finger", |
| 529 | sizeof(wacom_wac->name)); |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 530 | |
| 531 | error = wacom_add_shared_data(wacom_wac, dev); |
| 532 | if (error) |
| 533 | goto fail3; |
Ping Cheng | 49b764a | 2010-02-20 00:53:49 -0800 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | input_dev->name = wacom_wac->name; |
Dmitry Torokhov | 8da23fc | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 537 | input_dev->dev.parent = &intf->dev; |
| 538 | input_dev->open = wacom_open; |
| 539 | input_dev->close = wacom_close; |
| 540 | usb_to_input_id(dev, &input_dev->id); |
| 541 | input_set_drvdata(input_dev, wacom); |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 542 | |
Dmitry Torokhov | 8da23fc | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 543 | wacom_setup_input_capabilities(input_dev, wacom_wac); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 544 | |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 545 | usb_fill_int_urb(wacom->irq, dev, |
| 546 | usb_rcvintpipe(dev, endpoint->bEndpointAddress), |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 547 | wacom_wac->data, features->pktlen, |
Ping Cheng | 8d32e3a | 2006-09-26 13:34:47 -0700 | [diff] [blame] | 548 | wacom_sys_irq, wacom, endpoint->bInterval); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 549 | wacom->irq->transfer_dma = wacom->data_dma; |
| 550 | wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 551 | |
Dmitry Torokhov | 8da23fc | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 552 | error = input_register_device(input_dev); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 553 | if (error) |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 554 | goto fail4; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 555 | |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 556 | /* Note that if query fails it is not a hard failure */ |
| 557 | wacom_query_tablet_data(intf, features); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 558 | |
| 559 | usb_set_intfdata(intf, wacom); |
| 560 | return 0; |
| 561 | |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 562 | fail4: wacom_remove_shared_data(wacom_wac); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 563 | fail3: usb_free_urb(wacom->irq); |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 564 | fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 565 | fail1: input_free_device(input_dev); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 566 | kfree(wacom); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 567 | return error; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static void wacom_disconnect(struct usb_interface *intf) |
| 571 | { |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 572 | struct wacom *wacom = usb_get_intfdata(intf); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 573 | |
| 574 | usb_set_intfdata(intf, NULL); |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 575 | |
| 576 | usb_kill_urb(wacom->irq); |
Dmitry Torokhov | 8da23fc | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 577 | input_unregister_device(wacom->wacom_wac.input); |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 578 | usb_free_urb(wacom->irq); |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 579 | usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX, |
Dmitry Torokhov | 51269fe | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 580 | wacom->wacom_wac.data, wacom->data_dma); |
| 581 | wacom_remove_shared_data(&wacom->wacom_wac); |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 582 | kfree(wacom); |
| 583 | } |
| 584 | |
| 585 | static int wacom_suspend(struct usb_interface *intf, pm_message_t message) |
| 586 | { |
| 587 | struct wacom *wacom = usb_get_intfdata(intf); |
| 588 | |
| 589 | mutex_lock(&wacom->lock); |
| 590 | usb_kill_urb(wacom->irq); |
| 591 | mutex_unlock(&wacom->lock); |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | static int wacom_resume(struct usb_interface *intf) |
| 597 | { |
| 598 | struct wacom *wacom = usb_get_intfdata(intf); |
Dmitry Torokhov | 51269fe | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 599 | struct wacom_features *features = &wacom->wacom_wac.features; |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 600 | int rv; |
| 601 | |
| 602 | mutex_lock(&wacom->lock); |
Ping Cheng | 3810147 | 2010-04-13 23:07:52 -0700 | [diff] [blame] | 603 | |
| 604 | /* switch to wacom mode first */ |
| 605 | wacom_query_tablet_data(intf, features); |
| 606 | |
| 607 | if (wacom->open) |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 608 | rv = usb_submit_urb(wacom->irq, GFP_NOIO); |
Ping Cheng | 3810147 | 2010-04-13 23:07:52 -0700 | [diff] [blame] | 609 | else |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 610 | rv = 0; |
Ping Cheng | 3810147 | 2010-04-13 23:07:52 -0700 | [diff] [blame] | 611 | |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 612 | mutex_unlock(&wacom->lock); |
| 613 | |
| 614 | return rv; |
| 615 | } |
| 616 | |
| 617 | static int wacom_reset_resume(struct usb_interface *intf) |
| 618 | { |
| 619 | return wacom_resume(intf); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static struct usb_driver wacom_driver = { |
| 623 | .name = "wacom", |
Bastian Blank | b036f6f | 2010-02-10 23:06:23 -0800 | [diff] [blame] | 624 | .id_table = wacom_ids, |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 625 | .probe = wacom_probe, |
| 626 | .disconnect = wacom_disconnect, |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 627 | .suspend = wacom_suspend, |
| 628 | .resume = wacom_resume, |
| 629 | .reset_resume = wacom_reset_resume, |
| 630 | .supports_autosuspend = 1, |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 631 | }; |
| 632 | |
| 633 | static int __init wacom_init(void) |
| 634 | { |
| 635 | int result; |
Bastian Blank | b036f6f | 2010-02-10 23:06:23 -0800 | [diff] [blame] | 636 | |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 637 | result = usb_register(&wacom_driver); |
| 638 | if (result == 0) |
Greg Kroah-Hartman | 899ef6e | 2008-08-18 13:21:04 -0700 | [diff] [blame] | 639 | printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" |
| 640 | DRIVER_DESC "\n"); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 641 | return result; |
| 642 | } |
| 643 | |
| 644 | static void __exit wacom_exit(void) |
| 645 | { |
| 646 | usb_deregister(&wacom_driver); |
| 647 | } |
| 648 | |
| 649 | module_init(wacom_init); |
| 650 | module_exit(wacom_exit); |