blob: 2a97b7e76db1d33310820502f8e51ae01576af64 [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 Bunk54c9b222006-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;
179 features->device_type = BTN_TOOL_DOUBLETAP;
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 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 Cheng84eb5aa2011-03-12 20:35:18 -0800289 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Chengec67bbe2009-12-15 00:35:24 -0800290 }
Ping Cheng4a880812010-09-05 12:25:40 -0700291 if (features->type == BAMBOO_PT) {
292 /* need to reset back */
293 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800294 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Cheng4a880812010-09-05 12:25:40 -0700295 features->x_phy =
296 get_unaligned_le16(&report[i + 5]);
297 features->x_max =
298 get_unaligned_le16(&report[i + 8]);
299 i += 15;
300 } else {
301 features->x_max =
302 get_unaligned_le16(&report[i + 3]);
303 features->x_phy =
304 get_unaligned_le16(&report[i + 6]);
305 features->unit = report[i + 9];
306 features->unitExpo = report[i + 11];
307 i += 12;
308 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500309 } else if (pen) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800310 /* penabled only accepts exact bytes of data */
311 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800312 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800313 features->device_type = BTN_TOOL_PEN;
Ping Cheng545f4e92008-11-24 11:44:27 -0500314 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700315 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500316 i += 4;
317 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500318 }
319 break;
320
321 case HID_USAGE_Y:
Ping Chengec67bbe2009-12-15 00:35:24 -0800322 if (usage == WCM_DESKTOP) {
323 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800324 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800325 if (features->type == TABLETPC2FG) {
326 /* need to reset back */
327 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800328 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Chengec67bbe2009-12-15 00:35:24 -0800329 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700330 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800331 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700332 get_unaligned_le16(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800333 i += 7;
Ping Cheng4a880812010-09-05 12:25:40 -0700334 } else if (features->type == BAMBOO_PT) {
335 /* need to reset back */
336 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800337 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Cheng4a880812010-09-05 12:25:40 -0700338 features->y_phy =
339 get_unaligned_le16(&report[i + 3]);
340 features->y_max =
341 get_unaligned_le16(&report[i + 6]);
342 i += 12;
Ping Chengec67bbe2009-12-15 00:35:24 -0800343 } else {
344 features->y_max =
345 features->x_max;
346 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700347 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800348 i += 4;
349 }
350 } else if (pen) {
351 /* penabled only accepts exact bytes of data */
352 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800353 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800354 features->device_type = BTN_TOOL_PEN;
355 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700356 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800357 i += 4;
358 }
359 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500360 break;
361
362 case HID_USAGE_FINGER:
363 finger = 1;
364 i++;
365 break;
366
Chris Bagwell428f8582011-10-26 22:26:59 -0700367 /*
368 * Requiring Stylus Usage will ignore boot mouse
369 * X/Y values and some cases of invalid Digitizer X/Y
370 * values commonly reported.
371 */
Ping Cheng545f4e92008-11-24 11:44:27 -0500372 case HID_USAGE_STYLUS:
373 pen = 1;
374 i++;
375 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500376 }
377 break;
378
Chris Bagwell41343612011-10-26 22:32:52 -0700379 case HID_COLLECTION_END:
Ping Chengec67bbe2009-12-15 00:35:24 -0800380 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500381 finger = usage = 0;
382 break;
Chris Bagwell41343612011-10-26 22:32:52 -0700383
384 case HID_COLLECTION:
385 i++;
386 switch (report[i]) {
387 case HID_COLLECTION_LOGICAL:
388 i += wacom_parse_logical_collection(&report[i],
389 features);
390 break;
391 }
392 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500393 }
394 }
395
Ping Cheng545f4e92008-11-24 11:44:27 -0500396 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700397 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500398 kfree(report);
399 return result;
400}
401
Ping Chengec67bbe2009-12-15 00:35:24 -0800402static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700403{
404 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800405 int limit = 0, report_id = 2;
406 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700407
Ping Cheng3b48c912011-08-16 00:17:57 -0700408 rep_data = kmalloc(4, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700409 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800410 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700411
Ping Cheng3b48c912011-08-16 00:17:57 -0700412 /* ask to report tablet data if it is MT Tablet PC or
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700413 * not a Tablet PC */
414 if (features->type == TABLETPC2FG) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800415 do {
416 rep_data[0] = 3;
417 rep_data[1] = 4;
Ping Cheng3b48c912011-08-16 00:17:57 -0700418 rep_data[2] = 0;
419 rep_data[3] = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800420 report_id = 3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700421 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
422 report_id, rep_data, 4, 1);
Ping Chengec67bbe2009-12-15 00:35:24 -0800423 if (error >= 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700424 error = wacom_get_report(intf,
425 WAC_HID_FEATURE_REPORT,
426 report_id, rep_data, 4, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700427 } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
Chris Bagwell6e8ec532011-10-26 22:24:22 -0700428 } else if (features->type != TABLETPC &&
429 features->device_type == BTN_TOOL_PEN) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800430 do {
431 rep_data[0] = 2;
432 rep_data[1] = 2;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700433 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
434 report_id, rep_data, 2, 1);
Ping Chengec67bbe2009-12-15 00:35:24 -0800435 if (error >= 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700436 error = wacom_get_report(intf,
437 WAC_HID_FEATURE_REPORT,
438 report_id, rep_data, 2, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700439 } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
Ping Chengec67bbe2009-12-15 00:35:24 -0800440 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700441
442 kfree(rep_data);
443
444 return error < 0 ? error : 0;
445}
446
Ping Chengec67bbe2009-12-15 00:35:24 -0800447static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
448 struct wacom_features *features)
449{
450 int error = 0;
451 struct usb_host_interface *interface = intf->cur_altsetting;
452 struct hid_descriptor *hid_desc;
453
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700454 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800455 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700456 features->x_fuzz = 4;
457 features->y_fuzz = 4;
458 features->pressure_fuzz = 0;
459 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800460
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700461 /* only Tablet PCs and Bamboo P&T need to retrieve the info */
Ping Cheng4a880812010-09-05 12:25:40 -0700462 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
463 (features->type != BAMBOO_PT))
Ping Chengec67bbe2009-12-15 00:35:24 -0800464 goto out;
465
466 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
467 if (usb_get_extra_descriptor(&interface->endpoint[0],
468 HID_DEVICET_REPORT, &hid_desc)) {
469 printk("wacom: can not retrieve extra class descriptor\n");
470 error = 1;
471 goto out;
472 }
473 }
474 error = wacom_parse_hid(intf, hid_desc, features);
475 if (error)
476 goto out;
477
Ping Chengec67bbe2009-12-15 00:35:24 -0800478 out:
479 return error;
480}
481
Ping Cheng4492eff2010-03-19 22:18:15 -0700482struct wacom_usbdev_data {
483 struct list_head list;
484 struct kref kref;
485 struct usb_device *dev;
486 struct wacom_shared shared;
487};
488
489static LIST_HEAD(wacom_udev_list);
490static DEFINE_MUTEX(wacom_udev_list_lock);
491
492static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
493{
494 struct wacom_usbdev_data *data;
495
496 list_for_each_entry(data, &wacom_udev_list, list) {
497 if (data->dev == dev) {
498 kref_get(&data->kref);
499 return data;
500 }
501 }
502
503 return NULL;
504}
505
506static int wacom_add_shared_data(struct wacom_wac *wacom,
507 struct usb_device *dev)
508{
509 struct wacom_usbdev_data *data;
510 int retval = 0;
511
512 mutex_lock(&wacom_udev_list_lock);
513
514 data = wacom_get_usbdev_data(dev);
515 if (!data) {
516 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
517 if (!data) {
518 retval = -ENOMEM;
519 goto out;
520 }
521
522 kref_init(&data->kref);
523 data->dev = dev;
524 list_add_tail(&data->list, &wacom_udev_list);
525 }
526
527 wacom->shared = &data->shared;
528
529out:
530 mutex_unlock(&wacom_udev_list_lock);
531 return retval;
532}
533
534static void wacom_release_shared_data(struct kref *kref)
535{
536 struct wacom_usbdev_data *data =
537 container_of(kref, struct wacom_usbdev_data, kref);
538
539 mutex_lock(&wacom_udev_list_lock);
540 list_del(&data->list);
541 mutex_unlock(&wacom_udev_list_lock);
542
543 kfree(data);
544}
545
546static void wacom_remove_shared_data(struct wacom_wac *wacom)
547{
548 struct wacom_usbdev_data *data;
549
550 if (wacom->shared) {
551 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
552 kref_put(&data->kref, wacom_release_shared_data);
553 wacom->shared = NULL;
554 }
555}
556
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700557static int wacom_led_control(struct wacom *wacom)
558{
559 unsigned char *buf;
Ping Cheng09e7d942011-10-04 23:51:14 -0700560 int retval, led = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700561
562 buf = kzalloc(9, GFP_KERNEL);
563 if (!buf)
564 return -ENOMEM;
565
Jason Gerecke246835f2011-12-12 00:12:04 -0800566 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
567 wacom->wacom_wac.features.type == WACOM_24HD)
Ping Cheng09e7d942011-10-04 23:51:14 -0700568 led = (wacom->led.select[1] << 4) | 0x40;
569
570 led |= wacom->led.select[0] | 0x4;
571
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700572 buf[0] = WAC_CMD_LED_CONTROL;
Ping Cheng09e7d942011-10-04 23:51:14 -0700573 buf[1] = led;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700574 buf[2] = wacom->led.llv;
575 buf[3] = wacom->led.hlv;
576 buf[4] = wacom->led.img_lum;
577
578 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
579 buf, 9, WAC_CMD_RETRIES);
580 kfree(buf);
581
582 return retval;
583}
584
585static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
586{
587 unsigned char *buf;
588 int i, retval;
589
590 buf = kzalloc(259, GFP_KERNEL);
591 if (!buf)
592 return -ENOMEM;
593
594 /* Send 'start' command */
595 buf[0] = WAC_CMD_ICON_START;
596 buf[1] = 1;
597 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
598 buf, 2, WAC_CMD_RETRIES);
599 if (retval < 0)
600 goto out;
601
602 buf[0] = WAC_CMD_ICON_XFER;
603 buf[1] = button_id & 0x07;
604 for (i = 0; i < 4; i++) {
605 buf[2] = i;
606 memcpy(buf + 3, img + i * 256, 256);
607
608 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
609 buf, 259, WAC_CMD_RETRIES);
610 if (retval < 0)
611 break;
612 }
613
614 /* Send 'stop' */
615 buf[0] = WAC_CMD_ICON_START;
616 buf[1] = 0;
617 wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
618 buf, 2, WAC_CMD_RETRIES);
619
620out:
621 kfree(buf);
622 return retval;
623}
624
Ping Cheng09e7d942011-10-04 23:51:14 -0700625static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700626 const char *buf, size_t count)
627{
628 struct wacom *wacom = dev_get_drvdata(dev);
629 unsigned int id;
630 int err;
631
632 err = kstrtouint(buf, 10, &id);
633 if (err)
634 return err;
635
636 mutex_lock(&wacom->lock);
637
Ping Cheng09e7d942011-10-04 23:51:14 -0700638 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700639 err = wacom_led_control(wacom);
640
641 mutex_unlock(&wacom->lock);
642
643 return err < 0 ? err : count;
644}
645
Ping Cheng09e7d942011-10-04 23:51:14 -0700646#define DEVICE_LED_SELECT_ATTR(SET_ID) \
647static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
648 struct device_attribute *attr, const char *buf, size_t count) \
649{ \
650 return wacom_led_select_store(dev, SET_ID, buf, count); \
651} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700652static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
653 struct device_attribute *attr, char *buf) \
654{ \
655 struct wacom *wacom = dev_get_drvdata(dev); \
656 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
657} \
658static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
659 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700660 wacom_led##SET_ID##_select_store)
661
662DEVICE_LED_SELECT_ATTR(0);
663DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700664
665static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
666 const char *buf, size_t count)
667{
668 unsigned int value;
669 int err;
670
671 err = kstrtouint(buf, 10, &value);
672 if (err)
673 return err;
674
675 mutex_lock(&wacom->lock);
676
677 *dest = value & 0x7f;
678 err = wacom_led_control(wacom);
679
680 mutex_unlock(&wacom->lock);
681
682 return err < 0 ? err : count;
683}
684
685#define DEVICE_LUMINANCE_ATTR(name, field) \
686static ssize_t wacom_##name##_luminance_store(struct device *dev, \
687 struct device_attribute *attr, const char *buf, size_t count) \
688{ \
689 struct wacom *wacom = dev_get_drvdata(dev); \
690 \
691 return wacom_luminance_store(wacom, &wacom->led.field, \
692 buf, count); \
693} \
694static DEVICE_ATTR(name##_luminance, S_IWUSR, \
695 NULL, wacom_##name##_luminance_store)
696
697DEVICE_LUMINANCE_ATTR(status0, llv);
698DEVICE_LUMINANCE_ATTR(status1, hlv);
699DEVICE_LUMINANCE_ATTR(buttons, img_lum);
700
701static ssize_t wacom_button_image_store(struct device *dev, int button_id,
702 const char *buf, size_t count)
703{
704 struct wacom *wacom = dev_get_drvdata(dev);
705 int err;
706
707 if (count != 1024)
708 return -EINVAL;
709
710 mutex_lock(&wacom->lock);
711
712 err = wacom_led_putimage(wacom, button_id, buf);
713
714 mutex_unlock(&wacom->lock);
715
716 return err < 0 ? err : count;
717}
718
719#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
720static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
721 struct device_attribute *attr, const char *buf, size_t count) \
722{ \
723 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
724} \
725static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
726 NULL, wacom_btnimg##BUTTON_ID##_store)
727
728DEVICE_BTNIMG_ATTR(0);
729DEVICE_BTNIMG_ATTR(1);
730DEVICE_BTNIMG_ATTR(2);
731DEVICE_BTNIMG_ATTR(3);
732DEVICE_BTNIMG_ATTR(4);
733DEVICE_BTNIMG_ATTR(5);
734DEVICE_BTNIMG_ATTR(6);
735DEVICE_BTNIMG_ATTR(7);
736
Ping Cheng09e7d942011-10-04 23:51:14 -0700737static struct attribute *cintiq_led_attrs[] = {
738 &dev_attr_status_led0_select.attr,
739 &dev_attr_status_led1_select.attr,
740 NULL
741};
742
743static struct attribute_group cintiq_led_attr_group = {
744 .name = "wacom_led",
745 .attrs = cintiq_led_attrs,
746};
747
748static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700749 &dev_attr_status0_luminance.attr,
750 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700751 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700752 &dev_attr_buttons_luminance.attr,
753 &dev_attr_button0_rawimg.attr,
754 &dev_attr_button1_rawimg.attr,
755 &dev_attr_button2_rawimg.attr,
756 &dev_attr_button3_rawimg.attr,
757 &dev_attr_button4_rawimg.attr,
758 &dev_attr_button5_rawimg.attr,
759 &dev_attr_button6_rawimg.attr,
760 &dev_attr_button7_rawimg.attr,
761 NULL
762};
763
Ping Cheng09e7d942011-10-04 23:51:14 -0700764static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700765 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700766 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700767};
768
769static int wacom_initialize_leds(struct wacom *wacom)
770{
771 int error;
772
Ping Cheng09e7d942011-10-04 23:51:14 -0700773 /* Initialize default values */
774 switch (wacom->wacom_wac.features.type) {
775 case INTUOS4:
776 case INTUOS4L:
777 wacom->led.select[0] = 0;
778 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700779 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700780 wacom->led.hlv = 20;
781 wacom->led.img_lum = 10;
Ping Cheng09e7d942011-10-04 23:51:14 -0700782 error = sysfs_create_group(&wacom->intf->dev.kobj,
783 &intuos4_led_attr_group);
784 break;
785
Jason Gerecke246835f2011-12-12 00:12:04 -0800786 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700787 case WACOM_21UX2:
788 wacom->led.select[0] = 0;
789 wacom->led.select[1] = 0;
790 wacom->led.llv = 0;
791 wacom->led.hlv = 0;
792 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700793
794 error = sysfs_create_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700795 &cintiq_led_attr_group);
796 break;
797
798 default:
799 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700800 }
801
Ping Cheng09e7d942011-10-04 23:51:14 -0700802 if (error) {
803 dev_err(&wacom->intf->dev,
804 "cannot create sysfs group err: %d\n", error);
805 return error;
806 }
807 wacom_led_control(wacom);
808
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700809 return 0;
810}
811
812static void wacom_destroy_leds(struct wacom *wacom)
813{
Ping Cheng09e7d942011-10-04 23:51:14 -0700814 switch (wacom->wacom_wac.features.type) {
815 case INTUOS4:
816 case INTUOS4L:
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700817 sysfs_remove_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700818 &intuos4_led_attr_group);
819 break;
820
Jason Gerecke246835f2011-12-12 00:12:04 -0800821 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700822 case WACOM_21UX2:
823 sysfs_remove_group(&wacom->intf->dev.kobj,
824 &cintiq_led_attr_group);
825 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700826 }
827}
828
Ping Cheng3bea7332006-07-13 18:01:36 -0700829static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
830{
831 struct usb_device *dev = interface_to_usbdev(intf);
832 struct usb_endpoint_descriptor *endpoint;
833 struct wacom *wacom;
834 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800835 struct wacom_features *features;
Ping Cheng3bea7332006-07-13 18:01:36 -0700836 struct input_dev *input_dev;
Jason Childse33da8a2010-02-17 22:38:31 -0800837 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700838
Jason Childse33da8a2010-02-17 22:38:31 -0800839 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -0800840 return -EINVAL;
841
Ping Cheng3bea7332006-07-13 18:01:36 -0700842 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Ping Cheng3bea7332006-07-13 18:01:36 -0700843 input_dev = input_allocate_device();
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700844 if (!wacom || !input_dev) {
Jason Childse33da8a2010-02-17 22:38:31 -0800845 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700846 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800847 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700848
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700849 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800850 wacom_wac->features = *((struct wacom_features *)id->driver_info);
851 features = &wacom_wac->features;
852 if (features->pktlen > WACOM_PKGLEN_MAX) {
853 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -0700854 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800855 }
856
Daniel Mack997ea582010-04-12 13:17:25 +0200857 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
858 GFP_KERNEL, &wacom->data_dma);
Jason Childse33da8a2010-02-17 22:38:31 -0800859 if (!wacom_wac->data) {
860 error = -ENOMEM;
861 goto fail1;
862 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700863
864 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -0800865 if (!wacom->irq) {
866 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700867 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -0800868 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700869
870 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -0400871 wacom->intf = intf;
872 mutex_init(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700873 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
874 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
875
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700876 wacom_wac->input = input_dev;
Ping Cheng3bea7332006-07-13 18:01:36 -0700877
Ping Cheng545f4e92008-11-24 11:44:27 -0500878 endpoint = &intf->cur_altsetting->endpoint[0].desc;
879
Ping Chengec67bbe2009-12-15 00:35:24 -0800880 /* Retrieve the physical and logical size for OEM devices */
881 error = wacom_retrieve_hid_descriptor(intf, features);
882 if (error)
Alexander Strakh4b6d4432011-02-11 00:44:41 -0800883 goto fail3;
Ping Cheng545f4e92008-11-24 11:44:27 -0500884
Henrik Rydbergbc73dd32010-09-05 12:26:16 -0700885 wacom_setup_device_quirks(features);
886
Ping Cheng49b764a2010-02-20 00:53:49 -0800887 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
888
Henrik Rydbergbc73dd32010-09-05 12:26:16 -0700889 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -0800890 /* Append the device type to the name */
891 strlcat(wacom_wac->name,
892 features->device_type == BTN_TOOL_PEN ?
893 " Pen" : " Finger",
894 sizeof(wacom_wac->name));
Ping Cheng4492eff2010-03-19 22:18:15 -0700895
896 error = wacom_add_shared_data(wacom_wac, dev);
897 if (error)
898 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -0800899 }
900
901 input_dev->name = wacom_wac->name;
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700902 input_dev->dev.parent = &intf->dev;
903 input_dev->open = wacom_open;
904 input_dev->close = wacom_close;
905 usb_to_input_id(dev, &input_dev->id);
906 input_set_drvdata(input_dev, wacom);
Jason Childse33da8a2010-02-17 22:38:31 -0800907
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700908 wacom_setup_input_capabilities(input_dev, wacom_wac);
Ping Cheng3bea7332006-07-13 18:01:36 -0700909
Ping Cheng3bea7332006-07-13 18:01:36 -0700910 usb_fill_int_urb(wacom->irq, dev,
911 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -0800912 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700913 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700914 wacom->irq->transfer_dma = wacom->data_dma;
915 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
916
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700917 error = wacom_initialize_leds(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400918 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -0700919 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -0700920
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700921 error = input_register_device(input_dev);
922 if (error)
923 goto fail5;
924
Ping Chengec67bbe2009-12-15 00:35:24 -0800925 /* Note that if query fails it is not a hard failure */
926 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -0700927
928 usb_set_intfdata(intf, wacom);
929 return 0;
930
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700931 fail5: wacom_destroy_leds(wacom);
Ping Cheng4492eff2010-03-19 22:18:15 -0700932 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400933 fail3: usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200934 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400935 fail1: input_free_device(input_dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700936 kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400937 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700938}
939
940static void wacom_disconnect(struct usb_interface *intf)
941{
Oliver Neukume7224092008-04-15 01:31:57 -0400942 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700943
944 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -0400945
946 usb_kill_urb(wacom->irq);
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700947 input_unregister_device(wacom->wacom_wac.input);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700948 wacom_destroy_leds(wacom);
Oliver Neukume7224092008-04-15 01:31:57 -0400949 usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200950 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700951 wacom->wacom_wac.data, wacom->data_dma);
952 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -0400953 kfree(wacom);
954}
955
956static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
957{
958 struct wacom *wacom = usb_get_intfdata(intf);
959
960 mutex_lock(&wacom->lock);
961 usb_kill_urb(wacom->irq);
962 mutex_unlock(&wacom->lock);
963
964 return 0;
965}
966
967static int wacom_resume(struct usb_interface *intf)
968{
969 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700970 struct wacom_features *features = &wacom->wacom_wac.features;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700971 int rv = 0;
Oliver Neukume7224092008-04-15 01:31:57 -0400972
973 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -0700974
975 /* switch to wacom mode first */
976 wacom_query_tablet_data(intf, features);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700977 wacom_led_control(wacom);
Ping Cheng38101472010-04-13 23:07:52 -0700978
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700979 if (wacom->open && usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
980 rv = -EIO;
Ping Cheng38101472010-04-13 23:07:52 -0700981
Oliver Neukume7224092008-04-15 01:31:57 -0400982 mutex_unlock(&wacom->lock);
983
984 return rv;
985}
986
987static int wacom_reset_resume(struct usb_interface *intf)
988{
989 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700990}
991
992static struct usb_driver wacom_driver = {
993 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -0800994 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -0700995 .probe = wacom_probe,
996 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -0400997 .suspend = wacom_suspend,
998 .resume = wacom_resume,
999 .reset_resume = wacom_reset_resume,
1000 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -07001001};
1002
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -08001003module_usb_driver(wacom_driver);