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 |
Chris Bagwell | 4134361 | 2011-10-26 22:32:52 -0700 | [diff] [blame] | 31 | #define HID_COLLECTION 0xa1 |
| 32 | #define HID_COLLECTION_LOGICAL 0x02 |
| 33 | #define HID_COLLECTION_END 0xc0 |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 34 | |
| 35 | enum { |
| 36 | WCM_UNDEFINED = 0, |
| 37 | WCM_DESKTOP, |
| 38 | WCM_DIGITIZER, |
| 39 | }; |
| 40 | |
| 41 | struct hid_descriptor { |
| 42 | struct usb_descriptor_header header; |
| 43 | __le16 bcdHID; |
| 44 | u8 bCountryCode; |
| 45 | u8 bNumDescriptors; |
| 46 | u8 bDescriptorType; |
| 47 | __le16 wDescriptorLength; |
| 48 | } __attribute__ ((packed)); |
| 49 | |
| 50 | /* defines to get/set USB message */ |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 51 | #define USB_REQ_GET_REPORT 0x01 |
| 52 | #define USB_REQ_SET_REPORT 0x09 |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 53 | |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 54 | #define WAC_HID_FEATURE_REPORT 0x03 |
Ping Cheng | a417ea4 | 2011-08-16 00:17:56 -0700 | [diff] [blame] | 55 | #define WAC_MSG_RETRIES 5 |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 56 | |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 57 | #define WAC_CMD_LED_CONTROL 0x20 |
| 58 | #define WAC_CMD_ICON_START 0x21 |
| 59 | #define WAC_CMD_ICON_XFER 0x23 |
| 60 | #define WAC_CMD_RETRIES 10 |
| 61 | |
| 62 | static int wacom_get_report(struct usb_interface *intf, u8 type, u8 id, |
| 63 | void *buf, size_t size, unsigned int retries) |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 64 | { |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 65 | struct usb_device *dev = interface_to_usbdev(intf); |
| 66 | int retval; |
| 67 | |
| 68 | do { |
| 69 | retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 70 | USB_REQ_GET_REPORT, |
Chris Bagwell | 6e8ec53 | 2011-10-26 22:24:22 -0700 | [diff] [blame] | 71 | USB_DIR_IN | USB_TYPE_CLASS | |
| 72 | USB_RECIP_INTERFACE, |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 73 | (type << 8) + id, |
| 74 | intf->altsetting[0].desc.bInterfaceNumber, |
| 75 | buf, size, 100); |
| 76 | } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries); |
| 77 | |
| 78 | return retval; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 81 | static int wacom_set_report(struct usb_interface *intf, u8 type, u8 id, |
| 82 | void *buf, size_t size, unsigned int retries) |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 83 | { |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 84 | struct usb_device *dev = interface_to_usbdev(intf); |
| 85 | int retval; |
| 86 | |
| 87 | do { |
| 88 | retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 89 | USB_REQ_SET_REPORT, |
| 90 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 91 | (type << 8) + id, |
| 92 | intf->altsetting[0].desc.bInterfaceNumber, |
| 93 | buf, size, 1000); |
| 94 | } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries); |
| 95 | |
| 96 | return retval; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Adrian Bunk | 54c9b226 | 2006-11-20 03:23:58 +0100 | [diff] [blame] | 99 | static void wacom_sys_irq(struct urb *urb) |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 100 | { |
| 101 | struct wacom *wacom = urb->context; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 102 | int retval; |
| 103 | |
| 104 | switch (urb->status) { |
| 105 | case 0: |
| 106 | /* success */ |
| 107 | break; |
| 108 | case -ECONNRESET: |
| 109 | case -ENOENT: |
| 110 | case -ESHUTDOWN: |
| 111 | /* this urb is terminated, clean up */ |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 112 | dbg("%s - urb shutting down with status: %d", __func__, urb->status); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 113 | return; |
| 114 | default: |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 115 | dbg("%s - nonzero urb status received: %d", __func__, urb->status); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 116 | goto exit; |
| 117 | } |
| 118 | |
Dmitry Torokhov | 95dd3b3 | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 119 | wacom_wac_irq(&wacom->wacom_wac, urb->actual_length); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 120 | |
| 121 | exit: |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 122 | usb_mark_last_busy(wacom->usbdev); |
Dmitry Torokhov | 73a97f4 | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 123 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 124 | if (retval) |
| 125 | err ("%s - usb_submit_urb failed with result %d", |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 126 | __func__, retval); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 129 | static int wacom_open(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); |
Dmitry Torokhov | f6cd378 | 2010-10-04 21:46:11 -0700 | [diff] [blame] | 132 | int retval = 0; |
| 133 | |
| 134 | if (usb_autopm_get_interface(wacom->intf) < 0) |
| 135 | return -EIO; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 136 | |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 137 | mutex_lock(&wacom->lock); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 138 | |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 139 | if (usb_submit_urb(wacom->irq, GFP_KERNEL)) { |
Dmitry Torokhov | f6cd378 | 2010-10-04 21:46:11 -0700 | [diff] [blame] | 140 | retval = -EIO; |
| 141 | goto out; |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Dmitry Torokhov | 51269fe | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 144 | wacom->open = true; |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 145 | wacom->intf->needs_remote_wakeup = 1; |
| 146 | |
Dmitry Torokhov | f6cd378 | 2010-10-04 21:46:11 -0700 | [diff] [blame] | 147 | out: |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 148 | mutex_unlock(&wacom->lock); |
Dmitry Torokhov | 62ecae0 | 2010-10-10 14:24:16 -0700 | [diff] [blame] | 149 | usb_autopm_put_interface(wacom->intf); |
Dmitry Torokhov | f6cd378 | 2010-10-04 21:46:11 -0700 | [diff] [blame] | 150 | return retval; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void wacom_close(struct input_dev *dev) |
| 154 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 155 | struct wacom *wacom = input_get_drvdata(dev); |
Dmitry Torokhov | 62ecae0 | 2010-10-10 14:24:16 -0700 | [diff] [blame] | 156 | int autopm_error; |
| 157 | |
| 158 | autopm_error = usb_autopm_get_interface(wacom->intf); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 159 | |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 160 | mutex_lock(&wacom->lock); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 161 | usb_kill_urb(wacom->irq); |
Dmitry Torokhov | 51269fe | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 162 | wacom->open = false; |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 163 | wacom->intf->needs_remote_wakeup = 0; |
| 164 | mutex_unlock(&wacom->lock); |
Dmitry Torokhov | f6cd378 | 2010-10-04 21:46:11 -0700 | [diff] [blame] | 165 | |
Dmitry Torokhov | 62ecae0 | 2010-10-10 14:24:16 -0700 | [diff] [blame] | 166 | if (!autopm_error) |
| 167 | usb_autopm_put_interface(wacom->intf); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Chris Bagwell | 4134361 | 2011-10-26 22:32:52 -0700 | [diff] [blame] | 170 | static int wacom_parse_logical_collection(unsigned char *report, |
| 171 | struct wacom_features *features) |
| 172 | { |
| 173 | int length = 0; |
| 174 | |
| 175 | if (features->type == BAMBOO_PT) { |
| 176 | |
| 177 | /* Logical collection is only used by 3rd gen Bamboo Touch */ |
| 178 | features->pktlen = WACOM_PKGLEN_BBTOUCH3; |
Ping Cheng | 8b4a0c1f | 2012-01-31 00:07:33 -0800 | [diff] [blame] | 179 | features->device_type = BTN_TOOL_FINGER; |
Chris Bagwell | 4134361 | 2011-10-26 22:32:52 -0700 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * Stylus and Touch have same active area |
| 183 | * so compute physical size based on stylus |
| 184 | * data before its overwritten. |
| 185 | */ |
| 186 | features->x_phy = |
| 187 | (features->x_max * features->x_resolution) / 100; |
| 188 | features->y_phy = |
| 189 | (features->y_max * features->y_resolution) / 100; |
| 190 | |
| 191 | features->x_max = features->y_max = |
| 192 | get_unaligned_le16(&report[10]); |
| 193 | |
| 194 | length = 11; |
| 195 | } |
| 196 | return length; |
| 197 | } |
| 198 | |
Chris Bagwell | 428f858 | 2011-10-26 22:26:59 -0700 | [diff] [blame] | 199 | /* |
| 200 | * Interface Descriptor of wacom devices can be incomplete and |
| 201 | * inconsistent so wacom_features table is used to store stylus |
| 202 | * device's packet lengths, various maximum values, and tablet |
| 203 | * resolution based on product ID's. |
| 204 | * |
| 205 | * For devices that contain 2 interfaces, wacom_features table is |
| 206 | * inaccurate for the touch interface. Since the Interface Descriptor |
| 207 | * for touch interfaces has pretty complete data, this function exists |
| 208 | * to query tablet for this missing information instead of hard coding in |
| 209 | * an additional table. |
| 210 | * |
| 211 | * A typical Interface Descriptor for a stylus will contain a |
| 212 | * boot mouse application collection that is not of interest and this |
| 213 | * function will ignore it. |
| 214 | * |
| 215 | * It also contains a digitizer application collection that also is not |
| 216 | * of interest since any information it contains would be duplicate |
| 217 | * of what is in wacom_features. Usually it defines a report of an array |
| 218 | * of bytes that could be used as max length of the stylus packet returned. |
| 219 | * If it happens to define a Digitizer-Stylus Physical Collection then |
| 220 | * the X and Y logical values contain valid data but it is ignored. |
| 221 | * |
| 222 | * A typical Interface Descriptor for a touch interface will contain a |
| 223 | * Digitizer-Finger Physical Collection which will define both logical |
| 224 | * X/Y maximum as well as the physical size of tablet. Since touch |
| 225 | * interfaces haven't supported pressure or distance, this is enough |
| 226 | * information to override invalid values in the wacom_features table. |
Chris Bagwell | 4134361 | 2011-10-26 22:32:52 -0700 | [diff] [blame] | 227 | * |
| 228 | * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical |
| 229 | * Collection. Instead they define a Logical Collection with a single |
| 230 | * Logical Maximum for both X and Y. |
Chris Bagwell | 428f858 | 2011-10-26 22:26:59 -0700 | [diff] [blame] | 231 | */ |
| 232 | static int wacom_parse_hid(struct usb_interface *intf, |
| 233 | struct hid_descriptor *hid_desc, |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 234 | struct wacom_features *features) |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 235 | { |
| 236 | struct usb_device *dev = interface_to_usbdev(intf); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 237 | char limit = 0; |
| 238 | /* result has to be defined as int for some devices */ |
| 239 | int result = 0; |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 240 | int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0; |
| 241 | unsigned char *report; |
| 242 | |
| 243 | report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL); |
| 244 | if (!report) |
| 245 | return -ENOMEM; |
| 246 | |
| 247 | /* retrive report descriptors */ |
| 248 | do { |
| 249 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 250 | USB_REQ_GET_DESCRIPTOR, |
| 251 | USB_RECIP_INTERFACE | USB_DIR_IN, |
| 252 | HID_DEVICET_REPORT << 8, |
| 253 | intf->altsetting[0].desc.bInterfaceNumber, /* interface */ |
| 254 | report, |
| 255 | hid_desc->wDescriptorLength, |
| 256 | 5000); /* 5 secs */ |
Ping Cheng | a417ea4 | 2011-08-16 00:17:56 -0700 | [diff] [blame] | 257 | } while (result < 0 && limit++ < WAC_MSG_RETRIES); |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 258 | |
Ping Cheng | 384318e | 2009-04-28 07:49:54 -0700 | [diff] [blame] | 259 | /* No need to parse the Descriptor. It isn't an error though */ |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 260 | if (result < 0) |
| 261 | goto out; |
| 262 | |
| 263 | for (i = 0; i < hid_desc->wDescriptorLength; i++) { |
| 264 | |
| 265 | switch (report[i]) { |
| 266 | case HID_USAGE_PAGE: |
| 267 | switch (report[i + 1]) { |
| 268 | case HID_USAGE_PAGE_DIGITIZER: |
| 269 | usage = WCM_DIGITIZER; |
| 270 | i++; |
| 271 | break; |
| 272 | |
| 273 | case HID_USAGE_PAGE_DESKTOP: |
| 274 | usage = WCM_DESKTOP; |
| 275 | i++; |
| 276 | break; |
| 277 | } |
| 278 | break; |
| 279 | |
| 280 | case HID_USAGE: |
| 281 | switch (report[i + 1]) { |
| 282 | case HID_USAGE_X: |
| 283 | if (usage == WCM_DESKTOP) { |
| 284 | if (finger) { |
Ping Cheng | 84eb5aa | 2011-03-12 20:35:18 -0800 | [diff] [blame] | 285 | features->device_type = BTN_TOOL_FINGER; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 286 | if (features->type == TABLETPC2FG) { |
| 287 | /* need to reset back */ |
| 288 | features->pktlen = WACOM_PKGLEN_TPC2FG; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 289 | } |
Ping Cheng | 4a88081 | 2010-09-05 12:25:40 -0700 | [diff] [blame] | 290 | if (features->type == BAMBOO_PT) { |
| 291 | /* need to reset back */ |
| 292 | features->pktlen = WACOM_PKGLEN_BBTOUCH; |
Ping Cheng | 4a88081 | 2010-09-05 12:25:40 -0700 | [diff] [blame] | 293 | features->x_phy = |
| 294 | get_unaligned_le16(&report[i + 5]); |
| 295 | features->x_max = |
| 296 | get_unaligned_le16(&report[i + 8]); |
| 297 | i += 15; |
| 298 | } else { |
| 299 | features->x_max = |
| 300 | get_unaligned_le16(&report[i + 3]); |
| 301 | features->x_phy = |
| 302 | get_unaligned_le16(&report[i + 6]); |
| 303 | features->unit = report[i + 9]; |
| 304 | features->unitExpo = report[i + 11]; |
| 305 | i += 12; |
| 306 | } |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 307 | } else if (pen) { |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 308 | /* penabled only accepts exact bytes of data */ |
| 309 | if (features->type == TABLETPC2FG) |
Ping Cheng | 57e413d | 2010-03-01 23:50:24 -0800 | [diff] [blame] | 310 | features->pktlen = WACOM_PKGLEN_GRAPHIRE; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 311 | features->device_type = BTN_TOOL_PEN; |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 312 | features->x_max = |
Dmitry Torokhov | 252f776 | 2010-03-19 22:33:38 -0700 | [diff] [blame] | 313 | get_unaligned_le16(&report[i + 3]); |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 314 | i += 4; |
| 315 | } |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 316 | } |
| 317 | break; |
| 318 | |
| 319 | case HID_USAGE_Y: |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 320 | if (usage == WCM_DESKTOP) { |
| 321 | if (finger) { |
Ping Cheng | 84eb5aa | 2011-03-12 20:35:18 -0800 | [diff] [blame] | 322 | features->device_type = BTN_TOOL_FINGER; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 323 | if (features->type == TABLETPC2FG) { |
| 324 | /* need to reset back */ |
| 325 | features->pktlen = WACOM_PKGLEN_TPC2FG; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 326 | features->y_max = |
Dmitry Torokhov | 252f776 | 2010-03-19 22:33:38 -0700 | [diff] [blame] | 327 | get_unaligned_le16(&report[i + 3]); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 328 | features->y_phy = |
Dmitry Torokhov | 252f776 | 2010-03-19 22:33:38 -0700 | [diff] [blame] | 329 | get_unaligned_le16(&report[i + 6]); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 330 | i += 7; |
Ping Cheng | 4a88081 | 2010-09-05 12:25:40 -0700 | [diff] [blame] | 331 | } else if (features->type == BAMBOO_PT) { |
| 332 | /* need to reset back */ |
| 333 | features->pktlen = WACOM_PKGLEN_BBTOUCH; |
Ping Cheng | 4a88081 | 2010-09-05 12:25:40 -0700 | [diff] [blame] | 334 | features->y_phy = |
| 335 | get_unaligned_le16(&report[i + 3]); |
| 336 | features->y_max = |
| 337 | get_unaligned_le16(&report[i + 6]); |
| 338 | i += 12; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 339 | } else { |
| 340 | features->y_max = |
| 341 | features->x_max; |
| 342 | features->y_phy = |
Dmitry Torokhov | 252f776 | 2010-03-19 22:33:38 -0700 | [diff] [blame] | 343 | get_unaligned_le16(&report[i + 3]); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 344 | i += 4; |
| 345 | } |
| 346 | } else if (pen) { |
| 347 | /* penabled only accepts exact bytes of data */ |
| 348 | if (features->type == TABLETPC2FG) |
Ping Cheng | 57e413d | 2010-03-01 23:50:24 -0800 | [diff] [blame] | 349 | features->pktlen = WACOM_PKGLEN_GRAPHIRE; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 350 | features->device_type = BTN_TOOL_PEN; |
| 351 | features->y_max = |
Dmitry Torokhov | 252f776 | 2010-03-19 22:33:38 -0700 | [diff] [blame] | 352 | get_unaligned_le16(&report[i + 3]); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 353 | i += 4; |
| 354 | } |
| 355 | } |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 356 | break; |
| 357 | |
| 358 | case HID_USAGE_FINGER: |
| 359 | finger = 1; |
| 360 | i++; |
| 361 | break; |
| 362 | |
Chris Bagwell | 428f858 | 2011-10-26 22:26:59 -0700 | [diff] [blame] | 363 | /* |
| 364 | * Requiring Stylus Usage will ignore boot mouse |
| 365 | * X/Y values and some cases of invalid Digitizer X/Y |
| 366 | * values commonly reported. |
| 367 | */ |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 368 | case HID_USAGE_STYLUS: |
| 369 | pen = 1; |
| 370 | i++; |
| 371 | break; |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 372 | } |
| 373 | break; |
| 374 | |
Chris Bagwell | 4134361 | 2011-10-26 22:32:52 -0700 | [diff] [blame] | 375 | case HID_COLLECTION_END: |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 376 | /* reset UsagePage and Finger */ |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 377 | finger = usage = 0; |
| 378 | break; |
Chris Bagwell | 4134361 | 2011-10-26 22:32:52 -0700 | [diff] [blame] | 379 | |
| 380 | case HID_COLLECTION: |
| 381 | i++; |
| 382 | switch (report[i]) { |
| 383 | case HID_COLLECTION_LOGICAL: |
| 384 | i += wacom_parse_logical_collection(&report[i], |
| 385 | features); |
| 386 | break; |
| 387 | } |
| 388 | break; |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 392 | out: |
Ping Cheng | 384318e | 2009-04-28 07:49:54 -0700 | [diff] [blame] | 393 | result = 0; |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 394 | kfree(report); |
| 395 | return result; |
| 396 | } |
| 397 | |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 398 | 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] | 399 | { |
| 400 | unsigned char *rep_data; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 401 | int limit = 0, report_id = 2; |
| 402 | int error = -ENOMEM; |
Dmitry Torokhov | 3b7307c | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 403 | |
Ping Cheng | 3b48c91 | 2011-08-16 00:17:57 -0700 | [diff] [blame] | 404 | rep_data = kmalloc(4, GFP_KERNEL); |
Dmitry Torokhov | 3b7307c | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 405 | if (!rep_data) |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 406 | return error; |
Dmitry Torokhov | 3b7307c | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 407 | |
Ping Cheng | 3b48c91 | 2011-08-16 00:17:57 -0700 | [diff] [blame] | 408 | /* ask to report tablet data if it is MT Tablet PC or |
Chris Bagwell | 3dc9f40 | 2010-09-12 00:08:40 -0700 | [diff] [blame] | 409 | * not a Tablet PC */ |
| 410 | if (features->type == TABLETPC2FG) { |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 411 | do { |
| 412 | rep_data[0] = 3; |
| 413 | rep_data[1] = 4; |
Ping Cheng | 3b48c91 | 2011-08-16 00:17:57 -0700 | [diff] [blame] | 414 | rep_data[2] = 0; |
| 415 | rep_data[3] = 0; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 416 | report_id = 3; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 417 | error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT, |
| 418 | report_id, rep_data, 4, 1); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 419 | if (error >= 0) |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 420 | error = wacom_get_report(intf, |
| 421 | WAC_HID_FEATURE_REPORT, |
| 422 | report_id, rep_data, 4, 1); |
Ping Cheng | a417ea4 | 2011-08-16 00:17:56 -0700 | [diff] [blame] | 423 | } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES); |
Chris Bagwell | 6e8ec53 | 2011-10-26 22:24:22 -0700 | [diff] [blame] | 424 | } else if (features->type != TABLETPC && |
| 425 | features->device_type == BTN_TOOL_PEN) { |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 426 | do { |
| 427 | rep_data[0] = 2; |
| 428 | rep_data[1] = 2; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 429 | error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT, |
| 430 | report_id, rep_data, 2, 1); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 431 | if (error >= 0) |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 432 | error = wacom_get_report(intf, |
| 433 | WAC_HID_FEATURE_REPORT, |
| 434 | report_id, rep_data, 2, 1); |
Ping Cheng | a417ea4 | 2011-08-16 00:17:56 -0700 | [diff] [blame] | 435 | } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 436 | } |
Dmitry Torokhov | 3b7307c | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 437 | |
| 438 | kfree(rep_data); |
| 439 | |
| 440 | return error < 0 ? error : 0; |
| 441 | } |
| 442 | |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 443 | static int wacom_retrieve_hid_descriptor(struct usb_interface *intf, |
| 444 | struct wacom_features *features) |
| 445 | { |
| 446 | int error = 0; |
| 447 | struct usb_host_interface *interface = intf->cur_altsetting; |
| 448 | struct hid_descriptor *hid_desc; |
| 449 | |
Henrik Rydberg | fed87e6 | 2010-09-05 12:25:11 -0700 | [diff] [blame] | 450 | /* default features */ |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 451 | features->device_type = BTN_TOOL_PEN; |
Henrik Rydberg | fed87e6 | 2010-09-05 12:25:11 -0700 | [diff] [blame] | 452 | features->x_fuzz = 4; |
| 453 | features->y_fuzz = 4; |
| 454 | features->pressure_fuzz = 0; |
| 455 | features->distance_fuzz = 0; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 456 | |
Chris Bagwell | 3dc9f40 | 2010-09-12 00:08:40 -0700 | [diff] [blame] | 457 | /* only Tablet PCs and Bamboo P&T need to retrieve the info */ |
Ping Cheng | 4a88081 | 2010-09-05 12:25:40 -0700 | [diff] [blame] | 458 | if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) && |
| 459 | (features->type != BAMBOO_PT)) |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 460 | goto out; |
| 461 | |
| 462 | if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) { |
| 463 | if (usb_get_extra_descriptor(&interface->endpoint[0], |
| 464 | HID_DEVICET_REPORT, &hid_desc)) { |
| 465 | printk("wacom: can not retrieve extra class descriptor\n"); |
| 466 | error = 1; |
| 467 | goto out; |
| 468 | } |
| 469 | } |
| 470 | error = wacom_parse_hid(intf, hid_desc, features); |
| 471 | if (error) |
| 472 | goto out; |
| 473 | |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 474 | out: |
| 475 | return error; |
| 476 | } |
| 477 | |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 478 | struct wacom_usbdev_data { |
| 479 | struct list_head list; |
| 480 | struct kref kref; |
| 481 | struct usb_device *dev; |
| 482 | struct wacom_shared shared; |
| 483 | }; |
| 484 | |
| 485 | static LIST_HEAD(wacom_udev_list); |
| 486 | static DEFINE_MUTEX(wacom_udev_list_lock); |
| 487 | |
| 488 | static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev) |
| 489 | { |
| 490 | struct wacom_usbdev_data *data; |
| 491 | |
| 492 | list_for_each_entry(data, &wacom_udev_list, list) { |
| 493 | if (data->dev == dev) { |
| 494 | kref_get(&data->kref); |
| 495 | return data; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | return NULL; |
| 500 | } |
| 501 | |
| 502 | static int wacom_add_shared_data(struct wacom_wac *wacom, |
| 503 | struct usb_device *dev) |
| 504 | { |
| 505 | struct wacom_usbdev_data *data; |
| 506 | int retval = 0; |
| 507 | |
| 508 | mutex_lock(&wacom_udev_list_lock); |
| 509 | |
| 510 | data = wacom_get_usbdev_data(dev); |
| 511 | if (!data) { |
| 512 | data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL); |
| 513 | if (!data) { |
| 514 | retval = -ENOMEM; |
| 515 | goto out; |
| 516 | } |
| 517 | |
| 518 | kref_init(&data->kref); |
| 519 | data->dev = dev; |
| 520 | list_add_tail(&data->list, &wacom_udev_list); |
| 521 | } |
| 522 | |
| 523 | wacom->shared = &data->shared; |
| 524 | |
| 525 | out: |
| 526 | mutex_unlock(&wacom_udev_list_lock); |
| 527 | return retval; |
| 528 | } |
| 529 | |
| 530 | static void wacom_release_shared_data(struct kref *kref) |
| 531 | { |
| 532 | struct wacom_usbdev_data *data = |
| 533 | container_of(kref, struct wacom_usbdev_data, kref); |
| 534 | |
| 535 | mutex_lock(&wacom_udev_list_lock); |
| 536 | list_del(&data->list); |
| 537 | mutex_unlock(&wacom_udev_list_lock); |
| 538 | |
| 539 | kfree(data); |
| 540 | } |
| 541 | |
| 542 | static void wacom_remove_shared_data(struct wacom_wac *wacom) |
| 543 | { |
| 544 | struct wacom_usbdev_data *data; |
| 545 | |
| 546 | if (wacom->shared) { |
| 547 | data = container_of(wacom->shared, struct wacom_usbdev_data, shared); |
| 548 | kref_put(&data->kref, wacom_release_shared_data); |
| 549 | wacom->shared = NULL; |
| 550 | } |
| 551 | } |
| 552 | |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 553 | static int wacom_led_control(struct wacom *wacom) |
| 554 | { |
| 555 | unsigned char *buf; |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 556 | int retval, led = 0; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 557 | |
| 558 | buf = kzalloc(9, GFP_KERNEL); |
| 559 | if (!buf) |
| 560 | return -ENOMEM; |
| 561 | |
Jason Gerecke | 246835f | 2011-12-12 00:12:04 -0800 | [diff] [blame] | 562 | if (wacom->wacom_wac.features.type == WACOM_21UX2 || |
| 563 | wacom->wacom_wac.features.type == WACOM_24HD) |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 564 | led = (wacom->led.select[1] << 4) | 0x40; |
| 565 | |
| 566 | led |= wacom->led.select[0] | 0x4; |
| 567 | |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 568 | buf[0] = WAC_CMD_LED_CONTROL; |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 569 | buf[1] = led; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 570 | buf[2] = wacom->led.llv; |
| 571 | buf[3] = wacom->led.hlv; |
| 572 | buf[4] = wacom->led.img_lum; |
| 573 | |
| 574 | retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL, |
| 575 | buf, 9, WAC_CMD_RETRIES); |
| 576 | kfree(buf); |
| 577 | |
| 578 | return retval; |
| 579 | } |
| 580 | |
| 581 | static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img) |
| 582 | { |
| 583 | unsigned char *buf; |
| 584 | int i, retval; |
| 585 | |
| 586 | buf = kzalloc(259, GFP_KERNEL); |
| 587 | if (!buf) |
| 588 | return -ENOMEM; |
| 589 | |
| 590 | /* Send 'start' command */ |
| 591 | buf[0] = WAC_CMD_ICON_START; |
| 592 | buf[1] = 1; |
| 593 | retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START, |
| 594 | buf, 2, WAC_CMD_RETRIES); |
| 595 | if (retval < 0) |
| 596 | goto out; |
| 597 | |
| 598 | buf[0] = WAC_CMD_ICON_XFER; |
| 599 | buf[1] = button_id & 0x07; |
| 600 | for (i = 0; i < 4; i++) { |
| 601 | buf[2] = i; |
| 602 | memcpy(buf + 3, img + i * 256, 256); |
| 603 | |
| 604 | retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER, |
| 605 | buf, 259, WAC_CMD_RETRIES); |
| 606 | if (retval < 0) |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | /* Send 'stop' */ |
| 611 | buf[0] = WAC_CMD_ICON_START; |
| 612 | buf[1] = 0; |
| 613 | wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START, |
| 614 | buf, 2, WAC_CMD_RETRIES); |
| 615 | |
| 616 | out: |
| 617 | kfree(buf); |
| 618 | return retval; |
| 619 | } |
| 620 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 621 | static ssize_t wacom_led_select_store(struct device *dev, int set_id, |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 622 | const char *buf, size_t count) |
| 623 | { |
| 624 | struct wacom *wacom = dev_get_drvdata(dev); |
| 625 | unsigned int id; |
| 626 | int err; |
| 627 | |
| 628 | err = kstrtouint(buf, 10, &id); |
| 629 | if (err) |
| 630 | return err; |
| 631 | |
| 632 | mutex_lock(&wacom->lock); |
| 633 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 634 | wacom->led.select[set_id] = id & 0x3; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 635 | err = wacom_led_control(wacom); |
| 636 | |
| 637 | mutex_unlock(&wacom->lock); |
| 638 | |
| 639 | return err < 0 ? err : count; |
| 640 | } |
| 641 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 642 | #define DEVICE_LED_SELECT_ATTR(SET_ID) \ |
| 643 | static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \ |
| 644 | struct device_attribute *attr, const char *buf, size_t count) \ |
| 645 | { \ |
| 646 | return wacom_led_select_store(dev, SET_ID, buf, count); \ |
| 647 | } \ |
Ping Cheng | 04c59ab | 2011-10-04 23:51:49 -0700 | [diff] [blame] | 648 | static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \ |
| 649 | struct device_attribute *attr, char *buf) \ |
| 650 | { \ |
| 651 | struct wacom *wacom = dev_get_drvdata(dev); \ |
| 652 | return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \ |
| 653 | } \ |
| 654 | static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \ |
| 655 | wacom_led##SET_ID##_select_show, \ |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 656 | wacom_led##SET_ID##_select_store) |
| 657 | |
| 658 | DEVICE_LED_SELECT_ATTR(0); |
| 659 | DEVICE_LED_SELECT_ATTR(1); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 660 | |
| 661 | static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest, |
| 662 | const char *buf, size_t count) |
| 663 | { |
| 664 | unsigned int value; |
| 665 | int err; |
| 666 | |
| 667 | err = kstrtouint(buf, 10, &value); |
| 668 | if (err) |
| 669 | return err; |
| 670 | |
| 671 | mutex_lock(&wacom->lock); |
| 672 | |
| 673 | *dest = value & 0x7f; |
| 674 | err = wacom_led_control(wacom); |
| 675 | |
| 676 | mutex_unlock(&wacom->lock); |
| 677 | |
| 678 | return err < 0 ? err : count; |
| 679 | } |
| 680 | |
| 681 | #define DEVICE_LUMINANCE_ATTR(name, field) \ |
| 682 | static ssize_t wacom_##name##_luminance_store(struct device *dev, \ |
| 683 | struct device_attribute *attr, const char *buf, size_t count) \ |
| 684 | { \ |
| 685 | struct wacom *wacom = dev_get_drvdata(dev); \ |
| 686 | \ |
| 687 | return wacom_luminance_store(wacom, &wacom->led.field, \ |
| 688 | buf, count); \ |
| 689 | } \ |
| 690 | static DEVICE_ATTR(name##_luminance, S_IWUSR, \ |
| 691 | NULL, wacom_##name##_luminance_store) |
| 692 | |
| 693 | DEVICE_LUMINANCE_ATTR(status0, llv); |
| 694 | DEVICE_LUMINANCE_ATTR(status1, hlv); |
| 695 | DEVICE_LUMINANCE_ATTR(buttons, img_lum); |
| 696 | |
| 697 | static ssize_t wacom_button_image_store(struct device *dev, int button_id, |
| 698 | const char *buf, size_t count) |
| 699 | { |
| 700 | struct wacom *wacom = dev_get_drvdata(dev); |
| 701 | int err; |
| 702 | |
| 703 | if (count != 1024) |
| 704 | return -EINVAL; |
| 705 | |
| 706 | mutex_lock(&wacom->lock); |
| 707 | |
| 708 | err = wacom_led_putimage(wacom, button_id, buf); |
| 709 | |
| 710 | mutex_unlock(&wacom->lock); |
| 711 | |
| 712 | return err < 0 ? err : count; |
| 713 | } |
| 714 | |
| 715 | #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \ |
| 716 | static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \ |
| 717 | struct device_attribute *attr, const char *buf, size_t count) \ |
| 718 | { \ |
| 719 | return wacom_button_image_store(dev, BUTTON_ID, buf, count); \ |
| 720 | } \ |
| 721 | static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \ |
| 722 | NULL, wacom_btnimg##BUTTON_ID##_store) |
| 723 | |
| 724 | DEVICE_BTNIMG_ATTR(0); |
| 725 | DEVICE_BTNIMG_ATTR(1); |
| 726 | DEVICE_BTNIMG_ATTR(2); |
| 727 | DEVICE_BTNIMG_ATTR(3); |
| 728 | DEVICE_BTNIMG_ATTR(4); |
| 729 | DEVICE_BTNIMG_ATTR(5); |
| 730 | DEVICE_BTNIMG_ATTR(6); |
| 731 | DEVICE_BTNIMG_ATTR(7); |
| 732 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 733 | static struct attribute *cintiq_led_attrs[] = { |
| 734 | &dev_attr_status_led0_select.attr, |
| 735 | &dev_attr_status_led1_select.attr, |
| 736 | NULL |
| 737 | }; |
| 738 | |
| 739 | static struct attribute_group cintiq_led_attr_group = { |
| 740 | .name = "wacom_led", |
| 741 | .attrs = cintiq_led_attrs, |
| 742 | }; |
| 743 | |
| 744 | static struct attribute *intuos4_led_attrs[] = { |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 745 | &dev_attr_status0_luminance.attr, |
| 746 | &dev_attr_status1_luminance.attr, |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 747 | &dev_attr_status_led0_select.attr, |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 748 | &dev_attr_buttons_luminance.attr, |
| 749 | &dev_attr_button0_rawimg.attr, |
| 750 | &dev_attr_button1_rawimg.attr, |
| 751 | &dev_attr_button2_rawimg.attr, |
| 752 | &dev_attr_button3_rawimg.attr, |
| 753 | &dev_attr_button4_rawimg.attr, |
| 754 | &dev_attr_button5_rawimg.attr, |
| 755 | &dev_attr_button6_rawimg.attr, |
| 756 | &dev_attr_button7_rawimg.attr, |
| 757 | NULL |
| 758 | }; |
| 759 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 760 | static struct attribute_group intuos4_led_attr_group = { |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 761 | .name = "wacom_led", |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 762 | .attrs = intuos4_led_attrs, |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 763 | }; |
| 764 | |
| 765 | static int wacom_initialize_leds(struct wacom *wacom) |
| 766 | { |
| 767 | int error; |
| 768 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 769 | /* Initialize default values */ |
| 770 | switch (wacom->wacom_wac.features.type) { |
| 771 | case INTUOS4: |
| 772 | case INTUOS4L: |
| 773 | wacom->led.select[0] = 0; |
| 774 | wacom->led.select[1] = 0; |
Ping Cheng | f4fa9a6 | 2011-10-04 23:49:42 -0700 | [diff] [blame] | 775 | wacom->led.llv = 10; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 776 | wacom->led.hlv = 20; |
| 777 | wacom->led.img_lum = 10; |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 778 | error = sysfs_create_group(&wacom->intf->dev.kobj, |
| 779 | &intuos4_led_attr_group); |
| 780 | break; |
| 781 | |
Jason Gerecke | 246835f | 2011-12-12 00:12:04 -0800 | [diff] [blame] | 782 | case WACOM_24HD: |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 783 | case WACOM_21UX2: |
| 784 | wacom->led.select[0] = 0; |
| 785 | wacom->led.select[1] = 0; |
| 786 | wacom->led.llv = 0; |
| 787 | wacom->led.hlv = 0; |
| 788 | wacom->led.img_lum = 0; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 789 | |
| 790 | error = sysfs_create_group(&wacom->intf->dev.kobj, |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 791 | &cintiq_led_attr_group); |
| 792 | break; |
| 793 | |
| 794 | default: |
| 795 | return 0; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 798 | if (error) { |
| 799 | dev_err(&wacom->intf->dev, |
| 800 | "cannot create sysfs group err: %d\n", error); |
| 801 | return error; |
| 802 | } |
| 803 | wacom_led_control(wacom); |
| 804 | |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | static void wacom_destroy_leds(struct wacom *wacom) |
| 809 | { |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 810 | switch (wacom->wacom_wac.features.type) { |
| 811 | case INTUOS4: |
| 812 | case INTUOS4L: |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 813 | sysfs_remove_group(&wacom->intf->dev.kobj, |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 814 | &intuos4_led_attr_group); |
| 815 | break; |
| 816 | |
Jason Gerecke | 246835f | 2011-12-12 00:12:04 -0800 | [diff] [blame] | 817 | case WACOM_24HD: |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 818 | case WACOM_21UX2: |
| 819 | sysfs_remove_group(&wacom->intf->dev.kobj, |
| 820 | &cintiq_led_attr_group); |
| 821 | break; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
Chris Bagwell | 3aac0ef | 2012-03-25 23:25:45 -0700 | [diff] [blame^] | 825 | static int wacom_register_input(struct wacom *wacom) |
| 826 | { |
| 827 | struct input_dev *input_dev; |
| 828 | struct usb_interface *intf = wacom->intf; |
| 829 | struct usb_device *dev = interface_to_usbdev(intf); |
| 830 | struct wacom_wac *wacom_wac = &(wacom->wacom_wac); |
| 831 | int error; |
| 832 | |
| 833 | input_dev = input_allocate_device(); |
| 834 | if (!input_dev) |
| 835 | return -ENOMEM; |
| 836 | |
| 837 | input_dev->name = wacom_wac->name; |
| 838 | input_dev->dev.parent = &intf->dev; |
| 839 | input_dev->open = wacom_open; |
| 840 | input_dev->close = wacom_close; |
| 841 | usb_to_input_id(dev, &input_dev->id); |
| 842 | input_set_drvdata(input_dev, wacom); |
| 843 | |
| 844 | wacom_wac->input = input_dev; |
| 845 | wacom_setup_input_capabilities(input_dev, wacom_wac); |
| 846 | |
| 847 | error = input_register_device(input_dev); |
| 848 | if (error) { |
| 849 | input_free_device(input_dev); |
| 850 | wacom_wac->input = NULL; |
| 851 | } |
| 852 | |
| 853 | return error; |
| 854 | } |
| 855 | |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 856 | static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 857 | { |
| 858 | struct usb_device *dev = interface_to_usbdev(intf); |
| 859 | struct usb_endpoint_descriptor *endpoint; |
| 860 | struct wacom *wacom; |
| 861 | struct wacom_wac *wacom_wac; |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 862 | struct wacom_features *features; |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 863 | int error; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 864 | |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 865 | if (!id->driver_info) |
Bastian Blank | b036f6f | 2010-02-10 23:06:23 -0800 | [diff] [blame] | 866 | return -EINVAL; |
| 867 | |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 868 | wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 869 | |
Dmitry Torokhov | 51269fe | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 870 | wacom_wac = &wacom->wacom_wac; |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 871 | wacom_wac->features = *((struct wacom_features *)id->driver_info); |
| 872 | features = &wacom_wac->features; |
| 873 | if (features->pktlen > WACOM_PKGLEN_MAX) { |
| 874 | error = -EINVAL; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 875 | goto fail1; |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 876 | } |
| 877 | |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 878 | wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX, |
| 879 | GFP_KERNEL, &wacom->data_dma); |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 880 | if (!wacom_wac->data) { |
| 881 | error = -ENOMEM; |
| 882 | goto fail1; |
| 883 | } |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 884 | |
| 885 | wacom->irq = usb_alloc_urb(0, GFP_KERNEL); |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 886 | if (!wacom->irq) { |
| 887 | error = -ENOMEM; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 888 | goto fail2; |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 889 | } |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 890 | |
| 891 | wacom->usbdev = dev; |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 892 | wacom->intf = intf; |
| 893 | mutex_init(&wacom->lock); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 894 | usb_make_path(dev, wacom->phys, sizeof(wacom->phys)); |
| 895 | strlcat(wacom->phys, "/input0", sizeof(wacom->phys)); |
| 896 | |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 897 | endpoint = &intf->cur_altsetting->endpoint[0].desc; |
| 898 | |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 899 | /* Retrieve the physical and logical size for OEM devices */ |
| 900 | error = wacom_retrieve_hid_descriptor(intf, features); |
| 901 | if (error) |
Alexander Strakh | 4b6d443 | 2011-02-11 00:44:41 -0800 | [diff] [blame] | 902 | goto fail3; |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 903 | |
Henrik Rydberg | bc73dd3 | 2010-09-05 12:26:16 -0700 | [diff] [blame] | 904 | wacom_setup_device_quirks(features); |
| 905 | |
Ping Cheng | 49b764a | 2010-02-20 00:53:49 -0800 | [diff] [blame] | 906 | strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name)); |
| 907 | |
Henrik Rydberg | bc73dd3 | 2010-09-05 12:26:16 -0700 | [diff] [blame] | 908 | if (features->quirks & WACOM_QUIRK_MULTI_INPUT) { |
Ping Cheng | 49b764a | 2010-02-20 00:53:49 -0800 | [diff] [blame] | 909 | /* Append the device type to the name */ |
| 910 | strlcat(wacom_wac->name, |
| 911 | features->device_type == BTN_TOOL_PEN ? |
| 912 | " Pen" : " Finger", |
| 913 | sizeof(wacom_wac->name)); |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 914 | |
| 915 | error = wacom_add_shared_data(wacom_wac, dev); |
| 916 | if (error) |
| 917 | goto fail3; |
Ping Cheng | 49b764a | 2010-02-20 00:53:49 -0800 | [diff] [blame] | 918 | } |
| 919 | |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 920 | usb_fill_int_urb(wacom->irq, dev, |
| 921 | usb_rcvintpipe(dev, endpoint->bEndpointAddress), |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 922 | wacom_wac->data, features->pktlen, |
Ping Cheng | 8d32e3a | 2006-09-26 13:34:47 -0700 | [diff] [blame] | 923 | wacom_sys_irq, wacom, endpoint->bInterval); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 924 | wacom->irq->transfer_dma = wacom->data_dma; |
| 925 | wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 926 | |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 927 | error = wacom_initialize_leds(wacom); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 928 | if (error) |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 929 | goto fail4; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 930 | |
Chris Bagwell | 3aac0ef | 2012-03-25 23:25:45 -0700 | [diff] [blame^] | 931 | error = wacom_register_input(wacom); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 932 | if (error) |
| 933 | goto fail5; |
| 934 | |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 935 | /* Note that if query fails it is not a hard failure */ |
| 936 | wacom_query_tablet_data(intf, features); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 937 | |
| 938 | usb_set_intfdata(intf, wacom); |
| 939 | return 0; |
| 940 | |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 941 | fail5: wacom_destroy_leds(wacom); |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 942 | fail4: wacom_remove_shared_data(wacom_wac); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 943 | fail3: usb_free_urb(wacom->irq); |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 944 | fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma); |
Chris Bagwell | 3aac0ef | 2012-03-25 23:25:45 -0700 | [diff] [blame^] | 945 | fail1: kfree(wacom); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 946 | return error; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | static void wacom_disconnect(struct usb_interface *intf) |
| 950 | { |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 951 | struct wacom *wacom = usb_get_intfdata(intf); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 952 | |
| 953 | usb_set_intfdata(intf, NULL); |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 954 | |
| 955 | usb_kill_urb(wacom->irq); |
Dmitry Torokhov | 8da23fc | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 956 | input_unregister_device(wacom->wacom_wac.input); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 957 | wacom_destroy_leds(wacom); |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 958 | usb_free_urb(wacom->irq); |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 959 | usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX, |
Dmitry Torokhov | 51269fe | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 960 | wacom->wacom_wac.data, wacom->data_dma); |
| 961 | wacom_remove_shared_data(&wacom->wacom_wac); |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 962 | kfree(wacom); |
| 963 | } |
| 964 | |
| 965 | static int wacom_suspend(struct usb_interface *intf, pm_message_t message) |
| 966 | { |
| 967 | struct wacom *wacom = usb_get_intfdata(intf); |
| 968 | |
| 969 | mutex_lock(&wacom->lock); |
| 970 | usb_kill_urb(wacom->irq); |
| 971 | mutex_unlock(&wacom->lock); |
| 972 | |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | static int wacom_resume(struct usb_interface *intf) |
| 977 | { |
| 978 | struct wacom *wacom = usb_get_intfdata(intf); |
Dmitry Torokhov | 51269fe | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 979 | struct wacom_features *features = &wacom->wacom_wac.features; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 980 | int rv = 0; |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 981 | |
| 982 | mutex_lock(&wacom->lock); |
Ping Cheng | 3810147 | 2010-04-13 23:07:52 -0700 | [diff] [blame] | 983 | |
| 984 | /* switch to wacom mode first */ |
| 985 | wacom_query_tablet_data(intf, features); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 986 | wacom_led_control(wacom); |
Ping Cheng | 3810147 | 2010-04-13 23:07:52 -0700 | [diff] [blame] | 987 | |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 988 | if (wacom->open && usb_submit_urb(wacom->irq, GFP_NOIO) < 0) |
| 989 | rv = -EIO; |
Ping Cheng | 3810147 | 2010-04-13 23:07:52 -0700 | [diff] [blame] | 990 | |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 991 | mutex_unlock(&wacom->lock); |
| 992 | |
| 993 | return rv; |
| 994 | } |
| 995 | |
| 996 | static int wacom_reset_resume(struct usb_interface *intf) |
| 997 | { |
| 998 | return wacom_resume(intf); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | static struct usb_driver wacom_driver = { |
| 1002 | .name = "wacom", |
Bastian Blank | b036f6f | 2010-02-10 23:06:23 -0800 | [diff] [blame] | 1003 | .id_table = wacom_ids, |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 1004 | .probe = wacom_probe, |
| 1005 | .disconnect = wacom_disconnect, |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 1006 | .suspend = wacom_suspend, |
| 1007 | .resume = wacom_resume, |
| 1008 | .reset_resume = wacom_reset_resume, |
| 1009 | .supports_autosuspend = 1, |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 1010 | }; |
| 1011 | |
Greg Kroah-Hartman | 08642e7 | 2011-11-18 09:48:31 -0800 | [diff] [blame] | 1012 | module_usb_driver(wacom_driver); |