blob: d8cc9ce165ff2b3829a498aca8a4b44c3ec2db53 [file] [log] [blame]
Ping Cheng3bea7332006-07-13 18:01:36 -07001/*
Dmitry Torokhov4104d132007-05-07 16:16:29 -04002 * drivers/input/tablet/wacom_sys.c
Ping Cheng3bea7332006-07-13 18:01:36 -07003 *
Ping Cheng232f5692009-12-15 00:35:24 -08004 * USB Wacom tablet support - system specific code
Ping Cheng3bea7332006-07-13 18:01:36 -07005 */
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 Cheng3bea7332006-07-13 18:01:36 -070014#include "wacom_wac.h"
Dmitry Torokhov51269fe2010-03-19 22:18:15 -070015#include "wacom.h"
Ping Cheng3bea7332006-07-13 18:01:36 -070016
Ping Cheng545f4e92008-11-24 11:44:27 -050017/* 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 Bagwell41343612011-10-26 22:32:52 -070031#define HID_COLLECTION 0xa1
32#define HID_COLLECTION_LOGICAL 0x02
33#define HID_COLLECTION_END 0xc0
Ping Cheng545f4e92008-11-24 11:44:27 -050034
35enum {
36 WCM_UNDEFINED = 0,
37 WCM_DESKTOP,
38 WCM_DIGITIZER,
39};
40
41struct 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 Cheng3bea7332006-07-13 18:01:36 -070051#define USB_REQ_GET_REPORT 0x01
52#define USB_REQ_SET_REPORT 0x09
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070053
Ping Cheng545f4e92008-11-24 11:44:27 -050054#define WAC_HID_FEATURE_REPORT 0x03
Ping Chenga417ea42011-08-16 00:17:56 -070055#define WAC_MSG_RETRIES 5
Ping Cheng3bea7332006-07-13 18:01:36 -070056
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070057#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
62static int wacom_get_report(struct usb_interface *intf, u8 type, u8 id,
63 void *buf, size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070064{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070065 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 Bagwell6e8ec532011-10-26 22:24:22 -070071 USB_DIR_IN | USB_TYPE_CLASS |
72 USB_RECIP_INTERFACE,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070073 (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 Cheng3bea7332006-07-13 18:01:36 -070079}
80
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070081static int wacom_set_report(struct usb_interface *intf, u8 type, u8 id,
82 void *buf, size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070083{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070084 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 Cheng3bea7332006-07-13 18:01:36 -070097}
98
Adrian Bunk54c9b2262006-11-20 03:23:58 +010099static void wacom_sys_irq(struct urb *urb)
Ping Cheng3bea7332006-07-13 18:01:36 -0700100{
101 struct wacom *wacom = urb->context;
Ping Cheng3bea7332006-07-13 18:01:36 -0700102 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 Harrisonea3e6c52008-05-05 11:36:18 -0400112 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -0700113 return;
114 default:
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400115 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -0700116 goto exit;
117 }
118
Dmitry Torokhov95dd3b32010-03-19 22:18:15 -0700119 wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
Ping Cheng3bea7332006-07-13 18:01:36 -0700120
121 exit:
Oliver Neukume7224092008-04-15 01:31:57 -0400122 usb_mark_last_busy(wacom->usbdev);
Dmitry Torokhov73a97f42010-03-19 22:18:15 -0700123 retval = usb_submit_urb(urb, GFP_ATOMIC);
Ping Cheng3bea7332006-07-13 18:01:36 -0700124 if (retval)
125 err ("%s - usb_submit_urb failed with result %d",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400126 __func__, retval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700127}
128
Ping Cheng3bea7332006-07-13 18:01:36 -0700129static int wacom_open(struct input_dev *dev)
130{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400131 struct wacom *wacom = input_get_drvdata(dev);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700132 int retval = 0;
133
134 if (usb_autopm_get_interface(wacom->intf) < 0)
135 return -EIO;
Ping Cheng3bea7332006-07-13 18:01:36 -0700136
Oliver Neukume7224092008-04-15 01:31:57 -0400137 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700138
Oliver Neukume7224092008-04-15 01:31:57 -0400139 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700140 retval = -EIO;
141 goto out;
Oliver Neukume7224092008-04-15 01:31:57 -0400142 }
143
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700144 wacom->open = true;
Oliver Neukume7224092008-04-15 01:31:57 -0400145 wacom->intf->needs_remote_wakeup = 1;
146
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700147out:
Oliver Neukume7224092008-04-15 01:31:57 -0400148 mutex_unlock(&wacom->lock);
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700149 usb_autopm_put_interface(wacom->intf);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700150 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -0700151}
152
153static void wacom_close(struct input_dev *dev)
154{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400155 struct wacom *wacom = input_get_drvdata(dev);
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700156 int autopm_error;
157
158 autopm_error = usb_autopm_get_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700159
Oliver Neukume7224092008-04-15 01:31:57 -0400160 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700161 usb_kill_urb(wacom->irq);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700162 wacom->open = false;
Oliver Neukume7224092008-04-15 01:31:57 -0400163 wacom->intf->needs_remote_wakeup = 0;
164 mutex_unlock(&wacom->lock);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700165
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700166 if (!autopm_error)
167 usb_autopm_put_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700168}
169
Chris Bagwell41343612011-10-26 22:32:52 -0700170static 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 Cheng8b4a0c1f2012-01-31 00:07:33 -0800179 features->device_type = BTN_TOOL_FINGER;
Chris Bagwell41343612011-10-26 22:32:52 -0700180
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 Bagwell428f8582011-10-26 22:26:59 -0700199/*
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 Bagwell41343612011-10-26 22:32:52 -0700227 *
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 Bagwell428f8582011-10-26 22:26:59 -0700231 */
232static int wacom_parse_hid(struct usb_interface *intf,
233 struct hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800234 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500235{
236 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800237 char limit = 0;
238 /* result has to be defined as int for some devices */
239 int result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500240 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 Chenga417ea42011-08-16 00:17:56 -0700257 } while (result < 0 && limit++ < WAC_MSG_RETRIES);
Ping Cheng545f4e92008-11-24 11:44:27 -0500258
Ping Cheng384318e2009-04-28 07:49:54 -0700259 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500260 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 Cheng84eb5aa2011-03-12 20:35:18 -0800285 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800286 if (features->type == TABLETPC2FG) {
287 /* need to reset back */
288 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Chengec67bbe2009-12-15 00:35:24 -0800289 }
Ping Cheng4a880812010-09-05 12:25:40 -0700290 if (features->type == BAMBOO_PT) {
291 /* need to reset back */
292 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng4a880812010-09-05 12:25:40 -0700293 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 Cheng545f4e92008-11-24 11:44:27 -0500307 } else if (pen) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800308 /* penabled only accepts exact bytes of data */
309 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800310 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800311 features->device_type = BTN_TOOL_PEN;
Ping Cheng545f4e92008-11-24 11:44:27 -0500312 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700313 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500314 i += 4;
315 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500316 }
317 break;
318
319 case HID_USAGE_Y:
Ping Chengec67bbe2009-12-15 00:35:24 -0800320 if (usage == WCM_DESKTOP) {
321 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800322 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800323 if (features->type == TABLETPC2FG) {
324 /* need to reset back */
325 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Chengec67bbe2009-12-15 00:35:24 -0800326 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700327 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800328 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700329 get_unaligned_le16(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800330 i += 7;
Ping Cheng4a880812010-09-05 12:25:40 -0700331 } else if (features->type == BAMBOO_PT) {
332 /* need to reset back */
333 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng4a880812010-09-05 12:25:40 -0700334 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 Chengec67bbe2009-12-15 00:35:24 -0800339 } else {
340 features->y_max =
341 features->x_max;
342 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700343 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800344 i += 4;
345 }
346 } else if (pen) {
347 /* penabled only accepts exact bytes of data */
348 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800349 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800350 features->device_type = BTN_TOOL_PEN;
351 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700352 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800353 i += 4;
354 }
355 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500356 break;
357
358 case HID_USAGE_FINGER:
359 finger = 1;
360 i++;
361 break;
362
Chris Bagwell428f8582011-10-26 22:26:59 -0700363 /*
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 Cheng545f4e92008-11-24 11:44:27 -0500368 case HID_USAGE_STYLUS:
369 pen = 1;
370 i++;
371 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500372 }
373 break;
374
Chris Bagwell41343612011-10-26 22:32:52 -0700375 case HID_COLLECTION_END:
Ping Chengec67bbe2009-12-15 00:35:24 -0800376 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500377 finger = usage = 0;
378 break;
Chris Bagwell41343612011-10-26 22:32:52 -0700379
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 Cheng545f4e92008-11-24 11:44:27 -0500389 }
390 }
391
Ping Cheng545f4e92008-11-24 11:44:27 -0500392 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700393 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500394 kfree(report);
395 return result;
396}
397
Ping Chengec67bbe2009-12-15 00:35:24 -0800398static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700399{
400 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800401 int limit = 0, report_id = 2;
402 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700403
Ping Cheng3b48c912011-08-16 00:17:57 -0700404 rep_data = kmalloc(4, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700405 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800406 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700407
Ping Cheng3b48c912011-08-16 00:17:57 -0700408 /* ask to report tablet data if it is MT Tablet PC or
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700409 * not a Tablet PC */
410 if (features->type == TABLETPC2FG) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800411 do {
412 rep_data[0] = 3;
413 rep_data[1] = 4;
Ping Cheng3b48c912011-08-16 00:17:57 -0700414 rep_data[2] = 0;
415 rep_data[3] = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800416 report_id = 3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700417 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
418 report_id, rep_data, 4, 1);
Ping Chengec67bbe2009-12-15 00:35:24 -0800419 if (error >= 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700420 error = wacom_get_report(intf,
421 WAC_HID_FEATURE_REPORT,
422 report_id, rep_data, 4, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700423 } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
Chris Bagwell6e8ec532011-10-26 22:24:22 -0700424 } else if (features->type != TABLETPC &&
Chris Bagwelld3825d52012-03-25 23:26:11 -0700425 features->type != WIRELESS &&
Chris Bagwell6e8ec532011-10-26 22:24:22 -0700426 features->device_type == BTN_TOOL_PEN) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800427 do {
428 rep_data[0] = 2;
429 rep_data[1] = 2;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700430 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
431 report_id, rep_data, 2, 1);
Ping Chengec67bbe2009-12-15 00:35:24 -0800432 if (error >= 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700433 error = wacom_get_report(intf,
434 WAC_HID_FEATURE_REPORT,
435 report_id, rep_data, 2, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700436 } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
Ping Chengec67bbe2009-12-15 00:35:24 -0800437 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700438
439 kfree(rep_data);
440
441 return error < 0 ? error : 0;
442}
443
Ping Chengec67bbe2009-12-15 00:35:24 -0800444static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
445 struct wacom_features *features)
446{
447 int error = 0;
448 struct usb_host_interface *interface = intf->cur_altsetting;
449 struct hid_descriptor *hid_desc;
450
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700451 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800452 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700453 features->x_fuzz = 4;
454 features->y_fuzz = 4;
455 features->pressure_fuzz = 0;
456 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800457
Chris Bagwelld3825d52012-03-25 23:26:11 -0700458 /*
459 * The wireless device HID is basic and layout conflicts with
460 * other tablets (monitor and touch interface can look like pen).
461 * Skip the query for this type and modify defaults based on
462 * interface number.
463 */
464 if (features->type == WIRELESS) {
465 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
466 features->device_type = 0;
467 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
468 features->device_type = BTN_TOOL_DOUBLETAP;
469 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
470 }
471 }
472
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700473 /* only Tablet PCs and Bamboo P&T need to retrieve the info */
Ping Cheng4a880812010-09-05 12:25:40 -0700474 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
475 (features->type != BAMBOO_PT))
Ping Chengec67bbe2009-12-15 00:35:24 -0800476 goto out;
477
478 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
479 if (usb_get_extra_descriptor(&interface->endpoint[0],
480 HID_DEVICET_REPORT, &hid_desc)) {
481 printk("wacom: can not retrieve extra class descriptor\n");
482 error = 1;
483 goto out;
484 }
485 }
486 error = wacom_parse_hid(intf, hid_desc, features);
487 if (error)
488 goto out;
489
Ping Chengec67bbe2009-12-15 00:35:24 -0800490 out:
491 return error;
492}
493
Ping Cheng4492eff2010-03-19 22:18:15 -0700494struct wacom_usbdev_data {
495 struct list_head list;
496 struct kref kref;
497 struct usb_device *dev;
498 struct wacom_shared shared;
499};
500
501static LIST_HEAD(wacom_udev_list);
502static DEFINE_MUTEX(wacom_udev_list_lock);
503
504static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
505{
506 struct wacom_usbdev_data *data;
507
508 list_for_each_entry(data, &wacom_udev_list, list) {
509 if (data->dev == dev) {
510 kref_get(&data->kref);
511 return data;
512 }
513 }
514
515 return NULL;
516}
517
518static int wacom_add_shared_data(struct wacom_wac *wacom,
519 struct usb_device *dev)
520{
521 struct wacom_usbdev_data *data;
522 int retval = 0;
523
524 mutex_lock(&wacom_udev_list_lock);
525
526 data = wacom_get_usbdev_data(dev);
527 if (!data) {
528 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
529 if (!data) {
530 retval = -ENOMEM;
531 goto out;
532 }
533
534 kref_init(&data->kref);
535 data->dev = dev;
536 list_add_tail(&data->list, &wacom_udev_list);
537 }
538
539 wacom->shared = &data->shared;
540
541out:
542 mutex_unlock(&wacom_udev_list_lock);
543 return retval;
544}
545
546static void wacom_release_shared_data(struct kref *kref)
547{
548 struct wacom_usbdev_data *data =
549 container_of(kref, struct wacom_usbdev_data, kref);
550
551 mutex_lock(&wacom_udev_list_lock);
552 list_del(&data->list);
553 mutex_unlock(&wacom_udev_list_lock);
554
555 kfree(data);
556}
557
558static void wacom_remove_shared_data(struct wacom_wac *wacom)
559{
560 struct wacom_usbdev_data *data;
561
562 if (wacom->shared) {
563 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
564 kref_put(&data->kref, wacom_release_shared_data);
565 wacom->shared = NULL;
566 }
567}
568
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700569static int wacom_led_control(struct wacom *wacom)
570{
571 unsigned char *buf;
Ping Cheng09e7d942011-10-04 23:51:14 -0700572 int retval, led = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700573
574 buf = kzalloc(9, GFP_KERNEL);
575 if (!buf)
576 return -ENOMEM;
577
Jason Gerecke246835f2011-12-12 00:12:04 -0800578 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
579 wacom->wacom_wac.features.type == WACOM_24HD)
Ping Cheng09e7d942011-10-04 23:51:14 -0700580 led = (wacom->led.select[1] << 4) | 0x40;
581
582 led |= wacom->led.select[0] | 0x4;
583
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700584 buf[0] = WAC_CMD_LED_CONTROL;
Ping Cheng09e7d942011-10-04 23:51:14 -0700585 buf[1] = led;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700586 buf[2] = wacom->led.llv;
587 buf[3] = wacom->led.hlv;
588 buf[4] = wacom->led.img_lum;
589
590 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
591 buf, 9, WAC_CMD_RETRIES);
592 kfree(buf);
593
594 return retval;
595}
596
597static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
598{
599 unsigned char *buf;
600 int i, retval;
601
602 buf = kzalloc(259, GFP_KERNEL);
603 if (!buf)
604 return -ENOMEM;
605
606 /* Send 'start' command */
607 buf[0] = WAC_CMD_ICON_START;
608 buf[1] = 1;
609 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
610 buf, 2, WAC_CMD_RETRIES);
611 if (retval < 0)
612 goto out;
613
614 buf[0] = WAC_CMD_ICON_XFER;
615 buf[1] = button_id & 0x07;
616 for (i = 0; i < 4; i++) {
617 buf[2] = i;
618 memcpy(buf + 3, img + i * 256, 256);
619
620 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
621 buf, 259, WAC_CMD_RETRIES);
622 if (retval < 0)
623 break;
624 }
625
626 /* Send 'stop' */
627 buf[0] = WAC_CMD_ICON_START;
628 buf[1] = 0;
629 wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
630 buf, 2, WAC_CMD_RETRIES);
631
632out:
633 kfree(buf);
634 return retval;
635}
636
Ping Cheng09e7d942011-10-04 23:51:14 -0700637static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700638 const char *buf, size_t count)
639{
640 struct wacom *wacom = dev_get_drvdata(dev);
641 unsigned int id;
642 int err;
643
644 err = kstrtouint(buf, 10, &id);
645 if (err)
646 return err;
647
648 mutex_lock(&wacom->lock);
649
Ping Cheng09e7d942011-10-04 23:51:14 -0700650 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700651 err = wacom_led_control(wacom);
652
653 mutex_unlock(&wacom->lock);
654
655 return err < 0 ? err : count;
656}
657
Ping Cheng09e7d942011-10-04 23:51:14 -0700658#define DEVICE_LED_SELECT_ATTR(SET_ID) \
659static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
660 struct device_attribute *attr, const char *buf, size_t count) \
661{ \
662 return wacom_led_select_store(dev, SET_ID, buf, count); \
663} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700664static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
665 struct device_attribute *attr, char *buf) \
666{ \
667 struct wacom *wacom = dev_get_drvdata(dev); \
668 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
669} \
670static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
671 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700672 wacom_led##SET_ID##_select_store)
673
674DEVICE_LED_SELECT_ATTR(0);
675DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700676
677static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
678 const char *buf, size_t count)
679{
680 unsigned int value;
681 int err;
682
683 err = kstrtouint(buf, 10, &value);
684 if (err)
685 return err;
686
687 mutex_lock(&wacom->lock);
688
689 *dest = value & 0x7f;
690 err = wacom_led_control(wacom);
691
692 mutex_unlock(&wacom->lock);
693
694 return err < 0 ? err : count;
695}
696
697#define DEVICE_LUMINANCE_ATTR(name, field) \
698static ssize_t wacom_##name##_luminance_store(struct device *dev, \
699 struct device_attribute *attr, const char *buf, size_t count) \
700{ \
701 struct wacom *wacom = dev_get_drvdata(dev); \
702 \
703 return wacom_luminance_store(wacom, &wacom->led.field, \
704 buf, count); \
705} \
706static DEVICE_ATTR(name##_luminance, S_IWUSR, \
707 NULL, wacom_##name##_luminance_store)
708
709DEVICE_LUMINANCE_ATTR(status0, llv);
710DEVICE_LUMINANCE_ATTR(status1, hlv);
711DEVICE_LUMINANCE_ATTR(buttons, img_lum);
712
713static ssize_t wacom_button_image_store(struct device *dev, int button_id,
714 const char *buf, size_t count)
715{
716 struct wacom *wacom = dev_get_drvdata(dev);
717 int err;
718
719 if (count != 1024)
720 return -EINVAL;
721
722 mutex_lock(&wacom->lock);
723
724 err = wacom_led_putimage(wacom, button_id, buf);
725
726 mutex_unlock(&wacom->lock);
727
728 return err < 0 ? err : count;
729}
730
731#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
732static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
733 struct device_attribute *attr, const char *buf, size_t count) \
734{ \
735 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
736} \
737static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
738 NULL, wacom_btnimg##BUTTON_ID##_store)
739
740DEVICE_BTNIMG_ATTR(0);
741DEVICE_BTNIMG_ATTR(1);
742DEVICE_BTNIMG_ATTR(2);
743DEVICE_BTNIMG_ATTR(3);
744DEVICE_BTNIMG_ATTR(4);
745DEVICE_BTNIMG_ATTR(5);
746DEVICE_BTNIMG_ATTR(6);
747DEVICE_BTNIMG_ATTR(7);
748
Ping Cheng09e7d942011-10-04 23:51:14 -0700749static struct attribute *cintiq_led_attrs[] = {
750 &dev_attr_status_led0_select.attr,
751 &dev_attr_status_led1_select.attr,
752 NULL
753};
754
755static struct attribute_group cintiq_led_attr_group = {
756 .name = "wacom_led",
757 .attrs = cintiq_led_attrs,
758};
759
760static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700761 &dev_attr_status0_luminance.attr,
762 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700763 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700764 &dev_attr_buttons_luminance.attr,
765 &dev_attr_button0_rawimg.attr,
766 &dev_attr_button1_rawimg.attr,
767 &dev_attr_button2_rawimg.attr,
768 &dev_attr_button3_rawimg.attr,
769 &dev_attr_button4_rawimg.attr,
770 &dev_attr_button5_rawimg.attr,
771 &dev_attr_button6_rawimg.attr,
772 &dev_attr_button7_rawimg.attr,
773 NULL
774};
775
Ping Cheng09e7d942011-10-04 23:51:14 -0700776static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700777 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700778 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700779};
780
781static int wacom_initialize_leds(struct wacom *wacom)
782{
783 int error;
784
Ping Cheng09e7d942011-10-04 23:51:14 -0700785 /* Initialize default values */
786 switch (wacom->wacom_wac.features.type) {
787 case INTUOS4:
788 case INTUOS4L:
789 wacom->led.select[0] = 0;
790 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700791 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700792 wacom->led.hlv = 20;
793 wacom->led.img_lum = 10;
Ping Cheng09e7d942011-10-04 23:51:14 -0700794 error = sysfs_create_group(&wacom->intf->dev.kobj,
795 &intuos4_led_attr_group);
796 break;
797
Jason Gerecke246835f2011-12-12 00:12:04 -0800798 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700799 case WACOM_21UX2:
800 wacom->led.select[0] = 0;
801 wacom->led.select[1] = 0;
802 wacom->led.llv = 0;
803 wacom->led.hlv = 0;
804 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700805
806 error = sysfs_create_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700807 &cintiq_led_attr_group);
808 break;
809
810 default:
811 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700812 }
813
Ping Cheng09e7d942011-10-04 23:51:14 -0700814 if (error) {
815 dev_err(&wacom->intf->dev,
816 "cannot create sysfs group err: %d\n", error);
817 return error;
818 }
819 wacom_led_control(wacom);
820
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700821 return 0;
822}
823
824static void wacom_destroy_leds(struct wacom *wacom)
825{
Ping Cheng09e7d942011-10-04 23:51:14 -0700826 switch (wacom->wacom_wac.features.type) {
827 case INTUOS4:
828 case INTUOS4L:
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700829 sysfs_remove_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700830 &intuos4_led_attr_group);
831 break;
832
Jason Gerecke246835f2011-12-12 00:12:04 -0800833 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700834 case WACOM_21UX2:
835 sysfs_remove_group(&wacom->intf->dev.kobj,
836 &cintiq_led_attr_group);
837 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700838 }
839}
840
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700841static int wacom_register_input(struct wacom *wacom)
842{
843 struct input_dev *input_dev;
844 struct usb_interface *intf = wacom->intf;
845 struct usb_device *dev = interface_to_usbdev(intf);
846 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
847 int error;
848
849 input_dev = input_allocate_device();
850 if (!input_dev)
851 return -ENOMEM;
852
853 input_dev->name = wacom_wac->name;
854 input_dev->dev.parent = &intf->dev;
855 input_dev->open = wacom_open;
856 input_dev->close = wacom_close;
857 usb_to_input_id(dev, &input_dev->id);
858 input_set_drvdata(input_dev, wacom);
859
860 wacom_wac->input = input_dev;
861 wacom_setup_input_capabilities(input_dev, wacom_wac);
862
863 error = input_register_device(input_dev);
864 if (error) {
865 input_free_device(input_dev);
866 wacom_wac->input = NULL;
867 }
868
869 return error;
870}
871
Ping Cheng3bea7332006-07-13 18:01:36 -0700872static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
873{
874 struct usb_device *dev = interface_to_usbdev(intf);
875 struct usb_endpoint_descriptor *endpoint;
876 struct wacom *wacom;
877 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800878 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -0800879 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700880
Jason Childse33da8a2010-02-17 22:38:31 -0800881 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -0800882 return -EINVAL;
883
Ping Cheng3bea7332006-07-13 18:01:36 -0700884 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Ping Cheng3bea7332006-07-13 18:01:36 -0700885
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700886 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800887 wacom_wac->features = *((struct wacom_features *)id->driver_info);
888 features = &wacom_wac->features;
889 if (features->pktlen > WACOM_PKGLEN_MAX) {
890 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -0700891 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800892 }
893
Daniel Mack997ea582010-04-12 13:17:25 +0200894 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
895 GFP_KERNEL, &wacom->data_dma);
Jason Childse33da8a2010-02-17 22:38:31 -0800896 if (!wacom_wac->data) {
897 error = -ENOMEM;
898 goto fail1;
899 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700900
901 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -0800902 if (!wacom->irq) {
903 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700904 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -0800905 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700906
907 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -0400908 wacom->intf = intf;
909 mutex_init(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700910 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
911 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
912
Ping Cheng545f4e92008-11-24 11:44:27 -0500913 endpoint = &intf->cur_altsetting->endpoint[0].desc;
914
Ping Chengec67bbe2009-12-15 00:35:24 -0800915 /* Retrieve the physical and logical size for OEM devices */
916 error = wacom_retrieve_hid_descriptor(intf, features);
917 if (error)
Alexander Strakh4b6d4432011-02-11 00:44:41 -0800918 goto fail3;
Ping Cheng545f4e92008-11-24 11:44:27 -0500919
Henrik Rydbergbc73dd32010-09-05 12:26:16 -0700920 wacom_setup_device_quirks(features);
921
Ping Cheng49b764a2010-02-20 00:53:49 -0800922 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
923
Henrik Rydbergbc73dd32010-09-05 12:26:16 -0700924 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -0800925 /* Append the device type to the name */
926 strlcat(wacom_wac->name,
927 features->device_type == BTN_TOOL_PEN ?
928 " Pen" : " Finger",
929 sizeof(wacom_wac->name));
Ping Cheng4492eff2010-03-19 22:18:15 -0700930
931 error = wacom_add_shared_data(wacom_wac, dev);
932 if (error)
933 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -0800934 }
935
Ping Cheng3bea7332006-07-13 18:01:36 -0700936 usb_fill_int_urb(wacom->irq, dev,
937 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -0800938 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700939 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700940 wacom->irq->transfer_dma = wacom->data_dma;
941 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
942
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700943 error = wacom_initialize_leds(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400944 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -0700945 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -0700946
Chris Bagwelld3825d52012-03-25 23:26:11 -0700947 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
948 error = wacom_register_input(wacom);
949 if (error)
950 goto fail5;
951 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700952
Ping Chengec67bbe2009-12-15 00:35:24 -0800953 /* Note that if query fails it is not a hard failure */
954 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -0700955
956 usb_set_intfdata(intf, wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -0700957
958 if (features->quirks & WACOM_QUIRK_MONITOR) {
959 if (usb_submit_urb(wacom->irq, GFP_KERNEL))
960 goto fail5;
961 }
962
Ping Cheng3bea7332006-07-13 18:01:36 -0700963 return 0;
964
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700965 fail5: wacom_destroy_leds(wacom);
Ping Cheng4492eff2010-03-19 22:18:15 -0700966 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400967 fail3: usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200968 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700969 fail1: kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400970 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700971}
972
973static void wacom_disconnect(struct usb_interface *intf)
974{
Oliver Neukume7224092008-04-15 01:31:57 -0400975 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700976
977 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -0400978
979 usb_kill_urb(wacom->irq);
Chris Bagwelld3825d52012-03-25 23:26:11 -0700980 if (wacom->wacom_wac.input)
981 input_unregister_device(wacom->wacom_wac.input);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700982 wacom_destroy_leds(wacom);
Oliver Neukume7224092008-04-15 01:31:57 -0400983 usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200984 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700985 wacom->wacom_wac.data, wacom->data_dma);
986 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -0400987 kfree(wacom);
988}
989
990static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
991{
992 struct wacom *wacom = usb_get_intfdata(intf);
993
994 mutex_lock(&wacom->lock);
995 usb_kill_urb(wacom->irq);
996 mutex_unlock(&wacom->lock);
997
998 return 0;
999}
1000
1001static int wacom_resume(struct usb_interface *intf)
1002{
1003 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001004 struct wacom_features *features = &wacom->wacom_wac.features;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001005 int rv = 0;
Oliver Neukume7224092008-04-15 01:31:57 -04001006
1007 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -07001008
1009 /* switch to wacom mode first */
1010 wacom_query_tablet_data(intf, features);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001011 wacom_led_control(wacom);
Ping Cheng38101472010-04-13 23:07:52 -07001012
Chris Bagwelld3825d52012-03-25 23:26:11 -07001013 if ((wacom->open || features->quirks & WACOM_QUIRK_MONITOR)
1014 && usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001015 rv = -EIO;
Ping Cheng38101472010-04-13 23:07:52 -07001016
Oliver Neukume7224092008-04-15 01:31:57 -04001017 mutex_unlock(&wacom->lock);
1018
1019 return rv;
1020}
1021
1022static int wacom_reset_resume(struct usb_interface *intf)
1023{
1024 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001025}
1026
1027static struct usb_driver wacom_driver = {
1028 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001029 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001030 .probe = wacom_probe,
1031 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -04001032 .suspend = wacom_suspend,
1033 .resume = wacom_resume,
1034 .reset_resume = wacom_reset_resume,
1035 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -07001036};
1037
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -08001038module_usb_driver(wacom_driver);