blob: 5ceeab6e95f16701b7aeca7213faedfd847e1eb2 [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"
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -070016#include <linux/hid.h>
Ping Cheng3bea7332006-07-13 18:01:36 -070017
Ping Cheng545f4e92008-11-24 11:44:27 -050018/* defines to get HID report descriptor */
19#define HID_DEVICET_HID (USB_TYPE_CLASS | 0x01)
20#define HID_DEVICET_REPORT (USB_TYPE_CLASS | 0x02)
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -070021#define HID_HDESC_USAGE_UNDEFINED 0x00
22#define HID_HDESC_USAGE_PAGE 0x05
23#define HID_HDESC_USAGE 0x09
24#define HID_HDESC_COLLECTION 0xa1
25#define HID_HDESC_COLLECTION_LOGICAL 0x02
26#define HID_HDESC_COLLECTION_END 0xc0
Ping Cheng545f4e92008-11-24 11:44:27 -050027
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -070028struct wac_hid_descriptor {
Ping Cheng545f4e92008-11-24 11:44:27 -050029 struct usb_descriptor_header header;
30 __le16 bcdHID;
31 u8 bCountryCode;
32 u8 bNumDescriptors;
33 u8 bDescriptorType;
34 __le16 wDescriptorLength;
35} __attribute__ ((packed));
36
37/* defines to get/set USB message */
Ping Cheng3bea7332006-07-13 18:01:36 -070038#define USB_REQ_GET_REPORT 0x01
39#define USB_REQ_SET_REPORT 0x09
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070040
Ping Cheng545f4e92008-11-24 11:44:27 -050041#define WAC_HID_FEATURE_REPORT 0x03
Ping Chenga417ea42011-08-16 00:17:56 -070042#define WAC_MSG_RETRIES 5
Ping Cheng3bea7332006-07-13 18:01:36 -070043
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070044#define WAC_CMD_LED_CONTROL 0x20
45#define WAC_CMD_ICON_START 0x21
46#define WAC_CMD_ICON_XFER 0x23
47#define WAC_CMD_RETRIES 10
48
49static int wacom_get_report(struct usb_interface *intf, u8 type, u8 id,
50 void *buf, size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070051{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070052 struct usb_device *dev = interface_to_usbdev(intf);
53 int retval;
54
55 do {
56 retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
57 USB_REQ_GET_REPORT,
Chris Bagwell6e8ec532011-10-26 22:24:22 -070058 USB_DIR_IN | USB_TYPE_CLASS |
59 USB_RECIP_INTERFACE,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070060 (type << 8) + id,
61 intf->altsetting[0].desc.bInterfaceNumber,
62 buf, size, 100);
63 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
64
65 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070066}
67
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070068static int wacom_set_report(struct usb_interface *intf, u8 type, u8 id,
69 void *buf, size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070070{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070071 struct usb_device *dev = interface_to_usbdev(intf);
72 int retval;
73
74 do {
75 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
76 USB_REQ_SET_REPORT,
77 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
78 (type << 8) + id,
79 intf->altsetting[0].desc.bInterfaceNumber,
80 buf, size, 1000);
81 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
82
83 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070084}
85
Benjamin Tissoires29b47392014-07-24 12:52:23 -070086static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
87 u8 *raw_data, int size)
Ping Cheng3bea7332006-07-13 18:01:36 -070088{
Benjamin Tissoires29b47392014-07-24 12:52:23 -070089 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070090
Benjamin Tissoires29b47392014-07-24 12:52:23 -070091 if (size > WACOM_PKGLEN_MAX)
92 return 1;
Ping Cheng3bea7332006-07-13 18:01:36 -070093
Benjamin Tissoires29b47392014-07-24 12:52:23 -070094 memcpy(wacom->wacom_wac.data, raw_data, size);
Ping Cheng3bea7332006-07-13 18:01:36 -070095
Benjamin Tissoires29b47392014-07-24 12:52:23 -070096 wacom_wac_irq(&wacom->wacom_wac, size);
97
98 return 0;
Ping Cheng3bea7332006-07-13 18:01:36 -070099}
100
Ping Cheng3bea7332006-07-13 18:01:36 -0700101static int wacom_open(struct input_dev *dev)
102{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400103 struct wacom *wacom = input_get_drvdata(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700104 int retval;
Ping Cheng3bea7332006-07-13 18:01:36 -0700105
Oliver Neukume7224092008-04-15 01:31:57 -0400106 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700107 retval = hid_hw_open(wacom->hdev);
Oliver Neukume7224092008-04-15 01:31:57 -0400108 mutex_unlock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700109
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700110 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -0700111}
112
113static void wacom_close(struct input_dev *dev)
114{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400115 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700116
Oliver Neukume7224092008-04-15 01:31:57 -0400117 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700118 hid_hw_close(wacom->hdev);
Oliver Neukume7224092008-04-15 01:31:57 -0400119 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700120}
121
Chris Bagwell16bf2882012-03-25 23:26:20 -0700122/*
Jason Gerecke115d5e12012-10-03 17:24:32 -0700123 * Calculate the resolution of the X or Y axis, given appropriate HID data.
124 * This function is little more than hidinput_calc_abs_res stripped down.
125 */
126static int wacom_calc_hid_res(int logical_extents, int physical_extents,
127 unsigned char unit, unsigned char exponent)
128{
129 int prev, unit_exponent;
130
131 /* Check if the extents are sane */
132 if (logical_extents <= 0 || physical_extents <= 0)
133 return 0;
134
135 /* Get signed value of nybble-sized twos-compliment exponent */
136 unit_exponent = exponent;
137 if (unit_exponent > 7)
138 unit_exponent -= 16;
139
140 /* Convert physical_extents to millimeters */
141 if (unit == 0x11) { /* If centimeters */
142 unit_exponent += 1;
143 } else if (unit == 0x13) { /* If inches */
144 prev = physical_extents;
145 physical_extents *= 254;
146 if (physical_extents < prev)
147 return 0;
148 unit_exponent -= 1;
149 } else {
150 return 0;
151 }
152
153 /* Apply negative unit exponent */
154 for (; unit_exponent < 0; unit_exponent++) {
155 prev = logical_extents;
156 logical_extents *= 10;
157 if (logical_extents < prev)
158 return 0;
159 }
160 /* Apply positive unit exponent */
161 for (; unit_exponent > 0; unit_exponent--) {
162 prev = physical_extents;
163 physical_extents *= 10;
164 if (physical_extents < prev)
165 return 0;
166 }
167
168 /* Calculate resolution */
169 return logical_extents / physical_extents;
170}
171
Chris Bagwell41343612011-10-26 22:32:52 -0700172static int wacom_parse_logical_collection(unsigned char *report,
173 struct wacom_features *features)
174{
175 int length = 0;
176
177 if (features->type == BAMBOO_PT) {
178
179 /* Logical collection is only used by 3rd gen Bamboo Touch */
180 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
Ping Cheng8b4a0c12012-01-31 00:07:33 -0800181 features->device_type = BTN_TOOL_FINGER;
Chris Bagwell41343612011-10-26 22:32:52 -0700182
Chris Bagwell41343612011-10-26 22:32:52 -0700183 features->x_max = features->y_max =
184 get_unaligned_le16(&report[10]);
185
186 length = 11;
187 }
188 return length;
189}
190
Ping Chengf393ee22012-04-29 21:09:17 -0700191static void wacom_retrieve_report_data(struct usb_interface *intf,
192 struct wacom_features *features)
193{
194 int result = 0;
195 unsigned char *rep_data;
196
197 rep_data = kmalloc(2, GFP_KERNEL);
198 if (rep_data) {
199
200 rep_data[0] = 12;
201 result = wacom_get_report(intf, WAC_HID_FEATURE_REPORT,
Ping Cheng61c91dd2012-06-28 16:46:27 -0700202 rep_data[0], rep_data, 2,
Ping Chengf393ee22012-04-29 21:09:17 -0700203 WAC_MSG_RETRIES);
204
205 if (result >= 0 && rep_data[1] > 2)
206 features->touch_max = rep_data[1];
207
208 kfree(rep_data);
209 }
210}
211
Chris Bagwell428f8582011-10-26 22:26:59 -0700212/*
213 * Interface Descriptor of wacom devices can be incomplete and
214 * inconsistent so wacom_features table is used to store stylus
215 * device's packet lengths, various maximum values, and tablet
216 * resolution based on product ID's.
217 *
218 * For devices that contain 2 interfaces, wacom_features table is
219 * inaccurate for the touch interface. Since the Interface Descriptor
220 * for touch interfaces has pretty complete data, this function exists
221 * to query tablet for this missing information instead of hard coding in
222 * an additional table.
223 *
224 * A typical Interface Descriptor for a stylus will contain a
225 * boot mouse application collection that is not of interest and this
226 * function will ignore it.
227 *
228 * It also contains a digitizer application collection that also is not
229 * of interest since any information it contains would be duplicate
230 * of what is in wacom_features. Usually it defines a report of an array
231 * of bytes that could be used as max length of the stylus packet returned.
232 * If it happens to define a Digitizer-Stylus Physical Collection then
233 * the X and Y logical values contain valid data but it is ignored.
234 *
235 * A typical Interface Descriptor for a touch interface will contain a
236 * Digitizer-Finger Physical Collection which will define both logical
237 * X/Y maximum as well as the physical size of tablet. Since touch
238 * interfaces haven't supported pressure or distance, this is enough
239 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700240 *
241 * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
242 * Collection. Instead they define a Logical Collection with a single
243 * Logical Maximum for both X and Y.
Jason Gereckeae584ca2012-04-03 15:50:40 -0700244 *
245 * Intuos5 touch interface does not contain useful data. We deal with
246 * this after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700247 */
248static int wacom_parse_hid(struct usb_interface *intf,
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700249 struct wac_hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800250 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500251{
252 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800253 char limit = 0;
254 /* result has to be defined as int for some devices */
Ping Cheng1d0d6df2013-11-25 18:43:45 -0800255 int result = 0, touch_max = 0;
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700256 int i = 0, page = 0, finger = 0, pen = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500257 unsigned char *report;
258
259 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
260 if (!report)
261 return -ENOMEM;
262
263 /* retrive report descriptors */
264 do {
265 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
266 USB_REQ_GET_DESCRIPTOR,
267 USB_RECIP_INTERFACE | USB_DIR_IN,
268 HID_DEVICET_REPORT << 8,
269 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
270 report,
271 hid_desc->wDescriptorLength,
272 5000); /* 5 secs */
Ping Chenga417ea42011-08-16 00:17:56 -0700273 } while (result < 0 && limit++ < WAC_MSG_RETRIES);
Ping Cheng545f4e92008-11-24 11:44:27 -0500274
Ping Cheng384318e2009-04-28 07:49:54 -0700275 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500276 if (result < 0)
277 goto out;
278
279 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
280
281 switch (report[i]) {
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700282 case HID_HDESC_USAGE_PAGE:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700283 page = report[i + 1];
284 i++;
Ping Cheng545f4e92008-11-24 11:44:27 -0500285 break;
286
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700287 case HID_HDESC_USAGE:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700288 switch (page << 16 | report[i + 1]) {
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700289 case HID_GD_X:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700290 if (finger) {
291 features->device_type = BTN_TOOL_FINGER;
292 /* touch device at least supports one touch point */
293 touch_max = 1;
294 switch (features->type) {
295 case TABLETPC2FG:
296 features->pktlen = WACOM_PKGLEN_TPC2FG;
297 break;
Ping Cheng3699dd72012-11-03 12:16:13 -0700298
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700299 case MTSCREEN:
300 case WACOM_24HDT:
301 features->pktlen = WACOM_PKGLEN_MTOUCH;
302 break;
Ping Cheng3699dd72012-11-03 12:16:13 -0700303
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700304 case MTTPC:
Jason Gerecked51ddb22014-05-14 17:14:29 -0700305 case MTTPC_B:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700306 features->pktlen = WACOM_PKGLEN_MTTPC;
307 break;
Ping Cheng6afdc282012-11-03 12:16:15 -0700308
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700309 case BAMBOO_PT:
310 features->pktlen = WACOM_PKGLEN_BBTOUCH;
311 break;
Ping Cheng3699dd72012-11-03 12:16:13 -0700312
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700313 default:
314 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
315 break;
316 }
Ping Cheng19635182012-04-29 21:09:18 -0700317
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700318 switch (features->type) {
319 case BAMBOO_PT:
320 features->x_phy =
321 get_unaligned_le16(&report[i + 5]);
322 features->x_max =
323 get_unaligned_le16(&report[i + 8]);
324 i += 15;
325 break;
Ping Cheng3699dd72012-11-03 12:16:13 -0700326
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700327 case WACOM_24HDT:
Ping Cheng545f4e92008-11-24 11:44:27 -0500328 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700329 get_unaligned_le16(&report[i + 3]);
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700330 features->x_phy =
331 get_unaligned_le16(&report[i + 8]);
332 features->unit = report[i - 1];
333 features->unitExpo = report[i - 3];
334 i += 12;
335 break;
336
Jason Gerecked51ddb22014-05-14 17:14:29 -0700337 case MTTPC_B:
338 features->x_max =
339 get_unaligned_le16(&report[i + 3]);
340 features->x_phy =
341 get_unaligned_le16(&report[i + 6]);
342 features->unit = report[i - 5];
343 features->unitExpo = report[i - 3];
344 i += 9;
345 break;
346
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700347 default:
348 features->x_max =
349 get_unaligned_le16(&report[i + 3]);
350 features->x_phy =
351 get_unaligned_le16(&report[i + 6]);
352 features->unit = report[i + 9];
353 features->unitExpo = report[i + 11];
354 i += 12;
355 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500356 }
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700357 } else if (pen) {
358 /* penabled only accepts exact bytes of data */
359 if (features->type >= TABLETPC)
360 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
361 features->device_type = BTN_TOOL_PEN;
362 features->x_max =
363 get_unaligned_le16(&report[i + 3]);
364 i += 4;
Ping Cheng545f4e92008-11-24 11:44:27 -0500365 }
366 break;
367
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700368 case HID_GD_Y:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700369 if (finger) {
370 switch (features->type) {
371 case TABLETPC2FG:
372 case MTSCREEN:
373 case MTTPC:
Ping Chengec67bbe2009-12-15 00:35:24 -0800374 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700375 get_unaligned_le16(&report[i + 3]);
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700376 features->y_phy =
377 get_unaligned_le16(&report[i + 6]);
378 i += 7;
379 break;
380
381 case WACOM_24HDT:
382 features->y_max =
383 get_unaligned_le16(&report[i + 3]);
384 features->y_phy =
385 get_unaligned_le16(&report[i - 2]);
386 i += 7;
387 break;
388
389 case BAMBOO_PT:
390 features->y_phy =
391 get_unaligned_le16(&report[i + 3]);
392 features->y_max =
393 get_unaligned_le16(&report[i + 6]);
394 i += 12;
395 break;
396
Jason Gerecked51ddb22014-05-14 17:14:29 -0700397 case MTTPC_B:
398 features->y_max =
399 get_unaligned_le16(&report[i + 3]);
400 features->y_phy =
401 get_unaligned_le16(&report[i + 6]);
402 i += 9;
403 break;
404
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700405 default:
406 features->y_max =
407 features->x_max;
408 features->y_phy =
409 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800410 i += 4;
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700411 break;
Ping Chengec67bbe2009-12-15 00:35:24 -0800412 }
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700413 } else if (pen) {
414 features->y_max =
415 get_unaligned_le16(&report[i + 3]);
416 i += 4;
Ping Chengec67bbe2009-12-15 00:35:24 -0800417 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500418 break;
419
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700420 case HID_DG_FINGER:
Ping Cheng545f4e92008-11-24 11:44:27 -0500421 finger = 1;
422 i++;
423 break;
424
Chris Bagwell428f8582011-10-26 22:26:59 -0700425 /*
426 * Requiring Stylus Usage will ignore boot mouse
427 * X/Y values and some cases of invalid Digitizer X/Y
428 * values commonly reported.
429 */
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700430 case HID_DG_STYLUS:
Ping Cheng545f4e92008-11-24 11:44:27 -0500431 pen = 1;
432 i++;
433 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700434
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700435 case HID_DG_CONTACTMAX:
Ping Cheng1cecc5c2012-06-28 16:47:30 -0700436 /* leave touch_max as is if predefined */
437 if (!features->touch_max)
438 wacom_retrieve_report_data(intf, features);
Ping Chengf393ee22012-04-29 21:09:17 -0700439 i++;
440 break;
Jason Gereckee9fc4132014-04-19 13:49:37 -0700441
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700442 case HID_DG_TIPPRESSURE:
Jason Gereckee9fc4132014-04-19 13:49:37 -0700443 if (pen) {
444 features->pressure_max =
445 get_unaligned_le16(&report[i + 3]);
446 i += 4;
447 }
448 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500449 }
450 break;
451
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700452 case HID_HDESC_COLLECTION_END:
Ping Chengec67bbe2009-12-15 00:35:24 -0800453 /* reset UsagePage and Finger */
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700454 finger = page = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500455 break;
Chris Bagwell41343612011-10-26 22:32:52 -0700456
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700457 case HID_HDESC_COLLECTION:
Chris Bagwell41343612011-10-26 22:32:52 -0700458 i++;
459 switch (report[i]) {
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700460 case HID_HDESC_COLLECTION_LOGICAL:
Chris Bagwell41343612011-10-26 22:32:52 -0700461 i += wacom_parse_logical_collection(&report[i],
462 features);
463 break;
464 }
465 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500466 }
467 }
468
Ping Cheng545f4e92008-11-24 11:44:27 -0500469 out:
Ping Cheng1d0d6df2013-11-25 18:43:45 -0800470 if (!features->touch_max && touch_max)
471 features->touch_max = touch_max;
Ping Cheng384318e2009-04-28 07:49:54 -0700472 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500473 kfree(report);
474 return result;
475}
476
Jason Gereckefe494bc2012-10-03 17:25:35 -0700477static int wacom_set_device_mode(struct usb_interface *intf, int report_id, int length, int mode)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700478{
479 unsigned char *rep_data;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700480 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700481
Jason Gereckefe494bc2012-10-03 17:25:35 -0700482 rep_data = kzalloc(length, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700483 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800484 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700485
Jason Gereckefe494bc2012-10-03 17:25:35 -0700486 do {
Chris Bagwell9937c022013-01-23 19:37:34 -0800487 rep_data[0] = report_id;
488 rep_data[1] = mode;
489
Jason Gereckefe494bc2012-10-03 17:25:35 -0700490 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
491 report_id, rep_data, length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700492 if (error >= 0)
493 error = wacom_get_report(intf, WAC_HID_FEATURE_REPORT,
494 report_id, rep_data, length, 1);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700495 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700496
497 kfree(rep_data);
498
499 return error < 0 ? error : 0;
500}
501
Jason Gereckefe494bc2012-10-03 17:25:35 -0700502/*
503 * Switch the tablet into its most-capable mode. Wacom tablets are
504 * typically configured to power-up in a mode which sends mouse-like
505 * reports to the OS. To get absolute position, pressure data, etc.
506 * from the tablet, it is necessary to switch the tablet out of this
507 * mode and into one which sends the full range of tablet data.
508 */
509static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
510{
511 if (features->device_type == BTN_TOOL_FINGER) {
512 if (features->type > TABLETPC) {
513 /* MT Tablet PC touch */
514 return wacom_set_device_mode(intf, 3, 4, 4);
515 }
Jason Gerecke36d3c512013-09-20 09:47:35 -0700516 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
Jason Gereckeb1e42792012-10-21 00:38:04 -0700517 return wacom_set_device_mode(intf, 18, 3, 2);
518 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700519 } else if (features->device_type == BTN_TOOL_PEN) {
520 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
521 return wacom_set_device_mode(intf, 2, 2, 2);
522 }
523 }
524
525 return 0;
526}
527
Ping Chengec67bbe2009-12-15 00:35:24 -0800528static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
Ping Cheng19635182012-04-29 21:09:18 -0700529 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800530{
531 int error = 0;
532 struct usb_host_interface *interface = intf->cur_altsetting;
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700533 struct wac_hid_descriptor *hid_desc;
Ping Chengec67bbe2009-12-15 00:35:24 -0800534
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700535 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800536 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700537 features->x_fuzz = 4;
538 features->y_fuzz = 4;
539 features->pressure_fuzz = 0;
540 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800541
Chris Bagwelld3825d52012-03-25 23:26:11 -0700542 /*
543 * The wireless device HID is basic and layout conflicts with
544 * other tablets (monitor and touch interface can look like pen).
545 * Skip the query for this type and modify defaults based on
546 * interface number.
547 */
548 if (features->type == WIRELESS) {
549 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
550 features->device_type = 0;
551 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
Ping Chengadad0042012-06-28 16:48:17 -0700552 features->device_type = BTN_TOOL_FINGER;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700553 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
554 }
555 }
556
Ping Cheng19635182012-04-29 21:09:18 -0700557 /* only devices that support touch need to retrieve the info */
Ping Chengea2e6022012-06-12 00:14:12 -0700558 if (features->type < BAMBOO_PT) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800559 goto out;
Ping Cheng19635182012-04-29 21:09:18 -0700560 }
Ping Chengec67bbe2009-12-15 00:35:24 -0800561
Dmitry Torokhova882c932012-05-02 00:13:38 -0700562 error = usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc);
563 if (error) {
564 error = usb_get_extra_descriptor(&interface->endpoint[0],
565 HID_DEVICET_REPORT, &hid_desc);
566 if (error) {
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -0700567 dev_err(&intf->dev,
568 "can not retrieve extra class descriptor\n");
Ping Chengec67bbe2009-12-15 00:35:24 -0800569 goto out;
570 }
571 }
572 error = wacom_parse_hid(intf, hid_desc, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800573
Ping Chengec67bbe2009-12-15 00:35:24 -0800574 out:
575 return error;
576}
577
Ping Cheng4492eff2010-03-19 22:18:15 -0700578struct wacom_usbdev_data {
579 struct list_head list;
580 struct kref kref;
581 struct usb_device *dev;
582 struct wacom_shared shared;
583};
584
585static LIST_HEAD(wacom_udev_list);
586static DEFINE_MUTEX(wacom_udev_list_lock);
587
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700588static struct usb_device *wacom_get_sibling(struct usb_device *dev, int vendor, int product)
589{
590 int port1;
591 struct usb_device *sibling;
592
593 if (vendor == 0 && product == 0)
594 return dev;
595
596 if (dev->parent == NULL)
597 return NULL;
598
599 usb_hub_for_each_child(dev->parent, port1, sibling) {
600 struct usb_device_descriptor *d;
601 if (sibling == NULL)
602 continue;
603
604 d = &sibling->descriptor;
605 if (d->idVendor == vendor && d->idProduct == product)
606 return sibling;
607 }
608
609 return NULL;
610}
611
Ping Cheng4492eff2010-03-19 22:18:15 -0700612static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
613{
614 struct wacom_usbdev_data *data;
615
616 list_for_each_entry(data, &wacom_udev_list, list) {
617 if (data->dev == dev) {
618 kref_get(&data->kref);
619 return data;
620 }
621 }
622
623 return NULL;
624}
625
626static int wacom_add_shared_data(struct wacom_wac *wacom,
627 struct usb_device *dev)
628{
629 struct wacom_usbdev_data *data;
630 int retval = 0;
631
632 mutex_lock(&wacom_udev_list_lock);
633
634 data = wacom_get_usbdev_data(dev);
635 if (!data) {
636 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
637 if (!data) {
638 retval = -ENOMEM;
639 goto out;
640 }
641
642 kref_init(&data->kref);
643 data->dev = dev;
644 list_add_tail(&data->list, &wacom_udev_list);
645 }
646
647 wacom->shared = &data->shared;
648
649out:
650 mutex_unlock(&wacom_udev_list_lock);
651 return retval;
652}
653
654static void wacom_release_shared_data(struct kref *kref)
655{
656 struct wacom_usbdev_data *data =
657 container_of(kref, struct wacom_usbdev_data, kref);
658
659 mutex_lock(&wacom_udev_list_lock);
660 list_del(&data->list);
661 mutex_unlock(&wacom_udev_list_lock);
662
663 kfree(data);
664}
665
666static void wacom_remove_shared_data(struct wacom_wac *wacom)
667{
668 struct wacom_usbdev_data *data;
669
670 if (wacom->shared) {
671 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
672 kref_put(&data->kref, wacom_release_shared_data);
673 wacom->shared = NULL;
674 }
675}
676
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700677static int wacom_led_control(struct wacom *wacom)
678{
679 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700680 int retval;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700681
682 buf = kzalloc(9, GFP_KERNEL);
683 if (!buf)
684 return -ENOMEM;
685
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700686 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700687 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700688 /*
689 * Touch Ring and crop mark LED luminance may take on
690 * one of four values:
691 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
692 */
693 int ring_led = wacom->led.select[0] & 0x03;
694 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
695 int crop_lum = 0;
Ping Cheng09e7d942011-10-04 23:51:14 -0700696
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700697 buf[0] = WAC_CMD_LED_CONTROL;
698 buf[1] = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
699 }
700 else {
701 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700702
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700703 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
704 wacom->wacom_wac.features.type == WACOM_24HD)
705 led |= (wacom->led.select[1] << 4) | 0x40;
706
707 buf[0] = WAC_CMD_LED_CONTROL;
708 buf[1] = led;
709 buf[2] = wacom->led.llv;
710 buf[3] = wacom->led.hlv;
711 buf[4] = wacom->led.img_lum;
712 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700713
714 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
715 buf, 9, WAC_CMD_RETRIES);
716 kfree(buf);
717
718 return retval;
719}
720
721static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
722{
723 unsigned char *buf;
724 int i, retval;
725
726 buf = kzalloc(259, GFP_KERNEL);
727 if (!buf)
728 return -ENOMEM;
729
730 /* Send 'start' command */
731 buf[0] = WAC_CMD_ICON_START;
732 buf[1] = 1;
733 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
734 buf, 2, WAC_CMD_RETRIES);
735 if (retval < 0)
736 goto out;
737
738 buf[0] = WAC_CMD_ICON_XFER;
739 buf[1] = button_id & 0x07;
740 for (i = 0; i < 4; i++) {
741 buf[2] = i;
742 memcpy(buf + 3, img + i * 256, 256);
743
744 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
745 buf, 259, WAC_CMD_RETRIES);
746 if (retval < 0)
747 break;
748 }
749
750 /* Send 'stop' */
751 buf[0] = WAC_CMD_ICON_START;
752 buf[1] = 0;
753 wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
754 buf, 2, WAC_CMD_RETRIES);
755
756out:
757 kfree(buf);
758 return retval;
759}
760
Ping Cheng09e7d942011-10-04 23:51:14 -0700761static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700762 const char *buf, size_t count)
763{
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700764 struct hid_device *hdev = dev_get_drvdata(dev);
765 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700766 unsigned int id;
767 int err;
768
769 err = kstrtouint(buf, 10, &id);
770 if (err)
771 return err;
772
773 mutex_lock(&wacom->lock);
774
Ping Cheng09e7d942011-10-04 23:51:14 -0700775 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700776 err = wacom_led_control(wacom);
777
778 mutex_unlock(&wacom->lock);
779
780 return err < 0 ? err : count;
781}
782
Ping Cheng09e7d942011-10-04 23:51:14 -0700783#define DEVICE_LED_SELECT_ATTR(SET_ID) \
784static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
785 struct device_attribute *attr, const char *buf, size_t count) \
786{ \
787 return wacom_led_select_store(dev, SET_ID, buf, count); \
788} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700789static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
790 struct device_attribute *attr, char *buf) \
791{ \
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700792 struct hid_device *hdev = dev_get_drvdata(dev); \
793 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700794 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
795} \
796static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
797 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700798 wacom_led##SET_ID##_select_store)
799
800DEVICE_LED_SELECT_ATTR(0);
801DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700802
803static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
804 const char *buf, size_t count)
805{
806 unsigned int value;
807 int err;
808
809 err = kstrtouint(buf, 10, &value);
810 if (err)
811 return err;
812
813 mutex_lock(&wacom->lock);
814
815 *dest = value & 0x7f;
816 err = wacom_led_control(wacom);
817
818 mutex_unlock(&wacom->lock);
819
820 return err < 0 ? err : count;
821}
822
823#define DEVICE_LUMINANCE_ATTR(name, field) \
824static ssize_t wacom_##name##_luminance_store(struct device *dev, \
825 struct device_attribute *attr, const char *buf, size_t count) \
826{ \
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700827 struct hid_device *hdev = dev_get_drvdata(dev); \
828 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700829 \
830 return wacom_luminance_store(wacom, &wacom->led.field, \
831 buf, count); \
832} \
833static DEVICE_ATTR(name##_luminance, S_IWUSR, \
834 NULL, wacom_##name##_luminance_store)
835
836DEVICE_LUMINANCE_ATTR(status0, llv);
837DEVICE_LUMINANCE_ATTR(status1, hlv);
838DEVICE_LUMINANCE_ATTR(buttons, img_lum);
839
840static ssize_t wacom_button_image_store(struct device *dev, int button_id,
841 const char *buf, size_t count)
842{
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700843 struct hid_device *hdev = dev_get_drvdata(dev);
844 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700845 int err;
846
847 if (count != 1024)
848 return -EINVAL;
849
850 mutex_lock(&wacom->lock);
851
852 err = wacom_led_putimage(wacom, button_id, buf);
853
854 mutex_unlock(&wacom->lock);
855
856 return err < 0 ? err : count;
857}
858
859#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
860static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
861 struct device_attribute *attr, const char *buf, size_t count) \
862{ \
863 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
864} \
865static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
866 NULL, wacom_btnimg##BUTTON_ID##_store)
867
868DEVICE_BTNIMG_ATTR(0);
869DEVICE_BTNIMG_ATTR(1);
870DEVICE_BTNIMG_ATTR(2);
871DEVICE_BTNIMG_ATTR(3);
872DEVICE_BTNIMG_ATTR(4);
873DEVICE_BTNIMG_ATTR(5);
874DEVICE_BTNIMG_ATTR(6);
875DEVICE_BTNIMG_ATTR(7);
876
Ping Cheng09e7d942011-10-04 23:51:14 -0700877static struct attribute *cintiq_led_attrs[] = {
878 &dev_attr_status_led0_select.attr,
879 &dev_attr_status_led1_select.attr,
880 NULL
881};
882
883static struct attribute_group cintiq_led_attr_group = {
884 .name = "wacom_led",
885 .attrs = cintiq_led_attrs,
886};
887
888static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700889 &dev_attr_status0_luminance.attr,
890 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700891 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700892 &dev_attr_buttons_luminance.attr,
893 &dev_attr_button0_rawimg.attr,
894 &dev_attr_button1_rawimg.attr,
895 &dev_attr_button2_rawimg.attr,
896 &dev_attr_button3_rawimg.attr,
897 &dev_attr_button4_rawimg.attr,
898 &dev_attr_button5_rawimg.attr,
899 &dev_attr_button6_rawimg.attr,
900 &dev_attr_button7_rawimg.attr,
901 NULL
902};
903
Ping Cheng09e7d942011-10-04 23:51:14 -0700904static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700905 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700906 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700907};
908
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700909static struct attribute *intuos5_led_attrs[] = {
910 &dev_attr_status0_luminance.attr,
911 &dev_attr_status_led0_select.attr,
912 NULL
913};
914
915static struct attribute_group intuos5_led_attr_group = {
916 .name = "wacom_led",
917 .attrs = intuos5_led_attrs,
918};
919
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700920static int wacom_initialize_leds(struct wacom *wacom)
921{
922 int error;
923
Ping Cheng09e7d942011-10-04 23:51:14 -0700924 /* Initialize default values */
925 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700926 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700927 case INTUOS4:
928 case INTUOS4L:
929 wacom->led.select[0] = 0;
930 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700931 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700932 wacom->led.hlv = 20;
933 wacom->led.img_lum = 10;
Ping Cheng09e7d942011-10-04 23:51:14 -0700934 error = sysfs_create_group(&wacom->intf->dev.kobj,
935 &intuos4_led_attr_group);
936 break;
937
Jason Gerecke246835f2011-12-12 00:12:04 -0800938 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700939 case WACOM_21UX2:
940 wacom->led.select[0] = 0;
941 wacom->led.select[1] = 0;
942 wacom->led.llv = 0;
943 wacom->led.hlv = 0;
944 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700945
946 error = sysfs_create_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700947 &cintiq_led_attr_group);
948 break;
949
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700950 case INTUOS5S:
951 case INTUOS5:
952 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700953 case INTUOSPS:
954 case INTUOSPM:
955 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700956 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
957 wacom->led.select[0] = 0;
958 wacom->led.select[1] = 0;
959 wacom->led.llv = 32;
960 wacom->led.hlv = 0;
961 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700962
Ping Chengc2b0c272013-09-20 09:50:14 -0700963 error = sysfs_create_group(&wacom->intf->dev.kobj,
964 &intuos5_led_attr_group);
965 } else
966 return 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700967 break;
968
Ping Cheng09e7d942011-10-04 23:51:14 -0700969 default:
970 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700971 }
972
Ping Cheng09e7d942011-10-04 23:51:14 -0700973 if (error) {
974 dev_err(&wacom->intf->dev,
975 "cannot create sysfs group err: %d\n", error);
976 return error;
977 }
978 wacom_led_control(wacom);
979
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700980 return 0;
981}
982
983static void wacom_destroy_leds(struct wacom *wacom)
984{
Ping Cheng09e7d942011-10-04 23:51:14 -0700985 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700986 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700987 case INTUOS4:
988 case INTUOS4L:
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700989 sysfs_remove_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700990 &intuos4_led_attr_group);
991 break;
992
Jason Gerecke246835f2011-12-12 00:12:04 -0800993 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700994 case WACOM_21UX2:
995 sysfs_remove_group(&wacom->intf->dev.kobj,
996 &cintiq_led_attr_group);
997 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700998
999 case INTUOS5S:
1000 case INTUOS5:
1001 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001002 case INTUOSPS:
1003 case INTUOSPM:
1004 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -07001005 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
1006 sysfs_remove_group(&wacom->intf->dev.kobj,
1007 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001008 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001009 }
1010}
1011
Chris Bagwella1d552c2012-03-25 23:26:30 -07001012static enum power_supply_property wacom_battery_props[] = {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001013 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001014 POWER_SUPPLY_PROP_CAPACITY
1015};
1016
1017static int wacom_battery_get_property(struct power_supply *psy,
1018 enum power_supply_property psp,
1019 union power_supply_propval *val)
1020{
1021 struct wacom *wacom = container_of(psy, struct wacom, battery);
1022 int ret = 0;
1023
1024 switch (psp) {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001025 case POWER_SUPPLY_PROP_SCOPE:
1026 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1027 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001028 case POWER_SUPPLY_PROP_CAPACITY:
1029 val->intval =
1030 wacom->wacom_wac.battery_capacity * 100 / 31;
1031 break;
1032 default:
1033 ret = -EINVAL;
1034 break;
1035 }
1036
1037 return ret;
1038}
1039
1040static int wacom_initialize_battery(struct wacom *wacom)
1041{
1042 int error = 0;
1043
1044 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) {
1045 wacom->battery.properties = wacom_battery_props;
1046 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
1047 wacom->battery.get_property = wacom_battery_get_property;
1048 wacom->battery.name = "wacom_battery";
1049 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1050 wacom->battery.use_for_apm = 0;
1051
1052 error = power_supply_register(&wacom->usbdev->dev,
1053 &wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001054
1055 if (!error)
1056 power_supply_powers(&wacom->battery,
1057 &wacom->usbdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001058 }
1059
1060 return error;
1061}
1062
1063static void wacom_destroy_battery(struct wacom *wacom)
1064{
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001065 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR &&
1066 wacom->battery.dev) {
Chris Bagwella1d552c2012-03-25 23:26:30 -07001067 power_supply_unregister(&wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001068 wacom->battery.dev = NULL;
1069 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001070}
1071
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001072static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001073{
1074 struct input_dev *input_dev;
1075 struct usb_interface *intf = wacom->intf;
1076 struct usb_device *dev = interface_to_usbdev(intf);
1077 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001078
1079 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001080 if (!input_dev)
1081 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001082
1083 input_dev->name = wacom_wac->name;
Benjamin Tissoires7097d4c2014-07-24 12:48:06 -07001084 input_dev->phys = wacom->phys;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001085 input_dev->dev.parent = &intf->dev;
1086 input_dev->open = wacom_open;
1087 input_dev->close = wacom_close;
1088 usb_to_input_id(dev, &input_dev->id);
1089 input_set_drvdata(input_dev, wacom);
1090
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001091 return input_dev;
1092}
1093
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001094static void wacom_unregister_inputs(struct wacom *wacom)
1095{
1096 if (wacom->wacom_wac.input)
1097 input_unregister_device(wacom->wacom_wac.input);
1098 if (wacom->wacom_wac.pad_input)
1099 input_unregister_device(wacom->wacom_wac.pad_input);
1100 wacom->wacom_wac.input = NULL;
1101 wacom->wacom_wac.pad_input = NULL;
1102}
1103
1104static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001105{
1106 struct input_dev *input_dev, *pad_input_dev;
1107 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1108 int error;
1109
1110 input_dev = wacom_allocate_input(wacom);
1111 pad_input_dev = wacom_allocate_input(wacom);
1112 if (!input_dev || !pad_input_dev) {
1113 error = -ENOMEM;
1114 goto fail1;
1115 }
1116
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001117 wacom_wac->input = input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001118 wacom_wac->pad_input = pad_input_dev;
1119 wacom_wac->pad_input->name = wacom_wac->pad_name;
1120
Ping Cheng19635182012-04-29 21:09:18 -07001121 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1122 if (error)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001123 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001124
1125 error = input_register_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001126 if (error)
1127 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001128
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001129 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1130 if (error) {
1131 /* no pad in use on this interface */
1132 input_free_device(pad_input_dev);
1133 wacom_wac->pad_input = NULL;
1134 pad_input_dev = NULL;
1135 } else {
1136 error = input_register_device(pad_input_dev);
1137 if (error)
1138 goto fail3;
1139 }
1140
Ping Cheng19635182012-04-29 21:09:18 -07001141 return 0;
1142
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001143fail3:
1144 input_unregister_device(input_dev);
1145 input_dev = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001146fail2:
Ping Cheng19635182012-04-29 21:09:18 -07001147 wacom_wac->input = NULL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001148 wacom_wac->pad_input = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001149fail1:
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001150 if (input_dev)
1151 input_free_device(input_dev);
1152 if (pad_input_dev)
1153 input_free_device(pad_input_dev);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001154 return error;
1155}
1156
Chris Bagwell16bf2882012-03-25 23:26:20 -07001157static void wacom_wireless_work(struct work_struct *work)
1158{
1159 struct wacom *wacom = container_of(work, struct wacom, work);
1160 struct usb_device *usbdev = wacom->usbdev;
1161 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001162 struct hid_device *hdev1, *hdev2;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001163 struct wacom *wacom1, *wacom2;
1164 struct wacom_wac *wacom_wac1, *wacom_wac2;
1165 int error;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001166
1167 /*
1168 * Regardless if this is a disconnect or a new tablet,
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001169 * remove any existing input and battery devices.
Chris Bagwell16bf2882012-03-25 23:26:20 -07001170 */
1171
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001172 wacom_destroy_battery(wacom);
1173
Chris Bagwell16bf2882012-03-25 23:26:20 -07001174 /* Stylus interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001175 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1176 wacom1 = hid_get_drvdata(hdev1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001177 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001178 wacom_unregister_inputs(wacom1);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001179
1180 /* Touch interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001181 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1182 wacom2 = hid_get_drvdata(hdev2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001183 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001184 wacom_unregister_inputs(wacom2);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001185
1186 if (wacom_wac->pid == 0) {
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001187 dev_info(&wacom->intf->dev, "wireless tablet disconnected\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001188 } else {
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001189 const struct hid_device_id *id = wacom_ids;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001190
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001191 dev_info(&wacom->intf->dev,
1192 "wireless tablet connected with PID %x\n",
1193 wacom_wac->pid);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001194
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001195 while (id->bus) {
1196 if (id->vendor == USB_VENDOR_ID_WACOM &&
1197 id->product == wacom_wac->pid)
Chris Bagwell16bf2882012-03-25 23:26:20 -07001198 break;
1199 id++;
1200 }
1201
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001202 if (!id->bus) {
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001203 dev_info(&wacom->intf->dev,
1204 "ignoring unknown PID.\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001205 return;
1206 }
1207
1208 /* Stylus interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001209 wacom_wac1->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001210 *((struct wacom_features *)id->driver_data);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001211 wacom_wac1->features.device_type = BTN_TOOL_PEN;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001212 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1213 wacom_wac1->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001214 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1215 wacom_wac1->features.name);
Ping Cheng961794a2013-12-05 12:54:53 -08001216 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1217 wacom_wac1->shared->type = wacom_wac1->features.type;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001218 error = wacom_register_inputs(wacom1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001219 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001220 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001221
1222 /* Touch interface */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001223 if (wacom_wac1->features.touch_max ||
1224 wacom_wac1->features.type == INTUOSHT) {
Ping Cheng57bcfce2013-10-15 23:44:00 -07001225 wacom_wac2->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001226 *((struct wacom_features *)id->driver_data);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001227 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1228 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1229 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1230 if (wacom_wac2->features.touch_max)
1231 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1232 "%s (WL) Finger",wacom_wac2->features.name);
1233 else
1234 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1235 "%s (WL) Pad",wacom_wac2->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001236 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1237 "%s (WL) Pad", wacom_wac2->features.name);
1238 error = wacom_register_inputs(wacom2);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001239 if (error)
1240 goto fail;
Ping Cheng961794a2013-12-05 12:54:53 -08001241
1242 if (wacom_wac1->features.type == INTUOSHT &&
1243 wacom_wac1->features.touch_max)
1244 wacom_wac->shared->touch_input = wacom_wac2->input;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001245 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001246
1247 error = wacom_initialize_battery(wacom);
1248 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001249 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001250 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001251
1252 return;
1253
Ping Cheng57bcfce2013-10-15 23:44:00 -07001254fail:
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001255 wacom_unregister_inputs(wacom1);
1256 wacom_unregister_inputs(wacom2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001257 return;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001258}
1259
Ping Cheng401d7d12013-07-28 00:38:54 -07001260/*
1261 * Not all devices report physical dimensions from HID.
1262 * Compute the default from hardcoded logical dimension
1263 * and resolution before driver overwrites them.
1264 */
1265static void wacom_set_default_phy(struct wacom_features *features)
1266{
1267 if (features->x_resolution) {
1268 features->x_phy = (features->x_max * 100) /
1269 features->x_resolution;
1270 features->y_phy = (features->y_max * 100) /
1271 features->y_resolution;
1272 }
1273}
1274
1275static void wacom_calculate_res(struct wacom_features *features)
1276{
1277 features->x_resolution = wacom_calc_hid_res(features->x_max,
1278 features->x_phy,
1279 features->unit,
1280 features->unitExpo);
1281 features->y_resolution = wacom_calc_hid_res(features->y_max,
1282 features->y_phy,
1283 features->unit,
1284 features->unitExpo);
1285}
1286
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001287static int wacom_probe(struct hid_device *hdev,
1288 const struct hid_device_id *id)
Ping Cheng3bea7332006-07-13 18:01:36 -07001289{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001290 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Ping Cheng3bea7332006-07-13 18:01:36 -07001291 struct usb_device *dev = interface_to_usbdev(intf);
1292 struct usb_endpoint_descriptor *endpoint;
1293 struct wacom *wacom;
1294 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001295 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001296 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001297
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001298 if (!id->driver_data)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001299 return -EINVAL;
1300
Ping Cheng3bea7332006-07-13 18:01:36 -07001301 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001302 if (!wacom)
1303 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001304
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001305 hid_set_drvdata(hdev, wacom);
1306 wacom->hdev = hdev;
1307
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001308 wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001309 wacom_wac->features = *((struct wacom_features *)id->driver_data);
Jason Childse33da8a2010-02-17 22:38:31 -08001310 features = &wacom_wac->features;
1311 if (features->pktlen > WACOM_PKGLEN_MAX) {
1312 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001313 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -08001314 }
1315
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001316 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1317 error = -ENODEV;
Jason Childse33da8a2010-02-17 22:38:31 -08001318 goto fail1;
1319 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001320
Ping Cheng3bea7332006-07-13 18:01:36 -07001321 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001322 wacom->intf = intf;
1323 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001324 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001325 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
1326 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
1327
Ping Cheng545f4e92008-11-24 11:44:27 -05001328 endpoint = &intf->cur_altsetting->endpoint[0].desc;
1329
Ping Cheng401d7d12013-07-28 00:38:54 -07001330 /* set the default size in case we do not get them from hid */
1331 wacom_set_default_phy(features);
1332
Ping Chengf393ee22012-04-29 21:09:17 -07001333 /* Retrieve the physical and logical size for touch devices */
Ping Chengec67bbe2009-12-15 00:35:24 -08001334 error = wacom_retrieve_hid_descriptor(intf, features);
1335 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001336 goto fail1;
Ping Cheng545f4e92008-11-24 11:44:27 -05001337
Jason Gereckeae584ca2012-04-03 15:50:40 -07001338 /*
1339 * Intuos5 has no useful data about its touch interface in its
1340 * HID descriptor. If this is the touch interface (wMaxPacketSize
1341 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1342 */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001343 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
Jason Gereckeae584ca2012-04-03 15:50:40 -07001344 if (endpoint->wMaxPacketSize == WACOM_PKGLEN_BBTOUCH3) {
1345 features->device_type = BTN_TOOL_FINGER;
1346 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
1347
Jason Gereckeae584ca2012-04-03 15:50:40 -07001348 features->x_max = 4096;
1349 features->y_max = 4096;
1350 } else {
1351 features->device_type = BTN_TOOL_PEN;
1352 }
1353 }
1354
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001355 wacom_setup_device_quirks(features);
1356
Ping Cheng401d7d12013-07-28 00:38:54 -07001357 /* set unit to "100th of a mm" for devices not reported by HID */
1358 if (!features->unit) {
1359 features->unit = 0x11;
1360 features->unitExpo = 16 - 3;
1361 }
1362 wacom_calculate_res(features);
1363
Ping Cheng49b764a2010-02-20 00:53:49 -08001364 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001365 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1366 "%s Pad", features->name);
Ping Cheng49b764a2010-02-20 00:53:49 -08001367
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001368 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Jason Gereckeaea2bf62012-10-21 00:38:03 -07001369 struct usb_device *other_dev;
1370
Ping Cheng49b764a2010-02-20 00:53:49 -08001371 /* Append the device type to the name */
Ping Cheng57bcfce2013-10-15 23:44:00 -07001372 if (features->device_type != BTN_TOOL_FINGER)
1373 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1374 else if (features->touch_max)
1375 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1376 else
1377 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
Ping Cheng4492eff2010-03-19 22:18:15 -07001378
Jason Gereckeaea2bf62012-10-21 00:38:03 -07001379 other_dev = wacom_get_sibling(dev, features->oVid, features->oPid);
1380 if (other_dev == NULL || wacom_get_usbdev_data(other_dev) == NULL)
1381 other_dev = dev;
1382 error = wacom_add_shared_data(wacom_wac, other_dev);
Ping Cheng4492eff2010-03-19 22:18:15 -07001383 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001384 goto fail1;
Ping Cheng49b764a2010-02-20 00:53:49 -08001385 }
1386
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001387 error = wacom_initialize_leds(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001388 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001389 goto fail2;
Ping Cheng3bea7332006-07-13 18:01:36 -07001390
Chris Bagwelld3825d52012-03-25 23:26:11 -07001391 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001392 error = wacom_register_inputs(wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001393 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001394 goto fail3;
Chris Bagwelld3825d52012-03-25 23:26:11 -07001395 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001396
Ping Chengec67bbe2009-12-15 00:35:24 -08001397 /* Note that if query fails it is not a hard failure */
1398 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -07001399
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001400 /* Regular HID work starts now */
1401 error = hid_parse(hdev);
1402 if (error) {
1403 hid_err(hdev, "parse failed\n");
1404 goto fail4;
Chris Bagwelld3825d52012-03-25 23:26:11 -07001405 }
Ping Cheng961794a2013-12-05 12:54:53 -08001406
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001407 error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1408 if (error) {
1409 hid_err(hdev, "hw start failed\n");
1410 goto fail4;
1411 }
1412
1413 if (features->quirks & WACOM_QUIRK_MONITOR)
1414 error = hid_hw_open(hdev);
1415
Ping Cheng961794a2013-12-05 12:54:53 -08001416 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1417 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1418 wacom_wac->shared->touch_input = wacom_wac->input;
1419 }
1420
Ping Cheng3bea7332006-07-13 18:01:36 -07001421 return 0;
1422
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001423 fail4: wacom_unregister_inputs(wacom);
1424 fail3: wacom_destroy_leds(wacom);
1425 fail2: wacom_remove_shared_data(wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001426 fail1: kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001427 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001428 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001429}
1430
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001431static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001432{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001433 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001434
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001435 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001436
Chris Bagwell16bf2882012-03-25 23:26:20 -07001437 cancel_work_sync(&wacom->work);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001438 wacom_unregister_inputs(wacom);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001439 wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001440 wacom_destroy_leds(wacom);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001441 wacom_remove_shared_data(&wacom->wacom_wac);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001442
1443 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001444 kfree(wacom);
1445}
1446
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001447static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001448{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001449 struct wacom *wacom = hid_get_drvdata(hdev);
1450 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001451
1452 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001453
1454 /* switch to wacom mode first */
1455 wacom_query_tablet_data(wacom->intf, features);
1456 wacom_led_control(wacom);
1457
Oliver Neukume7224092008-04-15 01:31:57 -04001458 mutex_unlock(&wacom->lock);
1459
1460 return 0;
1461}
1462
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001463static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001464{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001465 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001466}
1467
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001468static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001469 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001470 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001471 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001472 .remove = wacom_remove,
1473#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001474 .resume = wacom_resume,
1475 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001476#endif
1477 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001478};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001479module_hid_driver(wacom_driver);