blob: 0d269212931e86be95d4c77c26b667ac781200c2 [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 Bagwell16bf2882012-03-25 23:26:20 -0700170/*
171 * Static values for max X/Y and resolution of Pen interface is stored in
172 * features. This mean physical size of active area can be computed.
173 * This is useful to do when Pen and Touch have same active area of tablet.
174 * This means for Touch device, we only need to find max X/Y value and we
175 * have enough information to compute resolution of touch.
176 */
177static void wacom_set_phy_from_res(struct wacom_features *features)
178{
179 features->x_phy = (features->x_max * 100) / features->x_resolution;
180 features->y_phy = (features->y_max * 100) / features->y_resolution;
181}
182
Chris Bagwell41343612011-10-26 22:32:52 -0700183static int wacom_parse_logical_collection(unsigned char *report,
184 struct wacom_features *features)
185{
186 int length = 0;
187
188 if (features->type == BAMBOO_PT) {
189
190 /* Logical collection is only used by 3rd gen Bamboo Touch */
191 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
Ping Cheng8b4a0c1f2012-01-31 00:07:33 -0800192 features->device_type = BTN_TOOL_FINGER;
Chris Bagwell41343612011-10-26 22:32:52 -0700193
Chris Bagwell16bf2882012-03-25 23:26:20 -0700194 wacom_set_phy_from_res(features);
Chris Bagwell41343612011-10-26 22:32:52 -0700195
196 features->x_max = features->y_max =
197 get_unaligned_le16(&report[10]);
198
199 length = 11;
200 }
201 return length;
202}
203
Chris Bagwell428f8582011-10-26 22:26:59 -0700204/*
205 * Interface Descriptor of wacom devices can be incomplete and
206 * inconsistent so wacom_features table is used to store stylus
207 * device's packet lengths, various maximum values, and tablet
208 * resolution based on product ID's.
209 *
210 * For devices that contain 2 interfaces, wacom_features table is
211 * inaccurate for the touch interface. Since the Interface Descriptor
212 * for touch interfaces has pretty complete data, this function exists
213 * to query tablet for this missing information instead of hard coding in
214 * an additional table.
215 *
216 * A typical Interface Descriptor for a stylus will contain a
217 * boot mouse application collection that is not of interest and this
218 * function will ignore it.
219 *
220 * It also contains a digitizer application collection that also is not
221 * of interest since any information it contains would be duplicate
222 * of what is in wacom_features. Usually it defines a report of an array
223 * of bytes that could be used as max length of the stylus packet returned.
224 * If it happens to define a Digitizer-Stylus Physical Collection then
225 * the X and Y logical values contain valid data but it is ignored.
226 *
227 * A typical Interface Descriptor for a touch interface will contain a
228 * Digitizer-Finger Physical Collection which will define both logical
229 * X/Y maximum as well as the physical size of tablet. Since touch
230 * interfaces haven't supported pressure or distance, this is enough
231 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700232 *
233 * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
234 * Collection. Instead they define a Logical Collection with a single
235 * Logical Maximum for both X and Y.
Chris Bagwell428f8582011-10-26 22:26:59 -0700236 */
237static int wacom_parse_hid(struct usb_interface *intf,
238 struct hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800239 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500240{
241 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800242 char limit = 0;
243 /* result has to be defined as int for some devices */
244 int result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500245 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
246 unsigned char *report;
247
248 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
249 if (!report)
250 return -ENOMEM;
251
252 /* retrive report descriptors */
253 do {
254 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
255 USB_REQ_GET_DESCRIPTOR,
256 USB_RECIP_INTERFACE | USB_DIR_IN,
257 HID_DEVICET_REPORT << 8,
258 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
259 report,
260 hid_desc->wDescriptorLength,
261 5000); /* 5 secs */
Ping Chenga417ea42011-08-16 00:17:56 -0700262 } while (result < 0 && limit++ < WAC_MSG_RETRIES);
Ping Cheng545f4e92008-11-24 11:44:27 -0500263
Ping Cheng384318e2009-04-28 07:49:54 -0700264 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500265 if (result < 0)
266 goto out;
267
268 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
269
270 switch (report[i]) {
271 case HID_USAGE_PAGE:
272 switch (report[i + 1]) {
273 case HID_USAGE_PAGE_DIGITIZER:
274 usage = WCM_DIGITIZER;
275 i++;
276 break;
277
278 case HID_USAGE_PAGE_DESKTOP:
279 usage = WCM_DESKTOP;
280 i++;
281 break;
282 }
283 break;
284
285 case HID_USAGE:
286 switch (report[i + 1]) {
287 case HID_USAGE_X:
288 if (usage == WCM_DESKTOP) {
289 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800290 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800291 if (features->type == TABLETPC2FG) {
292 /* need to reset back */
293 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Chengec67bbe2009-12-15 00:35:24 -0800294 }
Ping Cheng4a880812010-09-05 12:25:40 -0700295 if (features->type == BAMBOO_PT) {
296 /* need to reset back */
297 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng4a880812010-09-05 12:25:40 -0700298 features->x_phy =
299 get_unaligned_le16(&report[i + 5]);
300 features->x_max =
301 get_unaligned_le16(&report[i + 8]);
302 i += 15;
303 } else {
304 features->x_max =
305 get_unaligned_le16(&report[i + 3]);
306 features->x_phy =
307 get_unaligned_le16(&report[i + 6]);
308 features->unit = report[i + 9];
309 features->unitExpo = report[i + 11];
310 i += 12;
311 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500312 } else if (pen) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800313 /* penabled only accepts exact bytes of data */
314 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800315 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800316 features->device_type = BTN_TOOL_PEN;
Ping Cheng545f4e92008-11-24 11:44:27 -0500317 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700318 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500319 i += 4;
320 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500321 }
322 break;
323
324 case HID_USAGE_Y:
Ping Chengec67bbe2009-12-15 00:35:24 -0800325 if (usage == WCM_DESKTOP) {
326 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800327 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800328 if (features->type == TABLETPC2FG) {
329 /* need to reset back */
330 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Chengec67bbe2009-12-15 00:35:24 -0800331 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700332 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800333 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700334 get_unaligned_le16(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800335 i += 7;
Ping Cheng4a880812010-09-05 12:25:40 -0700336 } else if (features->type == BAMBOO_PT) {
337 /* need to reset back */
338 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng4a880812010-09-05 12:25:40 -0700339 features->y_phy =
340 get_unaligned_le16(&report[i + 3]);
341 features->y_max =
342 get_unaligned_le16(&report[i + 6]);
343 i += 12;
Ping Chengec67bbe2009-12-15 00:35:24 -0800344 } else {
345 features->y_max =
346 features->x_max;
347 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700348 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800349 i += 4;
350 }
351 } else if (pen) {
352 /* penabled only accepts exact bytes of data */
353 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800354 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800355 features->device_type = BTN_TOOL_PEN;
356 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700357 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800358 i += 4;
359 }
360 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500361 break;
362
363 case HID_USAGE_FINGER:
364 finger = 1;
365 i++;
366 break;
367
Chris Bagwell428f8582011-10-26 22:26:59 -0700368 /*
369 * Requiring Stylus Usage will ignore boot mouse
370 * X/Y values and some cases of invalid Digitizer X/Y
371 * values commonly reported.
372 */
Ping Cheng545f4e92008-11-24 11:44:27 -0500373 case HID_USAGE_STYLUS:
374 pen = 1;
375 i++;
376 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500377 }
378 break;
379
Chris Bagwell41343612011-10-26 22:32:52 -0700380 case HID_COLLECTION_END:
Ping Chengec67bbe2009-12-15 00:35:24 -0800381 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500382 finger = usage = 0;
383 break;
Chris Bagwell41343612011-10-26 22:32:52 -0700384
385 case HID_COLLECTION:
386 i++;
387 switch (report[i]) {
388 case HID_COLLECTION_LOGICAL:
389 i += wacom_parse_logical_collection(&report[i],
390 features);
391 break;
392 }
393 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500394 }
395 }
396
Ping Cheng545f4e92008-11-24 11:44:27 -0500397 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700398 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500399 kfree(report);
400 return result;
401}
402
Ping Chengec67bbe2009-12-15 00:35:24 -0800403static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700404{
405 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800406 int limit = 0, report_id = 2;
407 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700408
Ping Cheng3b48c912011-08-16 00:17:57 -0700409 rep_data = kmalloc(4, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700410 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800411 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700412
Ping Cheng3b48c912011-08-16 00:17:57 -0700413 /* ask to report tablet data if it is MT Tablet PC or
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700414 * not a Tablet PC */
415 if (features->type == TABLETPC2FG) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800416 do {
417 rep_data[0] = 3;
418 rep_data[1] = 4;
Ping Cheng3b48c912011-08-16 00:17:57 -0700419 rep_data[2] = 0;
420 rep_data[3] = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800421 report_id = 3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700422 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
423 report_id, rep_data, 4, 1);
Ping Chengec67bbe2009-12-15 00:35:24 -0800424 if (error >= 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700425 error = wacom_get_report(intf,
426 WAC_HID_FEATURE_REPORT,
427 report_id, rep_data, 4, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700428 } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
Chris Bagwell6e8ec532011-10-26 22:24:22 -0700429 } else if (features->type != TABLETPC &&
Chris Bagwelld3825d52012-03-25 23:26:11 -0700430 features->type != WIRELESS &&
Chris Bagwell6e8ec532011-10-26 22:24:22 -0700431 features->device_type == BTN_TOOL_PEN) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800432 do {
433 rep_data[0] = 2;
434 rep_data[1] = 2;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700435 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
436 report_id, rep_data, 2, 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, 2, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700441 } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
Ping Chengec67bbe2009-12-15 00:35:24 -0800442 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700443
444 kfree(rep_data);
445
446 return error < 0 ? error : 0;
447}
448
Ping Chengec67bbe2009-12-15 00:35:24 -0800449static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
450 struct wacom_features *features)
451{
452 int error = 0;
453 struct usb_host_interface *interface = intf->cur_altsetting;
454 struct hid_descriptor *hid_desc;
455
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700456 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800457 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700458 features->x_fuzz = 4;
459 features->y_fuzz = 4;
460 features->pressure_fuzz = 0;
461 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800462
Chris Bagwelld3825d52012-03-25 23:26:11 -0700463 /*
464 * The wireless device HID is basic and layout conflicts with
465 * other tablets (monitor and touch interface can look like pen).
466 * Skip the query for this type and modify defaults based on
467 * interface number.
468 */
469 if (features->type == WIRELESS) {
470 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
471 features->device_type = 0;
472 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
473 features->device_type = BTN_TOOL_DOUBLETAP;
474 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
475 }
476 }
477
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700478 /* only Tablet PCs and Bamboo P&T need to retrieve the info */
Ping Cheng4a880812010-09-05 12:25:40 -0700479 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
480 (features->type != BAMBOO_PT))
Ping Chengec67bbe2009-12-15 00:35:24 -0800481 goto out;
482
483 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
484 if (usb_get_extra_descriptor(&interface->endpoint[0],
485 HID_DEVICET_REPORT, &hid_desc)) {
486 printk("wacom: can not retrieve extra class descriptor\n");
487 error = 1;
488 goto out;
489 }
490 }
491 error = wacom_parse_hid(intf, hid_desc, features);
492 if (error)
493 goto out;
494
Ping Chengec67bbe2009-12-15 00:35:24 -0800495 out:
496 return error;
497}
498
Ping Cheng4492eff2010-03-19 22:18:15 -0700499struct wacom_usbdev_data {
500 struct list_head list;
501 struct kref kref;
502 struct usb_device *dev;
503 struct wacom_shared shared;
504};
505
506static LIST_HEAD(wacom_udev_list);
507static DEFINE_MUTEX(wacom_udev_list_lock);
508
509static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
510{
511 struct wacom_usbdev_data *data;
512
513 list_for_each_entry(data, &wacom_udev_list, list) {
514 if (data->dev == dev) {
515 kref_get(&data->kref);
516 return data;
517 }
518 }
519
520 return NULL;
521}
522
523static int wacom_add_shared_data(struct wacom_wac *wacom,
524 struct usb_device *dev)
525{
526 struct wacom_usbdev_data *data;
527 int retval = 0;
528
529 mutex_lock(&wacom_udev_list_lock);
530
531 data = wacom_get_usbdev_data(dev);
532 if (!data) {
533 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
534 if (!data) {
535 retval = -ENOMEM;
536 goto out;
537 }
538
539 kref_init(&data->kref);
540 data->dev = dev;
541 list_add_tail(&data->list, &wacom_udev_list);
542 }
543
544 wacom->shared = &data->shared;
545
546out:
547 mutex_unlock(&wacom_udev_list_lock);
548 return retval;
549}
550
551static void wacom_release_shared_data(struct kref *kref)
552{
553 struct wacom_usbdev_data *data =
554 container_of(kref, struct wacom_usbdev_data, kref);
555
556 mutex_lock(&wacom_udev_list_lock);
557 list_del(&data->list);
558 mutex_unlock(&wacom_udev_list_lock);
559
560 kfree(data);
561}
562
563static void wacom_remove_shared_data(struct wacom_wac *wacom)
564{
565 struct wacom_usbdev_data *data;
566
567 if (wacom->shared) {
568 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
569 kref_put(&data->kref, wacom_release_shared_data);
570 wacom->shared = NULL;
571 }
572}
573
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700574static int wacom_led_control(struct wacom *wacom)
575{
576 unsigned char *buf;
Ping Cheng09e7d942011-10-04 23:51:14 -0700577 int retval, led = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700578
579 buf = kzalloc(9, GFP_KERNEL);
580 if (!buf)
581 return -ENOMEM;
582
Jason Gerecke246835f2011-12-12 00:12:04 -0800583 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
584 wacom->wacom_wac.features.type == WACOM_24HD)
Ping Cheng09e7d942011-10-04 23:51:14 -0700585 led = (wacom->led.select[1] << 4) | 0x40;
586
587 led |= wacom->led.select[0] | 0x4;
588
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700589 buf[0] = WAC_CMD_LED_CONTROL;
Ping Cheng09e7d942011-10-04 23:51:14 -0700590 buf[1] = led;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700591 buf[2] = wacom->led.llv;
592 buf[3] = wacom->led.hlv;
593 buf[4] = wacom->led.img_lum;
594
595 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
596 buf, 9, WAC_CMD_RETRIES);
597 kfree(buf);
598
599 return retval;
600}
601
602static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
603{
604 unsigned char *buf;
605 int i, retval;
606
607 buf = kzalloc(259, GFP_KERNEL);
608 if (!buf)
609 return -ENOMEM;
610
611 /* Send 'start' command */
612 buf[0] = WAC_CMD_ICON_START;
613 buf[1] = 1;
614 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
615 buf, 2, WAC_CMD_RETRIES);
616 if (retval < 0)
617 goto out;
618
619 buf[0] = WAC_CMD_ICON_XFER;
620 buf[1] = button_id & 0x07;
621 for (i = 0; i < 4; i++) {
622 buf[2] = i;
623 memcpy(buf + 3, img + i * 256, 256);
624
625 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
626 buf, 259, WAC_CMD_RETRIES);
627 if (retval < 0)
628 break;
629 }
630
631 /* Send 'stop' */
632 buf[0] = WAC_CMD_ICON_START;
633 buf[1] = 0;
634 wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
635 buf, 2, WAC_CMD_RETRIES);
636
637out:
638 kfree(buf);
639 return retval;
640}
641
Ping Cheng09e7d942011-10-04 23:51:14 -0700642static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700643 const char *buf, size_t count)
644{
645 struct wacom *wacom = dev_get_drvdata(dev);
646 unsigned int id;
647 int err;
648
649 err = kstrtouint(buf, 10, &id);
650 if (err)
651 return err;
652
653 mutex_lock(&wacom->lock);
654
Ping Cheng09e7d942011-10-04 23:51:14 -0700655 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700656 err = wacom_led_control(wacom);
657
658 mutex_unlock(&wacom->lock);
659
660 return err < 0 ? err : count;
661}
662
Ping Cheng09e7d942011-10-04 23:51:14 -0700663#define DEVICE_LED_SELECT_ATTR(SET_ID) \
664static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
665 struct device_attribute *attr, const char *buf, size_t count) \
666{ \
667 return wacom_led_select_store(dev, SET_ID, buf, count); \
668} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700669static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
670 struct device_attribute *attr, char *buf) \
671{ \
672 struct wacom *wacom = dev_get_drvdata(dev); \
673 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
674} \
675static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
676 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700677 wacom_led##SET_ID##_select_store)
678
679DEVICE_LED_SELECT_ATTR(0);
680DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700681
682static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
683 const char *buf, size_t count)
684{
685 unsigned int value;
686 int err;
687
688 err = kstrtouint(buf, 10, &value);
689 if (err)
690 return err;
691
692 mutex_lock(&wacom->lock);
693
694 *dest = value & 0x7f;
695 err = wacom_led_control(wacom);
696
697 mutex_unlock(&wacom->lock);
698
699 return err < 0 ? err : count;
700}
701
702#define DEVICE_LUMINANCE_ATTR(name, field) \
703static ssize_t wacom_##name##_luminance_store(struct device *dev, \
704 struct device_attribute *attr, const char *buf, size_t count) \
705{ \
706 struct wacom *wacom = dev_get_drvdata(dev); \
707 \
708 return wacom_luminance_store(wacom, &wacom->led.field, \
709 buf, count); \
710} \
711static DEVICE_ATTR(name##_luminance, S_IWUSR, \
712 NULL, wacom_##name##_luminance_store)
713
714DEVICE_LUMINANCE_ATTR(status0, llv);
715DEVICE_LUMINANCE_ATTR(status1, hlv);
716DEVICE_LUMINANCE_ATTR(buttons, img_lum);
717
718static ssize_t wacom_button_image_store(struct device *dev, int button_id,
719 const char *buf, size_t count)
720{
721 struct wacom *wacom = dev_get_drvdata(dev);
722 int err;
723
724 if (count != 1024)
725 return -EINVAL;
726
727 mutex_lock(&wacom->lock);
728
729 err = wacom_led_putimage(wacom, button_id, buf);
730
731 mutex_unlock(&wacom->lock);
732
733 return err < 0 ? err : count;
734}
735
736#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
737static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
738 struct device_attribute *attr, const char *buf, size_t count) \
739{ \
740 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
741} \
742static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
743 NULL, wacom_btnimg##BUTTON_ID##_store)
744
745DEVICE_BTNIMG_ATTR(0);
746DEVICE_BTNIMG_ATTR(1);
747DEVICE_BTNIMG_ATTR(2);
748DEVICE_BTNIMG_ATTR(3);
749DEVICE_BTNIMG_ATTR(4);
750DEVICE_BTNIMG_ATTR(5);
751DEVICE_BTNIMG_ATTR(6);
752DEVICE_BTNIMG_ATTR(7);
753
Ping Cheng09e7d942011-10-04 23:51:14 -0700754static struct attribute *cintiq_led_attrs[] = {
755 &dev_attr_status_led0_select.attr,
756 &dev_attr_status_led1_select.attr,
757 NULL
758};
759
760static struct attribute_group cintiq_led_attr_group = {
761 .name = "wacom_led",
762 .attrs = cintiq_led_attrs,
763};
764
765static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700766 &dev_attr_status0_luminance.attr,
767 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700768 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700769 &dev_attr_buttons_luminance.attr,
770 &dev_attr_button0_rawimg.attr,
771 &dev_attr_button1_rawimg.attr,
772 &dev_attr_button2_rawimg.attr,
773 &dev_attr_button3_rawimg.attr,
774 &dev_attr_button4_rawimg.attr,
775 &dev_attr_button5_rawimg.attr,
776 &dev_attr_button6_rawimg.attr,
777 &dev_attr_button7_rawimg.attr,
778 NULL
779};
780
Ping Cheng09e7d942011-10-04 23:51:14 -0700781static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700782 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700783 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700784};
785
786static int wacom_initialize_leds(struct wacom *wacom)
787{
788 int error;
789
Ping Cheng09e7d942011-10-04 23:51:14 -0700790 /* Initialize default values */
791 switch (wacom->wacom_wac.features.type) {
792 case INTUOS4:
793 case INTUOS4L:
794 wacom->led.select[0] = 0;
795 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700796 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700797 wacom->led.hlv = 20;
798 wacom->led.img_lum = 10;
Ping Cheng09e7d942011-10-04 23:51:14 -0700799 error = sysfs_create_group(&wacom->intf->dev.kobj,
800 &intuos4_led_attr_group);
801 break;
802
Jason Gerecke246835f2011-12-12 00:12:04 -0800803 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700804 case WACOM_21UX2:
805 wacom->led.select[0] = 0;
806 wacom->led.select[1] = 0;
807 wacom->led.llv = 0;
808 wacom->led.hlv = 0;
809 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700810
811 error = sysfs_create_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700812 &cintiq_led_attr_group);
813 break;
814
815 default:
816 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700817 }
818
Ping Cheng09e7d942011-10-04 23:51:14 -0700819 if (error) {
820 dev_err(&wacom->intf->dev,
821 "cannot create sysfs group err: %d\n", error);
822 return error;
823 }
824 wacom_led_control(wacom);
825
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700826 return 0;
827}
828
829static void wacom_destroy_leds(struct wacom *wacom)
830{
Ping Cheng09e7d942011-10-04 23:51:14 -0700831 switch (wacom->wacom_wac.features.type) {
832 case INTUOS4:
833 case INTUOS4L:
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700834 sysfs_remove_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700835 &intuos4_led_attr_group);
836 break;
837
Jason Gerecke246835f2011-12-12 00:12:04 -0800838 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700839 case WACOM_21UX2:
840 sysfs_remove_group(&wacom->intf->dev.kobj,
841 &cintiq_led_attr_group);
842 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700843 }
844}
845
Chris Bagwella1d552c2012-03-25 23:26:30 -0700846static enum power_supply_property wacom_battery_props[] = {
847 POWER_SUPPLY_PROP_CAPACITY
848};
849
850static int wacom_battery_get_property(struct power_supply *psy,
851 enum power_supply_property psp,
852 union power_supply_propval *val)
853{
854 struct wacom *wacom = container_of(psy, struct wacom, battery);
855 int ret = 0;
856
857 switch (psp) {
858 case POWER_SUPPLY_PROP_CAPACITY:
859 val->intval =
860 wacom->wacom_wac.battery_capacity * 100 / 31;
861 break;
862 default:
863 ret = -EINVAL;
864 break;
865 }
866
867 return ret;
868}
869
870static int wacom_initialize_battery(struct wacom *wacom)
871{
872 int error = 0;
873
874 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) {
875 wacom->battery.properties = wacom_battery_props;
876 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
877 wacom->battery.get_property = wacom_battery_get_property;
878 wacom->battery.name = "wacom_battery";
879 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
880 wacom->battery.use_for_apm = 0;
881
882 error = power_supply_register(&wacom->usbdev->dev,
883 &wacom->battery);
884 }
885
886 return error;
887}
888
889static void wacom_destroy_battery(struct wacom *wacom)
890{
891 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR)
892 power_supply_unregister(&wacom->battery);
893}
894
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700895static int wacom_register_input(struct wacom *wacom)
896{
897 struct input_dev *input_dev;
898 struct usb_interface *intf = wacom->intf;
899 struct usb_device *dev = interface_to_usbdev(intf);
900 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
901 int error;
902
903 input_dev = input_allocate_device();
904 if (!input_dev)
905 return -ENOMEM;
906
907 input_dev->name = wacom_wac->name;
908 input_dev->dev.parent = &intf->dev;
909 input_dev->open = wacom_open;
910 input_dev->close = wacom_close;
911 usb_to_input_id(dev, &input_dev->id);
912 input_set_drvdata(input_dev, wacom);
913
914 wacom_wac->input = input_dev;
915 wacom_setup_input_capabilities(input_dev, wacom_wac);
916
917 error = input_register_device(input_dev);
918 if (error) {
919 input_free_device(input_dev);
920 wacom_wac->input = NULL;
921 }
922
923 return error;
924}
925
Chris Bagwell16bf2882012-03-25 23:26:20 -0700926static void wacom_wireless_work(struct work_struct *work)
927{
928 struct wacom *wacom = container_of(work, struct wacom, work);
929 struct usb_device *usbdev = wacom->usbdev;
930 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
931
932 /*
933 * Regardless if this is a disconnect or a new tablet,
934 * remove any existing input devices.
935 */
936
937 /* Stylus interface */
938 wacom = usb_get_intfdata(usbdev->config->interface[1]);
939 if (wacom->wacom_wac.input)
940 input_unregister_device(wacom->wacom_wac.input);
941 wacom->wacom_wac.input = 0;
942
943 /* Touch interface */
944 wacom = usb_get_intfdata(usbdev->config->interface[2]);
945 if (wacom->wacom_wac.input)
946 input_unregister_device(wacom->wacom_wac.input);
947 wacom->wacom_wac.input = 0;
948
949 if (wacom_wac->pid == 0) {
950 printk(KERN_INFO "wacom: wireless tablet disconnected\n");
951 } else {
952 const struct usb_device_id *id = wacom_ids;
953
954 printk(KERN_INFO
955 "wacom: wireless tablet connected with PID %x\n",
956 wacom_wac->pid);
957
958 while (id->match_flags) {
959 if (id->idVendor == USB_VENDOR_ID_WACOM &&
960 id->idProduct == wacom_wac->pid)
961 break;
962 id++;
963 }
964
965 if (!id->match_flags) {
966 printk(KERN_INFO
967 "wacom: ignorning unknown PID.\n");
968 return;
969 }
970
971 /* Stylus interface */
972 wacom = usb_get_intfdata(usbdev->config->interface[1]);
973 wacom_wac = &wacom->wacom_wac;
974 wacom_wac->features =
975 *((struct wacom_features *)id->driver_info);
976 wacom_wac->features.device_type = BTN_TOOL_PEN;
977 wacom_register_input(wacom);
978
979 /* Touch interface */
980 wacom = usb_get_intfdata(usbdev->config->interface[2]);
981 wacom_wac = &wacom->wacom_wac;
982 wacom_wac->features =
983 *((struct wacom_features *)id->driver_info);
984 wacom_wac->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
985 wacom_wac->features.device_type = BTN_TOOL_FINGER;
986 wacom_set_phy_from_res(&wacom_wac->features);
987 wacom_wac->features.x_max = wacom_wac->features.y_max = 4096;
988 wacom_register_input(wacom);
989 }
990}
991
Ping Cheng3bea7332006-07-13 18:01:36 -0700992static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
993{
994 struct usb_device *dev = interface_to_usbdev(intf);
995 struct usb_endpoint_descriptor *endpoint;
996 struct wacom *wacom;
997 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800998 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -0800999 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001000
Jason Childse33da8a2010-02-17 22:38:31 -08001001 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001002 return -EINVAL;
1003
Ping Cheng3bea7332006-07-13 18:01:36 -07001004 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001005 if (!wacom)
1006 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001007
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001008 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001009 wacom_wac->features = *((struct wacom_features *)id->driver_info);
1010 features = &wacom_wac->features;
1011 if (features->pktlen > WACOM_PKGLEN_MAX) {
1012 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001013 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -08001014 }
1015
Daniel Mack997ea582010-04-12 13:17:25 +02001016 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
1017 GFP_KERNEL, &wacom->data_dma);
Jason Childse33da8a2010-02-17 22:38:31 -08001018 if (!wacom_wac->data) {
1019 error = -ENOMEM;
1020 goto fail1;
1021 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001022
1023 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -08001024 if (!wacom->irq) {
1025 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001026 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -08001027 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001028
1029 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001030 wacom->intf = intf;
1031 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001032 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001033 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
1034 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
1035
Ping Cheng545f4e92008-11-24 11:44:27 -05001036 endpoint = &intf->cur_altsetting->endpoint[0].desc;
1037
Ping Chengec67bbe2009-12-15 00:35:24 -08001038 /* Retrieve the physical and logical size for OEM devices */
1039 error = wacom_retrieve_hid_descriptor(intf, features);
1040 if (error)
Alexander Strakh4b6d4432011-02-11 00:44:41 -08001041 goto fail3;
Ping Cheng545f4e92008-11-24 11:44:27 -05001042
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001043 wacom_setup_device_quirks(features);
1044
Ping Cheng49b764a2010-02-20 00:53:49 -08001045 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1046
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001047 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -08001048 /* Append the device type to the name */
1049 strlcat(wacom_wac->name,
1050 features->device_type == BTN_TOOL_PEN ?
1051 " Pen" : " Finger",
1052 sizeof(wacom_wac->name));
Ping Cheng4492eff2010-03-19 22:18:15 -07001053
1054 error = wacom_add_shared_data(wacom_wac, dev);
1055 if (error)
1056 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -08001057 }
1058
Ping Cheng3bea7332006-07-13 18:01:36 -07001059 usb_fill_int_urb(wacom->irq, dev,
1060 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -08001061 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -07001062 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -07001063 wacom->irq->transfer_dma = wacom->data_dma;
1064 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1065
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001066 error = wacom_initialize_leds(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001067 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -07001068 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -07001069
Chris Bagwella1d552c2012-03-25 23:26:30 -07001070 error = wacom_initialize_battery(wacom);
1071 if (error)
1072 goto fail5;
1073
Chris Bagwelld3825d52012-03-25 23:26:11 -07001074 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1075 error = wacom_register_input(wacom);
1076 if (error)
Chris Bagwella1d552c2012-03-25 23:26:30 -07001077 goto fail6;
Chris Bagwelld3825d52012-03-25 23:26:11 -07001078 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001079
Ping Chengec67bbe2009-12-15 00:35:24 -08001080 /* Note that if query fails it is not a hard failure */
1081 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -07001082
1083 usb_set_intfdata(intf, wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001084
1085 if (features->quirks & WACOM_QUIRK_MONITOR) {
1086 if (usb_submit_urb(wacom->irq, GFP_KERNEL))
1087 goto fail5;
1088 }
1089
Ping Cheng3bea7332006-07-13 18:01:36 -07001090 return 0;
1091
Chris Bagwella1d552c2012-03-25 23:26:30 -07001092 fail6: wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001093 fail5: wacom_destroy_leds(wacom);
Ping Cheng4492eff2010-03-19 22:18:15 -07001094 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001095 fail3: usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +02001096 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001097 fail1: kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001098 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001099}
1100
1101static void wacom_disconnect(struct usb_interface *intf)
1102{
Oliver Neukume7224092008-04-15 01:31:57 -04001103 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001104
1105 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001106
1107 usb_kill_urb(wacom->irq);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001108 cancel_work_sync(&wacom->work);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001109 if (wacom->wacom_wac.input)
1110 input_unregister_device(wacom->wacom_wac.input);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001111 wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001112 wacom_destroy_leds(wacom);
Oliver Neukume7224092008-04-15 01:31:57 -04001113 usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +02001114 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001115 wacom->wacom_wac.data, wacom->data_dma);
1116 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -04001117 kfree(wacom);
1118}
1119
1120static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
1121{
1122 struct wacom *wacom = usb_get_intfdata(intf);
1123
1124 mutex_lock(&wacom->lock);
1125 usb_kill_urb(wacom->irq);
1126 mutex_unlock(&wacom->lock);
1127
1128 return 0;
1129}
1130
1131static int wacom_resume(struct usb_interface *intf)
1132{
1133 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001134 struct wacom_features *features = &wacom->wacom_wac.features;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001135 int rv = 0;
Oliver Neukume7224092008-04-15 01:31:57 -04001136
1137 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -07001138
1139 /* switch to wacom mode first */
1140 wacom_query_tablet_data(intf, features);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001141 wacom_led_control(wacom);
Ping Cheng38101472010-04-13 23:07:52 -07001142
Chris Bagwelld3825d52012-03-25 23:26:11 -07001143 if ((wacom->open || features->quirks & WACOM_QUIRK_MONITOR)
1144 && usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001145 rv = -EIO;
Ping Cheng38101472010-04-13 23:07:52 -07001146
Oliver Neukume7224092008-04-15 01:31:57 -04001147 mutex_unlock(&wacom->lock);
1148
1149 return rv;
1150}
1151
1152static int wacom_reset_resume(struct usb_interface *intf)
1153{
1154 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001155}
1156
1157static struct usb_driver wacom_driver = {
1158 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001159 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001160 .probe = wacom_probe,
1161 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -04001162 .suspend = wacom_suspend,
1163 .resume = wacom_resume,
1164 .reset_resume = wacom_reset_resume,
1165 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -07001166};
1167
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -08001168module_usb_driver(wacom_driver);