blob: b8710594185a3ae5701dd323c8815731711a672c [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 Cheng8b4a0c12012-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
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700491struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700492 struct list_head list;
493 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700494 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700495 struct wacom_shared shared;
496};
497
498static LIST_HEAD(wacom_udev_list);
499static DEFINE_MUTEX(wacom_udev_list_lock);
500
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700501static bool wacom_are_sibling(struct hid_device *hdev,
502 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700503{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700504 struct wacom *wacom = hid_get_drvdata(hdev);
505 struct wacom_features *features = &wacom->wacom_wac.features;
506 int vid = features->oVid;
507 int pid = features->oPid;
508 int n1,n2;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700509
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700510 if (vid == 0 && pid == 0) {
511 vid = hdev->vendor;
512 pid = hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700513 }
514
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700515 if (vid != sibling->vendor || pid != sibling->product)
516 return false;
517
518 /* Compare the physical path. */
519 n1 = strrchr(hdev->phys, '.') - hdev->phys;
520 n2 = strrchr(sibling->phys, '.') - sibling->phys;
521 if (n1 != n2 || n1 <= 0 || n2 <= 0)
522 return false;
523
524 return !strncmp(hdev->phys, sibling->phys, n1);
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700525}
526
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700527static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700528{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700529 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700530
531 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700532 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700533 kref_get(&data->kref);
534 return data;
535 }
536 }
537
538 return NULL;
539}
540
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700541static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700542{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700543 struct wacom *wacom = hid_get_drvdata(hdev);
544 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
545 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700546 int retval = 0;
547
548 mutex_lock(&wacom_udev_list_lock);
549
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700550 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700551 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700552 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700553 if (!data) {
554 retval = -ENOMEM;
555 goto out;
556 }
557
558 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700559 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700560 list_add_tail(&data->list, &wacom_udev_list);
561 }
562
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700563 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700564
565out:
566 mutex_unlock(&wacom_udev_list_lock);
567 return retval;
568}
569
570static void wacom_release_shared_data(struct kref *kref)
571{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700572 struct wacom_hdev_data *data =
573 container_of(kref, struct wacom_hdev_data, kref);
Ping Cheng4492eff2010-03-19 22:18:15 -0700574
575 mutex_lock(&wacom_udev_list_lock);
576 list_del(&data->list);
577 mutex_unlock(&wacom_udev_list_lock);
578
579 kfree(data);
580}
581
582static void wacom_remove_shared_data(struct wacom_wac *wacom)
583{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700584 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700585
586 if (wacom->shared) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700587 data = container_of(wacom->shared, struct wacom_hdev_data, shared);
Ping Cheng4492eff2010-03-19 22:18:15 -0700588 kref_put(&data->kref, wacom_release_shared_data);
589 wacom->shared = NULL;
590 }
591}
592
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700593static int wacom_led_control(struct wacom *wacom)
594{
595 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700596 int retval;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700597
598 buf = kzalloc(9, GFP_KERNEL);
599 if (!buf)
600 return -ENOMEM;
601
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700602 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700603 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700604 /*
605 * Touch Ring and crop mark LED luminance may take on
606 * one of four values:
607 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
608 */
609 int ring_led = wacom->led.select[0] & 0x03;
610 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
611 int crop_lum = 0;
Ping Cheng09e7d942011-10-04 23:51:14 -0700612
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700613 buf[0] = WAC_CMD_LED_CONTROL;
614 buf[1] = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
615 }
616 else {
617 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700618
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700619 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
620 wacom->wacom_wac.features.type == WACOM_24HD)
621 led |= (wacom->led.select[1] << 4) | 0x40;
622
623 buf[0] = WAC_CMD_LED_CONTROL;
624 buf[1] = led;
625 buf[2] = wacom->led.llv;
626 buf[3] = wacom->led.hlv;
627 buf[4] = wacom->led.img_lum;
628 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700629
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700630 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
631 WAC_CMD_LED_CONTROL, buf, 9, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700632 kfree(buf);
633
634 return retval;
635}
636
637static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
638{
639 unsigned char *buf;
640 int i, retval;
641
642 buf = kzalloc(259, GFP_KERNEL);
643 if (!buf)
644 return -ENOMEM;
645
646 /* Send 'start' command */
647 buf[0] = WAC_CMD_ICON_START;
648 buf[1] = 1;
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700649 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
650 WAC_CMD_ICON_START, buf, 2, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700651 if (retval < 0)
652 goto out;
653
654 buf[0] = WAC_CMD_ICON_XFER;
655 buf[1] = button_id & 0x07;
656 for (i = 0; i < 4; i++) {
657 buf[2] = i;
658 memcpy(buf + 3, img + i * 256, 256);
659
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700660 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
661 WAC_CMD_ICON_XFER,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700662 buf, 259, WAC_CMD_RETRIES);
663 if (retval < 0)
664 break;
665 }
666
667 /* Send 'stop' */
668 buf[0] = WAC_CMD_ICON_START;
669 buf[1] = 0;
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700670 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, WAC_CMD_ICON_START,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700671 buf, 2, WAC_CMD_RETRIES);
672
673out:
674 kfree(buf);
675 return retval;
676}
677
Ping Cheng09e7d942011-10-04 23:51:14 -0700678static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700679 const char *buf, size_t count)
680{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700681 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700682 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700683 unsigned int id;
684 int err;
685
686 err = kstrtouint(buf, 10, &id);
687 if (err)
688 return err;
689
690 mutex_lock(&wacom->lock);
691
Ping Cheng09e7d942011-10-04 23:51:14 -0700692 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700693 err = wacom_led_control(wacom);
694
695 mutex_unlock(&wacom->lock);
696
697 return err < 0 ? err : count;
698}
699
Ping Cheng09e7d942011-10-04 23:51:14 -0700700#define DEVICE_LED_SELECT_ATTR(SET_ID) \
701static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
702 struct device_attribute *attr, const char *buf, size_t count) \
703{ \
704 return wacom_led_select_store(dev, SET_ID, buf, count); \
705} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700706static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
707 struct device_attribute *attr, char *buf) \
708{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700709 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700710 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700711 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
712} \
713static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
714 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700715 wacom_led##SET_ID##_select_store)
716
717DEVICE_LED_SELECT_ATTR(0);
718DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700719
720static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
721 const char *buf, size_t count)
722{
723 unsigned int value;
724 int err;
725
726 err = kstrtouint(buf, 10, &value);
727 if (err)
728 return err;
729
730 mutex_lock(&wacom->lock);
731
732 *dest = value & 0x7f;
733 err = wacom_led_control(wacom);
734
735 mutex_unlock(&wacom->lock);
736
737 return err < 0 ? err : count;
738}
739
740#define DEVICE_LUMINANCE_ATTR(name, field) \
741static ssize_t wacom_##name##_luminance_store(struct device *dev, \
742 struct device_attribute *attr, const char *buf, size_t count) \
743{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700744 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700745 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700746 \
747 return wacom_luminance_store(wacom, &wacom->led.field, \
748 buf, count); \
749} \
750static DEVICE_ATTR(name##_luminance, S_IWUSR, \
751 NULL, wacom_##name##_luminance_store)
752
753DEVICE_LUMINANCE_ATTR(status0, llv);
754DEVICE_LUMINANCE_ATTR(status1, hlv);
755DEVICE_LUMINANCE_ATTR(buttons, img_lum);
756
757static ssize_t wacom_button_image_store(struct device *dev, int button_id,
758 const char *buf, size_t count)
759{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700760 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700761 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700762 int err;
763
764 if (count != 1024)
765 return -EINVAL;
766
767 mutex_lock(&wacom->lock);
768
769 err = wacom_led_putimage(wacom, button_id, buf);
770
771 mutex_unlock(&wacom->lock);
772
773 return err < 0 ? err : count;
774}
775
776#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
777static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
778 struct device_attribute *attr, const char *buf, size_t count) \
779{ \
780 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
781} \
782static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
783 NULL, wacom_btnimg##BUTTON_ID##_store)
784
785DEVICE_BTNIMG_ATTR(0);
786DEVICE_BTNIMG_ATTR(1);
787DEVICE_BTNIMG_ATTR(2);
788DEVICE_BTNIMG_ATTR(3);
789DEVICE_BTNIMG_ATTR(4);
790DEVICE_BTNIMG_ATTR(5);
791DEVICE_BTNIMG_ATTR(6);
792DEVICE_BTNIMG_ATTR(7);
793
Ping Cheng09e7d942011-10-04 23:51:14 -0700794static struct attribute *cintiq_led_attrs[] = {
795 &dev_attr_status_led0_select.attr,
796 &dev_attr_status_led1_select.attr,
797 NULL
798};
799
800static struct attribute_group cintiq_led_attr_group = {
801 .name = "wacom_led",
802 .attrs = cintiq_led_attrs,
803};
804
805static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700806 &dev_attr_status0_luminance.attr,
807 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700808 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700809 &dev_attr_buttons_luminance.attr,
810 &dev_attr_button0_rawimg.attr,
811 &dev_attr_button1_rawimg.attr,
812 &dev_attr_button2_rawimg.attr,
813 &dev_attr_button3_rawimg.attr,
814 &dev_attr_button4_rawimg.attr,
815 &dev_attr_button5_rawimg.attr,
816 &dev_attr_button6_rawimg.attr,
817 &dev_attr_button7_rawimg.attr,
818 NULL
819};
820
Ping Cheng09e7d942011-10-04 23:51:14 -0700821static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700822 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700823 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700824};
825
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700826static struct attribute *intuos5_led_attrs[] = {
827 &dev_attr_status0_luminance.attr,
828 &dev_attr_status_led0_select.attr,
829 NULL
830};
831
832static struct attribute_group intuos5_led_attr_group = {
833 .name = "wacom_led",
834 .attrs = intuos5_led_attrs,
835};
836
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700837static int wacom_initialize_leds(struct wacom *wacom)
838{
839 int error;
840
Ping Cheng09e7d942011-10-04 23:51:14 -0700841 /* Initialize default values */
842 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700843 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700844 case INTUOS4:
845 case INTUOS4L:
846 wacom->led.select[0] = 0;
847 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700848 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700849 wacom->led.hlv = 20;
850 wacom->led.img_lum = 10;
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700851 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700852 &intuos4_led_attr_group);
853 break;
854
Jason Gerecke246835f2011-12-12 00:12:04 -0800855 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700856 case WACOM_21UX2:
857 wacom->led.select[0] = 0;
858 wacom->led.select[1] = 0;
859 wacom->led.llv = 0;
860 wacom->led.hlv = 0;
861 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700862
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700863 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700864 &cintiq_led_attr_group);
865 break;
866
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700867 case INTUOS5S:
868 case INTUOS5:
869 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700870 case INTUOSPS:
871 case INTUOSPM:
872 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700873 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
874 wacom->led.select[0] = 0;
875 wacom->led.select[1] = 0;
876 wacom->led.llv = 32;
877 wacom->led.hlv = 0;
878 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700879
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700880 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700881 &intuos5_led_attr_group);
882 } else
883 return 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700884 break;
885
Ping Cheng09e7d942011-10-04 23:51:14 -0700886 default:
887 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700888 }
889
Ping Cheng09e7d942011-10-04 23:51:14 -0700890 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700891 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -0700892 "cannot create sysfs group err: %d\n", error);
893 return error;
894 }
895 wacom_led_control(wacom);
896
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700897 return 0;
898}
899
900static void wacom_destroy_leds(struct wacom *wacom)
901{
Ping Cheng09e7d942011-10-04 23:51:14 -0700902 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700903 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700904 case INTUOS4:
905 case INTUOS4L:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700906 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700907 &intuos4_led_attr_group);
908 break;
909
Jason Gerecke246835f2011-12-12 00:12:04 -0800910 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700911 case WACOM_21UX2:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700912 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700913 &cintiq_led_attr_group);
914 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700915
916 case INTUOS5S:
917 case INTUOS5:
918 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700919 case INTUOSPS:
920 case INTUOSPM:
921 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700922 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700923 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700924 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700925 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700926 }
927}
928
Chris Bagwella1d552c2012-03-25 23:26:30 -0700929static enum power_supply_property wacom_battery_props[] = {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700930 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700931 POWER_SUPPLY_PROP_CAPACITY
932};
933
934static int wacom_battery_get_property(struct power_supply *psy,
935 enum power_supply_property psp,
936 union power_supply_propval *val)
937{
938 struct wacom *wacom = container_of(psy, struct wacom, battery);
939 int ret = 0;
940
941 switch (psp) {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700942 case POWER_SUPPLY_PROP_SCOPE:
943 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
944 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700945 case POWER_SUPPLY_PROP_CAPACITY:
946 val->intval =
947 wacom->wacom_wac.battery_capacity * 100 / 31;
948 break;
949 default:
950 ret = -EINVAL;
951 break;
952 }
953
954 return ret;
955}
956
957static int wacom_initialize_battery(struct wacom *wacom)
958{
959 int error = 0;
960
961 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) {
962 wacom->battery.properties = wacom_battery_props;
963 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
964 wacom->battery.get_property = wacom_battery_get_property;
965 wacom->battery.name = "wacom_battery";
966 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
967 wacom->battery.use_for_apm = 0;
968
Benjamin Tissoiresdd3181a2014-07-24 13:01:40 -0700969 error = power_supply_register(&wacom->hdev->dev,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700970 &wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700971
972 if (!error)
973 power_supply_powers(&wacom->battery,
Benjamin Tissoiresdd3181a2014-07-24 13:01:40 -0700974 &wacom->hdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -0700975 }
976
977 return error;
978}
979
980static void wacom_destroy_battery(struct wacom *wacom)
981{
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700982 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR &&
983 wacom->battery.dev) {
Chris Bagwella1d552c2012-03-25 23:26:30 -0700984 power_supply_unregister(&wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700985 wacom->battery.dev = NULL;
986 }
Chris Bagwella1d552c2012-03-25 23:26:30 -0700987}
988
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -0700989static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700990{
991 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -0700992 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700993 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700994
995 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -0700996 if (!input_dev)
997 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700998
999 input_dev->name = wacom_wac->name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001000 input_dev->phys = hdev->phys;
1001 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001002 input_dev->open = wacom_open;
1003 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001004 input_dev->uniq = hdev->uniq;
1005 input_dev->id.bustype = hdev->bus;
1006 input_dev->id.vendor = hdev->vendor;
1007 input_dev->id.product = hdev->product;
1008 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001009 input_set_drvdata(input_dev, wacom);
1010
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001011 return input_dev;
1012}
1013
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001014static void wacom_unregister_inputs(struct wacom *wacom)
1015{
1016 if (wacom->wacom_wac.input)
1017 input_unregister_device(wacom->wacom_wac.input);
1018 if (wacom->wacom_wac.pad_input)
1019 input_unregister_device(wacom->wacom_wac.pad_input);
1020 wacom->wacom_wac.input = NULL;
1021 wacom->wacom_wac.pad_input = NULL;
1022}
1023
1024static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001025{
1026 struct input_dev *input_dev, *pad_input_dev;
1027 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1028 int error;
1029
1030 input_dev = wacom_allocate_input(wacom);
1031 pad_input_dev = wacom_allocate_input(wacom);
1032 if (!input_dev || !pad_input_dev) {
1033 error = -ENOMEM;
1034 goto fail1;
1035 }
1036
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001037 wacom_wac->input = input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001038 wacom_wac->pad_input = pad_input_dev;
1039 wacom_wac->pad_input->name = wacom_wac->pad_name;
1040
Ping Cheng19635182012-04-29 21:09:18 -07001041 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1042 if (error)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001043 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001044
1045 error = input_register_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001046 if (error)
1047 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001048
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001049 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1050 if (error) {
1051 /* no pad in use on this interface */
1052 input_free_device(pad_input_dev);
1053 wacom_wac->pad_input = NULL;
1054 pad_input_dev = NULL;
1055 } else {
1056 error = input_register_device(pad_input_dev);
1057 if (error)
1058 goto fail3;
1059 }
1060
Ping Cheng19635182012-04-29 21:09:18 -07001061 return 0;
1062
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001063fail3:
1064 input_unregister_device(input_dev);
1065 input_dev = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001066fail2:
Ping Cheng19635182012-04-29 21:09:18 -07001067 wacom_wac->input = NULL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001068 wacom_wac->pad_input = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001069fail1:
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001070 if (input_dev)
1071 input_free_device(input_dev);
1072 if (pad_input_dev)
1073 input_free_device(pad_input_dev);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001074 return error;
1075}
1076
Chris Bagwell16bf2882012-03-25 23:26:20 -07001077static void wacom_wireless_work(struct work_struct *work)
1078{
1079 struct wacom *wacom = container_of(work, struct wacom, work);
1080 struct usb_device *usbdev = wacom->usbdev;
1081 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001082 struct hid_device *hdev1, *hdev2;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001083 struct wacom *wacom1, *wacom2;
1084 struct wacom_wac *wacom_wac1, *wacom_wac2;
1085 int error;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001086
1087 /*
1088 * Regardless if this is a disconnect or a new tablet,
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001089 * remove any existing input and battery devices.
Chris Bagwell16bf2882012-03-25 23:26:20 -07001090 */
1091
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001092 wacom_destroy_battery(wacom);
1093
Chris Bagwell16bf2882012-03-25 23:26:20 -07001094 /* Stylus interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001095 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1096 wacom1 = hid_get_drvdata(hdev1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001097 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001098 wacom_unregister_inputs(wacom1);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001099
1100 /* Touch interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001101 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1102 wacom2 = hid_get_drvdata(hdev2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001103 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001104 wacom_unregister_inputs(wacom2);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001105
1106 if (wacom_wac->pid == 0) {
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001107 dev_info(&wacom->intf->dev, "wireless tablet disconnected\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001108 } else {
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001109 const struct hid_device_id *id = wacom_ids;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001110
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001111 dev_info(&wacom->intf->dev,
1112 "wireless tablet connected with PID %x\n",
1113 wacom_wac->pid);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001114
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001115 while (id->bus) {
1116 if (id->vendor == USB_VENDOR_ID_WACOM &&
1117 id->product == wacom_wac->pid)
Chris Bagwell16bf2882012-03-25 23:26:20 -07001118 break;
1119 id++;
1120 }
1121
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001122 if (!id->bus) {
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001123 dev_info(&wacom->intf->dev,
1124 "ignoring unknown PID.\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001125 return;
1126 }
1127
1128 /* Stylus interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001129 wacom_wac1->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001130 *((struct wacom_features *)id->driver_data);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001131 wacom_wac1->features.device_type = BTN_TOOL_PEN;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001132 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1133 wacom_wac1->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001134 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1135 wacom_wac1->features.name);
Ping Cheng961794a2013-12-05 12:54:53 -08001136 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1137 wacom_wac1->shared->type = wacom_wac1->features.type;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001138 error = wacom_register_inputs(wacom1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001139 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001140 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001141
1142 /* Touch interface */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001143 if (wacom_wac1->features.touch_max ||
1144 wacom_wac1->features.type == INTUOSHT) {
Ping Cheng57bcfce2013-10-15 23:44:00 -07001145 wacom_wac2->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001146 *((struct wacom_features *)id->driver_data);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001147 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1148 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1149 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1150 if (wacom_wac2->features.touch_max)
1151 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1152 "%s (WL) Finger",wacom_wac2->features.name);
1153 else
1154 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1155 "%s (WL) Pad",wacom_wac2->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001156 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1157 "%s (WL) Pad", wacom_wac2->features.name);
1158 error = wacom_register_inputs(wacom2);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001159 if (error)
1160 goto fail;
Ping Cheng961794a2013-12-05 12:54:53 -08001161
1162 if (wacom_wac1->features.type == INTUOSHT &&
1163 wacom_wac1->features.touch_max)
1164 wacom_wac->shared->touch_input = wacom_wac2->input;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001165 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001166
1167 error = wacom_initialize_battery(wacom);
1168 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001169 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001170 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001171
1172 return;
1173
Ping Cheng57bcfce2013-10-15 23:44:00 -07001174fail:
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001175 wacom_unregister_inputs(wacom1);
1176 wacom_unregister_inputs(wacom2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001177 return;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001178}
1179
Ping Cheng401d7d12013-07-28 00:38:54 -07001180/*
1181 * Not all devices report physical dimensions from HID.
1182 * Compute the default from hardcoded logical dimension
1183 * and resolution before driver overwrites them.
1184 */
1185static void wacom_set_default_phy(struct wacom_features *features)
1186{
1187 if (features->x_resolution) {
1188 features->x_phy = (features->x_max * 100) /
1189 features->x_resolution;
1190 features->y_phy = (features->y_max * 100) /
1191 features->y_resolution;
1192 }
1193}
1194
1195static void wacom_calculate_res(struct wacom_features *features)
1196{
1197 features->x_resolution = wacom_calc_hid_res(features->x_max,
1198 features->x_phy,
1199 features->unit,
1200 features->unitExpo);
1201 features->y_resolution = wacom_calc_hid_res(features->y_max,
1202 features->y_phy,
1203 features->unit,
1204 features->unitExpo);
1205}
1206
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001207static int wacom_hid_report_len(struct hid_report *report)
1208{
1209 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1210 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1211}
1212
1213static size_t wacom_compute_pktlen(struct hid_device *hdev)
1214{
1215 struct hid_report_enum *report_enum;
1216 struct hid_report *report;
1217 size_t size = 0;
1218
1219 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1220
1221 list_for_each_entry(report, &report_enum->report_list, list) {
1222 size_t report_size = wacom_hid_report_len(report);
1223 if (report_size > size)
1224 size = report_size;
1225 }
1226
1227 return size;
1228}
1229
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001230static int wacom_probe(struct hid_device *hdev,
1231 const struct hid_device_id *id)
Ping Cheng3bea7332006-07-13 18:01:36 -07001232{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001233 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Ping Cheng3bea7332006-07-13 18:01:36 -07001234 struct usb_device *dev = interface_to_usbdev(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001235 struct wacom *wacom;
1236 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001237 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001238 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001239
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001240 if (!id->driver_data)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001241 return -EINVAL;
1242
Ping Cheng3bea7332006-07-13 18:01:36 -07001243 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001244 if (!wacom)
1245 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001246
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001247 hid_set_drvdata(hdev, wacom);
1248 wacom->hdev = hdev;
1249
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001250 /* ask for the report descriptor to be loaded by HID */
1251 error = hid_parse(hdev);
1252 if (error) {
1253 hid_err(hdev, "parse failed\n");
1254 goto fail1;
1255 }
1256
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001257 wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001258 wacom_wac->features = *((struct wacom_features *)id->driver_data);
Jason Childse33da8a2010-02-17 22:38:31 -08001259 features = &wacom_wac->features;
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001260 features->pktlen = wacom_compute_pktlen(hdev);
Jason Childse33da8a2010-02-17 22:38:31 -08001261 if (features->pktlen > WACOM_PKGLEN_MAX) {
1262 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001263 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -08001264 }
1265
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001266 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1267 error = -ENODEV;
Jason Childse33da8a2010-02-17 22:38:31 -08001268 goto fail1;
1269 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001270
Ping Cheng3bea7332006-07-13 18:01:36 -07001271 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001272 wacom->intf = intf;
1273 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001274 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001275
Ping Cheng401d7d12013-07-28 00:38:54 -07001276 /* set the default size in case we do not get them from hid */
1277 wacom_set_default_phy(features);
1278
Ping Chengf393ee22012-04-29 21:09:17 -07001279 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001280 error = wacom_retrieve_hid_descriptor(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -08001281 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001282 goto fail1;
Ping Cheng545f4e92008-11-24 11:44:27 -05001283
Jason Gereckeae584ca2012-04-03 15:50:40 -07001284 /*
1285 * Intuos5 has no useful data about its touch interface in its
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001286 * HID descriptor. If this is the touch interface (PacketSize
Jason Gereckeae584ca2012-04-03 15:50:40 -07001287 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1288 */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001289 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001290 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
Jason Gereckeae584ca2012-04-03 15:50:40 -07001291 features->device_type = BTN_TOOL_FINGER;
Jason Gereckeae584ca2012-04-03 15:50:40 -07001292
Jason Gereckeae584ca2012-04-03 15:50:40 -07001293 features->x_max = 4096;
1294 features->y_max = 4096;
1295 } else {
1296 features->device_type = BTN_TOOL_PEN;
1297 }
1298 }
1299
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001300 wacom_setup_device_quirks(features);
1301
Ping Cheng401d7d12013-07-28 00:38:54 -07001302 /* set unit to "100th of a mm" for devices not reported by HID */
1303 if (!features->unit) {
1304 features->unit = 0x11;
1305 features->unitExpo = 16 - 3;
1306 }
1307 wacom_calculate_res(features);
1308
Ping Cheng49b764a2010-02-20 00:53:49 -08001309 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001310 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1311 "%s Pad", features->name);
Ping Cheng49b764a2010-02-20 00:53:49 -08001312
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001313 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -08001314 /* Append the device type to the name */
Ping Cheng57bcfce2013-10-15 23:44:00 -07001315 if (features->device_type != BTN_TOOL_FINGER)
1316 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1317 else if (features->touch_max)
1318 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1319 else
1320 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
Ping Cheng4492eff2010-03-19 22:18:15 -07001321
Benjamin Tissoires4451e082014-07-24 13:01:05 -07001322 error = wacom_add_shared_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -07001323 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001324 goto fail1;
Ping Cheng49b764a2010-02-20 00:53:49 -08001325 }
1326
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001327 error = wacom_initialize_leds(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001328 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001329 goto fail2;
Ping Cheng3bea7332006-07-13 18:01:36 -07001330
Chris Bagwelld3825d52012-03-25 23:26:11 -07001331 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001332 error = wacom_register_inputs(wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001333 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001334 goto fail3;
Chris Bagwelld3825d52012-03-25 23:26:11 -07001335 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001336
Ping Chengec67bbe2009-12-15 00:35:24 -08001337 /* Note that if query fails it is not a hard failure */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001338 wacom_query_tablet_data(hdev, features);
Ping Cheng3bea7332006-07-13 18:01:36 -07001339
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001340 /* Regular HID work starts now */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001341 error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1342 if (error) {
1343 hid_err(hdev, "hw start failed\n");
1344 goto fail4;
1345 }
1346
1347 if (features->quirks & WACOM_QUIRK_MONITOR)
1348 error = hid_hw_open(hdev);
1349
Ping Cheng961794a2013-12-05 12:54:53 -08001350 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1351 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1352 wacom_wac->shared->touch_input = wacom_wac->input;
1353 }
1354
Ping Cheng3bea7332006-07-13 18:01:36 -07001355 return 0;
1356
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001357 fail4: wacom_unregister_inputs(wacom);
1358 fail3: wacom_destroy_leds(wacom);
1359 fail2: wacom_remove_shared_data(wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001360 fail1: kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001361 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001362 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001363}
1364
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001365static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001366{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001367 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001368
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001369 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001370
Chris Bagwell16bf2882012-03-25 23:26:20 -07001371 cancel_work_sync(&wacom->work);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001372 wacom_unregister_inputs(wacom);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001373 wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001374 wacom_destroy_leds(wacom);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001375 wacom_remove_shared_data(&wacom->wacom_wac);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001376
1377 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001378 kfree(wacom);
1379}
1380
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001381static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001382{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001383 struct wacom *wacom = hid_get_drvdata(hdev);
1384 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001385
1386 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001387
1388 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001389 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001390 wacom_led_control(wacom);
1391
Oliver Neukume7224092008-04-15 01:31:57 -04001392 mutex_unlock(&wacom->lock);
1393
1394 return 0;
1395}
1396
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001397static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001398{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001399 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001400}
1401
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001402static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001403 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001404 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001405 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001406 .remove = wacom_remove,
1407#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001408 .resume = wacom_resume,
1409 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001410#endif
1411 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001412};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001413module_hid_driver(wacom_driver);