blob: 5e7a5648e7084e09e484b1c51cd38dd3d79a109f [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 &&
162 (field->application == WACOM_G9_DIGITIZER ||
163 field->application == WACOM_G11_DIGITIZER)) {
164 wacom->wacom_wac.mode_report = field->report->id;
165 wacom->wacom_wac.mode_value = 0;
166 }
167 break;
168
169 case WACOM_G9_PAGE:
170 case WACOM_G11_PAGE:
171 if (field->report->id == 0x03 &&
172 (field->application == WACOM_G9_TOUCHSCREEN ||
173 field->application == WACOM_G11_TOUCHSCREEN)) {
174 wacom->wacom_wac.mode_report = field->report->id;
175 wacom->wacom_wac.mode_value = 0;
176 }
177 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700178 }
179}
180
Chris Bagwell428f8582011-10-26 22:26:59 -0700181/*
182 * Interface Descriptor of wacom devices can be incomplete and
183 * inconsistent so wacom_features table is used to store stylus
184 * device's packet lengths, various maximum values, and tablet
185 * resolution based on product ID's.
186 *
187 * For devices that contain 2 interfaces, wacom_features table is
188 * inaccurate for the touch interface. Since the Interface Descriptor
189 * for touch interfaces has pretty complete data, this function exists
190 * to query tablet for this missing information instead of hard coding in
191 * an additional table.
192 *
193 * A typical Interface Descriptor for a stylus will contain a
194 * boot mouse application collection that is not of interest and this
195 * function will ignore it.
196 *
197 * It also contains a digitizer application collection that also is not
198 * of interest since any information it contains would be duplicate
199 * of what is in wacom_features. Usually it defines a report of an array
200 * of bytes that could be used as max length of the stylus packet returned.
201 * If it happens to define a Digitizer-Stylus Physical Collection then
202 * the X and Y logical values contain valid data but it is ignored.
203 *
204 * A typical Interface Descriptor for a touch interface will contain a
205 * Digitizer-Finger Physical Collection which will define both logical
206 * X/Y maximum as well as the physical size of tablet. Since touch
207 * interfaces haven't supported pressure or distance, this is enough
208 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700209 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700210 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
211 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700212 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700213static void wacom_usage_mapping(struct hid_device *hdev,
214 struct hid_field *field, struct hid_usage *usage)
215{
216 struct wacom *wacom = hid_get_drvdata(hdev);
217 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoiresd97a5522015-01-05 16:32:12 -0500218 bool finger = WACOM_FINGER_FIELD(field);
219 bool pen = WACOM_PEN_FIELD(field);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700220
221 /*
222 * Requiring Stylus Usage will ignore boot mouse
223 * X/Y values and some cases of invalid Digitizer X/Y
224 * values commonly reported.
225 */
Jason Gerecke042628a2015-04-30 17:51:54 -0700226 if (pen)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700227 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -0700228 else if (finger)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700229 features->device_type |= WACOM_DEVICETYPE_TOUCH;
Jason Gerecke042628a2015-04-30 17:51:54 -0700230 else
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700231 return;
232
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800233 /*
234 * Bamboo models do not support HID_DG_CONTACTMAX.
235 * And, Bamboo Pen only descriptor contains touch.
236 */
Ping Cheng3b164a02015-09-23 09:59:10 -0700237 if (features->type > BAMBOO_PT) {
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800238 /* ISDv4 touch devices at least supports one touch point */
239 if (finger && !features->touch_max)
240 features->touch_max = 1;
241 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700242
243 switch (usage->hid) {
244 case HID_GD_X:
245 features->x_max = field->logical_maximum;
246 if (finger) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700247 features->x_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700248 if ((features->type != BAMBOO_PT) &&
249 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700250 features->unit = field->unit;
251 features->unitExpo = field->unit_exponent;
252 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700253 }
254 break;
255 case HID_GD_Y:
256 features->y_max = field->logical_maximum;
257 if (finger) {
258 features->y_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700259 if ((features->type != BAMBOO_PT) &&
260 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700261 features->unit = field->unit;
262 features->unitExpo = field->unit_exponent;
263 }
264 }
265 break;
266 case HID_DG_TIPPRESSURE:
267 if (pen)
268 features->pressure_max = field->logical_maximum;
269 break;
270 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400271
272 if (features->type == HID_GENERIC)
273 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700274}
275
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800276static void wacom_post_parse_hid(struct hid_device *hdev,
277 struct wacom_features *features)
278{
279 struct wacom *wacom = hid_get_drvdata(hdev);
280 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
281
282 if (features->type == HID_GENERIC) {
283 /* Any last-minute generic device setup */
284 if (features->touch_max > 1) {
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -0700285 input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800286 INPUT_MT_DIRECT);
287 }
288 }
289}
290
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700291static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800292 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500293{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700294 struct hid_report_enum *rep_enum;
295 struct hid_report *hreport;
296 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500297
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700298 /* check features first */
299 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
300 list_for_each_entry(hreport, &rep_enum->report_list, list) {
301 for (i = 0; i < hreport->maxfield; i++) {
302 /* Ignore if report count is out of bounds. */
303 if (hreport->field[i]->report_count < 1)
304 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500305
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700306 for (j = 0; j < hreport->field[i]->maxusage; j++) {
307 wacom_feature_mapping(hdev, hreport->field[i],
308 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500309 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500310 }
311 }
312
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700313 /* now check the input usages */
314 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
315 list_for_each_entry(hreport, &rep_enum->report_list, list) {
316
317 if (!hreport->maxfield)
318 continue;
319
320 for (i = 0; i < hreport->maxfield; i++)
321 for (j = 0; j < hreport->field[i]->maxusage; j++)
322 wacom_usage_mapping(hdev, hreport->field[i],
323 hreport->field[i]->usage + j);
324 }
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800325
326 wacom_post_parse_hid(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -0500327}
328
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400329static int wacom_hid_set_device_mode(struct hid_device *hdev)
330{
331 struct wacom *wacom = hid_get_drvdata(hdev);
332 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
333 struct hid_report *r;
334 struct hid_report_enum *re;
335
336 if (hid_data->inputmode < 0)
337 return 0;
338
339 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
340 r = re->report_id_hash[hid_data->inputmode];
341 if (r) {
342 r->field[0]->value[hid_data->inputmode_index] = 2;
343 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
344 }
345 return 0;
346}
347
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700348static int wacom_set_device_mode(struct hid_device *hdev,
349 struct wacom_wac *wacom_wac)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700350{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700351 u8 *rep_data;
352 struct hid_report *r;
353 struct hid_report_enum *re;
354 int length;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700355 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700356
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700357 if (wacom_wac->mode_report < 0)
358 return 0;
359
360 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
361 r = re->report_id_hash[wacom_wac->mode_report];
362 if (!r)
363 return -EINVAL;
364
365 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700366 if (!rep_data)
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700367 return -ENOMEM;
368
369 length = hid_report_len(r);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700370
Jason Gereckefe494bc2012-10-03 17:25:35 -0700371 do {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700372 rep_data[0] = wacom_wac->mode_report;
373 rep_data[1] = wacom_wac->mode_value;
Chris Bagwell9937c022013-01-23 19:37:34 -0800374
Przemo Firszt296b7372014-08-06 14:00:38 -0700375 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
376 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700377 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700378 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700379 rep_data, length, 1);
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700380 } while (error >= 0 &&
381 rep_data[1] != wacom_wac->mode_report &&
382 limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700383
384 kfree(rep_data);
385
386 return error < 0 ? error : 0;
387}
388
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700389static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
390 struct wacom_features *features)
391{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700392 struct wacom *wacom = hid_get_drvdata(hdev);
393 int ret;
394 u8 rep_data[2];
395
396 switch (features->type) {
397 case GRAPHIRE_BT:
398 rep_data[0] = 0x03;
399 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700400 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
401 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700402
403 if (ret >= 0) {
404 rep_data[0] = speed == 0 ? 0x05 : 0x06;
405 rep_data[1] = 0x00;
406
407 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700408 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700409
410 if (ret >= 0) {
411 wacom->wacom_wac.bt_high_speed = speed;
412 return 0;
413 }
414 }
415
416 /*
417 * Note that if the raw queries fail, it's not a hard failure
418 * and it is safe to continue
419 */
420 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
421 rep_data[0], ret);
422 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700423 case INTUOS4WL:
424 if (speed == 1)
425 wacom->wacom_wac.bt_features &= ~0x20;
426 else
427 wacom->wacom_wac.bt_features |= 0x20;
428
429 rep_data[0] = 0x03;
430 rep_data[1] = wacom->wacom_wac.bt_features;
431
Przemo Firszt296b7372014-08-06 14:00:38 -0700432 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
433 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700434 if (ret >= 0)
435 wacom->wacom_wac.bt_high_speed = speed;
436 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700437 }
438
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700439 return 0;
440}
441
Jason Gereckefe494bc2012-10-03 17:25:35 -0700442/*
443 * Switch the tablet into its most-capable mode. Wacom tablets are
444 * typically configured to power-up in a mode which sends mouse-like
445 * reports to the OS. To get absolute position, pressure data, etc.
446 * from the tablet, it is necessary to switch the tablet out of this
447 * mode and into one which sends the full range of tablet data.
448 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700449static int wacom_query_tablet_data(struct hid_device *hdev,
450 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700451{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700452 struct wacom *wacom = hid_get_drvdata(hdev);
453 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
454
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700455 if (hdev->bus == BUS_BLUETOOTH)
456 return wacom_bt_query_tablet_data(hdev, 1, features);
457
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700458 if (features->type != HID_GENERIC) {
459 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
460 if (features->type > TABLETPC) {
461 /* MT Tablet PC touch */
462 wacom_wac->mode_report = 3;
463 wacom_wac->mode_value = 4;
464 } else if (features->type == WACOM_24HDT) {
465 wacom_wac->mode_report = 18;
466 wacom_wac->mode_value = 2;
467 } else if (features->type == WACOM_27QHDT) {
468 wacom_wac->mode_report = 131;
469 wacom_wac->mode_value = 2;
470 } else if (features->type == BAMBOO_PAD) {
471 wacom_wac->mode_report = 2;
472 wacom_wac->mode_value = 2;
473 }
474 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
475 if (features->type <= BAMBOO_PT) {
476 wacom_wac->mode_report = 2;
477 wacom_wac->mode_value = 2;
478 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700479 }
480 }
481
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700482 wacom_set_device_mode(hdev, wacom_wac);
483
484 if (features->type == HID_GENERIC)
485 return wacom_hid_set_device_mode(hdev);
486
Jason Gereckefe494bc2012-10-03 17:25:35 -0700487 return 0;
488}
489
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700490static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700491 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800492{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700493 struct wacom *wacom = hid_get_drvdata(hdev);
494 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800495
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700496 /* default features */
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700497 features->x_fuzz = 4;
498 features->y_fuzz = 4;
499 features->pressure_fuzz = 0;
Jason Gereckebef7e202016-04-22 14:30:53 -0700500 features->distance_fuzz = 1;
501 features->tilt_fuzz = 1;
Ping Chengec67bbe2009-12-15 00:35:24 -0800502
Chris Bagwelld3825d52012-03-25 23:26:11 -0700503 /*
504 * The wireless device HID is basic and layout conflicts with
505 * other tablets (monitor and touch interface can look like pen).
506 * Skip the query for this type and modify defaults based on
507 * interface number.
508 */
509 if (features->type == WIRELESS) {
Jason Gerecke3f14a632015-08-03 10:17:05 -0700510 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
Jason Gereckeccad85c2015-08-03 10:17:04 -0700511 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700512 else
Jason Gereckeaa86b182015-06-15 18:01:42 -0700513 features->device_type = WACOM_DEVICETYPE_NONE;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700514 return;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700515 }
516
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700517 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800518}
519
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700520struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700521 struct list_head list;
522 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700523 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700524 struct wacom_shared shared;
525};
526
527static LIST_HEAD(wacom_udev_list);
528static DEFINE_MUTEX(wacom_udev_list_lock);
529
Jason Gerecke41372d52016-08-08 12:06:30 -0700530static bool compare_device_paths(struct hid_device *hdev_a,
531 struct hid_device *hdev_b, char separator)
532{
533 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
534 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
535
536 if (n1 != n2 || n1 <= 0 || n2 <= 0)
537 return false;
538
539 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
540}
541
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700542static bool wacom_are_sibling(struct hid_device *hdev,
543 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700544{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700545 struct wacom *wacom = hid_get_drvdata(hdev);
546 struct wacom_features *features = &wacom->wacom_wac.features;
Jason Gerecke41372d52016-08-08 12:06:30 -0700547 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
548 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
549 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
550 __u32 oPid = features->oPid ? features->oPid : hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700551
Jason Gerecke41372d52016-08-08 12:06:30 -0700552 /* The defined oVid/oPid must match that of the sibling */
553 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
554 return false;
555 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
556 return false;
557
558 /*
559 * Devices with the same VID/PID must share the same physical
560 * device path, while those with different VID/PID must share
561 * the same physical parent device path.
562 */
563 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
564 if (!compare_device_paths(hdev, sibling, '/'))
565 return false;
566 } else {
567 if (!compare_device_paths(hdev, sibling, '.'))
568 return false;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700569 }
570
Jason Gerecke41372d52016-08-08 12:06:30 -0700571 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
572 if (features->type != HID_GENERIC)
573 return true;
574
575 /*
576 * Direct-input devices may not be siblings of indirect-input
577 * devices.
578 */
579 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
580 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700581 return false;
582
Jason Gerecke41372d52016-08-08 12:06:30 -0700583 /*
584 * Indirect-input devices may not be siblings of direct-input
585 * devices.
586 */
587 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
588 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700589 return false;
590
Jason Gerecke41372d52016-08-08 12:06:30 -0700591 /* Pen devices may only be siblings of touch devices */
592 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
593 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
594 return false;
595
596 /* Touch devices may only be siblings of pen devices */
597 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
598 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
599 return false;
600
601 /*
602 * No reason could be found for these two devices to NOT be
603 * siblings, so there's a good chance they ARE siblings
604 */
605 return true;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700606}
607
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700608static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700609{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700610 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700611
Jason Gerecke41372d52016-08-08 12:06:30 -0700612 /* Try to find an already-probed interface from the same device */
613 list_for_each_entry(data, &wacom_udev_list, list) {
614 if (compare_device_paths(hdev, data->dev, '/'))
615 return data;
616 }
617
618 /* Fallback to finding devices that appear to be "siblings" */
Ping Cheng4492eff2010-03-19 22:18:15 -0700619 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700620 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700621 kref_get(&data->kref);
622 return data;
623 }
624 }
625
626 return NULL;
627}
628
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200629static void wacom_release_shared_data(struct kref *kref)
630{
631 struct wacom_hdev_data *data =
632 container_of(kref, struct wacom_hdev_data, kref);
633
634 mutex_lock(&wacom_udev_list_lock);
635 list_del(&data->list);
636 mutex_unlock(&wacom_udev_list_lock);
637
638 kfree(data);
639}
640
641static void wacom_remove_shared_data(void *res)
642{
643 struct wacom *wacom = res;
644 struct wacom_hdev_data *data;
645 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
646
647 if (wacom_wac->shared) {
648 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
649 shared);
650
651 if (wacom_wac->shared->touch == wacom->hdev)
652 wacom_wac->shared->touch = NULL;
653 else if (wacom_wac->shared->pen == wacom->hdev)
654 wacom_wac->shared->pen = NULL;
655
656 kref_put(&data->kref, wacom_release_shared_data);
657 wacom_wac->shared = NULL;
658 }
659}
660
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700661static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700662{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700663 struct wacom *wacom = hid_get_drvdata(hdev);
664 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
665 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700666 int retval = 0;
667
668 mutex_lock(&wacom_udev_list_lock);
669
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700670 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700671 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700672 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700673 if (!data) {
674 retval = -ENOMEM;
675 goto out;
676 }
677
678 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700679 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700680 list_add_tail(&data->list, &wacom_udev_list);
681 }
682
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700683 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700684
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200685 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
686 if (retval) {
687 mutex_unlock(&wacom_udev_list_lock);
688 wacom_remove_shared_data(wacom);
689 return retval;
690 }
691
Jason Gereckeaa86b182015-06-15 18:01:42 -0700692 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500693 wacom_wac->shared->touch = hdev;
Jason Gereckeaa86b182015-06-15 18:01:42 -0700694 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500695 wacom_wac->shared->pen = hdev;
696
Ping Cheng4492eff2010-03-19 22:18:15 -0700697out:
698 mutex_unlock(&wacom_udev_list_lock);
699 return retval;
700}
701
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700702static int wacom_led_control(struct wacom *wacom)
703{
704 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700705 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700706 unsigned char report_id = WAC_CMD_LED_CONTROL;
707 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700708
Benjamin Tissoires97f55412016-07-13 18:06:10 +0200709 if (!hid_get_drvdata(wacom->hdev))
710 return -ENODEV;
711
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200712 if (!wacom->led.groups)
713 return -ENOTSUPP;
714
Ping Cheng912ca212014-09-10 12:41:31 -0700715 if (wacom->wacom_wac.pid) { /* wireless connected */
716 report_id = WAC_CMD_WL_LED_CONTROL;
717 buf_size = 13;
718 }
719 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700720 if (!buf)
721 return -ENOMEM;
722
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700723 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700724 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700725 /*
726 * Touch Ring and crop mark LED luminance may take on
727 * one of four values:
728 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
729 */
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200730 int ring_led = wacom->led.groups[0].select & 0x03;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700731 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
732 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700733 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700734
Ping Cheng912ca212014-09-10 12:41:31 -0700735 buf[0] = report_id;
736 if (wacom->wacom_wac.pid) {
737 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
738 buf, buf_size, WAC_CMD_RETRIES);
739 buf[0] = report_id;
740 buf[4] = led_bits;
741 } else
742 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700743 }
744 else {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200745 int led = wacom->led.groups[0].select | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700746
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700747 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
748 wacom->wacom_wac.features.type == WACOM_24HD)
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200749 led |= (wacom->led.groups[1].select << 4) | 0x40;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700750
Ping Cheng912ca212014-09-10 12:41:31 -0700751 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700752 buf[1] = led;
753 buf[2] = wacom->led.llv;
754 buf[3] = wacom->led.hlv;
755 buf[4] = wacom->led.img_lum;
756 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700757
Ping Cheng912ca212014-09-10 12:41:31 -0700758 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700759 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700760 kfree(buf);
761
762 return retval;
763}
764
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700765static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
766 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700767{
768 unsigned char *buf;
769 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700770 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700771
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700772 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700773 if (!buf)
774 return -ENOMEM;
775
776 /* Send 'start' command */
777 buf[0] = WAC_CMD_ICON_START;
778 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700779 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
780 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700781 if (retval < 0)
782 goto out;
783
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700784 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700785 buf[1] = button_id & 0x07;
786 for (i = 0; i < 4; i++) {
787 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700788 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700789
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700790 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700791 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700792 if (retval < 0)
793 break;
794 }
795
796 /* Send 'stop' */
797 buf[0] = WAC_CMD_ICON_START;
798 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700799 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
800 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700801
802out:
803 kfree(buf);
804 return retval;
805}
806
Ping Cheng09e7d942011-10-04 23:51:14 -0700807static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700808 const char *buf, size_t count)
809{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800810 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700811 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700812 unsigned int id;
813 int err;
814
815 err = kstrtouint(buf, 10, &id);
816 if (err)
817 return err;
818
819 mutex_lock(&wacom->lock);
820
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200821 wacom->led.groups[set_id].select = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700822 err = wacom_led_control(wacom);
823
824 mutex_unlock(&wacom->lock);
825
826 return err < 0 ? err : count;
827}
828
Ping Cheng09e7d942011-10-04 23:51:14 -0700829#define DEVICE_LED_SELECT_ATTR(SET_ID) \
830static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
831 struct device_attribute *attr, const char *buf, size_t count) \
832{ \
833 return wacom_led_select_store(dev, SET_ID, buf, count); \
834} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700835static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
836 struct device_attribute *attr, char *buf) \
837{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800838 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700839 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700840 return scnprintf(buf, PAGE_SIZE, "%d\n", \
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200841 wacom->led.groups[SET_ID].select); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700842} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700843static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700844 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700845 wacom_led##SET_ID##_select_store)
846
847DEVICE_LED_SELECT_ATTR(0);
848DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700849
850static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
851 const char *buf, size_t count)
852{
853 unsigned int value;
854 int err;
855
856 err = kstrtouint(buf, 10, &value);
857 if (err)
858 return err;
859
860 mutex_lock(&wacom->lock);
861
862 *dest = value & 0x7f;
863 err = wacom_led_control(wacom);
864
865 mutex_unlock(&wacom->lock);
866
867 return err < 0 ? err : count;
868}
869
870#define DEVICE_LUMINANCE_ATTR(name, field) \
871static ssize_t wacom_##name##_luminance_store(struct device *dev, \
872 struct device_attribute *attr, const char *buf, size_t count) \
873{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800874 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700875 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700876 \
877 return wacom_luminance_store(wacom, &wacom->led.field, \
878 buf, count); \
879} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700880static ssize_t wacom_##name##_luminance_show(struct device *dev, \
881 struct device_attribute *attr, char *buf) \
882{ \
883 struct wacom *wacom = dev_get_drvdata(dev); \
884 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
885} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700886static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700887 wacom_##name##_luminance_show, \
888 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700889
890DEVICE_LUMINANCE_ATTR(status0, llv);
891DEVICE_LUMINANCE_ATTR(status1, hlv);
892DEVICE_LUMINANCE_ATTR(buttons, img_lum);
893
894static ssize_t wacom_button_image_store(struct device *dev, int button_id,
895 const char *buf, size_t count)
896{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800897 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700898 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700899 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700900 unsigned len;
901 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700902
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700903 if (hdev->bus == BUS_BLUETOOTH) {
904 len = 256;
905 xfer_id = WAC_CMD_ICON_BT_XFER;
906 } else {
907 len = 1024;
908 xfer_id = WAC_CMD_ICON_XFER;
909 }
910
911 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700912 return -EINVAL;
913
914 mutex_lock(&wacom->lock);
915
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700916 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700917
918 mutex_unlock(&wacom->lock);
919
920 return err < 0 ? err : count;
921}
922
923#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
924static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
925 struct device_attribute *attr, const char *buf, size_t count) \
926{ \
927 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
928} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700929static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700930 NULL, wacom_btnimg##BUTTON_ID##_store)
931
932DEVICE_BTNIMG_ATTR(0);
933DEVICE_BTNIMG_ATTR(1);
934DEVICE_BTNIMG_ATTR(2);
935DEVICE_BTNIMG_ATTR(3);
936DEVICE_BTNIMG_ATTR(4);
937DEVICE_BTNIMG_ATTR(5);
938DEVICE_BTNIMG_ATTR(6);
939DEVICE_BTNIMG_ATTR(7);
940
Ping Cheng09e7d942011-10-04 23:51:14 -0700941static struct attribute *cintiq_led_attrs[] = {
942 &dev_attr_status_led0_select.attr,
943 &dev_attr_status_led1_select.attr,
944 NULL
945};
946
947static struct attribute_group cintiq_led_attr_group = {
948 .name = "wacom_led",
949 .attrs = cintiq_led_attrs,
950};
951
952static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700953 &dev_attr_status0_luminance.attr,
954 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700955 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700956 &dev_attr_buttons_luminance.attr,
957 &dev_attr_button0_rawimg.attr,
958 &dev_attr_button1_rawimg.attr,
959 &dev_attr_button2_rawimg.attr,
960 &dev_attr_button3_rawimg.attr,
961 &dev_attr_button4_rawimg.attr,
962 &dev_attr_button5_rawimg.attr,
963 &dev_attr_button6_rawimg.attr,
964 &dev_attr_button7_rawimg.attr,
965 NULL
966};
967
Ping Cheng09e7d942011-10-04 23:51:14 -0700968static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700969 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700970 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700971};
972
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700973static struct attribute *intuos5_led_attrs[] = {
974 &dev_attr_status0_luminance.attr,
975 &dev_attr_status_led0_select.attr,
976 NULL
977};
978
979static struct attribute_group intuos5_led_attr_group = {
980 .name = "wacom_led",
981 .attrs = intuos5_led_attrs,
982};
983
Benjamin Tissoires2df68a82016-07-13 18:05:56 +0200984struct wacom_sysfs_group_devres {
985 struct attribute_group *group;
986 struct kobject *root;
987};
988
989static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
990{
991 struct wacom_sysfs_group_devres *devres = res;
992 struct kobject *kobj = devres->root;
993
994 dev_dbg(dev, "%s: dropping reference to %s\n",
995 __func__, devres->group->name);
996 sysfs_remove_group(kobj, devres->group);
997}
998
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +0200999static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1000 struct kobject *root,
1001 struct attribute_group *group)
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001002{
1003 struct wacom_sysfs_group_devres *devres;
1004 int error;
1005
1006 devres = devres_alloc(wacom_devm_sysfs_group_release,
1007 sizeof(struct wacom_sysfs_group_devres),
1008 GFP_KERNEL);
1009 if (!devres)
1010 return -ENOMEM;
1011
1012 devres->group = group;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001013 devres->root = root;
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001014
1015 error = sysfs_create_group(devres->root, group);
1016 if (error)
1017 return error;
1018
1019 devres_add(&wacom->hdev->dev, devres);
1020
1021 return 0;
1022}
1023
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001024static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1025 struct attribute_group *group)
1026{
1027 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1028 group);
1029}
1030
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001031enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001032{
1033 struct wacom *wacom = led->wacom;
1034
1035 if (wacom->led.max_hlv)
1036 return led->hlv * LED_FULL / wacom->led.max_hlv;
1037
1038 if (wacom->led.max_llv)
1039 return led->llv * LED_FULL / wacom->led.max_llv;
1040
1041 /* device doesn't support brightness tuning */
1042 return LED_FULL;
1043}
1044
1045static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1046{
1047 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1048 struct wacom *wacom = led->wacom;
1049
1050 if (wacom->led.groups[led->group].select != led->id)
1051 return LED_OFF;
1052
1053 return wacom_leds_brightness_get(led);
1054}
1055
1056static int wacom_led_brightness_set(struct led_classdev *cdev,
1057 enum led_brightness brightness)
1058{
1059 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1060 struct wacom *wacom = led->wacom;
1061 int error;
1062
1063 mutex_lock(&wacom->lock);
1064
1065 if (!wacom->led.groups || (brightness == LED_OFF &&
1066 wacom->led.groups[led->group].select != led->id)) {
1067 error = 0;
1068 goto out;
1069 }
1070
1071 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1072 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1073
1074 wacom->led.groups[led->group].select = led->id;
1075
1076 error = wacom_led_control(wacom);
1077
1078out:
1079 mutex_unlock(&wacom->lock);
1080
1081 return error;
1082}
1083
1084static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1085 enum led_brightness brightness)
1086{
1087}
1088
1089static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1090 struct wacom_led *led, unsigned int group,
1091 unsigned int id, bool read_only)
1092{
1093 int error;
1094 char *name;
1095
1096 name = devm_kasprintf(dev, GFP_KERNEL,
1097 "%s::wacom-%d.%d",
1098 dev_name(dev),
1099 group,
1100 id);
1101 if (!name)
1102 return -ENOMEM;
1103
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001104 if (!read_only) {
1105 led->trigger.name = name;
1106 error = devm_led_trigger_register(dev, &led->trigger);
1107 if (error) {
1108 hid_err(wacom->hdev,
1109 "failed to register LED trigger %s: %d\n",
1110 led->cdev.name, error);
1111 return error;
1112 }
1113 }
1114
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001115 led->group = group;
1116 led->id = id;
1117 led->wacom = wacom;
1118 led->llv = wacom->led.llv;
1119 led->hlv = wacom->led.hlv;
1120 led->cdev.name = name;
1121 led->cdev.max_brightness = LED_FULL;
1122 led->cdev.flags = LED_HW_PLUGGABLE;
1123 led->cdev.brightness_get = __wacom_led_brightness_get;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001124 if (!read_only) {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001125 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001126 led->cdev.default_trigger = led->cdev.name;
1127 } else {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001128 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001129 }
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001130
1131 error = devm_led_classdev_register(dev, &led->cdev);
1132 if (error) {
1133 hid_err(wacom->hdev,
1134 "failed to register LED %s: %d\n",
1135 led->cdev.name, error);
1136 led->cdev.name = NULL;
1137 return error;
1138 }
1139
1140 return 0;
1141}
1142
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001143static void wacom_led_groups_release_one(void *data)
1144{
1145 struct wacom_group_leds *group = data;
1146
1147 devres_release_group(group->dev, group);
1148}
1149
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001150static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1151 struct wacom *wacom,
1152 int group_id, int count,
1153 bool read_only)
1154{
1155 struct wacom_led *leds;
1156 int i, error;
1157
1158 if (group_id >= wacom->led.count || count <= 0)
1159 return -EINVAL;
1160
1161 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1162 return -ENOMEM;
1163
1164 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1165 if (!leds) {
1166 error = -ENOMEM;
1167 goto err;
1168 }
1169
1170 wacom->led.groups[group_id].leds = leds;
1171 wacom->led.groups[group_id].count = count;
1172
1173 for (i = 0; i < count; i++) {
1174 error = wacom_led_register_one(dev, wacom, &leds[i],
1175 group_id, i, read_only);
1176 if (error)
1177 goto err;
1178 }
1179
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001180 wacom->led.groups[group_id].dev = dev;
1181
1182 devres_close_group(dev, &wacom->led.groups[group_id]);
1183
1184 /*
1185 * There is a bug (?) in devm_led_classdev_register() in which its
1186 * increments the refcount of the parent. If the parent is an input
1187 * device, that means the ref count never reaches 0 when
1188 * devm_input_device_release() gets called.
1189 * This means that the LEDs are still there after disconnect.
1190 * Manually force the release of the group so that the leds are released
1191 * once we are done using them.
1192 */
1193 error = devm_add_action_or_reset(&wacom->hdev->dev,
1194 wacom_led_groups_release_one,
1195 &wacom->led.groups[group_id]);
1196 if (error)
1197 return error;
1198
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001199 return 0;
1200
1201err:
1202 devres_release_group(dev, &wacom->led.groups[group_id]);
1203 return error;
1204}
1205
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001206struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1207 unsigned int id)
1208{
1209 struct wacom_group_leds *group;
1210
1211 if (group_id >= wacom->led.count)
1212 return NULL;
1213
1214 group = &wacom->led.groups[group_id];
1215
1216 if (!group->leds)
1217 return NULL;
1218
1219 id %= group->count;
1220
1221 return &group->leds[id];
1222}
1223
1224/**
1225 * wacom_led_next: gives the next available led with a wacom trigger.
1226 *
1227 * returns the next available struct wacom_led which has its default trigger
1228 * or the current one if none is available.
1229 */
1230struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1231{
1232 struct wacom_led *next_led;
1233 int group, next;
1234
1235 if (!wacom || !cur)
1236 return NULL;
1237
1238 group = cur->group;
1239 next = cur->id;
1240
1241 do {
1242 next_led = wacom_led_find(wacom, group, ++next);
1243 if (!next_led || next_led == cur)
1244 return next_led;
1245 } while (next_led->cdev.trigger != &next_led->trigger);
1246
1247 return next_led;
1248}
1249
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001250static void wacom_led_groups_release(void *data)
1251{
1252 struct wacom *wacom = data;
1253
1254 wacom->led.groups = NULL;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001255 wacom->led.count = 0;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001256}
1257
1258static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1259{
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001260 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001261 struct wacom_group_leds *groups;
1262 int error;
1263
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001264 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001265 GFP_KERNEL);
1266 if (!groups)
1267 return -ENOMEM;
1268
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001269 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001270 if (error)
1271 return error;
1272
1273 wacom->led.groups = groups;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001274 wacom->led.count = count;
1275
1276 return 0;
1277}
1278
1279static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1280 int led_per_group, bool read_only)
1281{
1282 struct device *dev;
1283 int i, error;
1284
1285 if (!wacom->wacom_wac.pad_input)
1286 return -EINVAL;
1287
1288 dev = &wacom->wacom_wac.pad_input->dev;
1289
1290 error = wacom_led_groups_allocate(wacom, group_count);
1291 if (error)
1292 return error;
1293
1294 for (i = 0; i < group_count; i++) {
1295 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1296 led_per_group,
1297 read_only);
1298 if (error)
1299 return error;
1300 }
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001301
1302 return 0;
1303}
1304
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001305static int wacom_initialize_leds(struct wacom *wacom)
1306{
1307 int error;
1308
Jason Gerecke862cf552015-06-15 18:01:43 -07001309 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1310 return 0;
1311
Ping Cheng09e7d942011-10-04 23:51:14 -07001312 /* Initialize default values */
1313 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -07001314 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -07001315 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -07001316 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -07001317 case INTUOS4L:
Ping Chengf4fa9a62011-10-04 23:49:42 -07001318 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001319 wacom->led.hlv = 20;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001320 wacom->led.max_llv = 127;
1321 wacom->led.max_hlv = 127;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001322 wacom->led.img_lum = 10;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001323
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001324 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001325 if (error) {
1326 hid_err(wacom->hdev,
1327 "cannot create leds err: %d\n", error);
1328 return error;
1329 }
1330
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001331 error = wacom_devm_sysfs_create_group(wacom,
1332 &intuos4_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001333 break;
1334
Jason Gerecke246835f2011-12-12 00:12:04 -08001335 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -07001336 case WACOM_21UX2:
Ping Cheng09e7d942011-10-04 23:51:14 -07001337 wacom->led.llv = 0;
1338 wacom->led.hlv = 0;
1339 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001340
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001341 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001342 if (error) {
1343 hid_err(wacom->hdev,
1344 "cannot create leds err: %d\n", error);
1345 return error;
1346 }
1347
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001348 error = wacom_devm_sysfs_create_group(wacom,
1349 &cintiq_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001350 break;
1351
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001352 case INTUOS5S:
1353 case INTUOS5:
1354 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001355 case INTUOSPS:
1356 case INTUOSPM:
1357 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -07001358 wacom->led.llv = 32;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001359 wacom->led.max_llv = 96;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001360
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001361 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001362 if (error) {
1363 hid_err(wacom->hdev,
1364 "cannot create leds err: %d\n", error);
1365 return error;
1366 }
1367
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001368 error = wacom_devm_sysfs_create_group(wacom,
1369 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001370 break;
1371
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001372 case REMOTE:
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001373 wacom->led.llv = 255;
1374 wacom->led.max_llv = 255;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001375 error = wacom_led_groups_allocate(wacom, 5);
1376 if (error) {
1377 hid_err(wacom->hdev,
1378 "cannot create leds err: %d\n", error);
1379 return error;
1380 }
1381 return 0;
1382
Ping Cheng09e7d942011-10-04 23:51:14 -07001383 default:
1384 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001385 }
1386
Ping Cheng09e7d942011-10-04 23:51:14 -07001387 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -07001388 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -07001389 "cannot create sysfs group err: %d\n", error);
1390 return error;
1391 }
1392 wacom_led_control(wacom);
1393
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001394 return 0;
1395}
1396
Chris Bagwella1d552c2012-03-25 23:26:30 -07001397static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001398 POWER_SUPPLY_PROP_MODEL_NAME,
Jason Gerecke71fa6412015-03-11 10:25:41 -07001399 POWER_SUPPLY_PROP_PRESENT,
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001400 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001401 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001402 POWER_SUPPLY_PROP_CAPACITY
1403};
1404
1405static int wacom_battery_get_property(struct power_supply *psy,
1406 enum power_supply_property psp,
1407 union power_supply_propval *val)
1408{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001409 struct wacom_battery *battery = power_supply_get_drvdata(psy);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001410 int ret = 0;
1411
1412 switch (psp) {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001413 case POWER_SUPPLY_PROP_MODEL_NAME:
1414 val->strval = battery->wacom->wacom_wac.name;
1415 break;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001416 case POWER_SUPPLY_PROP_PRESENT:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001417 val->intval = battery->bat_connected;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001418 break;
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001419 case POWER_SUPPLY_PROP_SCOPE:
1420 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1421 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001422 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001423 val->intval = battery->battery_capacity;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001424 break;
1425 case POWER_SUPPLY_PROP_STATUS:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001426 if (battery->bat_charging)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001427 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001428 else if (battery->battery_capacity == 100 &&
1429 battery->ps_connected)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001430 val->intval = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001431 else if (battery->ps_connected)
Jason Gereckeb0882cb2015-03-06 11:47:43 -08001432 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001433 else
1434 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001435 break;
1436 default:
1437 ret = -EINVAL;
1438 break;
1439 }
1440
1441 return ret;
1442}
1443
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001444static int __wacom_initialize_battery(struct wacom *wacom,
1445 struct wacom_battery *battery)
Chris Bagwella1d552c2012-03-25 23:26:30 -07001446{
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001447 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001448 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001449 struct power_supply_config psy_cfg = { .drv_data = battery, };
Benjamin Tissoires136ae5e2016-07-13 18:06:16 +02001450 struct power_supply *ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001451 struct power_supply_desc *bat_desc = &battery->bat_desc;
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001452 unsigned long n;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001453 int error;
1454
1455 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1456 return -ENOMEM;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001457
Benjamin Tissoires99569532016-07-13 18:06:17 +02001458 battery->wacom = wacom;
1459
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001460 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001461
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001462 bat_desc->properties = wacom_battery_props;
1463 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1464 bat_desc->get_property = wacom_battery_get_property;
1465 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1466 bat_desc->name = battery->bat_name;
Benjamin Tissoires96983292016-07-13 18:06:15 +02001467 bat_desc->type = POWER_SUPPLY_TYPE_USB;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001468 bat_desc->use_for_apm = 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001469
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001470 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1471 if (IS_ERR(ps_bat)) {
1472 error = PTR_ERR(ps_bat);
1473 goto err;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001474 }
1475
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001476 power_supply_powers(ps_bat, &wacom->hdev->dev);
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001477
1478 battery->battery = ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001479
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001480 devres_close_group(dev, bat_desc);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001481 return 0;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001482
1483err:
1484 devres_release_group(dev, bat_desc);
1485 return error;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001486}
1487
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001488static int wacom_initialize_battery(struct wacom *wacom)
1489{
1490 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1491 return __wacom_initialize_battery(wacom, &wacom->battery);
1492
1493 return 0;
1494}
1495
Chris Bagwella1d552c2012-03-25 23:26:30 -07001496static void wacom_destroy_battery(struct wacom *wacom)
1497{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001498 if (wacom->battery.battery) {
1499 devres_release_group(&wacom->hdev->dev,
1500 &wacom->battery.bat_desc);
1501 wacom->battery.battery = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001502 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001503}
1504
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001505static ssize_t wacom_show_speed(struct device *dev,
1506 struct device_attribute
1507 *attr, char *buf)
1508{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001509 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001510 struct wacom *wacom = hid_get_drvdata(hdev);
1511
1512 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1513}
1514
1515static ssize_t wacom_store_speed(struct device *dev,
1516 struct device_attribute *attr,
1517 const char *buf, size_t count)
1518{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001519 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001520 struct wacom *wacom = hid_get_drvdata(hdev);
1521 u8 new_speed;
1522
1523 if (kstrtou8(buf, 0, &new_speed))
1524 return -EINVAL;
1525
1526 if (new_speed != 0 && new_speed != 1)
1527 return -EINVAL;
1528
1529 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1530
1531 return count;
1532}
1533
Ping Chenge0984bc2014-09-10 12:40:05 -07001534static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001535 wacom_show_speed, wacom_store_speed);
1536
Aaron Skomra72b236d2015-08-20 16:05:17 -07001537
1538static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1539 struct kobj_attribute *kattr,
1540 char *buf, int index)
1541{
Geliang Tang2cf83832015-12-27 17:25:24 +08001542 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001543 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001544 struct wacom *wacom = hid_get_drvdata(hdev);
1545 u8 mode;
1546
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001547 mode = wacom->led.groups[index].select;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001548 if (mode >= 0 && mode < 3)
1549 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
1550 else
1551 return snprintf(buf, PAGE_SIZE, "%d\n", -1);
1552}
1553
1554#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1555static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1556 struct kobj_attribute *kattr, char *buf) \
1557{ \
1558 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1559} \
1560static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1561 .attr = {.name = "remote_mode", \
1562 .mode = DEV_ATTR_RO_PERM}, \
1563 .show = wacom_show_remote##SET_ID##_mode, \
1564}; \
1565static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1566 &remote##SET_ID##_mode_attr.attr, \
1567 NULL \
1568}; \
1569static struct attribute_group remote##SET_ID##_serial_group = { \
1570 .name = NULL, \
1571 .attrs = remote##SET_ID##_serial_attrs, \
1572}
1573
1574DEVICE_EKR_ATTR_GROUP(0);
1575DEVICE_EKR_ATTR_GROUP(1);
1576DEVICE_EKR_ATTR_GROUP(2);
1577DEVICE_EKR_ATTR_GROUP(3);
1578DEVICE_EKR_ATTR_GROUP(4);
1579
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02001580static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1581 int index)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001582{
1583 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001584 struct wacom_remote *remote = wacom->remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001585
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001586 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001587 GFP_KERNEL,
1588 "%d", serial);
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001589 if (!remote->remotes[index].group.name)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001590 return -ENOMEM;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001591
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001592 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001593 &remote->remotes[index].group);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001594 if (error) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001595 remote->remotes[index].group.name = NULL;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001596 hid_err(wacom->hdev,
1597 "cannot create sysfs group err: %d\n", error);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001598 return error;
1599 }
1600
1601 return 0;
1602}
1603
Aaron Skomra72b236d2015-08-20 16:05:17 -07001604static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1605{
1606 const size_t buf_size = 2;
1607 unsigned char *buf;
1608 int retval;
1609
1610 buf = kzalloc(buf_size, GFP_KERNEL);
1611 if (!buf)
1612 return -ENOMEM;
1613
1614 buf[0] = WAC_CMD_DELETE_PAIRING;
1615 buf[1] = selector;
1616
1617 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1618 buf_size, WAC_CMD_RETRIES);
1619 kfree(buf);
1620
1621 return retval;
1622}
1623
1624static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1625 struct kobj_attribute *attr,
1626 const char *buf, size_t count)
1627{
1628 unsigned char selector = 0;
Geliang Tang2cf83832015-12-27 17:25:24 +08001629 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001630 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001631 struct wacom *wacom = hid_get_drvdata(hdev);
1632 int err;
1633
1634 if (!strncmp(buf, "*\n", 2)) {
1635 selector = WAC_CMD_UNPAIR_ALL;
1636 } else {
1637 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1638 buf);
1639 return -1;
1640 }
1641
1642 mutex_lock(&wacom->lock);
1643
1644 err = wacom_cmd_unpair_remote(wacom, selector);
1645 mutex_unlock(&wacom->lock);
1646
1647 return err < 0 ? err : count;
1648}
1649
1650static struct kobj_attribute unpair_remote_attr = {
1651 .attr = {.name = "unpair_remote", .mode = 0200},
1652 .store = wacom_store_unpair_remote,
1653};
1654
1655static const struct attribute *remote_unpair_attrs[] = {
1656 &unpair_remote_attr.attr,
1657 NULL
1658};
1659
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001660static void wacom_remotes_destroy(void *data)
1661{
1662 struct wacom *wacom = data;
1663 struct wacom_remote *remote = wacom->remote;
1664
1665 if (!remote)
1666 return;
1667
1668 kobject_put(remote->remote_dir);
1669 kfifo_free(&remote->remote_fifo);
1670 wacom->remote = NULL;
1671}
1672
1673static int wacom_initialize_remotes(struct wacom *wacom)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001674{
1675 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001676 struct wacom_remote *remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001677 int i;
1678
1679 if (wacom->wacom_wac.features.type != REMOTE)
1680 return 0;
1681
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001682 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1683 GFP_KERNEL);
1684 if (!remote)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001685 return -ENOMEM;
1686
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001687 wacom->remote = remote;
1688
1689 spin_lock_init(&remote->remote_lock);
1690
1691 error = kfifo_alloc(&remote->remote_fifo,
1692 5 * sizeof(struct wacom_remote_data),
1693 GFP_KERNEL);
1694 if (error) {
1695 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1696 return -ENOMEM;
1697 }
1698
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001699 remote->remotes[0].group = remote0_serial_group;
1700 remote->remotes[1].group = remote1_serial_group;
1701 remote->remotes[2].group = remote2_serial_group;
1702 remote->remotes[3].group = remote3_serial_group;
1703 remote->remotes[4].group = remote4_serial_group;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001704
1705 remote->remote_dir = kobject_create_and_add("wacom_remote",
1706 &wacom->hdev->dev.kobj);
1707 if (!remote->remote_dir)
1708 return -ENOMEM;
1709
1710 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001711
1712 if (error) {
1713 hid_err(wacom->hdev,
1714 "cannot create sysfs group err: %d\n", error);
1715 return error;
1716 }
1717
1718 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001719 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001720 remote->remotes[i].serial = 0;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001721 }
1722
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001723 error = devm_add_action_or_reset(&wacom->hdev->dev,
1724 wacom_remotes_destroy, wacom);
1725 if (error)
1726 return error;
1727
Aaron Skomra72b236d2015-08-20 16:05:17 -07001728 return 0;
1729}
1730
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001731static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001732{
1733 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001734 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001735 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001736
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001737 input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001738 if (!input_dev)
1739 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001740
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001741 input_dev->name = wacom_wac->features.name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001742 input_dev->phys = hdev->phys;
1743 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001744 input_dev->open = wacom_open;
1745 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001746 input_dev->uniq = hdev->uniq;
1747 input_dev->id.bustype = hdev->bus;
1748 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001749 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001750 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001751 input_set_drvdata(input_dev, wacom);
1752
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001753 return input_dev;
1754}
1755
Jason Gerecked9f2d202015-07-13 18:03:44 -07001756static int wacom_allocate_inputs(struct wacom *wacom)
1757{
1758 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1759
1760 wacom_wac->pen_input = wacom_allocate_input(wacom);
1761 wacom_wac->touch_input = wacom_allocate_input(wacom);
1762 wacom_wac->pad_input = wacom_allocate_input(wacom);
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001763 if (!wacom_wac->pen_input ||
1764 !wacom_wac->touch_input ||
1765 !wacom_wac->pad_input)
Jason Gerecked9f2d202015-07-13 18:03:44 -07001766 return -ENOMEM;
Jason Gerecked9f2d202015-07-13 18:03:44 -07001767
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001768 wacom_wac->pen_input->name = wacom_wac->pen_name;
Jason Gerecked9f2d202015-07-13 18:03:44 -07001769 wacom_wac->touch_input->name = wacom_wac->touch_name;
1770 wacom_wac->pad_input->name = wacom_wac->pad_name;
1771
1772 return 0;
1773}
1774
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001775static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001776{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001777 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001778 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Jason Gerecke2636a3f2015-06-15 18:01:44 -07001779 int error = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001780
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001781 pen_input_dev = wacom_wac->pen_input;
1782 touch_input_dev = wacom_wac->touch_input;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001783 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001784
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001785 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001786 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001787
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001788 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1789 if (error) {
1790 /* no pen in use on this interface */
1791 input_free_device(pen_input_dev);
1792 wacom_wac->pen_input = NULL;
1793 pen_input_dev = NULL;
1794 } else {
1795 error = input_register_device(pen_input_dev);
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001796 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001797 goto fail;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001798 }
1799
1800 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1801 if (error) {
1802 /* no touch in use on this interface */
1803 input_free_device(touch_input_dev);
1804 wacom_wac->touch_input = NULL;
1805 touch_input_dev = NULL;
1806 } else {
1807 error = input_register_device(touch_input_dev);
1808 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001809 goto fail;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001810 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001811
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001812 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1813 if (error) {
1814 /* no pad in use on this interface */
1815 input_free_device(pad_input_dev);
1816 wacom_wac->pad_input = NULL;
1817 pad_input_dev = NULL;
1818 } else {
1819 error = input_register_device(pad_input_dev);
1820 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001821 goto fail;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001822 }
1823
Ping Cheng19635182012-04-29 21:09:18 -07001824 return 0;
1825
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001826fail:
1827 wacom_wac->pad_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001828 wacom_wac->touch_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001829 wacom_wac->pen_input = NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001830 return error;
1831}
1832
Jason Gerecke0be01712015-08-05 15:44:53 -07001833/*
1834 * Not all devices report physical dimensions from HID.
1835 * Compute the default from hardcoded logical dimension
1836 * and resolution before driver overwrites them.
1837 */
1838static void wacom_set_default_phy(struct wacom_features *features)
1839{
1840 if (features->x_resolution) {
1841 features->x_phy = (features->x_max * 100) /
1842 features->x_resolution;
1843 features->y_phy = (features->y_max * 100) /
1844 features->y_resolution;
1845 }
1846}
1847
1848static void wacom_calculate_res(struct wacom_features *features)
1849{
1850 /* set unit to "100th of a mm" for devices not reported by HID */
1851 if (!features->unit) {
1852 features->unit = 0x11;
1853 features->unitExpo = -3;
1854 }
1855
1856 features->x_resolution = wacom_calc_hid_res(features->x_max,
1857 features->x_phy,
1858 features->unit,
1859 features->unitExpo);
1860 features->y_resolution = wacom_calc_hid_res(features->y_max,
1861 features->y_phy,
1862 features->unit,
1863 features->unitExpo);
1864}
1865
Jason Gereckefce99572015-03-06 11:47:40 -08001866void wacom_battery_work(struct work_struct *work)
1867{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02001868 struct wacom *wacom = container_of(work, struct wacom, battery_work);
Jason Gereckefce99572015-03-06 11:47:40 -08001869
1870 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001871 !wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001872 wacom_initialize_battery(wacom);
1873 }
1874 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001875 wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001876 wacom_destroy_battery(wacom);
1877 }
1878}
1879
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001880static size_t wacom_compute_pktlen(struct hid_device *hdev)
1881{
1882 struct hid_report_enum *report_enum;
1883 struct hid_report *report;
1884 size_t size = 0;
1885
1886 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1887
1888 list_for_each_entry(report, &report_enum->report_list, list) {
Mathieu Magnaudetdabb05c62014-11-27 16:02:36 +01001889 size_t report_size = hid_report_len(report);
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001890 if (report_size > size)
1891 size = report_size;
1892 }
1893
1894 return size;
1895}
1896
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001897static void wacom_update_name(struct wacom *wacom, const char *suffix)
Ping Chengc24eab42015-04-24 15:32:51 -07001898{
1899 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1900 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke44b52502015-06-15 18:01:41 -07001901 char name[WACOM_NAME_MAX];
Ping Chengc24eab42015-04-24 15:32:51 -07001902
1903 /* Generic devices name unspecified */
1904 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1905 if (strstr(wacom->hdev->name, "Wacom") ||
1906 strstr(wacom->hdev->name, "wacom") ||
1907 strstr(wacom->hdev->name, "WACOM")) {
1908 /* name is in HID descriptor, use it */
Jason Gerecke44b52502015-06-15 18:01:41 -07001909 strlcpy(name, wacom->hdev->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001910
1911 /* strip out excess whitespaces */
1912 while (1) {
Jason Gerecke44b52502015-06-15 18:01:41 -07001913 char *gap = strstr(name, " ");
Ping Chengc24eab42015-04-24 15:32:51 -07001914 if (gap == NULL)
1915 break;
1916 /* shift everything including the terminator */
1917 memmove(gap, gap+1, strlen(gap));
1918 }
1919 /* get rid of trailing whitespace */
Jason Gerecke44b52502015-06-15 18:01:41 -07001920 if (name[strlen(name)-1] == ' ')
1921 name[strlen(name)-1] = '\0';
Ping Chengc24eab42015-04-24 15:32:51 -07001922 } else {
1923 /* no meaningful name retrieved. use product ID */
Jason Gerecke44b52502015-06-15 18:01:41 -07001924 snprintf(name, sizeof(name),
Ping Chengc24eab42015-04-24 15:32:51 -07001925 "%s %X", features->name, wacom->hdev->product);
1926 }
1927 } else {
Jason Gerecke44b52502015-06-15 18:01:41 -07001928 strlcpy(name, features->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001929 }
1930
Benjamin Tissoires99569532016-07-13 18:06:17 +02001931 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
1932 name, suffix);
1933
Ping Chengc24eab42015-04-24 15:32:51 -07001934 /* Append the device type to the name */
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001935 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001936 "%s%s Pen", name, suffix);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001937 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001938 "%s%s Finger", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07001939 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001940 "%s%s Pad", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07001941}
1942
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02001943static void wacom_release_resources(struct wacom *wacom)
1944{
1945 struct hid_device *hdev = wacom->hdev;
1946
1947 if (!wacom->resources)
1948 return;
1949
1950 devres_release_group(&hdev->dev, wacom);
1951
1952 wacom->resources = false;
1953
1954 wacom->wacom_wac.pen_input = NULL;
1955 wacom->wacom_wac.touch_input = NULL;
1956 wacom->wacom_wac.pad_input = NULL;
1957}
1958
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001959static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
Ping Cheng3bea7332006-07-13 18:01:36 -07001960{
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001961 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1962 struct wacom_features *features = &wacom_wac->features;
1963 struct hid_device *hdev = wacom->hdev;
Jason Childse33da8a2010-02-17 22:38:31 -08001964 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001965 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07001966
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001967 features->pktlen = wacom_compute_pktlen(hdev);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001968 if (features->pktlen > WACOM_PKGLEN_MAX)
1969 return -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001970
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02001971 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
1972 return -ENOMEM;
1973
1974 wacom->resources = true;
1975
Jason Gerecke3f14a632015-08-03 10:17:05 -07001976 error = wacom_allocate_inputs(wacom);
1977 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02001978 goto fail;
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001979
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05001980 /*
1981 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
1982 * into debug mode for the touch part.
1983 * We ignore the other interfaces.
1984 */
1985 if (features->type == BAMBOO_PAD) {
1986 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
1987 features->type = HID_GENERIC;
1988 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
1989 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
1990 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02001991 goto fail;
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05001992 }
1993 }
1994
Ping Cheng401d7d12013-07-28 00:38:54 -07001995 /* set the default size in case we do not get them from hid */
1996 wacom_set_default_phy(features);
1997
Ping Chengf393ee22012-04-29 21:09:17 -07001998 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001999 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng42f4f272015-04-15 16:53:54 -07002000 wacom_setup_device_quirks(wacom);
Jason Gerecke042628a2015-04-30 17:51:54 -07002001
Jason Gereckeaa86b182015-06-15 18:01:42 -07002002 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2003 features->type != WIRELESS) {
Jason Gerecke8e116d32015-04-30 17:51:55 -07002004 error = features->type == HID_GENERIC ? -ENODEV : 0;
2005
Jason Gerecke042628a2015-04-30 17:51:54 -07002006 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
Jason Gerecke8e116d32015-04-30 17:51:55 -07002007 hdev->name,
2008 error ? "Ignoring" : "Assuming pen");
2009
2010 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002011 goto fail;
Jason Gerecke042628a2015-04-30 17:51:54 -07002012
Jason Gereckeaa86b182015-06-15 18:01:42 -07002013 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -07002014 }
2015
Ping Cheng401d7d12013-07-28 00:38:54 -07002016 wacom_calculate_res(features);
2017
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002018 wacom_update_name(wacom, wireless ? " (WL)" : "");
Ping Cheng4492eff2010-03-19 22:18:15 -07002019
Ping Chengf3586d22015-03-20 14:57:00 -07002020 error = wacom_add_shared_data(hdev);
2021 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002022 goto fail;
Ping Cheng49b764a2010-02-20 00:53:49 -08002023
Jason Gereckeccad85c2015-08-03 10:17:04 -07002024 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002025 (features->quirks & WACOM_QUIRK_BATTERY)) {
2026 error = wacom_initialize_battery(wacom);
2027 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002028 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002029 }
2030
Jason Gerecke3f14a632015-08-03 10:17:05 -07002031 error = wacom_register_inputs(wacom);
2032 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002033 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002034
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002035 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002036 error = wacom_initialize_leds(wacom);
2037 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002038 goto fail;
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002039
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002040 error = wacom_initialize_remotes(wacom);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002041 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002042 goto fail;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002043 }
2044
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002045 if (features->type == HID_GENERIC)
2046 connect_mask |= HID_CONNECT_DRIVER;
2047
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002048 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002049 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002050 if (error) {
2051 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002052 goto fail;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002053 }
2054
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002055 if (!wireless) {
2056 /* Note that if query fails it is not a hard failure */
2057 wacom_query_tablet_data(hdev, features);
2058 }
Jason Gerecke86e88f02015-11-03 16:57:58 -08002059
2060 /* touch only Bamboo doesn't support pen */
2061 if ((features->type == BAMBOO_TOUCH) &&
2062 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2063 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002064 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08002065 }
2066
2067 /* pen only Bamboo neither support touch nor pad */
2068 if ((features->type == BAMBOO_PEN) &&
2069 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2070 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2071 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002072 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08002073 }
2074
Jason Gereckeccad85c2015-08-03 10:17:04 -07002075 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002076 error = hid_hw_open(hdev);
2077
Ping Chengeda01da2015-09-23 13:51:15 -07002078 if ((wacom_wac->features.type == INTUOSHT ||
Benjamin Tissoires97f9afa2016-07-13 18:05:49 +02002079 wacom_wac->features.type == INTUOSHT2) &&
Ping Chengeda01da2015-09-23 13:51:15 -07002080 (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
Benjamin Tissoires97f9afa2016-07-13 18:05:49 +02002081 wacom_wac->shared->type = wacom_wac->features.type;
2082 wacom_wac->shared->touch_input = wacom_wac->touch_input;
Ping Cheng961794a2013-12-05 12:54:53 -08002083 }
2084
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002085 devres_close_group(&hdev->dev, wacom);
2086
Ping Cheng3bea7332006-07-13 18:01:36 -07002087 return 0;
2088
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002089fail_quirks:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002090 hid_hw_stop(hdev);
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002091fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002092 wacom_release_resources(wacom);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002093 return error;
2094}
2095
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002096static void wacom_wireless_work(struct work_struct *work)
2097{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002098 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002099 struct usb_device *usbdev = wacom->usbdev;
2100 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2101 struct hid_device *hdev1, *hdev2;
2102 struct wacom *wacom1, *wacom2;
2103 struct wacom_wac *wacom_wac1, *wacom_wac2;
2104 int error;
2105
2106 /*
2107 * Regardless if this is a disconnect or a new tablet,
2108 * remove any existing input and battery devices.
2109 */
2110
2111 wacom_destroy_battery(wacom);
2112
2113 /* Stylus interface */
2114 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2115 wacom1 = hid_get_drvdata(hdev1);
2116 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002117 wacom_release_resources(wacom1);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002118
2119 /* Touch interface */
2120 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2121 wacom2 = hid_get_drvdata(hdev2);
2122 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002123 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002124
2125 if (wacom_wac->pid == 0) {
2126 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002127 } else {
2128 const struct hid_device_id *id = wacom_ids;
2129
2130 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2131 wacom_wac->pid);
2132
2133 while (id->bus) {
2134 if (id->vendor == USB_VENDOR_ID_WACOM &&
2135 id->product == wacom_wac->pid)
2136 break;
2137 id++;
2138 }
2139
2140 if (!id->bus) {
2141 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2142 return;
2143 }
2144
2145 /* Stylus interface */
2146 wacom_wac1->features =
2147 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002148
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002149 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002150 hid_hw_stop(hdev1);
2151 error = wacom_parse_and_register(wacom1, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002152 if (error)
2153 goto fail;
2154
2155 /* Touch interface */
2156 if (wacom_wac1->features.touch_max ||
2157 (wacom_wac1->features.type >= INTUOSHT &&
2158 wacom_wac1->features.type <= BAMBOO_PT)) {
2159 wacom_wac2->features =
2160 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002161 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002162 hid_hw_stop(hdev2);
2163 error = wacom_parse_and_register(wacom2, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002164 if (error)
2165 goto fail;
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002166 }
2167
Benjamin Tissoires99569532016-07-13 18:06:17 +02002168 strlcpy(wacom_wac->name, wacom_wac1->name,
2169 sizeof(wacom_wac->name));
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002170 error = wacom_initialize_battery(wacom);
2171 if (error)
2172 goto fail;
2173 }
2174
2175 return;
2176
2177fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002178 wacom_release_resources(wacom1);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002179 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002180 return;
2181}
2182
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002183static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2184{
2185 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002186 u32 serial = remote->remotes[index].serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002187 int i;
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002188 unsigned long flags;
2189
2190 spin_lock_irqsave(&remote->remote_lock, flags);
2191 remote->remotes[index].registered = false;
2192 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002193
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002194 if (remote->remotes[index].battery.battery)
2195 devres_release_group(&wacom->hdev->dev,
2196 &remote->remotes[index].battery.bat_desc);
2197
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002198 if (remote->remotes[index].group.name)
2199 devres_release_group(&wacom->hdev->dev,
2200 &remote->remotes[index]);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002201
2202 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002203 if (remote->remotes[i].serial == serial) {
2204 remote->remotes[i].serial = 0;
2205 remote->remotes[i].group.name = NULL;
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002206 remote->remotes[i].registered = false;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002207 remote->remotes[i].battery.battery = NULL;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002208 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2209 }
2210 }
2211}
2212
2213static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2214 unsigned int index)
2215{
2216 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002217 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002218 int error, k;
2219
2220 /* A remote can pair more than once with an EKR,
2221 * check to make sure this serial isn't already paired.
2222 */
2223 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002224 if (remote->remotes[k].serial == serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002225 break;
2226 }
2227
2228 if (k < WACOM_MAX_REMOTES) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002229 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002230 return 0;
2231 }
2232
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002233 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002234 return -ENOMEM;
2235
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002236 error = wacom_remote_create_attr_group(wacom, serial, index);
2237 if (error)
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002238 goto fail;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002239
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002240 remote->remotes[index].input = wacom_allocate_input(wacom);
2241 if (!remote->remotes[index].input) {
2242 error = -ENOMEM;
2243 goto fail;
2244 }
2245 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2246 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2247
2248 if (!remote->remotes[index].input->name) {
2249 error = -EINVAL;
2250 goto fail;
2251 }
2252
2253 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2254 &wacom->wacom_wac);
2255 if (error)
2256 goto fail;
2257
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002258 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002259
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002260 error = input_register_device(remote->remotes[index].input);
2261 if (error)
2262 goto fail;
2263
Benjamin Tissoires97f55412016-07-13 18:06:10 +02002264 error = wacom_led_groups_alloc_and_register_one(
2265 &remote->remotes[index].input->dev,
2266 wacom, index, 3, true);
2267 if (error)
2268 goto fail;
2269
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002270 remote->remotes[index].registered = true;
2271
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002272 devres_close_group(dev, &remote->remotes[index]);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002273 return 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002274
2275fail:
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002276 devres_release_group(dev, &remote->remotes[index]);
2277 remote->remotes[index].serial = 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002278 return error;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002279}
2280
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002281static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2282{
2283 struct wacom_remote *remote = wacom->remote;
2284 int error;
2285
2286 if (!remote->remotes[index].registered)
2287 return 0;
2288
2289 if (remote->remotes[index].battery.battery)
2290 return 0;
2291
2292 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2293 return 0;
2294
2295 error = __wacom_initialize_battery(wacom,
2296 &wacom->remote->remotes[index].battery);
2297 if (error)
2298 return error;
2299
2300 return 0;
2301}
2302
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002303static void wacom_remote_work(struct work_struct *work)
2304{
2305 struct wacom *wacom = container_of(work, struct wacom, remote_work);
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002306 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002307 struct wacom_remote_data data;
2308 unsigned long flags;
2309 unsigned int count;
2310 u32 serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002311 int i;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002312
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002313 spin_lock_irqsave(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002314
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002315 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002316
2317 if (count != sizeof(data)) {
2318 hid_err(wacom->hdev,
2319 "workitem triggered without status available\n");
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002320 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002321 return;
2322 }
2323
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002324 if (!kfifo_is_empty(&remote->remote_fifo))
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002325 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2326
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002327 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002328
2329 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2330 serial = data.remote[i].serial;
2331 if (data.remote[i].connected) {
2332
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002333 if (remote->remotes[i].serial == serial) {
2334 wacom_remote_attach_battery(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002335 continue;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002336 }
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002337
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002338 if (remote->remotes[i].serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002339 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002340
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002341 wacom_remote_create_one(wacom, serial, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002342
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002343 } else if (remote->remotes[i].serial) {
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002344 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002345 }
2346 }
2347}
2348
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002349static int wacom_probe(struct hid_device *hdev,
2350 const struct hid_device_id *id)
2351{
2352 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2353 struct usb_device *dev = interface_to_usbdev(intf);
2354 struct wacom *wacom;
2355 struct wacom_wac *wacom_wac;
2356 struct wacom_features *features;
2357 int error;
2358
2359 if (!id->driver_data)
2360 return -EINVAL;
2361
2362 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2363
2364 /* hid-core sets this quirk for the boot interface */
2365 hdev->quirks &= ~HID_QUIRK_NOGET;
2366
Benjamin Tissoires19b64332016-07-13 18:05:58 +02002367 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002368 if (!wacom)
2369 return -ENOMEM;
2370
2371 hid_set_drvdata(hdev, wacom);
2372 wacom->hdev = hdev;
2373
2374 wacom_wac = &wacom->wacom_wac;
2375 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2376 features = &wacom_wac->features;
2377
2378 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2379 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002380 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002381 }
2382
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002383 wacom_wac->hid_data.inputmode = -1;
Jason Gerecke326ea2a2016-04-04 11:26:52 -07002384 wacom_wac->mode_report = -1;
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002385
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002386 wacom->usbdev = dev;
2387 wacom->intf = intf;
2388 mutex_init(&wacom->lock);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002389 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2390 INIT_WORK(&wacom->battery_work, wacom_battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002391 INIT_WORK(&wacom->remote_work, wacom_remote_work);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002392
2393 /* ask for the report descriptor to be loaded by HID */
2394 error = hid_parse(hdev);
2395 if (error) {
2396 hid_err(hdev, "parse failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002397 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002398 }
2399
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002400 error = wacom_parse_and_register(wacom, false);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002401 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002402 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002403
2404 if (hdev->bus == BUS_BLUETOOTH) {
2405 error = device_create_file(&hdev->dev, &dev_attr_speed);
2406 if (error)
2407 hid_warn(hdev,
2408 "can't create sysfs speed attribute err: %d\n",
2409 error);
2410 }
2411
2412 return 0;
2413
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002414fail:
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002415 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04002416 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07002417}
2418
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002419static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07002420{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002421 struct wacom *wacom = hid_get_drvdata(hdev);
Benjamin Tissoiresf6205162016-02-12 17:27:45 +01002422 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2423 struct wacom_features *features = &wacom_wac->features;
2424
2425 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2426 hid_hw_close(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07002427
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002428 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002429
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002430 cancel_work_sync(&wacom->wireless_work);
2431 cancel_work_sync(&wacom->battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002432 cancel_work_sync(&wacom->remote_work);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002433 if (hdev->bus == BUS_BLUETOOTH)
2434 device_remove_file(&hdev->dev, &dev_attr_speed);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002435
2436 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04002437}
2438
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002439#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002440static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002441{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002442 struct wacom *wacom = hid_get_drvdata(hdev);
2443 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04002444
2445 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002446
2447 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07002448 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002449 wacom_led_control(wacom);
2450
Oliver Neukume7224092008-04-15 01:31:57 -04002451 mutex_unlock(&wacom->lock);
2452
2453 return 0;
2454}
2455
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002456static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002457{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002458 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002459}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002460#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04002461
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002462static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07002463 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08002464 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07002465 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002466 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002467 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002468#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04002469 .resume = wacom_resume,
2470 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002471#endif
2472 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07002473};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002474module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07002475
2476MODULE_VERSION(DRIVER_VERSION);
2477MODULE_AUTHOR(DRIVER_AUTHOR);
2478MODULE_DESCRIPTION(DRIVER_DESC);
2479MODULE_LICENSE(DRIVER_LICENSE);