blob: d8f3d3dc2c8860ca4782d74a8f9175a8f771eaf1 [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
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700530static bool wacom_are_sibling(struct hid_device *hdev,
531 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700532{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700533 struct wacom *wacom = hid_get_drvdata(hdev);
534 struct wacom_features *features = &wacom->wacom_wac.features;
535 int vid = features->oVid;
536 int pid = features->oPid;
537 int n1,n2;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700538
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700539 if (vid == 0 && pid == 0) {
540 vid = hdev->vendor;
541 pid = hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700542 }
543
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700544 if (vid != sibling->vendor || pid != sibling->product)
545 return false;
546
547 /* Compare the physical path. */
548 n1 = strrchr(hdev->phys, '.') - hdev->phys;
549 n2 = strrchr(sibling->phys, '.') - sibling->phys;
550 if (n1 != n2 || n1 <= 0 || n2 <= 0)
551 return false;
552
553 return !strncmp(hdev->phys, sibling->phys, n1);
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700554}
555
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700556static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700557{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700558 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700559
560 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700561 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700562 kref_get(&data->kref);
563 return data;
564 }
565 }
566
567 return NULL;
568}
569
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200570static void wacom_release_shared_data(struct kref *kref)
571{
572 struct wacom_hdev_data *data =
573 container_of(kref, struct wacom_hdev_data, kref);
574
575 mutex_lock(&wacom_udev_list_lock);
576 list_del(&data->list);
577 mutex_unlock(&wacom_udev_list_lock);
578
579 kfree(data);
580}
581
582static void wacom_remove_shared_data(void *res)
583{
584 struct wacom *wacom = res;
585 struct wacom_hdev_data *data;
586 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
587
588 if (wacom_wac->shared) {
589 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
590 shared);
591
592 if (wacom_wac->shared->touch == wacom->hdev)
593 wacom_wac->shared->touch = NULL;
594 else if (wacom_wac->shared->pen == wacom->hdev)
595 wacom_wac->shared->pen = NULL;
596
597 kref_put(&data->kref, wacom_release_shared_data);
598 wacom_wac->shared = NULL;
599 }
600}
601
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700602static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700603{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700604 struct wacom *wacom = hid_get_drvdata(hdev);
605 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
606 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700607 int retval = 0;
608
609 mutex_lock(&wacom_udev_list_lock);
610
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700611 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700612 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700613 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700614 if (!data) {
615 retval = -ENOMEM;
616 goto out;
617 }
618
619 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700620 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700621 list_add_tail(&data->list, &wacom_udev_list);
622 }
623
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700624 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700625
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200626 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
627 if (retval) {
628 mutex_unlock(&wacom_udev_list_lock);
629 wacom_remove_shared_data(wacom);
630 return retval;
631 }
632
Jason Gereckeaa86b182015-06-15 18:01:42 -0700633 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500634 wacom_wac->shared->touch = hdev;
Jason Gereckeaa86b182015-06-15 18:01:42 -0700635 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500636 wacom_wac->shared->pen = hdev;
637
Ping Cheng4492eff2010-03-19 22:18:15 -0700638out:
639 mutex_unlock(&wacom_udev_list_lock);
640 return retval;
641}
642
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700643static int wacom_led_control(struct wacom *wacom)
644{
645 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700646 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700647 unsigned char report_id = WAC_CMD_LED_CONTROL;
648 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700649
Benjamin Tissoires97f55412016-07-13 18:06:10 +0200650 if (!hid_get_drvdata(wacom->hdev))
651 return -ENODEV;
652
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200653 if (!wacom->led.groups)
654 return -ENOTSUPP;
655
Ping Cheng912ca212014-09-10 12:41:31 -0700656 if (wacom->wacom_wac.pid) { /* wireless connected */
657 report_id = WAC_CMD_WL_LED_CONTROL;
658 buf_size = 13;
659 }
660 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700661 if (!buf)
662 return -ENOMEM;
663
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700664 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700665 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700666 /*
667 * Touch Ring and crop mark LED luminance may take on
668 * one of four values:
669 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
670 */
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200671 int ring_led = wacom->led.groups[0].select & 0x03;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700672 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
673 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700674 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700675
Ping Cheng912ca212014-09-10 12:41:31 -0700676 buf[0] = report_id;
677 if (wacom->wacom_wac.pid) {
678 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
679 buf, buf_size, WAC_CMD_RETRIES);
680 buf[0] = report_id;
681 buf[4] = led_bits;
682 } else
683 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700684 }
685 else {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200686 int led = wacom->led.groups[0].select | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700687
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700688 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
689 wacom->wacom_wac.features.type == WACOM_24HD)
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200690 led |= (wacom->led.groups[1].select << 4) | 0x40;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700691
Ping Cheng912ca212014-09-10 12:41:31 -0700692 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700693 buf[1] = led;
694 buf[2] = wacom->led.llv;
695 buf[3] = wacom->led.hlv;
696 buf[4] = wacom->led.img_lum;
697 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700698
Ping Cheng912ca212014-09-10 12:41:31 -0700699 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700700 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700701 kfree(buf);
702
703 return retval;
704}
705
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700706static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
707 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700708{
709 unsigned char *buf;
710 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700711 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700712
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700713 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700714 if (!buf)
715 return -ENOMEM;
716
717 /* Send 'start' command */
718 buf[0] = WAC_CMD_ICON_START;
719 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700720 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
721 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700722 if (retval < 0)
723 goto out;
724
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700725 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700726 buf[1] = button_id & 0x07;
727 for (i = 0; i < 4; i++) {
728 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700729 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700730
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700731 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700732 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700733 if (retval < 0)
734 break;
735 }
736
737 /* Send 'stop' */
738 buf[0] = WAC_CMD_ICON_START;
739 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700740 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
741 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700742
743out:
744 kfree(buf);
745 return retval;
746}
747
Ping Cheng09e7d942011-10-04 23:51:14 -0700748static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700749 const char *buf, size_t count)
750{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800751 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700752 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700753 unsigned int id;
754 int err;
755
756 err = kstrtouint(buf, 10, &id);
757 if (err)
758 return err;
759
760 mutex_lock(&wacom->lock);
761
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200762 wacom->led.groups[set_id].select = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700763 err = wacom_led_control(wacom);
764
765 mutex_unlock(&wacom->lock);
766
767 return err < 0 ? err : count;
768}
769
Ping Cheng09e7d942011-10-04 23:51:14 -0700770#define DEVICE_LED_SELECT_ATTR(SET_ID) \
771static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
772 struct device_attribute *attr, const char *buf, size_t count) \
773{ \
774 return wacom_led_select_store(dev, SET_ID, buf, count); \
775} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700776static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
777 struct device_attribute *attr, char *buf) \
778{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800779 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700780 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700781 return scnprintf(buf, PAGE_SIZE, "%d\n", \
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200782 wacom->led.groups[SET_ID].select); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700783} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700784static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700785 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700786 wacom_led##SET_ID##_select_store)
787
788DEVICE_LED_SELECT_ATTR(0);
789DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700790
791static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
792 const char *buf, size_t count)
793{
794 unsigned int value;
795 int err;
796
797 err = kstrtouint(buf, 10, &value);
798 if (err)
799 return err;
800
801 mutex_lock(&wacom->lock);
802
803 *dest = value & 0x7f;
804 err = wacom_led_control(wacom);
805
806 mutex_unlock(&wacom->lock);
807
808 return err < 0 ? err : count;
809}
810
811#define DEVICE_LUMINANCE_ATTR(name, field) \
812static ssize_t wacom_##name##_luminance_store(struct device *dev, \
813 struct device_attribute *attr, const char *buf, size_t count) \
814{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800815 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700816 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700817 \
818 return wacom_luminance_store(wacom, &wacom->led.field, \
819 buf, count); \
820} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700821static ssize_t wacom_##name##_luminance_show(struct device *dev, \
822 struct device_attribute *attr, char *buf) \
823{ \
824 struct wacom *wacom = dev_get_drvdata(dev); \
825 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
826} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700827static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700828 wacom_##name##_luminance_show, \
829 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700830
831DEVICE_LUMINANCE_ATTR(status0, llv);
832DEVICE_LUMINANCE_ATTR(status1, hlv);
833DEVICE_LUMINANCE_ATTR(buttons, img_lum);
834
835static ssize_t wacom_button_image_store(struct device *dev, int button_id,
836 const char *buf, size_t count)
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);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700840 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700841 unsigned len;
842 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700843
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700844 if (hdev->bus == BUS_BLUETOOTH) {
845 len = 256;
846 xfer_id = WAC_CMD_ICON_BT_XFER;
847 } else {
848 len = 1024;
849 xfer_id = WAC_CMD_ICON_XFER;
850 }
851
852 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700853 return -EINVAL;
854
855 mutex_lock(&wacom->lock);
856
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700857 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700858
859 mutex_unlock(&wacom->lock);
860
861 return err < 0 ? err : count;
862}
863
864#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
865static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
866 struct device_attribute *attr, const char *buf, size_t count) \
867{ \
868 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
869} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700870static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700871 NULL, wacom_btnimg##BUTTON_ID##_store)
872
873DEVICE_BTNIMG_ATTR(0);
874DEVICE_BTNIMG_ATTR(1);
875DEVICE_BTNIMG_ATTR(2);
876DEVICE_BTNIMG_ATTR(3);
877DEVICE_BTNIMG_ATTR(4);
878DEVICE_BTNIMG_ATTR(5);
879DEVICE_BTNIMG_ATTR(6);
880DEVICE_BTNIMG_ATTR(7);
881
Ping Cheng09e7d942011-10-04 23:51:14 -0700882static struct attribute *cintiq_led_attrs[] = {
883 &dev_attr_status_led0_select.attr,
884 &dev_attr_status_led1_select.attr,
885 NULL
886};
887
888static struct attribute_group cintiq_led_attr_group = {
889 .name = "wacom_led",
890 .attrs = cintiq_led_attrs,
891};
892
893static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700894 &dev_attr_status0_luminance.attr,
895 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700896 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700897 &dev_attr_buttons_luminance.attr,
898 &dev_attr_button0_rawimg.attr,
899 &dev_attr_button1_rawimg.attr,
900 &dev_attr_button2_rawimg.attr,
901 &dev_attr_button3_rawimg.attr,
902 &dev_attr_button4_rawimg.attr,
903 &dev_attr_button5_rawimg.attr,
904 &dev_attr_button6_rawimg.attr,
905 &dev_attr_button7_rawimg.attr,
906 NULL
907};
908
Ping Cheng09e7d942011-10-04 23:51:14 -0700909static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700910 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700911 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700912};
913
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700914static struct attribute *intuos5_led_attrs[] = {
915 &dev_attr_status0_luminance.attr,
916 &dev_attr_status_led0_select.attr,
917 NULL
918};
919
920static struct attribute_group intuos5_led_attr_group = {
921 .name = "wacom_led",
922 .attrs = intuos5_led_attrs,
923};
924
Benjamin Tissoires2df68a82016-07-13 18:05:56 +0200925struct wacom_sysfs_group_devres {
926 struct attribute_group *group;
927 struct kobject *root;
928};
929
930static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
931{
932 struct wacom_sysfs_group_devres *devres = res;
933 struct kobject *kobj = devres->root;
934
935 dev_dbg(dev, "%s: dropping reference to %s\n",
936 __func__, devres->group->name);
937 sysfs_remove_group(kobj, devres->group);
938}
939
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +0200940static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
941 struct kobject *root,
942 struct attribute_group *group)
Benjamin Tissoires2df68a82016-07-13 18:05:56 +0200943{
944 struct wacom_sysfs_group_devres *devres;
945 int error;
946
947 devres = devres_alloc(wacom_devm_sysfs_group_release,
948 sizeof(struct wacom_sysfs_group_devres),
949 GFP_KERNEL);
950 if (!devres)
951 return -ENOMEM;
952
953 devres->group = group;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +0200954 devres->root = root;
Benjamin Tissoires2df68a82016-07-13 18:05:56 +0200955
956 error = sysfs_create_group(devres->root, group);
957 if (error)
958 return error;
959
960 devres_add(&wacom->hdev->dev, devres);
961
962 return 0;
963}
964
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +0200965static int wacom_devm_sysfs_create_group(struct wacom *wacom,
966 struct attribute_group *group)
967{
968 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
969 group);
970}
971
Benjamin Tissoires34736aa2016-07-13 18:06:12 +0200972enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
Benjamin Tissoires97f55412016-07-13 18:06:10 +0200973{
974 struct wacom *wacom = led->wacom;
975
976 if (wacom->led.max_hlv)
977 return led->hlv * LED_FULL / wacom->led.max_hlv;
978
979 if (wacom->led.max_llv)
980 return led->llv * LED_FULL / wacom->led.max_llv;
981
982 /* device doesn't support brightness tuning */
983 return LED_FULL;
984}
985
986static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
987{
988 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
989 struct wacom *wacom = led->wacom;
990
991 if (wacom->led.groups[led->group].select != led->id)
992 return LED_OFF;
993
994 return wacom_leds_brightness_get(led);
995}
996
997static int wacom_led_brightness_set(struct led_classdev *cdev,
998 enum led_brightness brightness)
999{
1000 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1001 struct wacom *wacom = led->wacom;
1002 int error;
1003
1004 mutex_lock(&wacom->lock);
1005
1006 if (!wacom->led.groups || (brightness == LED_OFF &&
1007 wacom->led.groups[led->group].select != led->id)) {
1008 error = 0;
1009 goto out;
1010 }
1011
1012 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1013 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1014
1015 wacom->led.groups[led->group].select = led->id;
1016
1017 error = wacom_led_control(wacom);
1018
1019out:
1020 mutex_unlock(&wacom->lock);
1021
1022 return error;
1023}
1024
1025static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1026 enum led_brightness brightness)
1027{
1028}
1029
1030static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1031 struct wacom_led *led, unsigned int group,
1032 unsigned int id, bool read_only)
1033{
1034 int error;
1035 char *name;
1036
1037 name = devm_kasprintf(dev, GFP_KERNEL,
1038 "%s::wacom-%d.%d",
1039 dev_name(dev),
1040 group,
1041 id);
1042 if (!name)
1043 return -ENOMEM;
1044
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001045 if (!read_only) {
1046 led->trigger.name = name;
1047 error = devm_led_trigger_register(dev, &led->trigger);
1048 if (error) {
1049 hid_err(wacom->hdev,
1050 "failed to register LED trigger %s: %d\n",
1051 led->cdev.name, error);
1052 return error;
1053 }
1054 }
1055
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001056 led->group = group;
1057 led->id = id;
1058 led->wacom = wacom;
1059 led->llv = wacom->led.llv;
1060 led->hlv = wacom->led.hlv;
1061 led->cdev.name = name;
1062 led->cdev.max_brightness = LED_FULL;
1063 led->cdev.flags = LED_HW_PLUGGABLE;
1064 led->cdev.brightness_get = __wacom_led_brightness_get;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001065 if (!read_only) {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001066 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001067 led->cdev.default_trigger = led->cdev.name;
1068 } else {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001069 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001070 }
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001071
1072 error = devm_led_classdev_register(dev, &led->cdev);
1073 if (error) {
1074 hid_err(wacom->hdev,
1075 "failed to register LED %s: %d\n",
1076 led->cdev.name, error);
1077 led->cdev.name = NULL;
1078 return error;
1079 }
1080
1081 return 0;
1082}
1083
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001084static void wacom_led_groups_release_one(void *data)
1085{
1086 struct wacom_group_leds *group = data;
1087
1088 devres_release_group(group->dev, group);
1089}
1090
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001091static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1092 struct wacom *wacom,
1093 int group_id, int count,
1094 bool read_only)
1095{
1096 struct wacom_led *leds;
1097 int i, error;
1098
1099 if (group_id >= wacom->led.count || count <= 0)
1100 return -EINVAL;
1101
1102 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1103 return -ENOMEM;
1104
1105 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1106 if (!leds) {
1107 error = -ENOMEM;
1108 goto err;
1109 }
1110
1111 wacom->led.groups[group_id].leds = leds;
1112 wacom->led.groups[group_id].count = count;
1113
1114 for (i = 0; i < count; i++) {
1115 error = wacom_led_register_one(dev, wacom, &leds[i],
1116 group_id, i, read_only);
1117 if (error)
1118 goto err;
1119 }
1120
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001121 wacom->led.groups[group_id].dev = dev;
1122
1123 devres_close_group(dev, &wacom->led.groups[group_id]);
1124
1125 /*
1126 * There is a bug (?) in devm_led_classdev_register() in which its
1127 * increments the refcount of the parent. If the parent is an input
1128 * device, that means the ref count never reaches 0 when
1129 * devm_input_device_release() gets called.
1130 * This means that the LEDs are still there after disconnect.
1131 * Manually force the release of the group so that the leds are released
1132 * once we are done using them.
1133 */
1134 error = devm_add_action_or_reset(&wacom->hdev->dev,
1135 wacom_led_groups_release_one,
1136 &wacom->led.groups[group_id]);
1137 if (error)
1138 return error;
1139
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001140 return 0;
1141
1142err:
1143 devres_release_group(dev, &wacom->led.groups[group_id]);
1144 return error;
1145}
1146
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001147struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1148 unsigned int id)
1149{
1150 struct wacom_group_leds *group;
1151
1152 if (group_id >= wacom->led.count)
1153 return NULL;
1154
1155 group = &wacom->led.groups[group_id];
1156
1157 if (!group->leds)
1158 return NULL;
1159
1160 id %= group->count;
1161
1162 return &group->leds[id];
1163}
1164
1165/**
1166 * wacom_led_next: gives the next available led with a wacom trigger.
1167 *
1168 * returns the next available struct wacom_led which has its default trigger
1169 * or the current one if none is available.
1170 */
1171struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1172{
1173 struct wacom_led *next_led;
1174 int group, next;
1175
1176 if (!wacom || !cur)
1177 return NULL;
1178
1179 group = cur->group;
1180 next = cur->id;
1181
1182 do {
1183 next_led = wacom_led_find(wacom, group, ++next);
1184 if (!next_led || next_led == cur)
1185 return next_led;
1186 } while (next_led->cdev.trigger != &next_led->trigger);
1187
1188 return next_led;
1189}
1190
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001191static void wacom_led_groups_release(void *data)
1192{
1193 struct wacom *wacom = data;
1194
1195 wacom->led.groups = NULL;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001196 wacom->led.count = 0;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001197}
1198
1199static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1200{
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001201 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001202 struct wacom_group_leds *groups;
1203 int error;
1204
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001205 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001206 GFP_KERNEL);
1207 if (!groups)
1208 return -ENOMEM;
1209
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001210 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001211 if (error)
1212 return error;
1213
1214 wacom->led.groups = groups;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001215 wacom->led.count = count;
1216
1217 return 0;
1218}
1219
1220static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1221 int led_per_group, bool read_only)
1222{
1223 struct device *dev;
1224 int i, error;
1225
1226 if (!wacom->wacom_wac.pad_input)
1227 return -EINVAL;
1228
1229 dev = &wacom->wacom_wac.pad_input->dev;
1230
1231 error = wacom_led_groups_allocate(wacom, group_count);
1232 if (error)
1233 return error;
1234
1235 for (i = 0; i < group_count; i++) {
1236 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1237 led_per_group,
1238 read_only);
1239 if (error)
1240 return error;
1241 }
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001242
1243 return 0;
1244}
1245
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001246static int wacom_initialize_leds(struct wacom *wacom)
1247{
1248 int error;
1249
Jason Gerecke862cf552015-06-15 18:01:43 -07001250 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1251 return 0;
1252
Ping Cheng09e7d942011-10-04 23:51:14 -07001253 /* Initialize default values */
1254 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -07001255 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -07001256 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -07001257 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -07001258 case INTUOS4L:
Ping Chengf4fa9a62011-10-04 23:49:42 -07001259 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001260 wacom->led.hlv = 20;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001261 wacom->led.max_llv = 127;
1262 wacom->led.max_hlv = 127;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001263 wacom->led.img_lum = 10;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001264
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001265 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001266 if (error) {
1267 hid_err(wacom->hdev,
1268 "cannot create leds err: %d\n", error);
1269 return error;
1270 }
1271
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001272 error = wacom_devm_sysfs_create_group(wacom,
1273 &intuos4_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001274 break;
1275
Jason Gerecke246835f2011-12-12 00:12:04 -08001276 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -07001277 case WACOM_21UX2:
Ping Cheng09e7d942011-10-04 23:51:14 -07001278 wacom->led.llv = 0;
1279 wacom->led.hlv = 0;
1280 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001281
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001282 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001283 if (error) {
1284 hid_err(wacom->hdev,
1285 "cannot create leds err: %d\n", error);
1286 return error;
1287 }
1288
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001289 error = wacom_devm_sysfs_create_group(wacom,
1290 &cintiq_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001291 break;
1292
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001293 case INTUOS5S:
1294 case INTUOS5:
1295 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001296 case INTUOSPS:
1297 case INTUOSPM:
1298 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -07001299 wacom->led.llv = 32;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001300 wacom->led.max_llv = 96;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001301
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001302 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001303 if (error) {
1304 hid_err(wacom->hdev,
1305 "cannot create leds err: %d\n", error);
1306 return error;
1307 }
1308
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001309 error = wacom_devm_sysfs_create_group(wacom,
1310 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001311 break;
1312
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001313 case REMOTE:
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001314 wacom->led.llv = 255;
1315 wacom->led.max_llv = 255;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001316 error = wacom_led_groups_allocate(wacom, 5);
1317 if (error) {
1318 hid_err(wacom->hdev,
1319 "cannot create leds err: %d\n", error);
1320 return error;
1321 }
1322 return 0;
1323
Ping Cheng09e7d942011-10-04 23:51:14 -07001324 default:
1325 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001326 }
1327
Ping Cheng09e7d942011-10-04 23:51:14 -07001328 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -07001329 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -07001330 "cannot create sysfs group err: %d\n", error);
1331 return error;
1332 }
1333 wacom_led_control(wacom);
1334
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001335 return 0;
1336}
1337
Chris Bagwella1d552c2012-03-25 23:26:30 -07001338static enum power_supply_property wacom_battery_props[] = {
Jason Gerecke71fa6412015-03-11 10:25:41 -07001339 POWER_SUPPLY_PROP_PRESENT,
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001340 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001341 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001342 POWER_SUPPLY_PROP_CAPACITY
1343};
1344
1345static int wacom_battery_get_property(struct power_supply *psy,
1346 enum power_supply_property psp,
1347 union power_supply_propval *val)
1348{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001349 struct wacom_battery *battery = power_supply_get_drvdata(psy);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001350 int ret = 0;
1351
1352 switch (psp) {
Jason Gerecke71fa6412015-03-11 10:25:41 -07001353 case POWER_SUPPLY_PROP_PRESENT:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001354 val->intval = battery->bat_connected;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001355 break;
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001356 case POWER_SUPPLY_PROP_SCOPE:
1357 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1358 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001359 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001360 val->intval = battery->battery_capacity;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001361 break;
1362 case POWER_SUPPLY_PROP_STATUS:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001363 if (battery->bat_charging)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001364 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001365 else if (battery->battery_capacity == 100 &&
1366 battery->ps_connected)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001367 val->intval = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001368 else if (battery->ps_connected)
Jason Gereckeb0882cb2015-03-06 11:47:43 -08001369 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001370 else
1371 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001372 break;
1373 default:
1374 ret = -EINVAL;
1375 break;
1376 }
1377
1378 return ret;
1379}
1380
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001381static int __wacom_initialize_battery(struct wacom *wacom,
1382 struct wacom_battery *battery)
Chris Bagwella1d552c2012-03-25 23:26:30 -07001383{
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001384 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001385 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001386 struct power_supply_config psy_cfg = { .drv_data = battery, };
Benjamin Tissoires136ae5e2016-07-13 18:06:16 +02001387 struct power_supply *ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001388 struct power_supply_desc *bat_desc = &battery->bat_desc;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001389 unsigned long n;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001390 int error;
1391
1392 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1393 return -ENOMEM;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001394
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001395 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001396
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001397 bat_desc->properties = wacom_battery_props;
1398 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1399 bat_desc->get_property = wacom_battery_get_property;
1400 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1401 bat_desc->name = battery->bat_name;
Benjamin Tissoires96983292016-07-13 18:06:15 +02001402 bat_desc->type = POWER_SUPPLY_TYPE_USB;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001403 bat_desc->use_for_apm = 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001404
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001405 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1406 if (IS_ERR(ps_bat)) {
1407 error = PTR_ERR(ps_bat);
1408 goto err;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001409 }
1410
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001411 power_supply_powers(ps_bat, &wacom->hdev->dev);
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001412
1413 battery->battery = ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001414
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001415 devres_close_group(dev, bat_desc);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001416 return 0;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001417
1418err:
1419 devres_release_group(dev, bat_desc);
1420 return error;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001421}
1422
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001423static int wacom_initialize_battery(struct wacom *wacom)
1424{
1425 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1426 return __wacom_initialize_battery(wacom, &wacom->battery);
1427
1428 return 0;
1429}
1430
Chris Bagwella1d552c2012-03-25 23:26:30 -07001431static void wacom_destroy_battery(struct wacom *wacom)
1432{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001433 if (wacom->battery.battery) {
1434 devres_release_group(&wacom->hdev->dev,
1435 &wacom->battery.bat_desc);
1436 wacom->battery.battery = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001437 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001438}
1439
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001440static ssize_t wacom_show_speed(struct device *dev,
1441 struct device_attribute
1442 *attr, char *buf)
1443{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001444 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001445 struct wacom *wacom = hid_get_drvdata(hdev);
1446
1447 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1448}
1449
1450static ssize_t wacom_store_speed(struct device *dev,
1451 struct device_attribute *attr,
1452 const char *buf, size_t count)
1453{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001454 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001455 struct wacom *wacom = hid_get_drvdata(hdev);
1456 u8 new_speed;
1457
1458 if (kstrtou8(buf, 0, &new_speed))
1459 return -EINVAL;
1460
1461 if (new_speed != 0 && new_speed != 1)
1462 return -EINVAL;
1463
1464 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1465
1466 return count;
1467}
1468
Ping Chenge0984bc2014-09-10 12:40:05 -07001469static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001470 wacom_show_speed, wacom_store_speed);
1471
Aaron Skomra72b236d2015-08-20 16:05:17 -07001472
1473static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1474 struct kobj_attribute *kattr,
1475 char *buf, int index)
1476{
Geliang Tang2cf83832015-12-27 17:25:24 +08001477 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001478 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001479 struct wacom *wacom = hid_get_drvdata(hdev);
1480 u8 mode;
1481
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001482 mode = wacom->led.groups[index].select;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001483 if (mode >= 0 && mode < 3)
1484 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
1485 else
1486 return snprintf(buf, PAGE_SIZE, "%d\n", -1);
1487}
1488
1489#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1490static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1491 struct kobj_attribute *kattr, char *buf) \
1492{ \
1493 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1494} \
1495static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1496 .attr = {.name = "remote_mode", \
1497 .mode = DEV_ATTR_RO_PERM}, \
1498 .show = wacom_show_remote##SET_ID##_mode, \
1499}; \
1500static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1501 &remote##SET_ID##_mode_attr.attr, \
1502 NULL \
1503}; \
1504static struct attribute_group remote##SET_ID##_serial_group = { \
1505 .name = NULL, \
1506 .attrs = remote##SET_ID##_serial_attrs, \
1507}
1508
1509DEVICE_EKR_ATTR_GROUP(0);
1510DEVICE_EKR_ATTR_GROUP(1);
1511DEVICE_EKR_ATTR_GROUP(2);
1512DEVICE_EKR_ATTR_GROUP(3);
1513DEVICE_EKR_ATTR_GROUP(4);
1514
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02001515static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1516 int index)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001517{
1518 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001519 struct wacom_remote *remote = wacom->remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001520
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001521 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001522 GFP_KERNEL,
1523 "%d", serial);
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001524 if (!remote->remotes[index].group.name)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001525 return -ENOMEM;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001526
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001527 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001528 &remote->remotes[index].group);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001529 if (error) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001530 remote->remotes[index].group.name = NULL;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001531 hid_err(wacom->hdev,
1532 "cannot create sysfs group err: %d\n", error);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001533 return error;
1534 }
1535
1536 return 0;
1537}
1538
Aaron Skomra72b236d2015-08-20 16:05:17 -07001539static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1540{
1541 const size_t buf_size = 2;
1542 unsigned char *buf;
1543 int retval;
1544
1545 buf = kzalloc(buf_size, GFP_KERNEL);
1546 if (!buf)
1547 return -ENOMEM;
1548
1549 buf[0] = WAC_CMD_DELETE_PAIRING;
1550 buf[1] = selector;
1551
1552 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1553 buf_size, WAC_CMD_RETRIES);
1554 kfree(buf);
1555
1556 return retval;
1557}
1558
1559static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1560 struct kobj_attribute *attr,
1561 const char *buf, size_t count)
1562{
1563 unsigned char selector = 0;
Geliang Tang2cf83832015-12-27 17:25:24 +08001564 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001565 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001566 struct wacom *wacom = hid_get_drvdata(hdev);
1567 int err;
1568
1569 if (!strncmp(buf, "*\n", 2)) {
1570 selector = WAC_CMD_UNPAIR_ALL;
1571 } else {
1572 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1573 buf);
1574 return -1;
1575 }
1576
1577 mutex_lock(&wacom->lock);
1578
1579 err = wacom_cmd_unpair_remote(wacom, selector);
1580 mutex_unlock(&wacom->lock);
1581
1582 return err < 0 ? err : count;
1583}
1584
1585static struct kobj_attribute unpair_remote_attr = {
1586 .attr = {.name = "unpair_remote", .mode = 0200},
1587 .store = wacom_store_unpair_remote,
1588};
1589
1590static const struct attribute *remote_unpair_attrs[] = {
1591 &unpair_remote_attr.attr,
1592 NULL
1593};
1594
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001595static void wacom_remotes_destroy(void *data)
1596{
1597 struct wacom *wacom = data;
1598 struct wacom_remote *remote = wacom->remote;
1599
1600 if (!remote)
1601 return;
1602
1603 kobject_put(remote->remote_dir);
1604 kfifo_free(&remote->remote_fifo);
1605 wacom->remote = NULL;
1606}
1607
1608static int wacom_initialize_remotes(struct wacom *wacom)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001609{
1610 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001611 struct wacom_remote *remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001612 int i;
1613
1614 if (wacom->wacom_wac.features.type != REMOTE)
1615 return 0;
1616
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001617 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1618 GFP_KERNEL);
1619 if (!remote)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001620 return -ENOMEM;
1621
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001622 wacom->remote = remote;
1623
1624 spin_lock_init(&remote->remote_lock);
1625
1626 error = kfifo_alloc(&remote->remote_fifo,
1627 5 * sizeof(struct wacom_remote_data),
1628 GFP_KERNEL);
1629 if (error) {
1630 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1631 return -ENOMEM;
1632 }
1633
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001634 remote->remotes[0].group = remote0_serial_group;
1635 remote->remotes[1].group = remote1_serial_group;
1636 remote->remotes[2].group = remote2_serial_group;
1637 remote->remotes[3].group = remote3_serial_group;
1638 remote->remotes[4].group = remote4_serial_group;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001639
1640 remote->remote_dir = kobject_create_and_add("wacom_remote",
1641 &wacom->hdev->dev.kobj);
1642 if (!remote->remote_dir)
1643 return -ENOMEM;
1644
1645 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001646
1647 if (error) {
1648 hid_err(wacom->hdev,
1649 "cannot create sysfs group err: %d\n", error);
1650 return error;
1651 }
1652
1653 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001654 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001655 remote->remotes[i].serial = 0;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001656 }
1657
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001658 error = devm_add_action_or_reset(&wacom->hdev->dev,
1659 wacom_remotes_destroy, wacom);
1660 if (error)
1661 return error;
1662
Aaron Skomra72b236d2015-08-20 16:05:17 -07001663 return 0;
1664}
1665
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001666static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001667{
1668 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001669 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001670 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001671
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001672 input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001673 if (!input_dev)
1674 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001675
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001676 input_dev->name = wacom_wac->features.name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001677 input_dev->phys = hdev->phys;
1678 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001679 input_dev->open = wacom_open;
1680 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001681 input_dev->uniq = hdev->uniq;
1682 input_dev->id.bustype = hdev->bus;
1683 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001684 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001685 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001686 input_set_drvdata(input_dev, wacom);
1687
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001688 return input_dev;
1689}
1690
Jason Gerecked9f2d202015-07-13 18:03:44 -07001691static int wacom_allocate_inputs(struct wacom *wacom)
1692{
1693 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1694
1695 wacom_wac->pen_input = wacom_allocate_input(wacom);
1696 wacom_wac->touch_input = wacom_allocate_input(wacom);
1697 wacom_wac->pad_input = wacom_allocate_input(wacom);
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001698 if (!wacom_wac->pen_input ||
1699 !wacom_wac->touch_input ||
1700 !wacom_wac->pad_input)
Jason Gerecked9f2d202015-07-13 18:03:44 -07001701 return -ENOMEM;
Jason Gerecked9f2d202015-07-13 18:03:44 -07001702
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001703 wacom_wac->pen_input->name = wacom_wac->pen_name;
Jason Gerecked9f2d202015-07-13 18:03:44 -07001704 wacom_wac->touch_input->name = wacom_wac->touch_name;
1705 wacom_wac->pad_input->name = wacom_wac->pad_name;
1706
1707 return 0;
1708}
1709
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001710static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001711{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001712 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001713 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Jason Gerecke2636a3f2015-06-15 18:01:44 -07001714 int error = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001715
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001716 pen_input_dev = wacom_wac->pen_input;
1717 touch_input_dev = wacom_wac->touch_input;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001718 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001719
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001720 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001721 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001722
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001723 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1724 if (error) {
1725 /* no pen in use on this interface */
1726 input_free_device(pen_input_dev);
1727 wacom_wac->pen_input = NULL;
1728 pen_input_dev = NULL;
1729 } else {
1730 error = input_register_device(pen_input_dev);
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001731 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001732 goto fail;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001733 }
1734
1735 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1736 if (error) {
1737 /* no touch in use on this interface */
1738 input_free_device(touch_input_dev);
1739 wacom_wac->touch_input = NULL;
1740 touch_input_dev = NULL;
1741 } else {
1742 error = input_register_device(touch_input_dev);
1743 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001744 goto fail;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001745 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001746
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001747 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1748 if (error) {
1749 /* no pad in use on this interface */
1750 input_free_device(pad_input_dev);
1751 wacom_wac->pad_input = NULL;
1752 pad_input_dev = NULL;
1753 } else {
1754 error = input_register_device(pad_input_dev);
1755 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001756 goto fail;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001757 }
1758
Ping Cheng19635182012-04-29 21:09:18 -07001759 return 0;
1760
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001761fail:
1762 wacom_wac->pad_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001763 wacom_wac->touch_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001764 wacom_wac->pen_input = NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001765 return error;
1766}
1767
Jason Gerecke0be01712015-08-05 15:44:53 -07001768/*
1769 * Not all devices report physical dimensions from HID.
1770 * Compute the default from hardcoded logical dimension
1771 * and resolution before driver overwrites them.
1772 */
1773static void wacom_set_default_phy(struct wacom_features *features)
1774{
1775 if (features->x_resolution) {
1776 features->x_phy = (features->x_max * 100) /
1777 features->x_resolution;
1778 features->y_phy = (features->y_max * 100) /
1779 features->y_resolution;
1780 }
1781}
1782
1783static void wacom_calculate_res(struct wacom_features *features)
1784{
1785 /* set unit to "100th of a mm" for devices not reported by HID */
1786 if (!features->unit) {
1787 features->unit = 0x11;
1788 features->unitExpo = -3;
1789 }
1790
1791 features->x_resolution = wacom_calc_hid_res(features->x_max,
1792 features->x_phy,
1793 features->unit,
1794 features->unitExpo);
1795 features->y_resolution = wacom_calc_hid_res(features->y_max,
1796 features->y_phy,
1797 features->unit,
1798 features->unitExpo);
1799}
1800
Jason Gereckefce99572015-03-06 11:47:40 -08001801void wacom_battery_work(struct work_struct *work)
1802{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02001803 struct wacom *wacom = container_of(work, struct wacom, battery_work);
Jason Gereckefce99572015-03-06 11:47:40 -08001804
1805 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001806 !wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001807 wacom_initialize_battery(wacom);
1808 }
1809 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001810 wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001811 wacom_destroy_battery(wacom);
1812 }
1813}
1814
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001815static size_t wacom_compute_pktlen(struct hid_device *hdev)
1816{
1817 struct hid_report_enum *report_enum;
1818 struct hid_report *report;
1819 size_t size = 0;
1820
1821 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1822
1823 list_for_each_entry(report, &report_enum->report_list, list) {
Mathieu Magnaudetdabb05c62014-11-27 16:02:36 +01001824 size_t report_size = hid_report_len(report);
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001825 if (report_size > size)
1826 size = report_size;
1827 }
1828
1829 return size;
1830}
1831
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001832static void wacom_update_name(struct wacom *wacom, const char *suffix)
Ping Chengc24eab42015-04-24 15:32:51 -07001833{
1834 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1835 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke44b52502015-06-15 18:01:41 -07001836 char name[WACOM_NAME_MAX];
Ping Chengc24eab42015-04-24 15:32:51 -07001837
1838 /* Generic devices name unspecified */
1839 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1840 if (strstr(wacom->hdev->name, "Wacom") ||
1841 strstr(wacom->hdev->name, "wacom") ||
1842 strstr(wacom->hdev->name, "WACOM")) {
1843 /* name is in HID descriptor, use it */
Jason Gerecke44b52502015-06-15 18:01:41 -07001844 strlcpy(name, wacom->hdev->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001845
1846 /* strip out excess whitespaces */
1847 while (1) {
Jason Gerecke44b52502015-06-15 18:01:41 -07001848 char *gap = strstr(name, " ");
Ping Chengc24eab42015-04-24 15:32:51 -07001849 if (gap == NULL)
1850 break;
1851 /* shift everything including the terminator */
1852 memmove(gap, gap+1, strlen(gap));
1853 }
1854 /* get rid of trailing whitespace */
Jason Gerecke44b52502015-06-15 18:01:41 -07001855 if (name[strlen(name)-1] == ' ')
1856 name[strlen(name)-1] = '\0';
Ping Chengc24eab42015-04-24 15:32:51 -07001857 } else {
1858 /* no meaningful name retrieved. use product ID */
Jason Gerecke44b52502015-06-15 18:01:41 -07001859 snprintf(name, sizeof(name),
Ping Chengc24eab42015-04-24 15:32:51 -07001860 "%s %X", features->name, wacom->hdev->product);
1861 }
1862 } else {
Jason Gerecke44b52502015-06-15 18:01:41 -07001863 strlcpy(name, features->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001864 }
1865
1866 /* Append the device type to the name */
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001867 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001868 "%s%s Pen", name, suffix);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001869 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001870 "%s%s Finger", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07001871 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001872 "%s%s Pad", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07001873}
1874
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02001875static void wacom_release_resources(struct wacom *wacom)
1876{
1877 struct hid_device *hdev = wacom->hdev;
1878
1879 if (!wacom->resources)
1880 return;
1881
1882 devres_release_group(&hdev->dev, wacom);
1883
1884 wacom->resources = false;
1885
1886 wacom->wacom_wac.pen_input = NULL;
1887 wacom->wacom_wac.touch_input = NULL;
1888 wacom->wacom_wac.pad_input = NULL;
1889}
1890
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001891static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
Ping Cheng3bea7332006-07-13 18:01:36 -07001892{
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001893 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1894 struct wacom_features *features = &wacom_wac->features;
1895 struct hid_device *hdev = wacom->hdev;
Jason Childse33da8a2010-02-17 22:38:31 -08001896 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001897 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07001898
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001899 features->pktlen = wacom_compute_pktlen(hdev);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001900 if (features->pktlen > WACOM_PKGLEN_MAX)
1901 return -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001902
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02001903 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
1904 return -ENOMEM;
1905
1906 wacom->resources = true;
1907
Jason Gerecke3f14a632015-08-03 10:17:05 -07001908 error = wacom_allocate_inputs(wacom);
1909 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02001910 goto fail;
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001911
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05001912 /*
1913 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
1914 * into debug mode for the touch part.
1915 * We ignore the other interfaces.
1916 */
1917 if (features->type == BAMBOO_PAD) {
1918 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
1919 features->type = HID_GENERIC;
1920 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
1921 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
1922 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02001923 goto fail;
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05001924 }
1925 }
1926
Ping Cheng401d7d12013-07-28 00:38:54 -07001927 /* set the default size in case we do not get them from hid */
1928 wacom_set_default_phy(features);
1929
Ping Chengf393ee22012-04-29 21:09:17 -07001930 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001931 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng42f4f272015-04-15 16:53:54 -07001932 wacom_setup_device_quirks(wacom);
Jason Gerecke042628a2015-04-30 17:51:54 -07001933
Jason Gereckeaa86b182015-06-15 18:01:42 -07001934 if (features->device_type == WACOM_DEVICETYPE_NONE &&
1935 features->type != WIRELESS) {
Jason Gerecke8e116d32015-04-30 17:51:55 -07001936 error = features->type == HID_GENERIC ? -ENODEV : 0;
1937
Jason Gerecke042628a2015-04-30 17:51:54 -07001938 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
Jason Gerecke8e116d32015-04-30 17:51:55 -07001939 hdev->name,
1940 error ? "Ignoring" : "Assuming pen");
1941
1942 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02001943 goto fail;
Jason Gerecke042628a2015-04-30 17:51:54 -07001944
Jason Gereckeaa86b182015-06-15 18:01:42 -07001945 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -07001946 }
1947
Ping Cheng401d7d12013-07-28 00:38:54 -07001948 wacom_calculate_res(features);
1949
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001950 wacom_update_name(wacom, wireless ? " (WL)" : "");
Ping Cheng4492eff2010-03-19 22:18:15 -07001951
Ping Chengf3586d22015-03-20 14:57:00 -07001952 error = wacom_add_shared_data(hdev);
1953 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02001954 goto fail;
Ping Cheng49b764a2010-02-20 00:53:49 -08001955
Jason Gereckeccad85c2015-08-03 10:17:04 -07001956 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001957 (features->quirks & WACOM_QUIRK_BATTERY)) {
1958 error = wacom_initialize_battery(wacom);
1959 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02001960 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001961 }
1962
Jason Gerecke3f14a632015-08-03 10:17:05 -07001963 error = wacom_register_inputs(wacom);
1964 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02001965 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001966
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001967 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02001968 error = wacom_initialize_leds(wacom);
1969 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02001970 goto fail;
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02001971
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001972 error = wacom_initialize_remotes(wacom);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001973 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02001974 goto fail;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001975 }
1976
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001977 if (features->type == HID_GENERIC)
1978 connect_mask |= HID_CONNECT_DRIVER;
1979
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001980 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001981 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001982 if (error) {
1983 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02001984 goto fail;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001985 }
1986
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001987 if (!wireless) {
1988 /* Note that if query fails it is not a hard failure */
1989 wacom_query_tablet_data(hdev, features);
1990 }
Jason Gerecke86e88f02015-11-03 16:57:58 -08001991
1992 /* touch only Bamboo doesn't support pen */
1993 if ((features->type == BAMBOO_TOUCH) &&
1994 (features->device_type & WACOM_DEVICETYPE_PEN)) {
1995 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001996 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08001997 }
1998
1999 /* pen only Bamboo neither support touch nor pad */
2000 if ((features->type == BAMBOO_PEN) &&
2001 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2002 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2003 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002004 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08002005 }
2006
Jason Gereckeccad85c2015-08-03 10:17:04 -07002007 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002008 error = hid_hw_open(hdev);
2009
Ping Chengeda01da2015-09-23 13:51:15 -07002010 if ((wacom_wac->features.type == INTUOSHT ||
Benjamin Tissoires97f9afa2016-07-13 18:05:49 +02002011 wacom_wac->features.type == INTUOSHT2) &&
Ping Chengeda01da2015-09-23 13:51:15 -07002012 (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
Benjamin Tissoires97f9afa2016-07-13 18:05:49 +02002013 wacom_wac->shared->type = wacom_wac->features.type;
2014 wacom_wac->shared->touch_input = wacom_wac->touch_input;
Ping Cheng961794a2013-12-05 12:54:53 -08002015 }
2016
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002017 devres_close_group(&hdev->dev, wacom);
2018
Ping Cheng3bea7332006-07-13 18:01:36 -07002019 return 0;
2020
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002021fail_quirks:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002022 hid_hw_stop(hdev);
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002023fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002024 wacom_release_resources(wacom);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002025 return error;
2026}
2027
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002028static void wacom_wireless_work(struct work_struct *work)
2029{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002030 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002031 struct usb_device *usbdev = wacom->usbdev;
2032 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2033 struct hid_device *hdev1, *hdev2;
2034 struct wacom *wacom1, *wacom2;
2035 struct wacom_wac *wacom_wac1, *wacom_wac2;
2036 int error;
2037
2038 /*
2039 * Regardless if this is a disconnect or a new tablet,
2040 * remove any existing input and battery devices.
2041 */
2042
2043 wacom_destroy_battery(wacom);
2044
2045 /* Stylus interface */
2046 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2047 wacom1 = hid_get_drvdata(hdev1);
2048 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002049 wacom_release_resources(wacom1);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002050
2051 /* Touch interface */
2052 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2053 wacom2 = hid_get_drvdata(hdev2);
2054 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002055 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002056
2057 if (wacom_wac->pid == 0) {
2058 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002059 } else {
2060 const struct hid_device_id *id = wacom_ids;
2061
2062 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2063 wacom_wac->pid);
2064
2065 while (id->bus) {
2066 if (id->vendor == USB_VENDOR_ID_WACOM &&
2067 id->product == wacom_wac->pid)
2068 break;
2069 id++;
2070 }
2071
2072 if (!id->bus) {
2073 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2074 return;
2075 }
2076
2077 /* Stylus interface */
2078 wacom_wac1->features =
2079 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002080
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002081 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002082 hid_hw_stop(hdev1);
2083 error = wacom_parse_and_register(wacom1, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002084 if (error)
2085 goto fail;
2086
2087 /* Touch interface */
2088 if (wacom_wac1->features.touch_max ||
2089 (wacom_wac1->features.type >= INTUOSHT &&
2090 wacom_wac1->features.type <= BAMBOO_PT)) {
2091 wacom_wac2->features =
2092 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002093 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002094 hid_hw_stop(hdev2);
2095 error = wacom_parse_and_register(wacom2, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002096 if (error)
2097 goto fail;
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002098 }
2099
2100 error = wacom_initialize_battery(wacom);
2101 if (error)
2102 goto fail;
2103 }
2104
2105 return;
2106
2107fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002108 wacom_release_resources(wacom1);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002109 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002110 return;
2111}
2112
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002113static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2114{
2115 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002116 u32 serial = remote->remotes[index].serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002117 int i;
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002118 unsigned long flags;
2119
2120 spin_lock_irqsave(&remote->remote_lock, flags);
2121 remote->remotes[index].registered = false;
2122 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002123
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002124 if (remote->remotes[index].battery.battery)
2125 devres_release_group(&wacom->hdev->dev,
2126 &remote->remotes[index].battery.bat_desc);
2127
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002128 if (remote->remotes[index].group.name)
2129 devres_release_group(&wacom->hdev->dev,
2130 &remote->remotes[index]);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002131
2132 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002133 if (remote->remotes[i].serial == serial) {
2134 remote->remotes[i].serial = 0;
2135 remote->remotes[i].group.name = NULL;
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002136 remote->remotes[i].registered = false;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002137 remote->remotes[i].battery.battery = NULL;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002138 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2139 }
2140 }
2141}
2142
2143static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2144 unsigned int index)
2145{
2146 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002147 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002148 int error, k;
2149
2150 /* A remote can pair more than once with an EKR,
2151 * check to make sure this serial isn't already paired.
2152 */
2153 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002154 if (remote->remotes[k].serial == serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002155 break;
2156 }
2157
2158 if (k < WACOM_MAX_REMOTES) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002159 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002160 return 0;
2161 }
2162
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002163 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002164 return -ENOMEM;
2165
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002166 error = wacom_remote_create_attr_group(wacom, serial, index);
2167 if (error)
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002168 goto fail;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002169
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002170 remote->remotes[index].input = wacom_allocate_input(wacom);
2171 if (!remote->remotes[index].input) {
2172 error = -ENOMEM;
2173 goto fail;
2174 }
2175 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2176 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2177
2178 if (!remote->remotes[index].input->name) {
2179 error = -EINVAL;
2180 goto fail;
2181 }
2182
2183 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2184 &wacom->wacom_wac);
2185 if (error)
2186 goto fail;
2187
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002188 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002189
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002190 error = input_register_device(remote->remotes[index].input);
2191 if (error)
2192 goto fail;
2193
Benjamin Tissoires97f55412016-07-13 18:06:10 +02002194 error = wacom_led_groups_alloc_and_register_one(
2195 &remote->remotes[index].input->dev,
2196 wacom, index, 3, true);
2197 if (error)
2198 goto fail;
2199
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002200 remote->remotes[index].registered = true;
2201
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002202 devres_close_group(dev, &remote->remotes[index]);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002203 return 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002204
2205fail:
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002206 devres_release_group(dev, &remote->remotes[index]);
2207 remote->remotes[index].serial = 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002208 return error;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002209}
2210
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002211static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2212{
2213 struct wacom_remote *remote = wacom->remote;
2214 int error;
2215
2216 if (!remote->remotes[index].registered)
2217 return 0;
2218
2219 if (remote->remotes[index].battery.battery)
2220 return 0;
2221
2222 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2223 return 0;
2224
2225 error = __wacom_initialize_battery(wacom,
2226 &wacom->remote->remotes[index].battery);
2227 if (error)
2228 return error;
2229
2230 return 0;
2231}
2232
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002233static void wacom_remote_work(struct work_struct *work)
2234{
2235 struct wacom *wacom = container_of(work, struct wacom, remote_work);
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002236 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002237 struct wacom_remote_data data;
2238 unsigned long flags;
2239 unsigned int count;
2240 u32 serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002241 int i;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002242
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002243 spin_lock_irqsave(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002244
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002245 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002246
2247 if (count != sizeof(data)) {
2248 hid_err(wacom->hdev,
2249 "workitem triggered without status available\n");
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002250 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002251 return;
2252 }
2253
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002254 if (!kfifo_is_empty(&remote->remote_fifo))
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002255 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2256
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002257 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002258
2259 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2260 serial = data.remote[i].serial;
2261 if (data.remote[i].connected) {
2262
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002263 if (remote->remotes[i].serial == serial) {
2264 wacom_remote_attach_battery(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002265 continue;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002266 }
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002267
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002268 if (remote->remotes[i].serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002269 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002270
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002271 wacom_remote_create_one(wacom, serial, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002272
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002273 } else if (remote->remotes[i].serial) {
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002274 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002275 }
2276 }
2277}
2278
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002279static int wacom_probe(struct hid_device *hdev,
2280 const struct hid_device_id *id)
2281{
2282 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2283 struct usb_device *dev = interface_to_usbdev(intf);
2284 struct wacom *wacom;
2285 struct wacom_wac *wacom_wac;
2286 struct wacom_features *features;
2287 int error;
2288
2289 if (!id->driver_data)
2290 return -EINVAL;
2291
2292 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2293
2294 /* hid-core sets this quirk for the boot interface */
2295 hdev->quirks &= ~HID_QUIRK_NOGET;
2296
Benjamin Tissoires19b64332016-07-13 18:05:58 +02002297 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002298 if (!wacom)
2299 return -ENOMEM;
2300
2301 hid_set_drvdata(hdev, wacom);
2302 wacom->hdev = hdev;
2303
2304 wacom_wac = &wacom->wacom_wac;
2305 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2306 features = &wacom_wac->features;
2307
2308 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2309 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002310 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002311 }
2312
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002313 wacom_wac->hid_data.inputmode = -1;
Jason Gerecke326ea2a2016-04-04 11:26:52 -07002314 wacom_wac->mode_report = -1;
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002315
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002316 wacom->usbdev = dev;
2317 wacom->intf = intf;
2318 mutex_init(&wacom->lock);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002319 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2320 INIT_WORK(&wacom->battery_work, wacom_battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002321 INIT_WORK(&wacom->remote_work, wacom_remote_work);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002322
2323 /* ask for the report descriptor to be loaded by HID */
2324 error = hid_parse(hdev);
2325 if (error) {
2326 hid_err(hdev, "parse failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002327 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002328 }
2329
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002330 error = wacom_parse_and_register(wacom, false);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002331 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002332 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002333
2334 if (hdev->bus == BUS_BLUETOOTH) {
2335 error = device_create_file(&hdev->dev, &dev_attr_speed);
2336 if (error)
2337 hid_warn(hdev,
2338 "can't create sysfs speed attribute err: %d\n",
2339 error);
2340 }
2341
2342 return 0;
2343
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002344fail:
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002345 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04002346 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07002347}
2348
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002349static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07002350{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002351 struct wacom *wacom = hid_get_drvdata(hdev);
Benjamin Tissoiresf6205162016-02-12 17:27:45 +01002352 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2353 struct wacom_features *features = &wacom_wac->features;
2354
2355 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2356 hid_hw_close(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07002357
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002358 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002359
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002360 cancel_work_sync(&wacom->wireless_work);
2361 cancel_work_sync(&wacom->battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002362 cancel_work_sync(&wacom->remote_work);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002363 if (hdev->bus == BUS_BLUETOOTH)
2364 device_remove_file(&hdev->dev, &dev_attr_speed);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002365
2366 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04002367}
2368
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002369#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002370static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002371{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002372 struct wacom *wacom = hid_get_drvdata(hdev);
2373 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04002374
2375 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002376
2377 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07002378 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002379 wacom_led_control(wacom);
2380
Oliver Neukume7224092008-04-15 01:31:57 -04002381 mutex_unlock(&wacom->lock);
2382
2383 return 0;
2384}
2385
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002386static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002387{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002388 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002389}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002390#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04002391
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002392static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07002393 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08002394 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07002395 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002396 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002397 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002398#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04002399 .resume = wacom_resume,
2400 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002401#endif
2402 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07002403};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002404module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07002405
2406MODULE_VERSION(DRIVER_VERSION);
2407MODULE_AUTHOR(DRIVER_AUTHOR);
2408MODULE_DESCRIPTION(DRIVER_DESC);
2409MODULE_LICENSE(DRIVER_LICENSE);