blob: 5dd2640c7f216f519f1a4051d827da48991ca5a2 [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
28#define WAC_REMOTE_SERIAL_MAX_STRLEN 9
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070029
Ping Chenge0984bc2014-09-10 12:40:05 -070030#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
31#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
Aaron Skomra72b236d2015-08-20 16:05:17 -070032#define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
Ping Chenge0984bc2014-09-10 12:40:05 -070033
Ping Chengc64d8832014-09-10 12:41:04 -070034static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
35 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070036{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070037 int retval;
38
39 do {
Ping Chengc64d8832014-09-10 12:41:04 -070040 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070041 HID_REQ_GET_REPORT);
Jason Gereckeaef31562015-05-21 10:44:31 -070042 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
43
44 if (retval < 0)
45 hid_err(hdev, "wacom_get_report: ran out of retries "
46 "(last error = %d)\n", retval);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070047
48 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070049}
50
Przemo Firszt296b7372014-08-06 14:00:38 -070051static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
52 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070053{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070054 int retval;
55
56 do {
Przemo Firszt296b7372014-08-06 14:00:38 -070057 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070058 HID_REQ_SET_REPORT);
Jason Gereckeaef31562015-05-21 10:44:31 -070059 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
60
61 if (retval < 0)
62 hid_err(hdev, "wacom_set_report: ran out of retries "
63 "(last error = %d)\n", retval);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070064
65 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070066}
67
Benjamin Tissoires29b47392014-07-24 12:52:23 -070068static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
69 u8 *raw_data, int size)
Ping Cheng3bea7332006-07-13 18:01:36 -070070{
Benjamin Tissoires29b47392014-07-24 12:52:23 -070071 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070072
Benjamin Tissoires29b47392014-07-24 12:52:23 -070073 if (size > WACOM_PKGLEN_MAX)
74 return 1;
Ping Cheng3bea7332006-07-13 18:01:36 -070075
Benjamin Tissoires29b47392014-07-24 12:52:23 -070076 memcpy(wacom->wacom_wac.data, raw_data, size);
Ping Cheng3bea7332006-07-13 18:01:36 -070077
Benjamin Tissoires29b47392014-07-24 12:52:23 -070078 wacom_wac_irq(&wacom->wacom_wac, size);
79
80 return 0;
Ping Cheng3bea7332006-07-13 18:01:36 -070081}
82
Ping Cheng3bea7332006-07-13 18:01:36 -070083static int wacom_open(struct input_dev *dev)
84{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040085 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -070086
Benjamin Tissoiresdff67412014-12-01 11:52:40 -050087 return hid_hw_open(wacom->hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070088}
89
90static void wacom_close(struct input_dev *dev)
91{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040092 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -070093
Benjamin Tissoires29b47392014-07-24 12:52:23 -070094 hid_hw_close(wacom->hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070095}
96
Chris Bagwell16bf2882012-03-25 23:26:20 -070097/*
Benjamin Tissoires198fdee2014-07-24 13:03:05 -070098 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
Jason Gerecke115d5e12012-10-03 17:24:32 -070099 */
100static int wacom_calc_hid_res(int logical_extents, int physical_extents,
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700101 unsigned unit, int exponent)
Jason Gerecke115d5e12012-10-03 17:24:32 -0700102{
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700103 struct hid_field field = {
104 .logical_maximum = logical_extents,
105 .physical_maximum = physical_extents,
106 .unit = unit,
107 .unit_exponent = exponent,
108 };
Jason Gerecke115d5e12012-10-03 17:24:32 -0700109
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700110 return hidinput_calc_abs_res(&field, ABS_X);
Jason Gerecke115d5e12012-10-03 17:24:32 -0700111}
112
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700113static void wacom_feature_mapping(struct hid_device *hdev,
114 struct hid_field *field, struct hid_usage *usage)
Chris Bagwell41343612011-10-26 22:32:52 -0700115{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700116 struct wacom *wacom = hid_get_drvdata(hdev);
117 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400118 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400119 u8 *data;
120 int ret;
Chris Bagwell41343612011-10-26 22:32:52 -0700121
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700122 switch (usage->hid) {
123 case HID_DG_CONTACTMAX:
124 /* leave touch_max as is if predefined */
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400125 if (!features->touch_max) {
126 /* read manually */
127 data = kzalloc(2, GFP_KERNEL);
128 if (!data)
129 break;
130 data[0] = field->report->id;
131 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700132 data, 2, WAC_CMD_RETRIES);
133 if (ret == 2) {
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400134 features->touch_max = data[1];
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700135 } else {
136 features->touch_max = 16;
137 hid_warn(hdev, "wacom_feature_mapping: "
138 "could not get HID_DG_CONTACTMAX, "
139 "defaulting to %d\n",
140 features->touch_max);
141 }
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400142 kfree(data);
143 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700144 break;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400145 case HID_DG_INPUTMODE:
146 /* Ignore if value index is out of bounds. */
147 if (usage->usage_index >= field->report_count) {
148 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
149 break;
150 }
151
152 hid_data->inputmode = field->report->id;
153 hid_data->inputmode_index = usage->usage_index;
154 break;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700155
156 case HID_UP_DIGITIZER:
157 if (field->report->id == 0x0B &&
158 (field->application == WACOM_G9_DIGITIZER ||
159 field->application == WACOM_G11_DIGITIZER)) {
160 wacom->wacom_wac.mode_report = field->report->id;
161 wacom->wacom_wac.mode_value = 0;
162 }
163 break;
164
165 case WACOM_G9_PAGE:
166 case WACOM_G11_PAGE:
167 if (field->report->id == 0x03 &&
168 (field->application == WACOM_G9_TOUCHSCREEN ||
169 field->application == WACOM_G11_TOUCHSCREEN)) {
170 wacom->wacom_wac.mode_report = field->report->id;
171 wacom->wacom_wac.mode_value = 0;
172 }
173 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700174 }
175}
176
Chris Bagwell428f8582011-10-26 22:26:59 -0700177/*
178 * Interface Descriptor of wacom devices can be incomplete and
179 * inconsistent so wacom_features table is used to store stylus
180 * device's packet lengths, various maximum values, and tablet
181 * resolution based on product ID's.
182 *
183 * For devices that contain 2 interfaces, wacom_features table is
184 * inaccurate for the touch interface. Since the Interface Descriptor
185 * for touch interfaces has pretty complete data, this function exists
186 * to query tablet for this missing information instead of hard coding in
187 * an additional table.
188 *
189 * A typical Interface Descriptor for a stylus will contain a
190 * boot mouse application collection that is not of interest and this
191 * function will ignore it.
192 *
193 * It also contains a digitizer application collection that also is not
194 * of interest since any information it contains would be duplicate
195 * of what is in wacom_features. Usually it defines a report of an array
196 * of bytes that could be used as max length of the stylus packet returned.
197 * If it happens to define a Digitizer-Stylus Physical Collection then
198 * the X and Y logical values contain valid data but it is ignored.
199 *
200 * A typical Interface Descriptor for a touch interface will contain a
201 * Digitizer-Finger Physical Collection which will define both logical
202 * X/Y maximum as well as the physical size of tablet. Since touch
203 * interfaces haven't supported pressure or distance, this is enough
204 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700205 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700206 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
207 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700208 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700209static void wacom_usage_mapping(struct hid_device *hdev,
210 struct hid_field *field, struct hid_usage *usage)
211{
212 struct wacom *wacom = hid_get_drvdata(hdev);
213 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoiresd97a5522015-01-05 16:32:12 -0500214 bool finger = WACOM_FINGER_FIELD(field);
215 bool pen = WACOM_PEN_FIELD(field);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700216
217 /*
218 * Requiring Stylus Usage will ignore boot mouse
219 * X/Y values and some cases of invalid Digitizer X/Y
220 * values commonly reported.
221 */
Jason Gerecke042628a2015-04-30 17:51:54 -0700222 if (pen)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700223 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -0700224 else if (finger)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700225 features->device_type |= WACOM_DEVICETYPE_TOUCH;
Jason Gerecke042628a2015-04-30 17:51:54 -0700226 else
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700227 return;
228
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800229 /*
230 * Bamboo models do not support HID_DG_CONTACTMAX.
231 * And, Bamboo Pen only descriptor contains touch.
232 */
Ping Cheng3b164a02015-09-23 09:59:10 -0700233 if (features->type > BAMBOO_PT) {
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800234 /* ISDv4 touch devices at least supports one touch point */
235 if (finger && !features->touch_max)
236 features->touch_max = 1;
237 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700238
239 switch (usage->hid) {
240 case HID_GD_X:
241 features->x_max = field->logical_maximum;
242 if (finger) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700243 features->x_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700244 if ((features->type != BAMBOO_PT) &&
245 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700246 features->unit = field->unit;
247 features->unitExpo = field->unit_exponent;
248 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700249 }
250 break;
251 case HID_GD_Y:
252 features->y_max = field->logical_maximum;
253 if (finger) {
254 features->y_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700255 if ((features->type != BAMBOO_PT) &&
256 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700257 features->unit = field->unit;
258 features->unitExpo = field->unit_exponent;
259 }
260 }
261 break;
262 case HID_DG_TIPPRESSURE:
263 if (pen)
264 features->pressure_max = field->logical_maximum;
265 break;
266 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400267
268 if (features->type == HID_GENERIC)
269 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700270}
271
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800272static void wacom_post_parse_hid(struct hid_device *hdev,
273 struct wacom_features *features)
274{
275 struct wacom *wacom = hid_get_drvdata(hdev);
276 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
277
278 if (features->type == HID_GENERIC) {
279 /* Any last-minute generic device setup */
280 if (features->touch_max > 1) {
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -0700281 input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800282 INPUT_MT_DIRECT);
283 }
284 }
285}
286
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700287static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800288 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500289{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700290 struct hid_report_enum *rep_enum;
291 struct hid_report *hreport;
292 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500293
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700294 /* check features first */
295 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
296 list_for_each_entry(hreport, &rep_enum->report_list, list) {
297 for (i = 0; i < hreport->maxfield; i++) {
298 /* Ignore if report count is out of bounds. */
299 if (hreport->field[i]->report_count < 1)
300 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500301
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700302 for (j = 0; j < hreport->field[i]->maxusage; j++) {
303 wacom_feature_mapping(hdev, hreport->field[i],
304 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500305 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500306 }
307 }
308
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700309 /* now check the input usages */
310 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
311 list_for_each_entry(hreport, &rep_enum->report_list, list) {
312
313 if (!hreport->maxfield)
314 continue;
315
316 for (i = 0; i < hreport->maxfield; i++)
317 for (j = 0; j < hreport->field[i]->maxusage; j++)
318 wacom_usage_mapping(hdev, hreport->field[i],
319 hreport->field[i]->usage + j);
320 }
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800321
322 wacom_post_parse_hid(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -0500323}
324
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400325static int wacom_hid_set_device_mode(struct hid_device *hdev)
326{
327 struct wacom *wacom = hid_get_drvdata(hdev);
328 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
329 struct hid_report *r;
330 struct hid_report_enum *re;
331
332 if (hid_data->inputmode < 0)
333 return 0;
334
335 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
336 r = re->report_id_hash[hid_data->inputmode];
337 if (r) {
338 r->field[0]->value[hid_data->inputmode_index] = 2;
339 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
340 }
341 return 0;
342}
343
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700344static int wacom_set_device_mode(struct hid_device *hdev,
345 struct wacom_wac *wacom_wac)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700346{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700347 u8 *rep_data;
348 struct hid_report *r;
349 struct hid_report_enum *re;
350 int length;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700351 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700352
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700353 if (wacom_wac->mode_report < 0)
354 return 0;
355
356 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
357 r = re->report_id_hash[wacom_wac->mode_report];
358 if (!r)
359 return -EINVAL;
360
361 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700362 if (!rep_data)
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700363 return -ENOMEM;
364
365 length = hid_report_len(r);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700366
Jason Gereckefe494bc2012-10-03 17:25:35 -0700367 do {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700368 rep_data[0] = wacom_wac->mode_report;
369 rep_data[1] = wacom_wac->mode_value;
Chris Bagwell9937c022013-01-23 19:37:34 -0800370
Przemo Firszt296b7372014-08-06 14:00:38 -0700371 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
372 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700373 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700374 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700375 rep_data, length, 1);
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700376 } while (error >= 0 &&
377 rep_data[1] != wacom_wac->mode_report &&
378 limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700379
380 kfree(rep_data);
381
382 return error < 0 ? error : 0;
383}
384
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700385static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
386 struct wacom_features *features)
387{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700388 struct wacom *wacom = hid_get_drvdata(hdev);
389 int ret;
390 u8 rep_data[2];
391
392 switch (features->type) {
393 case GRAPHIRE_BT:
394 rep_data[0] = 0x03;
395 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700396 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
397 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700398
399 if (ret >= 0) {
400 rep_data[0] = speed == 0 ? 0x05 : 0x06;
401 rep_data[1] = 0x00;
402
403 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700404 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700405
406 if (ret >= 0) {
407 wacom->wacom_wac.bt_high_speed = speed;
408 return 0;
409 }
410 }
411
412 /*
413 * Note that if the raw queries fail, it's not a hard failure
414 * and it is safe to continue
415 */
416 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
417 rep_data[0], ret);
418 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700419 case INTUOS4WL:
420 if (speed == 1)
421 wacom->wacom_wac.bt_features &= ~0x20;
422 else
423 wacom->wacom_wac.bt_features |= 0x20;
424
425 rep_data[0] = 0x03;
426 rep_data[1] = wacom->wacom_wac.bt_features;
427
Przemo Firszt296b7372014-08-06 14:00:38 -0700428 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
429 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700430 if (ret >= 0)
431 wacom->wacom_wac.bt_high_speed = speed;
432 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700433 }
434
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700435 return 0;
436}
437
Jason Gereckefe494bc2012-10-03 17:25:35 -0700438/*
439 * Switch the tablet into its most-capable mode. Wacom tablets are
440 * typically configured to power-up in a mode which sends mouse-like
441 * reports to the OS. To get absolute position, pressure data, etc.
442 * from the tablet, it is necessary to switch the tablet out of this
443 * mode and into one which sends the full range of tablet data.
444 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700445static int wacom_query_tablet_data(struct hid_device *hdev,
446 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700447{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700448 struct wacom *wacom = hid_get_drvdata(hdev);
449 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
450
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700451 if (hdev->bus == BUS_BLUETOOTH)
452 return wacom_bt_query_tablet_data(hdev, 1, features);
453
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700454 if (features->type != HID_GENERIC) {
455 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
456 if (features->type > TABLETPC) {
457 /* MT Tablet PC touch */
458 wacom_wac->mode_report = 3;
459 wacom_wac->mode_value = 4;
460 } else if (features->type == WACOM_24HDT) {
461 wacom_wac->mode_report = 18;
462 wacom_wac->mode_value = 2;
463 } else if (features->type == WACOM_27QHDT) {
464 wacom_wac->mode_report = 131;
465 wacom_wac->mode_value = 2;
466 } else if (features->type == BAMBOO_PAD) {
467 wacom_wac->mode_report = 2;
468 wacom_wac->mode_value = 2;
469 }
470 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
471 if (features->type <= BAMBOO_PT) {
472 wacom_wac->mode_report = 2;
473 wacom_wac->mode_value = 2;
474 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700475 }
476 }
477
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700478 wacom_set_device_mode(hdev, wacom_wac);
479
480 if (features->type == HID_GENERIC)
481 return wacom_hid_set_device_mode(hdev);
482
Jason Gereckefe494bc2012-10-03 17:25:35 -0700483 return 0;
484}
485
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700486static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700487 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800488{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700489 struct wacom *wacom = hid_get_drvdata(hdev);
490 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800491
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700492 /* default features */
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700493 features->x_fuzz = 4;
494 features->y_fuzz = 4;
495 features->pressure_fuzz = 0;
Jason Gereckebef7e202016-04-22 14:30:53 -0700496 features->distance_fuzz = 1;
497 features->tilt_fuzz = 1;
Ping Chengec67bbe2009-12-15 00:35:24 -0800498
Chris Bagwelld3825d52012-03-25 23:26:11 -0700499 /*
500 * The wireless device HID is basic and layout conflicts with
501 * other tablets (monitor and touch interface can look like pen).
502 * Skip the query for this type and modify defaults based on
503 * interface number.
504 */
505 if (features->type == WIRELESS) {
Jason Gerecke3f14a632015-08-03 10:17:05 -0700506 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
Jason Gereckeccad85c2015-08-03 10:17:04 -0700507 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700508 else
Jason Gereckeaa86b182015-06-15 18:01:42 -0700509 features->device_type = WACOM_DEVICETYPE_NONE;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700510 return;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700511 }
512
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700513 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800514}
515
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700516struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700517 struct list_head list;
518 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700519 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700520 struct wacom_shared shared;
521};
522
523static LIST_HEAD(wacom_udev_list);
524static DEFINE_MUTEX(wacom_udev_list_lock);
525
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700526static bool wacom_are_sibling(struct hid_device *hdev,
527 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700528{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700529 struct wacom *wacom = hid_get_drvdata(hdev);
530 struct wacom_features *features = &wacom->wacom_wac.features;
531 int vid = features->oVid;
532 int pid = features->oPid;
533 int n1,n2;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700534
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700535 if (vid == 0 && pid == 0) {
536 vid = hdev->vendor;
537 pid = hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700538 }
539
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700540 if (vid != sibling->vendor || pid != sibling->product)
541 return false;
542
543 /* Compare the physical path. */
544 n1 = strrchr(hdev->phys, '.') - hdev->phys;
545 n2 = strrchr(sibling->phys, '.') - sibling->phys;
546 if (n1 != n2 || n1 <= 0 || n2 <= 0)
547 return false;
548
549 return !strncmp(hdev->phys, sibling->phys, n1);
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700550}
551
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700552static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700553{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700554 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700555
556 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700557 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700558 kref_get(&data->kref);
559 return data;
560 }
561 }
562
563 return NULL;
564}
565
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700566static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700567{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700568 struct wacom *wacom = hid_get_drvdata(hdev);
569 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
570 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700571 int retval = 0;
572
573 mutex_lock(&wacom_udev_list_lock);
574
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700575 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700576 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700577 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700578 if (!data) {
579 retval = -ENOMEM;
580 goto out;
581 }
582
583 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700584 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700585 list_add_tail(&data->list, &wacom_udev_list);
586 }
587
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700588 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700589
Jason Gereckeaa86b182015-06-15 18:01:42 -0700590 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500591 wacom_wac->shared->touch = hdev;
Jason Gereckeaa86b182015-06-15 18:01:42 -0700592 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500593 wacom_wac->shared->pen = hdev;
594
Ping Cheng4492eff2010-03-19 22:18:15 -0700595out:
596 mutex_unlock(&wacom_udev_list_lock);
597 return retval;
598}
599
600static void wacom_release_shared_data(struct kref *kref)
601{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700602 struct wacom_hdev_data *data =
603 container_of(kref, struct wacom_hdev_data, kref);
Ping Cheng4492eff2010-03-19 22:18:15 -0700604
605 mutex_lock(&wacom_udev_list_lock);
606 list_del(&data->list);
607 mutex_unlock(&wacom_udev_list_lock);
608
609 kfree(data);
610}
611
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500612static void wacom_remove_shared_data(struct wacom *wacom)
Ping Cheng4492eff2010-03-19 22:18:15 -0700613{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700614 struct wacom_hdev_data *data;
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500615 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Ping Cheng4492eff2010-03-19 22:18:15 -0700616
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500617 if (wacom_wac->shared) {
618 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
619 shared);
620
621 if (wacom_wac->shared->touch == wacom->hdev)
622 wacom_wac->shared->touch = NULL;
623 else if (wacom_wac->shared->pen == wacom->hdev)
624 wacom_wac->shared->pen = NULL;
625
Ping Cheng4492eff2010-03-19 22:18:15 -0700626 kref_put(&data->kref, wacom_release_shared_data);
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500627 wacom_wac->shared = NULL;
Ping Cheng4492eff2010-03-19 22:18:15 -0700628 }
629}
630
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700631static int wacom_led_control(struct wacom *wacom)
632{
633 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700634 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700635 unsigned char report_id = WAC_CMD_LED_CONTROL;
636 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700637
Ping Cheng912ca212014-09-10 12:41:31 -0700638 if (wacom->wacom_wac.pid) { /* wireless connected */
639 report_id = WAC_CMD_WL_LED_CONTROL;
640 buf_size = 13;
641 }
642 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700643 if (!buf)
644 return -ENOMEM;
645
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700646 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700647 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700648 /*
649 * Touch Ring and crop mark LED luminance may take on
650 * one of four values:
651 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
652 */
653 int ring_led = wacom->led.select[0] & 0x03;
654 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
655 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700656 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700657
Ping Cheng912ca212014-09-10 12:41:31 -0700658 buf[0] = report_id;
659 if (wacom->wacom_wac.pid) {
660 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
661 buf, buf_size, WAC_CMD_RETRIES);
662 buf[0] = report_id;
663 buf[4] = led_bits;
664 } else
665 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700666 }
667 else {
668 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700669
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700670 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
671 wacom->wacom_wac.features.type == WACOM_24HD)
672 led |= (wacom->led.select[1] << 4) | 0x40;
673
Ping Cheng912ca212014-09-10 12:41:31 -0700674 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700675 buf[1] = led;
676 buf[2] = wacom->led.llv;
677 buf[3] = wacom->led.hlv;
678 buf[4] = wacom->led.img_lum;
679 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700680
Ping Cheng912ca212014-09-10 12:41:31 -0700681 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700682 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700683 kfree(buf);
684
685 return retval;
686}
687
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700688static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
689 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700690{
691 unsigned char *buf;
692 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700693 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700694
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700695 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700696 if (!buf)
697 return -ENOMEM;
698
699 /* Send 'start' command */
700 buf[0] = WAC_CMD_ICON_START;
701 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700702 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
703 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700704 if (retval < 0)
705 goto out;
706
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700707 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700708 buf[1] = button_id & 0x07;
709 for (i = 0; i < 4; i++) {
710 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700711 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700712
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700713 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700714 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700715 if (retval < 0)
716 break;
717 }
718
719 /* Send 'stop' */
720 buf[0] = WAC_CMD_ICON_START;
721 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700722 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
723 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700724
725out:
726 kfree(buf);
727 return retval;
728}
729
Ping Cheng09e7d942011-10-04 23:51:14 -0700730static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700731 const char *buf, size_t count)
732{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800733 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700734 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700735 unsigned int id;
736 int err;
737
738 err = kstrtouint(buf, 10, &id);
739 if (err)
740 return err;
741
742 mutex_lock(&wacom->lock);
743
Ping Cheng09e7d942011-10-04 23:51:14 -0700744 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700745 err = wacom_led_control(wacom);
746
747 mutex_unlock(&wacom->lock);
748
749 return err < 0 ? err : count;
750}
751
Ping Cheng09e7d942011-10-04 23:51:14 -0700752#define DEVICE_LED_SELECT_ATTR(SET_ID) \
753static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
754 struct device_attribute *attr, const char *buf, size_t count) \
755{ \
756 return wacom_led_select_store(dev, SET_ID, buf, count); \
757} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700758static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
759 struct device_attribute *attr, char *buf) \
760{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800761 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700762 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700763 return scnprintf(buf, PAGE_SIZE, "%d\n", \
764 wacom->led.select[SET_ID]); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700765} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700766static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700767 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700768 wacom_led##SET_ID##_select_store)
769
770DEVICE_LED_SELECT_ATTR(0);
771DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700772
773static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
774 const char *buf, size_t count)
775{
776 unsigned int value;
777 int err;
778
779 err = kstrtouint(buf, 10, &value);
780 if (err)
781 return err;
782
783 mutex_lock(&wacom->lock);
784
785 *dest = value & 0x7f;
786 err = wacom_led_control(wacom);
787
788 mutex_unlock(&wacom->lock);
789
790 return err < 0 ? err : count;
791}
792
793#define DEVICE_LUMINANCE_ATTR(name, field) \
794static ssize_t wacom_##name##_luminance_store(struct device *dev, \
795 struct device_attribute *attr, const char *buf, size_t count) \
796{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800797 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700798 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700799 \
800 return wacom_luminance_store(wacom, &wacom->led.field, \
801 buf, count); \
802} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700803static ssize_t wacom_##name##_luminance_show(struct device *dev, \
804 struct device_attribute *attr, char *buf) \
805{ \
806 struct wacom *wacom = dev_get_drvdata(dev); \
807 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
808} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700809static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700810 wacom_##name##_luminance_show, \
811 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700812
813DEVICE_LUMINANCE_ATTR(status0, llv);
814DEVICE_LUMINANCE_ATTR(status1, hlv);
815DEVICE_LUMINANCE_ATTR(buttons, img_lum);
816
817static ssize_t wacom_button_image_store(struct device *dev, int button_id,
818 const char *buf, size_t count)
819{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800820 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700821 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700822 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700823 unsigned len;
824 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700825
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700826 if (hdev->bus == BUS_BLUETOOTH) {
827 len = 256;
828 xfer_id = WAC_CMD_ICON_BT_XFER;
829 } else {
830 len = 1024;
831 xfer_id = WAC_CMD_ICON_XFER;
832 }
833
834 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700835 return -EINVAL;
836
837 mutex_lock(&wacom->lock);
838
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700839 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700840
841 mutex_unlock(&wacom->lock);
842
843 return err < 0 ? err : count;
844}
845
846#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
847static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
848 struct device_attribute *attr, const char *buf, size_t count) \
849{ \
850 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
851} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700852static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700853 NULL, wacom_btnimg##BUTTON_ID##_store)
854
855DEVICE_BTNIMG_ATTR(0);
856DEVICE_BTNIMG_ATTR(1);
857DEVICE_BTNIMG_ATTR(2);
858DEVICE_BTNIMG_ATTR(3);
859DEVICE_BTNIMG_ATTR(4);
860DEVICE_BTNIMG_ATTR(5);
861DEVICE_BTNIMG_ATTR(6);
862DEVICE_BTNIMG_ATTR(7);
863
Ping Cheng09e7d942011-10-04 23:51:14 -0700864static struct attribute *cintiq_led_attrs[] = {
865 &dev_attr_status_led0_select.attr,
866 &dev_attr_status_led1_select.attr,
867 NULL
868};
869
870static struct attribute_group cintiq_led_attr_group = {
871 .name = "wacom_led",
872 .attrs = cintiq_led_attrs,
873};
874
875static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700876 &dev_attr_status0_luminance.attr,
877 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700878 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700879 &dev_attr_buttons_luminance.attr,
880 &dev_attr_button0_rawimg.attr,
881 &dev_attr_button1_rawimg.attr,
882 &dev_attr_button2_rawimg.attr,
883 &dev_attr_button3_rawimg.attr,
884 &dev_attr_button4_rawimg.attr,
885 &dev_attr_button5_rawimg.attr,
886 &dev_attr_button6_rawimg.attr,
887 &dev_attr_button7_rawimg.attr,
888 NULL
889};
890
Ping Cheng09e7d942011-10-04 23:51:14 -0700891static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700892 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700893 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700894};
895
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700896static struct attribute *intuos5_led_attrs[] = {
897 &dev_attr_status0_luminance.attr,
898 &dev_attr_status_led0_select.attr,
899 NULL
900};
901
902static struct attribute_group intuos5_led_attr_group = {
903 .name = "wacom_led",
904 .attrs = intuos5_led_attrs,
905};
906
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700907static int wacom_initialize_leds(struct wacom *wacom)
908{
909 int error;
910
Jason Gerecke862cf552015-06-15 18:01:43 -0700911 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
912 return 0;
913
Ping Cheng09e7d942011-10-04 23:51:14 -0700914 /* Initialize default values */
915 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700916 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700917 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700918 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700919 case INTUOS4L:
920 wacom->led.select[0] = 0;
921 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700922 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700923 wacom->led.hlv = 20;
924 wacom->led.img_lum = 10;
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700925 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700926 &intuos4_led_attr_group);
927 break;
928
Jason Gerecke246835f2011-12-12 00:12:04 -0800929 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700930 case WACOM_21UX2:
931 wacom->led.select[0] = 0;
932 wacom->led.select[1] = 0;
933 wacom->led.llv = 0;
934 wacom->led.hlv = 0;
935 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700936
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700937 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700938 &cintiq_led_attr_group);
939 break;
940
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700941 case INTUOS5S:
942 case INTUOS5:
943 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700944 case INTUOSPS:
945 case INTUOSPM:
946 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -0700947 wacom->led.select[0] = 0;
948 wacom->led.select[1] = 0;
949 wacom->led.llv = 32;
950 wacom->led.hlv = 0;
951 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700952
Jason Gerecke862cf552015-06-15 18:01:43 -0700953 error = sysfs_create_group(&wacom->hdev->dev.kobj,
954 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700955 break;
956
Ping Cheng09e7d942011-10-04 23:51:14 -0700957 default:
958 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700959 }
960
Ping Cheng09e7d942011-10-04 23:51:14 -0700961 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700962 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -0700963 "cannot create sysfs group err: %d\n", error);
964 return error;
965 }
966 wacom_led_control(wacom);
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700967 wacom->led_initialized = true;
Ping Cheng09e7d942011-10-04 23:51:14 -0700968
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700969 return 0;
970}
971
972static void wacom_destroy_leds(struct wacom *wacom)
973{
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700974 if (!wacom->led_initialized)
975 return;
976
Jason Gerecke862cf552015-06-15 18:01:43 -0700977 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
978 return;
979
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700980 wacom->led_initialized = false;
981
Ping Cheng09e7d942011-10-04 23:51:14 -0700982 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700983 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700984 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700985 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700986 case INTUOS4L:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700987 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700988 &intuos4_led_attr_group);
989 break;
990
Jason Gerecke246835f2011-12-12 00:12:04 -0800991 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700992 case WACOM_21UX2:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700993 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700994 &cintiq_led_attr_group);
995 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700996
997 case INTUOS5S:
998 case INTUOS5:
999 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001000 case INTUOSPS:
1001 case INTUOSPM:
1002 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -07001003 sysfs_remove_group(&wacom->hdev->dev.kobj,
1004 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001005 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001006 }
1007}
1008
Chris Bagwella1d552c2012-03-25 23:26:30 -07001009static enum power_supply_property wacom_battery_props[] = {
Jason Gerecke71fa6412015-03-11 10:25:41 -07001010 POWER_SUPPLY_PROP_PRESENT,
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001011 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001012 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001013 POWER_SUPPLY_PROP_CAPACITY
1014};
1015
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001016static enum power_supply_property wacom_ac_props[] = {
1017 POWER_SUPPLY_PROP_PRESENT,
1018 POWER_SUPPLY_PROP_ONLINE,
1019 POWER_SUPPLY_PROP_SCOPE,
1020};
1021
Chris Bagwella1d552c2012-03-25 23:26:30 -07001022static int wacom_battery_get_property(struct power_supply *psy,
1023 enum power_supply_property psp,
1024 union power_supply_propval *val)
1025{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001026 struct wacom *wacom = power_supply_get_drvdata(psy);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001027 int ret = 0;
1028
1029 switch (psp) {
Jason Gerecke71fa6412015-03-11 10:25:41 -07001030 case POWER_SUPPLY_PROP_PRESENT:
1031 val->intval = wacom->wacom_wac.bat_connected;
1032 break;
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001033 case POWER_SUPPLY_PROP_SCOPE:
1034 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1035 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001036 case POWER_SUPPLY_PROP_CAPACITY:
1037 val->intval =
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001038 wacom->wacom_wac.battery_capacity;
1039 break;
1040 case POWER_SUPPLY_PROP_STATUS:
1041 if (wacom->wacom_wac.bat_charging)
1042 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1043 else if (wacom->wacom_wac.battery_capacity == 100 &&
1044 wacom->wacom_wac.ps_connected)
1045 val->intval = POWER_SUPPLY_STATUS_FULL;
Jason Gereckeb0882cb2015-03-06 11:47:43 -08001046 else if (wacom->wacom_wac.ps_connected)
1047 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001048 else
1049 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001050 break;
1051 default:
1052 ret = -EINVAL;
1053 break;
1054 }
1055
1056 return ret;
1057}
1058
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001059static int wacom_ac_get_property(struct power_supply *psy,
1060 enum power_supply_property psp,
1061 union power_supply_propval *val)
1062{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001063 struct wacom *wacom = power_supply_get_drvdata(psy);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001064 int ret = 0;
1065
1066 switch (psp) {
1067 case POWER_SUPPLY_PROP_PRESENT:
1068 /* fall through */
1069 case POWER_SUPPLY_PROP_ONLINE:
1070 val->intval = wacom->wacom_wac.ps_connected;
1071 break;
1072 case POWER_SUPPLY_PROP_SCOPE:
1073 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1074 break;
1075 default:
1076 ret = -EINVAL;
1077 break;
1078 }
1079 return ret;
1080}
1081
Chris Bagwella1d552c2012-03-25 23:26:30 -07001082static int wacom_initialize_battery(struct wacom *wacom)
1083{
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001084 static atomic_t battery_no = ATOMIC_INIT(0);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001085 struct power_supply_config psy_cfg = { .drv_data = wacom, };
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001086 unsigned long n;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001087
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001088 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001089 struct power_supply_desc *bat_desc = &wacom->battery_desc;
1090 struct power_supply_desc *ac_desc = &wacom->ac_desc;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001091 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001092
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001093 bat_desc->properties = wacom_battery_props;
1094 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1095 bat_desc->get_property = wacom_battery_get_property;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001096 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001097 bat_desc->name = wacom->wacom_wac.bat_name;
1098 bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1099 bat_desc->use_for_apm = 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001100
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001101 ac_desc->properties = wacom_ac_props;
1102 ac_desc->num_properties = ARRAY_SIZE(wacom_ac_props);
1103 ac_desc->get_property = wacom_ac_get_property;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001104 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001105 ac_desc->name = wacom->wacom_wac.ac_name;
1106 ac_desc->type = POWER_SUPPLY_TYPE_MAINS;
1107 ac_desc->use_for_apm = 0;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001108
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001109 wacom->battery = power_supply_register(&wacom->hdev->dev,
1110 &wacom->battery_desc, &psy_cfg);
1111 if (IS_ERR(wacom->battery))
1112 return PTR_ERR(wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001113
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001114 power_supply_powers(wacom->battery, &wacom->hdev->dev);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001115
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001116 wacom->ac = power_supply_register(&wacom->hdev->dev,
1117 &wacom->ac_desc,
1118 &psy_cfg);
1119 if (IS_ERR(wacom->ac)) {
1120 power_supply_unregister(wacom->battery);
1121 return PTR_ERR(wacom->ac);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001122 }
1123
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001124 power_supply_powers(wacom->ac, &wacom->hdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001125 }
1126
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001127 return 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001128}
1129
1130static void wacom_destroy_battery(struct wacom *wacom)
1131{
Linus Torvalds8de29a32015-04-14 08:25:26 -08001132 if (wacom->battery) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001133 power_supply_unregister(wacom->battery);
1134 wacom->battery = NULL;
1135 power_supply_unregister(wacom->ac);
1136 wacom->ac = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001137 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001138}
1139
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001140static ssize_t wacom_show_speed(struct device *dev,
1141 struct device_attribute
1142 *attr, char *buf)
1143{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001144 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001145 struct wacom *wacom = hid_get_drvdata(hdev);
1146
1147 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1148}
1149
1150static ssize_t wacom_store_speed(struct device *dev,
1151 struct device_attribute *attr,
1152 const char *buf, size_t count)
1153{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001154 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001155 struct wacom *wacom = hid_get_drvdata(hdev);
1156 u8 new_speed;
1157
1158 if (kstrtou8(buf, 0, &new_speed))
1159 return -EINVAL;
1160
1161 if (new_speed != 0 && new_speed != 1)
1162 return -EINVAL;
1163
1164 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1165
1166 return count;
1167}
1168
Ping Chenge0984bc2014-09-10 12:40:05 -07001169static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001170 wacom_show_speed, wacom_store_speed);
1171
Aaron Skomra72b236d2015-08-20 16:05:17 -07001172
1173static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1174 struct kobj_attribute *kattr,
1175 char *buf, int index)
1176{
Geliang Tang2cf83832015-12-27 17:25:24 +08001177 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001178 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001179 struct wacom *wacom = hid_get_drvdata(hdev);
1180 u8 mode;
1181
1182 mode = wacom->led.select[index];
1183 if (mode >= 0 && mode < 3)
1184 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
1185 else
1186 return snprintf(buf, PAGE_SIZE, "%d\n", -1);
1187}
1188
1189#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1190static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1191 struct kobj_attribute *kattr, char *buf) \
1192{ \
1193 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1194} \
1195static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1196 .attr = {.name = "remote_mode", \
1197 .mode = DEV_ATTR_RO_PERM}, \
1198 .show = wacom_show_remote##SET_ID##_mode, \
1199}; \
1200static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1201 &remote##SET_ID##_mode_attr.attr, \
1202 NULL \
1203}; \
1204static struct attribute_group remote##SET_ID##_serial_group = { \
1205 .name = NULL, \
1206 .attrs = remote##SET_ID##_serial_attrs, \
1207}
1208
1209DEVICE_EKR_ATTR_GROUP(0);
1210DEVICE_EKR_ATTR_GROUP(1);
1211DEVICE_EKR_ATTR_GROUP(2);
1212DEVICE_EKR_ATTR_GROUP(3);
1213DEVICE_EKR_ATTR_GROUP(4);
1214
1215int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial, int index)
1216{
1217 int error = 0;
1218 char *buf;
1219 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1220
1221 wacom_wac->serial[index] = serial;
1222
1223 buf = kzalloc(WAC_REMOTE_SERIAL_MAX_STRLEN, GFP_KERNEL);
1224 if (!buf)
1225 return -ENOMEM;
1226 snprintf(buf, WAC_REMOTE_SERIAL_MAX_STRLEN, "%d", serial);
1227 wacom->remote_group[index].name = buf;
1228
1229 error = sysfs_create_group(wacom->remote_dir,
1230 &wacom->remote_group[index]);
1231 if (error) {
1232 hid_err(wacom->hdev,
1233 "cannot create sysfs group err: %d\n", error);
1234 kobject_put(wacom->remote_dir);
1235 return error;
1236 }
1237
1238 return 0;
1239}
1240
1241void wacom_remote_destroy_attr_group(struct wacom *wacom, __u32 serial)
1242{
1243 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1244 int i;
1245
1246 if (!serial)
1247 return;
1248
1249 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1250 if (wacom_wac->serial[i] == serial) {
1251 wacom_wac->serial[i] = 0;
1252 wacom->led.select[i] = WACOM_STATUS_UNKNOWN;
1253 if (wacom->remote_group[i].name) {
1254 sysfs_remove_group(wacom->remote_dir,
1255 &wacom->remote_group[i]);
1256 kfree(wacom->remote_group[i].name);
1257 wacom->remote_group[i].name = NULL;
1258 }
1259 }
1260 }
1261}
1262
1263static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1264{
1265 const size_t buf_size = 2;
1266 unsigned char *buf;
1267 int retval;
1268
1269 buf = kzalloc(buf_size, GFP_KERNEL);
1270 if (!buf)
1271 return -ENOMEM;
1272
1273 buf[0] = WAC_CMD_DELETE_PAIRING;
1274 buf[1] = selector;
1275
1276 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1277 buf_size, WAC_CMD_RETRIES);
1278 kfree(buf);
1279
1280 return retval;
1281}
1282
1283static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1284 struct kobj_attribute *attr,
1285 const char *buf, size_t count)
1286{
1287 unsigned char selector = 0;
Geliang Tang2cf83832015-12-27 17:25:24 +08001288 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001289 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001290 struct wacom *wacom = hid_get_drvdata(hdev);
1291 int err;
1292
1293 if (!strncmp(buf, "*\n", 2)) {
1294 selector = WAC_CMD_UNPAIR_ALL;
1295 } else {
1296 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1297 buf);
1298 return -1;
1299 }
1300
1301 mutex_lock(&wacom->lock);
1302
1303 err = wacom_cmd_unpair_remote(wacom, selector);
1304 mutex_unlock(&wacom->lock);
1305
1306 return err < 0 ? err : count;
1307}
1308
1309static struct kobj_attribute unpair_remote_attr = {
1310 .attr = {.name = "unpair_remote", .mode = 0200},
1311 .store = wacom_store_unpair_remote,
1312};
1313
1314static const struct attribute *remote_unpair_attrs[] = {
1315 &unpair_remote_attr.attr,
1316 NULL
1317};
1318
1319static int wacom_initialize_remote(struct wacom *wacom)
1320{
1321 int error = 0;
1322 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1323 int i;
1324
1325 if (wacom->wacom_wac.features.type != REMOTE)
1326 return 0;
1327
1328 wacom->remote_group[0] = remote0_serial_group;
1329 wacom->remote_group[1] = remote1_serial_group;
1330 wacom->remote_group[2] = remote2_serial_group;
1331 wacom->remote_group[3] = remote3_serial_group;
1332 wacom->remote_group[4] = remote4_serial_group;
1333
1334 wacom->remote_dir = kobject_create_and_add("wacom_remote",
1335 &wacom->hdev->dev.kobj);
1336 if (!wacom->remote_dir)
1337 return -ENOMEM;
1338
1339 error = sysfs_create_files(wacom->remote_dir, remote_unpair_attrs);
1340
1341 if (error) {
1342 hid_err(wacom->hdev,
1343 "cannot create sysfs group err: %d\n", error);
1344 return error;
1345 }
1346
1347 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1348 wacom->led.select[i] = WACOM_STATUS_UNKNOWN;
1349 wacom_wac->serial[i] = 0;
1350 }
1351
1352 return 0;
1353}
1354
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001355static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001356{
1357 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001358 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001359 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001360
1361 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001362 if (!input_dev)
1363 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001364
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001365 input_dev->name = wacom_wac->features.name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001366 input_dev->phys = hdev->phys;
1367 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001368 input_dev->open = wacom_open;
1369 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001370 input_dev->uniq = hdev->uniq;
1371 input_dev->id.bustype = hdev->bus;
1372 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001373 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001374 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001375 input_set_drvdata(input_dev, wacom);
1376
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001377 return input_dev;
1378}
1379
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001380static void wacom_clean_inputs(struct wacom *wacom)
1381{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001382 if (wacom->wacom_wac.pen_input) {
1383 if (wacom->wacom_wac.pen_registered)
1384 input_unregister_device(wacom->wacom_wac.pen_input);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001385 else
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001386 input_free_device(wacom->wacom_wac.pen_input);
1387 }
1388 if (wacom->wacom_wac.touch_input) {
1389 if (wacom->wacom_wac.touch_registered)
1390 input_unregister_device(wacom->wacom_wac.touch_input);
1391 else
1392 input_free_device(wacom->wacom_wac.touch_input);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001393 }
1394 if (wacom->wacom_wac.pad_input) {
Ping Cheng954df6a2014-11-20 16:31:12 -08001395 if (wacom->wacom_wac.pad_registered)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001396 input_unregister_device(wacom->wacom_wac.pad_input);
1397 else
1398 input_free_device(wacom->wacom_wac.pad_input);
1399 }
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001400 wacom->wacom_wac.pen_input = NULL;
1401 wacom->wacom_wac.touch_input = NULL;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001402 wacom->wacom_wac.pad_input = NULL;
Benjamin Tissoires17f28472016-02-12 17:27:44 +01001403 wacom->wacom_wac.pen_registered = false;
1404 wacom->wacom_wac.touch_registered = false;
1405 wacom->wacom_wac.pad_registered = false;
Ping Cheng912ca212014-09-10 12:41:31 -07001406 wacom_destroy_leds(wacom);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001407}
1408
Jason Gerecked9f2d202015-07-13 18:03:44 -07001409static int wacom_allocate_inputs(struct wacom *wacom)
1410{
1411 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1412
1413 wacom_wac->pen_input = wacom_allocate_input(wacom);
1414 wacom_wac->touch_input = wacom_allocate_input(wacom);
1415 wacom_wac->pad_input = wacom_allocate_input(wacom);
1416 if (!wacom_wac->pen_input || !wacom_wac->touch_input || !wacom_wac->pad_input) {
1417 wacom_clean_inputs(wacom);
1418 return -ENOMEM;
1419 }
1420
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001421 wacom_wac->pen_input->name = wacom_wac->pen_name;
Jason Gerecked9f2d202015-07-13 18:03:44 -07001422 wacom_wac->touch_input->name = wacom_wac->touch_name;
1423 wacom_wac->pad_input->name = wacom_wac->pad_name;
1424
1425 return 0;
1426}
1427
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001428static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001429{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001430 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001431 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Jason Gerecke2636a3f2015-06-15 18:01:44 -07001432 int error = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001433
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001434 pen_input_dev = wacom_wac->pen_input;
1435 touch_input_dev = wacom_wac->touch_input;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001436 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001437
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001438 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001439 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001440
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001441 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1442 if (error) {
1443 /* no pen in use on this interface */
1444 input_free_device(pen_input_dev);
1445 wacom_wac->pen_input = NULL;
1446 pen_input_dev = NULL;
1447 } else {
1448 error = input_register_device(pen_input_dev);
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001449 if (error)
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001450 goto fail_register_pen_input;
1451 wacom_wac->pen_registered = true;
1452 }
1453
1454 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1455 if (error) {
1456 /* no touch in use on this interface */
1457 input_free_device(touch_input_dev);
1458 wacom_wac->touch_input = NULL;
1459 touch_input_dev = NULL;
1460 } else {
1461 error = input_register_device(touch_input_dev);
1462 if (error)
1463 goto fail_register_touch_input;
1464 wacom_wac->touch_registered = true;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001465 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001466
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001467 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1468 if (error) {
1469 /* no pad in use on this interface */
1470 input_free_device(pad_input_dev);
1471 wacom_wac->pad_input = NULL;
1472 pad_input_dev = NULL;
1473 } else {
1474 error = input_register_device(pad_input_dev);
1475 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001476 goto fail_register_pad_input;
Ping Cheng954df6a2014-11-20 16:31:12 -08001477 wacom_wac->pad_registered = true;
Ping Cheng912ca212014-09-10 12:41:31 -07001478
1479 error = wacom_initialize_leds(wacom);
1480 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001481 goto fail_leds;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001482 }
1483
Ping Cheng19635182012-04-29 21:09:18 -07001484 return 0;
1485
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001486fail_leds:
Ping Cheng912ca212014-09-10 12:41:31 -07001487 input_unregister_device(pad_input_dev);
1488 pad_input_dev = NULL;
Ping Cheng954df6a2014-11-20 16:31:12 -08001489 wacom_wac->pad_registered = false;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001490fail_register_pad_input:
Dan Carpenter0fd72ff2015-06-24 17:27:43 +03001491 if (touch_input_dev)
1492 input_unregister_device(touch_input_dev);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001493 wacom_wac->touch_input = NULL;
1494 wacom_wac->touch_registered = false;
1495fail_register_touch_input:
Dan Carpenter0fd72ff2015-06-24 17:27:43 +03001496 if (pen_input_dev)
1497 input_unregister_device(pen_input_dev);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001498 wacom_wac->pen_input = NULL;
1499 wacom_wac->pen_registered = false;
1500fail_register_pen_input:
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001501 return error;
1502}
1503
Jason Gerecke0be01712015-08-05 15:44:53 -07001504/*
1505 * Not all devices report physical dimensions from HID.
1506 * Compute the default from hardcoded logical dimension
1507 * and resolution before driver overwrites them.
1508 */
1509static void wacom_set_default_phy(struct wacom_features *features)
1510{
1511 if (features->x_resolution) {
1512 features->x_phy = (features->x_max * 100) /
1513 features->x_resolution;
1514 features->y_phy = (features->y_max * 100) /
1515 features->y_resolution;
1516 }
1517}
1518
1519static void wacom_calculate_res(struct wacom_features *features)
1520{
1521 /* set unit to "100th of a mm" for devices not reported by HID */
1522 if (!features->unit) {
1523 features->unit = 0x11;
1524 features->unitExpo = -3;
1525 }
1526
1527 features->x_resolution = wacom_calc_hid_res(features->x_max,
1528 features->x_phy,
1529 features->unit,
1530 features->unitExpo);
1531 features->y_resolution = wacom_calc_hid_res(features->y_max,
1532 features->y_phy,
1533 features->unit,
1534 features->unitExpo);
1535}
1536
Jason Gereckefce99572015-03-06 11:47:40 -08001537void wacom_battery_work(struct work_struct *work)
1538{
1539 struct wacom *wacom = container_of(work, struct wacom, work);
1540
1541 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Linus Torvalds8de29a32015-04-14 08:25:26 -08001542 !wacom->battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001543 wacom_initialize_battery(wacom);
1544 }
1545 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Linus Torvalds8de29a32015-04-14 08:25:26 -08001546 wacom->battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001547 wacom_destroy_battery(wacom);
1548 }
1549}
1550
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001551static size_t wacom_compute_pktlen(struct hid_device *hdev)
1552{
1553 struct hid_report_enum *report_enum;
1554 struct hid_report *report;
1555 size_t size = 0;
1556
1557 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1558
1559 list_for_each_entry(report, &report_enum->report_list, list) {
Mathieu Magnaudetdabb05c62014-11-27 16:02:36 +01001560 size_t report_size = hid_report_len(report);
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001561 if (report_size > size)
1562 size = report_size;
1563 }
1564
1565 return size;
1566}
1567
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001568static void wacom_update_name(struct wacom *wacom, const char *suffix)
Ping Chengc24eab42015-04-24 15:32:51 -07001569{
1570 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1571 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke44b52502015-06-15 18:01:41 -07001572 char name[WACOM_NAME_MAX];
Ping Chengc24eab42015-04-24 15:32:51 -07001573
1574 /* Generic devices name unspecified */
1575 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1576 if (strstr(wacom->hdev->name, "Wacom") ||
1577 strstr(wacom->hdev->name, "wacom") ||
1578 strstr(wacom->hdev->name, "WACOM")) {
1579 /* name is in HID descriptor, use it */
Jason Gerecke44b52502015-06-15 18:01:41 -07001580 strlcpy(name, wacom->hdev->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001581
1582 /* strip out excess whitespaces */
1583 while (1) {
Jason Gerecke44b52502015-06-15 18:01:41 -07001584 char *gap = strstr(name, " ");
Ping Chengc24eab42015-04-24 15:32:51 -07001585 if (gap == NULL)
1586 break;
1587 /* shift everything including the terminator */
1588 memmove(gap, gap+1, strlen(gap));
1589 }
1590 /* get rid of trailing whitespace */
Jason Gerecke44b52502015-06-15 18:01:41 -07001591 if (name[strlen(name)-1] == ' ')
1592 name[strlen(name)-1] = '\0';
Ping Chengc24eab42015-04-24 15:32:51 -07001593 } else {
1594 /* no meaningful name retrieved. use product ID */
Jason Gerecke44b52502015-06-15 18:01:41 -07001595 snprintf(name, sizeof(name),
Ping Chengc24eab42015-04-24 15:32:51 -07001596 "%s %X", features->name, wacom->hdev->product);
1597 }
1598 } else {
Jason Gerecke44b52502015-06-15 18:01:41 -07001599 strlcpy(name, features->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001600 }
1601
1602 /* Append the device type to the name */
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001603 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001604 "%s%s Pen", name, suffix);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001605 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001606 "%s%s Finger", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07001607 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001608 "%s%s Pad", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07001609}
1610
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001611static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
Ping Cheng3bea7332006-07-13 18:01:36 -07001612{
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001613 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1614 struct wacom_features *features = &wacom_wac->features;
1615 struct hid_device *hdev = wacom->hdev;
Jason Childse33da8a2010-02-17 22:38:31 -08001616 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001617 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07001618
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001619 features->pktlen = wacom_compute_pktlen(hdev);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001620 if (features->pktlen > WACOM_PKGLEN_MAX)
1621 return -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001622
Jason Gerecke3f14a632015-08-03 10:17:05 -07001623 error = wacom_allocate_inputs(wacom);
1624 if (error)
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001625 return error;
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001626
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05001627 /*
1628 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
1629 * into debug mode for the touch part.
1630 * We ignore the other interfaces.
1631 */
1632 if (features->type == BAMBOO_PAD) {
1633 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
1634 features->type = HID_GENERIC;
1635 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
1636 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
1637 error = -ENODEV;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001638 goto fail_allocate_inputs;
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05001639 }
1640 }
1641
Ping Cheng401d7d12013-07-28 00:38:54 -07001642 /* set the default size in case we do not get them from hid */
1643 wacom_set_default_phy(features);
1644
Ping Chengf393ee22012-04-29 21:09:17 -07001645 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001646 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng42f4f272015-04-15 16:53:54 -07001647 wacom_setup_device_quirks(wacom);
Jason Gerecke042628a2015-04-30 17:51:54 -07001648
Jason Gereckeaa86b182015-06-15 18:01:42 -07001649 if (features->device_type == WACOM_DEVICETYPE_NONE &&
1650 features->type != WIRELESS) {
Jason Gerecke8e116d32015-04-30 17:51:55 -07001651 error = features->type == HID_GENERIC ? -ENODEV : 0;
1652
Jason Gerecke042628a2015-04-30 17:51:54 -07001653 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
Jason Gerecke8e116d32015-04-30 17:51:55 -07001654 hdev->name,
1655 error ? "Ignoring" : "Assuming pen");
1656
1657 if (error)
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001658 goto fail_parsed;
Jason Gerecke042628a2015-04-30 17:51:54 -07001659
Jason Gereckeaa86b182015-06-15 18:01:42 -07001660 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -07001661 }
1662
Ping Cheng401d7d12013-07-28 00:38:54 -07001663 wacom_calculate_res(features);
1664
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001665 wacom_update_name(wacom, wireless ? " (WL)" : "");
Ping Cheng4492eff2010-03-19 22:18:15 -07001666
Ping Chengf3586d22015-03-20 14:57:00 -07001667 error = wacom_add_shared_data(hdev);
1668 if (error)
1669 goto fail_shared_data;
Ping Cheng49b764a2010-02-20 00:53:49 -08001670
Jason Gereckeccad85c2015-08-03 10:17:04 -07001671 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001672 (features->quirks & WACOM_QUIRK_BATTERY)) {
1673 error = wacom_initialize_battery(wacom);
1674 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001675 goto fail_battery;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001676 }
1677
Jason Gerecke3f14a632015-08-03 10:17:05 -07001678 error = wacom_register_inputs(wacom);
1679 if (error)
1680 goto fail_register_inputs;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001681
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001682 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
1683 error = wacom_initialize_remote(wacom);
1684 if (error)
1685 goto fail_remote;
1686 }
1687
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001688 if (features->type == HID_GENERIC)
1689 connect_mask |= HID_CONNECT_DRIVER;
1690
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001691 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001692 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001693 if (error) {
1694 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001695 goto fail_hw_start;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001696 }
1697
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001698 if (!wireless) {
1699 /* Note that if query fails it is not a hard failure */
1700 wacom_query_tablet_data(hdev, features);
1701 }
Jason Gerecke86e88f02015-11-03 16:57:58 -08001702
1703 /* touch only Bamboo doesn't support pen */
1704 if ((features->type == BAMBOO_TOUCH) &&
1705 (features->device_type & WACOM_DEVICETYPE_PEN)) {
1706 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001707 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08001708 }
1709
1710 /* pen only Bamboo neither support touch nor pad */
1711 if ((features->type == BAMBOO_PEN) &&
1712 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
1713 (features->device_type & WACOM_DEVICETYPE_PAD))) {
1714 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001715 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08001716 }
1717
Jason Gereckeccad85c2015-08-03 10:17:04 -07001718 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001719 error = hid_hw_open(hdev);
1720
Ping Chengeda01da2015-09-23 13:51:15 -07001721 if ((wacom_wac->features.type == INTUOSHT ||
Benjamin Tissoires97f9afa2016-07-13 18:05:49 +02001722 wacom_wac->features.type == INTUOSHT2) &&
Ping Chengeda01da2015-09-23 13:51:15 -07001723 (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
Benjamin Tissoires97f9afa2016-07-13 18:05:49 +02001724 wacom_wac->shared->type = wacom_wac->features.type;
1725 wacom_wac->shared->touch_input = wacom_wac->touch_input;
Ping Cheng961794a2013-12-05 12:54:53 -08001726 }
1727
Ping Cheng3bea7332006-07-13 18:01:36 -07001728 return 0;
1729
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001730fail_quirks:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001731 hid_hw_stop(hdev);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001732fail_hw_start:
1733 kobject_put(wacom->remote_dir);
1734fail_remote:
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001735 wacom_clean_inputs(wacom);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001736fail_register_inputs:
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001737 wacom_destroy_battery(wacom);
1738fail_battery:
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -05001739 wacom_remove_shared_data(wacom);
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001740fail_shared_data:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001741fail_parsed:
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001742fail_allocate_inputs:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001743 wacom_clean_inputs(wacom);
1744 return error;
1745}
1746
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001747static void wacom_wireless_work(struct work_struct *work)
1748{
1749 struct wacom *wacom = container_of(work, struct wacom, work);
1750 struct usb_device *usbdev = wacom->usbdev;
1751 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1752 struct hid_device *hdev1, *hdev2;
1753 struct wacom *wacom1, *wacom2;
1754 struct wacom_wac *wacom_wac1, *wacom_wac2;
1755 int error;
1756
1757 /*
1758 * Regardless if this is a disconnect or a new tablet,
1759 * remove any existing input and battery devices.
1760 */
1761
1762 wacom_destroy_battery(wacom);
1763
1764 /* Stylus interface */
1765 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1766 wacom1 = hid_get_drvdata(hdev1);
1767 wacom_wac1 = &(wacom1->wacom_wac);
1768 wacom_clean_inputs(wacom1);
1769
1770 /* Touch interface */
1771 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1772 wacom2 = hid_get_drvdata(hdev2);
1773 wacom_wac2 = &(wacom2->wacom_wac);
1774 wacom_clean_inputs(wacom2);
1775
1776 if (wacom_wac->pid == 0) {
1777 hid_info(wacom->hdev, "wireless tablet disconnected\n");
1778 wacom_wac1->shared->type = 0;
1779 } else {
1780 const struct hid_device_id *id = wacom_ids;
1781
1782 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1783 wacom_wac->pid);
1784
1785 while (id->bus) {
1786 if (id->vendor == USB_VENDOR_ID_WACOM &&
1787 id->product == wacom_wac->pid)
1788 break;
1789 id++;
1790 }
1791
1792 if (!id->bus) {
1793 hid_info(wacom->hdev, "ignoring unknown PID.\n");
1794 return;
1795 }
1796
1797 /* Stylus interface */
1798 wacom_wac1->features =
1799 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001800
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001801 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001802 hid_hw_stop(hdev1);
1803 error = wacom_parse_and_register(wacom1, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001804 if (error)
1805 goto fail;
1806
1807 /* Touch interface */
1808 if (wacom_wac1->features.touch_max ||
1809 (wacom_wac1->features.type >= INTUOSHT &&
1810 wacom_wac1->features.type <= BAMBOO_PT)) {
1811 wacom_wac2->features =
1812 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001813 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001814 hid_hw_stop(hdev2);
1815 error = wacom_parse_and_register(wacom2, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001816 if (error)
1817 goto fail;
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001818 }
1819
1820 error = wacom_initialize_battery(wacom);
1821 if (error)
1822 goto fail;
1823 }
1824
1825 return;
1826
1827fail:
1828 wacom_clean_inputs(wacom1);
1829 wacom_clean_inputs(wacom2);
1830 return;
1831}
1832
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001833static int wacom_probe(struct hid_device *hdev,
1834 const struct hid_device_id *id)
1835{
1836 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1837 struct usb_device *dev = interface_to_usbdev(intf);
1838 struct wacom *wacom;
1839 struct wacom_wac *wacom_wac;
1840 struct wacom_features *features;
1841 int error;
1842
1843 if (!id->driver_data)
1844 return -EINVAL;
1845
1846 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1847
1848 /* hid-core sets this quirk for the boot interface */
1849 hdev->quirks &= ~HID_QUIRK_NOGET;
1850
1851 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1852 if (!wacom)
1853 return -ENOMEM;
1854
1855 hid_set_drvdata(hdev, wacom);
1856 wacom->hdev = hdev;
1857
1858 wacom_wac = &wacom->wacom_wac;
1859 wacom_wac->features = *((struct wacom_features *)id->driver_data);
1860 features = &wacom_wac->features;
1861
1862 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1863 error = -ENODEV;
1864 goto fail_type;
1865 }
1866
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07001867 wacom_wac->hid_data.inputmode = -1;
Jason Gerecke326ea2a2016-04-04 11:26:52 -07001868 wacom_wac->mode_report = -1;
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07001869
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001870 wacom->usbdev = dev;
1871 wacom->intf = intf;
1872 mutex_init(&wacom->lock);
1873 INIT_WORK(&wacom->work, wacom_wireless_work);
1874
1875 /* ask for the report descriptor to be loaded by HID */
1876 error = hid_parse(hdev);
1877 if (error) {
1878 hid_err(hdev, "parse failed\n");
1879 goto fail_parse;
1880 }
1881
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001882 error = wacom_parse_and_register(wacom, false);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001883 if (error)
1884 goto fail_parse;
1885
1886 if (hdev->bus == BUS_BLUETOOTH) {
1887 error = device_create_file(&hdev->dev, &dev_attr_speed);
1888 if (error)
1889 hid_warn(hdev,
1890 "can't create sysfs speed attribute err: %d\n",
1891 error);
1892 }
1893
1894 return 0;
1895
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001896fail_type:
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001897fail_parse:
1898 kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001899 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001900 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001901}
1902
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001903static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001904{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001905 struct wacom *wacom = hid_get_drvdata(hdev);
Benjamin Tissoiresf6205162016-02-12 17:27:45 +01001906 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1907 struct wacom_features *features = &wacom_wac->features;
1908
1909 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
1910 hid_hw_close(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001911
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001912 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001913
Chris Bagwell16bf2882012-03-25 23:26:20 -07001914 cancel_work_sync(&wacom->work);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001915 kobject_put(wacom->remote_dir);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001916 wacom_clean_inputs(wacom);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001917 if (hdev->bus == BUS_BLUETOOTH)
1918 device_remove_file(&hdev->dev, &dev_attr_speed);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001919 wacom_destroy_battery(wacom);
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -05001920 wacom_remove_shared_data(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001921
1922 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001923 kfree(wacom);
1924}
1925
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001926#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001927static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001928{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001929 struct wacom *wacom = hid_get_drvdata(hdev);
1930 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001931
1932 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001933
1934 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001935 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001936 wacom_led_control(wacom);
1937
Oliver Neukume7224092008-04-15 01:31:57 -04001938 mutex_unlock(&wacom->lock);
1939
1940 return 0;
1941}
1942
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001943static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001944{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001945 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001946}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001947#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04001948
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001949static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001950 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001951 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001952 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001953 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001954 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001955#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001956 .resume = wacom_resume,
1957 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001958#endif
1959 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001960};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001961module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07001962
1963MODULE_VERSION(DRIVER_VERSION);
1964MODULE_AUTHOR(DRIVER_AUTHOR);
1965MODULE_DESCRIPTION(DRIVER_DESC);
1966MODULE_LICENSE(DRIVER_LICENSE);