blob: ce76e1ef2dbc53e710a2c52ac0b74c3de9bcec23 [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
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -070018#define HID_HDESC_USAGE_UNDEFINED 0x00
19#define HID_HDESC_USAGE_PAGE 0x05
20#define HID_HDESC_USAGE 0x09
21#define HID_HDESC_COLLECTION 0xa1
22#define HID_HDESC_COLLECTION_LOGICAL 0x02
23#define HID_HDESC_COLLECTION_END 0xc0
Ping Cheng545f4e92008-11-24 11:44:27 -050024
Ping Chenga417ea42011-08-16 00:17:56 -070025#define WAC_MSG_RETRIES 5
Ping Cheng3bea7332006-07-13 18:01:36 -070026
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070027#define WAC_CMD_LED_CONTROL 0x20
28#define WAC_CMD_ICON_START 0x21
29#define WAC_CMD_ICON_XFER 0x23
30#define WAC_CMD_RETRIES 10
31
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070032static int wacom_get_report(struct hid_device *hdev, u8 type, u8 id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070033 void *buf, size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070034{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070035 int retval;
36
37 do {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070038 retval = hid_hw_raw_request(hdev, id, buf, size, type,
39 HID_REQ_GET_REPORT);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070040 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
41
42 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070043}
44
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070045static int wacom_set_report(struct hid_device *hdev, u8 type, u8 id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070046 void *buf, size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070047{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070048 int retval;
49
50 do {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070051 retval = hid_hw_raw_request(hdev, id, buf, size, type,
52 HID_REQ_SET_REPORT);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070053 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
54
55 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070056}
57
Benjamin Tissoires29b47392014-07-24 12:52:23 -070058static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
59 u8 *raw_data, int size)
Ping Cheng3bea7332006-07-13 18:01:36 -070060{
Benjamin Tissoires29b47392014-07-24 12:52:23 -070061 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070062
Benjamin Tissoires29b47392014-07-24 12:52:23 -070063 if (size > WACOM_PKGLEN_MAX)
64 return 1;
Ping Cheng3bea7332006-07-13 18:01:36 -070065
Benjamin Tissoires29b47392014-07-24 12:52:23 -070066 memcpy(wacom->wacom_wac.data, raw_data, size);
Ping Cheng3bea7332006-07-13 18:01:36 -070067
Benjamin Tissoires29b47392014-07-24 12:52:23 -070068 wacom_wac_irq(&wacom->wacom_wac, size);
69
70 return 0;
Ping Cheng3bea7332006-07-13 18:01:36 -070071}
72
Ping Cheng3bea7332006-07-13 18:01:36 -070073static int wacom_open(struct input_dev *dev)
74{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040075 struct wacom *wacom = input_get_drvdata(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070076 int retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070077
Oliver Neukume7224092008-04-15 01:31:57 -040078 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070079 retval = hid_hw_open(wacom->hdev);
Oliver Neukume7224092008-04-15 01:31:57 -040080 mutex_unlock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070081
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -070082 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070083}
84
85static void wacom_close(struct input_dev *dev)
86{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040087 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -070088
Oliver Neukume7224092008-04-15 01:31:57 -040089 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070090 hid_hw_close(wacom->hdev);
Oliver Neukume7224092008-04-15 01:31:57 -040091 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -070092}
93
Chris Bagwell16bf2882012-03-25 23:26:20 -070094/*
Jason Gerecke115d5e12012-10-03 17:24:32 -070095 * Calculate the resolution of the X or Y axis, given appropriate HID data.
96 * This function is little more than hidinput_calc_abs_res stripped down.
97 */
98static int wacom_calc_hid_res(int logical_extents, int physical_extents,
99 unsigned char unit, unsigned char exponent)
100{
101 int prev, unit_exponent;
102
103 /* Check if the extents are sane */
104 if (logical_extents <= 0 || physical_extents <= 0)
105 return 0;
106
107 /* Get signed value of nybble-sized twos-compliment exponent */
108 unit_exponent = exponent;
109 if (unit_exponent > 7)
110 unit_exponent -= 16;
111
112 /* Convert physical_extents to millimeters */
113 if (unit == 0x11) { /* If centimeters */
114 unit_exponent += 1;
115 } else if (unit == 0x13) { /* If inches */
116 prev = physical_extents;
117 physical_extents *= 254;
118 if (physical_extents < prev)
119 return 0;
120 unit_exponent -= 1;
121 } else {
122 return 0;
123 }
124
125 /* Apply negative unit exponent */
126 for (; unit_exponent < 0; unit_exponent++) {
127 prev = logical_extents;
128 logical_extents *= 10;
129 if (logical_extents < prev)
130 return 0;
131 }
132 /* Apply positive unit exponent */
133 for (; unit_exponent > 0; unit_exponent--) {
134 prev = physical_extents;
135 physical_extents *= 10;
136 if (physical_extents < prev)
137 return 0;
138 }
139
140 /* Calculate resolution */
141 return logical_extents / physical_extents;
142}
143
Chris Bagwell41343612011-10-26 22:32:52 -0700144static int wacom_parse_logical_collection(unsigned char *report,
145 struct wacom_features *features)
146{
147 int length = 0;
148
149 if (features->type == BAMBOO_PT) {
150
151 /* Logical collection is only used by 3rd gen Bamboo Touch */
Ping Cheng8b4a0c1f2012-01-31 00:07:33 -0800152 features->device_type = BTN_TOOL_FINGER;
Chris Bagwell41343612011-10-26 22:32:52 -0700153
Chris Bagwell41343612011-10-26 22:32:52 -0700154 features->x_max = features->y_max =
155 get_unaligned_le16(&report[10]);
156
157 length = 11;
158 }
159 return length;
160}
161
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700162static void wacom_retrieve_report_data(struct hid_device *hdev,
Ping Chengf393ee22012-04-29 21:09:17 -0700163 struct wacom_features *features)
164{
165 int result = 0;
166 unsigned char *rep_data;
167
168 rep_data = kmalloc(2, GFP_KERNEL);
169 if (rep_data) {
170
171 rep_data[0] = 12;
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700172 result = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Cheng61c91dd2012-06-28 16:46:27 -0700173 rep_data[0], rep_data, 2,
Ping Chengf393ee22012-04-29 21:09:17 -0700174 WAC_MSG_RETRIES);
175
176 if (result >= 0 && rep_data[1] > 2)
177 features->touch_max = rep_data[1];
178
179 kfree(rep_data);
180 }
181}
182
Chris Bagwell428f8582011-10-26 22:26:59 -0700183/*
184 * Interface Descriptor of wacom devices can be incomplete and
185 * inconsistent so wacom_features table is used to store stylus
186 * device's packet lengths, various maximum values, and tablet
187 * resolution based on product ID's.
188 *
189 * For devices that contain 2 interfaces, wacom_features table is
190 * inaccurate for the touch interface. Since the Interface Descriptor
191 * for touch interfaces has pretty complete data, this function exists
192 * to query tablet for this missing information instead of hard coding in
193 * an additional table.
194 *
195 * A typical Interface Descriptor for a stylus will contain a
196 * boot mouse application collection that is not of interest and this
197 * function will ignore it.
198 *
199 * It also contains a digitizer application collection that also is not
200 * of interest since any information it contains would be duplicate
201 * of what is in wacom_features. Usually it defines a report of an array
202 * of bytes that could be used as max length of the stylus packet returned.
203 * If it happens to define a Digitizer-Stylus Physical Collection then
204 * the X and Y logical values contain valid data but it is ignored.
205 *
206 * A typical Interface Descriptor for a touch interface will contain a
207 * Digitizer-Finger Physical Collection which will define both logical
208 * X/Y maximum as well as the physical size of tablet. Since touch
209 * interfaces haven't supported pressure or distance, this is enough
210 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700211 *
212 * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
213 * Collection. Instead they define a Logical Collection with a single
214 * Logical Maximum for both X and Y.
Jason Gereckeae584ca2012-04-03 15:50:40 -0700215 *
216 * Intuos5 touch interface does not contain useful data. We deal with
217 * this after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700218 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700219static int wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800220 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500221{
Ping Chengec67bbe2009-12-15 00:35:24 -0800222 /* result has to be defined as int for some devices */
Ping Cheng1d0d6df2013-11-25 18:43:45 -0800223 int result = 0, touch_max = 0;
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700224 int i = 0, page = 0, finger = 0, pen = 0;
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -0700225 unsigned char *report = hdev->rdesc;
Ping Cheng545f4e92008-11-24 11:44:27 -0500226
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -0700227 for (i = 0; i < hdev->rsize; i++) {
Ping Cheng545f4e92008-11-24 11:44:27 -0500228
229 switch (report[i]) {
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700230 case HID_HDESC_USAGE_PAGE:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700231 page = report[i + 1];
232 i++;
Ping Cheng545f4e92008-11-24 11:44:27 -0500233 break;
234
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700235 case HID_HDESC_USAGE:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700236 switch (page << 16 | report[i + 1]) {
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700237 case HID_GD_X:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700238 if (finger) {
239 features->device_type = BTN_TOOL_FINGER;
240 /* touch device at least supports one touch point */
241 touch_max = 1;
Ping Cheng19635182012-04-29 21:09:18 -0700242
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700243 switch (features->type) {
244 case BAMBOO_PT:
245 features->x_phy =
246 get_unaligned_le16(&report[i + 5]);
247 features->x_max =
248 get_unaligned_le16(&report[i + 8]);
249 i += 15;
250 break;
Ping Cheng3699dd72012-11-03 12:16:13 -0700251
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700252 case WACOM_24HDT:
Ping Cheng545f4e92008-11-24 11:44:27 -0500253 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700254 get_unaligned_le16(&report[i + 3]);
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700255 features->x_phy =
256 get_unaligned_le16(&report[i + 8]);
257 features->unit = report[i - 1];
258 features->unitExpo = report[i - 3];
259 i += 12;
260 break;
261
Jason Gerecked51ddb22014-05-14 17:14:29 -0700262 case MTTPC_B:
263 features->x_max =
264 get_unaligned_le16(&report[i + 3]);
265 features->x_phy =
266 get_unaligned_le16(&report[i + 6]);
267 features->unit = report[i - 5];
268 features->unitExpo = report[i - 3];
269 i += 9;
270 break;
271
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700272 default:
273 features->x_max =
274 get_unaligned_le16(&report[i + 3]);
275 features->x_phy =
276 get_unaligned_le16(&report[i + 6]);
277 features->unit = report[i + 9];
278 features->unitExpo = report[i + 11];
279 i += 12;
280 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500281 }
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700282 } else if (pen) {
283 /* penabled only accepts exact bytes of data */
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700284 features->device_type = BTN_TOOL_PEN;
285 features->x_max =
286 get_unaligned_le16(&report[i + 3]);
287 i += 4;
Ping Cheng545f4e92008-11-24 11:44:27 -0500288 }
289 break;
290
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700291 case HID_GD_Y:
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700292 if (finger) {
293 switch (features->type) {
294 case TABLETPC2FG:
295 case MTSCREEN:
296 case MTTPC:
Ping Chengec67bbe2009-12-15 00:35:24 -0800297 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700298 get_unaligned_le16(&report[i + 3]);
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700299 features->y_phy =
300 get_unaligned_le16(&report[i + 6]);
301 i += 7;
302 break;
303
304 case WACOM_24HDT:
305 features->y_max =
306 get_unaligned_le16(&report[i + 3]);
307 features->y_phy =
308 get_unaligned_le16(&report[i - 2]);
309 i += 7;
310 break;
311
312 case BAMBOO_PT:
313 features->y_phy =
314 get_unaligned_le16(&report[i + 3]);
315 features->y_max =
316 get_unaligned_le16(&report[i + 6]);
317 i += 12;
318 break;
319
Jason Gerecked51ddb22014-05-14 17:14:29 -0700320 case MTTPC_B:
321 features->y_max =
322 get_unaligned_le16(&report[i + 3]);
323 features->y_phy =
324 get_unaligned_le16(&report[i + 6]);
325 i += 9;
326 break;
327
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700328 default:
329 features->y_max =
330 features->x_max;
331 features->y_phy =
332 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800333 i += 4;
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700334 break;
Ping Chengec67bbe2009-12-15 00:35:24 -0800335 }
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700336 } else if (pen) {
337 features->y_max =
338 get_unaligned_le16(&report[i + 3]);
339 i += 4;
Ping Chengec67bbe2009-12-15 00:35:24 -0800340 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500341 break;
342
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700343 case HID_DG_FINGER:
Ping Cheng545f4e92008-11-24 11:44:27 -0500344 finger = 1;
345 i++;
346 break;
347
Chris Bagwell428f8582011-10-26 22:26:59 -0700348 /*
349 * Requiring Stylus Usage will ignore boot mouse
350 * X/Y values and some cases of invalid Digitizer X/Y
351 * values commonly reported.
352 */
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700353 case HID_DG_STYLUS:
Ping Cheng545f4e92008-11-24 11:44:27 -0500354 pen = 1;
355 i++;
356 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700357
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700358 case HID_DG_CONTACTMAX:
Ping Cheng1cecc5c2012-06-28 16:47:30 -0700359 /* leave touch_max as is if predefined */
360 if (!features->touch_max)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700361 wacom_retrieve_report_data(hdev, features);
Ping Chengf393ee22012-04-29 21:09:17 -0700362 i++;
363 break;
Jason Gereckee9fc4132014-04-19 13:49:37 -0700364
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700365 case HID_DG_TIPPRESSURE:
Jason Gereckee9fc4132014-04-19 13:49:37 -0700366 if (pen) {
367 features->pressure_max =
368 get_unaligned_le16(&report[i + 3]);
369 i += 4;
370 }
371 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500372 }
373 break;
374
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700375 case HID_HDESC_COLLECTION_END:
Ping Chengec67bbe2009-12-15 00:35:24 -0800376 /* reset UsagePage and Finger */
Jason Gerecke5866d9e2014-04-19 13:46:40 -0700377 finger = page = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500378 break;
Chris Bagwell41343612011-10-26 22:32:52 -0700379
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700380 case HID_HDESC_COLLECTION:
Chris Bagwell41343612011-10-26 22:32:52 -0700381 i++;
382 switch (report[i]) {
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -0700383 case HID_HDESC_COLLECTION_LOGICAL:
Chris Bagwell41343612011-10-26 22:32:52 -0700384 i += wacom_parse_logical_collection(&report[i],
385 features);
386 break;
387 }
388 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500389 }
390 }
391
Ping Cheng1d0d6df2013-11-25 18:43:45 -0800392 if (!features->touch_max && touch_max)
393 features->touch_max = touch_max;
Ping Cheng384318e2009-04-28 07:49:54 -0700394 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500395 return result;
396}
397
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700398static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
399 int length, int mode)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700400{
401 unsigned char *rep_data;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700402 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700403
Jason Gereckefe494bc2012-10-03 17:25:35 -0700404 rep_data = kzalloc(length, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700405 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800406 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700407
Jason Gereckefe494bc2012-10-03 17:25:35 -0700408 do {
Chris Bagwell9937c022013-01-23 19:37:34 -0800409 rep_data[0] = report_id;
410 rep_data[1] = mode;
411
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700412 error = wacom_set_report(hdev, HID_FEATURE_REPORT,
Jason Gereckefe494bc2012-10-03 17:25:35 -0700413 report_id, rep_data, length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700414 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700415 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700416 report_id, rep_data, length, 1);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700417 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700418
419 kfree(rep_data);
420
421 return error < 0 ? error : 0;
422}
423
Jason Gereckefe494bc2012-10-03 17:25:35 -0700424/*
425 * Switch the tablet into its most-capable mode. Wacom tablets are
426 * typically configured to power-up in a mode which sends mouse-like
427 * reports to the OS. To get absolute position, pressure data, etc.
428 * from the tablet, it is necessary to switch the tablet out of this
429 * mode and into one which sends the full range of tablet data.
430 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700431static int wacom_query_tablet_data(struct hid_device *hdev,
432 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700433{
434 if (features->device_type == BTN_TOOL_FINGER) {
435 if (features->type > TABLETPC) {
436 /* MT Tablet PC touch */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700437 return wacom_set_device_mode(hdev, 3, 4, 4);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700438 }
Jason Gerecke36d3c512013-09-20 09:47:35 -0700439 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700440 return wacom_set_device_mode(hdev, 18, 3, 2);
Jason Gereckeb1e42792012-10-21 00:38:04 -0700441 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700442 } else if (features->device_type == BTN_TOOL_PEN) {
443 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700444 return wacom_set_device_mode(hdev, 2, 2, 2);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700445 }
446 }
447
448 return 0;
449}
450
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700451static int wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700452 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800453{
454 int error = 0;
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700455 struct wacom *wacom = hid_get_drvdata(hdev);
456 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800457
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700458 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800459 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700460 features->x_fuzz = 4;
461 features->y_fuzz = 4;
462 features->pressure_fuzz = 0;
463 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800464
Chris Bagwelld3825d52012-03-25 23:26:11 -0700465 /*
466 * The wireless device HID is basic and layout conflicts with
467 * other tablets (monitor and touch interface can look like pen).
468 * Skip the query for this type and modify defaults based on
469 * interface number.
470 */
471 if (features->type == WIRELESS) {
472 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
473 features->device_type = 0;
474 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
Ping Chengadad0042012-06-28 16:48:17 -0700475 features->device_type = BTN_TOOL_FINGER;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700476 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
477 }
478 }
479
Ping Cheng19635182012-04-29 21:09:18 -0700480 /* only devices that support touch need to retrieve the info */
Ping Chengea2e6022012-06-12 00:14:12 -0700481 if (features->type < BAMBOO_PT) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800482 goto out;
Ping Cheng19635182012-04-29 21:09:18 -0700483 }
Ping Chengec67bbe2009-12-15 00:35:24 -0800484
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -0700485 error = wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800486
Ping Chengec67bbe2009-12-15 00:35:24 -0800487 out:
488 return error;
489}
490
Ping Cheng4492eff2010-03-19 22:18:15 -0700491struct wacom_usbdev_data {
492 struct list_head list;
493 struct kref kref;
494 struct usb_device *dev;
495 struct wacom_shared shared;
496};
497
498static LIST_HEAD(wacom_udev_list);
499static DEFINE_MUTEX(wacom_udev_list_lock);
500
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700501static struct usb_device *wacom_get_sibling(struct usb_device *dev, int vendor, int product)
502{
503 int port1;
504 struct usb_device *sibling;
505
506 if (vendor == 0 && product == 0)
507 return dev;
508
509 if (dev->parent == NULL)
510 return NULL;
511
512 usb_hub_for_each_child(dev->parent, port1, sibling) {
513 struct usb_device_descriptor *d;
514 if (sibling == NULL)
515 continue;
516
517 d = &sibling->descriptor;
518 if (d->idVendor == vendor && d->idProduct == product)
519 return sibling;
520 }
521
522 return NULL;
523}
524
Ping Cheng4492eff2010-03-19 22:18:15 -0700525static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
526{
527 struct wacom_usbdev_data *data;
528
529 list_for_each_entry(data, &wacom_udev_list, list) {
530 if (data->dev == dev) {
531 kref_get(&data->kref);
532 return data;
533 }
534 }
535
536 return NULL;
537}
538
539static int wacom_add_shared_data(struct wacom_wac *wacom,
540 struct usb_device *dev)
541{
542 struct wacom_usbdev_data *data;
543 int retval = 0;
544
545 mutex_lock(&wacom_udev_list_lock);
546
547 data = wacom_get_usbdev_data(dev);
548 if (!data) {
549 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
550 if (!data) {
551 retval = -ENOMEM;
552 goto out;
553 }
554
555 kref_init(&data->kref);
556 data->dev = dev;
557 list_add_tail(&data->list, &wacom_udev_list);
558 }
559
560 wacom->shared = &data->shared;
561
562out:
563 mutex_unlock(&wacom_udev_list_lock);
564 return retval;
565}
566
567static void wacom_release_shared_data(struct kref *kref)
568{
569 struct wacom_usbdev_data *data =
570 container_of(kref, struct wacom_usbdev_data, kref);
571
572 mutex_lock(&wacom_udev_list_lock);
573 list_del(&data->list);
574 mutex_unlock(&wacom_udev_list_lock);
575
576 kfree(data);
577}
578
579static void wacom_remove_shared_data(struct wacom_wac *wacom)
580{
581 struct wacom_usbdev_data *data;
582
583 if (wacom->shared) {
584 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
585 kref_put(&data->kref, wacom_release_shared_data);
586 wacom->shared = NULL;
587 }
588}
589
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700590static int wacom_led_control(struct wacom *wacom)
591{
592 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700593 int retval;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700594
595 buf = kzalloc(9, GFP_KERNEL);
596 if (!buf)
597 return -ENOMEM;
598
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700599 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700600 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700601 /*
602 * Touch Ring and crop mark LED luminance may take on
603 * one of four values:
604 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
605 */
606 int ring_led = wacom->led.select[0] & 0x03;
607 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
608 int crop_lum = 0;
Ping Cheng09e7d942011-10-04 23:51:14 -0700609
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700610 buf[0] = WAC_CMD_LED_CONTROL;
611 buf[1] = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
612 }
613 else {
614 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700615
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700616 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
617 wacom->wacom_wac.features.type == WACOM_24HD)
618 led |= (wacom->led.select[1] << 4) | 0x40;
619
620 buf[0] = WAC_CMD_LED_CONTROL;
621 buf[1] = led;
622 buf[2] = wacom->led.llv;
623 buf[3] = wacom->led.hlv;
624 buf[4] = wacom->led.img_lum;
625 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700626
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700627 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
628 WAC_CMD_LED_CONTROL, buf, 9, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700629 kfree(buf);
630
631 return retval;
632}
633
634static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
635{
636 unsigned char *buf;
637 int i, retval;
638
639 buf = kzalloc(259, GFP_KERNEL);
640 if (!buf)
641 return -ENOMEM;
642
643 /* Send 'start' command */
644 buf[0] = WAC_CMD_ICON_START;
645 buf[1] = 1;
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700646 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
647 WAC_CMD_ICON_START, buf, 2, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700648 if (retval < 0)
649 goto out;
650
651 buf[0] = WAC_CMD_ICON_XFER;
652 buf[1] = button_id & 0x07;
653 for (i = 0; i < 4; i++) {
654 buf[2] = i;
655 memcpy(buf + 3, img + i * 256, 256);
656
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700657 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
658 WAC_CMD_ICON_XFER,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700659 buf, 259, WAC_CMD_RETRIES);
660 if (retval < 0)
661 break;
662 }
663
664 /* Send 'stop' */
665 buf[0] = WAC_CMD_ICON_START;
666 buf[1] = 0;
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700667 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, WAC_CMD_ICON_START,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700668 buf, 2, WAC_CMD_RETRIES);
669
670out:
671 kfree(buf);
672 return retval;
673}
674
Ping Cheng09e7d942011-10-04 23:51:14 -0700675static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700676 const char *buf, size_t count)
677{
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700678 struct hid_device *hdev = dev_get_drvdata(dev);
679 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700680 unsigned int id;
681 int err;
682
683 err = kstrtouint(buf, 10, &id);
684 if (err)
685 return err;
686
687 mutex_lock(&wacom->lock);
688
Ping Cheng09e7d942011-10-04 23:51:14 -0700689 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700690 err = wacom_led_control(wacom);
691
692 mutex_unlock(&wacom->lock);
693
694 return err < 0 ? err : count;
695}
696
Ping Cheng09e7d942011-10-04 23:51:14 -0700697#define DEVICE_LED_SELECT_ATTR(SET_ID) \
698static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
699 struct device_attribute *attr, const char *buf, size_t count) \
700{ \
701 return wacom_led_select_store(dev, SET_ID, buf, count); \
702} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700703static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
704 struct device_attribute *attr, char *buf) \
705{ \
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700706 struct hid_device *hdev = dev_get_drvdata(dev); \
707 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700708 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
709} \
710static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
711 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700712 wacom_led##SET_ID##_select_store)
713
714DEVICE_LED_SELECT_ATTR(0);
715DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700716
717static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
718 const char *buf, size_t count)
719{
720 unsigned int value;
721 int err;
722
723 err = kstrtouint(buf, 10, &value);
724 if (err)
725 return err;
726
727 mutex_lock(&wacom->lock);
728
729 *dest = value & 0x7f;
730 err = wacom_led_control(wacom);
731
732 mutex_unlock(&wacom->lock);
733
734 return err < 0 ? err : count;
735}
736
737#define DEVICE_LUMINANCE_ATTR(name, field) \
738static ssize_t wacom_##name##_luminance_store(struct device *dev, \
739 struct device_attribute *attr, const char *buf, size_t count) \
740{ \
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700741 struct hid_device *hdev = dev_get_drvdata(dev); \
742 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700743 \
744 return wacom_luminance_store(wacom, &wacom->led.field, \
745 buf, count); \
746} \
747static DEVICE_ATTR(name##_luminance, S_IWUSR, \
748 NULL, wacom_##name##_luminance_store)
749
750DEVICE_LUMINANCE_ATTR(status0, llv);
751DEVICE_LUMINANCE_ATTR(status1, hlv);
752DEVICE_LUMINANCE_ATTR(buttons, img_lum);
753
754static ssize_t wacom_button_image_store(struct device *dev, int button_id,
755 const char *buf, size_t count)
756{
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700757 struct hid_device *hdev = dev_get_drvdata(dev);
758 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700759 int err;
760
761 if (count != 1024)
762 return -EINVAL;
763
764 mutex_lock(&wacom->lock);
765
766 err = wacom_led_putimage(wacom, button_id, buf);
767
768 mutex_unlock(&wacom->lock);
769
770 return err < 0 ? err : count;
771}
772
773#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
774static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
775 struct device_attribute *attr, const char *buf, size_t count) \
776{ \
777 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
778} \
779static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
780 NULL, wacom_btnimg##BUTTON_ID##_store)
781
782DEVICE_BTNIMG_ATTR(0);
783DEVICE_BTNIMG_ATTR(1);
784DEVICE_BTNIMG_ATTR(2);
785DEVICE_BTNIMG_ATTR(3);
786DEVICE_BTNIMG_ATTR(4);
787DEVICE_BTNIMG_ATTR(5);
788DEVICE_BTNIMG_ATTR(6);
789DEVICE_BTNIMG_ATTR(7);
790
Ping Cheng09e7d942011-10-04 23:51:14 -0700791static struct attribute *cintiq_led_attrs[] = {
792 &dev_attr_status_led0_select.attr,
793 &dev_attr_status_led1_select.attr,
794 NULL
795};
796
797static struct attribute_group cintiq_led_attr_group = {
798 .name = "wacom_led",
799 .attrs = cintiq_led_attrs,
800};
801
802static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700803 &dev_attr_status0_luminance.attr,
804 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700805 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700806 &dev_attr_buttons_luminance.attr,
807 &dev_attr_button0_rawimg.attr,
808 &dev_attr_button1_rawimg.attr,
809 &dev_attr_button2_rawimg.attr,
810 &dev_attr_button3_rawimg.attr,
811 &dev_attr_button4_rawimg.attr,
812 &dev_attr_button5_rawimg.attr,
813 &dev_attr_button6_rawimg.attr,
814 &dev_attr_button7_rawimg.attr,
815 NULL
816};
817
Ping Cheng09e7d942011-10-04 23:51:14 -0700818static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700819 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700820 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700821};
822
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700823static struct attribute *intuos5_led_attrs[] = {
824 &dev_attr_status0_luminance.attr,
825 &dev_attr_status_led0_select.attr,
826 NULL
827};
828
829static struct attribute_group intuos5_led_attr_group = {
830 .name = "wacom_led",
831 .attrs = intuos5_led_attrs,
832};
833
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700834static int wacom_initialize_leds(struct wacom *wacom)
835{
836 int error;
837
Ping Cheng09e7d942011-10-04 23:51:14 -0700838 /* Initialize default values */
839 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700840 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700841 case INTUOS4:
842 case INTUOS4L:
843 wacom->led.select[0] = 0;
844 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700845 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700846 wacom->led.hlv = 20;
847 wacom->led.img_lum = 10;
Ping Cheng09e7d942011-10-04 23:51:14 -0700848 error = sysfs_create_group(&wacom->intf->dev.kobj,
849 &intuos4_led_attr_group);
850 break;
851
Jason Gerecke246835f2011-12-12 00:12:04 -0800852 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700853 case WACOM_21UX2:
854 wacom->led.select[0] = 0;
855 wacom->led.select[1] = 0;
856 wacom->led.llv = 0;
857 wacom->led.hlv = 0;
858 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700859
860 error = sysfs_create_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700861 &cintiq_led_attr_group);
862 break;
863
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700864 case INTUOS5S:
865 case INTUOS5:
866 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700867 case INTUOSPS:
868 case INTUOSPM:
869 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700870 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
871 wacom->led.select[0] = 0;
872 wacom->led.select[1] = 0;
873 wacom->led.llv = 32;
874 wacom->led.hlv = 0;
875 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700876
Ping Chengc2b0c272013-09-20 09:50:14 -0700877 error = sysfs_create_group(&wacom->intf->dev.kobj,
878 &intuos5_led_attr_group);
879 } else
880 return 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700881 break;
882
Ping Cheng09e7d942011-10-04 23:51:14 -0700883 default:
884 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700885 }
886
Ping Cheng09e7d942011-10-04 23:51:14 -0700887 if (error) {
888 dev_err(&wacom->intf->dev,
889 "cannot create sysfs group err: %d\n", error);
890 return error;
891 }
892 wacom_led_control(wacom);
893
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700894 return 0;
895}
896
897static void wacom_destroy_leds(struct wacom *wacom)
898{
Ping Cheng09e7d942011-10-04 23:51:14 -0700899 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700900 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700901 case INTUOS4:
902 case INTUOS4L:
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700903 sysfs_remove_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700904 &intuos4_led_attr_group);
905 break;
906
Jason Gerecke246835f2011-12-12 00:12:04 -0800907 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700908 case WACOM_21UX2:
909 sysfs_remove_group(&wacom->intf->dev.kobj,
910 &cintiq_led_attr_group);
911 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700912
913 case INTUOS5S:
914 case INTUOS5:
915 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700916 case INTUOSPS:
917 case INTUOSPM:
918 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700919 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
920 sysfs_remove_group(&wacom->intf->dev.kobj,
921 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700922 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700923 }
924}
925
Chris Bagwella1d552c2012-03-25 23:26:30 -0700926static enum power_supply_property wacom_battery_props[] = {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700927 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700928 POWER_SUPPLY_PROP_CAPACITY
929};
930
931static int wacom_battery_get_property(struct power_supply *psy,
932 enum power_supply_property psp,
933 union power_supply_propval *val)
934{
935 struct wacom *wacom = container_of(psy, struct wacom, battery);
936 int ret = 0;
937
938 switch (psp) {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700939 case POWER_SUPPLY_PROP_SCOPE:
940 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
941 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700942 case POWER_SUPPLY_PROP_CAPACITY:
943 val->intval =
944 wacom->wacom_wac.battery_capacity * 100 / 31;
945 break;
946 default:
947 ret = -EINVAL;
948 break;
949 }
950
951 return ret;
952}
953
954static int wacom_initialize_battery(struct wacom *wacom)
955{
956 int error = 0;
957
958 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) {
959 wacom->battery.properties = wacom_battery_props;
960 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
961 wacom->battery.get_property = wacom_battery_get_property;
962 wacom->battery.name = "wacom_battery";
963 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
964 wacom->battery.use_for_apm = 0;
965
966 error = power_supply_register(&wacom->usbdev->dev,
967 &wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700968
969 if (!error)
970 power_supply_powers(&wacom->battery,
971 &wacom->usbdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -0700972 }
973
974 return error;
975}
976
977static void wacom_destroy_battery(struct wacom *wacom)
978{
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700979 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR &&
980 wacom->battery.dev) {
Chris Bagwella1d552c2012-03-25 23:26:30 -0700981 power_supply_unregister(&wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700982 wacom->battery.dev = NULL;
983 }
Chris Bagwella1d552c2012-03-25 23:26:30 -0700984}
985
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -0700986static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700987{
988 struct input_dev *input_dev;
989 struct usb_interface *intf = wacom->intf;
990 struct usb_device *dev = interface_to_usbdev(intf);
991 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700992
993 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -0700994 if (!input_dev)
995 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700996
997 input_dev->name = wacom_wac->name;
Benjamin Tissoires7097d4c2014-07-24 12:48:06 -0700998 input_dev->phys = wacom->phys;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700999 input_dev->dev.parent = &intf->dev;
1000 input_dev->open = wacom_open;
1001 input_dev->close = wacom_close;
1002 usb_to_input_id(dev, &input_dev->id);
1003 input_set_drvdata(input_dev, wacom);
1004
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001005 return input_dev;
1006}
1007
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001008static void wacom_unregister_inputs(struct wacom *wacom)
1009{
1010 if (wacom->wacom_wac.input)
1011 input_unregister_device(wacom->wacom_wac.input);
1012 if (wacom->wacom_wac.pad_input)
1013 input_unregister_device(wacom->wacom_wac.pad_input);
1014 wacom->wacom_wac.input = NULL;
1015 wacom->wacom_wac.pad_input = NULL;
1016}
1017
1018static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001019{
1020 struct input_dev *input_dev, *pad_input_dev;
1021 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1022 int error;
1023
1024 input_dev = wacom_allocate_input(wacom);
1025 pad_input_dev = wacom_allocate_input(wacom);
1026 if (!input_dev || !pad_input_dev) {
1027 error = -ENOMEM;
1028 goto fail1;
1029 }
1030
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001031 wacom_wac->input = input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001032 wacom_wac->pad_input = pad_input_dev;
1033 wacom_wac->pad_input->name = wacom_wac->pad_name;
1034
Ping Cheng19635182012-04-29 21:09:18 -07001035 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1036 if (error)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001037 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001038
1039 error = input_register_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001040 if (error)
1041 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001042
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001043 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1044 if (error) {
1045 /* no pad in use on this interface */
1046 input_free_device(pad_input_dev);
1047 wacom_wac->pad_input = NULL;
1048 pad_input_dev = NULL;
1049 } else {
1050 error = input_register_device(pad_input_dev);
1051 if (error)
1052 goto fail3;
1053 }
1054
Ping Cheng19635182012-04-29 21:09:18 -07001055 return 0;
1056
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001057fail3:
1058 input_unregister_device(input_dev);
1059 input_dev = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001060fail2:
Ping Cheng19635182012-04-29 21:09:18 -07001061 wacom_wac->input = NULL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001062 wacom_wac->pad_input = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001063fail1:
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001064 if (input_dev)
1065 input_free_device(input_dev);
1066 if (pad_input_dev)
1067 input_free_device(pad_input_dev);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001068 return error;
1069}
1070
Chris Bagwell16bf2882012-03-25 23:26:20 -07001071static void wacom_wireless_work(struct work_struct *work)
1072{
1073 struct wacom *wacom = container_of(work, struct wacom, work);
1074 struct usb_device *usbdev = wacom->usbdev;
1075 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001076 struct hid_device *hdev1, *hdev2;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001077 struct wacom *wacom1, *wacom2;
1078 struct wacom_wac *wacom_wac1, *wacom_wac2;
1079 int error;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001080
1081 /*
1082 * Regardless if this is a disconnect or a new tablet,
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001083 * remove any existing input and battery devices.
Chris Bagwell16bf2882012-03-25 23:26:20 -07001084 */
1085
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001086 wacom_destroy_battery(wacom);
1087
Chris Bagwell16bf2882012-03-25 23:26:20 -07001088 /* Stylus interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001089 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1090 wacom1 = hid_get_drvdata(hdev1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001091 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001092 wacom_unregister_inputs(wacom1);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001093
1094 /* Touch interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001095 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1096 wacom2 = hid_get_drvdata(hdev2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001097 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001098 wacom_unregister_inputs(wacom2);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001099
1100 if (wacom_wac->pid == 0) {
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001101 dev_info(&wacom->intf->dev, "wireless tablet disconnected\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001102 } else {
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001103 const struct hid_device_id *id = wacom_ids;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001104
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001105 dev_info(&wacom->intf->dev,
1106 "wireless tablet connected with PID %x\n",
1107 wacom_wac->pid);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001108
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001109 while (id->bus) {
1110 if (id->vendor == USB_VENDOR_ID_WACOM &&
1111 id->product == wacom_wac->pid)
Chris Bagwell16bf2882012-03-25 23:26:20 -07001112 break;
1113 id++;
1114 }
1115
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001116 if (!id->bus) {
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001117 dev_info(&wacom->intf->dev,
1118 "ignoring unknown PID.\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001119 return;
1120 }
1121
1122 /* Stylus interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001123 wacom_wac1->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001124 *((struct wacom_features *)id->driver_data);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001125 wacom_wac1->features.device_type = BTN_TOOL_PEN;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001126 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1127 wacom_wac1->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001128 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1129 wacom_wac1->features.name);
Ping Cheng961794a2013-12-05 12:54:53 -08001130 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1131 wacom_wac1->shared->type = wacom_wac1->features.type;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001132 error = wacom_register_inputs(wacom1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001133 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001134 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001135
1136 /* Touch interface */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001137 if (wacom_wac1->features.touch_max ||
1138 wacom_wac1->features.type == INTUOSHT) {
Ping Cheng57bcfce2013-10-15 23:44:00 -07001139 wacom_wac2->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001140 *((struct wacom_features *)id->driver_data);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001141 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1142 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1143 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1144 if (wacom_wac2->features.touch_max)
1145 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1146 "%s (WL) Finger",wacom_wac2->features.name);
1147 else
1148 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1149 "%s (WL) Pad",wacom_wac2->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001150 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1151 "%s (WL) Pad", wacom_wac2->features.name);
1152 error = wacom_register_inputs(wacom2);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001153 if (error)
1154 goto fail;
Ping Cheng961794a2013-12-05 12:54:53 -08001155
1156 if (wacom_wac1->features.type == INTUOSHT &&
1157 wacom_wac1->features.touch_max)
1158 wacom_wac->shared->touch_input = wacom_wac2->input;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001159 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001160
1161 error = wacom_initialize_battery(wacom);
1162 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001163 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001164 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001165
1166 return;
1167
Ping Cheng57bcfce2013-10-15 23:44:00 -07001168fail:
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001169 wacom_unregister_inputs(wacom1);
1170 wacom_unregister_inputs(wacom2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001171 return;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001172}
1173
Ping Cheng401d7d12013-07-28 00:38:54 -07001174/*
1175 * Not all devices report physical dimensions from HID.
1176 * Compute the default from hardcoded logical dimension
1177 * and resolution before driver overwrites them.
1178 */
1179static void wacom_set_default_phy(struct wacom_features *features)
1180{
1181 if (features->x_resolution) {
1182 features->x_phy = (features->x_max * 100) /
1183 features->x_resolution;
1184 features->y_phy = (features->y_max * 100) /
1185 features->y_resolution;
1186 }
1187}
1188
1189static void wacom_calculate_res(struct wacom_features *features)
1190{
1191 features->x_resolution = wacom_calc_hid_res(features->x_max,
1192 features->x_phy,
1193 features->unit,
1194 features->unitExpo);
1195 features->y_resolution = wacom_calc_hid_res(features->y_max,
1196 features->y_phy,
1197 features->unit,
1198 features->unitExpo);
1199}
1200
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001201static int wacom_hid_report_len(struct hid_report *report)
1202{
1203 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1204 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1205}
1206
1207static size_t wacom_compute_pktlen(struct hid_device *hdev)
1208{
1209 struct hid_report_enum *report_enum;
1210 struct hid_report *report;
1211 size_t size = 0;
1212
1213 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1214
1215 list_for_each_entry(report, &report_enum->report_list, list) {
1216 size_t report_size = wacom_hid_report_len(report);
1217 if (report_size > size)
1218 size = report_size;
1219 }
1220
1221 return size;
1222}
1223
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001224static int wacom_probe(struct hid_device *hdev,
1225 const struct hid_device_id *id)
Ping Cheng3bea7332006-07-13 18:01:36 -07001226{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001227 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Ping Cheng3bea7332006-07-13 18:01:36 -07001228 struct usb_device *dev = interface_to_usbdev(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001229 struct wacom *wacom;
1230 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001231 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001232 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001233
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001234 if (!id->driver_data)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001235 return -EINVAL;
1236
Ping Cheng3bea7332006-07-13 18:01:36 -07001237 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001238 if (!wacom)
1239 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001240
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001241 hid_set_drvdata(hdev, wacom);
1242 wacom->hdev = hdev;
1243
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001244 /* ask for the report descriptor to be loaded by HID */
1245 error = hid_parse(hdev);
1246 if (error) {
1247 hid_err(hdev, "parse failed\n");
1248 goto fail1;
1249 }
1250
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001251 wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001252 wacom_wac->features = *((struct wacom_features *)id->driver_data);
Jason Childse33da8a2010-02-17 22:38:31 -08001253 features = &wacom_wac->features;
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001254 features->pktlen = wacom_compute_pktlen(hdev);
Jason Childse33da8a2010-02-17 22:38:31 -08001255 if (features->pktlen > WACOM_PKGLEN_MAX) {
1256 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001257 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -08001258 }
1259
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001260 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1261 error = -ENODEV;
Jason Childse33da8a2010-02-17 22:38:31 -08001262 goto fail1;
1263 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001264
Ping Cheng3bea7332006-07-13 18:01:36 -07001265 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001266 wacom->intf = intf;
1267 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001268 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001269 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
1270 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
1271
Ping Cheng401d7d12013-07-28 00:38:54 -07001272 /* set the default size in case we do not get them from hid */
1273 wacom_set_default_phy(features);
1274
Ping Chengf393ee22012-04-29 21:09:17 -07001275 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001276 error = wacom_retrieve_hid_descriptor(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -08001277 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001278 goto fail1;
Ping Cheng545f4e92008-11-24 11:44:27 -05001279
Jason Gereckeae584ca2012-04-03 15:50:40 -07001280 /*
1281 * Intuos5 has no useful data about its touch interface in its
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001282 * HID descriptor. If this is the touch interface (PacketSize
Jason Gereckeae584ca2012-04-03 15:50:40 -07001283 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1284 */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001285 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001286 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
Jason Gereckeae584ca2012-04-03 15:50:40 -07001287 features->device_type = BTN_TOOL_FINGER;
Jason Gereckeae584ca2012-04-03 15:50:40 -07001288
Jason Gereckeae584ca2012-04-03 15:50:40 -07001289 features->x_max = 4096;
1290 features->y_max = 4096;
1291 } else {
1292 features->device_type = BTN_TOOL_PEN;
1293 }
1294 }
1295
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001296 wacom_setup_device_quirks(features);
1297
Ping Cheng401d7d12013-07-28 00:38:54 -07001298 /* set unit to "100th of a mm" for devices not reported by HID */
1299 if (!features->unit) {
1300 features->unit = 0x11;
1301 features->unitExpo = 16 - 3;
1302 }
1303 wacom_calculate_res(features);
1304
Ping Cheng49b764a2010-02-20 00:53:49 -08001305 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001306 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1307 "%s Pad", features->name);
Ping Cheng49b764a2010-02-20 00:53:49 -08001308
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001309 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Jason Gereckeaea2bf62012-10-21 00:38:03 -07001310 struct usb_device *other_dev;
1311
Ping Cheng49b764a2010-02-20 00:53:49 -08001312 /* Append the device type to the name */
Ping Cheng57bcfce2013-10-15 23:44:00 -07001313 if (features->device_type != BTN_TOOL_FINGER)
1314 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1315 else if (features->touch_max)
1316 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1317 else
1318 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
Ping Cheng4492eff2010-03-19 22:18:15 -07001319
Jason Gereckeaea2bf62012-10-21 00:38:03 -07001320 other_dev = wacom_get_sibling(dev, features->oVid, features->oPid);
1321 if (other_dev == NULL || wacom_get_usbdev_data(other_dev) == NULL)
1322 other_dev = dev;
1323 error = wacom_add_shared_data(wacom_wac, other_dev);
Ping Cheng4492eff2010-03-19 22:18:15 -07001324 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001325 goto fail1;
Ping Cheng49b764a2010-02-20 00:53:49 -08001326 }
1327
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001328 error = wacom_initialize_leds(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001329 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001330 goto fail2;
Ping Cheng3bea7332006-07-13 18:01:36 -07001331
Chris Bagwelld3825d52012-03-25 23:26:11 -07001332 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001333 error = wacom_register_inputs(wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001334 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001335 goto fail3;
Chris Bagwelld3825d52012-03-25 23:26:11 -07001336 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001337
Ping Chengec67bbe2009-12-15 00:35:24 -08001338 /* Note that if query fails it is not a hard failure */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001339 wacom_query_tablet_data(hdev, features);
Ping Cheng3bea7332006-07-13 18:01:36 -07001340
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001341 /* Regular HID work starts now */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001342 error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1343 if (error) {
1344 hid_err(hdev, "hw start failed\n");
1345 goto fail4;
1346 }
1347
1348 if (features->quirks & WACOM_QUIRK_MONITOR)
1349 error = hid_hw_open(hdev);
1350
Ping Cheng961794a2013-12-05 12:54:53 -08001351 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1352 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1353 wacom_wac->shared->touch_input = wacom_wac->input;
1354 }
1355
Ping Cheng3bea7332006-07-13 18:01:36 -07001356 return 0;
1357
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001358 fail4: wacom_unregister_inputs(wacom);
1359 fail3: wacom_destroy_leds(wacom);
1360 fail2: wacom_remove_shared_data(wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001361 fail1: kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001362 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001363 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001364}
1365
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001366static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001367{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001368 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001369
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001370 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001371
Chris Bagwell16bf2882012-03-25 23:26:20 -07001372 cancel_work_sync(&wacom->work);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001373 wacom_unregister_inputs(wacom);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001374 wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001375 wacom_destroy_leds(wacom);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001376 wacom_remove_shared_data(&wacom->wacom_wac);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001377
1378 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001379 kfree(wacom);
1380}
1381
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001382static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001383{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001384 struct wacom *wacom = hid_get_drvdata(hdev);
1385 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001386
1387 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001388
1389 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001390 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001391 wacom_led_control(wacom);
1392
Oliver Neukume7224092008-04-15 01:31:57 -04001393 mutex_unlock(&wacom->lock);
1394
1395 return 0;
1396}
1397
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001398static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001399{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001400 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001401}
1402
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001403static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001404 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001405 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001406 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001407 .remove = wacom_remove,
1408#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001409 .resume = wacom_resume,
1410 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001411#endif
1412 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001413};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001414module_hid_driver(wacom_driver);