blob: 3db4684a0b77317c928f7e4d83b1800eda18892f [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)
Greg Kroah-Hartmanb3169fe2012-04-25 14:48:44 -0700125 dev_err(&wacom->intf->dev,
126 "%s - usb_submit_urb failed with result %d\n",
127 __func__, retval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700128}
129
Ping Cheng3bea7332006-07-13 18:01:36 -0700130static int wacom_open(struct input_dev *dev)
131{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400132 struct wacom *wacom = input_get_drvdata(dev);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700133 int retval = 0;
134
135 if (usb_autopm_get_interface(wacom->intf) < 0)
136 return -EIO;
Ping Cheng3bea7332006-07-13 18:01:36 -0700137
Oliver Neukume7224092008-04-15 01:31:57 -0400138 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700139
Oliver Neukume7224092008-04-15 01:31:57 -0400140 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700141 retval = -EIO;
142 goto out;
Oliver Neukume7224092008-04-15 01:31:57 -0400143 }
144
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700145 wacom->open = true;
Oliver Neukume7224092008-04-15 01:31:57 -0400146 wacom->intf->needs_remote_wakeup = 1;
147
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700148out:
Oliver Neukume7224092008-04-15 01:31:57 -0400149 mutex_unlock(&wacom->lock);
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700150 usb_autopm_put_interface(wacom->intf);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700151 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -0700152}
153
154static void wacom_close(struct input_dev *dev)
155{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400156 struct wacom *wacom = input_get_drvdata(dev);
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700157 int autopm_error;
158
159 autopm_error = usb_autopm_get_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700160
Oliver Neukume7224092008-04-15 01:31:57 -0400161 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700162 usb_kill_urb(wacom->irq);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700163 wacom->open = false;
Oliver Neukume7224092008-04-15 01:31:57 -0400164 wacom->intf->needs_remote_wakeup = 0;
165 mutex_unlock(&wacom->lock);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700166
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700167 if (!autopm_error)
168 usb_autopm_put_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700169}
170
Chris Bagwell16bf2882012-03-25 23:26:20 -0700171/*
172 * Static values for max X/Y and resolution of Pen interface is stored in
173 * features. This mean physical size of active area can be computed.
174 * This is useful to do when Pen and Touch have same active area of tablet.
175 * This means for Touch device, we only need to find max X/Y value and we
176 * have enough information to compute resolution of touch.
177 */
178static void wacom_set_phy_from_res(struct wacom_features *features)
179{
180 features->x_phy = (features->x_max * 100) / features->x_resolution;
181 features->y_phy = (features->y_max * 100) / features->y_resolution;
182}
183
Chris Bagwell41343612011-10-26 22:32:52 -0700184static int wacom_parse_logical_collection(unsigned char *report,
185 struct wacom_features *features)
186{
187 int length = 0;
188
189 if (features->type == BAMBOO_PT) {
190
191 /* Logical collection is only used by 3rd gen Bamboo Touch */
192 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
Ping Cheng8b4a0c1f2012-01-31 00:07:33 -0800193 features->device_type = BTN_TOOL_FINGER;
Chris Bagwell41343612011-10-26 22:32:52 -0700194
Chris Bagwell16bf2882012-03-25 23:26:20 -0700195 wacom_set_phy_from_res(features);
Chris Bagwell41343612011-10-26 22:32:52 -0700196
197 features->x_max = features->y_max =
198 get_unaligned_le16(&report[10]);
199
200 length = 11;
201 }
202 return length;
203}
204
Chris Bagwell428f8582011-10-26 22:26:59 -0700205/*
206 * Interface Descriptor of wacom devices can be incomplete and
207 * inconsistent so wacom_features table is used to store stylus
208 * device's packet lengths, various maximum values, and tablet
209 * resolution based on product ID's.
210 *
211 * For devices that contain 2 interfaces, wacom_features table is
212 * inaccurate for the touch interface. Since the Interface Descriptor
213 * for touch interfaces has pretty complete data, this function exists
214 * to query tablet for this missing information instead of hard coding in
215 * an additional table.
216 *
217 * A typical Interface Descriptor for a stylus will contain a
218 * boot mouse application collection that is not of interest and this
219 * function will ignore it.
220 *
221 * It also contains a digitizer application collection that also is not
222 * of interest since any information it contains would be duplicate
223 * of what is in wacom_features. Usually it defines a report of an array
224 * of bytes that could be used as max length of the stylus packet returned.
225 * If it happens to define a Digitizer-Stylus Physical Collection then
226 * the X and Y logical values contain valid data but it is ignored.
227 *
228 * A typical Interface Descriptor for a touch interface will contain a
229 * Digitizer-Finger Physical Collection which will define both logical
230 * X/Y maximum as well as the physical size of tablet. Since touch
231 * interfaces haven't supported pressure or distance, this is enough
232 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700233 *
234 * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
235 * Collection. Instead they define a Logical Collection with a single
236 * Logical Maximum for both X and Y.
Chris Bagwell428f8582011-10-26 22:26:59 -0700237 */
238static int wacom_parse_hid(struct usb_interface *intf,
239 struct hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800240 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500241{
242 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800243 char limit = 0;
244 /* result has to be defined as int for some devices */
245 int result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500246 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
247 unsigned char *report;
248
249 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
250 if (!report)
251 return -ENOMEM;
252
253 /* retrive report descriptors */
254 do {
255 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
256 USB_REQ_GET_DESCRIPTOR,
257 USB_RECIP_INTERFACE | USB_DIR_IN,
258 HID_DEVICET_REPORT << 8,
259 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
260 report,
261 hid_desc->wDescriptorLength,
262 5000); /* 5 secs */
Ping Chenga417ea42011-08-16 00:17:56 -0700263 } while (result < 0 && limit++ < WAC_MSG_RETRIES);
Ping Cheng545f4e92008-11-24 11:44:27 -0500264
Ping Cheng384318e2009-04-28 07:49:54 -0700265 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500266 if (result < 0)
267 goto out;
268
269 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
270
271 switch (report[i]) {
272 case HID_USAGE_PAGE:
273 switch (report[i + 1]) {
274 case HID_USAGE_PAGE_DIGITIZER:
275 usage = WCM_DIGITIZER;
276 i++;
277 break;
278
279 case HID_USAGE_PAGE_DESKTOP:
280 usage = WCM_DESKTOP;
281 i++;
282 break;
283 }
284 break;
285
286 case HID_USAGE:
287 switch (report[i + 1]) {
288 case HID_USAGE_X:
289 if (usage == WCM_DESKTOP) {
290 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800291 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800292 if (features->type == TABLETPC2FG) {
293 /* need to reset back */
294 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Chengec67bbe2009-12-15 00:35:24 -0800295 }
Ping Cheng4a880812010-09-05 12:25:40 -0700296 if (features->type == BAMBOO_PT) {
297 /* need to reset back */
298 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng4a880812010-09-05 12:25:40 -0700299 features->x_phy =
300 get_unaligned_le16(&report[i + 5]);
301 features->x_max =
302 get_unaligned_le16(&report[i + 8]);
303 i += 15;
304 } else {
305 features->x_max =
306 get_unaligned_le16(&report[i + 3]);
307 features->x_phy =
308 get_unaligned_le16(&report[i + 6]);
309 features->unit = report[i + 9];
310 features->unitExpo = report[i + 11];
311 i += 12;
312 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500313 } else if (pen) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800314 /* penabled only accepts exact bytes of data */
315 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800316 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800317 features->device_type = BTN_TOOL_PEN;
Ping Cheng545f4e92008-11-24 11:44:27 -0500318 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700319 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500320 i += 4;
321 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500322 }
323 break;
324
325 case HID_USAGE_Y:
Ping Chengec67bbe2009-12-15 00:35:24 -0800326 if (usage == WCM_DESKTOP) {
327 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800328 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800329 if (features->type == TABLETPC2FG) {
330 /* need to reset back */
331 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Chengec67bbe2009-12-15 00:35:24 -0800332 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700333 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800334 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700335 get_unaligned_le16(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800336 i += 7;
Ping Cheng4a880812010-09-05 12:25:40 -0700337 } else if (features->type == BAMBOO_PT) {
338 /* need to reset back */
339 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng4a880812010-09-05 12:25:40 -0700340 features->y_phy =
341 get_unaligned_le16(&report[i + 3]);
342 features->y_max =
343 get_unaligned_le16(&report[i + 6]);
344 i += 12;
Ping Chengec67bbe2009-12-15 00:35:24 -0800345 } else {
346 features->y_max =
347 features->x_max;
348 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700349 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800350 i += 4;
351 }
352 } else if (pen) {
353 /* penabled only accepts exact bytes of data */
354 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800355 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800356 features->device_type = BTN_TOOL_PEN;
357 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700358 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800359 i += 4;
360 }
361 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500362 break;
363
364 case HID_USAGE_FINGER:
365 finger = 1;
366 i++;
367 break;
368
Chris Bagwell428f8582011-10-26 22:26:59 -0700369 /*
370 * Requiring Stylus Usage will ignore boot mouse
371 * X/Y values and some cases of invalid Digitizer X/Y
372 * values commonly reported.
373 */
Ping Cheng545f4e92008-11-24 11:44:27 -0500374 case HID_USAGE_STYLUS:
375 pen = 1;
376 i++;
377 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500378 }
379 break;
380
Chris Bagwell41343612011-10-26 22:32:52 -0700381 case HID_COLLECTION_END:
Ping Chengec67bbe2009-12-15 00:35:24 -0800382 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500383 finger = usage = 0;
384 break;
Chris Bagwell41343612011-10-26 22:32:52 -0700385
386 case HID_COLLECTION:
387 i++;
388 switch (report[i]) {
389 case HID_COLLECTION_LOGICAL:
390 i += wacom_parse_logical_collection(&report[i],
391 features);
392 break;
393 }
394 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500395 }
396 }
397
Ping Cheng545f4e92008-11-24 11:44:27 -0500398 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700399 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500400 kfree(report);
401 return result;
402}
403
Ping Chengec67bbe2009-12-15 00:35:24 -0800404static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700405{
406 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800407 int limit = 0, report_id = 2;
408 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700409
Ping Cheng3b48c912011-08-16 00:17:57 -0700410 rep_data = kmalloc(4, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700411 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800412 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700413
Ping Cheng3b48c912011-08-16 00:17:57 -0700414 /* ask to report tablet data if it is MT Tablet PC or
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700415 * not a Tablet PC */
416 if (features->type == TABLETPC2FG) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800417 do {
418 rep_data[0] = 3;
419 rep_data[1] = 4;
Ping Cheng3b48c912011-08-16 00:17:57 -0700420 rep_data[2] = 0;
421 rep_data[3] = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800422 report_id = 3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700423 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
424 report_id, rep_data, 4, 1);
Ping Chengec67bbe2009-12-15 00:35:24 -0800425 if (error >= 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700426 error = wacom_get_report(intf,
427 WAC_HID_FEATURE_REPORT,
428 report_id, rep_data, 4, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700429 } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
Chris Bagwell6e8ec532011-10-26 22:24:22 -0700430 } else if (features->type != TABLETPC &&
Chris Bagwelld3825d52012-03-25 23:26:11 -0700431 features->type != WIRELESS &&
Chris Bagwell6e8ec532011-10-26 22:24:22 -0700432 features->device_type == BTN_TOOL_PEN) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800433 do {
434 rep_data[0] = 2;
435 rep_data[1] = 2;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700436 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
437 report_id, rep_data, 2, 1);
Ping Chengec67bbe2009-12-15 00:35:24 -0800438 if (error >= 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700439 error = wacom_get_report(intf,
440 WAC_HID_FEATURE_REPORT,
441 report_id, rep_data, 2, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700442 } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
Ping Chengec67bbe2009-12-15 00:35:24 -0800443 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700444
445 kfree(rep_data);
446
447 return error < 0 ? error : 0;
448}
449
Ping Chengec67bbe2009-12-15 00:35:24 -0800450static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
451 struct wacom_features *features)
452{
453 int error = 0;
454 struct usb_host_interface *interface = intf->cur_altsetting;
455 struct hid_descriptor *hid_desc;
456
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700457 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800458 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700459 features->x_fuzz = 4;
460 features->y_fuzz = 4;
461 features->pressure_fuzz = 0;
462 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800463
Chris Bagwelld3825d52012-03-25 23:26:11 -0700464 /*
465 * The wireless device HID is basic and layout conflicts with
466 * other tablets (monitor and touch interface can look like pen).
467 * Skip the query for this type and modify defaults based on
468 * interface number.
469 */
470 if (features->type == WIRELESS) {
471 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
472 features->device_type = 0;
473 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
474 features->device_type = BTN_TOOL_DOUBLETAP;
475 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
476 }
477 }
478
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700479 /* only Tablet PCs and Bamboo P&T need to retrieve the info */
Ping Cheng4a880812010-09-05 12:25:40 -0700480 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
481 (features->type != BAMBOO_PT))
Ping Chengec67bbe2009-12-15 00:35:24 -0800482 goto out;
483
484 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
485 if (usb_get_extra_descriptor(&interface->endpoint[0],
486 HID_DEVICET_REPORT, &hid_desc)) {
487 printk("wacom: can not retrieve extra class descriptor\n");
488 error = 1;
489 goto out;
490 }
491 }
492 error = wacom_parse_hid(intf, hid_desc, features);
493 if (error)
494 goto out;
495
Ping Chengec67bbe2009-12-15 00:35:24 -0800496 out:
497 return error;
498}
499
Ping Cheng4492eff2010-03-19 22:18:15 -0700500struct wacom_usbdev_data {
501 struct list_head list;
502 struct kref kref;
503 struct usb_device *dev;
504 struct wacom_shared shared;
505};
506
507static LIST_HEAD(wacom_udev_list);
508static DEFINE_MUTEX(wacom_udev_list_lock);
509
510static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
511{
512 struct wacom_usbdev_data *data;
513
514 list_for_each_entry(data, &wacom_udev_list, list) {
515 if (data->dev == dev) {
516 kref_get(&data->kref);
517 return data;
518 }
519 }
520
521 return NULL;
522}
523
524static int wacom_add_shared_data(struct wacom_wac *wacom,
525 struct usb_device *dev)
526{
527 struct wacom_usbdev_data *data;
528 int retval = 0;
529
530 mutex_lock(&wacom_udev_list_lock);
531
532 data = wacom_get_usbdev_data(dev);
533 if (!data) {
534 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
535 if (!data) {
536 retval = -ENOMEM;
537 goto out;
538 }
539
540 kref_init(&data->kref);
541 data->dev = dev;
542 list_add_tail(&data->list, &wacom_udev_list);
543 }
544
545 wacom->shared = &data->shared;
546
547out:
548 mutex_unlock(&wacom_udev_list_lock);
549 return retval;
550}
551
552static void wacom_release_shared_data(struct kref *kref)
553{
554 struct wacom_usbdev_data *data =
555 container_of(kref, struct wacom_usbdev_data, kref);
556
557 mutex_lock(&wacom_udev_list_lock);
558 list_del(&data->list);
559 mutex_unlock(&wacom_udev_list_lock);
560
561 kfree(data);
562}
563
564static void wacom_remove_shared_data(struct wacom_wac *wacom)
565{
566 struct wacom_usbdev_data *data;
567
568 if (wacom->shared) {
569 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
570 kref_put(&data->kref, wacom_release_shared_data);
571 wacom->shared = NULL;
572 }
573}
574
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700575static int wacom_led_control(struct wacom *wacom)
576{
577 unsigned char *buf;
Ping Cheng09e7d942011-10-04 23:51:14 -0700578 int retval, led = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700579
580 buf = kzalloc(9, GFP_KERNEL);
581 if (!buf)
582 return -ENOMEM;
583
Jason Gerecke246835f2011-12-12 00:12:04 -0800584 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
585 wacom->wacom_wac.features.type == WACOM_24HD)
Ping Cheng09e7d942011-10-04 23:51:14 -0700586 led = (wacom->led.select[1] << 4) | 0x40;
587
588 led |= wacom->led.select[0] | 0x4;
589
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700590 buf[0] = WAC_CMD_LED_CONTROL;
Ping Cheng09e7d942011-10-04 23:51:14 -0700591 buf[1] = led;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700592 buf[2] = wacom->led.llv;
593 buf[3] = wacom->led.hlv;
594 buf[4] = wacom->led.img_lum;
595
596 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
597 buf, 9, WAC_CMD_RETRIES);
598 kfree(buf);
599
600 return retval;
601}
602
603static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
604{
605 unsigned char *buf;
606 int i, retval;
607
608 buf = kzalloc(259, GFP_KERNEL);
609 if (!buf)
610 return -ENOMEM;
611
612 /* Send 'start' command */
613 buf[0] = WAC_CMD_ICON_START;
614 buf[1] = 1;
615 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
616 buf, 2, WAC_CMD_RETRIES);
617 if (retval < 0)
618 goto out;
619
620 buf[0] = WAC_CMD_ICON_XFER;
621 buf[1] = button_id & 0x07;
622 for (i = 0; i < 4; i++) {
623 buf[2] = i;
624 memcpy(buf + 3, img + i * 256, 256);
625
626 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
627 buf, 259, WAC_CMD_RETRIES);
628 if (retval < 0)
629 break;
630 }
631
632 /* Send 'stop' */
633 buf[0] = WAC_CMD_ICON_START;
634 buf[1] = 0;
635 wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
636 buf, 2, WAC_CMD_RETRIES);
637
638out:
639 kfree(buf);
640 return retval;
641}
642
Ping Cheng09e7d942011-10-04 23:51:14 -0700643static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700644 const char *buf, size_t count)
645{
646 struct wacom *wacom = dev_get_drvdata(dev);
647 unsigned int id;
648 int err;
649
650 err = kstrtouint(buf, 10, &id);
651 if (err)
652 return err;
653
654 mutex_lock(&wacom->lock);
655
Ping Cheng09e7d942011-10-04 23:51:14 -0700656 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700657 err = wacom_led_control(wacom);
658
659 mutex_unlock(&wacom->lock);
660
661 return err < 0 ? err : count;
662}
663
Ping Cheng09e7d942011-10-04 23:51:14 -0700664#define DEVICE_LED_SELECT_ATTR(SET_ID) \
665static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
666 struct device_attribute *attr, const char *buf, size_t count) \
667{ \
668 return wacom_led_select_store(dev, SET_ID, buf, count); \
669} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700670static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
671 struct device_attribute *attr, char *buf) \
672{ \
673 struct wacom *wacom = dev_get_drvdata(dev); \
674 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
675} \
676static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
677 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700678 wacom_led##SET_ID##_select_store)
679
680DEVICE_LED_SELECT_ATTR(0);
681DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700682
683static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
684 const char *buf, size_t count)
685{
686 unsigned int value;
687 int err;
688
689 err = kstrtouint(buf, 10, &value);
690 if (err)
691 return err;
692
693 mutex_lock(&wacom->lock);
694
695 *dest = value & 0x7f;
696 err = wacom_led_control(wacom);
697
698 mutex_unlock(&wacom->lock);
699
700 return err < 0 ? err : count;
701}
702
703#define DEVICE_LUMINANCE_ATTR(name, field) \
704static ssize_t wacom_##name##_luminance_store(struct device *dev, \
705 struct device_attribute *attr, const char *buf, size_t count) \
706{ \
707 struct wacom *wacom = dev_get_drvdata(dev); \
708 \
709 return wacom_luminance_store(wacom, &wacom->led.field, \
710 buf, count); \
711} \
712static DEVICE_ATTR(name##_luminance, S_IWUSR, \
713 NULL, wacom_##name##_luminance_store)
714
715DEVICE_LUMINANCE_ATTR(status0, llv);
716DEVICE_LUMINANCE_ATTR(status1, hlv);
717DEVICE_LUMINANCE_ATTR(buttons, img_lum);
718
719static ssize_t wacom_button_image_store(struct device *dev, int button_id,
720 const char *buf, size_t count)
721{
722 struct wacom *wacom = dev_get_drvdata(dev);
723 int err;
724
725 if (count != 1024)
726 return -EINVAL;
727
728 mutex_lock(&wacom->lock);
729
730 err = wacom_led_putimage(wacom, button_id, buf);
731
732 mutex_unlock(&wacom->lock);
733
734 return err < 0 ? err : count;
735}
736
737#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
738static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
739 struct device_attribute *attr, const char *buf, size_t count) \
740{ \
741 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
742} \
743static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
744 NULL, wacom_btnimg##BUTTON_ID##_store)
745
746DEVICE_BTNIMG_ATTR(0);
747DEVICE_BTNIMG_ATTR(1);
748DEVICE_BTNIMG_ATTR(2);
749DEVICE_BTNIMG_ATTR(3);
750DEVICE_BTNIMG_ATTR(4);
751DEVICE_BTNIMG_ATTR(5);
752DEVICE_BTNIMG_ATTR(6);
753DEVICE_BTNIMG_ATTR(7);
754
Ping Cheng09e7d942011-10-04 23:51:14 -0700755static struct attribute *cintiq_led_attrs[] = {
756 &dev_attr_status_led0_select.attr,
757 &dev_attr_status_led1_select.attr,
758 NULL
759};
760
761static struct attribute_group cintiq_led_attr_group = {
762 .name = "wacom_led",
763 .attrs = cintiq_led_attrs,
764};
765
766static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700767 &dev_attr_status0_luminance.attr,
768 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700769 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700770 &dev_attr_buttons_luminance.attr,
771 &dev_attr_button0_rawimg.attr,
772 &dev_attr_button1_rawimg.attr,
773 &dev_attr_button2_rawimg.attr,
774 &dev_attr_button3_rawimg.attr,
775 &dev_attr_button4_rawimg.attr,
776 &dev_attr_button5_rawimg.attr,
777 &dev_attr_button6_rawimg.attr,
778 &dev_attr_button7_rawimg.attr,
779 NULL
780};
781
Ping Cheng09e7d942011-10-04 23:51:14 -0700782static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700783 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700784 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700785};
786
787static int wacom_initialize_leds(struct wacom *wacom)
788{
789 int error;
790
Ping Cheng09e7d942011-10-04 23:51:14 -0700791 /* Initialize default values */
792 switch (wacom->wacom_wac.features.type) {
793 case INTUOS4:
794 case INTUOS4L:
795 wacom->led.select[0] = 0;
796 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700797 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700798 wacom->led.hlv = 20;
799 wacom->led.img_lum = 10;
Ping Cheng09e7d942011-10-04 23:51:14 -0700800 error = sysfs_create_group(&wacom->intf->dev.kobj,
801 &intuos4_led_attr_group);
802 break;
803
Jason Gerecke246835f2011-12-12 00:12:04 -0800804 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700805 case WACOM_21UX2:
806 wacom->led.select[0] = 0;
807 wacom->led.select[1] = 0;
808 wacom->led.llv = 0;
809 wacom->led.hlv = 0;
810 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700811
812 error = sysfs_create_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700813 &cintiq_led_attr_group);
814 break;
815
816 default:
817 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700818 }
819
Ping Cheng09e7d942011-10-04 23:51:14 -0700820 if (error) {
821 dev_err(&wacom->intf->dev,
822 "cannot create sysfs group err: %d\n", error);
823 return error;
824 }
825 wacom_led_control(wacom);
826
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700827 return 0;
828}
829
830static void wacom_destroy_leds(struct wacom *wacom)
831{
Ping Cheng09e7d942011-10-04 23:51:14 -0700832 switch (wacom->wacom_wac.features.type) {
833 case INTUOS4:
834 case INTUOS4L:
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700835 sysfs_remove_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700836 &intuos4_led_attr_group);
837 break;
838
Jason Gerecke246835f2011-12-12 00:12:04 -0800839 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700840 case WACOM_21UX2:
841 sysfs_remove_group(&wacom->intf->dev.kobj,
842 &cintiq_led_attr_group);
843 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700844 }
845}
846
Chris Bagwella1d552c2012-03-25 23:26:30 -0700847static enum power_supply_property wacom_battery_props[] = {
848 POWER_SUPPLY_PROP_CAPACITY
849};
850
851static int wacom_battery_get_property(struct power_supply *psy,
852 enum power_supply_property psp,
853 union power_supply_propval *val)
854{
855 struct wacom *wacom = container_of(psy, struct wacom, battery);
856 int ret = 0;
857
858 switch (psp) {
859 case POWER_SUPPLY_PROP_CAPACITY:
860 val->intval =
861 wacom->wacom_wac.battery_capacity * 100 / 31;
862 break;
863 default:
864 ret = -EINVAL;
865 break;
866 }
867
868 return ret;
869}
870
871static int wacom_initialize_battery(struct wacom *wacom)
872{
873 int error = 0;
874
875 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) {
876 wacom->battery.properties = wacom_battery_props;
877 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
878 wacom->battery.get_property = wacom_battery_get_property;
879 wacom->battery.name = "wacom_battery";
880 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
881 wacom->battery.use_for_apm = 0;
882
883 error = power_supply_register(&wacom->usbdev->dev,
884 &wacom->battery);
885 }
886
887 return error;
888}
889
890static void wacom_destroy_battery(struct wacom *wacom)
891{
892 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR)
893 power_supply_unregister(&wacom->battery);
894}
895
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700896static int wacom_register_input(struct wacom *wacom)
897{
898 struct input_dev *input_dev;
899 struct usb_interface *intf = wacom->intf;
900 struct usb_device *dev = interface_to_usbdev(intf);
901 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
902 int error;
903
904 input_dev = input_allocate_device();
905 if (!input_dev)
906 return -ENOMEM;
907
908 input_dev->name = wacom_wac->name;
909 input_dev->dev.parent = &intf->dev;
910 input_dev->open = wacom_open;
911 input_dev->close = wacom_close;
912 usb_to_input_id(dev, &input_dev->id);
913 input_set_drvdata(input_dev, wacom);
914
915 wacom_wac->input = input_dev;
916 wacom_setup_input_capabilities(input_dev, wacom_wac);
917
918 error = input_register_device(input_dev);
919 if (error) {
920 input_free_device(input_dev);
921 wacom_wac->input = NULL;
922 }
923
924 return error;
925}
926
Chris Bagwell16bf2882012-03-25 23:26:20 -0700927static void wacom_wireless_work(struct work_struct *work)
928{
929 struct wacom *wacom = container_of(work, struct wacom, work);
930 struct usb_device *usbdev = wacom->usbdev;
931 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
932
933 /*
934 * Regardless if this is a disconnect or a new tablet,
935 * remove any existing input devices.
936 */
937
938 /* Stylus interface */
939 wacom = usb_get_intfdata(usbdev->config->interface[1]);
940 if (wacom->wacom_wac.input)
941 input_unregister_device(wacom->wacom_wac.input);
942 wacom->wacom_wac.input = 0;
943
944 /* Touch interface */
945 wacom = usb_get_intfdata(usbdev->config->interface[2]);
946 if (wacom->wacom_wac.input)
947 input_unregister_device(wacom->wacom_wac.input);
948 wacom->wacom_wac.input = 0;
949
950 if (wacom_wac->pid == 0) {
951 printk(KERN_INFO "wacom: wireless tablet disconnected\n");
952 } else {
953 const struct usb_device_id *id = wacom_ids;
954
955 printk(KERN_INFO
956 "wacom: wireless tablet connected with PID %x\n",
957 wacom_wac->pid);
958
959 while (id->match_flags) {
960 if (id->idVendor == USB_VENDOR_ID_WACOM &&
961 id->idProduct == wacom_wac->pid)
962 break;
963 id++;
964 }
965
966 if (!id->match_flags) {
967 printk(KERN_INFO
968 "wacom: ignorning unknown PID.\n");
969 return;
970 }
971
972 /* Stylus interface */
973 wacom = usb_get_intfdata(usbdev->config->interface[1]);
974 wacom_wac = &wacom->wacom_wac;
975 wacom_wac->features =
976 *((struct wacom_features *)id->driver_info);
977 wacom_wac->features.device_type = BTN_TOOL_PEN;
978 wacom_register_input(wacom);
979
980 /* Touch interface */
981 wacom = usb_get_intfdata(usbdev->config->interface[2]);
982 wacom_wac = &wacom->wacom_wac;
983 wacom_wac->features =
984 *((struct wacom_features *)id->driver_info);
985 wacom_wac->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
986 wacom_wac->features.device_type = BTN_TOOL_FINGER;
987 wacom_set_phy_from_res(&wacom_wac->features);
988 wacom_wac->features.x_max = wacom_wac->features.y_max = 4096;
989 wacom_register_input(wacom);
990 }
991}
992
Ping Cheng3bea7332006-07-13 18:01:36 -0700993static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
994{
995 struct usb_device *dev = interface_to_usbdev(intf);
996 struct usb_endpoint_descriptor *endpoint;
997 struct wacom *wacom;
998 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800999 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001000 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001001
Jason Childse33da8a2010-02-17 22:38:31 -08001002 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001003 return -EINVAL;
1004
Ping Cheng3bea7332006-07-13 18:01:36 -07001005 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001006 if (!wacom)
1007 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001008
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001009 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001010 wacom_wac->features = *((struct wacom_features *)id->driver_info);
1011 features = &wacom_wac->features;
1012 if (features->pktlen > WACOM_PKGLEN_MAX) {
1013 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001014 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -08001015 }
1016
Daniel Mack997ea582010-04-12 13:17:25 +02001017 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
1018 GFP_KERNEL, &wacom->data_dma);
Jason Childse33da8a2010-02-17 22:38:31 -08001019 if (!wacom_wac->data) {
1020 error = -ENOMEM;
1021 goto fail1;
1022 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001023
1024 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -08001025 if (!wacom->irq) {
1026 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001027 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -08001028 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001029
1030 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001031 wacom->intf = intf;
1032 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001033 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001034 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
1035 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
1036
Ping Cheng545f4e92008-11-24 11:44:27 -05001037 endpoint = &intf->cur_altsetting->endpoint[0].desc;
1038
Ping Chengec67bbe2009-12-15 00:35:24 -08001039 /* Retrieve the physical and logical size for OEM devices */
1040 error = wacom_retrieve_hid_descriptor(intf, features);
1041 if (error)
Alexander Strakh4b6d4432011-02-11 00:44:41 -08001042 goto fail3;
Ping Cheng545f4e92008-11-24 11:44:27 -05001043
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001044 wacom_setup_device_quirks(features);
1045
Ping Cheng49b764a2010-02-20 00:53:49 -08001046 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1047
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001048 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -08001049 /* Append the device type to the name */
1050 strlcat(wacom_wac->name,
1051 features->device_type == BTN_TOOL_PEN ?
1052 " Pen" : " Finger",
1053 sizeof(wacom_wac->name));
Ping Cheng4492eff2010-03-19 22:18:15 -07001054
1055 error = wacom_add_shared_data(wacom_wac, dev);
1056 if (error)
1057 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -08001058 }
1059
Ping Cheng3bea7332006-07-13 18:01:36 -07001060 usb_fill_int_urb(wacom->irq, dev,
1061 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -08001062 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -07001063 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -07001064 wacom->irq->transfer_dma = wacom->data_dma;
1065 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1066
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001067 error = wacom_initialize_leds(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001068 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -07001069 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -07001070
Chris Bagwella1d552c2012-03-25 23:26:30 -07001071 error = wacom_initialize_battery(wacom);
1072 if (error)
1073 goto fail5;
1074
Chris Bagwelld3825d52012-03-25 23:26:11 -07001075 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1076 error = wacom_register_input(wacom);
1077 if (error)
Chris Bagwella1d552c2012-03-25 23:26:30 -07001078 goto fail6;
Chris Bagwelld3825d52012-03-25 23:26:11 -07001079 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001080
Ping Chengec67bbe2009-12-15 00:35:24 -08001081 /* Note that if query fails it is not a hard failure */
1082 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -07001083
1084 usb_set_intfdata(intf, wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001085
1086 if (features->quirks & WACOM_QUIRK_MONITOR) {
1087 if (usb_submit_urb(wacom->irq, GFP_KERNEL))
1088 goto fail5;
1089 }
1090
Ping Cheng3bea7332006-07-13 18:01:36 -07001091 return 0;
1092
Chris Bagwella1d552c2012-03-25 23:26:30 -07001093 fail6: wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001094 fail5: wacom_destroy_leds(wacom);
Ping Cheng4492eff2010-03-19 22:18:15 -07001095 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001096 fail3: usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +02001097 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001098 fail1: kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001099 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001100}
1101
1102static void wacom_disconnect(struct usb_interface *intf)
1103{
Oliver Neukume7224092008-04-15 01:31:57 -04001104 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001105
1106 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001107
1108 usb_kill_urb(wacom->irq);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001109 cancel_work_sync(&wacom->work);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001110 if (wacom->wacom_wac.input)
1111 input_unregister_device(wacom->wacom_wac.input);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001112 wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001113 wacom_destroy_leds(wacom);
Oliver Neukume7224092008-04-15 01:31:57 -04001114 usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +02001115 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001116 wacom->wacom_wac.data, wacom->data_dma);
1117 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -04001118 kfree(wacom);
1119}
1120
1121static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
1122{
1123 struct wacom *wacom = usb_get_intfdata(intf);
1124
1125 mutex_lock(&wacom->lock);
1126 usb_kill_urb(wacom->irq);
1127 mutex_unlock(&wacom->lock);
1128
1129 return 0;
1130}
1131
1132static int wacom_resume(struct usb_interface *intf)
1133{
1134 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001135 struct wacom_features *features = &wacom->wacom_wac.features;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001136 int rv = 0;
Oliver Neukume7224092008-04-15 01:31:57 -04001137
1138 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -07001139
1140 /* switch to wacom mode first */
1141 wacom_query_tablet_data(intf, features);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001142 wacom_led_control(wacom);
Ping Cheng38101472010-04-13 23:07:52 -07001143
Chris Bagwelld3825d52012-03-25 23:26:11 -07001144 if ((wacom->open || features->quirks & WACOM_QUIRK_MONITOR)
1145 && usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001146 rv = -EIO;
Ping Cheng38101472010-04-13 23:07:52 -07001147
Oliver Neukume7224092008-04-15 01:31:57 -04001148 mutex_unlock(&wacom->lock);
1149
1150 return rv;
1151}
1152
1153static int wacom_reset_resume(struct usb_interface *intf)
1154{
1155 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001156}
1157
1158static struct usb_driver wacom_driver = {
1159 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001160 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001161 .probe = wacom_probe,
1162 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -04001163 .suspend = wacom_suspend,
1164 .resume = wacom_resume,
1165 .reset_resume = wacom_reset_resume,
1166 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -07001167};
1168
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -08001169module_usb_driver(wacom_driver);