blob: da2fccf5fa15321974d8fd217678cc1305d40a7a [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 Tissoires3dad1882016-07-13 18:05:54 +020094 /*
95 * wacom->hdev should never be null, but surprisingly, I had the case
96 * once while unplugging the Wacom Wireless Receiver.
97 */
98 if (wacom->hdev)
99 hid_hw_close(wacom->hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700100}
101
Chris Bagwell16bf2882012-03-25 23:26:20 -0700102/*
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700103 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
Jason Gerecke115d5e12012-10-03 17:24:32 -0700104 */
105static int wacom_calc_hid_res(int logical_extents, int physical_extents,
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700106 unsigned unit, int exponent)
Jason Gerecke115d5e12012-10-03 17:24:32 -0700107{
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700108 struct hid_field field = {
109 .logical_maximum = logical_extents,
110 .physical_maximum = physical_extents,
111 .unit = unit,
112 .unit_exponent = exponent,
113 };
Jason Gerecke115d5e12012-10-03 17:24:32 -0700114
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700115 return hidinput_calc_abs_res(&field, ABS_X);
Jason Gerecke115d5e12012-10-03 17:24:32 -0700116}
117
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700118static void wacom_feature_mapping(struct hid_device *hdev,
119 struct hid_field *field, struct hid_usage *usage)
Chris Bagwell41343612011-10-26 22:32:52 -0700120{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700121 struct wacom *wacom = hid_get_drvdata(hdev);
122 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400123 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400124 u8 *data;
125 int ret;
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 &&
163 (field->application == WACOM_G9_DIGITIZER ||
164 field->application == WACOM_G11_DIGITIZER)) {
165 wacom->wacom_wac.mode_report = field->report->id;
166 wacom->wacom_wac.mode_value = 0;
167 }
168 break;
169
170 case WACOM_G9_PAGE:
171 case WACOM_G11_PAGE:
172 if (field->report->id == 0x03 &&
173 (field->application == WACOM_G9_TOUCHSCREEN ||
174 field->application == WACOM_G11_TOUCHSCREEN)) {
175 wacom->wacom_wac.mode_report = field->report->id;
176 wacom->wacom_wac.mode_value = 0;
177 }
178 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700179 }
180}
181
Chris Bagwell428f8582011-10-26 22:26:59 -0700182/*
183 * Interface Descriptor of wacom devices can be incomplete and
184 * inconsistent so wacom_features table is used to store stylus
185 * device's packet lengths, various maximum values, and tablet
186 * resolution based on product ID's.
187 *
188 * For devices that contain 2 interfaces, wacom_features table is
189 * inaccurate for the touch interface. Since the Interface Descriptor
190 * for touch interfaces has pretty complete data, this function exists
191 * to query tablet for this missing information instead of hard coding in
192 * an additional table.
193 *
194 * A typical Interface Descriptor for a stylus will contain a
195 * boot mouse application collection that is not of interest and this
196 * function will ignore it.
197 *
198 * It also contains a digitizer application collection that also is not
199 * of interest since any information it contains would be duplicate
200 * of what is in wacom_features. Usually it defines a report of an array
201 * of bytes that could be used as max length of the stylus packet returned.
202 * If it happens to define a Digitizer-Stylus Physical Collection then
203 * the X and Y logical values contain valid data but it is ignored.
204 *
205 * A typical Interface Descriptor for a touch interface will contain a
206 * Digitizer-Finger Physical Collection which will define both logical
207 * X/Y maximum as well as the physical size of tablet. Since touch
208 * interfaces haven't supported pressure or distance, this is enough
209 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700210 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700211 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
212 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700213 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700214static void wacom_usage_mapping(struct hid_device *hdev,
215 struct hid_field *field, struct hid_usage *usage)
216{
217 struct wacom *wacom = hid_get_drvdata(hdev);
218 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoiresd97a5522015-01-05 16:32:12 -0500219 bool finger = WACOM_FINGER_FIELD(field);
220 bool pen = WACOM_PEN_FIELD(field);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700221
222 /*
223 * Requiring Stylus Usage will ignore boot mouse
224 * X/Y values and some cases of invalid Digitizer X/Y
225 * values commonly reported.
226 */
Jason Gerecke042628a2015-04-30 17:51:54 -0700227 if (pen)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700228 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -0700229 else if (finger)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700230 features->device_type |= WACOM_DEVICETYPE_TOUCH;
Jason Gerecke042628a2015-04-30 17:51:54 -0700231 else
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700232 return;
233
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800234 /*
235 * Bamboo models do not support HID_DG_CONTACTMAX.
236 * And, Bamboo Pen only descriptor contains touch.
237 */
Ping Cheng3b164a02015-09-23 09:59:10 -0700238 if (features->type > BAMBOO_PT) {
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800239 /* ISDv4 touch devices at least supports one touch point */
240 if (finger && !features->touch_max)
241 features->touch_max = 1;
242 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700243
244 switch (usage->hid) {
245 case HID_GD_X:
246 features->x_max = field->logical_maximum;
247 if (finger) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700248 features->x_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700249 if ((features->type != BAMBOO_PT) &&
250 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700251 features->unit = field->unit;
252 features->unitExpo = field->unit_exponent;
253 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700254 }
255 break;
256 case HID_GD_Y:
257 features->y_max = field->logical_maximum;
258 if (finger) {
259 features->y_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700260 if ((features->type != BAMBOO_PT) &&
261 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700262 features->unit = field->unit;
263 features->unitExpo = field->unit_exponent;
264 }
265 }
266 break;
267 case HID_DG_TIPPRESSURE:
268 if (pen)
269 features->pressure_max = field->logical_maximum;
270 break;
271 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400272
273 if (features->type == HID_GENERIC)
274 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700275}
276
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800277static void wacom_post_parse_hid(struct hid_device *hdev,
278 struct wacom_features *features)
279{
280 struct wacom *wacom = hid_get_drvdata(hdev);
281 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
282
283 if (features->type == HID_GENERIC) {
284 /* Any last-minute generic device setup */
285 if (features->touch_max > 1) {
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -0700286 input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800287 INPUT_MT_DIRECT);
288 }
289 }
290}
291
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700292static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800293 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500294{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700295 struct hid_report_enum *rep_enum;
296 struct hid_report *hreport;
297 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500298
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700299 /* check features first */
300 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
301 list_for_each_entry(hreport, &rep_enum->report_list, list) {
302 for (i = 0; i < hreport->maxfield; i++) {
303 /* Ignore if report count is out of bounds. */
304 if (hreport->field[i]->report_count < 1)
305 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500306
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700307 for (j = 0; j < hreport->field[i]->maxusage; j++) {
308 wacom_feature_mapping(hdev, hreport->field[i],
309 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500310 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500311 }
312 }
313
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700314 /* now check the input usages */
315 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
316 list_for_each_entry(hreport, &rep_enum->report_list, list) {
317
318 if (!hreport->maxfield)
319 continue;
320
321 for (i = 0; i < hreport->maxfield; i++)
322 for (j = 0; j < hreport->field[i]->maxusage; j++)
323 wacom_usage_mapping(hdev, hreport->field[i],
324 hreport->field[i]->usage + j);
325 }
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800326
327 wacom_post_parse_hid(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -0500328}
329
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400330static int wacom_hid_set_device_mode(struct hid_device *hdev)
331{
332 struct wacom *wacom = hid_get_drvdata(hdev);
333 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
334 struct hid_report *r;
335 struct hid_report_enum *re;
336
337 if (hid_data->inputmode < 0)
338 return 0;
339
340 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
341 r = re->report_id_hash[hid_data->inputmode];
342 if (r) {
343 r->field[0]->value[hid_data->inputmode_index] = 2;
344 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
345 }
346 return 0;
347}
348
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700349static int wacom_set_device_mode(struct hid_device *hdev,
350 struct wacom_wac *wacom_wac)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700351{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700352 u8 *rep_data;
353 struct hid_report *r;
354 struct hid_report_enum *re;
355 int length;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700356 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700357
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700358 if (wacom_wac->mode_report < 0)
359 return 0;
360
361 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
362 r = re->report_id_hash[wacom_wac->mode_report];
363 if (!r)
364 return -EINVAL;
365
366 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700367 if (!rep_data)
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700368 return -ENOMEM;
369
370 length = hid_report_len(r);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700371
Jason Gereckefe494bc2012-10-03 17:25:35 -0700372 do {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700373 rep_data[0] = wacom_wac->mode_report;
374 rep_data[1] = wacom_wac->mode_value;
Chris Bagwell9937c022013-01-23 19:37:34 -0800375
Przemo Firszt296b7372014-08-06 14:00:38 -0700376 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
377 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700378 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700379 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700380 rep_data, length, 1);
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700381 } while (error >= 0 &&
382 rep_data[1] != wacom_wac->mode_report &&
383 limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700384
385 kfree(rep_data);
386
387 return error < 0 ? error : 0;
388}
389
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700390static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
391 struct wacom_features *features)
392{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700393 struct wacom *wacom = hid_get_drvdata(hdev);
394 int ret;
395 u8 rep_data[2];
396
397 switch (features->type) {
398 case GRAPHIRE_BT:
399 rep_data[0] = 0x03;
400 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700401 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
402 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700403
404 if (ret >= 0) {
405 rep_data[0] = speed == 0 ? 0x05 : 0x06;
406 rep_data[1] = 0x00;
407
408 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700409 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700410
411 if (ret >= 0) {
412 wacom->wacom_wac.bt_high_speed = speed;
413 return 0;
414 }
415 }
416
417 /*
418 * Note that if the raw queries fail, it's not a hard failure
419 * and it is safe to continue
420 */
421 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
422 rep_data[0], ret);
423 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700424 case INTUOS4WL:
425 if (speed == 1)
426 wacom->wacom_wac.bt_features &= ~0x20;
427 else
428 wacom->wacom_wac.bt_features |= 0x20;
429
430 rep_data[0] = 0x03;
431 rep_data[1] = wacom->wacom_wac.bt_features;
432
Przemo Firszt296b7372014-08-06 14:00:38 -0700433 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
434 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700435 if (ret >= 0)
436 wacom->wacom_wac.bt_high_speed = speed;
437 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700438 }
439
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700440 return 0;
441}
442
Jason Gereckefe494bc2012-10-03 17:25:35 -0700443/*
444 * Switch the tablet into its most-capable mode. Wacom tablets are
445 * typically configured to power-up in a mode which sends mouse-like
446 * reports to the OS. To get absolute position, pressure data, etc.
447 * from the tablet, it is necessary to switch the tablet out of this
448 * mode and into one which sends the full range of tablet data.
449 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700450static int wacom_query_tablet_data(struct hid_device *hdev,
451 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700452{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700453 struct wacom *wacom = hid_get_drvdata(hdev);
454 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
455
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700456 if (hdev->bus == BUS_BLUETOOTH)
457 return wacom_bt_query_tablet_data(hdev, 1, features);
458
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700459 if (features->type != HID_GENERIC) {
460 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
461 if (features->type > TABLETPC) {
462 /* MT Tablet PC touch */
463 wacom_wac->mode_report = 3;
464 wacom_wac->mode_value = 4;
465 } else if (features->type == WACOM_24HDT) {
466 wacom_wac->mode_report = 18;
467 wacom_wac->mode_value = 2;
468 } else if (features->type == WACOM_27QHDT) {
469 wacom_wac->mode_report = 131;
470 wacom_wac->mode_value = 2;
471 } else if (features->type == BAMBOO_PAD) {
472 wacom_wac->mode_report = 2;
473 wacom_wac->mode_value = 2;
474 }
475 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
476 if (features->type <= BAMBOO_PT) {
477 wacom_wac->mode_report = 2;
478 wacom_wac->mode_value = 2;
479 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700480 }
481 }
482
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700483 wacom_set_device_mode(hdev, wacom_wac);
484
485 if (features->type == HID_GENERIC)
486 return wacom_hid_set_device_mode(hdev);
487
Jason Gereckefe494bc2012-10-03 17:25:35 -0700488 return 0;
489}
490
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700491static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700492 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800493{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700494 struct wacom *wacom = hid_get_drvdata(hdev);
495 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800496
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700497 /* default features */
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700498 features->x_fuzz = 4;
499 features->y_fuzz = 4;
500 features->pressure_fuzz = 0;
Jason Gereckebef7e202016-04-22 14:30:53 -0700501 features->distance_fuzz = 1;
502 features->tilt_fuzz = 1;
Ping Chengec67bbe2009-12-15 00:35:24 -0800503
Chris Bagwelld3825d52012-03-25 23:26:11 -0700504 /*
505 * The wireless device HID is basic and layout conflicts with
506 * other tablets (monitor and touch interface can look like pen).
507 * Skip the query for this type and modify defaults based on
508 * interface number.
509 */
510 if (features->type == WIRELESS) {
Jason Gerecke3f14a632015-08-03 10:17:05 -0700511 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
Jason Gereckeccad85c2015-08-03 10:17:04 -0700512 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700513 else
Jason Gereckeaa86b182015-06-15 18:01:42 -0700514 features->device_type = WACOM_DEVICETYPE_NONE;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700515 return;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700516 }
517
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700518 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800519}
520
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700521struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700522 struct list_head list;
523 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700524 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700525 struct wacom_shared shared;
526};
527
528static LIST_HEAD(wacom_udev_list);
529static DEFINE_MUTEX(wacom_udev_list_lock);
530
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700531static bool wacom_are_sibling(struct hid_device *hdev,
532 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700533{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700534 struct wacom *wacom = hid_get_drvdata(hdev);
535 struct wacom_features *features = &wacom->wacom_wac.features;
536 int vid = features->oVid;
537 int pid = features->oPid;
538 int n1,n2;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700539
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700540 if (vid == 0 && pid == 0) {
541 vid = hdev->vendor;
542 pid = hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700543 }
544
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700545 if (vid != sibling->vendor || pid != sibling->product)
546 return false;
547
548 /* Compare the physical path. */
549 n1 = strrchr(hdev->phys, '.') - hdev->phys;
550 n2 = strrchr(sibling->phys, '.') - sibling->phys;
551 if (n1 != n2 || n1 <= 0 || n2 <= 0)
552 return false;
553
554 return !strncmp(hdev->phys, sibling->phys, n1);
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700555}
556
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700557static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700558{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700559 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700560
561 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700562 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700563 kref_get(&data->kref);
564 return data;
565 }
566 }
567
568 return NULL;
569}
570
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700571static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700572{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700573 struct wacom *wacom = hid_get_drvdata(hdev);
574 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
575 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700576 int retval = 0;
577
578 mutex_lock(&wacom_udev_list_lock);
579
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700580 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700581 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700582 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700583 if (!data) {
584 retval = -ENOMEM;
585 goto out;
586 }
587
588 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700589 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700590 list_add_tail(&data->list, &wacom_udev_list);
591 }
592
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700593 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700594
Jason Gereckeaa86b182015-06-15 18:01:42 -0700595 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500596 wacom_wac->shared->touch = hdev;
Jason Gereckeaa86b182015-06-15 18:01:42 -0700597 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500598 wacom_wac->shared->pen = hdev;
599
Ping Cheng4492eff2010-03-19 22:18:15 -0700600out:
601 mutex_unlock(&wacom_udev_list_lock);
602 return retval;
603}
604
605static void wacom_release_shared_data(struct kref *kref)
606{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700607 struct wacom_hdev_data *data =
608 container_of(kref, struct wacom_hdev_data, kref);
Ping Cheng4492eff2010-03-19 22:18:15 -0700609
610 mutex_lock(&wacom_udev_list_lock);
611 list_del(&data->list);
612 mutex_unlock(&wacom_udev_list_lock);
613
614 kfree(data);
615}
616
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500617static void wacom_remove_shared_data(struct wacom *wacom)
Ping Cheng4492eff2010-03-19 22:18:15 -0700618{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700619 struct wacom_hdev_data *data;
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500620 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Ping Cheng4492eff2010-03-19 22:18:15 -0700621
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500622 if (wacom_wac->shared) {
623 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
624 shared);
625
626 if (wacom_wac->shared->touch == wacom->hdev)
627 wacom_wac->shared->touch = NULL;
628 else if (wacom_wac->shared->pen == wacom->hdev)
629 wacom_wac->shared->pen = NULL;
630
Ping Cheng4492eff2010-03-19 22:18:15 -0700631 kref_put(&data->kref, wacom_release_shared_data);
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500632 wacom_wac->shared = NULL;
Ping Cheng4492eff2010-03-19 22:18:15 -0700633 }
634}
635
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700636static int wacom_led_control(struct wacom *wacom)
637{
638 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700639 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700640 unsigned char report_id = WAC_CMD_LED_CONTROL;
641 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700642
Ping Cheng912ca212014-09-10 12:41:31 -0700643 if (wacom->wacom_wac.pid) { /* wireless connected */
644 report_id = WAC_CMD_WL_LED_CONTROL;
645 buf_size = 13;
646 }
647 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700648 if (!buf)
649 return -ENOMEM;
650
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700651 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700652 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700653 /*
654 * Touch Ring and crop mark LED luminance may take on
655 * one of four values:
656 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
657 */
658 int ring_led = wacom->led.select[0] & 0x03;
659 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
660 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700661 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700662
Ping Cheng912ca212014-09-10 12:41:31 -0700663 buf[0] = report_id;
664 if (wacom->wacom_wac.pid) {
665 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
666 buf, buf_size, WAC_CMD_RETRIES);
667 buf[0] = report_id;
668 buf[4] = led_bits;
669 } else
670 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700671 }
672 else {
673 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700674
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700675 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
676 wacom->wacom_wac.features.type == WACOM_24HD)
677 led |= (wacom->led.select[1] << 4) | 0x40;
678
Ping Cheng912ca212014-09-10 12:41:31 -0700679 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700680 buf[1] = led;
681 buf[2] = wacom->led.llv;
682 buf[3] = wacom->led.hlv;
683 buf[4] = wacom->led.img_lum;
684 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700685
Ping Cheng912ca212014-09-10 12:41:31 -0700686 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700687 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700688 kfree(buf);
689
690 return retval;
691}
692
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700693static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
694 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700695{
696 unsigned char *buf;
697 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700698 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700699
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700700 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700701 if (!buf)
702 return -ENOMEM;
703
704 /* Send 'start' command */
705 buf[0] = WAC_CMD_ICON_START;
706 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700707 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
708 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700709 if (retval < 0)
710 goto out;
711
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700712 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700713 buf[1] = button_id & 0x07;
714 for (i = 0; i < 4; i++) {
715 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700716 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700717
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700718 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700719 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700720 if (retval < 0)
721 break;
722 }
723
724 /* Send 'stop' */
725 buf[0] = WAC_CMD_ICON_START;
726 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700727 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
728 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700729
730out:
731 kfree(buf);
732 return retval;
733}
734
Ping Cheng09e7d942011-10-04 23:51:14 -0700735static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700736 const char *buf, size_t count)
737{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800738 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700739 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700740 unsigned int id;
741 int err;
742
743 err = kstrtouint(buf, 10, &id);
744 if (err)
745 return err;
746
747 mutex_lock(&wacom->lock);
748
Ping Cheng09e7d942011-10-04 23:51:14 -0700749 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700750 err = wacom_led_control(wacom);
751
752 mutex_unlock(&wacom->lock);
753
754 return err < 0 ? err : count;
755}
756
Ping Cheng09e7d942011-10-04 23:51:14 -0700757#define DEVICE_LED_SELECT_ATTR(SET_ID) \
758static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
759 struct device_attribute *attr, const char *buf, size_t count) \
760{ \
761 return wacom_led_select_store(dev, SET_ID, buf, count); \
762} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700763static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
764 struct device_attribute *attr, char *buf) \
765{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800766 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700767 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700768 return scnprintf(buf, PAGE_SIZE, "%d\n", \
769 wacom->led.select[SET_ID]); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700770} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700771static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700772 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700773 wacom_led##SET_ID##_select_store)
774
775DEVICE_LED_SELECT_ATTR(0);
776DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700777
778static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
779 const char *buf, size_t count)
780{
781 unsigned int value;
782 int err;
783
784 err = kstrtouint(buf, 10, &value);
785 if (err)
786 return err;
787
788 mutex_lock(&wacom->lock);
789
790 *dest = value & 0x7f;
791 err = wacom_led_control(wacom);
792
793 mutex_unlock(&wacom->lock);
794
795 return err < 0 ? err : count;
796}
797
798#define DEVICE_LUMINANCE_ATTR(name, field) \
799static ssize_t wacom_##name##_luminance_store(struct device *dev, \
800 struct device_attribute *attr, const char *buf, size_t count) \
801{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800802 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700803 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700804 \
805 return wacom_luminance_store(wacom, &wacom->led.field, \
806 buf, count); \
807} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700808static ssize_t wacom_##name##_luminance_show(struct device *dev, \
809 struct device_attribute *attr, char *buf) \
810{ \
811 struct wacom *wacom = dev_get_drvdata(dev); \
812 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
813} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700814static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700815 wacom_##name##_luminance_show, \
816 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700817
818DEVICE_LUMINANCE_ATTR(status0, llv);
819DEVICE_LUMINANCE_ATTR(status1, hlv);
820DEVICE_LUMINANCE_ATTR(buttons, img_lum);
821
822static ssize_t wacom_button_image_store(struct device *dev, int button_id,
823 const char *buf, size_t count)
824{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800825 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700826 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700827 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700828 unsigned len;
829 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700830
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700831 if (hdev->bus == BUS_BLUETOOTH) {
832 len = 256;
833 xfer_id = WAC_CMD_ICON_BT_XFER;
834 } else {
835 len = 1024;
836 xfer_id = WAC_CMD_ICON_XFER;
837 }
838
839 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700840 return -EINVAL;
841
842 mutex_lock(&wacom->lock);
843
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700844 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700845
846 mutex_unlock(&wacom->lock);
847
848 return err < 0 ? err : count;
849}
850
851#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
852static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
853 struct device_attribute *attr, const char *buf, size_t count) \
854{ \
855 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
856} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700857static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700858 NULL, wacom_btnimg##BUTTON_ID##_store)
859
860DEVICE_BTNIMG_ATTR(0);
861DEVICE_BTNIMG_ATTR(1);
862DEVICE_BTNIMG_ATTR(2);
863DEVICE_BTNIMG_ATTR(3);
864DEVICE_BTNIMG_ATTR(4);
865DEVICE_BTNIMG_ATTR(5);
866DEVICE_BTNIMG_ATTR(6);
867DEVICE_BTNIMG_ATTR(7);
868
Ping Cheng09e7d942011-10-04 23:51:14 -0700869static struct attribute *cintiq_led_attrs[] = {
870 &dev_attr_status_led0_select.attr,
871 &dev_attr_status_led1_select.attr,
872 NULL
873};
874
875static struct attribute_group cintiq_led_attr_group = {
876 .name = "wacom_led",
877 .attrs = cintiq_led_attrs,
878};
879
880static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700881 &dev_attr_status0_luminance.attr,
882 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700883 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700884 &dev_attr_buttons_luminance.attr,
885 &dev_attr_button0_rawimg.attr,
886 &dev_attr_button1_rawimg.attr,
887 &dev_attr_button2_rawimg.attr,
888 &dev_attr_button3_rawimg.attr,
889 &dev_attr_button4_rawimg.attr,
890 &dev_attr_button5_rawimg.attr,
891 &dev_attr_button6_rawimg.attr,
892 &dev_attr_button7_rawimg.attr,
893 NULL
894};
895
Ping Cheng09e7d942011-10-04 23:51:14 -0700896static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700897 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700898 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700899};
900
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700901static struct attribute *intuos5_led_attrs[] = {
902 &dev_attr_status0_luminance.attr,
903 &dev_attr_status_led0_select.attr,
904 NULL
905};
906
907static struct attribute_group intuos5_led_attr_group = {
908 .name = "wacom_led",
909 .attrs = intuos5_led_attrs,
910};
911
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700912static int wacom_initialize_leds(struct wacom *wacom)
913{
914 int error;
915
Jason Gerecke862cf552015-06-15 18:01:43 -0700916 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
917 return 0;
918
Ping Cheng09e7d942011-10-04 23:51:14 -0700919 /* Initialize default values */
920 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700921 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700922 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700923 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700924 case INTUOS4L:
925 wacom->led.select[0] = 0;
926 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700927 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700928 wacom->led.hlv = 20;
929 wacom->led.img_lum = 10;
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700930 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700931 &intuos4_led_attr_group);
932 break;
933
Jason Gerecke246835f2011-12-12 00:12:04 -0800934 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700935 case WACOM_21UX2:
936 wacom->led.select[0] = 0;
937 wacom->led.select[1] = 0;
938 wacom->led.llv = 0;
939 wacom->led.hlv = 0;
940 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700941
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700942 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700943 &cintiq_led_attr_group);
944 break;
945
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700946 case INTUOS5S:
947 case INTUOS5:
948 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700949 case INTUOSPS:
950 case INTUOSPM:
951 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -0700952 wacom->led.select[0] = 0;
953 wacom->led.select[1] = 0;
954 wacom->led.llv = 32;
955 wacom->led.hlv = 0;
956 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700957
Jason Gerecke862cf552015-06-15 18:01:43 -0700958 error = sysfs_create_group(&wacom->hdev->dev.kobj,
959 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700960 break;
961
Ping Cheng09e7d942011-10-04 23:51:14 -0700962 default:
963 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700964 }
965
Ping Cheng09e7d942011-10-04 23:51:14 -0700966 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700967 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -0700968 "cannot create sysfs group err: %d\n", error);
969 return error;
970 }
971 wacom_led_control(wacom);
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700972 wacom->led_initialized = true;
Ping Cheng09e7d942011-10-04 23:51:14 -0700973
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700974 return 0;
975}
976
977static void wacom_destroy_leds(struct wacom *wacom)
978{
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700979 if (!wacom->led_initialized)
980 return;
981
Jason Gerecke862cf552015-06-15 18:01:43 -0700982 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
983 return;
984
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700985 wacom->led_initialized = false;
986
Ping Cheng09e7d942011-10-04 23:51:14 -0700987 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700988 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700989 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700990 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700991 case INTUOS4L:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700992 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700993 &intuos4_led_attr_group);
994 break;
995
Jason Gerecke246835f2011-12-12 00:12:04 -0800996 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700997 case WACOM_21UX2:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700998 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700999 &cintiq_led_attr_group);
1000 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001001
1002 case INTUOS5S:
1003 case INTUOS5:
1004 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001005 case INTUOSPS:
1006 case INTUOSPM:
1007 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -07001008 sysfs_remove_group(&wacom->hdev->dev.kobj,
1009 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001010 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001011 }
1012}
1013
Chris Bagwella1d552c2012-03-25 23:26:30 -07001014static enum power_supply_property wacom_battery_props[] = {
Jason Gerecke71fa6412015-03-11 10:25:41 -07001015 POWER_SUPPLY_PROP_PRESENT,
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001016 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001017 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001018 POWER_SUPPLY_PROP_CAPACITY
1019};
1020
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001021static enum power_supply_property wacom_ac_props[] = {
1022 POWER_SUPPLY_PROP_PRESENT,
1023 POWER_SUPPLY_PROP_ONLINE,
1024 POWER_SUPPLY_PROP_SCOPE,
1025};
1026
Chris Bagwella1d552c2012-03-25 23:26:30 -07001027static int wacom_battery_get_property(struct power_supply *psy,
1028 enum power_supply_property psp,
1029 union power_supply_propval *val)
1030{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001031 struct wacom *wacom = power_supply_get_drvdata(psy);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001032 int ret = 0;
1033
1034 switch (psp) {
Jason Gerecke71fa6412015-03-11 10:25:41 -07001035 case POWER_SUPPLY_PROP_PRESENT:
1036 val->intval = wacom->wacom_wac.bat_connected;
1037 break;
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001038 case POWER_SUPPLY_PROP_SCOPE:
1039 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1040 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001041 case POWER_SUPPLY_PROP_CAPACITY:
1042 val->intval =
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001043 wacom->wacom_wac.battery_capacity;
1044 break;
1045 case POWER_SUPPLY_PROP_STATUS:
1046 if (wacom->wacom_wac.bat_charging)
1047 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1048 else if (wacom->wacom_wac.battery_capacity == 100 &&
1049 wacom->wacom_wac.ps_connected)
1050 val->intval = POWER_SUPPLY_STATUS_FULL;
Jason Gereckeb0882cb2015-03-06 11:47:43 -08001051 else if (wacom->wacom_wac.ps_connected)
1052 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001053 else
1054 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001055 break;
1056 default:
1057 ret = -EINVAL;
1058 break;
1059 }
1060
1061 return ret;
1062}
1063
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001064static int wacom_ac_get_property(struct power_supply *psy,
1065 enum power_supply_property psp,
1066 union power_supply_propval *val)
1067{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001068 struct wacom *wacom = power_supply_get_drvdata(psy);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001069 int ret = 0;
1070
1071 switch (psp) {
1072 case POWER_SUPPLY_PROP_PRESENT:
1073 /* fall through */
1074 case POWER_SUPPLY_PROP_ONLINE:
1075 val->intval = wacom->wacom_wac.ps_connected;
1076 break;
1077 case POWER_SUPPLY_PROP_SCOPE:
1078 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1079 break;
1080 default:
1081 ret = -EINVAL;
1082 break;
1083 }
1084 return ret;
1085}
1086
Chris Bagwella1d552c2012-03-25 23:26:30 -07001087static int wacom_initialize_battery(struct wacom *wacom)
1088{
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001089 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001090 struct device *dev = &wacom->hdev->dev;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001091 struct power_supply_config psy_cfg = { .drv_data = wacom, };
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001092 struct power_supply_desc *bat_desc = &wacom->battery_desc;
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001093 unsigned long n;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001094 int error;
1095
1096 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1097 return -ENOMEM;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001098
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001099 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001100 struct power_supply_desc *ac_desc = &wacom->ac_desc;
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001101 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001102
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001103 bat_desc->properties = wacom_battery_props;
1104 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1105 bat_desc->get_property = wacom_battery_get_property;
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001106 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001107 bat_desc->name = wacom->wacom_wac.bat_name;
1108 bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1109 bat_desc->use_for_apm = 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001110
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001111 ac_desc->properties = wacom_ac_props;
1112 ac_desc->num_properties = ARRAY_SIZE(wacom_ac_props);
1113 ac_desc->get_property = wacom_ac_get_property;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001114 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001115 ac_desc->name = wacom->wacom_wac.ac_name;
1116 ac_desc->type = POWER_SUPPLY_TYPE_MAINS;
1117 ac_desc->use_for_apm = 0;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001118
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001119 wacom->battery = devm_power_supply_register(dev,
1120 &wacom->battery_desc,
1121 &psy_cfg);
1122 if (IS_ERR(wacom->battery)) {
1123 error = PTR_ERR(wacom->battery);
1124 goto err;
1125 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001126
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001127 power_supply_powers(wacom->battery, &wacom->hdev->dev);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001128
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001129 wacom->ac = devm_power_supply_register(dev,
1130 &wacom->ac_desc,
1131 &psy_cfg);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001132 if (IS_ERR(wacom->ac)) {
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001133 error = PTR_ERR(wacom->ac);
1134 goto err;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001135 }
1136
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001137 power_supply_powers(wacom->ac, &wacom->hdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001138 }
1139
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001140 devres_close_group(dev, bat_desc);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001141 return 0;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001142
1143err:
1144 devres_release_group(dev, bat_desc);
1145 return error;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001146}
1147
1148static void wacom_destroy_battery(struct wacom *wacom)
1149{
Linus Torvalds8de29a32015-04-14 08:25:26 -08001150 if (wacom->battery) {
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001151 devres_release_group(&wacom->hdev->dev, &wacom->battery_desc);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001152 wacom->battery = NULL;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001153 wacom->ac = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001154 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001155}
1156
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001157static ssize_t wacom_show_speed(struct device *dev,
1158 struct device_attribute
1159 *attr, char *buf)
1160{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001161 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001162 struct wacom *wacom = hid_get_drvdata(hdev);
1163
1164 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1165}
1166
1167static ssize_t wacom_store_speed(struct device *dev,
1168 struct device_attribute *attr,
1169 const char *buf, size_t count)
1170{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001171 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001172 struct wacom *wacom = hid_get_drvdata(hdev);
1173 u8 new_speed;
1174
1175 if (kstrtou8(buf, 0, &new_speed))
1176 return -EINVAL;
1177
1178 if (new_speed != 0 && new_speed != 1)
1179 return -EINVAL;
1180
1181 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1182
1183 return count;
1184}
1185
Ping Chenge0984bc2014-09-10 12:40:05 -07001186static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001187 wacom_show_speed, wacom_store_speed);
1188
Aaron Skomra72b236d2015-08-20 16:05:17 -07001189
1190static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1191 struct kobj_attribute *kattr,
1192 char *buf, int index)
1193{
Geliang Tang2cf83832015-12-27 17:25:24 +08001194 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001195 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001196 struct wacom *wacom = hid_get_drvdata(hdev);
1197 u8 mode;
1198
1199 mode = wacom->led.select[index];
1200 if (mode >= 0 && mode < 3)
1201 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
1202 else
1203 return snprintf(buf, PAGE_SIZE, "%d\n", -1);
1204}
1205
1206#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1207static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1208 struct kobj_attribute *kattr, char *buf) \
1209{ \
1210 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1211} \
1212static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1213 .attr = {.name = "remote_mode", \
1214 .mode = DEV_ATTR_RO_PERM}, \
1215 .show = wacom_show_remote##SET_ID##_mode, \
1216}; \
1217static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1218 &remote##SET_ID##_mode_attr.attr, \
1219 NULL \
1220}; \
1221static struct attribute_group remote##SET_ID##_serial_group = { \
1222 .name = NULL, \
1223 .attrs = remote##SET_ID##_serial_attrs, \
1224}
1225
1226DEVICE_EKR_ATTR_GROUP(0);
1227DEVICE_EKR_ATTR_GROUP(1);
1228DEVICE_EKR_ATTR_GROUP(2);
1229DEVICE_EKR_ATTR_GROUP(3);
1230DEVICE_EKR_ATTR_GROUP(4);
1231
1232int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial, int index)
1233{
1234 int error = 0;
1235 char *buf;
1236 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1237
1238 wacom_wac->serial[index] = serial;
1239
1240 buf = kzalloc(WAC_REMOTE_SERIAL_MAX_STRLEN, GFP_KERNEL);
1241 if (!buf)
1242 return -ENOMEM;
1243 snprintf(buf, WAC_REMOTE_SERIAL_MAX_STRLEN, "%d", serial);
1244 wacom->remote_group[index].name = buf;
1245
1246 error = sysfs_create_group(wacom->remote_dir,
1247 &wacom->remote_group[index]);
1248 if (error) {
1249 hid_err(wacom->hdev,
1250 "cannot create sysfs group err: %d\n", error);
1251 kobject_put(wacom->remote_dir);
1252 return error;
1253 }
1254
1255 return 0;
1256}
1257
1258void wacom_remote_destroy_attr_group(struct wacom *wacom, __u32 serial)
1259{
1260 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1261 int i;
1262
1263 if (!serial)
1264 return;
1265
1266 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1267 if (wacom_wac->serial[i] == serial) {
1268 wacom_wac->serial[i] = 0;
1269 wacom->led.select[i] = WACOM_STATUS_UNKNOWN;
1270 if (wacom->remote_group[i].name) {
1271 sysfs_remove_group(wacom->remote_dir,
1272 &wacom->remote_group[i]);
1273 kfree(wacom->remote_group[i].name);
1274 wacom->remote_group[i].name = NULL;
1275 }
1276 }
1277 }
1278}
1279
1280static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1281{
1282 const size_t buf_size = 2;
1283 unsigned char *buf;
1284 int retval;
1285
1286 buf = kzalloc(buf_size, GFP_KERNEL);
1287 if (!buf)
1288 return -ENOMEM;
1289
1290 buf[0] = WAC_CMD_DELETE_PAIRING;
1291 buf[1] = selector;
1292
1293 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1294 buf_size, WAC_CMD_RETRIES);
1295 kfree(buf);
1296
1297 return retval;
1298}
1299
1300static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1301 struct kobj_attribute *attr,
1302 const char *buf, size_t count)
1303{
1304 unsigned char selector = 0;
Geliang Tang2cf83832015-12-27 17:25:24 +08001305 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001306 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001307 struct wacom *wacom = hid_get_drvdata(hdev);
1308 int err;
1309
1310 if (!strncmp(buf, "*\n", 2)) {
1311 selector = WAC_CMD_UNPAIR_ALL;
1312 } else {
1313 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1314 buf);
1315 return -1;
1316 }
1317
1318 mutex_lock(&wacom->lock);
1319
1320 err = wacom_cmd_unpair_remote(wacom, selector);
1321 mutex_unlock(&wacom->lock);
1322
1323 return err < 0 ? err : count;
1324}
1325
1326static struct kobj_attribute unpair_remote_attr = {
1327 .attr = {.name = "unpair_remote", .mode = 0200},
1328 .store = wacom_store_unpair_remote,
1329};
1330
1331static const struct attribute *remote_unpair_attrs[] = {
1332 &unpair_remote_attr.attr,
1333 NULL
1334};
1335
1336static int wacom_initialize_remote(struct wacom *wacom)
1337{
1338 int error = 0;
1339 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1340 int i;
1341
1342 if (wacom->wacom_wac.features.type != REMOTE)
1343 return 0;
1344
1345 wacom->remote_group[0] = remote0_serial_group;
1346 wacom->remote_group[1] = remote1_serial_group;
1347 wacom->remote_group[2] = remote2_serial_group;
1348 wacom->remote_group[3] = remote3_serial_group;
1349 wacom->remote_group[4] = remote4_serial_group;
1350
1351 wacom->remote_dir = kobject_create_and_add("wacom_remote",
1352 &wacom->hdev->dev.kobj);
1353 if (!wacom->remote_dir)
1354 return -ENOMEM;
1355
1356 error = sysfs_create_files(wacom->remote_dir, remote_unpair_attrs);
1357
1358 if (error) {
1359 hid_err(wacom->hdev,
1360 "cannot create sysfs group err: %d\n", error);
1361 return error;
1362 }
1363
1364 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1365 wacom->led.select[i] = WACOM_STATUS_UNKNOWN;
1366 wacom_wac->serial[i] = 0;
1367 }
1368
1369 return 0;
1370}
1371
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001372static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001373{
1374 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001375 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001376 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001377
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001378 input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001379 if (!input_dev)
1380 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001381
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001382 input_dev->name = wacom_wac->features.name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001383 input_dev->phys = hdev->phys;
1384 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001385 input_dev->open = wacom_open;
1386 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001387 input_dev->uniq = hdev->uniq;
1388 input_dev->id.bustype = hdev->bus;
1389 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001390 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001391 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001392 input_set_drvdata(input_dev, wacom);
1393
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001394 return input_dev;
1395}
1396
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001397static void wacom_clean_inputs(struct wacom *wacom)
1398{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001399 if (wacom->wacom_wac.pen_input) {
1400 if (wacom->wacom_wac.pen_registered)
1401 input_unregister_device(wacom->wacom_wac.pen_input);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001402 else
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001403 input_free_device(wacom->wacom_wac.pen_input);
1404 }
1405 if (wacom->wacom_wac.touch_input) {
1406 if (wacom->wacom_wac.touch_registered)
1407 input_unregister_device(wacom->wacom_wac.touch_input);
1408 else
1409 input_free_device(wacom->wacom_wac.touch_input);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001410 }
1411 if (wacom->wacom_wac.pad_input) {
Ping Cheng954df6a2014-11-20 16:31:12 -08001412 if (wacom->wacom_wac.pad_registered)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001413 input_unregister_device(wacom->wacom_wac.pad_input);
1414 else
1415 input_free_device(wacom->wacom_wac.pad_input);
1416 }
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001417 wacom->wacom_wac.pen_input = NULL;
1418 wacom->wacom_wac.touch_input = NULL;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001419 wacom->wacom_wac.pad_input = NULL;
Benjamin Tissoires17f28472016-02-12 17:27:44 +01001420 wacom->wacom_wac.pen_registered = false;
1421 wacom->wacom_wac.touch_registered = false;
1422 wacom->wacom_wac.pad_registered = false;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001423}
1424
Jason Gerecked9f2d202015-07-13 18:03:44 -07001425static int wacom_allocate_inputs(struct wacom *wacom)
1426{
1427 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1428
1429 wacom_wac->pen_input = wacom_allocate_input(wacom);
1430 wacom_wac->touch_input = wacom_allocate_input(wacom);
1431 wacom_wac->pad_input = wacom_allocate_input(wacom);
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001432 if (!wacom_wac->pen_input ||
1433 !wacom_wac->touch_input ||
1434 !wacom_wac->pad_input)
Jason Gerecked9f2d202015-07-13 18:03:44 -07001435 return -ENOMEM;
Jason Gerecked9f2d202015-07-13 18:03:44 -07001436
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001437 wacom_wac->pen_input->name = wacom_wac->pen_name;
Jason Gerecked9f2d202015-07-13 18:03:44 -07001438 wacom_wac->touch_input->name = wacom_wac->touch_name;
1439 wacom_wac->pad_input->name = wacom_wac->pad_name;
1440
1441 return 0;
1442}
1443
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001444static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001445{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001446 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001447 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Jason Gerecke2636a3f2015-06-15 18:01:44 -07001448 int error = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001449
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001450 pen_input_dev = wacom_wac->pen_input;
1451 touch_input_dev = wacom_wac->touch_input;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001452 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001453
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001454 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001455 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001456
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001457 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1458 if (error) {
1459 /* no pen in use on this interface */
1460 input_free_device(pen_input_dev);
1461 wacom_wac->pen_input = NULL;
1462 pen_input_dev = NULL;
1463 } else {
1464 error = input_register_device(pen_input_dev);
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001465 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001466 goto fail;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001467 wacom_wac->pen_registered = true;
1468 }
1469
1470 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1471 if (error) {
1472 /* no touch in use on this interface */
1473 input_free_device(touch_input_dev);
1474 wacom_wac->touch_input = NULL;
1475 touch_input_dev = NULL;
1476 } else {
1477 error = input_register_device(touch_input_dev);
1478 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001479 goto fail;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001480 wacom_wac->touch_registered = true;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001481 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001482
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001483 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1484 if (error) {
1485 /* no pad in use on this interface */
1486 input_free_device(pad_input_dev);
1487 wacom_wac->pad_input = NULL;
1488 pad_input_dev = NULL;
1489 } else {
1490 error = input_register_device(pad_input_dev);
1491 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001492 goto fail;
Ping Cheng954df6a2014-11-20 16:31:12 -08001493 wacom_wac->pad_registered = true;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001494 }
1495
Ping Cheng19635182012-04-29 21:09:18 -07001496 return 0;
1497
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001498fail:
1499 wacom_wac->pad_input = NULL;
1500 wacom_wac->pad_registered = false;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001501 wacom_wac->touch_input = NULL;
1502 wacom_wac->touch_registered = false;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001503 wacom_wac->pen_input = NULL;
1504 wacom_wac->pen_registered = false;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001505 return error;
1506}
1507
Jason Gerecke0be01712015-08-05 15:44:53 -07001508/*
1509 * Not all devices report physical dimensions from HID.
1510 * Compute the default from hardcoded logical dimension
1511 * and resolution before driver overwrites them.
1512 */
1513static void wacom_set_default_phy(struct wacom_features *features)
1514{
1515 if (features->x_resolution) {
1516 features->x_phy = (features->x_max * 100) /
1517 features->x_resolution;
1518 features->y_phy = (features->y_max * 100) /
1519 features->y_resolution;
1520 }
1521}
1522
1523static void wacom_calculate_res(struct wacom_features *features)
1524{
1525 /* set unit to "100th of a mm" for devices not reported by HID */
1526 if (!features->unit) {
1527 features->unit = 0x11;
1528 features->unitExpo = -3;
1529 }
1530
1531 features->x_resolution = wacom_calc_hid_res(features->x_max,
1532 features->x_phy,
1533 features->unit,
1534 features->unitExpo);
1535 features->y_resolution = wacom_calc_hid_res(features->y_max,
1536 features->y_phy,
1537 features->unit,
1538 features->unitExpo);
1539}
1540
Jason Gereckefce99572015-03-06 11:47:40 -08001541void wacom_battery_work(struct work_struct *work)
1542{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02001543 struct wacom *wacom = container_of(work, struct wacom, battery_work);
Jason Gereckefce99572015-03-06 11:47:40 -08001544
1545 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_initialize_battery(wacom);
1548 }
1549 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Linus Torvalds8de29a32015-04-14 08:25:26 -08001550 wacom->battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001551 wacom_destroy_battery(wacom);
1552 }
1553}
1554
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001555static size_t wacom_compute_pktlen(struct hid_device *hdev)
1556{
1557 struct hid_report_enum *report_enum;
1558 struct hid_report *report;
1559 size_t size = 0;
1560
1561 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1562
1563 list_for_each_entry(report, &report_enum->report_list, list) {
Mathieu Magnaudetdabb05c62014-11-27 16:02:36 +01001564 size_t report_size = hid_report_len(report);
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001565 if (report_size > size)
1566 size = report_size;
1567 }
1568
1569 return size;
1570}
1571
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001572static void wacom_update_name(struct wacom *wacom, const char *suffix)
Ping Chengc24eab42015-04-24 15:32:51 -07001573{
1574 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1575 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke44b52502015-06-15 18:01:41 -07001576 char name[WACOM_NAME_MAX];
Ping Chengc24eab42015-04-24 15:32:51 -07001577
1578 /* Generic devices name unspecified */
1579 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1580 if (strstr(wacom->hdev->name, "Wacom") ||
1581 strstr(wacom->hdev->name, "wacom") ||
1582 strstr(wacom->hdev->name, "WACOM")) {
1583 /* name is in HID descriptor, use it */
Jason Gerecke44b52502015-06-15 18:01:41 -07001584 strlcpy(name, wacom->hdev->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001585
1586 /* strip out excess whitespaces */
1587 while (1) {
Jason Gerecke44b52502015-06-15 18:01:41 -07001588 char *gap = strstr(name, " ");
Ping Chengc24eab42015-04-24 15:32:51 -07001589 if (gap == NULL)
1590 break;
1591 /* shift everything including the terminator */
1592 memmove(gap, gap+1, strlen(gap));
1593 }
1594 /* get rid of trailing whitespace */
Jason Gerecke44b52502015-06-15 18:01:41 -07001595 if (name[strlen(name)-1] == ' ')
1596 name[strlen(name)-1] = '\0';
Ping Chengc24eab42015-04-24 15:32:51 -07001597 } else {
1598 /* no meaningful name retrieved. use product ID */
Jason Gerecke44b52502015-06-15 18:01:41 -07001599 snprintf(name, sizeof(name),
Ping Chengc24eab42015-04-24 15:32:51 -07001600 "%s %X", features->name, wacom->hdev->product);
1601 }
1602 } else {
Jason Gerecke44b52502015-06-15 18:01:41 -07001603 strlcpy(name, features->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001604 }
1605
1606 /* Append the device type to the name */
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001607 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001608 "%s%s Pen", name, suffix);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001609 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001610 "%s%s Finger", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07001611 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001612 "%s%s Pad", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07001613}
1614
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001615static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
Ping Cheng3bea7332006-07-13 18:01:36 -07001616{
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001617 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1618 struct wacom_features *features = &wacom_wac->features;
1619 struct hid_device *hdev = wacom->hdev;
Jason Childse33da8a2010-02-17 22:38:31 -08001620 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001621 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07001622
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001623 features->pktlen = wacom_compute_pktlen(hdev);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001624 if (features->pktlen > WACOM_PKGLEN_MAX)
1625 return -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001626
Jason Gerecke3f14a632015-08-03 10:17:05 -07001627 error = wacom_allocate_inputs(wacom);
1628 if (error)
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001629 return error;
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001630
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05001631 /*
1632 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
1633 * into debug mode for the touch part.
1634 * We ignore the other interfaces.
1635 */
1636 if (features->type == BAMBOO_PAD) {
1637 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
1638 features->type = HID_GENERIC;
1639 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
1640 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
1641 error = -ENODEV;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001642 goto fail_allocate_inputs;
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05001643 }
1644 }
1645
Ping Cheng401d7d12013-07-28 00:38:54 -07001646 /* set the default size in case we do not get them from hid */
1647 wacom_set_default_phy(features);
1648
Ping Chengf393ee22012-04-29 21:09:17 -07001649 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001650 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng42f4f272015-04-15 16:53:54 -07001651 wacom_setup_device_quirks(wacom);
Jason Gerecke042628a2015-04-30 17:51:54 -07001652
Jason Gereckeaa86b182015-06-15 18:01:42 -07001653 if (features->device_type == WACOM_DEVICETYPE_NONE &&
1654 features->type != WIRELESS) {
Jason Gerecke8e116d32015-04-30 17:51:55 -07001655 error = features->type == HID_GENERIC ? -ENODEV : 0;
1656
Jason Gerecke042628a2015-04-30 17:51:54 -07001657 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
Jason Gerecke8e116d32015-04-30 17:51:55 -07001658 hdev->name,
1659 error ? "Ignoring" : "Assuming pen");
1660
1661 if (error)
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001662 goto fail_parsed;
Jason Gerecke042628a2015-04-30 17:51:54 -07001663
Jason Gereckeaa86b182015-06-15 18:01:42 -07001664 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -07001665 }
1666
Ping Cheng401d7d12013-07-28 00:38:54 -07001667 wacom_calculate_res(features);
1668
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001669 wacom_update_name(wacom, wireless ? " (WL)" : "");
Ping Cheng4492eff2010-03-19 22:18:15 -07001670
Ping Chengf3586d22015-03-20 14:57:00 -07001671 error = wacom_add_shared_data(hdev);
1672 if (error)
1673 goto fail_shared_data;
Ping Cheng49b764a2010-02-20 00:53:49 -08001674
Jason Gereckeccad85c2015-08-03 10:17:04 -07001675 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001676 (features->quirks & WACOM_QUIRK_BATTERY)) {
1677 error = wacom_initialize_battery(wacom);
1678 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001679 goto fail_battery;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001680 }
1681
Jason Gerecke3f14a632015-08-03 10:17:05 -07001682 error = wacom_register_inputs(wacom);
1683 if (error)
1684 goto fail_register_inputs;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001685
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001686 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02001687 error = wacom_initialize_leds(wacom);
1688 if (error)
1689 goto fail_leds;
1690
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001691 error = wacom_initialize_remote(wacom);
1692 if (error)
1693 goto fail_remote;
1694 }
1695
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001696 if (features->type == HID_GENERIC)
1697 connect_mask |= HID_CONNECT_DRIVER;
1698
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001699 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001700 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001701 if (error) {
1702 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001703 goto fail_hw_start;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001704 }
1705
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001706 if (!wireless) {
1707 /* Note that if query fails it is not a hard failure */
1708 wacom_query_tablet_data(hdev, features);
1709 }
Jason Gerecke86e88f02015-11-03 16:57:58 -08001710
1711 /* touch only Bamboo doesn't support pen */
1712 if ((features->type == BAMBOO_TOUCH) &&
1713 (features->device_type & WACOM_DEVICETYPE_PEN)) {
1714 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001715 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08001716 }
1717
1718 /* pen only Bamboo neither support touch nor pad */
1719 if ((features->type == BAMBOO_PEN) &&
1720 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
1721 (features->device_type & WACOM_DEVICETYPE_PAD))) {
1722 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001723 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08001724 }
1725
Jason Gereckeccad85c2015-08-03 10:17:04 -07001726 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001727 error = hid_hw_open(hdev);
1728
Ping Chengeda01da2015-09-23 13:51:15 -07001729 if ((wacom_wac->features.type == INTUOSHT ||
Benjamin Tissoires97f9afa2016-07-13 18:05:49 +02001730 wacom_wac->features.type == INTUOSHT2) &&
Ping Chengeda01da2015-09-23 13:51:15 -07001731 (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
Benjamin Tissoires97f9afa2016-07-13 18:05:49 +02001732 wacom_wac->shared->type = wacom_wac->features.type;
1733 wacom_wac->shared->touch_input = wacom_wac->touch_input;
Ping Cheng961794a2013-12-05 12:54:53 -08001734 }
1735
Ping Cheng3bea7332006-07-13 18:01:36 -07001736 return 0;
1737
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001738fail_quirks:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001739 hid_hw_stop(hdev);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001740fail_hw_start:
1741 kobject_put(wacom->remote_dir);
1742fail_remote:
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02001743 wacom_destroy_leds(wacom);
1744fail_leds:
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001745fail_register_inputs:
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001746fail_battery:
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -05001747 wacom_remove_shared_data(wacom);
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001748fail_shared_data:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001749fail_parsed:
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001750fail_allocate_inputs:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001751 return error;
1752}
1753
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001754static void wacom_wireless_work(struct work_struct *work)
1755{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02001756 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001757 struct usb_device *usbdev = wacom->usbdev;
1758 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1759 struct hid_device *hdev1, *hdev2;
1760 struct wacom *wacom1, *wacom2;
1761 struct wacom_wac *wacom_wac1, *wacom_wac2;
1762 int error;
1763
1764 /*
1765 * Regardless if this is a disconnect or a new tablet,
1766 * remove any existing input and battery devices.
1767 */
1768
1769 wacom_destroy_battery(wacom);
1770
1771 /* Stylus interface */
1772 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1773 wacom1 = hid_get_drvdata(hdev1);
1774 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02001775 wacom_destroy_leds(wacom1);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001776 wacom_clean_inputs(wacom1);
1777
1778 /* Touch interface */
1779 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1780 wacom2 = hid_get_drvdata(hdev2);
1781 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02001782 wacom_destroy_leds(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001783 wacom_clean_inputs(wacom2);
1784
1785 if (wacom_wac->pid == 0) {
1786 hid_info(wacom->hdev, "wireless tablet disconnected\n");
1787 wacom_wac1->shared->type = 0;
1788 } else {
1789 const struct hid_device_id *id = wacom_ids;
1790
1791 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1792 wacom_wac->pid);
1793
1794 while (id->bus) {
1795 if (id->vendor == USB_VENDOR_ID_WACOM &&
1796 id->product == wacom_wac->pid)
1797 break;
1798 id++;
1799 }
1800
1801 if (!id->bus) {
1802 hid_info(wacom->hdev, "ignoring unknown PID.\n");
1803 return;
1804 }
1805
1806 /* Stylus interface */
1807 wacom_wac1->features =
1808 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001809
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001810 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001811 hid_hw_stop(hdev1);
1812 error = wacom_parse_and_register(wacom1, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001813 if (error)
1814 goto fail;
1815
1816 /* Touch interface */
1817 if (wacom_wac1->features.touch_max ||
1818 (wacom_wac1->features.type >= INTUOSHT &&
1819 wacom_wac1->features.type <= BAMBOO_PT)) {
1820 wacom_wac2->features =
1821 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001822 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001823 hid_hw_stop(hdev2);
1824 error = wacom_parse_and_register(wacom2, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001825 if (error)
1826 goto fail;
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001827 }
1828
1829 error = wacom_initialize_battery(wacom);
1830 if (error)
1831 goto fail;
1832 }
1833
1834 return;
1835
1836fail:
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02001837 wacom_destroy_leds(wacom1);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001838 wacom_clean_inputs(wacom1);
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02001839 wacom_destroy_leds(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001840 wacom_clean_inputs(wacom2);
1841 return;
1842}
1843
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001844static int wacom_probe(struct hid_device *hdev,
1845 const struct hid_device_id *id)
1846{
1847 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1848 struct usb_device *dev = interface_to_usbdev(intf);
1849 struct wacom *wacom;
1850 struct wacom_wac *wacom_wac;
1851 struct wacom_features *features;
1852 int error;
1853
1854 if (!id->driver_data)
1855 return -EINVAL;
1856
1857 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1858
1859 /* hid-core sets this quirk for the boot interface */
1860 hdev->quirks &= ~HID_QUIRK_NOGET;
1861
1862 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1863 if (!wacom)
1864 return -ENOMEM;
1865
1866 hid_set_drvdata(hdev, wacom);
1867 wacom->hdev = hdev;
1868
1869 wacom_wac = &wacom->wacom_wac;
1870 wacom_wac->features = *((struct wacom_features *)id->driver_data);
1871 features = &wacom_wac->features;
1872
1873 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1874 error = -ENODEV;
1875 goto fail_type;
1876 }
1877
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07001878 wacom_wac->hid_data.inputmode = -1;
Jason Gerecke326ea2a2016-04-04 11:26:52 -07001879 wacom_wac->mode_report = -1;
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07001880
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001881 wacom->usbdev = dev;
1882 wacom->intf = intf;
1883 mutex_init(&wacom->lock);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02001884 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
1885 INIT_WORK(&wacom->battery_work, wacom_battery_work);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001886
1887 /* ask for the report descriptor to be loaded by HID */
1888 error = hid_parse(hdev);
1889 if (error) {
1890 hid_err(hdev, "parse failed\n");
1891 goto fail_parse;
1892 }
1893
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001894 error = wacom_parse_and_register(wacom, false);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001895 if (error)
1896 goto fail_parse;
1897
1898 if (hdev->bus == BUS_BLUETOOTH) {
1899 error = device_create_file(&hdev->dev, &dev_attr_speed);
1900 if (error)
1901 hid_warn(hdev,
1902 "can't create sysfs speed attribute err: %d\n",
1903 error);
1904 }
1905
1906 return 0;
1907
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001908fail_type:
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001909fail_parse:
1910 kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001911 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001912 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001913}
1914
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001915static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001916{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001917 struct wacom *wacom = hid_get_drvdata(hdev);
Benjamin Tissoiresf6205162016-02-12 17:27:45 +01001918 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1919 struct wacom_features *features = &wacom_wac->features;
1920
1921 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
1922 hid_hw_close(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001923
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001924 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001925
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02001926 cancel_work_sync(&wacom->wireless_work);
1927 cancel_work_sync(&wacom->battery_work);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02001928 kobject_put(wacom->remote_dir);
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02001929 wacom_destroy_leds(wacom);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001930 if (hdev->bus == BUS_BLUETOOTH)
1931 device_remove_file(&hdev->dev, &dev_attr_speed);
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -05001932 wacom_remove_shared_data(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001933
1934 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001935 kfree(wacom);
1936}
1937
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001938#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001939static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001940{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001941 struct wacom *wacom = hid_get_drvdata(hdev);
1942 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001943
1944 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001945
1946 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001947 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001948 wacom_led_control(wacom);
1949
Oliver Neukume7224092008-04-15 01:31:57 -04001950 mutex_unlock(&wacom->lock);
1951
1952 return 0;
1953}
1954
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001955static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001956{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001957 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001958}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001959#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04001960
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001961static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001962 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001963 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001964 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001965 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001966 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001967#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001968 .resume = wacom_resume,
1969 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001970#endif
1971 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001972};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001973module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07001974
1975MODULE_VERSION(DRIVER_VERSION);
1976MODULE_AUTHOR(DRIVER_AUTHOR);
1977MODULE_DESCRIPTION(DRIVER_DESC);
1978MODULE_LICENSE(DRIVER_LICENSE);