blob: dd0dbc250c94e63be65a3ebe12f6ba50c8bd3094 [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
Jason Gerecke5866d9e2014-04-19 13:46:40 -070025#define HID_USAGE_X ((HID_USAGE_PAGE_DESKTOP << 16) | 0x30)
26#define HID_USAGE_Y ((HID_USAGE_PAGE_DESKTOP << 16) | 0x31)
Jason Gereckee9fc4132014-04-19 13:49:37 -070027#define HID_USAGE_PRESSURE ((HID_USAGE_PAGE_DIGITIZER << 16) | 0x30)
Jason Gerecke5866d9e2014-04-19 13:46:40 -070028#define HID_USAGE_X_TILT ((HID_USAGE_PAGE_DIGITIZER << 16) | 0x3d)
29#define HID_USAGE_Y_TILT ((HID_USAGE_PAGE_DIGITIZER << 16) | 0x3e)
30#define HID_USAGE_FINGER ((HID_USAGE_PAGE_DIGITIZER << 16) | 0x22)
31#define HID_USAGE_STYLUS ((HID_USAGE_PAGE_DIGITIZER << 16) | 0x20)
32#define HID_USAGE_CONTACTMAX ((HID_USAGE_PAGE_DIGITIZER << 16) | 0x55)
Chris Bagwell41343612011-10-26 22:32:52 -070033#define HID_COLLECTION 0xa1
34#define HID_COLLECTION_LOGICAL 0x02
35#define HID_COLLECTION_END 0xc0
Ping Cheng545f4e92008-11-24 11:44:27 -050036
Ping Cheng545f4e92008-11-24 11:44:27 -050037struct hid_descriptor {
38 struct usb_descriptor_header header;
39 __le16 bcdHID;
40 u8 bCountryCode;
41 u8 bNumDescriptors;
42 u8 bDescriptorType;
43 __le16 wDescriptorLength;
44} __attribute__ ((packed));
45
46/* defines to get/set USB message */
Ping Cheng3bea7332006-07-13 18:01:36 -070047#define USB_REQ_GET_REPORT 0x01
48#define USB_REQ_SET_REPORT 0x09
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070049
Ping Cheng545f4e92008-11-24 11:44:27 -050050#define WAC_HID_FEATURE_REPORT 0x03
Ping Chenga417ea42011-08-16 00:17:56 -070051#define WAC_MSG_RETRIES 5
Ping Cheng3bea7332006-07-13 18:01:36 -070052
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070053#define WAC_CMD_LED_CONTROL 0x20
54#define WAC_CMD_ICON_START 0x21
55#define WAC_CMD_ICON_XFER 0x23
56#define WAC_CMD_RETRIES 10
57
58static int wacom_get_report(struct usb_interface *intf, u8 type, u8 id,
59 void *buf, size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070060{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070061 struct usb_device *dev = interface_to_usbdev(intf);
62 int retval;
63
64 do {
65 retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
66 USB_REQ_GET_REPORT,
Chris Bagwell6e8ec532011-10-26 22:24:22 -070067 USB_DIR_IN | USB_TYPE_CLASS |
68 USB_RECIP_INTERFACE,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070069 (type << 8) + id,
70 intf->altsetting[0].desc.bInterfaceNumber,
71 buf, size, 100);
72 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
73
74 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070075}
76
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070077static int wacom_set_report(struct usb_interface *intf, u8 type, u8 id,
78 void *buf, size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070079{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070080 struct usb_device *dev = interface_to_usbdev(intf);
81 int retval;
82
83 do {
84 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
85 USB_REQ_SET_REPORT,
86 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
87 (type << 8) + id,
88 intf->altsetting[0].desc.bInterfaceNumber,
89 buf, size, 1000);
90 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
91
92 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070093}
94
Adrian Bunk54c9b2262006-11-20 03:23:58 +010095static void wacom_sys_irq(struct urb *urb)
Ping Cheng3bea7332006-07-13 18:01:36 -070096{
97 struct wacom *wacom = urb->context;
Greg Kroah-Hartman65e78a202012-05-04 15:33:13 -070098 struct device *dev = &wacom->intf->dev;
Ping Cheng3bea7332006-07-13 18:01:36 -070099 int retval;
100
101 switch (urb->status) {
102 case 0:
103 /* success */
104 break;
105 case -ECONNRESET:
106 case -ENOENT:
107 case -ESHUTDOWN:
108 /* this urb is terminated, clean up */
Greg Kroah-Hartman3b6aee22012-05-01 21:24:28 -0700109 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
110 __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -0700111 return;
112 default:
Greg Kroah-Hartman3b6aee22012-05-01 21:24:28 -0700113 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
114 __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -0700115 goto exit;
116 }
117
Dmitry Torokhov95dd3b32010-03-19 22:18:15 -0700118 wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
Ping Cheng3bea7332006-07-13 18:01:36 -0700119
120 exit:
Oliver Neukume7224092008-04-15 01:31:57 -0400121 usb_mark_last_busy(wacom->usbdev);
Dmitry Torokhov73a97f42010-03-19 22:18:15 -0700122 retval = usb_submit_urb(urb, GFP_ATOMIC);
Ping Cheng3bea7332006-07-13 18:01:36 -0700123 if (retval)
Greg Kroah-Hartman3b6aee22012-05-01 21:24:28 -0700124 dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
Greg Kroah-Hartmanb3169fe2012-04-25 14:48:44 -0700125 __func__, retval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700126}
127
Ping Cheng3bea7332006-07-13 18:01:36 -0700128static int wacom_open(struct input_dev *dev)
129{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400130 struct wacom *wacom = input_get_drvdata(dev);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700131 int retval = 0;
132
133 if (usb_autopm_get_interface(wacom->intf) < 0)
134 return -EIO;
Ping Cheng3bea7332006-07-13 18:01:36 -0700135
Oliver Neukume7224092008-04-15 01:31:57 -0400136 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700137
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -0700138 if (wacom->open)
139 goto out;
140
Oliver Neukume7224092008-04-15 01:31:57 -0400141 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700142 retval = -EIO;
143 goto out;
Oliver Neukume7224092008-04-15 01:31:57 -0400144 }
145
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700146 wacom->open = true;
Oliver Neukume7224092008-04-15 01:31:57 -0400147 wacom->intf->needs_remote_wakeup = 1;
148
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700149out:
Oliver Neukume7224092008-04-15 01:31:57 -0400150 mutex_unlock(&wacom->lock);
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700151 usb_autopm_put_interface(wacom->intf);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700152 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -0700153}
154
155static void wacom_close(struct input_dev *dev)
156{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400157 struct wacom *wacom = input_get_drvdata(dev);
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700158 int autopm_error;
159
160 autopm_error = usb_autopm_get_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700161
Oliver Neukume7224092008-04-15 01:31:57 -0400162 mutex_lock(&wacom->lock);
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -0700163 if (!wacom->open)
164 goto out;
165
Ping Cheng3bea7332006-07-13 18:01:36 -0700166 usb_kill_urb(wacom->irq);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700167 wacom->open = false;
Oliver Neukume7224092008-04-15 01:31:57 -0400168 wacom->intf->needs_remote_wakeup = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -0700169
170out:
Oliver Neukume7224092008-04-15 01:31:57 -0400171 mutex_unlock(&wacom->lock);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700172
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700173 if (!autopm_error)
174 usb_autopm_put_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700175}
176
Chris Bagwell16bf2882012-03-25 23:26:20 -0700177/*
Jason Gerecke115d5e12012-10-03 17:24:32 -0700178 * Calculate the resolution of the X or Y axis, given appropriate HID data.
179 * This function is little more than hidinput_calc_abs_res stripped down.
180 */
181static int wacom_calc_hid_res(int logical_extents, int physical_extents,
182 unsigned char unit, unsigned char exponent)
183{
184 int prev, unit_exponent;
185
186 /* Check if the extents are sane */
187 if (logical_extents <= 0 || physical_extents <= 0)
188 return 0;
189
190 /* Get signed value of nybble-sized twos-compliment exponent */
191 unit_exponent = exponent;
192 if (unit_exponent > 7)
193 unit_exponent -= 16;
194
195 /* Convert physical_extents to millimeters */
196 if (unit == 0x11) { /* If centimeters */
197 unit_exponent += 1;
198 } else if (unit == 0x13) { /* If inches */
199 prev = physical_extents;
200 physical_extents *= 254;
201 if (physical_extents < prev)
202 return 0;
203 unit_exponent -= 1;
204 } else {
205 return 0;
206 }
207
208 /* Apply negative unit exponent */
209 for (; unit_exponent < 0; unit_exponent++) {
210 prev = logical_extents;
211 logical_extents *= 10;
212 if (logical_extents < prev)
213 return 0;
214 }
215 /* Apply positive unit exponent */
216 for (; unit_exponent > 0; unit_exponent--) {
217 prev = physical_extents;
218 physical_extents *= 10;
219 if (physical_extents < prev)
220 return 0;
221 }
222
223 /* Calculate resolution */
224 return logical_extents / physical_extents;
225}
226
Chris Bagwell41343612011-10-26 22:32:52 -0700227static int wacom_parse_logical_collection(unsigned char *report,
228 struct wacom_features *features)
229{
230 int length = 0;
231
232 if (features->type == BAMBOO_PT) {
233
234 /* Logical collection is only used by 3rd gen Bamboo Touch */
235 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
Ping Cheng8b4a0c12012-01-31 00:07:33 -0800236 features->device_type = BTN_TOOL_FINGER;
Chris Bagwell41343612011-10-26 22:32:52 -0700237
Chris Bagwell41343612011-10-26 22:32:52 -0700238 features->x_max = features->y_max =
239 get_unaligned_le16(&report[10]);
240
241 length = 11;
242 }
243 return length;
244}
245
Ping Chengf393ee22012-04-29 21:09:17 -0700246static void wacom_retrieve_report_data(struct usb_interface *intf,
247 struct wacom_features *features)
248{
249 int result = 0;
250 unsigned char *rep_data;
251
252 rep_data = kmalloc(2, GFP_KERNEL);
253 if (rep_data) {
254
255 rep_data[0] = 12;
256 result = wacom_get_report(intf, WAC_HID_FEATURE_REPORT,
Ping Cheng61c91dd2012-06-28 16:46:27 -0700257 rep_data[0], rep_data, 2,
Ping Chengf393ee22012-04-29 21:09:17 -0700258 WAC_MSG_RETRIES);
259
260 if (result >= 0 && rep_data[1] > 2)
261 features->touch_max = rep_data[1];
262
263 kfree(rep_data);
264 }
265}
266
Chris Bagwell428f8582011-10-26 22:26:59 -0700267/*
268 * Interface Descriptor of wacom devices can be incomplete and
269 * inconsistent so wacom_features table is used to store stylus
270 * device's packet lengths, various maximum values, and tablet
271 * resolution based on product ID's.
272 *
273 * For devices that contain 2 interfaces, wacom_features table is
274 * inaccurate for the touch interface. Since the Interface Descriptor
275 * for touch interfaces has pretty complete data, this function exists
276 * to query tablet for this missing information instead of hard coding in
277 * an additional table.
278 *
279 * A typical Interface Descriptor for a stylus will contain a
280 * boot mouse application collection that is not of interest and this
281 * function will ignore it.
282 *
283 * It also contains a digitizer application collection that also is not
284 * of interest since any information it contains would be duplicate
285 * of what is in wacom_features. Usually it defines a report of an array
286 * of bytes that could be used as max length of the stylus packet returned.
287 * If it happens to define a Digitizer-Stylus Physical Collection then
288 * the X and Y logical values contain valid data but it is ignored.
289 *
290 * A typical Interface Descriptor for a touch interface will contain a
291 * Digitizer-Finger Physical Collection which will define both logical
292 * X/Y maximum as well as the physical size of tablet. Since touch
293 * interfaces haven't supported pressure or distance, this is enough
294 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700295 *
296 * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
297 * Collection. Instead they define a Logical Collection with a single
298 * Logical Maximum for both X and Y.
Jason Gereckeae584ca2012-04-03 15:50:40 -0700299 *
300 * Intuos5 touch interface does not contain useful data. We deal with
301 * this after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700302 */
303static int wacom_parse_hid(struct usb_interface *intf,
304 struct hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800305 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500306{
307 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800308 char limit = 0;
309 /* result has to be defined as int for some devices */
Ping Cheng1d0d6df2013-11-25 18:43:45 -0800310 int result = 0, touch_max = 0;
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700311 int i = 0, page = 0, finger = 0, pen = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500312 unsigned char *report;
313
314 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
315 if (!report)
316 return -ENOMEM;
317
318 /* retrive report descriptors */
319 do {
320 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
321 USB_REQ_GET_DESCRIPTOR,
322 USB_RECIP_INTERFACE | USB_DIR_IN,
323 HID_DEVICET_REPORT << 8,
324 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
325 report,
326 hid_desc->wDescriptorLength,
327 5000); /* 5 secs */
Ping Chenga417ea42011-08-16 00:17:56 -0700328 } while (result < 0 && limit++ < WAC_MSG_RETRIES);
Ping Cheng545f4e92008-11-24 11:44:27 -0500329
Ping Cheng384318e2009-04-28 07:49:54 -0700330 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500331 if (result < 0)
332 goto out;
333
334 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
335
336 switch (report[i]) {
337 case HID_USAGE_PAGE:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700338 page = report[i + 1];
339 i++;
Ping Cheng545f4e92008-11-24 11:44:27 -0500340 break;
341
342 case HID_USAGE:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700343 switch (page << 16 | report[i + 1]) {
Ping Cheng545f4e92008-11-24 11:44:27 -0500344 case HID_USAGE_X:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700345 if (finger) {
346 features->device_type = BTN_TOOL_FINGER;
347 /* touch device at least supports one touch point */
348 touch_max = 1;
349 switch (features->type) {
350 case TABLETPC2FG:
351 features->pktlen = WACOM_PKGLEN_TPC2FG;
352 break;
Ping Cheng3699dd72012-11-03 12:16:13 -0700353
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700354 case MTSCREEN:
355 case WACOM_24HDT:
356 features->pktlen = WACOM_PKGLEN_MTOUCH;
357 break;
Ping Cheng3699dd72012-11-03 12:16:13 -0700358
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700359 case MTTPC:
Jason Gerecked51ddb22014-05-14 17:14:29 -0700360 case MTTPC_B:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700361 features->pktlen = WACOM_PKGLEN_MTTPC;
362 break;
Ping Cheng6afdc282012-11-03 12:16:15 -0700363
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700364 case BAMBOO_PT:
365 features->pktlen = WACOM_PKGLEN_BBTOUCH;
366 break;
Ping Cheng3699dd72012-11-03 12:16:13 -0700367
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700368 default:
369 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
370 break;
371 }
Ping Cheng19635182012-04-29 21:09:18 -0700372
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700373 switch (features->type) {
374 case BAMBOO_PT:
375 features->x_phy =
376 get_unaligned_le16(&report[i + 5]);
377 features->x_max =
378 get_unaligned_le16(&report[i + 8]);
379 i += 15;
380 break;
Ping Cheng3699dd72012-11-03 12:16:13 -0700381
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700382 case WACOM_24HDT:
Ping Cheng545f4e92008-11-24 11:44:27 -0500383 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700384 get_unaligned_le16(&report[i + 3]);
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700385 features->x_phy =
386 get_unaligned_le16(&report[i + 8]);
387 features->unit = report[i - 1];
388 features->unitExpo = report[i - 3];
389 i += 12;
390 break;
391
Jason Gerecked51ddb22014-05-14 17:14:29 -0700392 case MTTPC_B:
393 features->x_max =
394 get_unaligned_le16(&report[i + 3]);
395 features->x_phy =
396 get_unaligned_le16(&report[i + 6]);
397 features->unit = report[i - 5];
398 features->unitExpo = report[i - 3];
399 i += 9;
400 break;
401
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700402 default:
403 features->x_max =
404 get_unaligned_le16(&report[i + 3]);
405 features->x_phy =
406 get_unaligned_le16(&report[i + 6]);
407 features->unit = report[i + 9];
408 features->unitExpo = report[i + 11];
409 i += 12;
410 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500411 }
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700412 } else if (pen) {
413 /* penabled only accepts exact bytes of data */
414 if (features->type >= TABLETPC)
415 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
416 features->device_type = BTN_TOOL_PEN;
417 features->x_max =
418 get_unaligned_le16(&report[i + 3]);
419 i += 4;
Ping Cheng545f4e92008-11-24 11:44:27 -0500420 }
421 break;
422
423 case HID_USAGE_Y:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700424 if (finger) {
425 switch (features->type) {
426 case TABLETPC2FG:
427 case MTSCREEN:
428 case MTTPC:
Ping Chengec67bbe2009-12-15 00:35:24 -0800429 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700430 get_unaligned_le16(&report[i + 3]);
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700431 features->y_phy =
432 get_unaligned_le16(&report[i + 6]);
433 i += 7;
434 break;
435
436 case WACOM_24HDT:
437 features->y_max =
438 get_unaligned_le16(&report[i + 3]);
439 features->y_phy =
440 get_unaligned_le16(&report[i - 2]);
441 i += 7;
442 break;
443
444 case BAMBOO_PT:
445 features->y_phy =
446 get_unaligned_le16(&report[i + 3]);
447 features->y_max =
448 get_unaligned_le16(&report[i + 6]);
449 i += 12;
450 break;
451
Jason Gerecked51ddb22014-05-14 17:14:29 -0700452 case MTTPC_B:
453 features->y_max =
454 get_unaligned_le16(&report[i + 3]);
455 features->y_phy =
456 get_unaligned_le16(&report[i + 6]);
457 i += 9;
458 break;
459
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700460 default:
461 features->y_max =
462 features->x_max;
463 features->y_phy =
464 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800465 i += 4;
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700466 break;
Ping Chengec67bbe2009-12-15 00:35:24 -0800467 }
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700468 } else if (pen) {
469 features->y_max =
470 get_unaligned_le16(&report[i + 3]);
471 i += 4;
Ping Chengec67bbe2009-12-15 00:35:24 -0800472 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500473 break;
474
475 case HID_USAGE_FINGER:
476 finger = 1;
477 i++;
478 break;
479
Chris Bagwell428f8582011-10-26 22:26:59 -0700480 /*
481 * Requiring Stylus Usage will ignore boot mouse
482 * X/Y values and some cases of invalid Digitizer X/Y
483 * values commonly reported.
484 */
Ping Cheng545f4e92008-11-24 11:44:27 -0500485 case HID_USAGE_STYLUS:
486 pen = 1;
487 i++;
488 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700489
490 case HID_USAGE_CONTACTMAX:
Ping Cheng1cecc5c2012-06-28 16:47:30 -0700491 /* leave touch_max as is if predefined */
492 if (!features->touch_max)
493 wacom_retrieve_report_data(intf, features);
Ping Chengf393ee22012-04-29 21:09:17 -0700494 i++;
495 break;
Jason Gereckee9fc4132014-04-19 13:49:37 -0700496
497 case HID_USAGE_PRESSURE:
498 if (pen) {
499 features->pressure_max =
500 get_unaligned_le16(&report[i + 3]);
501 i += 4;
502 }
503 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500504 }
505 break;
506
Chris Bagwell41343612011-10-26 22:32:52 -0700507 case HID_COLLECTION_END:
Ping Chengec67bbe2009-12-15 00:35:24 -0800508 /* reset UsagePage and Finger */
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700509 finger = page = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500510 break;
Chris Bagwell41343612011-10-26 22:32:52 -0700511
512 case HID_COLLECTION:
513 i++;
514 switch (report[i]) {
515 case HID_COLLECTION_LOGICAL:
516 i += wacom_parse_logical_collection(&report[i],
517 features);
518 break;
519 }
520 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500521 }
522 }
523
Ping Cheng545f4e92008-11-24 11:44:27 -0500524 out:
Ping Cheng1d0d6df2013-11-25 18:43:45 -0800525 if (!features->touch_max && touch_max)
526 features->touch_max = touch_max;
Ping Cheng384318e2009-04-28 07:49:54 -0700527 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500528 kfree(report);
529 return result;
530}
531
Jason Gereckefe494bc2012-10-03 17:25:35 -0700532static int wacom_set_device_mode(struct usb_interface *intf, int report_id, int length, int mode)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700533{
534 unsigned char *rep_data;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700535 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700536
Jason Gereckefe494bc2012-10-03 17:25:35 -0700537 rep_data = kzalloc(length, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700538 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800539 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700540
Jason Gereckefe494bc2012-10-03 17:25:35 -0700541 do {
Chris Bagwell9937c022013-01-23 19:37:34 -0800542 rep_data[0] = report_id;
543 rep_data[1] = mode;
544
Jason Gereckefe494bc2012-10-03 17:25:35 -0700545 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
546 report_id, rep_data, length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700547 if (error >= 0)
548 error = wacom_get_report(intf, WAC_HID_FEATURE_REPORT,
549 report_id, rep_data, length, 1);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700550 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700551
552 kfree(rep_data);
553
554 return error < 0 ? error : 0;
555}
556
Jason Gereckefe494bc2012-10-03 17:25:35 -0700557/*
558 * Switch the tablet into its most-capable mode. Wacom tablets are
559 * typically configured to power-up in a mode which sends mouse-like
560 * reports to the OS. To get absolute position, pressure data, etc.
561 * from the tablet, it is necessary to switch the tablet out of this
562 * mode and into one which sends the full range of tablet data.
563 */
564static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
565{
566 if (features->device_type == BTN_TOOL_FINGER) {
567 if (features->type > TABLETPC) {
568 /* MT Tablet PC touch */
569 return wacom_set_device_mode(intf, 3, 4, 4);
570 }
Jason Gerecke36d3c512013-09-20 09:47:35 -0700571 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
Jason Gereckeb1e42792012-10-21 00:38:04 -0700572 return wacom_set_device_mode(intf, 18, 3, 2);
573 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700574 } else if (features->device_type == BTN_TOOL_PEN) {
575 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
576 return wacom_set_device_mode(intf, 2, 2, 2);
577 }
578 }
579
580 return 0;
581}
582
Ping Chengec67bbe2009-12-15 00:35:24 -0800583static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
Ping Cheng19635182012-04-29 21:09:18 -0700584 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800585{
586 int error = 0;
587 struct usb_host_interface *interface = intf->cur_altsetting;
588 struct hid_descriptor *hid_desc;
589
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700590 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800591 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700592 features->x_fuzz = 4;
593 features->y_fuzz = 4;
594 features->pressure_fuzz = 0;
595 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800596
Chris Bagwelld3825d52012-03-25 23:26:11 -0700597 /*
598 * The wireless device HID is basic and layout conflicts with
599 * other tablets (monitor and touch interface can look like pen).
600 * Skip the query for this type and modify defaults based on
601 * interface number.
602 */
603 if (features->type == WIRELESS) {
604 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
605 features->device_type = 0;
606 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
Ping Chengadad0042012-06-28 16:48:17 -0700607 features->device_type = BTN_TOOL_FINGER;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700608 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
609 }
610 }
611
Ping Cheng19635182012-04-29 21:09:18 -0700612 /* only devices that support touch need to retrieve the info */
Ping Chengea2e6022012-06-12 00:14:12 -0700613 if (features->type < BAMBOO_PT) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800614 goto out;
Ping Cheng19635182012-04-29 21:09:18 -0700615 }
Ping Chengec67bbe2009-12-15 00:35:24 -0800616
Dmitry Torokhova882c932012-05-02 00:13:38 -0700617 error = usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc);
618 if (error) {
619 error = usb_get_extra_descriptor(&interface->endpoint[0],
620 HID_DEVICET_REPORT, &hid_desc);
621 if (error) {
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -0700622 dev_err(&intf->dev,
623 "can not retrieve extra class descriptor\n");
Ping Chengec67bbe2009-12-15 00:35:24 -0800624 goto out;
625 }
626 }
627 error = wacom_parse_hid(intf, hid_desc, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800628
Ping Chengec67bbe2009-12-15 00:35:24 -0800629 out:
630 return error;
631}
632
Ping Cheng4492eff2010-03-19 22:18:15 -0700633struct wacom_usbdev_data {
634 struct list_head list;
635 struct kref kref;
636 struct usb_device *dev;
637 struct wacom_shared shared;
638};
639
640static LIST_HEAD(wacom_udev_list);
641static DEFINE_MUTEX(wacom_udev_list_lock);
642
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700643static struct usb_device *wacom_get_sibling(struct usb_device *dev, int vendor, int product)
644{
645 int port1;
646 struct usb_device *sibling;
647
648 if (vendor == 0 && product == 0)
649 return dev;
650
651 if (dev->parent == NULL)
652 return NULL;
653
654 usb_hub_for_each_child(dev->parent, port1, sibling) {
655 struct usb_device_descriptor *d;
656 if (sibling == NULL)
657 continue;
658
659 d = &sibling->descriptor;
660 if (d->idVendor == vendor && d->idProduct == product)
661 return sibling;
662 }
663
664 return NULL;
665}
666
Ping Cheng4492eff2010-03-19 22:18:15 -0700667static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
668{
669 struct wacom_usbdev_data *data;
670
671 list_for_each_entry(data, &wacom_udev_list, list) {
672 if (data->dev == dev) {
673 kref_get(&data->kref);
674 return data;
675 }
676 }
677
678 return NULL;
679}
680
681static int wacom_add_shared_data(struct wacom_wac *wacom,
682 struct usb_device *dev)
683{
684 struct wacom_usbdev_data *data;
685 int retval = 0;
686
687 mutex_lock(&wacom_udev_list_lock);
688
689 data = wacom_get_usbdev_data(dev);
690 if (!data) {
691 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
692 if (!data) {
693 retval = -ENOMEM;
694 goto out;
695 }
696
697 kref_init(&data->kref);
698 data->dev = dev;
699 list_add_tail(&data->list, &wacom_udev_list);
700 }
701
702 wacom->shared = &data->shared;
703
704out:
705 mutex_unlock(&wacom_udev_list_lock);
706 return retval;
707}
708
709static void wacom_release_shared_data(struct kref *kref)
710{
711 struct wacom_usbdev_data *data =
712 container_of(kref, struct wacom_usbdev_data, kref);
713
714 mutex_lock(&wacom_udev_list_lock);
715 list_del(&data->list);
716 mutex_unlock(&wacom_udev_list_lock);
717
718 kfree(data);
719}
720
721static void wacom_remove_shared_data(struct wacom_wac *wacom)
722{
723 struct wacom_usbdev_data *data;
724
725 if (wacom->shared) {
726 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
727 kref_put(&data->kref, wacom_release_shared_data);
728 wacom->shared = NULL;
729 }
730}
731
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700732static int wacom_led_control(struct wacom *wacom)
733{
734 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700735 int retval;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700736
737 buf = kzalloc(9, GFP_KERNEL);
738 if (!buf)
739 return -ENOMEM;
740
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700741 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700742 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700743 /*
744 * Touch Ring and crop mark LED luminance may take on
745 * one of four values:
746 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
747 */
748 int ring_led = wacom->led.select[0] & 0x03;
749 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
750 int crop_lum = 0;
Ping Cheng09e7d942011-10-04 23:51:14 -0700751
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700752 buf[0] = WAC_CMD_LED_CONTROL;
753 buf[1] = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
754 }
755 else {
756 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700757
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700758 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
759 wacom->wacom_wac.features.type == WACOM_24HD)
760 led |= (wacom->led.select[1] << 4) | 0x40;
761
762 buf[0] = WAC_CMD_LED_CONTROL;
763 buf[1] = led;
764 buf[2] = wacom->led.llv;
765 buf[3] = wacom->led.hlv;
766 buf[4] = wacom->led.img_lum;
767 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700768
769 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
770 buf, 9, WAC_CMD_RETRIES);
771 kfree(buf);
772
773 return retval;
774}
775
776static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
777{
778 unsigned char *buf;
779 int i, retval;
780
781 buf = kzalloc(259, GFP_KERNEL);
782 if (!buf)
783 return -ENOMEM;
784
785 /* Send 'start' command */
786 buf[0] = WAC_CMD_ICON_START;
787 buf[1] = 1;
788 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
789 buf, 2, WAC_CMD_RETRIES);
790 if (retval < 0)
791 goto out;
792
793 buf[0] = WAC_CMD_ICON_XFER;
794 buf[1] = button_id & 0x07;
795 for (i = 0; i < 4; i++) {
796 buf[2] = i;
797 memcpy(buf + 3, img + i * 256, 256);
798
799 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
800 buf, 259, WAC_CMD_RETRIES);
801 if (retval < 0)
802 break;
803 }
804
805 /* Send 'stop' */
806 buf[0] = WAC_CMD_ICON_START;
807 buf[1] = 0;
808 wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
809 buf, 2, WAC_CMD_RETRIES);
810
811out:
812 kfree(buf);
813 return retval;
814}
815
Ping Cheng09e7d942011-10-04 23:51:14 -0700816static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700817 const char *buf, size_t count)
818{
819 struct wacom *wacom = dev_get_drvdata(dev);
820 unsigned int id;
821 int err;
822
823 err = kstrtouint(buf, 10, &id);
824 if (err)
825 return err;
826
827 mutex_lock(&wacom->lock);
828
Ping Cheng09e7d942011-10-04 23:51:14 -0700829 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700830 err = wacom_led_control(wacom);
831
832 mutex_unlock(&wacom->lock);
833
834 return err < 0 ? err : count;
835}
836
Ping Cheng09e7d942011-10-04 23:51:14 -0700837#define DEVICE_LED_SELECT_ATTR(SET_ID) \
838static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
839 struct device_attribute *attr, const char *buf, size_t count) \
840{ \
841 return wacom_led_select_store(dev, SET_ID, buf, count); \
842} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700843static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
844 struct device_attribute *attr, char *buf) \
845{ \
846 struct wacom *wacom = dev_get_drvdata(dev); \
847 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
848} \
849static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
850 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700851 wacom_led##SET_ID##_select_store)
852
853DEVICE_LED_SELECT_ATTR(0);
854DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700855
856static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
857 const char *buf, size_t count)
858{
859 unsigned int value;
860 int err;
861
862 err = kstrtouint(buf, 10, &value);
863 if (err)
864 return err;
865
866 mutex_lock(&wacom->lock);
867
868 *dest = value & 0x7f;
869 err = wacom_led_control(wacom);
870
871 mutex_unlock(&wacom->lock);
872
873 return err < 0 ? err : count;
874}
875
876#define DEVICE_LUMINANCE_ATTR(name, field) \
877static ssize_t wacom_##name##_luminance_store(struct device *dev, \
878 struct device_attribute *attr, const char *buf, size_t count) \
879{ \
880 struct wacom *wacom = dev_get_drvdata(dev); \
881 \
882 return wacom_luminance_store(wacom, &wacom->led.field, \
883 buf, count); \
884} \
885static DEVICE_ATTR(name##_luminance, S_IWUSR, \
886 NULL, wacom_##name##_luminance_store)
887
888DEVICE_LUMINANCE_ATTR(status0, llv);
889DEVICE_LUMINANCE_ATTR(status1, hlv);
890DEVICE_LUMINANCE_ATTR(buttons, img_lum);
891
892static ssize_t wacom_button_image_store(struct device *dev, int button_id,
893 const char *buf, size_t count)
894{
895 struct wacom *wacom = dev_get_drvdata(dev);
896 int err;
897
898 if (count != 1024)
899 return -EINVAL;
900
901 mutex_lock(&wacom->lock);
902
903 err = wacom_led_putimage(wacom, button_id, buf);
904
905 mutex_unlock(&wacom->lock);
906
907 return err < 0 ? err : count;
908}
909
910#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
911static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
912 struct device_attribute *attr, const char *buf, size_t count) \
913{ \
914 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
915} \
916static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
917 NULL, wacom_btnimg##BUTTON_ID##_store)
918
919DEVICE_BTNIMG_ATTR(0);
920DEVICE_BTNIMG_ATTR(1);
921DEVICE_BTNIMG_ATTR(2);
922DEVICE_BTNIMG_ATTR(3);
923DEVICE_BTNIMG_ATTR(4);
924DEVICE_BTNIMG_ATTR(5);
925DEVICE_BTNIMG_ATTR(6);
926DEVICE_BTNIMG_ATTR(7);
927
Ping Cheng09e7d942011-10-04 23:51:14 -0700928static struct attribute *cintiq_led_attrs[] = {
929 &dev_attr_status_led0_select.attr,
930 &dev_attr_status_led1_select.attr,
931 NULL
932};
933
934static struct attribute_group cintiq_led_attr_group = {
935 .name = "wacom_led",
936 .attrs = cintiq_led_attrs,
937};
938
939static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700940 &dev_attr_status0_luminance.attr,
941 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700942 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700943 &dev_attr_buttons_luminance.attr,
944 &dev_attr_button0_rawimg.attr,
945 &dev_attr_button1_rawimg.attr,
946 &dev_attr_button2_rawimg.attr,
947 &dev_attr_button3_rawimg.attr,
948 &dev_attr_button4_rawimg.attr,
949 &dev_attr_button5_rawimg.attr,
950 &dev_attr_button6_rawimg.attr,
951 &dev_attr_button7_rawimg.attr,
952 NULL
953};
954
Ping Cheng09e7d942011-10-04 23:51:14 -0700955static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700956 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700957 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700958};
959
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700960static struct attribute *intuos5_led_attrs[] = {
961 &dev_attr_status0_luminance.attr,
962 &dev_attr_status_led0_select.attr,
963 NULL
964};
965
966static struct attribute_group intuos5_led_attr_group = {
967 .name = "wacom_led",
968 .attrs = intuos5_led_attrs,
969};
970
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700971static int wacom_initialize_leds(struct wacom *wacom)
972{
973 int error;
974
Ping Cheng09e7d942011-10-04 23:51:14 -0700975 /* Initialize default values */
976 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700977 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700978 case INTUOS4:
979 case INTUOS4L:
980 wacom->led.select[0] = 0;
981 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700982 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700983 wacom->led.hlv = 20;
984 wacom->led.img_lum = 10;
Ping Cheng09e7d942011-10-04 23:51:14 -0700985 error = sysfs_create_group(&wacom->intf->dev.kobj,
986 &intuos4_led_attr_group);
987 break;
988
Jason Gerecke246835f2011-12-12 00:12:04 -0800989 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700990 case WACOM_21UX2:
991 wacom->led.select[0] = 0;
992 wacom->led.select[1] = 0;
993 wacom->led.llv = 0;
994 wacom->led.hlv = 0;
995 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700996
997 error = sysfs_create_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700998 &cintiq_led_attr_group);
999 break;
1000
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001001 case INTUOS5S:
1002 case INTUOS5:
1003 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001004 case INTUOSPS:
1005 case INTUOSPM:
1006 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -07001007 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
1008 wacom->led.select[0] = 0;
1009 wacom->led.select[1] = 0;
1010 wacom->led.llv = 32;
1011 wacom->led.hlv = 0;
1012 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001013
Ping Chengc2b0c272013-09-20 09:50:14 -07001014 error = sysfs_create_group(&wacom->intf->dev.kobj,
1015 &intuos5_led_attr_group);
1016 } else
1017 return 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001018 break;
1019
Ping Cheng09e7d942011-10-04 23:51:14 -07001020 default:
1021 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001022 }
1023
Ping Cheng09e7d942011-10-04 23:51:14 -07001024 if (error) {
1025 dev_err(&wacom->intf->dev,
1026 "cannot create sysfs group err: %d\n", error);
1027 return error;
1028 }
1029 wacom_led_control(wacom);
1030
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001031 return 0;
1032}
1033
1034static void wacom_destroy_leds(struct wacom *wacom)
1035{
Ping Cheng09e7d942011-10-04 23:51:14 -07001036 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -07001037 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -07001038 case INTUOS4:
1039 case INTUOS4L:
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001040 sysfs_remove_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -07001041 &intuos4_led_attr_group);
1042 break;
1043
Jason Gerecke246835f2011-12-12 00:12:04 -08001044 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -07001045 case WACOM_21UX2:
1046 sysfs_remove_group(&wacom->intf->dev.kobj,
1047 &cintiq_led_attr_group);
1048 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001049
1050 case INTUOS5S:
1051 case INTUOS5:
1052 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001053 case INTUOSPS:
1054 case INTUOSPM:
1055 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -07001056 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
1057 sysfs_remove_group(&wacom->intf->dev.kobj,
1058 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001059 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001060 }
1061}
1062
Chris Bagwella1d552c2012-03-25 23:26:30 -07001063static enum power_supply_property wacom_battery_props[] = {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001064 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001065 POWER_SUPPLY_PROP_CAPACITY
1066};
1067
1068static int wacom_battery_get_property(struct power_supply *psy,
1069 enum power_supply_property psp,
1070 union power_supply_propval *val)
1071{
1072 struct wacom *wacom = container_of(psy, struct wacom, battery);
1073 int ret = 0;
1074
1075 switch (psp) {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001076 case POWER_SUPPLY_PROP_SCOPE:
1077 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1078 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001079 case POWER_SUPPLY_PROP_CAPACITY:
1080 val->intval =
1081 wacom->wacom_wac.battery_capacity * 100 / 31;
1082 break;
1083 default:
1084 ret = -EINVAL;
1085 break;
1086 }
1087
1088 return ret;
1089}
1090
1091static int wacom_initialize_battery(struct wacom *wacom)
1092{
1093 int error = 0;
1094
1095 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) {
1096 wacom->battery.properties = wacom_battery_props;
1097 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
1098 wacom->battery.get_property = wacom_battery_get_property;
1099 wacom->battery.name = "wacom_battery";
1100 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1101 wacom->battery.use_for_apm = 0;
1102
1103 error = power_supply_register(&wacom->usbdev->dev,
1104 &wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001105
1106 if (!error)
1107 power_supply_powers(&wacom->battery,
1108 &wacom->usbdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001109 }
1110
1111 return error;
1112}
1113
1114static void wacom_destroy_battery(struct wacom *wacom)
1115{
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001116 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR &&
1117 wacom->battery.dev) {
Chris Bagwella1d552c2012-03-25 23:26:30 -07001118 power_supply_unregister(&wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001119 wacom->battery.dev = NULL;
1120 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001121}
1122
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001123static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001124{
1125 struct input_dev *input_dev;
1126 struct usb_interface *intf = wacom->intf;
1127 struct usb_device *dev = interface_to_usbdev(intf);
1128 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001129
1130 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001131 if (!input_dev)
1132 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001133
1134 input_dev->name = wacom_wac->name;
Benjamin Tissoires7097d4c2014-07-24 12:48:06 -07001135 input_dev->phys = wacom->phys;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001136 input_dev->dev.parent = &intf->dev;
1137 input_dev->open = wacom_open;
1138 input_dev->close = wacom_close;
1139 usb_to_input_id(dev, &input_dev->id);
1140 input_set_drvdata(input_dev, wacom);
1141
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001142 return input_dev;
1143}
1144
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001145static void wacom_unregister_inputs(struct wacom *wacom)
1146{
1147 if (wacom->wacom_wac.input)
1148 input_unregister_device(wacom->wacom_wac.input);
1149 if (wacom->wacom_wac.pad_input)
1150 input_unregister_device(wacom->wacom_wac.pad_input);
1151 wacom->wacom_wac.input = NULL;
1152 wacom->wacom_wac.pad_input = NULL;
1153}
1154
1155static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001156{
1157 struct input_dev *input_dev, *pad_input_dev;
1158 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1159 int error;
1160
1161 input_dev = wacom_allocate_input(wacom);
1162 pad_input_dev = wacom_allocate_input(wacom);
1163 if (!input_dev || !pad_input_dev) {
1164 error = -ENOMEM;
1165 goto fail1;
1166 }
1167
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001168 wacom_wac->input = input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001169 wacom_wac->pad_input = pad_input_dev;
1170 wacom_wac->pad_input->name = wacom_wac->pad_name;
1171
Ping Cheng19635182012-04-29 21:09:18 -07001172 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1173 if (error)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001174 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001175
1176 error = input_register_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001177 if (error)
1178 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001179
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001180 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1181 if (error) {
1182 /* no pad in use on this interface */
1183 input_free_device(pad_input_dev);
1184 wacom_wac->pad_input = NULL;
1185 pad_input_dev = NULL;
1186 } else {
1187 error = input_register_device(pad_input_dev);
1188 if (error)
1189 goto fail3;
1190 }
1191
Ping Cheng19635182012-04-29 21:09:18 -07001192 return 0;
1193
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001194fail3:
1195 input_unregister_device(input_dev);
1196 input_dev = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001197fail2:
Ping Cheng19635182012-04-29 21:09:18 -07001198 wacom_wac->input = NULL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001199 wacom_wac->pad_input = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001200fail1:
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001201 if (input_dev)
1202 input_free_device(input_dev);
1203 if (pad_input_dev)
1204 input_free_device(pad_input_dev);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001205 return error;
1206}
1207
Chris Bagwell16bf2882012-03-25 23:26:20 -07001208static void wacom_wireless_work(struct work_struct *work)
1209{
1210 struct wacom *wacom = container_of(work, struct wacom, work);
1211 struct usb_device *usbdev = wacom->usbdev;
1212 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001213 struct wacom *wacom1, *wacom2;
1214 struct wacom_wac *wacom_wac1, *wacom_wac2;
1215 int error;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001216
1217 /*
1218 * Regardless if this is a disconnect or a new tablet,
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001219 * remove any existing input and battery devices.
Chris Bagwell16bf2882012-03-25 23:26:20 -07001220 */
1221
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001222 wacom_destroy_battery(wacom);
1223
Chris Bagwell16bf2882012-03-25 23:26:20 -07001224 /* Stylus interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001225 wacom1 = usb_get_intfdata(usbdev->config->interface[1]);
1226 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001227 wacom_unregister_inputs(wacom1);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001228
1229 /* Touch interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001230 wacom2 = usb_get_intfdata(usbdev->config->interface[2]);
1231 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001232 wacom_unregister_inputs(wacom2);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001233
1234 if (wacom_wac->pid == 0) {
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001235 dev_info(&wacom->intf->dev, "wireless tablet disconnected\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001236 } else {
1237 const struct usb_device_id *id = wacom_ids;
1238
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001239 dev_info(&wacom->intf->dev,
1240 "wireless tablet connected with PID %x\n",
1241 wacom_wac->pid);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001242
1243 while (id->match_flags) {
1244 if (id->idVendor == USB_VENDOR_ID_WACOM &&
1245 id->idProduct == wacom_wac->pid)
1246 break;
1247 id++;
1248 }
1249
1250 if (!id->match_flags) {
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001251 dev_info(&wacom->intf->dev,
1252 "ignoring unknown PID.\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001253 return;
1254 }
1255
1256 /* Stylus interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001257 wacom_wac1->features =
Chris Bagwell16bf2882012-03-25 23:26:20 -07001258 *((struct wacom_features *)id->driver_info);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001259 wacom_wac1->features.device_type = BTN_TOOL_PEN;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001260 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1261 wacom_wac1->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001262 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1263 wacom_wac1->features.name);
Ping Cheng961794a2013-12-05 12:54:53 -08001264 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1265 wacom_wac1->shared->type = wacom_wac1->features.type;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001266 error = wacom_register_inputs(wacom1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001267 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001268 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001269
1270 /* Touch interface */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001271 if (wacom_wac1->features.touch_max ||
1272 wacom_wac1->features.type == INTUOSHT) {
Ping Cheng57bcfce2013-10-15 23:44:00 -07001273 wacom_wac2->features =
1274 *((struct wacom_features *)id->driver_info);
1275 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1276 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1277 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1278 if (wacom_wac2->features.touch_max)
1279 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1280 "%s (WL) Finger",wacom_wac2->features.name);
1281 else
1282 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1283 "%s (WL) Pad",wacom_wac2->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001284 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1285 "%s (WL) Pad", wacom_wac2->features.name);
1286 error = wacom_register_inputs(wacom2);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001287 if (error)
1288 goto fail;
Ping Cheng961794a2013-12-05 12:54:53 -08001289
1290 if (wacom_wac1->features.type == INTUOSHT &&
1291 wacom_wac1->features.touch_max)
1292 wacom_wac->shared->touch_input = wacom_wac2->input;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001293 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001294
1295 error = wacom_initialize_battery(wacom);
1296 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001297 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001298 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001299
1300 return;
1301
Ping Cheng57bcfce2013-10-15 23:44:00 -07001302fail:
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001303 wacom_unregister_inputs(wacom1);
1304 wacom_unregister_inputs(wacom2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001305 return;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001306}
1307
Ping Cheng401d7d12013-07-28 00:38:54 -07001308/*
1309 * Not all devices report physical dimensions from HID.
1310 * Compute the default from hardcoded logical dimension
1311 * and resolution before driver overwrites them.
1312 */
1313static void wacom_set_default_phy(struct wacom_features *features)
1314{
1315 if (features->x_resolution) {
1316 features->x_phy = (features->x_max * 100) /
1317 features->x_resolution;
1318 features->y_phy = (features->y_max * 100) /
1319 features->y_resolution;
1320 }
1321}
1322
1323static void wacom_calculate_res(struct wacom_features *features)
1324{
1325 features->x_resolution = wacom_calc_hid_res(features->x_max,
1326 features->x_phy,
1327 features->unit,
1328 features->unitExpo);
1329 features->y_resolution = wacom_calc_hid_res(features->y_max,
1330 features->y_phy,
1331 features->unit,
1332 features->unitExpo);
1333}
1334
Ping Cheng3bea7332006-07-13 18:01:36 -07001335static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
1336{
1337 struct usb_device *dev = interface_to_usbdev(intf);
1338 struct usb_endpoint_descriptor *endpoint;
1339 struct wacom *wacom;
1340 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001341 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001342 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001343
Jason Childse33da8a2010-02-17 22:38:31 -08001344 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001345 return -EINVAL;
1346
Ping Cheng3bea7332006-07-13 18:01:36 -07001347 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001348 if (!wacom)
1349 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001350
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001351 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001352 wacom_wac->features = *((struct wacom_features *)id->driver_info);
1353 features = &wacom_wac->features;
1354 if (features->pktlen > WACOM_PKGLEN_MAX) {
1355 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001356 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -08001357 }
1358
Daniel Mack997ea582010-04-12 13:17:25 +02001359 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
1360 GFP_KERNEL, &wacom->data_dma);
Jason Childse33da8a2010-02-17 22:38:31 -08001361 if (!wacom_wac->data) {
1362 error = -ENOMEM;
1363 goto fail1;
1364 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001365
1366 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -08001367 if (!wacom->irq) {
1368 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001369 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -08001370 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001371
1372 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001373 wacom->intf = intf;
1374 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001375 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001376 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
1377 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
1378
Ping Cheng545f4e92008-11-24 11:44:27 -05001379 endpoint = &intf->cur_altsetting->endpoint[0].desc;
1380
Ping Cheng401d7d12013-07-28 00:38:54 -07001381 /* set the default size in case we do not get them from hid */
1382 wacom_set_default_phy(features);
1383
Ping Chengf393ee22012-04-29 21:09:17 -07001384 /* Retrieve the physical and logical size for touch devices */
Ping Chengec67bbe2009-12-15 00:35:24 -08001385 error = wacom_retrieve_hid_descriptor(intf, features);
1386 if (error)
Alexander Strakh4b6d4432011-02-11 00:44:41 -08001387 goto fail3;
Ping Cheng545f4e92008-11-24 11:44:27 -05001388
Jason Gereckeae584ca2012-04-03 15:50:40 -07001389 /*
1390 * Intuos5 has no useful data about its touch interface in its
1391 * HID descriptor. If this is the touch interface (wMaxPacketSize
1392 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1393 */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001394 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
Jason Gereckeae584ca2012-04-03 15:50:40 -07001395 if (endpoint->wMaxPacketSize == WACOM_PKGLEN_BBTOUCH3) {
1396 features->device_type = BTN_TOOL_FINGER;
1397 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
1398
Jason Gereckeae584ca2012-04-03 15:50:40 -07001399 features->x_max = 4096;
1400 features->y_max = 4096;
1401 } else {
1402 features->device_type = BTN_TOOL_PEN;
1403 }
1404 }
1405
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001406 wacom_setup_device_quirks(features);
1407
Ping Cheng401d7d12013-07-28 00:38:54 -07001408 /* set unit to "100th of a mm" for devices not reported by HID */
1409 if (!features->unit) {
1410 features->unit = 0x11;
1411 features->unitExpo = 16 - 3;
1412 }
1413 wacom_calculate_res(features);
1414
Ping Cheng49b764a2010-02-20 00:53:49 -08001415 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001416 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1417 "%s Pad", features->name);
Ping Cheng49b764a2010-02-20 00:53:49 -08001418
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001419 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Jason Gereckeaea2bf62012-10-21 00:38:03 -07001420 struct usb_device *other_dev;
1421
Ping Cheng49b764a2010-02-20 00:53:49 -08001422 /* Append the device type to the name */
Ping Cheng57bcfce2013-10-15 23:44:00 -07001423 if (features->device_type != BTN_TOOL_FINGER)
1424 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1425 else if (features->touch_max)
1426 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1427 else
1428 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
Ping Cheng4492eff2010-03-19 22:18:15 -07001429
Jason Gereckeaea2bf62012-10-21 00:38:03 -07001430 other_dev = wacom_get_sibling(dev, features->oVid, features->oPid);
1431 if (other_dev == NULL || wacom_get_usbdev_data(other_dev) == NULL)
1432 other_dev = dev;
1433 error = wacom_add_shared_data(wacom_wac, other_dev);
Ping Cheng4492eff2010-03-19 22:18:15 -07001434 if (error)
1435 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -08001436 }
1437
Ping Cheng3bea7332006-07-13 18:01:36 -07001438 usb_fill_int_urb(wacom->irq, dev,
1439 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -08001440 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -07001441 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -07001442 wacom->irq->transfer_dma = wacom->data_dma;
1443 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1444
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001445 error = wacom_initialize_leds(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001446 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -07001447 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -07001448
Chris Bagwelld3825d52012-03-25 23:26:11 -07001449 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001450 error = wacom_register_inputs(wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001451 if (error)
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001452 goto fail5;
Chris Bagwelld3825d52012-03-25 23:26:11 -07001453 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001454
Ping Chengec67bbe2009-12-15 00:35:24 -08001455 /* Note that if query fails it is not a hard failure */
1456 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -07001457
1458 usb_set_intfdata(intf, wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001459
1460 if (features->quirks & WACOM_QUIRK_MONITOR) {
Wei Yongjund4879c92013-08-24 16:41:56 -07001461 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
1462 error = -EIO;
Chris Bagwelld3825d52012-03-25 23:26:11 -07001463 goto fail5;
Wei Yongjund4879c92013-08-24 16:41:56 -07001464 }
Chris Bagwelld3825d52012-03-25 23:26:11 -07001465 }
Ping Cheng961794a2013-12-05 12:54:53 -08001466
1467 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1468 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1469 wacom_wac->shared->touch_input = wacom_wac->input;
1470 }
1471
Ping Cheng3bea7332006-07-13 18:01:36 -07001472 return 0;
1473
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001474 fail5: wacom_destroy_leds(wacom);
Ping Cheng4492eff2010-03-19 22:18:15 -07001475 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001476 fail3: usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +02001477 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001478 fail1: kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001479 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001480}
1481
1482static void wacom_disconnect(struct usb_interface *intf)
1483{
Oliver Neukume7224092008-04-15 01:31:57 -04001484 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001485
1486 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001487
1488 usb_kill_urb(wacom->irq);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001489 cancel_work_sync(&wacom->work);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001490 wacom_unregister_inputs(wacom);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001491 wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001492 wacom_destroy_leds(wacom);
Oliver Neukume7224092008-04-15 01:31:57 -04001493 usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +02001494 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001495 wacom->wacom_wac.data, wacom->data_dma);
1496 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -04001497 kfree(wacom);
1498}
1499
1500static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
1501{
1502 struct wacom *wacom = usb_get_intfdata(intf);
1503
1504 mutex_lock(&wacom->lock);
1505 usb_kill_urb(wacom->irq);
1506 mutex_unlock(&wacom->lock);
1507
1508 return 0;
1509}
1510
1511static int wacom_resume(struct usb_interface *intf)
1512{
1513 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001514 struct wacom_features *features = &wacom->wacom_wac.features;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001515 int rv = 0;
Oliver Neukume7224092008-04-15 01:31:57 -04001516
1517 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -07001518
1519 /* switch to wacom mode first */
1520 wacom_query_tablet_data(intf, features);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001521 wacom_led_control(wacom);
Ping Cheng38101472010-04-13 23:07:52 -07001522
Wei Yongjund4879c92013-08-24 16:41:56 -07001523 if ((wacom->open || (features->quirks & WACOM_QUIRK_MONITOR)) &&
1524 usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001525 rv = -EIO;
Ping Cheng38101472010-04-13 23:07:52 -07001526
Oliver Neukume7224092008-04-15 01:31:57 -04001527 mutex_unlock(&wacom->lock);
1528
1529 return rv;
1530}
1531
1532static int wacom_reset_resume(struct usb_interface *intf)
1533{
1534 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001535}
1536
1537static struct usb_driver wacom_driver = {
1538 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001539 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001540 .probe = wacom_probe,
1541 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -04001542 .suspend = wacom_suspend,
1543 .resume = wacom_resume,
1544 .reset_resume = wacom_reset_resume,
1545 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -07001546};
1547
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -08001548module_usb_driver(wacom_driver);