blob: ab498e480f3bf4627c153972c447d24faf22e9ea [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 }
318 } else if (usage == WCM_DIGITIZER) {
319 /* max pressure isn't reported
320 features->pressure_max = (unsigned short)
321 (report[i+4] << 8 | report[i + 3]);
322 */
323 features->pressure_max = 255;
324 i += 4;
325 }
326 break;
327
328 case HID_USAGE_Y:
Ping Chengec67bbe2009-12-15 00:35:24 -0800329 if (usage == WCM_DESKTOP) {
330 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800331 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800332 if (features->type == TABLETPC2FG) {
333 /* need to reset back */
334 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800335 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Chengec67bbe2009-12-15 00:35:24 -0800336 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700337 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800338 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700339 get_unaligned_le16(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800340 i += 7;
Ping Cheng4a880812010-09-05 12:25:40 -0700341 } else if (features->type == BAMBOO_PT) {
342 /* need to reset back */
343 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800344 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Cheng4a880812010-09-05 12:25:40 -0700345 features->y_phy =
346 get_unaligned_le16(&report[i + 3]);
347 features->y_max =
348 get_unaligned_le16(&report[i + 6]);
349 i += 12;
Ping Chengec67bbe2009-12-15 00:35:24 -0800350 } else {
351 features->y_max =
352 features->x_max;
353 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700354 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800355 i += 4;
356 }
357 } else if (pen) {
358 /* penabled only accepts exact bytes of data */
359 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800360 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800361 features->device_type = BTN_TOOL_PEN;
362 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700363 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800364 i += 4;
365 }
366 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500367 break;
368
369 case HID_USAGE_FINGER:
370 finger = 1;
371 i++;
372 break;
373
Chris Bagwell428f8582011-10-26 22:26:59 -0700374 /*
375 * Requiring Stylus Usage will ignore boot mouse
376 * X/Y values and some cases of invalid Digitizer X/Y
377 * values commonly reported.
378 */
Ping Cheng545f4e92008-11-24 11:44:27 -0500379 case HID_USAGE_STYLUS:
380 pen = 1;
381 i++;
382 break;
383
384 case HID_USAGE_UNDEFINED:
385 if (usage == WCM_DESKTOP && finger) /* capacity */
386 features->pressure_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700387 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500388 i += 4;
389 break;
390 }
391 break;
392
Chris Bagwell41343612011-10-26 22:32:52 -0700393 case HID_COLLECTION_END:
Ping Chengec67bbe2009-12-15 00:35:24 -0800394 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500395 finger = usage = 0;
396 break;
Chris Bagwell41343612011-10-26 22:32:52 -0700397
398 case HID_COLLECTION:
399 i++;
400 switch (report[i]) {
401 case HID_COLLECTION_LOGICAL:
402 i += wacom_parse_logical_collection(&report[i],
403 features);
404 break;
405 }
406 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500407 }
408 }
409
Ping Cheng545f4e92008-11-24 11:44:27 -0500410 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700411 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500412 kfree(report);
413 return result;
414}
415
Ping Chengec67bbe2009-12-15 00:35:24 -0800416static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700417{
418 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800419 int limit = 0, report_id = 2;
420 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700421
Ping Cheng3b48c912011-08-16 00:17:57 -0700422 rep_data = kmalloc(4, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700423 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800424 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700425
Ping Cheng3b48c912011-08-16 00:17:57 -0700426 /* ask to report tablet data if it is MT Tablet PC or
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700427 * not a Tablet PC */
428 if (features->type == TABLETPC2FG) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800429 do {
430 rep_data[0] = 3;
431 rep_data[1] = 4;
Ping Cheng3b48c912011-08-16 00:17:57 -0700432 rep_data[2] = 0;
433 rep_data[3] = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800434 report_id = 3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700435 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
436 report_id, rep_data, 4, 1);
Ping Chengec67bbe2009-12-15 00:35:24 -0800437 if (error >= 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700438 error = wacom_get_report(intf,
439 WAC_HID_FEATURE_REPORT,
440 report_id, rep_data, 4, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700441 } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
Chris Bagwell6e8ec532011-10-26 22:24:22 -0700442 } else if (features->type != TABLETPC &&
443 features->device_type == BTN_TOOL_PEN) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800444 do {
445 rep_data[0] = 2;
446 rep_data[1] = 2;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700447 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
448 report_id, rep_data, 2, 1);
Ping Chengec67bbe2009-12-15 00:35:24 -0800449 if (error >= 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700450 error = wacom_get_report(intf,
451 WAC_HID_FEATURE_REPORT,
452 report_id, rep_data, 2, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700453 } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
Ping Chengec67bbe2009-12-15 00:35:24 -0800454 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700455
456 kfree(rep_data);
457
458 return error < 0 ? error : 0;
459}
460
Ping Chengec67bbe2009-12-15 00:35:24 -0800461static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
462 struct wacom_features *features)
463{
464 int error = 0;
465 struct usb_host_interface *interface = intf->cur_altsetting;
466 struct hid_descriptor *hid_desc;
467
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700468 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800469 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700470 features->x_fuzz = 4;
471 features->y_fuzz = 4;
472 features->pressure_fuzz = 0;
473 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800474
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700475 /* only Tablet PCs and Bamboo P&T need to retrieve the info */
Ping Cheng4a880812010-09-05 12:25:40 -0700476 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
477 (features->type != BAMBOO_PT))
Ping Chengec67bbe2009-12-15 00:35:24 -0800478 goto out;
479
480 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
481 if (usb_get_extra_descriptor(&interface->endpoint[0],
482 HID_DEVICET_REPORT, &hid_desc)) {
483 printk("wacom: can not retrieve extra class descriptor\n");
484 error = 1;
485 goto out;
486 }
487 }
488 error = wacom_parse_hid(intf, hid_desc, features);
489 if (error)
490 goto out;
491
Ping Chengec67bbe2009-12-15 00:35:24 -0800492 out:
493 return error;
494}
495
Ping Cheng4492eff2010-03-19 22:18:15 -0700496struct wacom_usbdev_data {
497 struct list_head list;
498 struct kref kref;
499 struct usb_device *dev;
500 struct wacom_shared shared;
501};
502
503static LIST_HEAD(wacom_udev_list);
504static DEFINE_MUTEX(wacom_udev_list_lock);
505
506static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
507{
508 struct wacom_usbdev_data *data;
509
510 list_for_each_entry(data, &wacom_udev_list, list) {
511 if (data->dev == dev) {
512 kref_get(&data->kref);
513 return data;
514 }
515 }
516
517 return NULL;
518}
519
520static int wacom_add_shared_data(struct wacom_wac *wacom,
521 struct usb_device *dev)
522{
523 struct wacom_usbdev_data *data;
524 int retval = 0;
525
526 mutex_lock(&wacom_udev_list_lock);
527
528 data = wacom_get_usbdev_data(dev);
529 if (!data) {
530 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
531 if (!data) {
532 retval = -ENOMEM;
533 goto out;
534 }
535
536 kref_init(&data->kref);
537 data->dev = dev;
538 list_add_tail(&data->list, &wacom_udev_list);
539 }
540
541 wacom->shared = &data->shared;
542
543out:
544 mutex_unlock(&wacom_udev_list_lock);
545 return retval;
546}
547
548static void wacom_release_shared_data(struct kref *kref)
549{
550 struct wacom_usbdev_data *data =
551 container_of(kref, struct wacom_usbdev_data, kref);
552
553 mutex_lock(&wacom_udev_list_lock);
554 list_del(&data->list);
555 mutex_unlock(&wacom_udev_list_lock);
556
557 kfree(data);
558}
559
560static void wacom_remove_shared_data(struct wacom_wac *wacom)
561{
562 struct wacom_usbdev_data *data;
563
564 if (wacom->shared) {
565 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
566 kref_put(&data->kref, wacom_release_shared_data);
567 wacom->shared = NULL;
568 }
569}
570
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700571static int wacom_led_control(struct wacom *wacom)
572{
573 unsigned char *buf;
Ping Cheng09e7d942011-10-04 23:51:14 -0700574 int retval, led = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700575
576 buf = kzalloc(9, GFP_KERNEL);
577 if (!buf)
578 return -ENOMEM;
579
Ping Cheng09e7d942011-10-04 23:51:14 -0700580 if (wacom->wacom_wac.features.type == WACOM_21UX2)
581 led = (wacom->led.select[1] << 4) | 0x40;
582
583 led |= wacom->led.select[0] | 0x4;
584
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700585 buf[0] = WAC_CMD_LED_CONTROL;
Ping Cheng09e7d942011-10-04 23:51:14 -0700586 buf[1] = led;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700587 buf[2] = wacom->led.llv;
588 buf[3] = wacom->led.hlv;
589 buf[4] = wacom->led.img_lum;
590
591 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
592 buf, 9, WAC_CMD_RETRIES);
593 kfree(buf);
594
595 return retval;
596}
597
598static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
599{
600 unsigned char *buf;
601 int i, retval;
602
603 buf = kzalloc(259, GFP_KERNEL);
604 if (!buf)
605 return -ENOMEM;
606
607 /* Send 'start' command */
608 buf[0] = WAC_CMD_ICON_START;
609 buf[1] = 1;
610 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
611 buf, 2, WAC_CMD_RETRIES);
612 if (retval < 0)
613 goto out;
614
615 buf[0] = WAC_CMD_ICON_XFER;
616 buf[1] = button_id & 0x07;
617 for (i = 0; i < 4; i++) {
618 buf[2] = i;
619 memcpy(buf + 3, img + i * 256, 256);
620
621 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
622 buf, 259, WAC_CMD_RETRIES);
623 if (retval < 0)
624 break;
625 }
626
627 /* Send 'stop' */
628 buf[0] = WAC_CMD_ICON_START;
629 buf[1] = 0;
630 wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
631 buf, 2, WAC_CMD_RETRIES);
632
633out:
634 kfree(buf);
635 return retval;
636}
637
Ping Cheng09e7d942011-10-04 23:51:14 -0700638static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700639 const char *buf, size_t count)
640{
641 struct wacom *wacom = dev_get_drvdata(dev);
642 unsigned int id;
643 int err;
644
645 err = kstrtouint(buf, 10, &id);
646 if (err)
647 return err;
648
649 mutex_lock(&wacom->lock);
650
Ping Cheng09e7d942011-10-04 23:51:14 -0700651 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700652 err = wacom_led_control(wacom);
653
654 mutex_unlock(&wacom->lock);
655
656 return err < 0 ? err : count;
657}
658
Ping Cheng09e7d942011-10-04 23:51:14 -0700659#define DEVICE_LED_SELECT_ATTR(SET_ID) \
660static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
661 struct device_attribute *attr, const char *buf, size_t count) \
662{ \
663 return wacom_led_select_store(dev, SET_ID, buf, count); \
664} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700665static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
666 struct device_attribute *attr, char *buf) \
667{ \
668 struct wacom *wacom = dev_get_drvdata(dev); \
669 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
670} \
671static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
672 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700673 wacom_led##SET_ID##_select_store)
674
675DEVICE_LED_SELECT_ATTR(0);
676DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700677
678static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
679 const char *buf, size_t count)
680{
681 unsigned int value;
682 int err;
683
684 err = kstrtouint(buf, 10, &value);
685 if (err)
686 return err;
687
688 mutex_lock(&wacom->lock);
689
690 *dest = value & 0x7f;
691 err = wacom_led_control(wacom);
692
693 mutex_unlock(&wacom->lock);
694
695 return err < 0 ? err : count;
696}
697
698#define DEVICE_LUMINANCE_ATTR(name, field) \
699static ssize_t wacom_##name##_luminance_store(struct device *dev, \
700 struct device_attribute *attr, const char *buf, size_t count) \
701{ \
702 struct wacom *wacom = dev_get_drvdata(dev); \
703 \
704 return wacom_luminance_store(wacom, &wacom->led.field, \
705 buf, count); \
706} \
707static DEVICE_ATTR(name##_luminance, S_IWUSR, \
708 NULL, wacom_##name##_luminance_store)
709
710DEVICE_LUMINANCE_ATTR(status0, llv);
711DEVICE_LUMINANCE_ATTR(status1, hlv);
712DEVICE_LUMINANCE_ATTR(buttons, img_lum);
713
714static ssize_t wacom_button_image_store(struct device *dev, int button_id,
715 const char *buf, size_t count)
716{
717 struct wacom *wacom = dev_get_drvdata(dev);
718 int err;
719
720 if (count != 1024)
721 return -EINVAL;
722
723 mutex_lock(&wacom->lock);
724
725 err = wacom_led_putimage(wacom, button_id, buf);
726
727 mutex_unlock(&wacom->lock);
728
729 return err < 0 ? err : count;
730}
731
732#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
733static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
734 struct device_attribute *attr, const char *buf, size_t count) \
735{ \
736 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
737} \
738static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
739 NULL, wacom_btnimg##BUTTON_ID##_store)
740
741DEVICE_BTNIMG_ATTR(0);
742DEVICE_BTNIMG_ATTR(1);
743DEVICE_BTNIMG_ATTR(2);
744DEVICE_BTNIMG_ATTR(3);
745DEVICE_BTNIMG_ATTR(4);
746DEVICE_BTNIMG_ATTR(5);
747DEVICE_BTNIMG_ATTR(6);
748DEVICE_BTNIMG_ATTR(7);
749
Ping Cheng09e7d942011-10-04 23:51:14 -0700750static struct attribute *cintiq_led_attrs[] = {
751 &dev_attr_status_led0_select.attr,
752 &dev_attr_status_led1_select.attr,
753 NULL
754};
755
756static struct attribute_group cintiq_led_attr_group = {
757 .name = "wacom_led",
758 .attrs = cintiq_led_attrs,
759};
760
761static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700762 &dev_attr_status0_luminance.attr,
763 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700764 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700765 &dev_attr_buttons_luminance.attr,
766 &dev_attr_button0_rawimg.attr,
767 &dev_attr_button1_rawimg.attr,
768 &dev_attr_button2_rawimg.attr,
769 &dev_attr_button3_rawimg.attr,
770 &dev_attr_button4_rawimg.attr,
771 &dev_attr_button5_rawimg.attr,
772 &dev_attr_button6_rawimg.attr,
773 &dev_attr_button7_rawimg.attr,
774 NULL
775};
776
Ping Cheng09e7d942011-10-04 23:51:14 -0700777static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700778 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700779 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700780};
781
782static int wacom_initialize_leds(struct wacom *wacom)
783{
784 int error;
785
Ping Cheng09e7d942011-10-04 23:51:14 -0700786 /* Initialize default values */
787 switch (wacom->wacom_wac.features.type) {
788 case INTUOS4:
789 case INTUOS4L:
790 wacom->led.select[0] = 0;
791 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700792 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700793 wacom->led.hlv = 20;
794 wacom->led.img_lum = 10;
Ping Cheng09e7d942011-10-04 23:51:14 -0700795 error = sysfs_create_group(&wacom->intf->dev.kobj,
796 &intuos4_led_attr_group);
797 break;
798
799 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
833 case WACOM_21UX2:
834 sysfs_remove_group(&wacom->intf->dev.kobj,
835 &cintiq_led_attr_group);
836 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700837 }
838}
839
Ping Cheng3bea7332006-07-13 18:01:36 -0700840static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
841{
842 struct usb_device *dev = interface_to_usbdev(intf);
843 struct usb_endpoint_descriptor *endpoint;
844 struct wacom *wacom;
845 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800846 struct wacom_features *features;
Ping Cheng3bea7332006-07-13 18:01:36 -0700847 struct input_dev *input_dev;
Jason Childse33da8a2010-02-17 22:38:31 -0800848 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700849
Jason Childse33da8a2010-02-17 22:38:31 -0800850 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -0800851 return -EINVAL;
852
Ping Cheng3bea7332006-07-13 18:01:36 -0700853 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Ping Cheng3bea7332006-07-13 18:01:36 -0700854 input_dev = input_allocate_device();
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700855 if (!wacom || !input_dev) {
Jason Childse33da8a2010-02-17 22:38:31 -0800856 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700857 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800858 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700859
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700860 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800861 wacom_wac->features = *((struct wacom_features *)id->driver_info);
862 features = &wacom_wac->features;
863 if (features->pktlen > WACOM_PKGLEN_MAX) {
864 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -0700865 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800866 }
867
Daniel Mack997ea582010-04-12 13:17:25 +0200868 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
869 GFP_KERNEL, &wacom->data_dma);
Jason Childse33da8a2010-02-17 22:38:31 -0800870 if (!wacom_wac->data) {
871 error = -ENOMEM;
872 goto fail1;
873 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700874
875 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -0800876 if (!wacom->irq) {
877 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700878 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -0800879 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700880
881 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -0400882 wacom->intf = intf;
883 mutex_init(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700884 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
885 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
886
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700887 wacom_wac->input = input_dev;
Ping Cheng3bea7332006-07-13 18:01:36 -0700888
Ping Cheng545f4e92008-11-24 11:44:27 -0500889 endpoint = &intf->cur_altsetting->endpoint[0].desc;
890
Ping Chengec67bbe2009-12-15 00:35:24 -0800891 /* Retrieve the physical and logical size for OEM devices */
892 error = wacom_retrieve_hid_descriptor(intf, features);
893 if (error)
Alexander Strakh4b6d4432011-02-11 00:44:41 -0800894 goto fail3;
Ping Cheng545f4e92008-11-24 11:44:27 -0500895
Henrik Rydbergbc73dd32010-09-05 12:26:16 -0700896 wacom_setup_device_quirks(features);
897
Ping Cheng49b764a2010-02-20 00:53:49 -0800898 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
899
Henrik Rydbergbc73dd32010-09-05 12:26:16 -0700900 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -0800901 /* Append the device type to the name */
902 strlcat(wacom_wac->name,
903 features->device_type == BTN_TOOL_PEN ?
904 " Pen" : " Finger",
905 sizeof(wacom_wac->name));
Ping Cheng4492eff2010-03-19 22:18:15 -0700906
907 error = wacom_add_shared_data(wacom_wac, dev);
908 if (error)
909 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -0800910 }
911
912 input_dev->name = wacom_wac->name;
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700913 input_dev->dev.parent = &intf->dev;
914 input_dev->open = wacom_open;
915 input_dev->close = wacom_close;
916 usb_to_input_id(dev, &input_dev->id);
917 input_set_drvdata(input_dev, wacom);
Jason Childse33da8a2010-02-17 22:38:31 -0800918
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700919 wacom_setup_input_capabilities(input_dev, wacom_wac);
Ping Cheng3bea7332006-07-13 18:01:36 -0700920
Ping Cheng3bea7332006-07-13 18:01:36 -0700921 usb_fill_int_urb(wacom->irq, dev,
922 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -0800923 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700924 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700925 wacom->irq->transfer_dma = wacom->data_dma;
926 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
927
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700928 error = wacom_initialize_leds(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400929 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -0700930 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -0700931
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700932 error = input_register_device(input_dev);
933 if (error)
934 goto fail5;
935
Ping Chengec67bbe2009-12-15 00:35:24 -0800936 /* Note that if query fails it is not a hard failure */
937 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -0700938
939 usb_set_intfdata(intf, wacom);
940 return 0;
941
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700942 fail5: wacom_destroy_leds(wacom);
Ping Cheng4492eff2010-03-19 22:18:15 -0700943 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400944 fail3: usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200945 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400946 fail1: input_free_device(input_dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700947 kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400948 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700949}
950
951static void wacom_disconnect(struct usb_interface *intf)
952{
Oliver Neukume7224092008-04-15 01:31:57 -0400953 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700954
955 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -0400956
957 usb_kill_urb(wacom->irq);
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700958 input_unregister_device(wacom->wacom_wac.input);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700959 wacom_destroy_leds(wacom);
Oliver Neukume7224092008-04-15 01:31:57 -0400960 usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200961 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700962 wacom->wacom_wac.data, wacom->data_dma);
963 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -0400964 kfree(wacom);
965}
966
967static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
968{
969 struct wacom *wacom = usb_get_intfdata(intf);
970
971 mutex_lock(&wacom->lock);
972 usb_kill_urb(wacom->irq);
973 mutex_unlock(&wacom->lock);
974
975 return 0;
976}
977
978static int wacom_resume(struct usb_interface *intf)
979{
980 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700981 struct wacom_features *features = &wacom->wacom_wac.features;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700982 int rv = 0;
Oliver Neukume7224092008-04-15 01:31:57 -0400983
984 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -0700985
986 /* switch to wacom mode first */
987 wacom_query_tablet_data(intf, features);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700988 wacom_led_control(wacom);
Ping Cheng38101472010-04-13 23:07:52 -0700989
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700990 if (wacom->open && usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
991 rv = -EIO;
Ping Cheng38101472010-04-13 23:07:52 -0700992
Oliver Neukume7224092008-04-15 01:31:57 -0400993 mutex_unlock(&wacom->lock);
994
995 return rv;
996}
997
998static int wacom_reset_resume(struct usb_interface *intf)
999{
1000 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001001}
1002
1003static struct usb_driver wacom_driver = {
1004 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001005 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001006 .probe = wacom_probe,
1007 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -04001008 .suspend = wacom_suspend,
1009 .resume = wacom_resume,
1010 .reset_resume = wacom_reset_resume,
1011 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -07001012};
1013
1014static int __init wacom_init(void)
1015{
1016 int result;
Bastian Blankb036f6f2010-02-10 23:06:23 -08001017
Ping Cheng3bea7332006-07-13 18:01:36 -07001018 result = usb_register(&wacom_driver);
1019 if (result == 0)
Greg Kroah-Hartman899ef6e2008-08-18 13:21:04 -07001020 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1021 DRIVER_DESC "\n");
Ping Cheng3bea7332006-07-13 18:01:36 -07001022 return result;
1023}
1024
1025static void __exit wacom_exit(void)
1026{
1027 usb_deregister(&wacom_driver);
1028}
1029
1030module_init(wacom_init);
1031module_exit(wacom_exit);