blob: 120ab1b5df3c7709092788c67ef01b9888d59308 [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
Ping Chenga417ea42011-08-16 00:17:56 -070037#define WAC_MSG_RETRIES 5
Ping Cheng3bea7332006-07-13 18:01:36 -070038
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070039#define WAC_CMD_LED_CONTROL 0x20
40#define WAC_CMD_ICON_START 0x21
41#define WAC_CMD_ICON_XFER 0x23
42#define WAC_CMD_RETRIES 10
43
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070044static int wacom_get_report(struct hid_device *hdev, u8 type, u8 id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070045 void *buf, size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070046{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070047 int retval;
48
49 do {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070050 retval = hid_hw_raw_request(hdev, id, buf, size, type,
51 HID_REQ_GET_REPORT);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070052 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
53
54 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070055}
56
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070057static int wacom_set_report(struct hid_device *hdev, u8 type, u8 id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070058 void *buf, size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070059{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070060 int retval;
61
62 do {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070063 retval = hid_hw_raw_request(hdev, id, buf, size, type,
64 HID_REQ_SET_REPORT);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070065 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
66
67 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070068}
69
Benjamin Tissoires29b47392014-07-24 12:52:23 -070070static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
71 u8 *raw_data, int size)
Ping Cheng3bea7332006-07-13 18:01:36 -070072{
Benjamin Tissoires29b47392014-07-24 12:52:23 -070073 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070074
Benjamin Tissoires29b47392014-07-24 12:52:23 -070075 if (size > WACOM_PKGLEN_MAX)
76 return 1;
Ping Cheng3bea7332006-07-13 18:01:36 -070077
Benjamin Tissoires29b47392014-07-24 12:52:23 -070078 memcpy(wacom->wacom_wac.data, raw_data, size);
Ping Cheng3bea7332006-07-13 18:01:36 -070079
Benjamin Tissoires29b47392014-07-24 12:52:23 -070080 wacom_wac_irq(&wacom->wacom_wac, size);
81
82 return 0;
Ping Cheng3bea7332006-07-13 18:01:36 -070083}
84
Ping Cheng3bea7332006-07-13 18:01:36 -070085static int wacom_open(struct input_dev *dev)
86{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040087 struct wacom *wacom = input_get_drvdata(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070088 int retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070089
Oliver Neukume7224092008-04-15 01:31:57 -040090 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070091 retval = hid_hw_open(wacom->hdev);
Oliver Neukume7224092008-04-15 01:31:57 -040092 mutex_unlock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070093
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -070094 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070095}
96
97static void wacom_close(struct input_dev *dev)
98{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040099 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700100
Oliver Neukume7224092008-04-15 01:31:57 -0400101 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700102 hid_hw_close(wacom->hdev);
Oliver Neukume7224092008-04-15 01:31:57 -0400103 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700104}
105
Chris Bagwell16bf2882012-03-25 23:26:20 -0700106/*
Jason Gerecke115d5e12012-10-03 17:24:32 -0700107 * Calculate the resolution of the X or Y axis, given appropriate HID data.
108 * This function is little more than hidinput_calc_abs_res stripped down.
109 */
110static int wacom_calc_hid_res(int logical_extents, int physical_extents,
111 unsigned char unit, unsigned char exponent)
112{
113 int prev, unit_exponent;
114
115 /* Check if the extents are sane */
116 if (logical_extents <= 0 || physical_extents <= 0)
117 return 0;
118
119 /* Get signed value of nybble-sized twos-compliment exponent */
120 unit_exponent = exponent;
121 if (unit_exponent > 7)
122 unit_exponent -= 16;
123
124 /* Convert physical_extents to millimeters */
125 if (unit == 0x11) { /* If centimeters */
126 unit_exponent += 1;
127 } else if (unit == 0x13) { /* If inches */
128 prev = physical_extents;
129 physical_extents *= 254;
130 if (physical_extents < prev)
131 return 0;
132 unit_exponent -= 1;
133 } else {
134 return 0;
135 }
136
137 /* Apply negative unit exponent */
138 for (; unit_exponent < 0; unit_exponent++) {
139 prev = logical_extents;
140 logical_extents *= 10;
141 if (logical_extents < prev)
142 return 0;
143 }
144 /* Apply positive unit exponent */
145 for (; unit_exponent > 0; unit_exponent--) {
146 prev = physical_extents;
147 physical_extents *= 10;
148 if (physical_extents < prev)
149 return 0;
150 }
151
152 /* Calculate resolution */
153 return logical_extents / physical_extents;
154}
155
Chris Bagwell41343612011-10-26 22:32:52 -0700156static int wacom_parse_logical_collection(unsigned char *report,
157 struct wacom_features *features)
158{
159 int length = 0;
160
161 if (features->type == BAMBOO_PT) {
162
163 /* Logical collection is only used by 3rd gen Bamboo Touch */
164 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
Ping Cheng8b4a0c12012-01-31 00:07:33 -0800165 features->device_type = BTN_TOOL_FINGER;
Chris Bagwell41343612011-10-26 22:32:52 -0700166
Chris Bagwell41343612011-10-26 22:32:52 -0700167 features->x_max = features->y_max =
168 get_unaligned_le16(&report[10]);
169
170 length = 11;
171 }
172 return length;
173}
174
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700175static void wacom_retrieve_report_data(struct hid_device *hdev,
Ping Chengf393ee22012-04-29 21:09:17 -0700176 struct wacom_features *features)
177{
178 int result = 0;
179 unsigned char *rep_data;
180
181 rep_data = kmalloc(2, GFP_KERNEL);
182 if (rep_data) {
183
184 rep_data[0] = 12;
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700185 result = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Cheng61c91dd2012-06-28 16:46:27 -0700186 rep_data[0], rep_data, 2,
Ping Chengf393ee22012-04-29 21:09:17 -0700187 WAC_MSG_RETRIES);
188
189 if (result >= 0 && rep_data[1] > 2)
190 features->touch_max = rep_data[1];
191
192 kfree(rep_data);
193 }
194}
195
Chris Bagwell428f8582011-10-26 22:26:59 -0700196/*
197 * Interface Descriptor of wacom devices can be incomplete and
198 * inconsistent so wacom_features table is used to store stylus
199 * device's packet lengths, various maximum values, and tablet
200 * resolution based on product ID's.
201 *
202 * For devices that contain 2 interfaces, wacom_features table is
203 * inaccurate for the touch interface. Since the Interface Descriptor
204 * for touch interfaces has pretty complete data, this function exists
205 * to query tablet for this missing information instead of hard coding in
206 * an additional table.
207 *
208 * A typical Interface Descriptor for a stylus will contain a
209 * boot mouse application collection that is not of interest and this
210 * function will ignore it.
211 *
212 * It also contains a digitizer application collection that also is not
213 * of interest since any information it contains would be duplicate
214 * of what is in wacom_features. Usually it defines a report of an array
215 * of bytes that could be used as max length of the stylus packet returned.
216 * If it happens to define a Digitizer-Stylus Physical Collection then
217 * the X and Y logical values contain valid data but it is ignored.
218 *
219 * A typical Interface Descriptor for a touch interface will contain a
220 * Digitizer-Finger Physical Collection which will define both logical
221 * X/Y maximum as well as the physical size of tablet. Since touch
222 * interfaces haven't supported pressure or distance, this is enough
223 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700224 *
225 * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
226 * Collection. Instead they define a Logical Collection with a single
227 * Logical Maximum for both X and Y.
Jason Gereckeae584ca2012-04-03 15:50:40 -0700228 *
229 * Intuos5 touch interface does not contain useful data. We deal with
230 * this after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700231 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700232static int wacom_parse_hid(struct hid_device *hdev,
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700233 struct wac_hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800234 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500235{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700236 struct wacom *wacom = hid_get_drvdata(hdev);
237 struct usb_interface *intf = wacom->intf;
Ping Cheng545f4e92008-11-24 11:44:27 -0500238 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800239 char limit = 0;
240 /* result has to be defined as int for some devices */
Ping Cheng1d0d6df2013-11-25 18:43:45 -0800241 int result = 0, touch_max = 0;
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700242 int i = 0, page = 0, finger = 0, pen = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500243 unsigned char *report;
244
245 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
246 if (!report)
247 return -ENOMEM;
248
249 /* retrive report descriptors */
250 do {
251 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
252 USB_REQ_GET_DESCRIPTOR,
253 USB_RECIP_INTERFACE | USB_DIR_IN,
254 HID_DEVICET_REPORT << 8,
255 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
256 report,
257 hid_desc->wDescriptorLength,
258 5000); /* 5 secs */
Ping Chenga417ea42011-08-16 00:17:56 -0700259 } while (result < 0 && limit++ < WAC_MSG_RETRIES);
Ping Cheng545f4e92008-11-24 11:44:27 -0500260
Ping Cheng384318e2009-04-28 07:49:54 -0700261 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500262 if (result < 0)
263 goto out;
264
265 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
266
267 switch (report[i]) {
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700268 case HID_HDESC_USAGE_PAGE:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700269 page = report[i + 1];
270 i++;
Ping Cheng545f4e92008-11-24 11:44:27 -0500271 break;
272
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700273 case HID_HDESC_USAGE:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700274 switch (page << 16 | report[i + 1]) {
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700275 case HID_GD_X:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700276 if (finger) {
277 features->device_type = BTN_TOOL_FINGER;
278 /* touch device at least supports one touch point */
279 touch_max = 1;
280 switch (features->type) {
281 case TABLETPC2FG:
282 features->pktlen = WACOM_PKGLEN_TPC2FG;
283 break;
Ping Cheng3699dd72012-11-03 12:16:13 -0700284
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700285 case MTSCREEN:
286 case WACOM_24HDT:
287 features->pktlen = WACOM_PKGLEN_MTOUCH;
288 break;
Ping Cheng3699dd72012-11-03 12:16:13 -0700289
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700290 case MTTPC:
Jason Gerecked51ddb22014-05-14 17:14:29 -0700291 case MTTPC_B:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700292 features->pktlen = WACOM_PKGLEN_MTTPC;
293 break;
Ping Cheng6afdc282012-11-03 12:16:15 -0700294
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700295 case BAMBOO_PT:
296 features->pktlen = WACOM_PKGLEN_BBTOUCH;
297 break;
Ping Cheng3699dd72012-11-03 12:16:13 -0700298
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700299 default:
300 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
301 break;
302 }
Ping Cheng19635182012-04-29 21:09:18 -0700303
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700304 switch (features->type) {
305 case BAMBOO_PT:
306 features->x_phy =
307 get_unaligned_le16(&report[i + 5]);
308 features->x_max =
309 get_unaligned_le16(&report[i + 8]);
310 i += 15;
311 break;
Ping Cheng3699dd72012-11-03 12:16:13 -0700312
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700313 case WACOM_24HDT:
Ping Cheng545f4e92008-11-24 11:44:27 -0500314 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700315 get_unaligned_le16(&report[i + 3]);
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700316 features->x_phy =
317 get_unaligned_le16(&report[i + 8]);
318 features->unit = report[i - 1];
319 features->unitExpo = report[i - 3];
320 i += 12;
321 break;
322
Jason Gerecked51ddb22014-05-14 17:14:29 -0700323 case MTTPC_B:
324 features->x_max =
325 get_unaligned_le16(&report[i + 3]);
326 features->x_phy =
327 get_unaligned_le16(&report[i + 6]);
328 features->unit = report[i - 5];
329 features->unitExpo = report[i - 3];
330 i += 9;
331 break;
332
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700333 default:
334 features->x_max =
335 get_unaligned_le16(&report[i + 3]);
336 features->x_phy =
337 get_unaligned_le16(&report[i + 6]);
338 features->unit = report[i + 9];
339 features->unitExpo = report[i + 11];
340 i += 12;
341 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500342 }
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700343 } else if (pen) {
344 /* penabled only accepts exact bytes of data */
345 if (features->type >= TABLETPC)
346 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
347 features->device_type = BTN_TOOL_PEN;
348 features->x_max =
349 get_unaligned_le16(&report[i + 3]);
350 i += 4;
Ping Cheng545f4e92008-11-24 11:44:27 -0500351 }
352 break;
353
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700354 case HID_GD_Y:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700355 if (finger) {
356 switch (features->type) {
357 case TABLETPC2FG:
358 case MTSCREEN:
359 case MTTPC:
Ping Chengec67bbe2009-12-15 00:35:24 -0800360 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700361 get_unaligned_le16(&report[i + 3]);
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700362 features->y_phy =
363 get_unaligned_le16(&report[i + 6]);
364 i += 7;
365 break;
366
367 case WACOM_24HDT:
368 features->y_max =
369 get_unaligned_le16(&report[i + 3]);
370 features->y_phy =
371 get_unaligned_le16(&report[i - 2]);
372 i += 7;
373 break;
374
375 case BAMBOO_PT:
376 features->y_phy =
377 get_unaligned_le16(&report[i + 3]);
378 features->y_max =
379 get_unaligned_le16(&report[i + 6]);
380 i += 12;
381 break;
382
Jason Gerecked51ddb22014-05-14 17:14:29 -0700383 case MTTPC_B:
384 features->y_max =
385 get_unaligned_le16(&report[i + 3]);
386 features->y_phy =
387 get_unaligned_le16(&report[i + 6]);
388 i += 9;
389 break;
390
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700391 default:
392 features->y_max =
393 features->x_max;
394 features->y_phy =
395 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800396 i += 4;
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700397 break;
Ping Chengec67bbe2009-12-15 00:35:24 -0800398 }
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700399 } else if (pen) {
400 features->y_max =
401 get_unaligned_le16(&report[i + 3]);
402 i += 4;
Ping Chengec67bbe2009-12-15 00:35:24 -0800403 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500404 break;
405
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700406 case HID_DG_FINGER:
Ping Cheng545f4e92008-11-24 11:44:27 -0500407 finger = 1;
408 i++;
409 break;
410
Chris Bagwell428f8582011-10-26 22:26:59 -0700411 /*
412 * Requiring Stylus Usage will ignore boot mouse
413 * X/Y values and some cases of invalid Digitizer X/Y
414 * values commonly reported.
415 */
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700416 case HID_DG_STYLUS:
Ping Cheng545f4e92008-11-24 11:44:27 -0500417 pen = 1;
418 i++;
419 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700420
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700421 case HID_DG_CONTACTMAX:
Ping Cheng1cecc5c2012-06-28 16:47:30 -0700422 /* leave touch_max as is if predefined */
423 if (!features->touch_max)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700424 wacom_retrieve_report_data(hdev, features);
Ping Chengf393ee22012-04-29 21:09:17 -0700425 i++;
426 break;
Jason Gereckee9fc4132014-04-19 13:49:37 -0700427
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700428 case HID_DG_TIPPRESSURE:
Jason Gereckee9fc4132014-04-19 13:49:37 -0700429 if (pen) {
430 features->pressure_max =
431 get_unaligned_le16(&report[i + 3]);
432 i += 4;
433 }
434 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500435 }
436 break;
437
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700438 case HID_HDESC_COLLECTION_END:
Ping Chengec67bbe2009-12-15 00:35:24 -0800439 /* reset UsagePage and Finger */
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700440 finger = page = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500441 break;
Chris Bagwell41343612011-10-26 22:32:52 -0700442
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700443 case HID_HDESC_COLLECTION:
Chris Bagwell41343612011-10-26 22:32:52 -0700444 i++;
445 switch (report[i]) {
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700446 case HID_HDESC_COLLECTION_LOGICAL:
Chris Bagwell41343612011-10-26 22:32:52 -0700447 i += wacom_parse_logical_collection(&report[i],
448 features);
449 break;
450 }
451 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500452 }
453 }
454
Ping Cheng545f4e92008-11-24 11:44:27 -0500455 out:
Ping Cheng1d0d6df2013-11-25 18:43:45 -0800456 if (!features->touch_max && touch_max)
457 features->touch_max = touch_max;
Ping Cheng384318e2009-04-28 07:49:54 -0700458 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500459 kfree(report);
460 return result;
461}
462
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700463static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
464 int length, int mode)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700465{
466 unsigned char *rep_data;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700467 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700468
Jason Gereckefe494bc2012-10-03 17:25:35 -0700469 rep_data = kzalloc(length, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700470 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800471 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700472
Jason Gereckefe494bc2012-10-03 17:25:35 -0700473 do {
Chris Bagwell9937c022013-01-23 19:37:34 -0800474 rep_data[0] = report_id;
475 rep_data[1] = mode;
476
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700477 error = wacom_set_report(hdev, HID_FEATURE_REPORT,
Jason Gereckefe494bc2012-10-03 17:25:35 -0700478 report_id, rep_data, length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700479 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700480 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700481 report_id, rep_data, length, 1);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700482 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700483
484 kfree(rep_data);
485
486 return error < 0 ? error : 0;
487}
488
Jason Gereckefe494bc2012-10-03 17:25:35 -0700489/*
490 * Switch the tablet into its most-capable mode. Wacom tablets are
491 * typically configured to power-up in a mode which sends mouse-like
492 * reports to the OS. To get absolute position, pressure data, etc.
493 * from the tablet, it is necessary to switch the tablet out of this
494 * mode and into one which sends the full range of tablet data.
495 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700496static int wacom_query_tablet_data(struct hid_device *hdev,
497 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700498{
499 if (features->device_type == BTN_TOOL_FINGER) {
500 if (features->type > TABLETPC) {
501 /* MT Tablet PC touch */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700502 return wacom_set_device_mode(hdev, 3, 4, 4);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700503 }
Jason Gerecke36d3c512013-09-20 09:47:35 -0700504 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700505 return wacom_set_device_mode(hdev, 18, 3, 2);
Jason Gereckeb1e42792012-10-21 00:38:04 -0700506 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700507 } else if (features->device_type == BTN_TOOL_PEN) {
508 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700509 return wacom_set_device_mode(hdev, 2, 2, 2);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700510 }
511 }
512
513 return 0;
514}
515
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700516static int wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700517 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800518{
519 int error = 0;
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700520 struct wacom *wacom = hid_get_drvdata(hdev);
521 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800522 struct usb_host_interface *interface = intf->cur_altsetting;
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700523 struct wac_hid_descriptor *hid_desc;
Ping Chengec67bbe2009-12-15 00:35:24 -0800524
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700525 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800526 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700527 features->x_fuzz = 4;
528 features->y_fuzz = 4;
529 features->pressure_fuzz = 0;
530 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800531
Chris Bagwelld3825d52012-03-25 23:26:11 -0700532 /*
533 * The wireless device HID is basic and layout conflicts with
534 * other tablets (monitor and touch interface can look like pen).
535 * Skip the query for this type and modify defaults based on
536 * interface number.
537 */
538 if (features->type == WIRELESS) {
539 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
540 features->device_type = 0;
541 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
Ping Chengadad0042012-06-28 16:48:17 -0700542 features->device_type = BTN_TOOL_FINGER;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700543 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
544 }
545 }
546
Ping Cheng19635182012-04-29 21:09:18 -0700547 /* only devices that support touch need to retrieve the info */
Ping Chengea2e6022012-06-12 00:14:12 -0700548 if (features->type < BAMBOO_PT) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800549 goto out;
Ping Cheng19635182012-04-29 21:09:18 -0700550 }
Ping Chengec67bbe2009-12-15 00:35:24 -0800551
Dmitry Torokhova882c932012-05-02 00:13:38 -0700552 error = usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc);
553 if (error) {
554 error = usb_get_extra_descriptor(&interface->endpoint[0],
555 HID_DEVICET_REPORT, &hid_desc);
556 if (error) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700557 hid_err(hdev,
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -0700558 "can not retrieve extra class descriptor\n");
Ping Chengec67bbe2009-12-15 00:35:24 -0800559 goto out;
560 }
561 }
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700562 error = wacom_parse_hid(hdev, hid_desc, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800563
Ping Chengec67bbe2009-12-15 00:35:24 -0800564 out:
565 return error;
566}
567
Ping Cheng4492eff2010-03-19 22:18:15 -0700568struct wacom_usbdev_data {
569 struct list_head list;
570 struct kref kref;
571 struct usb_device *dev;
572 struct wacom_shared shared;
573};
574
575static LIST_HEAD(wacom_udev_list);
576static DEFINE_MUTEX(wacom_udev_list_lock);
577
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700578static struct usb_device *wacom_get_sibling(struct usb_device *dev, int vendor, int product)
579{
580 int port1;
581 struct usb_device *sibling;
582
583 if (vendor == 0 && product == 0)
584 return dev;
585
586 if (dev->parent == NULL)
587 return NULL;
588
589 usb_hub_for_each_child(dev->parent, port1, sibling) {
590 struct usb_device_descriptor *d;
591 if (sibling == NULL)
592 continue;
593
594 d = &sibling->descriptor;
595 if (d->idVendor == vendor && d->idProduct == product)
596 return sibling;
597 }
598
599 return NULL;
600}
601
Ping Cheng4492eff2010-03-19 22:18:15 -0700602static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
603{
604 struct wacom_usbdev_data *data;
605
606 list_for_each_entry(data, &wacom_udev_list, list) {
607 if (data->dev == dev) {
608 kref_get(&data->kref);
609 return data;
610 }
611 }
612
613 return NULL;
614}
615
616static int wacom_add_shared_data(struct wacom_wac *wacom,
617 struct usb_device *dev)
618{
619 struct wacom_usbdev_data *data;
620 int retval = 0;
621
622 mutex_lock(&wacom_udev_list_lock);
623
624 data = wacom_get_usbdev_data(dev);
625 if (!data) {
626 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
627 if (!data) {
628 retval = -ENOMEM;
629 goto out;
630 }
631
632 kref_init(&data->kref);
633 data->dev = dev;
634 list_add_tail(&data->list, &wacom_udev_list);
635 }
636
637 wacom->shared = &data->shared;
638
639out:
640 mutex_unlock(&wacom_udev_list_lock);
641 return retval;
642}
643
644static void wacom_release_shared_data(struct kref *kref)
645{
646 struct wacom_usbdev_data *data =
647 container_of(kref, struct wacom_usbdev_data, kref);
648
649 mutex_lock(&wacom_udev_list_lock);
650 list_del(&data->list);
651 mutex_unlock(&wacom_udev_list_lock);
652
653 kfree(data);
654}
655
656static void wacom_remove_shared_data(struct wacom_wac *wacom)
657{
658 struct wacom_usbdev_data *data;
659
660 if (wacom->shared) {
661 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
662 kref_put(&data->kref, wacom_release_shared_data);
663 wacom->shared = NULL;
664 }
665}
666
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700667static int wacom_led_control(struct wacom *wacom)
668{
669 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700670 int retval;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700671
672 buf = kzalloc(9, GFP_KERNEL);
673 if (!buf)
674 return -ENOMEM;
675
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700676 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700677 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700678 /*
679 * Touch Ring and crop mark LED luminance may take on
680 * one of four values:
681 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
682 */
683 int ring_led = wacom->led.select[0] & 0x03;
684 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
685 int crop_lum = 0;
Ping Cheng09e7d942011-10-04 23:51:14 -0700686
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700687 buf[0] = WAC_CMD_LED_CONTROL;
688 buf[1] = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
689 }
690 else {
691 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700692
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700693 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
694 wacom->wacom_wac.features.type == WACOM_24HD)
695 led |= (wacom->led.select[1] << 4) | 0x40;
696
697 buf[0] = WAC_CMD_LED_CONTROL;
698 buf[1] = led;
699 buf[2] = wacom->led.llv;
700 buf[3] = wacom->led.hlv;
701 buf[4] = wacom->led.img_lum;
702 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700703
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700704 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
705 WAC_CMD_LED_CONTROL, buf, 9, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700706 kfree(buf);
707
708 return retval;
709}
710
711static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
712{
713 unsigned char *buf;
714 int i, retval;
715
716 buf = kzalloc(259, GFP_KERNEL);
717 if (!buf)
718 return -ENOMEM;
719
720 /* Send 'start' command */
721 buf[0] = WAC_CMD_ICON_START;
722 buf[1] = 1;
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700723 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
724 WAC_CMD_ICON_START, buf, 2, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700725 if (retval < 0)
726 goto out;
727
728 buf[0] = WAC_CMD_ICON_XFER;
729 buf[1] = button_id & 0x07;
730 for (i = 0; i < 4; i++) {
731 buf[2] = i;
732 memcpy(buf + 3, img + i * 256, 256);
733
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700734 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
735 WAC_CMD_ICON_XFER,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700736 buf, 259, WAC_CMD_RETRIES);
737 if (retval < 0)
738 break;
739 }
740
741 /* Send 'stop' */
742 buf[0] = WAC_CMD_ICON_START;
743 buf[1] = 0;
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700744 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, WAC_CMD_ICON_START,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700745 buf, 2, WAC_CMD_RETRIES);
746
747out:
748 kfree(buf);
749 return retval;
750}
751
Ping Cheng09e7d942011-10-04 23:51:14 -0700752static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700753 const char *buf, size_t count)
754{
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700755 struct hid_device *hdev = dev_get_drvdata(dev);
756 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700757 unsigned int id;
758 int err;
759
760 err = kstrtouint(buf, 10, &id);
761 if (err)
762 return err;
763
764 mutex_lock(&wacom->lock);
765
Ping Cheng09e7d942011-10-04 23:51:14 -0700766 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700767 err = wacom_led_control(wacom);
768
769 mutex_unlock(&wacom->lock);
770
771 return err < 0 ? err : count;
772}
773
Ping Cheng09e7d942011-10-04 23:51:14 -0700774#define DEVICE_LED_SELECT_ATTR(SET_ID) \
775static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
776 struct device_attribute *attr, const char *buf, size_t count) \
777{ \
778 return wacom_led_select_store(dev, SET_ID, buf, count); \
779} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700780static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
781 struct device_attribute *attr, char *buf) \
782{ \
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700783 struct hid_device *hdev = dev_get_drvdata(dev); \
784 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700785 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
786} \
787static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
788 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700789 wacom_led##SET_ID##_select_store)
790
791DEVICE_LED_SELECT_ATTR(0);
792DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700793
794static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
795 const char *buf, size_t count)
796{
797 unsigned int value;
798 int err;
799
800 err = kstrtouint(buf, 10, &value);
801 if (err)
802 return err;
803
804 mutex_lock(&wacom->lock);
805
806 *dest = value & 0x7f;
807 err = wacom_led_control(wacom);
808
809 mutex_unlock(&wacom->lock);
810
811 return err < 0 ? err : count;
812}
813
814#define DEVICE_LUMINANCE_ATTR(name, field) \
815static ssize_t wacom_##name##_luminance_store(struct device *dev, \
816 struct device_attribute *attr, const char *buf, size_t count) \
817{ \
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700818 struct hid_device *hdev = dev_get_drvdata(dev); \
819 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700820 \
821 return wacom_luminance_store(wacom, &wacom->led.field, \
822 buf, count); \
823} \
824static DEVICE_ATTR(name##_luminance, S_IWUSR, \
825 NULL, wacom_##name##_luminance_store)
826
827DEVICE_LUMINANCE_ATTR(status0, llv);
828DEVICE_LUMINANCE_ATTR(status1, hlv);
829DEVICE_LUMINANCE_ATTR(buttons, img_lum);
830
831static ssize_t wacom_button_image_store(struct device *dev, int button_id,
832 const char *buf, size_t count)
833{
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700834 struct hid_device *hdev = dev_get_drvdata(dev);
835 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700836 int err;
837
838 if (count != 1024)
839 return -EINVAL;
840
841 mutex_lock(&wacom->lock);
842
843 err = wacom_led_putimage(wacom, button_id, buf);
844
845 mutex_unlock(&wacom->lock);
846
847 return err < 0 ? err : count;
848}
849
850#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
851static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
852 struct device_attribute *attr, const char *buf, size_t count) \
853{ \
854 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
855} \
856static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
857 NULL, wacom_btnimg##BUTTON_ID##_store)
858
859DEVICE_BTNIMG_ATTR(0);
860DEVICE_BTNIMG_ATTR(1);
861DEVICE_BTNIMG_ATTR(2);
862DEVICE_BTNIMG_ATTR(3);
863DEVICE_BTNIMG_ATTR(4);
864DEVICE_BTNIMG_ATTR(5);
865DEVICE_BTNIMG_ATTR(6);
866DEVICE_BTNIMG_ATTR(7);
867
Ping Cheng09e7d942011-10-04 23:51:14 -0700868static struct attribute *cintiq_led_attrs[] = {
869 &dev_attr_status_led0_select.attr,
870 &dev_attr_status_led1_select.attr,
871 NULL
872};
873
874static struct attribute_group cintiq_led_attr_group = {
875 .name = "wacom_led",
876 .attrs = cintiq_led_attrs,
877};
878
879static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700880 &dev_attr_status0_luminance.attr,
881 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700882 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700883 &dev_attr_buttons_luminance.attr,
884 &dev_attr_button0_rawimg.attr,
885 &dev_attr_button1_rawimg.attr,
886 &dev_attr_button2_rawimg.attr,
887 &dev_attr_button3_rawimg.attr,
888 &dev_attr_button4_rawimg.attr,
889 &dev_attr_button5_rawimg.attr,
890 &dev_attr_button6_rawimg.attr,
891 &dev_attr_button7_rawimg.attr,
892 NULL
893};
894
Ping Cheng09e7d942011-10-04 23:51:14 -0700895static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700896 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700897 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700898};
899
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700900static struct attribute *intuos5_led_attrs[] = {
901 &dev_attr_status0_luminance.attr,
902 &dev_attr_status_led0_select.attr,
903 NULL
904};
905
906static struct attribute_group intuos5_led_attr_group = {
907 .name = "wacom_led",
908 .attrs = intuos5_led_attrs,
909};
910
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700911static int wacom_initialize_leds(struct wacom *wacom)
912{
913 int error;
914
Ping Cheng09e7d942011-10-04 23:51:14 -0700915 /* Initialize default values */
916 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700917 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700918 case INTUOS4:
919 case INTUOS4L:
920 wacom->led.select[0] = 0;
921 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700922 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700923 wacom->led.hlv = 20;
924 wacom->led.img_lum = 10;
Ping Cheng09e7d942011-10-04 23:51:14 -0700925 error = sysfs_create_group(&wacom->intf->dev.kobj,
926 &intuos4_led_attr_group);
927 break;
928
Jason Gerecke246835f2011-12-12 00:12:04 -0800929 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700930 case WACOM_21UX2:
931 wacom->led.select[0] = 0;
932 wacom->led.select[1] = 0;
933 wacom->led.llv = 0;
934 wacom->led.hlv = 0;
935 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700936
937 error = sysfs_create_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700938 &cintiq_led_attr_group);
939 break;
940
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700941 case INTUOS5S:
942 case INTUOS5:
943 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700944 case INTUOSPS:
945 case INTUOSPM:
946 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700947 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
948 wacom->led.select[0] = 0;
949 wacom->led.select[1] = 0;
950 wacom->led.llv = 32;
951 wacom->led.hlv = 0;
952 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700953
Ping Chengc2b0c272013-09-20 09:50:14 -0700954 error = sysfs_create_group(&wacom->intf->dev.kobj,
955 &intuos5_led_attr_group);
956 } else
957 return 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700958 break;
959
Ping Cheng09e7d942011-10-04 23:51:14 -0700960 default:
961 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700962 }
963
Ping Cheng09e7d942011-10-04 23:51:14 -0700964 if (error) {
965 dev_err(&wacom->intf->dev,
966 "cannot create sysfs group err: %d\n", error);
967 return error;
968 }
969 wacom_led_control(wacom);
970
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700971 return 0;
972}
973
974static void wacom_destroy_leds(struct wacom *wacom)
975{
Ping Cheng09e7d942011-10-04 23:51:14 -0700976 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:
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700980 sysfs_remove_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700981 &intuos4_led_attr_group);
982 break;
983
Jason Gerecke246835f2011-12-12 00:12:04 -0800984 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700985 case WACOM_21UX2:
986 sysfs_remove_group(&wacom->intf->dev.kobj,
987 &cintiq_led_attr_group);
988 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700989
990 case INTUOS5S:
991 case INTUOS5:
992 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700993 case INTUOSPS:
994 case INTUOSPM:
995 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700996 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
997 sysfs_remove_group(&wacom->intf->dev.kobj,
998 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700999 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001000 }
1001}
1002
Chris Bagwella1d552c2012-03-25 23:26:30 -07001003static enum power_supply_property wacom_battery_props[] = {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001004 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001005 POWER_SUPPLY_PROP_CAPACITY
1006};
1007
1008static int wacom_battery_get_property(struct power_supply *psy,
1009 enum power_supply_property psp,
1010 union power_supply_propval *val)
1011{
1012 struct wacom *wacom = container_of(psy, struct wacom, battery);
1013 int ret = 0;
1014
1015 switch (psp) {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001016 case POWER_SUPPLY_PROP_SCOPE:
1017 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1018 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001019 case POWER_SUPPLY_PROP_CAPACITY:
1020 val->intval =
1021 wacom->wacom_wac.battery_capacity * 100 / 31;
1022 break;
1023 default:
1024 ret = -EINVAL;
1025 break;
1026 }
1027
1028 return ret;
1029}
1030
1031static int wacom_initialize_battery(struct wacom *wacom)
1032{
1033 int error = 0;
1034
1035 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) {
1036 wacom->battery.properties = wacom_battery_props;
1037 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
1038 wacom->battery.get_property = wacom_battery_get_property;
1039 wacom->battery.name = "wacom_battery";
1040 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1041 wacom->battery.use_for_apm = 0;
1042
1043 error = power_supply_register(&wacom->usbdev->dev,
1044 &wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001045
1046 if (!error)
1047 power_supply_powers(&wacom->battery,
1048 &wacom->usbdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001049 }
1050
1051 return error;
1052}
1053
1054static void wacom_destroy_battery(struct wacom *wacom)
1055{
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001056 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR &&
1057 wacom->battery.dev) {
Chris Bagwella1d552c2012-03-25 23:26:30 -07001058 power_supply_unregister(&wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001059 wacom->battery.dev = NULL;
1060 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001061}
1062
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001063static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001064{
1065 struct input_dev *input_dev;
1066 struct usb_interface *intf = wacom->intf;
1067 struct usb_device *dev = interface_to_usbdev(intf);
1068 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001069
1070 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001071 if (!input_dev)
1072 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001073
1074 input_dev->name = wacom_wac->name;
Benjamin Tissoires7097d4c2014-07-24 12:48:06 -07001075 input_dev->phys = wacom->phys;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001076 input_dev->dev.parent = &intf->dev;
1077 input_dev->open = wacom_open;
1078 input_dev->close = wacom_close;
1079 usb_to_input_id(dev, &input_dev->id);
1080 input_set_drvdata(input_dev, wacom);
1081
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001082 return input_dev;
1083}
1084
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001085static void wacom_unregister_inputs(struct wacom *wacom)
1086{
1087 if (wacom->wacom_wac.input)
1088 input_unregister_device(wacom->wacom_wac.input);
1089 if (wacom->wacom_wac.pad_input)
1090 input_unregister_device(wacom->wacom_wac.pad_input);
1091 wacom->wacom_wac.input = NULL;
1092 wacom->wacom_wac.pad_input = NULL;
1093}
1094
1095static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001096{
1097 struct input_dev *input_dev, *pad_input_dev;
1098 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1099 int error;
1100
1101 input_dev = wacom_allocate_input(wacom);
1102 pad_input_dev = wacom_allocate_input(wacom);
1103 if (!input_dev || !pad_input_dev) {
1104 error = -ENOMEM;
1105 goto fail1;
1106 }
1107
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001108 wacom_wac->input = input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001109 wacom_wac->pad_input = pad_input_dev;
1110 wacom_wac->pad_input->name = wacom_wac->pad_name;
1111
Ping Cheng19635182012-04-29 21:09:18 -07001112 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1113 if (error)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001114 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001115
1116 error = input_register_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001117 if (error)
1118 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001119
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001120 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1121 if (error) {
1122 /* no pad in use on this interface */
1123 input_free_device(pad_input_dev);
1124 wacom_wac->pad_input = NULL;
1125 pad_input_dev = NULL;
1126 } else {
1127 error = input_register_device(pad_input_dev);
1128 if (error)
1129 goto fail3;
1130 }
1131
Ping Cheng19635182012-04-29 21:09:18 -07001132 return 0;
1133
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001134fail3:
1135 input_unregister_device(input_dev);
1136 input_dev = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001137fail2:
Ping Cheng19635182012-04-29 21:09:18 -07001138 wacom_wac->input = NULL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001139 wacom_wac->pad_input = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001140fail1:
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001141 if (input_dev)
1142 input_free_device(input_dev);
1143 if (pad_input_dev)
1144 input_free_device(pad_input_dev);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001145 return error;
1146}
1147
Chris Bagwell16bf2882012-03-25 23:26:20 -07001148static void wacom_wireless_work(struct work_struct *work)
1149{
1150 struct wacom *wacom = container_of(work, struct wacom, work);
1151 struct usb_device *usbdev = wacom->usbdev;
1152 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001153 struct hid_device *hdev1, *hdev2;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001154 struct wacom *wacom1, *wacom2;
1155 struct wacom_wac *wacom_wac1, *wacom_wac2;
1156 int error;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001157
1158 /*
1159 * Regardless if this is a disconnect or a new tablet,
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001160 * remove any existing input and battery devices.
Chris Bagwell16bf2882012-03-25 23:26:20 -07001161 */
1162
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001163 wacom_destroy_battery(wacom);
1164
Chris Bagwell16bf2882012-03-25 23:26:20 -07001165 /* Stylus interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001166 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1167 wacom1 = hid_get_drvdata(hdev1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001168 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001169 wacom_unregister_inputs(wacom1);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001170
1171 /* Touch interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001172 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1173 wacom2 = hid_get_drvdata(hdev2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001174 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001175 wacom_unregister_inputs(wacom2);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001176
1177 if (wacom_wac->pid == 0) {
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001178 dev_info(&wacom->intf->dev, "wireless tablet disconnected\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001179 } else {
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001180 const struct hid_device_id *id = wacom_ids;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001181
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001182 dev_info(&wacom->intf->dev,
1183 "wireless tablet connected with PID %x\n",
1184 wacom_wac->pid);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001185
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001186 while (id->bus) {
1187 if (id->vendor == USB_VENDOR_ID_WACOM &&
1188 id->product == wacom_wac->pid)
Chris Bagwell16bf2882012-03-25 23:26:20 -07001189 break;
1190 id++;
1191 }
1192
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001193 if (!id->bus) {
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001194 dev_info(&wacom->intf->dev,
1195 "ignoring unknown PID.\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001196 return;
1197 }
1198
1199 /* Stylus interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001200 wacom_wac1->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001201 *((struct wacom_features *)id->driver_data);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001202 wacom_wac1->features.device_type = BTN_TOOL_PEN;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001203 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1204 wacom_wac1->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001205 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1206 wacom_wac1->features.name);
Ping Cheng961794a2013-12-05 12:54:53 -08001207 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1208 wacom_wac1->shared->type = wacom_wac1->features.type;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001209 error = wacom_register_inputs(wacom1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001210 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001211 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001212
1213 /* Touch interface */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001214 if (wacom_wac1->features.touch_max ||
1215 wacom_wac1->features.type == INTUOSHT) {
Ping Cheng57bcfce2013-10-15 23:44:00 -07001216 wacom_wac2->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001217 *((struct wacom_features *)id->driver_data);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001218 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1219 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1220 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1221 if (wacom_wac2->features.touch_max)
1222 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1223 "%s (WL) Finger",wacom_wac2->features.name);
1224 else
1225 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1226 "%s (WL) Pad",wacom_wac2->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001227 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1228 "%s (WL) Pad", wacom_wac2->features.name);
1229 error = wacom_register_inputs(wacom2);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001230 if (error)
1231 goto fail;
Ping Cheng961794a2013-12-05 12:54:53 -08001232
1233 if (wacom_wac1->features.type == INTUOSHT &&
1234 wacom_wac1->features.touch_max)
1235 wacom_wac->shared->touch_input = wacom_wac2->input;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001236 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001237
1238 error = wacom_initialize_battery(wacom);
1239 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001240 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001241 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001242
1243 return;
1244
Ping Cheng57bcfce2013-10-15 23:44:00 -07001245fail:
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001246 wacom_unregister_inputs(wacom1);
1247 wacom_unregister_inputs(wacom2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001248 return;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001249}
1250
Ping Cheng401d7d12013-07-28 00:38:54 -07001251/*
1252 * Not all devices report physical dimensions from HID.
1253 * Compute the default from hardcoded logical dimension
1254 * and resolution before driver overwrites them.
1255 */
1256static void wacom_set_default_phy(struct wacom_features *features)
1257{
1258 if (features->x_resolution) {
1259 features->x_phy = (features->x_max * 100) /
1260 features->x_resolution;
1261 features->y_phy = (features->y_max * 100) /
1262 features->y_resolution;
1263 }
1264}
1265
1266static void wacom_calculate_res(struct wacom_features *features)
1267{
1268 features->x_resolution = wacom_calc_hid_res(features->x_max,
1269 features->x_phy,
1270 features->unit,
1271 features->unitExpo);
1272 features->y_resolution = wacom_calc_hid_res(features->y_max,
1273 features->y_phy,
1274 features->unit,
1275 features->unitExpo);
1276}
1277
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001278static int wacom_probe(struct hid_device *hdev,
1279 const struct hid_device_id *id)
Ping Cheng3bea7332006-07-13 18:01:36 -07001280{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001281 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Ping Cheng3bea7332006-07-13 18:01:36 -07001282 struct usb_device *dev = interface_to_usbdev(intf);
1283 struct usb_endpoint_descriptor *endpoint;
1284 struct wacom *wacom;
1285 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001286 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001287 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001288
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001289 if (!id->driver_data)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001290 return -EINVAL;
1291
Ping Cheng3bea7332006-07-13 18:01:36 -07001292 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001293 if (!wacom)
1294 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001295
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001296 hid_set_drvdata(hdev, wacom);
1297 wacom->hdev = hdev;
1298
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001299 wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001300 wacom_wac->features = *((struct wacom_features *)id->driver_data);
Jason Childse33da8a2010-02-17 22:38:31 -08001301 features = &wacom_wac->features;
1302 if (features->pktlen > WACOM_PKGLEN_MAX) {
1303 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001304 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -08001305 }
1306
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001307 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1308 error = -ENODEV;
Jason Childse33da8a2010-02-17 22:38:31 -08001309 goto fail1;
1310 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001311
Ping Cheng3bea7332006-07-13 18:01:36 -07001312 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001313 wacom->intf = intf;
1314 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001315 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001316 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
1317 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
1318
Ping Cheng545f4e92008-11-24 11:44:27 -05001319 endpoint = &intf->cur_altsetting->endpoint[0].desc;
1320
Ping Cheng401d7d12013-07-28 00:38:54 -07001321 /* set the default size in case we do not get them from hid */
1322 wacom_set_default_phy(features);
1323
Ping Chengf393ee22012-04-29 21:09:17 -07001324 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001325 error = wacom_retrieve_hid_descriptor(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -08001326 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001327 goto fail1;
Ping Cheng545f4e92008-11-24 11:44:27 -05001328
Jason Gereckeae584ca2012-04-03 15:50:40 -07001329 /*
1330 * Intuos5 has no useful data about its touch interface in its
1331 * HID descriptor. If this is the touch interface (wMaxPacketSize
1332 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1333 */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001334 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
Jason Gereckeae584ca2012-04-03 15:50:40 -07001335 if (endpoint->wMaxPacketSize == WACOM_PKGLEN_BBTOUCH3) {
1336 features->device_type = BTN_TOOL_FINGER;
1337 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
1338
Jason Gereckeae584ca2012-04-03 15:50:40 -07001339 features->x_max = 4096;
1340 features->y_max = 4096;
1341 } else {
1342 features->device_type = BTN_TOOL_PEN;
1343 }
1344 }
1345
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001346 wacom_setup_device_quirks(features);
1347
Ping Cheng401d7d12013-07-28 00:38:54 -07001348 /* set unit to "100th of a mm" for devices not reported by HID */
1349 if (!features->unit) {
1350 features->unit = 0x11;
1351 features->unitExpo = 16 - 3;
1352 }
1353 wacom_calculate_res(features);
1354
Ping Cheng49b764a2010-02-20 00:53:49 -08001355 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001356 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1357 "%s Pad", features->name);
Ping Cheng49b764a2010-02-20 00:53:49 -08001358
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001359 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Jason Gereckeaea2bf62012-10-21 00:38:03 -07001360 struct usb_device *other_dev;
1361
Ping Cheng49b764a2010-02-20 00:53:49 -08001362 /* Append the device type to the name */
Ping Cheng57bcfce2013-10-15 23:44:00 -07001363 if (features->device_type != BTN_TOOL_FINGER)
1364 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1365 else if (features->touch_max)
1366 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1367 else
1368 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
Ping Cheng4492eff2010-03-19 22:18:15 -07001369
Jason Gereckeaea2bf62012-10-21 00:38:03 -07001370 other_dev = wacom_get_sibling(dev, features->oVid, features->oPid);
1371 if (other_dev == NULL || wacom_get_usbdev_data(other_dev) == NULL)
1372 other_dev = dev;
1373 error = wacom_add_shared_data(wacom_wac, other_dev);
Ping Cheng4492eff2010-03-19 22:18:15 -07001374 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001375 goto fail1;
Ping Cheng49b764a2010-02-20 00:53:49 -08001376 }
1377
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001378 error = wacom_initialize_leds(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001379 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001380 goto fail2;
Ping Cheng3bea7332006-07-13 18:01:36 -07001381
Chris Bagwelld3825d52012-03-25 23:26:11 -07001382 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001383 error = wacom_register_inputs(wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001384 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001385 goto fail3;
Chris Bagwelld3825d52012-03-25 23:26:11 -07001386 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001387
Ping Chengec67bbe2009-12-15 00:35:24 -08001388 /* Note that if query fails it is not a hard failure */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001389 wacom_query_tablet_data(hdev, features);
Ping Cheng3bea7332006-07-13 18:01:36 -07001390
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001391 /* Regular HID work starts now */
1392 error = hid_parse(hdev);
1393 if (error) {
1394 hid_err(hdev, "parse failed\n");
1395 goto fail4;
Chris Bagwelld3825d52012-03-25 23:26:11 -07001396 }
Ping Cheng961794a2013-12-05 12:54:53 -08001397
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001398 error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1399 if (error) {
1400 hid_err(hdev, "hw start failed\n");
1401 goto fail4;
1402 }
1403
1404 if (features->quirks & WACOM_QUIRK_MONITOR)
1405 error = hid_hw_open(hdev);
1406
Ping Cheng961794a2013-12-05 12:54:53 -08001407 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1408 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1409 wacom_wac->shared->touch_input = wacom_wac->input;
1410 }
1411
Ping Cheng3bea7332006-07-13 18:01:36 -07001412 return 0;
1413
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001414 fail4: wacom_unregister_inputs(wacom);
1415 fail3: wacom_destroy_leds(wacom);
1416 fail2: wacom_remove_shared_data(wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001417 fail1: kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001418 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001419 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001420}
1421
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001422static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001423{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001424 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001425
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001426 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001427
Chris Bagwell16bf2882012-03-25 23:26:20 -07001428 cancel_work_sync(&wacom->work);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001429 wacom_unregister_inputs(wacom);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001430 wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001431 wacom_destroy_leds(wacom);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001432 wacom_remove_shared_data(&wacom->wacom_wac);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001433
1434 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001435 kfree(wacom);
1436}
1437
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001438static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001439{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001440 struct wacom *wacom = hid_get_drvdata(hdev);
1441 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001442
1443 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001444
1445 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001446 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001447 wacom_led_control(wacom);
1448
Oliver Neukume7224092008-04-15 01:31:57 -04001449 mutex_unlock(&wacom->lock);
1450
1451 return 0;
1452}
1453
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001454static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001455{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001456 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001457}
1458
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001459static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001460 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001461 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001462 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001463 .remove = wacom_remove,
1464#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001465 .resume = wacom_resume,
1466 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001467#endif
1468 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001469};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001470module_hid_driver(wacom_driver);