blob: 8aeca038cc7331244eeeb5dc0468b22ef66e7d44 [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;
Jason Gerecke345857b2016-10-19 18:03:50 -0700125 int n;
Chris Bagwell41343612011-10-26 22:32:52 -0700126
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700127 switch (usage->hid) {
128 case HID_DG_CONTACTMAX:
129 /* leave touch_max as is if predefined */
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400130 if (!features->touch_max) {
131 /* read manually */
132 data = kzalloc(2, GFP_KERNEL);
133 if (!data)
134 break;
135 data[0] = field->report->id;
136 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700137 data, 2, WAC_CMD_RETRIES);
138 if (ret == 2) {
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400139 features->touch_max = data[1];
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700140 } else {
141 features->touch_max = 16;
142 hid_warn(hdev, "wacom_feature_mapping: "
143 "could not get HID_DG_CONTACTMAX, "
144 "defaulting to %d\n",
145 features->touch_max);
146 }
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400147 kfree(data);
148 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700149 break;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400150 case HID_DG_INPUTMODE:
151 /* Ignore if value index is out of bounds. */
152 if (usage->usage_index >= field->report_count) {
153 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
154 break;
155 }
156
157 hid_data->inputmode = field->report->id;
158 hid_data->inputmode_index = usage->usage_index;
159 break;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700160
161 case HID_UP_DIGITIZER:
162 if (field->report->id == 0x0B &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700163 (field->application == WACOM_HID_G9_PEN ||
164 field->application == WACOM_HID_G11_PEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700165 wacom->wacom_wac.mode_report = field->report->id;
166 wacom->wacom_wac.mode_value = 0;
167 }
168 break;
169
Jason Gereckec9c09582016-10-19 18:03:43 -0700170 case WACOM_HID_WD_DATAMODE:
171 wacom->wacom_wac.mode_report = field->report->id;
172 wacom->wacom_wac.mode_value = 2;
173 break;
174
Jason Gerecke8de82282016-10-19 18:03:37 -0700175 case WACOM_HID_UP_G9:
176 case WACOM_HID_UP_G11:
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700177 if (field->report->id == 0x03 &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700178 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
179 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700180 wacom->wacom_wac.mode_report = field->report->id;
181 wacom->wacom_wac.mode_value = 0;
182 }
183 break;
Jason Gerecke345857b2016-10-19 18:03:50 -0700184 case WACOM_HID_WD_OFFSETLEFT:
185 case WACOM_HID_WD_OFFSETTOP:
186 case WACOM_HID_WD_OFFSETRIGHT:
187 case WACOM_HID_WD_OFFSETBOTTOM:
188 /* read manually */
189 n = hid_report_len(field->report);
190 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
191 if (!data)
192 break;
193 data[0] = field->report->id;
194 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
195 data, n, WAC_CMD_RETRIES);
196 if (ret == n) {
197 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
198 data, n, 0);
199 } else {
200 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
201 __func__);
202 }
203 kfree(data);
204 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700205 }
206}
207
Chris Bagwell428f8582011-10-26 22:26:59 -0700208/*
209 * Interface Descriptor of wacom devices can be incomplete and
210 * inconsistent so wacom_features table is used to store stylus
211 * device's packet lengths, various maximum values, and tablet
212 * resolution based on product ID's.
213 *
214 * For devices that contain 2 interfaces, wacom_features table is
215 * inaccurate for the touch interface. Since the Interface Descriptor
216 * for touch interfaces has pretty complete data, this function exists
217 * to query tablet for this missing information instead of hard coding in
218 * an additional table.
219 *
220 * A typical Interface Descriptor for a stylus will contain a
221 * boot mouse application collection that is not of interest and this
222 * function will ignore it.
223 *
224 * It also contains a digitizer application collection that also is not
225 * of interest since any information it contains would be duplicate
226 * of what is in wacom_features. Usually it defines a report of an array
227 * of bytes that could be used as max length of the stylus packet returned.
228 * If it happens to define a Digitizer-Stylus Physical Collection then
229 * the X and Y logical values contain valid data but it is ignored.
230 *
231 * A typical Interface Descriptor for a touch interface will contain a
232 * Digitizer-Finger Physical Collection which will define both logical
233 * X/Y maximum as well as the physical size of tablet. Since touch
234 * interfaces haven't supported pressure or distance, this is enough
235 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700236 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700237 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
238 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700239 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700240static void wacom_usage_mapping(struct hid_device *hdev,
241 struct hid_field *field, struct hid_usage *usage)
242{
243 struct wacom *wacom = hid_get_drvdata(hdev);
244 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoiresd97a5522015-01-05 16:32:12 -0500245 bool finger = WACOM_FINGER_FIELD(field);
246 bool pen = WACOM_PEN_FIELD(field);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700247
248 /*
249 * Requiring Stylus Usage will ignore boot mouse
250 * X/Y values and some cases of invalid Digitizer X/Y
251 * values commonly reported.
252 */
Jason Gerecke042628a2015-04-30 17:51:54 -0700253 if (pen)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700254 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -0700255 else if (finger)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700256 features->device_type |= WACOM_DEVICETYPE_TOUCH;
Jason Gerecke042628a2015-04-30 17:51:54 -0700257 else
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700258 return;
259
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800260 /*
261 * Bamboo models do not support HID_DG_CONTACTMAX.
262 * And, Bamboo Pen only descriptor contains touch.
263 */
Ping Cheng3b164a02015-09-23 09:59:10 -0700264 if (features->type > BAMBOO_PT) {
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800265 /* ISDv4 touch devices at least supports one touch point */
266 if (finger && !features->touch_max)
267 features->touch_max = 1;
268 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700269
Jason Gerecke6005a132016-10-19 18:03:40 -0700270 /*
271 * ISDv4 devices which predate HID's adoption of the
272 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
273 * position instead. We can accurately detect if a
274 * usage with that value should be HID_DG_BARRELSWITCH2
275 * based on the surrounding usages, which have remained
276 * constant across generations.
277 */
278 if (features->type == HID_GENERIC &&
279 usage->hid == 0x000D0000 &&
280 field->application == HID_DG_PEN &&
281 field->physical == HID_DG_STYLUS) {
282 int i = usage->usage_index;
283
284 if (i-4 >= 0 && i+1 < field->maxusage &&
285 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
286 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
287 field->usage[i-2].hid == HID_DG_ERASER &&
288 field->usage[i-1].hid == HID_DG_INVERT &&
289 field->usage[i+1].hid == HID_DG_INRANGE) {
290 usage->hid = HID_DG_BARRELSWITCH2;
291 }
292 }
293
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700294 switch (usage->hid) {
295 case HID_GD_X:
296 features->x_max = field->logical_maximum;
297 if (finger) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700298 features->x_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700299 if ((features->type != BAMBOO_PT) &&
300 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700301 features->unit = field->unit;
302 features->unitExpo = field->unit_exponent;
303 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700304 }
305 break;
306 case HID_GD_Y:
307 features->y_max = field->logical_maximum;
308 if (finger) {
309 features->y_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700310 if ((features->type != BAMBOO_PT) &&
311 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700312 features->unit = field->unit;
313 features->unitExpo = field->unit_exponent;
314 }
315 }
316 break;
317 case HID_DG_TIPPRESSURE:
318 if (pen)
319 features->pressure_max = field->logical_maximum;
320 break;
321 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400322
323 if (features->type == HID_GENERIC)
324 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700325}
326
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800327static void wacom_post_parse_hid(struct hid_device *hdev,
328 struct wacom_features *features)
329{
330 struct wacom *wacom = hid_get_drvdata(hdev);
331 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
332
333 if (features->type == HID_GENERIC) {
334 /* Any last-minute generic device setup */
335 if (features->touch_max > 1) {
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -0700336 input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800337 INPUT_MT_DIRECT);
338 }
339 }
340}
341
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700342static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800343 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500344{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700345 struct hid_report_enum *rep_enum;
346 struct hid_report *hreport;
347 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500348
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700349 /* check features first */
350 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
351 list_for_each_entry(hreport, &rep_enum->report_list, list) {
352 for (i = 0; i < hreport->maxfield; i++) {
353 /* Ignore if report count is out of bounds. */
354 if (hreport->field[i]->report_count < 1)
355 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500356
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700357 for (j = 0; j < hreport->field[i]->maxusage; j++) {
358 wacom_feature_mapping(hdev, hreport->field[i],
359 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500360 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500361 }
362 }
363
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700364 /* now check the input usages */
365 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
366 list_for_each_entry(hreport, &rep_enum->report_list, list) {
367
368 if (!hreport->maxfield)
369 continue;
370
371 for (i = 0; i < hreport->maxfield; i++)
372 for (j = 0; j < hreport->field[i]->maxusage; j++)
373 wacom_usage_mapping(hdev, hreport->field[i],
374 hreport->field[i]->usage + j);
375 }
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800376
377 wacom_post_parse_hid(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -0500378}
379
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400380static int wacom_hid_set_device_mode(struct hid_device *hdev)
381{
382 struct wacom *wacom = hid_get_drvdata(hdev);
383 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
384 struct hid_report *r;
385 struct hid_report_enum *re;
386
387 if (hid_data->inputmode < 0)
388 return 0;
389
390 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
391 r = re->report_id_hash[hid_data->inputmode];
392 if (r) {
393 r->field[0]->value[hid_data->inputmode_index] = 2;
394 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
395 }
396 return 0;
397}
398
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700399static int wacom_set_device_mode(struct hid_device *hdev,
400 struct wacom_wac *wacom_wac)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700401{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700402 u8 *rep_data;
403 struct hid_report *r;
404 struct hid_report_enum *re;
405 int length;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700406 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700407
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700408 if (wacom_wac->mode_report < 0)
409 return 0;
410
411 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
412 r = re->report_id_hash[wacom_wac->mode_report];
413 if (!r)
414 return -EINVAL;
415
416 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700417 if (!rep_data)
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700418 return -ENOMEM;
419
420 length = hid_report_len(r);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700421
Jason Gereckefe494bc2012-10-03 17:25:35 -0700422 do {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700423 rep_data[0] = wacom_wac->mode_report;
424 rep_data[1] = wacom_wac->mode_value;
Chris Bagwell9937c022013-01-23 19:37:34 -0800425
Przemo Firszt296b7372014-08-06 14:00:38 -0700426 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
427 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700428 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700429 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700430 rep_data, length, 1);
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700431 } while (error >= 0 &&
432 rep_data[1] != wacom_wac->mode_report &&
433 limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700434
435 kfree(rep_data);
436
437 return error < 0 ? error : 0;
438}
439
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700440static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
441 struct wacom_features *features)
442{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700443 struct wacom *wacom = hid_get_drvdata(hdev);
444 int ret;
445 u8 rep_data[2];
446
447 switch (features->type) {
448 case GRAPHIRE_BT:
449 rep_data[0] = 0x03;
450 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700451 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
452 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700453
454 if (ret >= 0) {
455 rep_data[0] = speed == 0 ? 0x05 : 0x06;
456 rep_data[1] = 0x00;
457
458 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700459 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700460
461 if (ret >= 0) {
462 wacom->wacom_wac.bt_high_speed = speed;
463 return 0;
464 }
465 }
466
467 /*
468 * Note that if the raw queries fail, it's not a hard failure
469 * and it is safe to continue
470 */
471 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
472 rep_data[0], ret);
473 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700474 case INTUOS4WL:
475 if (speed == 1)
476 wacom->wacom_wac.bt_features &= ~0x20;
477 else
478 wacom->wacom_wac.bt_features |= 0x20;
479
480 rep_data[0] = 0x03;
481 rep_data[1] = wacom->wacom_wac.bt_features;
482
Przemo Firszt296b7372014-08-06 14:00:38 -0700483 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
484 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700485 if (ret >= 0)
486 wacom->wacom_wac.bt_high_speed = speed;
487 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700488 }
489
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700490 return 0;
491}
492
Jason Gereckefe494bc2012-10-03 17:25:35 -0700493/*
494 * Switch the tablet into its most-capable mode. Wacom tablets are
495 * typically configured to power-up in a mode which sends mouse-like
496 * reports to the OS. To get absolute position, pressure data, etc.
497 * from the tablet, it is necessary to switch the tablet out of this
498 * mode and into one which sends the full range of tablet data.
499 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700500static int wacom_query_tablet_data(struct hid_device *hdev,
501 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700502{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700503 struct wacom *wacom = hid_get_drvdata(hdev);
504 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
505
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700506 if (hdev->bus == BUS_BLUETOOTH)
507 return wacom_bt_query_tablet_data(hdev, 1, features);
508
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700509 if (features->type != HID_GENERIC) {
510 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
511 if (features->type > TABLETPC) {
512 /* MT Tablet PC touch */
513 wacom_wac->mode_report = 3;
514 wacom_wac->mode_value = 4;
515 } else if (features->type == WACOM_24HDT) {
516 wacom_wac->mode_report = 18;
517 wacom_wac->mode_value = 2;
518 } else if (features->type == WACOM_27QHDT) {
519 wacom_wac->mode_report = 131;
520 wacom_wac->mode_value = 2;
521 } else if (features->type == BAMBOO_PAD) {
522 wacom_wac->mode_report = 2;
523 wacom_wac->mode_value = 2;
524 }
525 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
526 if (features->type <= BAMBOO_PT) {
527 wacom_wac->mode_report = 2;
528 wacom_wac->mode_value = 2;
529 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700530 }
531 }
532
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700533 wacom_set_device_mode(hdev, wacom_wac);
534
535 if (features->type == HID_GENERIC)
536 return wacom_hid_set_device_mode(hdev);
537
Jason Gereckefe494bc2012-10-03 17:25:35 -0700538 return 0;
539}
540
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700541static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700542 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800543{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700544 struct wacom *wacom = hid_get_drvdata(hdev);
545 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800546
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700547 /* default features */
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700548 features->x_fuzz = 4;
549 features->y_fuzz = 4;
550 features->pressure_fuzz = 0;
Jason Gereckebef7e202016-04-22 14:30:53 -0700551 features->distance_fuzz = 1;
552 features->tilt_fuzz = 1;
Ping Chengec67bbe2009-12-15 00:35:24 -0800553
Chris Bagwelld3825d52012-03-25 23:26:11 -0700554 /*
555 * The wireless device HID is basic and layout conflicts with
556 * other tablets (monitor and touch interface can look like pen).
557 * Skip the query for this type and modify defaults based on
558 * interface number.
559 */
560 if (features->type == WIRELESS) {
Jason Gerecke3f14a632015-08-03 10:17:05 -0700561 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
Jason Gereckeccad85c2015-08-03 10:17:04 -0700562 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700563 else
Jason Gereckeaa86b182015-06-15 18:01:42 -0700564 features->device_type = WACOM_DEVICETYPE_NONE;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700565 return;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700566 }
567
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700568 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800569}
570
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700571struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700572 struct list_head list;
573 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700574 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700575 struct wacom_shared shared;
576};
577
578static LIST_HEAD(wacom_udev_list);
579static DEFINE_MUTEX(wacom_udev_list_lock);
580
Jason Gerecke41372d52016-08-08 12:06:30 -0700581static bool compare_device_paths(struct hid_device *hdev_a,
582 struct hid_device *hdev_b, char separator)
583{
584 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
585 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
586
587 if (n1 != n2 || n1 <= 0 || n2 <= 0)
588 return false;
589
590 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
591}
592
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700593static bool wacom_are_sibling(struct hid_device *hdev,
594 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700595{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700596 struct wacom *wacom = hid_get_drvdata(hdev);
597 struct wacom_features *features = &wacom->wacom_wac.features;
Jason Gerecke41372d52016-08-08 12:06:30 -0700598 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
599 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
600 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
601 __u32 oPid = features->oPid ? features->oPid : hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700602
Jason Gerecke41372d52016-08-08 12:06:30 -0700603 /* The defined oVid/oPid must match that of the sibling */
604 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
605 return false;
606 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
607 return false;
608
609 /*
610 * Devices with the same VID/PID must share the same physical
611 * device path, while those with different VID/PID must share
612 * the same physical parent device path.
613 */
614 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
615 if (!compare_device_paths(hdev, sibling, '/'))
616 return false;
617 } else {
618 if (!compare_device_paths(hdev, sibling, '.'))
619 return false;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700620 }
621
Jason Gerecke41372d52016-08-08 12:06:30 -0700622 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
623 if (features->type != HID_GENERIC)
624 return true;
625
626 /*
627 * Direct-input devices may not be siblings of indirect-input
628 * devices.
629 */
630 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
631 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700632 return false;
633
Jason Gerecke41372d52016-08-08 12:06:30 -0700634 /*
635 * Indirect-input devices may not be siblings of direct-input
636 * devices.
637 */
638 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
639 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700640 return false;
641
Jason Gerecke41372d52016-08-08 12:06:30 -0700642 /* Pen devices may only be siblings of touch devices */
643 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
644 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
645 return false;
646
647 /* Touch devices may only be siblings of pen devices */
648 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
649 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
650 return false;
651
652 /*
653 * No reason could be found for these two devices to NOT be
654 * siblings, so there's a good chance they ARE siblings
655 */
656 return true;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700657}
658
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700659static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700660{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700661 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700662
Jason Gerecke41372d52016-08-08 12:06:30 -0700663 /* Try to find an already-probed interface from the same device */
664 list_for_each_entry(data, &wacom_udev_list, list) {
665 if (compare_device_paths(hdev, data->dev, '/'))
666 return data;
667 }
668
669 /* Fallback to finding devices that appear to be "siblings" */
Ping Cheng4492eff2010-03-19 22:18:15 -0700670 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700671 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700672 kref_get(&data->kref);
673 return data;
674 }
675 }
676
677 return NULL;
678}
679
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200680static void wacom_release_shared_data(struct kref *kref)
681{
682 struct wacom_hdev_data *data =
683 container_of(kref, struct wacom_hdev_data, kref);
684
685 mutex_lock(&wacom_udev_list_lock);
686 list_del(&data->list);
687 mutex_unlock(&wacom_udev_list_lock);
688
689 kfree(data);
690}
691
692static void wacom_remove_shared_data(void *res)
693{
694 struct wacom *wacom = res;
695 struct wacom_hdev_data *data;
696 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
697
698 if (wacom_wac->shared) {
699 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
700 shared);
701
702 if (wacom_wac->shared->touch == wacom->hdev)
703 wacom_wac->shared->touch = NULL;
704 else if (wacom_wac->shared->pen == wacom->hdev)
705 wacom_wac->shared->pen = NULL;
706
707 kref_put(&data->kref, wacom_release_shared_data);
708 wacom_wac->shared = NULL;
709 }
710}
711
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700712static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700713{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700714 struct wacom *wacom = hid_get_drvdata(hdev);
715 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
716 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700717 int retval = 0;
718
719 mutex_lock(&wacom_udev_list_lock);
720
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700721 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700722 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700723 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700724 if (!data) {
725 retval = -ENOMEM;
726 goto out;
727 }
728
729 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700730 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700731 list_add_tail(&data->list, &wacom_udev_list);
732 }
733
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700734 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700735
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200736 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
737 if (retval) {
738 mutex_unlock(&wacom_udev_list_lock);
739 wacom_remove_shared_data(wacom);
740 return retval;
741 }
742
Jason Gereckea9ce7852017-01-17 15:38:58 -0800743 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
744 wacom_wac->shared->touch = hdev;
745 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
746 wacom_wac->shared->pen = hdev;
747
Ping Cheng4492eff2010-03-19 22:18:15 -0700748out:
749 mutex_unlock(&wacom_udev_list_lock);
750 return retval;
751}
752
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700753static int wacom_led_control(struct wacom *wacom)
754{
755 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700756 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700757 unsigned char report_id = WAC_CMD_LED_CONTROL;
758 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700759
Benjamin Tissoires97f55412016-07-13 18:06:10 +0200760 if (!hid_get_drvdata(wacom->hdev))
761 return -ENODEV;
762
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200763 if (!wacom->led.groups)
764 return -ENOTSUPP;
765
Ping Cheng912ca212014-09-10 12:41:31 -0700766 if (wacom->wacom_wac.pid) { /* wireless connected */
767 report_id = WAC_CMD_WL_LED_CONTROL;
768 buf_size = 13;
769 }
770 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700771 if (!buf)
772 return -ENOMEM;
773
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700774 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700775 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700776 /*
777 * Touch Ring and crop mark LED luminance may take on
778 * one of four values:
779 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
780 */
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200781 int ring_led = wacom->led.groups[0].select & 0x03;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700782 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
783 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700784 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700785
Ping Cheng912ca212014-09-10 12:41:31 -0700786 buf[0] = report_id;
787 if (wacom->wacom_wac.pid) {
788 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
789 buf, buf_size, WAC_CMD_RETRIES);
790 buf[0] = report_id;
791 buf[4] = led_bits;
792 } else
793 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700794 }
795 else {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200796 int led = wacom->led.groups[0].select | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700797
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700798 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
799 wacom->wacom_wac.features.type == WACOM_24HD)
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200800 led |= (wacom->led.groups[1].select << 4) | 0x40;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700801
Ping Cheng912ca212014-09-10 12:41:31 -0700802 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700803 buf[1] = led;
804 buf[2] = wacom->led.llv;
805 buf[3] = wacom->led.hlv;
806 buf[4] = wacom->led.img_lum;
807 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700808
Ping Cheng912ca212014-09-10 12:41:31 -0700809 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700810 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700811 kfree(buf);
812
813 return retval;
814}
815
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700816static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
817 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700818{
819 unsigned char *buf;
820 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700821 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700822
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700823 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700824 if (!buf)
825 return -ENOMEM;
826
827 /* Send 'start' command */
828 buf[0] = WAC_CMD_ICON_START;
829 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700830 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
831 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700832 if (retval < 0)
833 goto out;
834
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700835 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700836 buf[1] = button_id & 0x07;
837 for (i = 0; i < 4; i++) {
838 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700839 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700840
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700841 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700842 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700843 if (retval < 0)
844 break;
845 }
846
847 /* Send 'stop' */
848 buf[0] = WAC_CMD_ICON_START;
849 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700850 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
851 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700852
853out:
854 kfree(buf);
855 return retval;
856}
857
Ping Cheng09e7d942011-10-04 23:51:14 -0700858static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700859 const char *buf, size_t count)
860{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800861 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700862 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700863 unsigned int id;
864 int err;
865
866 err = kstrtouint(buf, 10, &id);
867 if (err)
868 return err;
869
870 mutex_lock(&wacom->lock);
871
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200872 wacom->led.groups[set_id].select = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700873 err = wacom_led_control(wacom);
874
875 mutex_unlock(&wacom->lock);
876
877 return err < 0 ? err : count;
878}
879
Ping Cheng09e7d942011-10-04 23:51:14 -0700880#define DEVICE_LED_SELECT_ATTR(SET_ID) \
881static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
882 struct device_attribute *attr, const char *buf, size_t count) \
883{ \
884 return wacom_led_select_store(dev, SET_ID, buf, count); \
885} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700886static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
887 struct device_attribute *attr, char *buf) \
888{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800889 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700890 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700891 return scnprintf(buf, PAGE_SIZE, "%d\n", \
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200892 wacom->led.groups[SET_ID].select); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700893} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700894static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700895 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700896 wacom_led##SET_ID##_select_store)
897
898DEVICE_LED_SELECT_ATTR(0);
899DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700900
901static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
902 const char *buf, size_t count)
903{
904 unsigned int value;
905 int err;
906
907 err = kstrtouint(buf, 10, &value);
908 if (err)
909 return err;
910
911 mutex_lock(&wacom->lock);
912
913 *dest = value & 0x7f;
914 err = wacom_led_control(wacom);
915
916 mutex_unlock(&wacom->lock);
917
918 return err < 0 ? err : count;
919}
920
921#define DEVICE_LUMINANCE_ATTR(name, field) \
922static ssize_t wacom_##name##_luminance_store(struct device *dev, \
923 struct device_attribute *attr, const char *buf, size_t count) \
924{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800925 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700926 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700927 \
928 return wacom_luminance_store(wacom, &wacom->led.field, \
929 buf, count); \
930} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700931static ssize_t wacom_##name##_luminance_show(struct device *dev, \
932 struct device_attribute *attr, char *buf) \
933{ \
934 struct wacom *wacom = dev_get_drvdata(dev); \
935 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
936} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700937static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700938 wacom_##name##_luminance_show, \
939 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700940
941DEVICE_LUMINANCE_ATTR(status0, llv);
942DEVICE_LUMINANCE_ATTR(status1, hlv);
943DEVICE_LUMINANCE_ATTR(buttons, img_lum);
944
945static ssize_t wacom_button_image_store(struct device *dev, int button_id,
946 const char *buf, size_t count)
947{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800948 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700949 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700950 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700951 unsigned len;
952 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700953
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700954 if (hdev->bus == BUS_BLUETOOTH) {
955 len = 256;
956 xfer_id = WAC_CMD_ICON_BT_XFER;
957 } else {
958 len = 1024;
959 xfer_id = WAC_CMD_ICON_XFER;
960 }
961
962 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700963 return -EINVAL;
964
965 mutex_lock(&wacom->lock);
966
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700967 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700968
969 mutex_unlock(&wacom->lock);
970
971 return err < 0 ? err : count;
972}
973
974#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
975static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
976 struct device_attribute *attr, const char *buf, size_t count) \
977{ \
978 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
979} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700980static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700981 NULL, wacom_btnimg##BUTTON_ID##_store)
982
983DEVICE_BTNIMG_ATTR(0);
984DEVICE_BTNIMG_ATTR(1);
985DEVICE_BTNIMG_ATTR(2);
986DEVICE_BTNIMG_ATTR(3);
987DEVICE_BTNIMG_ATTR(4);
988DEVICE_BTNIMG_ATTR(5);
989DEVICE_BTNIMG_ATTR(6);
990DEVICE_BTNIMG_ATTR(7);
991
Ping Cheng09e7d942011-10-04 23:51:14 -0700992static struct attribute *cintiq_led_attrs[] = {
993 &dev_attr_status_led0_select.attr,
994 &dev_attr_status_led1_select.attr,
995 NULL
996};
997
998static struct attribute_group cintiq_led_attr_group = {
999 .name = "wacom_led",
1000 .attrs = cintiq_led_attrs,
1001};
1002
1003static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001004 &dev_attr_status0_luminance.attr,
1005 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -07001006 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001007 &dev_attr_buttons_luminance.attr,
1008 &dev_attr_button0_rawimg.attr,
1009 &dev_attr_button1_rawimg.attr,
1010 &dev_attr_button2_rawimg.attr,
1011 &dev_attr_button3_rawimg.attr,
1012 &dev_attr_button4_rawimg.attr,
1013 &dev_attr_button5_rawimg.attr,
1014 &dev_attr_button6_rawimg.attr,
1015 &dev_attr_button7_rawimg.attr,
1016 NULL
1017};
1018
Ping Cheng09e7d942011-10-04 23:51:14 -07001019static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001020 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -07001021 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001022};
1023
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001024static struct attribute *intuos5_led_attrs[] = {
1025 &dev_attr_status0_luminance.attr,
1026 &dev_attr_status_led0_select.attr,
1027 NULL
1028};
1029
1030static struct attribute_group intuos5_led_attr_group = {
1031 .name = "wacom_led",
1032 .attrs = intuos5_led_attrs,
1033};
1034
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001035struct wacom_sysfs_group_devres {
1036 struct attribute_group *group;
1037 struct kobject *root;
1038};
1039
1040static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1041{
1042 struct wacom_sysfs_group_devres *devres = res;
1043 struct kobject *kobj = devres->root;
1044
1045 dev_dbg(dev, "%s: dropping reference to %s\n",
1046 __func__, devres->group->name);
1047 sysfs_remove_group(kobj, devres->group);
1048}
1049
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001050static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1051 struct kobject *root,
1052 struct attribute_group *group)
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001053{
1054 struct wacom_sysfs_group_devres *devres;
1055 int error;
1056
1057 devres = devres_alloc(wacom_devm_sysfs_group_release,
1058 sizeof(struct wacom_sysfs_group_devres),
1059 GFP_KERNEL);
1060 if (!devres)
1061 return -ENOMEM;
1062
1063 devres->group = group;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001064 devres->root = root;
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001065
1066 error = sysfs_create_group(devres->root, group);
1067 if (error)
1068 return error;
1069
1070 devres_add(&wacom->hdev->dev, devres);
1071
1072 return 0;
1073}
1074
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001075static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1076 struct attribute_group *group)
1077{
1078 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1079 group);
1080}
1081
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001082enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001083{
1084 struct wacom *wacom = led->wacom;
1085
1086 if (wacom->led.max_hlv)
1087 return led->hlv * LED_FULL / wacom->led.max_hlv;
1088
1089 if (wacom->led.max_llv)
1090 return led->llv * LED_FULL / wacom->led.max_llv;
1091
1092 /* device doesn't support brightness tuning */
1093 return LED_FULL;
1094}
1095
1096static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1097{
1098 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1099 struct wacom *wacom = led->wacom;
1100
1101 if (wacom->led.groups[led->group].select != led->id)
1102 return LED_OFF;
1103
1104 return wacom_leds_brightness_get(led);
1105}
1106
1107static int wacom_led_brightness_set(struct led_classdev *cdev,
1108 enum led_brightness brightness)
1109{
1110 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1111 struct wacom *wacom = led->wacom;
1112 int error;
1113
1114 mutex_lock(&wacom->lock);
1115
1116 if (!wacom->led.groups || (brightness == LED_OFF &&
1117 wacom->led.groups[led->group].select != led->id)) {
1118 error = 0;
1119 goto out;
1120 }
1121
1122 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1123 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1124
1125 wacom->led.groups[led->group].select = led->id;
1126
1127 error = wacom_led_control(wacom);
1128
1129out:
1130 mutex_unlock(&wacom->lock);
1131
1132 return error;
1133}
1134
1135static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1136 enum led_brightness brightness)
1137{
1138}
1139
1140static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1141 struct wacom_led *led, unsigned int group,
1142 unsigned int id, bool read_only)
1143{
1144 int error;
1145 char *name;
1146
1147 name = devm_kasprintf(dev, GFP_KERNEL,
1148 "%s::wacom-%d.%d",
1149 dev_name(dev),
1150 group,
1151 id);
1152 if (!name)
1153 return -ENOMEM;
1154
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001155 if (!read_only) {
1156 led->trigger.name = name;
1157 error = devm_led_trigger_register(dev, &led->trigger);
1158 if (error) {
1159 hid_err(wacom->hdev,
1160 "failed to register LED trigger %s: %d\n",
1161 led->cdev.name, error);
1162 return error;
1163 }
1164 }
1165
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001166 led->group = group;
1167 led->id = id;
1168 led->wacom = wacom;
1169 led->llv = wacom->led.llv;
1170 led->hlv = wacom->led.hlv;
1171 led->cdev.name = name;
1172 led->cdev.max_brightness = LED_FULL;
1173 led->cdev.flags = LED_HW_PLUGGABLE;
1174 led->cdev.brightness_get = __wacom_led_brightness_get;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001175 if (!read_only) {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001176 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001177 led->cdev.default_trigger = led->cdev.name;
1178 } else {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001179 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001180 }
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001181
1182 error = devm_led_classdev_register(dev, &led->cdev);
1183 if (error) {
1184 hid_err(wacom->hdev,
1185 "failed to register LED %s: %d\n",
1186 led->cdev.name, error);
1187 led->cdev.name = NULL;
1188 return error;
1189 }
1190
1191 return 0;
1192}
1193
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001194static void wacom_led_groups_release_one(void *data)
1195{
1196 struct wacom_group_leds *group = data;
1197
1198 devres_release_group(group->dev, group);
1199}
1200
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001201static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1202 struct wacom *wacom,
1203 int group_id, int count,
1204 bool read_only)
1205{
1206 struct wacom_led *leds;
1207 int i, error;
1208
1209 if (group_id >= wacom->led.count || count <= 0)
1210 return -EINVAL;
1211
1212 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1213 return -ENOMEM;
1214
1215 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1216 if (!leds) {
1217 error = -ENOMEM;
1218 goto err;
1219 }
1220
1221 wacom->led.groups[group_id].leds = leds;
1222 wacom->led.groups[group_id].count = count;
1223
1224 for (i = 0; i < count; i++) {
1225 error = wacom_led_register_one(dev, wacom, &leds[i],
1226 group_id, i, read_only);
1227 if (error)
1228 goto err;
1229 }
1230
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001231 wacom->led.groups[group_id].dev = dev;
1232
1233 devres_close_group(dev, &wacom->led.groups[group_id]);
1234
1235 /*
1236 * There is a bug (?) in devm_led_classdev_register() in which its
1237 * increments the refcount of the parent. If the parent is an input
1238 * device, that means the ref count never reaches 0 when
1239 * devm_input_device_release() gets called.
1240 * This means that the LEDs are still there after disconnect.
1241 * Manually force the release of the group so that the leds are released
1242 * once we are done using them.
1243 */
1244 error = devm_add_action_or_reset(&wacom->hdev->dev,
1245 wacom_led_groups_release_one,
1246 &wacom->led.groups[group_id]);
1247 if (error)
1248 return error;
1249
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001250 return 0;
1251
1252err:
1253 devres_release_group(dev, &wacom->led.groups[group_id]);
1254 return error;
1255}
1256
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001257struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1258 unsigned int id)
1259{
1260 struct wacom_group_leds *group;
1261
1262 if (group_id >= wacom->led.count)
1263 return NULL;
1264
1265 group = &wacom->led.groups[group_id];
1266
1267 if (!group->leds)
1268 return NULL;
1269
1270 id %= group->count;
1271
1272 return &group->leds[id];
1273}
1274
1275/**
1276 * wacom_led_next: gives the next available led with a wacom trigger.
1277 *
1278 * returns the next available struct wacom_led which has its default trigger
1279 * or the current one if none is available.
1280 */
1281struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1282{
1283 struct wacom_led *next_led;
1284 int group, next;
1285
1286 if (!wacom || !cur)
1287 return NULL;
1288
1289 group = cur->group;
1290 next = cur->id;
1291
1292 do {
1293 next_led = wacom_led_find(wacom, group, ++next);
1294 if (!next_led || next_led == cur)
1295 return next_led;
1296 } while (next_led->cdev.trigger != &next_led->trigger);
1297
1298 return next_led;
1299}
1300
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001301static void wacom_led_groups_release(void *data)
1302{
1303 struct wacom *wacom = data;
1304
1305 wacom->led.groups = NULL;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001306 wacom->led.count = 0;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001307}
1308
1309static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1310{
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001311 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001312 struct wacom_group_leds *groups;
1313 int error;
1314
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001315 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001316 GFP_KERNEL);
1317 if (!groups)
1318 return -ENOMEM;
1319
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001320 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001321 if (error)
1322 return error;
1323
1324 wacom->led.groups = groups;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001325 wacom->led.count = count;
1326
1327 return 0;
1328}
1329
1330static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1331 int led_per_group, bool read_only)
1332{
1333 struct device *dev;
1334 int i, error;
1335
1336 if (!wacom->wacom_wac.pad_input)
1337 return -EINVAL;
1338
1339 dev = &wacom->wacom_wac.pad_input->dev;
1340
1341 error = wacom_led_groups_allocate(wacom, group_count);
1342 if (error)
1343 return error;
1344
1345 for (i = 0; i < group_count; i++) {
1346 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1347 led_per_group,
1348 read_only);
1349 if (error)
1350 return error;
1351 }
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001352
1353 return 0;
1354}
1355
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001356static int wacom_initialize_leds(struct wacom *wacom)
1357{
1358 int error;
1359
Jason Gerecke862cf552015-06-15 18:01:43 -07001360 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1361 return 0;
1362
Ping Cheng09e7d942011-10-04 23:51:14 -07001363 /* Initialize default values */
1364 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -07001365 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -07001366 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -07001367 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -07001368 case INTUOS4L:
Ping Chengf4fa9a62011-10-04 23:49:42 -07001369 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001370 wacom->led.hlv = 20;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001371 wacom->led.max_llv = 127;
1372 wacom->led.max_hlv = 127;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001373 wacom->led.img_lum = 10;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001374
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001375 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001376 if (error) {
1377 hid_err(wacom->hdev,
1378 "cannot create leds err: %d\n", error);
1379 return error;
1380 }
1381
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001382 error = wacom_devm_sysfs_create_group(wacom,
1383 &intuos4_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001384 break;
1385
Jason Gerecke246835f2011-12-12 00:12:04 -08001386 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -07001387 case WACOM_21UX2:
Ping Cheng09e7d942011-10-04 23:51:14 -07001388 wacom->led.llv = 0;
1389 wacom->led.hlv = 0;
1390 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001391
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001392 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001393 if (error) {
1394 hid_err(wacom->hdev,
1395 "cannot create leds err: %d\n", error);
1396 return error;
1397 }
1398
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001399 error = wacom_devm_sysfs_create_group(wacom,
1400 &cintiq_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001401 break;
1402
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001403 case INTUOS5S:
1404 case INTUOS5:
1405 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001406 case INTUOSPS:
1407 case INTUOSPM:
1408 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -07001409 wacom->led.llv = 32;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001410 wacom->led.max_llv = 96;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001411
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001412 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001413 if (error) {
1414 hid_err(wacom->hdev,
1415 "cannot create leds err: %d\n", error);
1416 return error;
1417 }
1418
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001419 error = wacom_devm_sysfs_create_group(wacom,
1420 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001421 break;
1422
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001423 case REMOTE:
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001424 wacom->led.llv = 255;
1425 wacom->led.max_llv = 255;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001426 error = wacom_led_groups_allocate(wacom, 5);
1427 if (error) {
1428 hid_err(wacom->hdev,
1429 "cannot create leds err: %d\n", error);
1430 return error;
1431 }
1432 return 0;
1433
Ping Cheng09e7d942011-10-04 23:51:14 -07001434 default:
1435 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001436 }
1437
Ping Cheng09e7d942011-10-04 23:51:14 -07001438 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -07001439 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -07001440 "cannot create sysfs group err: %d\n", error);
1441 return error;
1442 }
1443 wacom_led_control(wacom);
1444
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001445 return 0;
1446}
1447
Chris Bagwella1d552c2012-03-25 23:26:30 -07001448static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001449 POWER_SUPPLY_PROP_MODEL_NAME,
Jason Gerecke71fa6412015-03-11 10:25:41 -07001450 POWER_SUPPLY_PROP_PRESENT,
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001451 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001452 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001453 POWER_SUPPLY_PROP_CAPACITY
1454};
1455
1456static int wacom_battery_get_property(struct power_supply *psy,
1457 enum power_supply_property psp,
1458 union power_supply_propval *val)
1459{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001460 struct wacom_battery *battery = power_supply_get_drvdata(psy);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001461 int ret = 0;
1462
1463 switch (psp) {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001464 case POWER_SUPPLY_PROP_MODEL_NAME:
1465 val->strval = battery->wacom->wacom_wac.name;
1466 break;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001467 case POWER_SUPPLY_PROP_PRESENT:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001468 val->intval = battery->bat_connected;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001469 break;
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001470 case POWER_SUPPLY_PROP_SCOPE:
1471 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1472 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001473 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001474 val->intval = battery->battery_capacity;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001475 break;
1476 case POWER_SUPPLY_PROP_STATUS:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001477 if (battery->bat_charging)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001478 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001479 else if (battery->battery_capacity == 100 &&
1480 battery->ps_connected)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001481 val->intval = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001482 else if (battery->ps_connected)
Jason Gereckeb0882cb2015-03-06 11:47:43 -08001483 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001484 else
1485 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001486 break;
1487 default:
1488 ret = -EINVAL;
1489 break;
1490 }
1491
1492 return ret;
1493}
1494
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001495static int __wacom_initialize_battery(struct wacom *wacom,
1496 struct wacom_battery *battery)
Chris Bagwella1d552c2012-03-25 23:26:30 -07001497{
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001498 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001499 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001500 struct power_supply_config psy_cfg = { .drv_data = battery, };
Benjamin Tissoires136ae5e2016-07-13 18:06:16 +02001501 struct power_supply *ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001502 struct power_supply_desc *bat_desc = &battery->bat_desc;
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001503 unsigned long n;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001504 int error;
1505
1506 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1507 return -ENOMEM;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001508
Benjamin Tissoires99569532016-07-13 18:06:17 +02001509 battery->wacom = wacom;
1510
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001511 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001512
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001513 bat_desc->properties = wacom_battery_props;
1514 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1515 bat_desc->get_property = wacom_battery_get_property;
1516 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1517 bat_desc->name = battery->bat_name;
Benjamin Tissoires96983292016-07-13 18:06:15 +02001518 bat_desc->type = POWER_SUPPLY_TYPE_USB;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001519 bat_desc->use_for_apm = 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001520
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001521 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1522 if (IS_ERR(ps_bat)) {
1523 error = PTR_ERR(ps_bat);
1524 goto err;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001525 }
1526
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001527 power_supply_powers(ps_bat, &wacom->hdev->dev);
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001528
1529 battery->battery = ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001530
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001531 devres_close_group(dev, bat_desc);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001532 return 0;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001533
1534err:
1535 devres_release_group(dev, bat_desc);
1536 return error;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001537}
1538
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001539static int wacom_initialize_battery(struct wacom *wacom)
1540{
1541 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1542 return __wacom_initialize_battery(wacom, &wacom->battery);
1543
1544 return 0;
1545}
1546
Chris Bagwella1d552c2012-03-25 23:26:30 -07001547static void wacom_destroy_battery(struct wacom *wacom)
1548{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001549 if (wacom->battery.battery) {
1550 devres_release_group(&wacom->hdev->dev,
1551 &wacom->battery.bat_desc);
1552 wacom->battery.battery = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001553 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001554}
1555
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001556static ssize_t wacom_show_speed(struct device *dev,
1557 struct device_attribute
1558 *attr, char *buf)
1559{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001560 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001561 struct wacom *wacom = hid_get_drvdata(hdev);
1562
1563 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1564}
1565
1566static ssize_t wacom_store_speed(struct device *dev,
1567 struct device_attribute *attr,
1568 const char *buf, size_t count)
1569{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001570 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001571 struct wacom *wacom = hid_get_drvdata(hdev);
1572 u8 new_speed;
1573
1574 if (kstrtou8(buf, 0, &new_speed))
1575 return -EINVAL;
1576
1577 if (new_speed != 0 && new_speed != 1)
1578 return -EINVAL;
1579
1580 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1581
1582 return count;
1583}
1584
Ping Chenge0984bc2014-09-10 12:40:05 -07001585static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001586 wacom_show_speed, wacom_store_speed);
1587
Aaron Skomra72b236d2015-08-20 16:05:17 -07001588
1589static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1590 struct kobj_attribute *kattr,
1591 char *buf, int index)
1592{
Geliang Tang2cf83832015-12-27 17:25:24 +08001593 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001594 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001595 struct wacom *wacom = hid_get_drvdata(hdev);
1596 u8 mode;
1597
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001598 mode = wacom->led.groups[index].select;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001599 if (mode >= 0 && mode < 3)
1600 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
1601 else
1602 return snprintf(buf, PAGE_SIZE, "%d\n", -1);
1603}
1604
1605#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1606static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1607 struct kobj_attribute *kattr, char *buf) \
1608{ \
1609 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1610} \
1611static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1612 .attr = {.name = "remote_mode", \
1613 .mode = DEV_ATTR_RO_PERM}, \
1614 .show = wacom_show_remote##SET_ID##_mode, \
1615}; \
1616static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1617 &remote##SET_ID##_mode_attr.attr, \
1618 NULL \
1619}; \
1620static struct attribute_group remote##SET_ID##_serial_group = { \
1621 .name = NULL, \
1622 .attrs = remote##SET_ID##_serial_attrs, \
1623}
1624
1625DEVICE_EKR_ATTR_GROUP(0);
1626DEVICE_EKR_ATTR_GROUP(1);
1627DEVICE_EKR_ATTR_GROUP(2);
1628DEVICE_EKR_ATTR_GROUP(3);
1629DEVICE_EKR_ATTR_GROUP(4);
1630
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02001631static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1632 int index)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001633{
1634 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001635 struct wacom_remote *remote = wacom->remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001636
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001637 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001638 GFP_KERNEL,
1639 "%d", serial);
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001640 if (!remote->remotes[index].group.name)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001641 return -ENOMEM;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001642
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001643 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001644 &remote->remotes[index].group);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001645 if (error) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001646 remote->remotes[index].group.name = NULL;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001647 hid_err(wacom->hdev,
1648 "cannot create sysfs group err: %d\n", error);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001649 return error;
1650 }
1651
1652 return 0;
1653}
1654
Aaron Skomra72b236d2015-08-20 16:05:17 -07001655static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1656{
1657 const size_t buf_size = 2;
1658 unsigned char *buf;
1659 int retval;
1660
1661 buf = kzalloc(buf_size, GFP_KERNEL);
1662 if (!buf)
1663 return -ENOMEM;
1664
1665 buf[0] = WAC_CMD_DELETE_PAIRING;
1666 buf[1] = selector;
1667
1668 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1669 buf_size, WAC_CMD_RETRIES);
1670 kfree(buf);
1671
1672 return retval;
1673}
1674
1675static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1676 struct kobj_attribute *attr,
1677 const char *buf, size_t count)
1678{
1679 unsigned char selector = 0;
Geliang Tang2cf83832015-12-27 17:25:24 +08001680 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001681 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001682 struct wacom *wacom = hid_get_drvdata(hdev);
1683 int err;
1684
1685 if (!strncmp(buf, "*\n", 2)) {
1686 selector = WAC_CMD_UNPAIR_ALL;
1687 } else {
1688 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1689 buf);
1690 return -1;
1691 }
1692
1693 mutex_lock(&wacom->lock);
1694
1695 err = wacom_cmd_unpair_remote(wacom, selector);
1696 mutex_unlock(&wacom->lock);
1697
1698 return err < 0 ? err : count;
1699}
1700
1701static struct kobj_attribute unpair_remote_attr = {
1702 .attr = {.name = "unpair_remote", .mode = 0200},
1703 .store = wacom_store_unpair_remote,
1704};
1705
1706static const struct attribute *remote_unpair_attrs[] = {
1707 &unpair_remote_attr.attr,
1708 NULL
1709};
1710
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001711static void wacom_remotes_destroy(void *data)
1712{
1713 struct wacom *wacom = data;
1714 struct wacom_remote *remote = wacom->remote;
1715
1716 if (!remote)
1717 return;
1718
1719 kobject_put(remote->remote_dir);
1720 kfifo_free(&remote->remote_fifo);
1721 wacom->remote = NULL;
1722}
1723
1724static int wacom_initialize_remotes(struct wacom *wacom)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001725{
1726 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001727 struct wacom_remote *remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001728 int i;
1729
1730 if (wacom->wacom_wac.features.type != REMOTE)
1731 return 0;
1732
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001733 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1734 GFP_KERNEL);
1735 if (!remote)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001736 return -ENOMEM;
1737
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001738 wacom->remote = remote;
1739
1740 spin_lock_init(&remote->remote_lock);
1741
1742 error = kfifo_alloc(&remote->remote_fifo,
1743 5 * sizeof(struct wacom_remote_data),
1744 GFP_KERNEL);
1745 if (error) {
1746 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1747 return -ENOMEM;
1748 }
1749
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001750 remote->remotes[0].group = remote0_serial_group;
1751 remote->remotes[1].group = remote1_serial_group;
1752 remote->remotes[2].group = remote2_serial_group;
1753 remote->remotes[3].group = remote3_serial_group;
1754 remote->remotes[4].group = remote4_serial_group;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001755
1756 remote->remote_dir = kobject_create_and_add("wacom_remote",
1757 &wacom->hdev->dev.kobj);
1758 if (!remote->remote_dir)
1759 return -ENOMEM;
1760
1761 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001762
1763 if (error) {
1764 hid_err(wacom->hdev,
1765 "cannot create sysfs group err: %d\n", error);
1766 return error;
1767 }
1768
1769 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001770 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001771 remote->remotes[i].serial = 0;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001772 }
1773
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001774 error = devm_add_action_or_reset(&wacom->hdev->dev,
1775 wacom_remotes_destroy, wacom);
1776 if (error)
1777 return error;
1778
Aaron Skomra72b236d2015-08-20 16:05:17 -07001779 return 0;
1780}
1781
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001782static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001783{
1784 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001785 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001786 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001787
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001788 input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001789 if (!input_dev)
1790 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001791
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001792 input_dev->name = wacom_wac->features.name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001793 input_dev->phys = hdev->phys;
1794 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001795 input_dev->open = wacom_open;
1796 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001797 input_dev->uniq = hdev->uniq;
1798 input_dev->id.bustype = hdev->bus;
1799 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001800 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001801 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001802 input_set_drvdata(input_dev, wacom);
1803
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001804 return input_dev;
1805}
1806
Jason Gerecked9f2d202015-07-13 18:03:44 -07001807static int wacom_allocate_inputs(struct wacom *wacom)
1808{
1809 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1810
1811 wacom_wac->pen_input = wacom_allocate_input(wacom);
1812 wacom_wac->touch_input = wacom_allocate_input(wacom);
1813 wacom_wac->pad_input = wacom_allocate_input(wacom);
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001814 if (!wacom_wac->pen_input ||
1815 !wacom_wac->touch_input ||
1816 !wacom_wac->pad_input)
Jason Gerecked9f2d202015-07-13 18:03:44 -07001817 return -ENOMEM;
Jason Gerecked9f2d202015-07-13 18:03:44 -07001818
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001819 wacom_wac->pen_input->name = wacom_wac->pen_name;
Jason Gerecked9f2d202015-07-13 18:03:44 -07001820 wacom_wac->touch_input->name = wacom_wac->touch_name;
1821 wacom_wac->pad_input->name = wacom_wac->pad_name;
1822
1823 return 0;
1824}
1825
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001826static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001827{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001828 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001829 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Jason Gerecke2636a3f2015-06-15 18:01:44 -07001830 int error = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001831
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001832 pen_input_dev = wacom_wac->pen_input;
1833 touch_input_dev = wacom_wac->touch_input;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001834 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001835
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001836 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001837 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001838
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001839 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1840 if (error) {
1841 /* no pen in use on this interface */
1842 input_free_device(pen_input_dev);
1843 wacom_wac->pen_input = NULL;
1844 pen_input_dev = NULL;
1845 } else {
1846 error = input_register_device(pen_input_dev);
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001847 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001848 goto fail;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001849 }
1850
1851 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1852 if (error) {
1853 /* no touch in use on this interface */
1854 input_free_device(touch_input_dev);
1855 wacom_wac->touch_input = NULL;
1856 touch_input_dev = NULL;
1857 } else {
1858 error = input_register_device(touch_input_dev);
1859 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001860 goto fail;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001861 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001862
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001863 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1864 if (error) {
1865 /* no pad in use on this interface */
1866 input_free_device(pad_input_dev);
1867 wacom_wac->pad_input = NULL;
1868 pad_input_dev = NULL;
1869 } else {
1870 error = input_register_device(pad_input_dev);
1871 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001872 goto fail;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001873 }
1874
Ping Cheng19635182012-04-29 21:09:18 -07001875 return 0;
1876
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001877fail:
1878 wacom_wac->pad_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001879 wacom_wac->touch_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001880 wacom_wac->pen_input = NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001881 return error;
1882}
1883
Jason Gerecke0be01712015-08-05 15:44:53 -07001884/*
1885 * Not all devices report physical dimensions from HID.
1886 * Compute the default from hardcoded logical dimension
1887 * and resolution before driver overwrites them.
1888 */
1889static void wacom_set_default_phy(struct wacom_features *features)
1890{
1891 if (features->x_resolution) {
1892 features->x_phy = (features->x_max * 100) /
1893 features->x_resolution;
1894 features->y_phy = (features->y_max * 100) /
1895 features->y_resolution;
1896 }
1897}
1898
1899static void wacom_calculate_res(struct wacom_features *features)
1900{
1901 /* set unit to "100th of a mm" for devices not reported by HID */
1902 if (!features->unit) {
1903 features->unit = 0x11;
1904 features->unitExpo = -3;
1905 }
1906
1907 features->x_resolution = wacom_calc_hid_res(features->x_max,
1908 features->x_phy,
1909 features->unit,
1910 features->unitExpo);
1911 features->y_resolution = wacom_calc_hid_res(features->y_max,
1912 features->y_phy,
1913 features->unit,
1914 features->unitExpo);
1915}
1916
Jason Gereckefce99572015-03-06 11:47:40 -08001917void wacom_battery_work(struct work_struct *work)
1918{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02001919 struct wacom *wacom = container_of(work, struct wacom, battery_work);
Jason Gereckefce99572015-03-06 11:47:40 -08001920
1921 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001922 !wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001923 wacom_initialize_battery(wacom);
1924 }
1925 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001926 wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001927 wacom_destroy_battery(wacom);
1928 }
1929}
1930
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001931static size_t wacom_compute_pktlen(struct hid_device *hdev)
1932{
1933 struct hid_report_enum *report_enum;
1934 struct hid_report *report;
1935 size_t size = 0;
1936
1937 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1938
1939 list_for_each_entry(report, &report_enum->report_list, list) {
Mathieu Magnaudetdabb05c62014-11-27 16:02:36 +01001940 size_t report_size = hid_report_len(report);
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001941 if (report_size > size)
1942 size = report_size;
1943 }
1944
1945 return size;
1946}
1947
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001948static void wacom_update_name(struct wacom *wacom, const char *suffix)
Ping Chengc24eab42015-04-24 15:32:51 -07001949{
1950 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1951 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke44b52502015-06-15 18:01:41 -07001952 char name[WACOM_NAME_MAX];
Ping Chengc24eab42015-04-24 15:32:51 -07001953
1954 /* Generic devices name unspecified */
1955 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1956 if (strstr(wacom->hdev->name, "Wacom") ||
1957 strstr(wacom->hdev->name, "wacom") ||
1958 strstr(wacom->hdev->name, "WACOM")) {
1959 /* name is in HID descriptor, use it */
Jason Gerecke44b52502015-06-15 18:01:41 -07001960 strlcpy(name, wacom->hdev->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001961
1962 /* strip out excess whitespaces */
1963 while (1) {
Jason Gerecke44b52502015-06-15 18:01:41 -07001964 char *gap = strstr(name, " ");
Ping Chengc24eab42015-04-24 15:32:51 -07001965 if (gap == NULL)
1966 break;
1967 /* shift everything including the terminator */
1968 memmove(gap, gap+1, strlen(gap));
1969 }
Jason Gereckef2209d42016-10-19 18:03:41 -07001970
1971 /* strip off excessive prefixing */
1972 if (strstr(name, "Wacom Co.,Ltd. Wacom ") == name) {
1973 int n = strlen(name);
1974 int x = strlen("Wacom Co.,Ltd. ");
1975 memmove(name, name+x, n-x+1);
1976 }
1977 if (strstr(name, "Wacom Co., Ltd. Wacom ") == name) {
1978 int n = strlen(name);
1979 int x = strlen("Wacom Co., Ltd. ");
1980 memmove(name, name+x, n-x+1);
1981 }
1982
Ping Chengc24eab42015-04-24 15:32:51 -07001983 /* get rid of trailing whitespace */
Jason Gerecke44b52502015-06-15 18:01:41 -07001984 if (name[strlen(name)-1] == ' ')
1985 name[strlen(name)-1] = '\0';
Ping Chengc24eab42015-04-24 15:32:51 -07001986 } else {
1987 /* no meaningful name retrieved. use product ID */
Jason Gerecke44b52502015-06-15 18:01:41 -07001988 snprintf(name, sizeof(name),
Ping Chengc24eab42015-04-24 15:32:51 -07001989 "%s %X", features->name, wacom->hdev->product);
1990 }
1991 } else {
Jason Gerecke44b52502015-06-15 18:01:41 -07001992 strlcpy(name, features->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001993 }
1994
Benjamin Tissoires99569532016-07-13 18:06:17 +02001995 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
1996 name, suffix);
1997
Ping Chengc24eab42015-04-24 15:32:51 -07001998 /* Append the device type to the name */
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001999 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002000 "%s%s Pen", name, suffix);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002001 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002002 "%s%s Finger", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07002003 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002004 "%s%s Pad", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07002005}
2006
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002007static void wacom_release_resources(struct wacom *wacom)
2008{
2009 struct hid_device *hdev = wacom->hdev;
2010
2011 if (!wacom->resources)
2012 return;
2013
2014 devres_release_group(&hdev->dev, wacom);
2015
2016 wacom->resources = false;
2017
2018 wacom->wacom_wac.pen_input = NULL;
2019 wacom->wacom_wac.touch_input = NULL;
2020 wacom->wacom_wac.pad_input = NULL;
2021}
2022
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002023static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
Ping Cheng3bea7332006-07-13 18:01:36 -07002024{
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002025 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2026 struct wacom_features *features = &wacom_wac->features;
2027 struct hid_device *hdev = wacom->hdev;
Jason Childse33da8a2010-02-17 22:38:31 -08002028 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002029 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07002030
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002031 features->pktlen = wacom_compute_pktlen(hdev);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002032 if (features->pktlen > WACOM_PKGLEN_MAX)
2033 return -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07002034
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002035 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2036 return -ENOMEM;
2037
2038 wacom->resources = true;
2039
Jason Gerecke3f14a632015-08-03 10:17:05 -07002040 error = wacom_allocate_inputs(wacom);
2041 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002042 goto fail;
Benjamin Tissoires494078b2014-09-23 12:08:07 -04002043
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002044 /*
2045 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2046 * into debug mode for the touch part.
2047 * We ignore the other interfaces.
2048 */
2049 if (features->type == BAMBOO_PAD) {
2050 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2051 features->type = HID_GENERIC;
2052 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2053 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2054 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002055 goto fail;
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002056 }
2057 }
2058
Ping Cheng401d7d12013-07-28 00:38:54 -07002059 /* set the default size in case we do not get them from hid */
2060 wacom_set_default_phy(features);
2061
Ping Chengf393ee22012-04-29 21:09:17 -07002062 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07002063 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng42f4f272015-04-15 16:53:54 -07002064 wacom_setup_device_quirks(wacom);
Jason Gerecke042628a2015-04-30 17:51:54 -07002065
Jason Gereckeaa86b182015-06-15 18:01:42 -07002066 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2067 features->type != WIRELESS) {
Jason Gerecke8e116d32015-04-30 17:51:55 -07002068 error = features->type == HID_GENERIC ? -ENODEV : 0;
2069
Jason Gerecke042628a2015-04-30 17:51:54 -07002070 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
Jason Gerecke8e116d32015-04-30 17:51:55 -07002071 hdev->name,
2072 error ? "Ignoring" : "Assuming pen");
2073
2074 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002075 goto fail;
Jason Gerecke042628a2015-04-30 17:51:54 -07002076
Jason Gereckeaa86b182015-06-15 18:01:42 -07002077 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -07002078 }
2079
Ping Cheng401d7d12013-07-28 00:38:54 -07002080 wacom_calculate_res(features);
2081
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002082 wacom_update_name(wacom, wireless ? " (WL)" : "");
Ping Cheng4492eff2010-03-19 22:18:15 -07002083
Jason Gereckea9ce7852017-01-17 15:38:58 -08002084 error = wacom_add_shared_data(hdev);
2085 if (error)
2086 goto fail;
Ping Cheng49b764a2010-02-20 00:53:49 -08002087
Jason Gereckeccad85c2015-08-03 10:17:04 -07002088 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002089 (features->quirks & WACOM_QUIRK_BATTERY)) {
2090 error = wacom_initialize_battery(wacom);
2091 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002092 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002093 }
2094
Jason Gerecke3f14a632015-08-03 10:17:05 -07002095 error = wacom_register_inputs(wacom);
2096 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002097 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002098
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002099 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002100 error = wacom_initialize_leds(wacom);
2101 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002102 goto fail;
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002103
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002104 error = wacom_initialize_remotes(wacom);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002105 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002106 goto fail;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002107 }
2108
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002109 if (features->type == HID_GENERIC)
2110 connect_mask |= HID_CONNECT_DRIVER;
2111
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002112 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002113 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002114 if (error) {
2115 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002116 goto fail;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002117 }
2118
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002119 if (!wireless) {
2120 /* Note that if query fails it is not a hard failure */
2121 wacom_query_tablet_data(hdev, features);
2122 }
Jason Gerecke86e88f02015-11-03 16:57:58 -08002123
2124 /* touch only Bamboo doesn't support pen */
2125 if ((features->type == BAMBOO_TOUCH) &&
2126 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2127 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002128 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08002129 }
2130
2131 /* pen only Bamboo neither support touch nor pad */
2132 if ((features->type == BAMBOO_PEN) &&
2133 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2134 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2135 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002136 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08002137 }
2138
Jason Gereckeccad85c2015-08-03 10:17:04 -07002139 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002140 error = hid_hw_open(hdev);
2141
Ping Chengeda01da2015-09-23 13:51:15 -07002142 if ((wacom_wac->features.type == INTUOSHT ||
Benjamin Tissoires97f9afa2016-07-13 18:05:49 +02002143 wacom_wac->features.type == INTUOSHT2) &&
Ping Chengeda01da2015-09-23 13:51:15 -07002144 (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
Benjamin Tissoires97f9afa2016-07-13 18:05:49 +02002145 wacom_wac->shared->type = wacom_wac->features.type;
2146 wacom_wac->shared->touch_input = wacom_wac->touch_input;
Ping Cheng961794a2013-12-05 12:54:53 -08002147 }
2148
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002149 devres_close_group(&hdev->dev, wacom);
2150
Ping Cheng3bea7332006-07-13 18:01:36 -07002151 return 0;
2152
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002153fail_quirks:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002154 hid_hw_stop(hdev);
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002155fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002156 wacom_release_resources(wacom);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002157 return error;
2158}
2159
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002160static void wacom_wireless_work(struct work_struct *work)
2161{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002162 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002163 struct usb_device *usbdev = wacom->usbdev;
2164 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2165 struct hid_device *hdev1, *hdev2;
2166 struct wacom *wacom1, *wacom2;
2167 struct wacom_wac *wacom_wac1, *wacom_wac2;
2168 int error;
2169
2170 /*
2171 * Regardless if this is a disconnect or a new tablet,
2172 * remove any existing input and battery devices.
2173 */
2174
2175 wacom_destroy_battery(wacom);
2176
2177 /* Stylus interface */
2178 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2179 wacom1 = hid_get_drvdata(hdev1);
2180 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002181 wacom_release_resources(wacom1);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002182
2183 /* Touch interface */
2184 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2185 wacom2 = hid_get_drvdata(hdev2);
2186 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002187 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002188
2189 if (wacom_wac->pid == 0) {
2190 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002191 } else {
2192 const struct hid_device_id *id = wacom_ids;
2193
2194 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2195 wacom_wac->pid);
2196
2197 while (id->bus) {
2198 if (id->vendor == USB_VENDOR_ID_WACOM &&
2199 id->product == wacom_wac->pid)
2200 break;
2201 id++;
2202 }
2203
2204 if (!id->bus) {
2205 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2206 return;
2207 }
2208
2209 /* Stylus interface */
2210 wacom_wac1->features =
2211 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002212
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002213 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002214 hid_hw_stop(hdev1);
2215 error = wacom_parse_and_register(wacom1, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002216 if (error)
2217 goto fail;
2218
2219 /* Touch interface */
2220 if (wacom_wac1->features.touch_max ||
2221 (wacom_wac1->features.type >= INTUOSHT &&
2222 wacom_wac1->features.type <= BAMBOO_PT)) {
2223 wacom_wac2->features =
2224 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002225 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002226 hid_hw_stop(hdev2);
2227 error = wacom_parse_and_register(wacom2, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002228 if (error)
2229 goto fail;
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002230 }
2231
Benjamin Tissoires99569532016-07-13 18:06:17 +02002232 strlcpy(wacom_wac->name, wacom_wac1->name,
2233 sizeof(wacom_wac->name));
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002234 error = wacom_initialize_battery(wacom);
2235 if (error)
2236 goto fail;
2237 }
2238
2239 return;
2240
2241fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002242 wacom_release_resources(wacom1);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002243 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002244 return;
2245}
2246
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002247static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2248{
2249 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002250 u32 serial = remote->remotes[index].serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002251 int i;
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002252 unsigned long flags;
2253
2254 spin_lock_irqsave(&remote->remote_lock, flags);
2255 remote->remotes[index].registered = false;
2256 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002257
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002258 if (remote->remotes[index].battery.battery)
2259 devres_release_group(&wacom->hdev->dev,
2260 &remote->remotes[index].battery.bat_desc);
2261
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002262 if (remote->remotes[index].group.name)
2263 devres_release_group(&wacom->hdev->dev,
2264 &remote->remotes[index]);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002265
2266 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002267 if (remote->remotes[i].serial == serial) {
2268 remote->remotes[i].serial = 0;
2269 remote->remotes[i].group.name = NULL;
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002270 remote->remotes[i].registered = false;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002271 remote->remotes[i].battery.battery = NULL;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002272 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2273 }
2274 }
2275}
2276
2277static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2278 unsigned int index)
2279{
2280 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002281 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002282 int error, k;
2283
2284 /* A remote can pair more than once with an EKR,
2285 * check to make sure this serial isn't already paired.
2286 */
2287 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002288 if (remote->remotes[k].serial == serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002289 break;
2290 }
2291
2292 if (k < WACOM_MAX_REMOTES) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002293 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002294 return 0;
2295 }
2296
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002297 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002298 return -ENOMEM;
2299
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002300 error = wacom_remote_create_attr_group(wacom, serial, index);
2301 if (error)
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002302 goto fail;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002303
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002304 remote->remotes[index].input = wacom_allocate_input(wacom);
2305 if (!remote->remotes[index].input) {
2306 error = -ENOMEM;
2307 goto fail;
2308 }
2309 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2310 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2311
2312 if (!remote->remotes[index].input->name) {
2313 error = -EINVAL;
2314 goto fail;
2315 }
2316
2317 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2318 &wacom->wacom_wac);
2319 if (error)
2320 goto fail;
2321
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002322 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002323
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002324 error = input_register_device(remote->remotes[index].input);
2325 if (error)
2326 goto fail;
2327
Benjamin Tissoires97f55412016-07-13 18:06:10 +02002328 error = wacom_led_groups_alloc_and_register_one(
2329 &remote->remotes[index].input->dev,
2330 wacom, index, 3, true);
2331 if (error)
2332 goto fail;
2333
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002334 remote->remotes[index].registered = true;
2335
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002336 devres_close_group(dev, &remote->remotes[index]);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002337 return 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002338
2339fail:
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002340 devres_release_group(dev, &remote->remotes[index]);
2341 remote->remotes[index].serial = 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002342 return error;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002343}
2344
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002345static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2346{
2347 struct wacom_remote *remote = wacom->remote;
2348 int error;
2349
2350 if (!remote->remotes[index].registered)
2351 return 0;
2352
2353 if (remote->remotes[index].battery.battery)
2354 return 0;
2355
2356 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2357 return 0;
2358
2359 error = __wacom_initialize_battery(wacom,
2360 &wacom->remote->remotes[index].battery);
2361 if (error)
2362 return error;
2363
2364 return 0;
2365}
2366
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002367static void wacom_remote_work(struct work_struct *work)
2368{
2369 struct wacom *wacom = container_of(work, struct wacom, remote_work);
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002370 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002371 struct wacom_remote_data data;
2372 unsigned long flags;
2373 unsigned int count;
2374 u32 serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002375 int i;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002376
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002377 spin_lock_irqsave(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002378
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002379 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002380
2381 if (count != sizeof(data)) {
2382 hid_err(wacom->hdev,
2383 "workitem triggered without status available\n");
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002384 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002385 return;
2386 }
2387
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002388 if (!kfifo_is_empty(&remote->remote_fifo))
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002389 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2390
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002391 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002392
2393 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2394 serial = data.remote[i].serial;
2395 if (data.remote[i].connected) {
2396
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002397 if (remote->remotes[i].serial == serial) {
2398 wacom_remote_attach_battery(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002399 continue;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002400 }
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002401
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002402 if (remote->remotes[i].serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002403 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002404
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002405 wacom_remote_create_one(wacom, serial, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002406
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002407 } else if (remote->remotes[i].serial) {
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002408 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002409 }
2410 }
2411}
2412
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002413static int wacom_probe(struct hid_device *hdev,
2414 const struct hid_device_id *id)
2415{
2416 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2417 struct usb_device *dev = interface_to_usbdev(intf);
2418 struct wacom *wacom;
2419 struct wacom_wac *wacom_wac;
2420 struct wacom_features *features;
2421 int error;
2422
2423 if (!id->driver_data)
2424 return -EINVAL;
2425
2426 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2427
2428 /* hid-core sets this quirk for the boot interface */
2429 hdev->quirks &= ~HID_QUIRK_NOGET;
2430
Benjamin Tissoires19b64332016-07-13 18:05:58 +02002431 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002432 if (!wacom)
2433 return -ENOMEM;
2434
2435 hid_set_drvdata(hdev, wacom);
2436 wacom->hdev = hdev;
2437
2438 wacom_wac = &wacom->wacom_wac;
2439 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2440 features = &wacom_wac->features;
2441
2442 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2443 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002444 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002445 }
2446
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002447 wacom_wac->hid_data.inputmode = -1;
Jason Gerecke326ea2a2016-04-04 11:26:52 -07002448 wacom_wac->mode_report = -1;
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002449
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002450 wacom->usbdev = dev;
2451 wacom->intf = intf;
2452 mutex_init(&wacom->lock);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002453 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2454 INIT_WORK(&wacom->battery_work, wacom_battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002455 INIT_WORK(&wacom->remote_work, wacom_remote_work);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002456
2457 /* ask for the report descriptor to be loaded by HID */
2458 error = hid_parse(hdev);
2459 if (error) {
2460 hid_err(hdev, "parse failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002461 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002462 }
2463
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002464 error = wacom_parse_and_register(wacom, false);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002465 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002466 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002467
2468 if (hdev->bus == BUS_BLUETOOTH) {
2469 error = device_create_file(&hdev->dev, &dev_attr_speed);
2470 if (error)
2471 hid_warn(hdev,
2472 "can't create sysfs speed attribute err: %d\n",
2473 error);
2474 }
2475
2476 return 0;
2477
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002478fail:
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002479 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04002480 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07002481}
2482
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002483static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07002484{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002485 struct wacom *wacom = hid_get_drvdata(hdev);
Benjamin Tissoiresf6205162016-02-12 17:27:45 +01002486 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2487 struct wacom_features *features = &wacom_wac->features;
2488
2489 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2490 hid_hw_close(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07002491
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002492 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002493
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002494 cancel_work_sync(&wacom->wireless_work);
2495 cancel_work_sync(&wacom->battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002496 cancel_work_sync(&wacom->remote_work);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002497 if (hdev->bus == BUS_BLUETOOTH)
2498 device_remove_file(&hdev->dev, &dev_attr_speed);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002499
2500 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04002501}
2502
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002503#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002504static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002505{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002506 struct wacom *wacom = hid_get_drvdata(hdev);
2507 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04002508
2509 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002510
2511 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07002512 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002513 wacom_led_control(wacom);
2514
Oliver Neukume7224092008-04-15 01:31:57 -04002515 mutex_unlock(&wacom->lock);
2516
2517 return 0;
2518}
2519
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002520static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002521{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002522 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002523}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002524#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04002525
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002526static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07002527 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08002528 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07002529 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002530 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002531 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002532#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04002533 .resume = wacom_resume,
2534 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002535#endif
2536 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07002537};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002538module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07002539
2540MODULE_VERSION(DRIVER_VERSION);
2541MODULE_AUTHOR(DRIVER_AUTHOR);
2542MODULE_DESCRIPTION(DRIVER_DESC);
2543MODULE_LICENSE(DRIVER_LICENSE);