blob: b2e2471bbef6b6c5d6db71cb897972980f6d34c7 [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"
Jason Gereckeb58ba1b2014-12-05 13:37:32 -080016#include <linux/input/mt.h>
Ping Cheng3bea7332006-07-13 18:01:36 -070017
Ping Chenga417ea42011-08-16 00:17:56 -070018#define WAC_MSG_RETRIES 5
Ping Cheng3bea7332006-07-13 18:01:36 -070019
Ping Cheng912ca212014-09-10 12:41:31 -070020#define WAC_CMD_WL_LED_CONTROL 0x03
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070021#define WAC_CMD_LED_CONTROL 0x20
22#define WAC_CMD_ICON_START 0x21
23#define WAC_CMD_ICON_XFER 0x23
Benjamin Tissoires849e2f02014-08-06 13:58:25 -070024#define WAC_CMD_ICON_BT_XFER 0x26
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070025#define WAC_CMD_RETRIES 10
Aaron Skomra72b236d2015-08-20 16:05:17 -070026#define WAC_CMD_DELETE_PAIRING 0x20
27#define WAC_CMD_UNPAIR_ALL 0xFF
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070028
Ping Chenge0984bc2014-09-10 12:40:05 -070029#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
30#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
Aaron Skomra72b236d2015-08-20 16:05:17 -070031#define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
Ping Chenge0984bc2014-09-10 12:40:05 -070032
Ping Chengc64d8832014-09-10 12:41:04 -070033static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
34 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070035{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070036 int retval;
37
38 do {
Ping Chengc64d8832014-09-10 12:41:04 -070039 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070040 HID_REQ_GET_REPORT);
Jason Gereckeaef31562015-05-21 10:44:31 -070041 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
42
43 if (retval < 0)
44 hid_err(hdev, "wacom_get_report: ran out of retries "
45 "(last error = %d)\n", retval);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070046
47 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070048}
49
Przemo Firszt296b7372014-08-06 14:00:38 -070050static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
51 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070052{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070053 int retval;
54
55 do {
Przemo Firszt296b7372014-08-06 14:00:38 -070056 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070057 HID_REQ_SET_REPORT);
Jason Gereckeaef31562015-05-21 10:44:31 -070058 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
59
60 if (retval < 0)
61 hid_err(hdev, "wacom_set_report: ran out of retries "
62 "(last error = %d)\n", retval);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070063
64 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070065}
66
Benjamin Tissoires29b47392014-07-24 12:52:23 -070067static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
68 u8 *raw_data, int size)
Ping Cheng3bea7332006-07-13 18:01:36 -070069{
Benjamin Tissoires29b47392014-07-24 12:52:23 -070070 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070071
Benjamin Tissoires29b47392014-07-24 12:52:23 -070072 if (size > WACOM_PKGLEN_MAX)
73 return 1;
Ping Cheng3bea7332006-07-13 18:01:36 -070074
Benjamin Tissoires29b47392014-07-24 12:52:23 -070075 memcpy(wacom->wacom_wac.data, raw_data, size);
Ping Cheng3bea7332006-07-13 18:01:36 -070076
Benjamin Tissoires29b47392014-07-24 12:52:23 -070077 wacom_wac_irq(&wacom->wacom_wac, size);
78
79 return 0;
Ping Cheng3bea7332006-07-13 18:01:36 -070080}
81
Ping Cheng3bea7332006-07-13 18:01:36 -070082static int wacom_open(struct input_dev *dev)
83{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040084 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -070085
Benjamin Tissoiresdff67412014-12-01 11:52:40 -050086 return hid_hw_open(wacom->hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070087}
88
89static void wacom_close(struct input_dev *dev)
90{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040091 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -070092
Benjamin Tissoires3dad1882016-07-13 18:05:54 +020093 /*
94 * wacom->hdev should never be null, but surprisingly, I had the case
95 * once while unplugging the Wacom Wireless Receiver.
96 */
97 if (wacom->hdev)
98 hid_hw_close(wacom->hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070099}
100
Chris Bagwell16bf2882012-03-25 23:26:20 -0700101/*
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700102 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
Jason Gerecke115d5e12012-10-03 17:24:32 -0700103 */
104static int wacom_calc_hid_res(int logical_extents, int physical_extents,
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700105 unsigned unit, int exponent)
Jason Gerecke115d5e12012-10-03 17:24:32 -0700106{
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700107 struct hid_field field = {
108 .logical_maximum = logical_extents,
109 .physical_maximum = physical_extents,
110 .unit = unit,
111 .unit_exponent = exponent,
112 };
Jason Gerecke115d5e12012-10-03 17:24:32 -0700113
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700114 return hidinput_calc_abs_res(&field, ABS_X);
Jason Gerecke115d5e12012-10-03 17:24:32 -0700115}
116
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700117static void wacom_feature_mapping(struct hid_device *hdev,
118 struct hid_field *field, struct hid_usage *usage)
Chris Bagwell41343612011-10-26 22:32:52 -0700119{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700120 struct wacom *wacom = hid_get_drvdata(hdev);
121 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400122 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400123 u8 *data;
124 int ret;
Chris Bagwell41343612011-10-26 22:32:52 -0700125
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700126 switch (usage->hid) {
127 case HID_DG_CONTACTMAX:
128 /* leave touch_max as is if predefined */
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400129 if (!features->touch_max) {
130 /* read manually */
131 data = kzalloc(2, GFP_KERNEL);
132 if (!data)
133 break;
134 data[0] = field->report->id;
135 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700136 data, 2, WAC_CMD_RETRIES);
137 if (ret == 2) {
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400138 features->touch_max = data[1];
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700139 } else {
140 features->touch_max = 16;
141 hid_warn(hdev, "wacom_feature_mapping: "
142 "could not get HID_DG_CONTACTMAX, "
143 "defaulting to %d\n",
144 features->touch_max);
145 }
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400146 kfree(data);
147 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700148 break;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400149 case HID_DG_INPUTMODE:
150 /* Ignore if value index is out of bounds. */
151 if (usage->usage_index >= field->report_count) {
152 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
153 break;
154 }
155
156 hid_data->inputmode = field->report->id;
157 hid_data->inputmode_index = usage->usage_index;
158 break;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700159
160 case HID_UP_DIGITIZER:
161 if (field->report->id == 0x0B &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700162 (field->application == WACOM_HID_G9_PEN ||
163 field->application == WACOM_HID_G11_PEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700164 wacom->wacom_wac.mode_report = field->report->id;
165 wacom->wacom_wac.mode_value = 0;
166 }
167 break;
168
Jason Gereckec9c09582016-10-19 18:03:43 -0700169 case WACOM_HID_WD_DATAMODE:
170 wacom->wacom_wac.mode_report = field->report->id;
171 wacom->wacom_wac.mode_value = 2;
172 break;
173
Jason Gerecke8de82282016-10-19 18:03:37 -0700174 case WACOM_HID_UP_G9:
175 case WACOM_HID_UP_G11:
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700176 if (field->report->id == 0x03 &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700177 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
178 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700179 wacom->wacom_wac.mode_report = field->report->id;
180 wacom->wacom_wac.mode_value = 0;
181 }
182 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700183 }
184}
185
Chris Bagwell428f8582011-10-26 22:26:59 -0700186/*
187 * Interface Descriptor of wacom devices can be incomplete and
188 * inconsistent so wacom_features table is used to store stylus
189 * device's packet lengths, various maximum values, and tablet
190 * resolution based on product ID's.
191 *
192 * For devices that contain 2 interfaces, wacom_features table is
193 * inaccurate for the touch interface. Since the Interface Descriptor
194 * for touch interfaces has pretty complete data, this function exists
195 * to query tablet for this missing information instead of hard coding in
196 * an additional table.
197 *
198 * A typical Interface Descriptor for a stylus will contain a
199 * boot mouse application collection that is not of interest and this
200 * function will ignore it.
201 *
202 * It also contains a digitizer application collection that also is not
203 * of interest since any information it contains would be duplicate
204 * of what is in wacom_features. Usually it defines a report of an array
205 * of bytes that could be used as max length of the stylus packet returned.
206 * If it happens to define a Digitizer-Stylus Physical Collection then
207 * the X and Y logical values contain valid data but it is ignored.
208 *
209 * A typical Interface Descriptor for a touch interface will contain a
210 * Digitizer-Finger Physical Collection which will define both logical
211 * X/Y maximum as well as the physical size of tablet. Since touch
212 * interfaces haven't supported pressure or distance, this is enough
213 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700214 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700215 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
216 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700217 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700218static void wacom_usage_mapping(struct hid_device *hdev,
219 struct hid_field *field, struct hid_usage *usage)
220{
221 struct wacom *wacom = hid_get_drvdata(hdev);
222 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoiresd97a5522015-01-05 16:32:12 -0500223 bool finger = WACOM_FINGER_FIELD(field);
224 bool pen = WACOM_PEN_FIELD(field);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700225
226 /*
227 * Requiring Stylus Usage will ignore boot mouse
228 * X/Y values and some cases of invalid Digitizer X/Y
229 * values commonly reported.
230 */
Jason Gerecke042628a2015-04-30 17:51:54 -0700231 if (pen)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700232 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -0700233 else if (finger)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700234 features->device_type |= WACOM_DEVICETYPE_TOUCH;
Jason Gerecke042628a2015-04-30 17:51:54 -0700235 else
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700236 return;
237
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800238 /*
239 * Bamboo models do not support HID_DG_CONTACTMAX.
240 * And, Bamboo Pen only descriptor contains touch.
241 */
Ping Cheng3b164a02015-09-23 09:59:10 -0700242 if (features->type > BAMBOO_PT) {
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800243 /* ISDv4 touch devices at least supports one touch point */
244 if (finger && !features->touch_max)
245 features->touch_max = 1;
246 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700247
Jason Gerecke6005a132016-10-19 18:03:40 -0700248 /*
249 * ISDv4 devices which predate HID's adoption of the
250 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
251 * position instead. We can accurately detect if a
252 * usage with that value should be HID_DG_BARRELSWITCH2
253 * based on the surrounding usages, which have remained
254 * constant across generations.
255 */
256 if (features->type == HID_GENERIC &&
257 usage->hid == 0x000D0000 &&
258 field->application == HID_DG_PEN &&
259 field->physical == HID_DG_STYLUS) {
260 int i = usage->usage_index;
261
262 if (i-4 >= 0 && i+1 < field->maxusage &&
263 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
264 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
265 field->usage[i-2].hid == HID_DG_ERASER &&
266 field->usage[i-1].hid == HID_DG_INVERT &&
267 field->usage[i+1].hid == HID_DG_INRANGE) {
268 usage->hid = HID_DG_BARRELSWITCH2;
269 }
270 }
271
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700272 switch (usage->hid) {
273 case HID_GD_X:
274 features->x_max = field->logical_maximum;
275 if (finger) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700276 features->x_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700277 if ((features->type != BAMBOO_PT) &&
278 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700279 features->unit = field->unit;
280 features->unitExpo = field->unit_exponent;
281 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700282 }
283 break;
284 case HID_GD_Y:
285 features->y_max = field->logical_maximum;
286 if (finger) {
287 features->y_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700288 if ((features->type != BAMBOO_PT) &&
289 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700290 features->unit = field->unit;
291 features->unitExpo = field->unit_exponent;
292 }
293 }
294 break;
295 case HID_DG_TIPPRESSURE:
296 if (pen)
297 features->pressure_max = field->logical_maximum;
298 break;
299 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400300
301 if (features->type == HID_GENERIC)
302 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700303}
304
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800305static void wacom_post_parse_hid(struct hid_device *hdev,
306 struct wacom_features *features)
307{
308 struct wacom *wacom = hid_get_drvdata(hdev);
309 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
310
311 if (features->type == HID_GENERIC) {
312 /* Any last-minute generic device setup */
313 if (features->touch_max > 1) {
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -0700314 input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800315 INPUT_MT_DIRECT);
316 }
317 }
318}
319
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700320static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800321 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500322{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700323 struct hid_report_enum *rep_enum;
324 struct hid_report *hreport;
325 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500326
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700327 /* check features first */
328 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
329 list_for_each_entry(hreport, &rep_enum->report_list, list) {
330 for (i = 0; i < hreport->maxfield; i++) {
331 /* Ignore if report count is out of bounds. */
332 if (hreport->field[i]->report_count < 1)
333 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500334
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700335 for (j = 0; j < hreport->field[i]->maxusage; j++) {
336 wacom_feature_mapping(hdev, hreport->field[i],
337 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500338 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500339 }
340 }
341
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700342 /* now check the input usages */
343 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
344 list_for_each_entry(hreport, &rep_enum->report_list, list) {
345
346 if (!hreport->maxfield)
347 continue;
348
349 for (i = 0; i < hreport->maxfield; i++)
350 for (j = 0; j < hreport->field[i]->maxusage; j++)
351 wacom_usage_mapping(hdev, hreport->field[i],
352 hreport->field[i]->usage + j);
353 }
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800354
355 wacom_post_parse_hid(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -0500356}
357
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400358static int wacom_hid_set_device_mode(struct hid_device *hdev)
359{
360 struct wacom *wacom = hid_get_drvdata(hdev);
361 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
362 struct hid_report *r;
363 struct hid_report_enum *re;
364
365 if (hid_data->inputmode < 0)
366 return 0;
367
368 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
369 r = re->report_id_hash[hid_data->inputmode];
370 if (r) {
371 r->field[0]->value[hid_data->inputmode_index] = 2;
372 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
373 }
374 return 0;
375}
376
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700377static int wacom_set_device_mode(struct hid_device *hdev,
378 struct wacom_wac *wacom_wac)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700379{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700380 u8 *rep_data;
381 struct hid_report *r;
382 struct hid_report_enum *re;
383 int length;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700384 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700385
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700386 if (wacom_wac->mode_report < 0)
387 return 0;
388
389 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
390 r = re->report_id_hash[wacom_wac->mode_report];
391 if (!r)
392 return -EINVAL;
393
394 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700395 if (!rep_data)
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700396 return -ENOMEM;
397
398 length = hid_report_len(r);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700399
Jason Gereckefe494bc2012-10-03 17:25:35 -0700400 do {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700401 rep_data[0] = wacom_wac->mode_report;
402 rep_data[1] = wacom_wac->mode_value;
Chris Bagwell9937c022013-01-23 19:37:34 -0800403
Przemo Firszt296b7372014-08-06 14:00:38 -0700404 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
405 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700406 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700407 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700408 rep_data, length, 1);
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700409 } while (error >= 0 &&
410 rep_data[1] != wacom_wac->mode_report &&
411 limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700412
413 kfree(rep_data);
414
415 return error < 0 ? error : 0;
416}
417
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700418static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
419 struct wacom_features *features)
420{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700421 struct wacom *wacom = hid_get_drvdata(hdev);
422 int ret;
423 u8 rep_data[2];
424
425 switch (features->type) {
426 case GRAPHIRE_BT:
427 rep_data[0] = 0x03;
428 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700429 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
430 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700431
432 if (ret >= 0) {
433 rep_data[0] = speed == 0 ? 0x05 : 0x06;
434 rep_data[1] = 0x00;
435
436 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700437 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700438
439 if (ret >= 0) {
440 wacom->wacom_wac.bt_high_speed = speed;
441 return 0;
442 }
443 }
444
445 /*
446 * Note that if the raw queries fail, it's not a hard failure
447 * and it is safe to continue
448 */
449 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
450 rep_data[0], ret);
451 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700452 case INTUOS4WL:
453 if (speed == 1)
454 wacom->wacom_wac.bt_features &= ~0x20;
455 else
456 wacom->wacom_wac.bt_features |= 0x20;
457
458 rep_data[0] = 0x03;
459 rep_data[1] = wacom->wacom_wac.bt_features;
460
Przemo Firszt296b7372014-08-06 14:00:38 -0700461 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
462 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700463 if (ret >= 0)
464 wacom->wacom_wac.bt_high_speed = speed;
465 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700466 }
467
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700468 return 0;
469}
470
Jason Gereckefe494bc2012-10-03 17:25:35 -0700471/*
472 * Switch the tablet into its most-capable mode. Wacom tablets are
473 * typically configured to power-up in a mode which sends mouse-like
474 * reports to the OS. To get absolute position, pressure data, etc.
475 * from the tablet, it is necessary to switch the tablet out of this
476 * mode and into one which sends the full range of tablet data.
477 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700478static int wacom_query_tablet_data(struct hid_device *hdev,
479 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700480{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700481 struct wacom *wacom = hid_get_drvdata(hdev);
482 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
483
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700484 if (hdev->bus == BUS_BLUETOOTH)
485 return wacom_bt_query_tablet_data(hdev, 1, features);
486
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700487 if (features->type != HID_GENERIC) {
488 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
489 if (features->type > TABLETPC) {
490 /* MT Tablet PC touch */
491 wacom_wac->mode_report = 3;
492 wacom_wac->mode_value = 4;
493 } else if (features->type == WACOM_24HDT) {
494 wacom_wac->mode_report = 18;
495 wacom_wac->mode_value = 2;
496 } else if (features->type == WACOM_27QHDT) {
497 wacom_wac->mode_report = 131;
498 wacom_wac->mode_value = 2;
499 } else if (features->type == BAMBOO_PAD) {
500 wacom_wac->mode_report = 2;
501 wacom_wac->mode_value = 2;
502 }
503 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
504 if (features->type <= BAMBOO_PT) {
505 wacom_wac->mode_report = 2;
506 wacom_wac->mode_value = 2;
507 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700508 }
509 }
510
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700511 wacom_set_device_mode(hdev, wacom_wac);
512
513 if (features->type == HID_GENERIC)
514 return wacom_hid_set_device_mode(hdev);
515
Jason Gereckefe494bc2012-10-03 17:25:35 -0700516 return 0;
517}
518
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700519static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700520 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800521{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700522 struct wacom *wacom = hid_get_drvdata(hdev);
523 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800524
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700525 /* default features */
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700526 features->x_fuzz = 4;
527 features->y_fuzz = 4;
528 features->pressure_fuzz = 0;
Jason Gereckebef7e202016-04-22 14:30:53 -0700529 features->distance_fuzz = 1;
530 features->tilt_fuzz = 1;
Ping Chengec67bbe2009-12-15 00:35:24 -0800531
Chris Bagwelld3825d52012-03-25 23:26:11 -0700532 /*
533 * The wireless device HID is basic and layout conflicts with
534 * other tablets (monitor and touch interface can look like pen).
535 * Skip the query for this type and modify defaults based on
536 * interface number.
537 */
538 if (features->type == WIRELESS) {
Jason Gerecke3f14a632015-08-03 10:17:05 -0700539 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
Jason Gereckeccad85c2015-08-03 10:17:04 -0700540 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700541 else
Jason Gereckeaa86b182015-06-15 18:01:42 -0700542 features->device_type = WACOM_DEVICETYPE_NONE;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700543 return;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700544 }
545
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700546 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800547}
548
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700549struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700550 struct list_head list;
551 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700552 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700553 struct wacom_shared shared;
554};
555
556static LIST_HEAD(wacom_udev_list);
557static DEFINE_MUTEX(wacom_udev_list_lock);
558
Jason Gerecke41372d52016-08-08 12:06:30 -0700559static bool compare_device_paths(struct hid_device *hdev_a,
560 struct hid_device *hdev_b, char separator)
561{
562 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
563 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
564
565 if (n1 != n2 || n1 <= 0 || n2 <= 0)
566 return false;
567
568 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
569}
570
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700571static bool wacom_are_sibling(struct hid_device *hdev,
572 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700573{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700574 struct wacom *wacom = hid_get_drvdata(hdev);
575 struct wacom_features *features = &wacom->wacom_wac.features;
Jason Gerecke41372d52016-08-08 12:06:30 -0700576 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
577 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
578 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
579 __u32 oPid = features->oPid ? features->oPid : hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700580
Jason Gerecke41372d52016-08-08 12:06:30 -0700581 /* The defined oVid/oPid must match that of the sibling */
582 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
583 return false;
584 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
585 return false;
586
587 /*
588 * Devices with the same VID/PID must share the same physical
589 * device path, while those with different VID/PID must share
590 * the same physical parent device path.
591 */
592 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
593 if (!compare_device_paths(hdev, sibling, '/'))
594 return false;
595 } else {
596 if (!compare_device_paths(hdev, sibling, '.'))
597 return false;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700598 }
599
Jason Gerecke41372d52016-08-08 12:06:30 -0700600 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
601 if (features->type != HID_GENERIC)
602 return true;
603
604 /*
605 * Direct-input devices may not be siblings of indirect-input
606 * devices.
607 */
608 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
609 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700610 return false;
611
Jason Gerecke41372d52016-08-08 12:06:30 -0700612 /*
613 * Indirect-input devices may not be siblings of direct-input
614 * devices.
615 */
616 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
617 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700618 return false;
619
Jason Gerecke41372d52016-08-08 12:06:30 -0700620 /* Pen devices may only be siblings of touch devices */
621 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
622 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
623 return false;
624
625 /* Touch devices may only be siblings of pen devices */
626 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
627 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
628 return false;
629
630 /*
631 * No reason could be found for these two devices to NOT be
632 * siblings, so there's a good chance they ARE siblings
633 */
634 return true;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700635}
636
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700637static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700638{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700639 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700640
Jason Gerecke41372d52016-08-08 12:06:30 -0700641 /* Try to find an already-probed interface from the same device */
642 list_for_each_entry(data, &wacom_udev_list, list) {
643 if (compare_device_paths(hdev, data->dev, '/'))
644 return data;
645 }
646
647 /* Fallback to finding devices that appear to be "siblings" */
Ping Cheng4492eff2010-03-19 22:18:15 -0700648 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700649 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700650 kref_get(&data->kref);
651 return data;
652 }
653 }
654
655 return NULL;
656}
657
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200658static void wacom_release_shared_data(struct kref *kref)
659{
660 struct wacom_hdev_data *data =
661 container_of(kref, struct wacom_hdev_data, kref);
662
663 mutex_lock(&wacom_udev_list_lock);
664 list_del(&data->list);
665 mutex_unlock(&wacom_udev_list_lock);
666
667 kfree(data);
668}
669
670static void wacom_remove_shared_data(void *res)
671{
672 struct wacom *wacom = res;
673 struct wacom_hdev_data *data;
674 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
675
676 if (wacom_wac->shared) {
677 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
678 shared);
679
680 if (wacom_wac->shared->touch == wacom->hdev)
681 wacom_wac->shared->touch = NULL;
682 else if (wacom_wac->shared->pen == wacom->hdev)
683 wacom_wac->shared->pen = NULL;
684
685 kref_put(&data->kref, wacom_release_shared_data);
686 wacom_wac->shared = NULL;
687 }
688}
689
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700690static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700691{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700692 struct wacom *wacom = hid_get_drvdata(hdev);
693 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
694 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700695 int retval = 0;
696
697 mutex_lock(&wacom_udev_list_lock);
698
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700699 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700700 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700701 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700702 if (!data) {
703 retval = -ENOMEM;
704 goto out;
705 }
706
707 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700708 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700709 list_add_tail(&data->list, &wacom_udev_list);
710 }
711
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700712 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700713
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200714 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
715 if (retval) {
716 mutex_unlock(&wacom_udev_list_lock);
717 wacom_remove_shared_data(wacom);
718 return retval;
719 }
720
Jason Gereckeaa86b182015-06-15 18:01:42 -0700721 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500722 wacom_wac->shared->touch = hdev;
Jason Gereckeaa86b182015-06-15 18:01:42 -0700723 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500724 wacom_wac->shared->pen = hdev;
725
Ping Cheng4492eff2010-03-19 22:18:15 -0700726out:
727 mutex_unlock(&wacom_udev_list_lock);
728 return retval;
729}
730
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700731static int wacom_led_control(struct wacom *wacom)
732{
733 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700734 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700735 unsigned char report_id = WAC_CMD_LED_CONTROL;
736 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700737
Benjamin Tissoires97f55412016-07-13 18:06:10 +0200738 if (!hid_get_drvdata(wacom->hdev))
739 return -ENODEV;
740
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200741 if (!wacom->led.groups)
742 return -ENOTSUPP;
743
Ping Cheng912ca212014-09-10 12:41:31 -0700744 if (wacom->wacom_wac.pid) { /* wireless connected */
745 report_id = WAC_CMD_WL_LED_CONTROL;
746 buf_size = 13;
747 }
748 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700749 if (!buf)
750 return -ENOMEM;
751
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700752 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700753 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700754 /*
755 * Touch Ring and crop mark LED luminance may take on
756 * one of four values:
757 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
758 */
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200759 int ring_led = wacom->led.groups[0].select & 0x03;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700760 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
761 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700762 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700763
Ping Cheng912ca212014-09-10 12:41:31 -0700764 buf[0] = report_id;
765 if (wacom->wacom_wac.pid) {
766 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
767 buf, buf_size, WAC_CMD_RETRIES);
768 buf[0] = report_id;
769 buf[4] = led_bits;
770 } else
771 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700772 }
773 else {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200774 int led = wacom->led.groups[0].select | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700775
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700776 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
777 wacom->wacom_wac.features.type == WACOM_24HD)
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200778 led |= (wacom->led.groups[1].select << 4) | 0x40;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700779
Ping Cheng912ca212014-09-10 12:41:31 -0700780 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700781 buf[1] = led;
782 buf[2] = wacom->led.llv;
783 buf[3] = wacom->led.hlv;
784 buf[4] = wacom->led.img_lum;
785 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700786
Ping Cheng912ca212014-09-10 12:41:31 -0700787 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700788 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700789 kfree(buf);
790
791 return retval;
792}
793
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700794static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
795 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700796{
797 unsigned char *buf;
798 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700799 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700800
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700801 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700802 if (!buf)
803 return -ENOMEM;
804
805 /* Send 'start' command */
806 buf[0] = WAC_CMD_ICON_START;
807 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700808 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
809 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700810 if (retval < 0)
811 goto out;
812
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700813 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700814 buf[1] = button_id & 0x07;
815 for (i = 0; i < 4; i++) {
816 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700817 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700818
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700819 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700820 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700821 if (retval < 0)
822 break;
823 }
824
825 /* Send 'stop' */
826 buf[0] = WAC_CMD_ICON_START;
827 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700828 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
829 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700830
831out:
832 kfree(buf);
833 return retval;
834}
835
Ping Cheng09e7d942011-10-04 23:51:14 -0700836static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700837 const char *buf, size_t count)
838{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800839 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700840 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700841 unsigned int id;
842 int err;
843
844 err = kstrtouint(buf, 10, &id);
845 if (err)
846 return err;
847
848 mutex_lock(&wacom->lock);
849
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200850 wacom->led.groups[set_id].select = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700851 err = wacom_led_control(wacom);
852
853 mutex_unlock(&wacom->lock);
854
855 return err < 0 ? err : count;
856}
857
Ping Cheng09e7d942011-10-04 23:51:14 -0700858#define DEVICE_LED_SELECT_ATTR(SET_ID) \
859static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
860 struct device_attribute *attr, const char *buf, size_t count) \
861{ \
862 return wacom_led_select_store(dev, SET_ID, buf, count); \
863} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700864static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
865 struct device_attribute *attr, char *buf) \
866{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800867 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700868 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700869 return scnprintf(buf, PAGE_SIZE, "%d\n", \
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200870 wacom->led.groups[SET_ID].select); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700871} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700872static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700873 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700874 wacom_led##SET_ID##_select_store)
875
876DEVICE_LED_SELECT_ATTR(0);
877DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700878
879static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
880 const char *buf, size_t count)
881{
882 unsigned int value;
883 int err;
884
885 err = kstrtouint(buf, 10, &value);
886 if (err)
887 return err;
888
889 mutex_lock(&wacom->lock);
890
891 *dest = value & 0x7f;
892 err = wacom_led_control(wacom);
893
894 mutex_unlock(&wacom->lock);
895
896 return err < 0 ? err : count;
897}
898
899#define DEVICE_LUMINANCE_ATTR(name, field) \
900static ssize_t wacom_##name##_luminance_store(struct device *dev, \
901 struct device_attribute *attr, const char *buf, size_t count) \
902{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800903 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700904 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700905 \
906 return wacom_luminance_store(wacom, &wacom->led.field, \
907 buf, count); \
908} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700909static ssize_t wacom_##name##_luminance_show(struct device *dev, \
910 struct device_attribute *attr, char *buf) \
911{ \
912 struct wacom *wacom = dev_get_drvdata(dev); \
913 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
914} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700915static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700916 wacom_##name##_luminance_show, \
917 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700918
919DEVICE_LUMINANCE_ATTR(status0, llv);
920DEVICE_LUMINANCE_ATTR(status1, hlv);
921DEVICE_LUMINANCE_ATTR(buttons, img_lum);
922
923static ssize_t wacom_button_image_store(struct device *dev, int button_id,
924 const char *buf, size_t count)
925{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800926 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700927 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700928 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700929 unsigned len;
930 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700931
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700932 if (hdev->bus == BUS_BLUETOOTH) {
933 len = 256;
934 xfer_id = WAC_CMD_ICON_BT_XFER;
935 } else {
936 len = 1024;
937 xfer_id = WAC_CMD_ICON_XFER;
938 }
939
940 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700941 return -EINVAL;
942
943 mutex_lock(&wacom->lock);
944
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700945 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700946
947 mutex_unlock(&wacom->lock);
948
949 return err < 0 ? err : count;
950}
951
952#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
953static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
954 struct device_attribute *attr, const char *buf, size_t count) \
955{ \
956 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
957} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700958static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700959 NULL, wacom_btnimg##BUTTON_ID##_store)
960
961DEVICE_BTNIMG_ATTR(0);
962DEVICE_BTNIMG_ATTR(1);
963DEVICE_BTNIMG_ATTR(2);
964DEVICE_BTNIMG_ATTR(3);
965DEVICE_BTNIMG_ATTR(4);
966DEVICE_BTNIMG_ATTR(5);
967DEVICE_BTNIMG_ATTR(6);
968DEVICE_BTNIMG_ATTR(7);
969
Ping Cheng09e7d942011-10-04 23:51:14 -0700970static struct attribute *cintiq_led_attrs[] = {
971 &dev_attr_status_led0_select.attr,
972 &dev_attr_status_led1_select.attr,
973 NULL
974};
975
976static struct attribute_group cintiq_led_attr_group = {
977 .name = "wacom_led",
978 .attrs = cintiq_led_attrs,
979};
980
981static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700982 &dev_attr_status0_luminance.attr,
983 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700984 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700985 &dev_attr_buttons_luminance.attr,
986 &dev_attr_button0_rawimg.attr,
987 &dev_attr_button1_rawimg.attr,
988 &dev_attr_button2_rawimg.attr,
989 &dev_attr_button3_rawimg.attr,
990 &dev_attr_button4_rawimg.attr,
991 &dev_attr_button5_rawimg.attr,
992 &dev_attr_button6_rawimg.attr,
993 &dev_attr_button7_rawimg.attr,
994 NULL
995};
996
Ping Cheng09e7d942011-10-04 23:51:14 -0700997static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700998 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700999 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001000};
1001
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001002static struct attribute *intuos5_led_attrs[] = {
1003 &dev_attr_status0_luminance.attr,
1004 &dev_attr_status_led0_select.attr,
1005 NULL
1006};
1007
1008static struct attribute_group intuos5_led_attr_group = {
1009 .name = "wacom_led",
1010 .attrs = intuos5_led_attrs,
1011};
1012
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001013struct wacom_sysfs_group_devres {
1014 struct attribute_group *group;
1015 struct kobject *root;
1016};
1017
1018static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1019{
1020 struct wacom_sysfs_group_devres *devres = res;
1021 struct kobject *kobj = devres->root;
1022
1023 dev_dbg(dev, "%s: dropping reference to %s\n",
1024 __func__, devres->group->name);
1025 sysfs_remove_group(kobj, devres->group);
1026}
1027
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001028static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1029 struct kobject *root,
1030 struct attribute_group *group)
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001031{
1032 struct wacom_sysfs_group_devres *devres;
1033 int error;
1034
1035 devres = devres_alloc(wacom_devm_sysfs_group_release,
1036 sizeof(struct wacom_sysfs_group_devres),
1037 GFP_KERNEL);
1038 if (!devres)
1039 return -ENOMEM;
1040
1041 devres->group = group;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001042 devres->root = root;
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001043
1044 error = sysfs_create_group(devres->root, group);
1045 if (error)
1046 return error;
1047
1048 devres_add(&wacom->hdev->dev, devres);
1049
1050 return 0;
1051}
1052
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001053static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1054 struct attribute_group *group)
1055{
1056 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1057 group);
1058}
1059
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001060enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001061{
1062 struct wacom *wacom = led->wacom;
1063
1064 if (wacom->led.max_hlv)
1065 return led->hlv * LED_FULL / wacom->led.max_hlv;
1066
1067 if (wacom->led.max_llv)
1068 return led->llv * LED_FULL / wacom->led.max_llv;
1069
1070 /* device doesn't support brightness tuning */
1071 return LED_FULL;
1072}
1073
1074static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1075{
1076 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1077 struct wacom *wacom = led->wacom;
1078
1079 if (wacom->led.groups[led->group].select != led->id)
1080 return LED_OFF;
1081
1082 return wacom_leds_brightness_get(led);
1083}
1084
1085static int wacom_led_brightness_set(struct led_classdev *cdev,
1086 enum led_brightness brightness)
1087{
1088 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1089 struct wacom *wacom = led->wacom;
1090 int error;
1091
1092 mutex_lock(&wacom->lock);
1093
1094 if (!wacom->led.groups || (brightness == LED_OFF &&
1095 wacom->led.groups[led->group].select != led->id)) {
1096 error = 0;
1097 goto out;
1098 }
1099
1100 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1101 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1102
1103 wacom->led.groups[led->group].select = led->id;
1104
1105 error = wacom_led_control(wacom);
1106
1107out:
1108 mutex_unlock(&wacom->lock);
1109
1110 return error;
1111}
1112
1113static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1114 enum led_brightness brightness)
1115{
1116}
1117
1118static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1119 struct wacom_led *led, unsigned int group,
1120 unsigned int id, bool read_only)
1121{
1122 int error;
1123 char *name;
1124
1125 name = devm_kasprintf(dev, GFP_KERNEL,
1126 "%s::wacom-%d.%d",
1127 dev_name(dev),
1128 group,
1129 id);
1130 if (!name)
1131 return -ENOMEM;
1132
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001133 if (!read_only) {
1134 led->trigger.name = name;
1135 error = devm_led_trigger_register(dev, &led->trigger);
1136 if (error) {
1137 hid_err(wacom->hdev,
1138 "failed to register LED trigger %s: %d\n",
1139 led->cdev.name, error);
1140 return error;
1141 }
1142 }
1143
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001144 led->group = group;
1145 led->id = id;
1146 led->wacom = wacom;
1147 led->llv = wacom->led.llv;
1148 led->hlv = wacom->led.hlv;
1149 led->cdev.name = name;
1150 led->cdev.max_brightness = LED_FULL;
1151 led->cdev.flags = LED_HW_PLUGGABLE;
1152 led->cdev.brightness_get = __wacom_led_brightness_get;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001153 if (!read_only) {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001154 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001155 led->cdev.default_trigger = led->cdev.name;
1156 } else {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001157 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001158 }
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001159
1160 error = devm_led_classdev_register(dev, &led->cdev);
1161 if (error) {
1162 hid_err(wacom->hdev,
1163 "failed to register LED %s: %d\n",
1164 led->cdev.name, error);
1165 led->cdev.name = NULL;
1166 return error;
1167 }
1168
1169 return 0;
1170}
1171
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001172static void wacom_led_groups_release_one(void *data)
1173{
1174 struct wacom_group_leds *group = data;
1175
1176 devres_release_group(group->dev, group);
1177}
1178
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001179static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1180 struct wacom *wacom,
1181 int group_id, int count,
1182 bool read_only)
1183{
1184 struct wacom_led *leds;
1185 int i, error;
1186
1187 if (group_id >= wacom->led.count || count <= 0)
1188 return -EINVAL;
1189
1190 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1191 return -ENOMEM;
1192
1193 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1194 if (!leds) {
1195 error = -ENOMEM;
1196 goto err;
1197 }
1198
1199 wacom->led.groups[group_id].leds = leds;
1200 wacom->led.groups[group_id].count = count;
1201
1202 for (i = 0; i < count; i++) {
1203 error = wacom_led_register_one(dev, wacom, &leds[i],
1204 group_id, i, read_only);
1205 if (error)
1206 goto err;
1207 }
1208
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001209 wacom->led.groups[group_id].dev = dev;
1210
1211 devres_close_group(dev, &wacom->led.groups[group_id]);
1212
1213 /*
1214 * There is a bug (?) in devm_led_classdev_register() in which its
1215 * increments the refcount of the parent. If the parent is an input
1216 * device, that means the ref count never reaches 0 when
1217 * devm_input_device_release() gets called.
1218 * This means that the LEDs are still there after disconnect.
1219 * Manually force the release of the group so that the leds are released
1220 * once we are done using them.
1221 */
1222 error = devm_add_action_or_reset(&wacom->hdev->dev,
1223 wacom_led_groups_release_one,
1224 &wacom->led.groups[group_id]);
1225 if (error)
1226 return error;
1227
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001228 return 0;
1229
1230err:
1231 devres_release_group(dev, &wacom->led.groups[group_id]);
1232 return error;
1233}
1234
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001235struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1236 unsigned int id)
1237{
1238 struct wacom_group_leds *group;
1239
1240 if (group_id >= wacom->led.count)
1241 return NULL;
1242
1243 group = &wacom->led.groups[group_id];
1244
1245 if (!group->leds)
1246 return NULL;
1247
1248 id %= group->count;
1249
1250 return &group->leds[id];
1251}
1252
1253/**
1254 * wacom_led_next: gives the next available led with a wacom trigger.
1255 *
1256 * returns the next available struct wacom_led which has its default trigger
1257 * or the current one if none is available.
1258 */
1259struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1260{
1261 struct wacom_led *next_led;
1262 int group, next;
1263
1264 if (!wacom || !cur)
1265 return NULL;
1266
1267 group = cur->group;
1268 next = cur->id;
1269
1270 do {
1271 next_led = wacom_led_find(wacom, group, ++next);
1272 if (!next_led || next_led == cur)
1273 return next_led;
1274 } while (next_led->cdev.trigger != &next_led->trigger);
1275
1276 return next_led;
1277}
1278
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001279static void wacom_led_groups_release(void *data)
1280{
1281 struct wacom *wacom = data;
1282
1283 wacom->led.groups = NULL;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001284 wacom->led.count = 0;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001285}
1286
1287static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1288{
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001289 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001290 struct wacom_group_leds *groups;
1291 int error;
1292
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001293 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001294 GFP_KERNEL);
1295 if (!groups)
1296 return -ENOMEM;
1297
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001298 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001299 if (error)
1300 return error;
1301
1302 wacom->led.groups = groups;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001303 wacom->led.count = count;
1304
1305 return 0;
1306}
1307
1308static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1309 int led_per_group, bool read_only)
1310{
1311 struct device *dev;
1312 int i, error;
1313
1314 if (!wacom->wacom_wac.pad_input)
1315 return -EINVAL;
1316
1317 dev = &wacom->wacom_wac.pad_input->dev;
1318
1319 error = wacom_led_groups_allocate(wacom, group_count);
1320 if (error)
1321 return error;
1322
1323 for (i = 0; i < group_count; i++) {
1324 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1325 led_per_group,
1326 read_only);
1327 if (error)
1328 return error;
1329 }
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001330
1331 return 0;
1332}
1333
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001334static int wacom_initialize_leds(struct wacom *wacom)
1335{
1336 int error;
1337
Jason Gerecke862cf552015-06-15 18:01:43 -07001338 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1339 return 0;
1340
Ping Cheng09e7d942011-10-04 23:51:14 -07001341 /* Initialize default values */
1342 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -07001343 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -07001344 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -07001345 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -07001346 case INTUOS4L:
Ping Chengf4fa9a62011-10-04 23:49:42 -07001347 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001348 wacom->led.hlv = 20;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001349 wacom->led.max_llv = 127;
1350 wacom->led.max_hlv = 127;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001351 wacom->led.img_lum = 10;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001352
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001353 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001354 if (error) {
1355 hid_err(wacom->hdev,
1356 "cannot create leds err: %d\n", error);
1357 return error;
1358 }
1359
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001360 error = wacom_devm_sysfs_create_group(wacom,
1361 &intuos4_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001362 break;
1363
Jason Gerecke246835f2011-12-12 00:12:04 -08001364 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -07001365 case WACOM_21UX2:
Ping Cheng09e7d942011-10-04 23:51:14 -07001366 wacom->led.llv = 0;
1367 wacom->led.hlv = 0;
1368 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001369
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001370 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001371 if (error) {
1372 hid_err(wacom->hdev,
1373 "cannot create leds err: %d\n", error);
1374 return error;
1375 }
1376
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001377 error = wacom_devm_sysfs_create_group(wacom,
1378 &cintiq_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001379 break;
1380
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001381 case INTUOS5S:
1382 case INTUOS5:
1383 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001384 case INTUOSPS:
1385 case INTUOSPM:
1386 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -07001387 wacom->led.llv = 32;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001388 wacom->led.max_llv = 96;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001389
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001390 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001391 if (error) {
1392 hid_err(wacom->hdev,
1393 "cannot create leds err: %d\n", error);
1394 return error;
1395 }
1396
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001397 error = wacom_devm_sysfs_create_group(wacom,
1398 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001399 break;
1400
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001401 case REMOTE:
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001402 wacom->led.llv = 255;
1403 wacom->led.max_llv = 255;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001404 error = wacom_led_groups_allocate(wacom, 5);
1405 if (error) {
1406 hid_err(wacom->hdev,
1407 "cannot create leds err: %d\n", error);
1408 return error;
1409 }
1410 return 0;
1411
Ping Cheng09e7d942011-10-04 23:51:14 -07001412 default:
1413 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001414 }
1415
Ping Cheng09e7d942011-10-04 23:51:14 -07001416 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -07001417 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -07001418 "cannot create sysfs group err: %d\n", error);
1419 return error;
1420 }
1421 wacom_led_control(wacom);
1422
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001423 return 0;
1424}
1425
Chris Bagwella1d552c2012-03-25 23:26:30 -07001426static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001427 POWER_SUPPLY_PROP_MODEL_NAME,
Jason Gerecke71fa6412015-03-11 10:25:41 -07001428 POWER_SUPPLY_PROP_PRESENT,
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001429 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001430 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001431 POWER_SUPPLY_PROP_CAPACITY
1432};
1433
1434static int wacom_battery_get_property(struct power_supply *psy,
1435 enum power_supply_property psp,
1436 union power_supply_propval *val)
1437{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001438 struct wacom_battery *battery = power_supply_get_drvdata(psy);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001439 int ret = 0;
1440
1441 switch (psp) {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001442 case POWER_SUPPLY_PROP_MODEL_NAME:
1443 val->strval = battery->wacom->wacom_wac.name;
1444 break;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001445 case POWER_SUPPLY_PROP_PRESENT:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001446 val->intval = battery->bat_connected;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001447 break;
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001448 case POWER_SUPPLY_PROP_SCOPE:
1449 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1450 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001451 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001452 val->intval = battery->battery_capacity;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001453 break;
1454 case POWER_SUPPLY_PROP_STATUS:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001455 if (battery->bat_charging)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001456 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001457 else if (battery->battery_capacity == 100 &&
1458 battery->ps_connected)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001459 val->intval = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001460 else if (battery->ps_connected)
Jason Gereckeb0882cb2015-03-06 11:47:43 -08001461 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001462 else
1463 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001464 break;
1465 default:
1466 ret = -EINVAL;
1467 break;
1468 }
1469
1470 return ret;
1471}
1472
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001473static int __wacom_initialize_battery(struct wacom *wacom,
1474 struct wacom_battery *battery)
Chris Bagwella1d552c2012-03-25 23:26:30 -07001475{
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001476 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001477 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001478 struct power_supply_config psy_cfg = { .drv_data = battery, };
Benjamin Tissoires136ae5e2016-07-13 18:06:16 +02001479 struct power_supply *ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001480 struct power_supply_desc *bat_desc = &battery->bat_desc;
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001481 unsigned long n;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001482 int error;
1483
1484 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1485 return -ENOMEM;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001486
Benjamin Tissoires99569532016-07-13 18:06:17 +02001487 battery->wacom = wacom;
1488
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001489 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001490
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001491 bat_desc->properties = wacom_battery_props;
1492 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1493 bat_desc->get_property = wacom_battery_get_property;
1494 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1495 bat_desc->name = battery->bat_name;
Benjamin Tissoires96983292016-07-13 18:06:15 +02001496 bat_desc->type = POWER_SUPPLY_TYPE_USB;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001497 bat_desc->use_for_apm = 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001498
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001499 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1500 if (IS_ERR(ps_bat)) {
1501 error = PTR_ERR(ps_bat);
1502 goto err;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001503 }
1504
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001505 power_supply_powers(ps_bat, &wacom->hdev->dev);
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001506
1507 battery->battery = ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001508
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001509 devres_close_group(dev, bat_desc);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001510 return 0;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001511
1512err:
1513 devres_release_group(dev, bat_desc);
1514 return error;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001515}
1516
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001517static int wacom_initialize_battery(struct wacom *wacom)
1518{
1519 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1520 return __wacom_initialize_battery(wacom, &wacom->battery);
1521
1522 return 0;
1523}
1524
Chris Bagwella1d552c2012-03-25 23:26:30 -07001525static void wacom_destroy_battery(struct wacom *wacom)
1526{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001527 if (wacom->battery.battery) {
1528 devres_release_group(&wacom->hdev->dev,
1529 &wacom->battery.bat_desc);
1530 wacom->battery.battery = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001531 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001532}
1533
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001534static ssize_t wacom_show_speed(struct device *dev,
1535 struct device_attribute
1536 *attr, char *buf)
1537{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001538 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001539 struct wacom *wacom = hid_get_drvdata(hdev);
1540
1541 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1542}
1543
1544static ssize_t wacom_store_speed(struct device *dev,
1545 struct device_attribute *attr,
1546 const char *buf, size_t count)
1547{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001548 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001549 struct wacom *wacom = hid_get_drvdata(hdev);
1550 u8 new_speed;
1551
1552 if (kstrtou8(buf, 0, &new_speed))
1553 return -EINVAL;
1554
1555 if (new_speed != 0 && new_speed != 1)
1556 return -EINVAL;
1557
1558 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1559
1560 return count;
1561}
1562
Ping Chenge0984bc2014-09-10 12:40:05 -07001563static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001564 wacom_show_speed, wacom_store_speed);
1565
Aaron Skomra72b236d2015-08-20 16:05:17 -07001566
1567static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1568 struct kobj_attribute *kattr,
1569 char *buf, int index)
1570{
Geliang Tang2cf83832015-12-27 17:25:24 +08001571 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001572 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001573 struct wacom *wacom = hid_get_drvdata(hdev);
1574 u8 mode;
1575
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001576 mode = wacom->led.groups[index].select;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001577 if (mode >= 0 && mode < 3)
1578 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
1579 else
1580 return snprintf(buf, PAGE_SIZE, "%d\n", -1);
1581}
1582
1583#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1584static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1585 struct kobj_attribute *kattr, char *buf) \
1586{ \
1587 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1588} \
1589static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1590 .attr = {.name = "remote_mode", \
1591 .mode = DEV_ATTR_RO_PERM}, \
1592 .show = wacom_show_remote##SET_ID##_mode, \
1593}; \
1594static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1595 &remote##SET_ID##_mode_attr.attr, \
1596 NULL \
1597}; \
1598static struct attribute_group remote##SET_ID##_serial_group = { \
1599 .name = NULL, \
1600 .attrs = remote##SET_ID##_serial_attrs, \
1601}
1602
1603DEVICE_EKR_ATTR_GROUP(0);
1604DEVICE_EKR_ATTR_GROUP(1);
1605DEVICE_EKR_ATTR_GROUP(2);
1606DEVICE_EKR_ATTR_GROUP(3);
1607DEVICE_EKR_ATTR_GROUP(4);
1608
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02001609static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1610 int index)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001611{
1612 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001613 struct wacom_remote *remote = wacom->remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001614
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001615 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001616 GFP_KERNEL,
1617 "%d", serial);
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001618 if (!remote->remotes[index].group.name)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001619 return -ENOMEM;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001620
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001621 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001622 &remote->remotes[index].group);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001623 if (error) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001624 remote->remotes[index].group.name = NULL;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001625 hid_err(wacom->hdev,
1626 "cannot create sysfs group err: %d\n", error);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001627 return error;
1628 }
1629
1630 return 0;
1631}
1632
Aaron Skomra72b236d2015-08-20 16:05:17 -07001633static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1634{
1635 const size_t buf_size = 2;
1636 unsigned char *buf;
1637 int retval;
1638
1639 buf = kzalloc(buf_size, GFP_KERNEL);
1640 if (!buf)
1641 return -ENOMEM;
1642
1643 buf[0] = WAC_CMD_DELETE_PAIRING;
1644 buf[1] = selector;
1645
1646 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1647 buf_size, WAC_CMD_RETRIES);
1648 kfree(buf);
1649
1650 return retval;
1651}
1652
1653static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1654 struct kobj_attribute *attr,
1655 const char *buf, size_t count)
1656{
1657 unsigned char selector = 0;
Geliang Tang2cf83832015-12-27 17:25:24 +08001658 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001659 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001660 struct wacom *wacom = hid_get_drvdata(hdev);
1661 int err;
1662
1663 if (!strncmp(buf, "*\n", 2)) {
1664 selector = WAC_CMD_UNPAIR_ALL;
1665 } else {
1666 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1667 buf);
1668 return -1;
1669 }
1670
1671 mutex_lock(&wacom->lock);
1672
1673 err = wacom_cmd_unpair_remote(wacom, selector);
1674 mutex_unlock(&wacom->lock);
1675
1676 return err < 0 ? err : count;
1677}
1678
1679static struct kobj_attribute unpair_remote_attr = {
1680 .attr = {.name = "unpair_remote", .mode = 0200},
1681 .store = wacom_store_unpair_remote,
1682};
1683
1684static const struct attribute *remote_unpair_attrs[] = {
1685 &unpair_remote_attr.attr,
1686 NULL
1687};
1688
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001689static void wacom_remotes_destroy(void *data)
1690{
1691 struct wacom *wacom = data;
1692 struct wacom_remote *remote = wacom->remote;
1693
1694 if (!remote)
1695 return;
1696
1697 kobject_put(remote->remote_dir);
1698 kfifo_free(&remote->remote_fifo);
1699 wacom->remote = NULL;
1700}
1701
1702static int wacom_initialize_remotes(struct wacom *wacom)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001703{
1704 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001705 struct wacom_remote *remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001706 int i;
1707
1708 if (wacom->wacom_wac.features.type != REMOTE)
1709 return 0;
1710
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001711 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1712 GFP_KERNEL);
1713 if (!remote)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001714 return -ENOMEM;
1715
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001716 wacom->remote = remote;
1717
1718 spin_lock_init(&remote->remote_lock);
1719
1720 error = kfifo_alloc(&remote->remote_fifo,
1721 5 * sizeof(struct wacom_remote_data),
1722 GFP_KERNEL);
1723 if (error) {
1724 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1725 return -ENOMEM;
1726 }
1727
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001728 remote->remotes[0].group = remote0_serial_group;
1729 remote->remotes[1].group = remote1_serial_group;
1730 remote->remotes[2].group = remote2_serial_group;
1731 remote->remotes[3].group = remote3_serial_group;
1732 remote->remotes[4].group = remote4_serial_group;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001733
1734 remote->remote_dir = kobject_create_and_add("wacom_remote",
1735 &wacom->hdev->dev.kobj);
1736 if (!remote->remote_dir)
1737 return -ENOMEM;
1738
1739 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001740
1741 if (error) {
1742 hid_err(wacom->hdev,
1743 "cannot create sysfs group err: %d\n", error);
1744 return error;
1745 }
1746
1747 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001748 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001749 remote->remotes[i].serial = 0;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001750 }
1751
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001752 error = devm_add_action_or_reset(&wacom->hdev->dev,
1753 wacom_remotes_destroy, wacom);
1754 if (error)
1755 return error;
1756
Aaron Skomra72b236d2015-08-20 16:05:17 -07001757 return 0;
1758}
1759
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001760static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001761{
1762 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001763 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001764 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001765
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001766 input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001767 if (!input_dev)
1768 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001769
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001770 input_dev->name = wacom_wac->features.name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001771 input_dev->phys = hdev->phys;
1772 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001773 input_dev->open = wacom_open;
1774 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001775 input_dev->uniq = hdev->uniq;
1776 input_dev->id.bustype = hdev->bus;
1777 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001778 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001779 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001780 input_set_drvdata(input_dev, wacom);
1781
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001782 return input_dev;
1783}
1784
Jason Gerecked9f2d202015-07-13 18:03:44 -07001785static int wacom_allocate_inputs(struct wacom *wacom)
1786{
1787 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1788
1789 wacom_wac->pen_input = wacom_allocate_input(wacom);
1790 wacom_wac->touch_input = wacom_allocate_input(wacom);
1791 wacom_wac->pad_input = wacom_allocate_input(wacom);
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001792 if (!wacom_wac->pen_input ||
1793 !wacom_wac->touch_input ||
1794 !wacom_wac->pad_input)
Jason Gerecked9f2d202015-07-13 18:03:44 -07001795 return -ENOMEM;
Jason Gerecked9f2d202015-07-13 18:03:44 -07001796
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001797 wacom_wac->pen_input->name = wacom_wac->pen_name;
Jason Gerecked9f2d202015-07-13 18:03:44 -07001798 wacom_wac->touch_input->name = wacom_wac->touch_name;
1799 wacom_wac->pad_input->name = wacom_wac->pad_name;
1800
1801 return 0;
1802}
1803
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001804static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001805{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001806 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001807 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Jason Gerecke2636a3f2015-06-15 18:01:44 -07001808 int error = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001809
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001810 pen_input_dev = wacom_wac->pen_input;
1811 touch_input_dev = wacom_wac->touch_input;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001812 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001813
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001814 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001815 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001816
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001817 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1818 if (error) {
1819 /* no pen in use on this interface */
1820 input_free_device(pen_input_dev);
1821 wacom_wac->pen_input = NULL;
1822 pen_input_dev = NULL;
1823 } else {
1824 error = input_register_device(pen_input_dev);
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001825 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001826 goto fail;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001827 }
1828
1829 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1830 if (error) {
1831 /* no touch in use on this interface */
1832 input_free_device(touch_input_dev);
1833 wacom_wac->touch_input = NULL;
1834 touch_input_dev = NULL;
1835 } else {
1836 error = input_register_device(touch_input_dev);
1837 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001838 goto fail;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001839 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001840
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001841 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1842 if (error) {
1843 /* no pad in use on this interface */
1844 input_free_device(pad_input_dev);
1845 wacom_wac->pad_input = NULL;
1846 pad_input_dev = NULL;
1847 } else {
1848 error = input_register_device(pad_input_dev);
1849 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001850 goto fail;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001851 }
1852
Ping Cheng19635182012-04-29 21:09:18 -07001853 return 0;
1854
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001855fail:
1856 wacom_wac->pad_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001857 wacom_wac->touch_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001858 wacom_wac->pen_input = NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001859 return error;
1860}
1861
Jason Gerecke0be01712015-08-05 15:44:53 -07001862/*
1863 * Not all devices report physical dimensions from HID.
1864 * Compute the default from hardcoded logical dimension
1865 * and resolution before driver overwrites them.
1866 */
1867static void wacom_set_default_phy(struct wacom_features *features)
1868{
1869 if (features->x_resolution) {
1870 features->x_phy = (features->x_max * 100) /
1871 features->x_resolution;
1872 features->y_phy = (features->y_max * 100) /
1873 features->y_resolution;
1874 }
1875}
1876
1877static void wacom_calculate_res(struct wacom_features *features)
1878{
1879 /* set unit to "100th of a mm" for devices not reported by HID */
1880 if (!features->unit) {
1881 features->unit = 0x11;
1882 features->unitExpo = -3;
1883 }
1884
1885 features->x_resolution = wacom_calc_hid_res(features->x_max,
1886 features->x_phy,
1887 features->unit,
1888 features->unitExpo);
1889 features->y_resolution = wacom_calc_hid_res(features->y_max,
1890 features->y_phy,
1891 features->unit,
1892 features->unitExpo);
1893}
1894
Jason Gereckefce99572015-03-06 11:47:40 -08001895void wacom_battery_work(struct work_struct *work)
1896{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02001897 struct wacom *wacom = container_of(work, struct wacom, battery_work);
Jason Gereckefce99572015-03-06 11:47:40 -08001898
1899 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001900 !wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001901 wacom_initialize_battery(wacom);
1902 }
1903 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001904 wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001905 wacom_destroy_battery(wacom);
1906 }
1907}
1908
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001909static size_t wacom_compute_pktlen(struct hid_device *hdev)
1910{
1911 struct hid_report_enum *report_enum;
1912 struct hid_report *report;
1913 size_t size = 0;
1914
1915 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1916
1917 list_for_each_entry(report, &report_enum->report_list, list) {
Mathieu Magnaudetdabb05c62014-11-27 16:02:36 +01001918 size_t report_size = hid_report_len(report);
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001919 if (report_size > size)
1920 size = report_size;
1921 }
1922
1923 return size;
1924}
1925
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001926static void wacom_update_name(struct wacom *wacom, const char *suffix)
Ping Chengc24eab42015-04-24 15:32:51 -07001927{
1928 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1929 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke44b52502015-06-15 18:01:41 -07001930 char name[WACOM_NAME_MAX];
Ping Chengc24eab42015-04-24 15:32:51 -07001931
1932 /* Generic devices name unspecified */
1933 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1934 if (strstr(wacom->hdev->name, "Wacom") ||
1935 strstr(wacom->hdev->name, "wacom") ||
1936 strstr(wacom->hdev->name, "WACOM")) {
1937 /* name is in HID descriptor, use it */
Jason Gerecke44b52502015-06-15 18:01:41 -07001938 strlcpy(name, wacom->hdev->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001939
1940 /* strip out excess whitespaces */
1941 while (1) {
Jason Gerecke44b52502015-06-15 18:01:41 -07001942 char *gap = strstr(name, " ");
Ping Chengc24eab42015-04-24 15:32:51 -07001943 if (gap == NULL)
1944 break;
1945 /* shift everything including the terminator */
1946 memmove(gap, gap+1, strlen(gap));
1947 }
Jason Gereckef2209d42016-10-19 18:03:41 -07001948
1949 /* strip off excessive prefixing */
1950 if (strstr(name, "Wacom Co.,Ltd. Wacom ") == name) {
1951 int n = strlen(name);
1952 int x = strlen("Wacom Co.,Ltd. ");
1953 memmove(name, name+x, n-x+1);
1954 }
1955 if (strstr(name, "Wacom Co., Ltd. Wacom ") == name) {
1956 int n = strlen(name);
1957 int x = strlen("Wacom Co., Ltd. ");
1958 memmove(name, name+x, n-x+1);
1959 }
1960
Ping Chengc24eab42015-04-24 15:32:51 -07001961 /* get rid of trailing whitespace */
Jason Gerecke44b52502015-06-15 18:01:41 -07001962 if (name[strlen(name)-1] == ' ')
1963 name[strlen(name)-1] = '\0';
Ping Chengc24eab42015-04-24 15:32:51 -07001964 } else {
1965 /* no meaningful name retrieved. use product ID */
Jason Gerecke44b52502015-06-15 18:01:41 -07001966 snprintf(name, sizeof(name),
Ping Chengc24eab42015-04-24 15:32:51 -07001967 "%s %X", features->name, wacom->hdev->product);
1968 }
1969 } else {
Jason Gerecke44b52502015-06-15 18:01:41 -07001970 strlcpy(name, features->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001971 }
1972
Benjamin Tissoires99569532016-07-13 18:06:17 +02001973 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
1974 name, suffix);
1975
Ping Chengc24eab42015-04-24 15:32:51 -07001976 /* Append the device type to the name */
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001977 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001978 "%s%s Pen", name, suffix);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001979 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001980 "%s%s Finger", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07001981 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001982 "%s%s Pad", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07001983}
1984
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02001985static void wacom_release_resources(struct wacom *wacom)
1986{
1987 struct hid_device *hdev = wacom->hdev;
1988
1989 if (!wacom->resources)
1990 return;
1991
1992 devres_release_group(&hdev->dev, wacom);
1993
1994 wacom->resources = false;
1995
1996 wacom->wacom_wac.pen_input = NULL;
1997 wacom->wacom_wac.touch_input = NULL;
1998 wacom->wacom_wac.pad_input = NULL;
1999}
2000
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002001static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
Ping Cheng3bea7332006-07-13 18:01:36 -07002002{
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002003 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2004 struct wacom_features *features = &wacom_wac->features;
2005 struct hid_device *hdev = wacom->hdev;
Jason Childse33da8a2010-02-17 22:38:31 -08002006 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002007 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07002008
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002009 features->pktlen = wacom_compute_pktlen(hdev);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002010 if (features->pktlen > WACOM_PKGLEN_MAX)
2011 return -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07002012
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002013 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2014 return -ENOMEM;
2015
2016 wacom->resources = true;
2017
Jason Gerecke3f14a632015-08-03 10:17:05 -07002018 error = wacom_allocate_inputs(wacom);
2019 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002020 goto fail;
Benjamin Tissoires494078b2014-09-23 12:08:07 -04002021
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002022 /*
2023 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2024 * into debug mode for the touch part.
2025 * We ignore the other interfaces.
2026 */
2027 if (features->type == BAMBOO_PAD) {
2028 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2029 features->type = HID_GENERIC;
2030 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2031 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2032 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002033 goto fail;
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002034 }
2035 }
2036
Ping Cheng401d7d12013-07-28 00:38:54 -07002037 /* set the default size in case we do not get them from hid */
2038 wacom_set_default_phy(features);
2039
Ping Chengf393ee22012-04-29 21:09:17 -07002040 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07002041 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng42f4f272015-04-15 16:53:54 -07002042 wacom_setup_device_quirks(wacom);
Jason Gerecke042628a2015-04-30 17:51:54 -07002043
Jason Gereckeaa86b182015-06-15 18:01:42 -07002044 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2045 features->type != WIRELESS) {
Jason Gerecke8e116d32015-04-30 17:51:55 -07002046 error = features->type == HID_GENERIC ? -ENODEV : 0;
2047
Jason Gerecke042628a2015-04-30 17:51:54 -07002048 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
Jason Gerecke8e116d32015-04-30 17:51:55 -07002049 hdev->name,
2050 error ? "Ignoring" : "Assuming pen");
2051
2052 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002053 goto fail;
Jason Gerecke042628a2015-04-30 17:51:54 -07002054
Jason Gereckeaa86b182015-06-15 18:01:42 -07002055 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -07002056 }
2057
Ping Cheng401d7d12013-07-28 00:38:54 -07002058 wacom_calculate_res(features);
2059
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002060 wacom_update_name(wacom, wireless ? " (WL)" : "");
Ping Cheng4492eff2010-03-19 22:18:15 -07002061
Ping Chengf3586d22015-03-20 14:57:00 -07002062 error = wacom_add_shared_data(hdev);
2063 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002064 goto fail;
Ping Cheng49b764a2010-02-20 00:53:49 -08002065
Jason Gereckeccad85c2015-08-03 10:17:04 -07002066 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002067 (features->quirks & WACOM_QUIRK_BATTERY)) {
2068 error = wacom_initialize_battery(wacom);
2069 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002070 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002071 }
2072
Jason Gerecke3f14a632015-08-03 10:17:05 -07002073 error = wacom_register_inputs(wacom);
2074 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002075 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002076
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002077 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002078 error = wacom_initialize_leds(wacom);
2079 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002080 goto fail;
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002081
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002082 error = wacom_initialize_remotes(wacom);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002083 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002084 goto fail;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002085 }
2086
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002087 if (features->type == HID_GENERIC)
2088 connect_mask |= HID_CONNECT_DRIVER;
2089
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002090 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002091 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002092 if (error) {
2093 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002094 goto fail;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002095 }
2096
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002097 if (!wireless) {
2098 /* Note that if query fails it is not a hard failure */
2099 wacom_query_tablet_data(hdev, features);
2100 }
Jason Gerecke86e88f02015-11-03 16:57:58 -08002101
2102 /* touch only Bamboo doesn't support pen */
2103 if ((features->type == BAMBOO_TOUCH) &&
2104 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2105 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002106 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08002107 }
2108
2109 /* pen only Bamboo neither support touch nor pad */
2110 if ((features->type == BAMBOO_PEN) &&
2111 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2112 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2113 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002114 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08002115 }
2116
Jason Gereckeccad85c2015-08-03 10:17:04 -07002117 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002118 error = hid_hw_open(hdev);
2119
Ping Chengeda01da2015-09-23 13:51:15 -07002120 if ((wacom_wac->features.type == INTUOSHT ||
Benjamin Tissoires97f9afa2016-07-13 18:05:49 +02002121 wacom_wac->features.type == INTUOSHT2) &&
Ping Chengeda01da2015-09-23 13:51:15 -07002122 (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
Benjamin Tissoires97f9afa2016-07-13 18:05:49 +02002123 wacom_wac->shared->type = wacom_wac->features.type;
2124 wacom_wac->shared->touch_input = wacom_wac->touch_input;
Ping Cheng961794a2013-12-05 12:54:53 -08002125 }
2126
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002127 devres_close_group(&hdev->dev, wacom);
2128
Ping Cheng3bea7332006-07-13 18:01:36 -07002129 return 0;
2130
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002131fail_quirks:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002132 hid_hw_stop(hdev);
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002133fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002134 wacom_release_resources(wacom);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002135 return error;
2136}
2137
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002138static void wacom_wireless_work(struct work_struct *work)
2139{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002140 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002141 struct usb_device *usbdev = wacom->usbdev;
2142 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2143 struct hid_device *hdev1, *hdev2;
2144 struct wacom *wacom1, *wacom2;
2145 struct wacom_wac *wacom_wac1, *wacom_wac2;
2146 int error;
2147
2148 /*
2149 * Regardless if this is a disconnect or a new tablet,
2150 * remove any existing input and battery devices.
2151 */
2152
2153 wacom_destroy_battery(wacom);
2154
2155 /* Stylus interface */
2156 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2157 wacom1 = hid_get_drvdata(hdev1);
2158 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002159 wacom_release_resources(wacom1);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002160
2161 /* Touch interface */
2162 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2163 wacom2 = hid_get_drvdata(hdev2);
2164 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002165 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002166
2167 if (wacom_wac->pid == 0) {
2168 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002169 } else {
2170 const struct hid_device_id *id = wacom_ids;
2171
2172 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2173 wacom_wac->pid);
2174
2175 while (id->bus) {
2176 if (id->vendor == USB_VENDOR_ID_WACOM &&
2177 id->product == wacom_wac->pid)
2178 break;
2179 id++;
2180 }
2181
2182 if (!id->bus) {
2183 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2184 return;
2185 }
2186
2187 /* Stylus interface */
2188 wacom_wac1->features =
2189 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002190
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002191 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002192 hid_hw_stop(hdev1);
2193 error = wacom_parse_and_register(wacom1, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002194 if (error)
2195 goto fail;
2196
2197 /* Touch interface */
2198 if (wacom_wac1->features.touch_max ||
2199 (wacom_wac1->features.type >= INTUOSHT &&
2200 wacom_wac1->features.type <= BAMBOO_PT)) {
2201 wacom_wac2->features =
2202 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002203 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002204 hid_hw_stop(hdev2);
2205 error = wacom_parse_and_register(wacom2, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002206 if (error)
2207 goto fail;
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002208 }
2209
Benjamin Tissoires99569532016-07-13 18:06:17 +02002210 strlcpy(wacom_wac->name, wacom_wac1->name,
2211 sizeof(wacom_wac->name));
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002212 error = wacom_initialize_battery(wacom);
2213 if (error)
2214 goto fail;
2215 }
2216
2217 return;
2218
2219fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002220 wacom_release_resources(wacom1);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002221 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002222 return;
2223}
2224
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002225static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2226{
2227 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002228 u32 serial = remote->remotes[index].serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002229 int i;
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002230 unsigned long flags;
2231
2232 spin_lock_irqsave(&remote->remote_lock, flags);
2233 remote->remotes[index].registered = false;
2234 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002235
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002236 if (remote->remotes[index].battery.battery)
2237 devres_release_group(&wacom->hdev->dev,
2238 &remote->remotes[index].battery.bat_desc);
2239
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002240 if (remote->remotes[index].group.name)
2241 devres_release_group(&wacom->hdev->dev,
2242 &remote->remotes[index]);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002243
2244 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002245 if (remote->remotes[i].serial == serial) {
2246 remote->remotes[i].serial = 0;
2247 remote->remotes[i].group.name = NULL;
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002248 remote->remotes[i].registered = false;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002249 remote->remotes[i].battery.battery = NULL;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002250 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2251 }
2252 }
2253}
2254
2255static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2256 unsigned int index)
2257{
2258 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002259 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002260 int error, k;
2261
2262 /* A remote can pair more than once with an EKR,
2263 * check to make sure this serial isn't already paired.
2264 */
2265 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002266 if (remote->remotes[k].serial == serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002267 break;
2268 }
2269
2270 if (k < WACOM_MAX_REMOTES) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002271 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002272 return 0;
2273 }
2274
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002275 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002276 return -ENOMEM;
2277
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002278 error = wacom_remote_create_attr_group(wacom, serial, index);
2279 if (error)
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002280 goto fail;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002281
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002282 remote->remotes[index].input = wacom_allocate_input(wacom);
2283 if (!remote->remotes[index].input) {
2284 error = -ENOMEM;
2285 goto fail;
2286 }
2287 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2288 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2289
2290 if (!remote->remotes[index].input->name) {
2291 error = -EINVAL;
2292 goto fail;
2293 }
2294
2295 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2296 &wacom->wacom_wac);
2297 if (error)
2298 goto fail;
2299
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002300 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002301
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002302 error = input_register_device(remote->remotes[index].input);
2303 if (error)
2304 goto fail;
2305
Benjamin Tissoires97f55412016-07-13 18:06:10 +02002306 error = wacom_led_groups_alloc_and_register_one(
2307 &remote->remotes[index].input->dev,
2308 wacom, index, 3, true);
2309 if (error)
2310 goto fail;
2311
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002312 remote->remotes[index].registered = true;
2313
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002314 devres_close_group(dev, &remote->remotes[index]);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002315 return 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002316
2317fail:
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002318 devres_release_group(dev, &remote->remotes[index]);
2319 remote->remotes[index].serial = 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002320 return error;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002321}
2322
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002323static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2324{
2325 struct wacom_remote *remote = wacom->remote;
2326 int error;
2327
2328 if (!remote->remotes[index].registered)
2329 return 0;
2330
2331 if (remote->remotes[index].battery.battery)
2332 return 0;
2333
2334 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2335 return 0;
2336
2337 error = __wacom_initialize_battery(wacom,
2338 &wacom->remote->remotes[index].battery);
2339 if (error)
2340 return error;
2341
2342 return 0;
2343}
2344
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002345static void wacom_remote_work(struct work_struct *work)
2346{
2347 struct wacom *wacom = container_of(work, struct wacom, remote_work);
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002348 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002349 struct wacom_remote_data data;
2350 unsigned long flags;
2351 unsigned int count;
2352 u32 serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002353 int i;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002354
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002355 spin_lock_irqsave(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002356
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002357 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002358
2359 if (count != sizeof(data)) {
2360 hid_err(wacom->hdev,
2361 "workitem triggered without status available\n");
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002362 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002363 return;
2364 }
2365
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002366 if (!kfifo_is_empty(&remote->remote_fifo))
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002367 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2368
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002369 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002370
2371 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2372 serial = data.remote[i].serial;
2373 if (data.remote[i].connected) {
2374
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002375 if (remote->remotes[i].serial == serial) {
2376 wacom_remote_attach_battery(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002377 continue;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002378 }
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002379
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002380 if (remote->remotes[i].serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002381 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002382
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002383 wacom_remote_create_one(wacom, serial, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002384
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002385 } else if (remote->remotes[i].serial) {
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002386 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002387 }
2388 }
2389}
2390
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002391static int wacom_probe(struct hid_device *hdev,
2392 const struct hid_device_id *id)
2393{
2394 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2395 struct usb_device *dev = interface_to_usbdev(intf);
2396 struct wacom *wacom;
2397 struct wacom_wac *wacom_wac;
2398 struct wacom_features *features;
2399 int error;
2400
2401 if (!id->driver_data)
2402 return -EINVAL;
2403
2404 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2405
2406 /* hid-core sets this quirk for the boot interface */
2407 hdev->quirks &= ~HID_QUIRK_NOGET;
2408
Benjamin Tissoires19b64332016-07-13 18:05:58 +02002409 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002410 if (!wacom)
2411 return -ENOMEM;
2412
2413 hid_set_drvdata(hdev, wacom);
2414 wacom->hdev = hdev;
2415
2416 wacom_wac = &wacom->wacom_wac;
2417 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2418 features = &wacom_wac->features;
2419
2420 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2421 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002422 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002423 }
2424
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002425 wacom_wac->hid_data.inputmode = -1;
Jason Gerecke326ea2a2016-04-04 11:26:52 -07002426 wacom_wac->mode_report = -1;
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002427
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002428 wacom->usbdev = dev;
2429 wacom->intf = intf;
2430 mutex_init(&wacom->lock);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002431 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2432 INIT_WORK(&wacom->battery_work, wacom_battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002433 INIT_WORK(&wacom->remote_work, wacom_remote_work);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002434
2435 /* ask for the report descriptor to be loaded by HID */
2436 error = hid_parse(hdev);
2437 if (error) {
2438 hid_err(hdev, "parse failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002439 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002440 }
2441
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002442 error = wacom_parse_and_register(wacom, false);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002443 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002444 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002445
2446 if (hdev->bus == BUS_BLUETOOTH) {
2447 error = device_create_file(&hdev->dev, &dev_attr_speed);
2448 if (error)
2449 hid_warn(hdev,
2450 "can't create sysfs speed attribute err: %d\n",
2451 error);
2452 }
2453
2454 return 0;
2455
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002456fail:
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002457 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04002458 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07002459}
2460
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002461static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07002462{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002463 struct wacom *wacom = hid_get_drvdata(hdev);
Benjamin Tissoiresf6205162016-02-12 17:27:45 +01002464 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2465 struct wacom_features *features = &wacom_wac->features;
2466
2467 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2468 hid_hw_close(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07002469
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002470 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002471
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002472 cancel_work_sync(&wacom->wireless_work);
2473 cancel_work_sync(&wacom->battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002474 cancel_work_sync(&wacom->remote_work);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002475 if (hdev->bus == BUS_BLUETOOTH)
2476 device_remove_file(&hdev->dev, &dev_attr_speed);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002477
2478 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04002479}
2480
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002481#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002482static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002483{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002484 struct wacom *wacom = hid_get_drvdata(hdev);
2485 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04002486
2487 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002488
2489 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07002490 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002491 wacom_led_control(wacom);
2492
Oliver Neukume7224092008-04-15 01:31:57 -04002493 mutex_unlock(&wacom->lock);
2494
2495 return 0;
2496}
2497
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002498static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002499{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002500 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002501}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002502#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04002503
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002504static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07002505 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08002506 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07002507 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002508 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002509 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002510#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04002511 .resume = wacom_resume,
2512 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002513#endif
2514 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07002515};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002516module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07002517
2518MODULE_VERSION(DRIVER_VERSION);
2519MODULE_AUTHOR(DRIVER_AUTHOR);
2520MODULE_DESCRIPTION(DRIVER_DESC);
2521MODULE_LICENSE(DRIVER_LICENSE);