blob: 68a560957871c5a497e4e3d0229ca03dc359d8da [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;
Ping Chengf393ee22012-04-29 21:09:17 -0700155 }
156}
157
Chris Bagwell428f8582011-10-26 22:26:59 -0700158/*
159 * Interface Descriptor of wacom devices can be incomplete and
160 * inconsistent so wacom_features table is used to store stylus
161 * device's packet lengths, various maximum values, and tablet
162 * resolution based on product ID's.
163 *
164 * For devices that contain 2 interfaces, wacom_features table is
165 * inaccurate for the touch interface. Since the Interface Descriptor
166 * for touch interfaces has pretty complete data, this function exists
167 * to query tablet for this missing information instead of hard coding in
168 * an additional table.
169 *
170 * A typical Interface Descriptor for a stylus will contain a
171 * boot mouse application collection that is not of interest and this
172 * function will ignore it.
173 *
174 * It also contains a digitizer application collection that also is not
175 * of interest since any information it contains would be duplicate
176 * of what is in wacom_features. Usually it defines a report of an array
177 * of bytes that could be used as max length of the stylus packet returned.
178 * If it happens to define a Digitizer-Stylus Physical Collection then
179 * the X and Y logical values contain valid data but it is ignored.
180 *
181 * A typical Interface Descriptor for a touch interface will contain a
182 * Digitizer-Finger Physical Collection which will define both logical
183 * X/Y maximum as well as the physical size of tablet. Since touch
184 * interfaces haven't supported pressure or distance, this is enough
185 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700186 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700187 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
188 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700189 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700190static void wacom_usage_mapping(struct hid_device *hdev,
191 struct hid_field *field, struct hid_usage *usage)
192{
193 struct wacom *wacom = hid_get_drvdata(hdev);
194 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoiresd97a5522015-01-05 16:32:12 -0500195 bool finger = WACOM_FINGER_FIELD(field);
196 bool pen = WACOM_PEN_FIELD(field);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700197
198 /*
199 * Requiring Stylus Usage will ignore boot mouse
200 * X/Y values and some cases of invalid Digitizer X/Y
201 * values commonly reported.
202 */
Jason Gerecke042628a2015-04-30 17:51:54 -0700203 if (pen)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700204 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -0700205 else if (finger)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700206 features->device_type |= WACOM_DEVICETYPE_TOUCH;
Jason Gerecke042628a2015-04-30 17:51:54 -0700207 else
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700208 return;
209
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800210 /*
211 * Bamboo models do not support HID_DG_CONTACTMAX.
212 * And, Bamboo Pen only descriptor contains touch.
213 */
Ping Cheng3b164a02015-09-23 09:59:10 -0700214 if (features->type > BAMBOO_PT) {
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800215 /* ISDv4 touch devices at least supports one touch point */
216 if (finger && !features->touch_max)
217 features->touch_max = 1;
218 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700219
220 switch (usage->hid) {
221 case HID_GD_X:
222 features->x_max = field->logical_maximum;
223 if (finger) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700224 features->x_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700225 if ((features->type != BAMBOO_PT) &&
226 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700227 features->unit = field->unit;
228 features->unitExpo = field->unit_exponent;
229 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700230 }
231 break;
232 case HID_GD_Y:
233 features->y_max = field->logical_maximum;
234 if (finger) {
235 features->y_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700236 if ((features->type != BAMBOO_PT) &&
237 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700238 features->unit = field->unit;
239 features->unitExpo = field->unit_exponent;
240 }
241 }
242 break;
243 case HID_DG_TIPPRESSURE:
244 if (pen)
245 features->pressure_max = field->logical_maximum;
246 break;
247 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400248
249 if (features->type == HID_GENERIC)
250 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700251}
252
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800253static void wacom_post_parse_hid(struct hid_device *hdev,
254 struct wacom_features *features)
255{
256 struct wacom *wacom = hid_get_drvdata(hdev);
257 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
258
259 if (features->type == HID_GENERIC) {
260 /* Any last-minute generic device setup */
261 if (features->touch_max > 1) {
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -0700262 input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800263 INPUT_MT_DIRECT);
264 }
265 }
266}
267
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700268static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800269 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500270{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700271 struct hid_report_enum *rep_enum;
272 struct hid_report *hreport;
273 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500274
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700275 /* check features first */
276 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
277 list_for_each_entry(hreport, &rep_enum->report_list, list) {
278 for (i = 0; i < hreport->maxfield; i++) {
279 /* Ignore if report count is out of bounds. */
280 if (hreport->field[i]->report_count < 1)
281 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500282
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700283 for (j = 0; j < hreport->field[i]->maxusage; j++) {
284 wacom_feature_mapping(hdev, hreport->field[i],
285 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500286 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500287 }
288 }
289
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700290 /* now check the input usages */
291 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
292 list_for_each_entry(hreport, &rep_enum->report_list, list) {
293
294 if (!hreport->maxfield)
295 continue;
296
297 for (i = 0; i < hreport->maxfield; i++)
298 for (j = 0; j < hreport->field[i]->maxusage; j++)
299 wacom_usage_mapping(hdev, hreport->field[i],
300 hreport->field[i]->usage + j);
301 }
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800302
303 wacom_post_parse_hid(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -0500304}
305
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400306static int wacom_hid_set_device_mode(struct hid_device *hdev)
307{
308 struct wacom *wacom = hid_get_drvdata(hdev);
309 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
310 struct hid_report *r;
311 struct hid_report_enum *re;
312
313 if (hid_data->inputmode < 0)
314 return 0;
315
316 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
317 r = re->report_id_hash[hid_data->inputmode];
318 if (r) {
319 r->field[0]->value[hid_data->inputmode_index] = 2;
320 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
321 }
322 return 0;
323}
324
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700325static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
326 int length, int mode)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700327{
328 unsigned char *rep_data;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700329 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700330
Jason Gereckefe494bc2012-10-03 17:25:35 -0700331 rep_data = kzalloc(length, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700332 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800333 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700334
Jason Gereckefe494bc2012-10-03 17:25:35 -0700335 do {
Chris Bagwell9937c022013-01-23 19:37:34 -0800336 rep_data[0] = report_id;
337 rep_data[1] = mode;
338
Przemo Firszt296b7372014-08-06 14:00:38 -0700339 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
340 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700341 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700342 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700343 rep_data, length, 1);
Jason Gereckea1c173d2015-08-05 15:45:30 -0700344 } while (error >= 0 && rep_data[1] != mode && limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700345
346 kfree(rep_data);
347
348 return error < 0 ? error : 0;
349}
350
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700351static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
352 struct wacom_features *features)
353{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700354 struct wacom *wacom = hid_get_drvdata(hdev);
355 int ret;
356 u8 rep_data[2];
357
358 switch (features->type) {
359 case GRAPHIRE_BT:
360 rep_data[0] = 0x03;
361 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700362 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
363 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700364
365 if (ret >= 0) {
366 rep_data[0] = speed == 0 ? 0x05 : 0x06;
367 rep_data[1] = 0x00;
368
369 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700370 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700371
372 if (ret >= 0) {
373 wacom->wacom_wac.bt_high_speed = speed;
374 return 0;
375 }
376 }
377
378 /*
379 * Note that if the raw queries fail, it's not a hard failure
380 * and it is safe to continue
381 */
382 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
383 rep_data[0], ret);
384 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700385 case INTUOS4WL:
386 if (speed == 1)
387 wacom->wacom_wac.bt_features &= ~0x20;
388 else
389 wacom->wacom_wac.bt_features |= 0x20;
390
391 rep_data[0] = 0x03;
392 rep_data[1] = wacom->wacom_wac.bt_features;
393
Przemo Firszt296b7372014-08-06 14:00:38 -0700394 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
395 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700396 if (ret >= 0)
397 wacom->wacom_wac.bt_high_speed = speed;
398 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700399 }
400
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700401 return 0;
402}
403
Jason Gereckefe494bc2012-10-03 17:25:35 -0700404/*
405 * Switch the tablet into its most-capable mode. Wacom tablets are
406 * typically configured to power-up in a mode which sends mouse-like
407 * reports to the OS. To get absolute position, pressure data, etc.
408 * from the tablet, it is necessary to switch the tablet out of this
409 * mode and into one which sends the full range of tablet data.
410 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700411static int wacom_query_tablet_data(struct hid_device *hdev,
412 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700413{
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700414 if (hdev->bus == BUS_BLUETOOTH)
415 return wacom_bt_query_tablet_data(hdev, 1, features);
416
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400417 if (features->type == HID_GENERIC)
418 return wacom_hid_set_device_mode(hdev);
419
Jason Gereckeaa86b182015-06-15 18:01:42 -0700420 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
Jason Gereckefe494bc2012-10-03 17:25:35 -0700421 if (features->type > TABLETPC) {
422 /* MT Tablet PC touch */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700423 return wacom_set_device_mode(hdev, 3, 4, 4);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700424 }
Jason Gerecke6212aae2015-11-02 15:41:12 -0800425 else if (features->type == WACOM_24HDT) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700426 return wacom_set_device_mode(hdev, 18, 3, 2);
Jason Gereckeb1e42792012-10-21 00:38:04 -0700427 }
Ping Cheng500d4162015-01-27 13:30:03 -0800428 else if (features->type == WACOM_27QHDT) {
429 return wacom_set_device_mode(hdev, 131, 3, 2);
430 }
Benjamin Tissoires8c97a762015-02-26 11:28:50 -0500431 else if (features->type == BAMBOO_PAD) {
432 return wacom_set_device_mode(hdev, 2, 2, 2);
433 }
Jason Gereckeaa86b182015-06-15 18:01:42 -0700434 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
Ping Cheng7adb91b2015-10-26 14:29:12 -0700435 if (features->type <= BAMBOO_PT) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700436 return wacom_set_device_mode(hdev, 2, 2, 2);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700437 }
438 }
439
440 return 0;
441}
442
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700443static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700444 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800445{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700446 struct wacom *wacom = hid_get_drvdata(hdev);
447 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800448
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700449 /* default features */
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700450 features->x_fuzz = 4;
451 features->y_fuzz = 4;
452 features->pressure_fuzz = 0;
453 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800454
Chris Bagwelld3825d52012-03-25 23:26:11 -0700455 /*
456 * The wireless device HID is basic and layout conflicts with
457 * other tablets (monitor and touch interface can look like pen).
458 * Skip the query for this type and modify defaults based on
459 * interface number.
460 */
461 if (features->type == WIRELESS) {
Jason Gerecke3f14a632015-08-03 10:17:05 -0700462 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
Jason Gereckeccad85c2015-08-03 10:17:04 -0700463 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700464 else
Jason Gereckeaa86b182015-06-15 18:01:42 -0700465 features->device_type = WACOM_DEVICETYPE_NONE;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700466 return;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700467 }
468
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700469 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800470}
471
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700472struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700473 struct list_head list;
474 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700475 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700476 struct wacom_shared shared;
477};
478
479static LIST_HEAD(wacom_udev_list);
480static DEFINE_MUTEX(wacom_udev_list_lock);
481
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700482static bool wacom_are_sibling(struct hid_device *hdev,
483 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700484{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700485 struct wacom *wacom = hid_get_drvdata(hdev);
486 struct wacom_features *features = &wacom->wacom_wac.features;
487 int vid = features->oVid;
488 int pid = features->oPid;
489 int n1,n2;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700490
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700491 if (vid == 0 && pid == 0) {
492 vid = hdev->vendor;
493 pid = hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700494 }
495
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700496 if (vid != sibling->vendor || pid != sibling->product)
497 return false;
498
499 /* Compare the physical path. */
500 n1 = strrchr(hdev->phys, '.') - hdev->phys;
501 n2 = strrchr(sibling->phys, '.') - sibling->phys;
502 if (n1 != n2 || n1 <= 0 || n2 <= 0)
503 return false;
504
505 return !strncmp(hdev->phys, sibling->phys, n1);
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700506}
507
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700508static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700509{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700510 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700511
512 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700513 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700514 kref_get(&data->kref);
515 return data;
516 }
517 }
518
519 return NULL;
520}
521
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700522static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700523{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700524 struct wacom *wacom = hid_get_drvdata(hdev);
525 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
526 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700527 int retval = 0;
528
529 mutex_lock(&wacom_udev_list_lock);
530
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700531 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700532 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700533 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700534 if (!data) {
535 retval = -ENOMEM;
536 goto out;
537 }
538
539 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700540 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700541 list_add_tail(&data->list, &wacom_udev_list);
542 }
543
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700544 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700545
Jason Gereckeaa86b182015-06-15 18:01:42 -0700546 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500547 wacom_wac->shared->touch = hdev;
Jason Gereckeaa86b182015-06-15 18:01:42 -0700548 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500549 wacom_wac->shared->pen = hdev;
550
Ping Cheng4492eff2010-03-19 22:18:15 -0700551out:
552 mutex_unlock(&wacom_udev_list_lock);
553 return retval;
554}
555
556static void wacom_release_shared_data(struct kref *kref)
557{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700558 struct wacom_hdev_data *data =
559 container_of(kref, struct wacom_hdev_data, kref);
Ping Cheng4492eff2010-03-19 22:18:15 -0700560
561 mutex_lock(&wacom_udev_list_lock);
562 list_del(&data->list);
563 mutex_unlock(&wacom_udev_list_lock);
564
565 kfree(data);
566}
567
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500568static void wacom_remove_shared_data(struct wacom *wacom)
Ping Cheng4492eff2010-03-19 22:18:15 -0700569{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700570 struct wacom_hdev_data *data;
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500571 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Ping Cheng4492eff2010-03-19 22:18:15 -0700572
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500573 if (wacom_wac->shared) {
574 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
575 shared);
576
577 if (wacom_wac->shared->touch == wacom->hdev)
578 wacom_wac->shared->touch = NULL;
579 else if (wacom_wac->shared->pen == wacom->hdev)
580 wacom_wac->shared->pen = NULL;
581
Ping Cheng4492eff2010-03-19 22:18:15 -0700582 kref_put(&data->kref, wacom_release_shared_data);
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500583 wacom_wac->shared = NULL;
Ping Cheng4492eff2010-03-19 22:18:15 -0700584 }
585}
586
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700587static int wacom_led_control(struct wacom *wacom)
588{
589 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700590 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700591 unsigned char report_id = WAC_CMD_LED_CONTROL;
592 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700593
Ping Cheng912ca212014-09-10 12:41:31 -0700594 if (wacom->wacom_wac.pid) { /* wireless connected */
595 report_id = WAC_CMD_WL_LED_CONTROL;
596 buf_size = 13;
597 }
598 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700599 if (!buf)
600 return -ENOMEM;
601
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700602 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700603 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700604 /*
605 * Touch Ring and crop mark LED luminance may take on
606 * one of four values:
607 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
608 */
609 int ring_led = wacom->led.select[0] & 0x03;
610 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
611 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700612 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700613
Ping Cheng912ca212014-09-10 12:41:31 -0700614 buf[0] = report_id;
615 if (wacom->wacom_wac.pid) {
616 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
617 buf, buf_size, WAC_CMD_RETRIES);
618 buf[0] = report_id;
619 buf[4] = led_bits;
620 } else
621 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700622 }
623 else {
624 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700625
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700626 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
627 wacom->wacom_wac.features.type == WACOM_24HD)
628 led |= (wacom->led.select[1] << 4) | 0x40;
629
Ping Cheng912ca212014-09-10 12:41:31 -0700630 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700631 buf[1] = led;
632 buf[2] = wacom->led.llv;
633 buf[3] = wacom->led.hlv;
634 buf[4] = wacom->led.img_lum;
635 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700636
Ping Cheng912ca212014-09-10 12:41:31 -0700637 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700638 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700639 kfree(buf);
640
641 return retval;
642}
643
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700644static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
645 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700646{
647 unsigned char *buf;
648 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700649 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700650
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700651 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700652 if (!buf)
653 return -ENOMEM;
654
655 /* Send 'start' command */
656 buf[0] = WAC_CMD_ICON_START;
657 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700658 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
659 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700660 if (retval < 0)
661 goto out;
662
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700663 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700664 buf[1] = button_id & 0x07;
665 for (i = 0; i < 4; i++) {
666 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700667 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700668
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700669 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700670 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700671 if (retval < 0)
672 break;
673 }
674
675 /* Send 'stop' */
676 buf[0] = WAC_CMD_ICON_START;
677 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700678 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
679 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700680
681out:
682 kfree(buf);
683 return retval;
684}
685
Ping Cheng09e7d942011-10-04 23:51:14 -0700686static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700687 const char *buf, size_t count)
688{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800689 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700690 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700691 unsigned int id;
692 int err;
693
694 err = kstrtouint(buf, 10, &id);
695 if (err)
696 return err;
697
698 mutex_lock(&wacom->lock);
699
Ping Cheng09e7d942011-10-04 23:51:14 -0700700 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700701 err = wacom_led_control(wacom);
702
703 mutex_unlock(&wacom->lock);
704
705 return err < 0 ? err : count;
706}
707
Ping Cheng09e7d942011-10-04 23:51:14 -0700708#define DEVICE_LED_SELECT_ATTR(SET_ID) \
709static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
710 struct device_attribute *attr, const char *buf, size_t count) \
711{ \
712 return wacom_led_select_store(dev, SET_ID, buf, count); \
713} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700714static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
715 struct device_attribute *attr, char *buf) \
716{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800717 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700718 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700719 return scnprintf(buf, PAGE_SIZE, "%d\n", \
720 wacom->led.select[SET_ID]); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700721} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700722static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700723 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700724 wacom_led##SET_ID##_select_store)
725
726DEVICE_LED_SELECT_ATTR(0);
727DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700728
729static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
730 const char *buf, size_t count)
731{
732 unsigned int value;
733 int err;
734
735 err = kstrtouint(buf, 10, &value);
736 if (err)
737 return err;
738
739 mutex_lock(&wacom->lock);
740
741 *dest = value & 0x7f;
742 err = wacom_led_control(wacom);
743
744 mutex_unlock(&wacom->lock);
745
746 return err < 0 ? err : count;
747}
748
749#define DEVICE_LUMINANCE_ATTR(name, field) \
750static ssize_t wacom_##name##_luminance_store(struct device *dev, \
751 struct device_attribute *attr, const char *buf, size_t count) \
752{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800753 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700754 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700755 \
756 return wacom_luminance_store(wacom, &wacom->led.field, \
757 buf, count); \
758} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700759static ssize_t wacom_##name##_luminance_show(struct device *dev, \
760 struct device_attribute *attr, char *buf) \
761{ \
762 struct wacom *wacom = dev_get_drvdata(dev); \
763 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
764} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700765static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700766 wacom_##name##_luminance_show, \
767 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700768
769DEVICE_LUMINANCE_ATTR(status0, llv);
770DEVICE_LUMINANCE_ATTR(status1, hlv);
771DEVICE_LUMINANCE_ATTR(buttons, img_lum);
772
773static ssize_t wacom_button_image_store(struct device *dev, int button_id,
774 const char *buf, size_t count)
775{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800776 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700777 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700778 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700779 unsigned len;
780 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700781
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700782 if (hdev->bus == BUS_BLUETOOTH) {
783 len = 256;
784 xfer_id = WAC_CMD_ICON_BT_XFER;
785 } else {
786 len = 1024;
787 xfer_id = WAC_CMD_ICON_XFER;
788 }
789
790 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700791 return -EINVAL;
792
793 mutex_lock(&wacom->lock);
794
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700795 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700796
797 mutex_unlock(&wacom->lock);
798
799 return err < 0 ? err : count;
800}
801
802#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
803static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
804 struct device_attribute *attr, const char *buf, size_t count) \
805{ \
806 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
807} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700808static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700809 NULL, wacom_btnimg##BUTTON_ID##_store)
810
811DEVICE_BTNIMG_ATTR(0);
812DEVICE_BTNIMG_ATTR(1);
813DEVICE_BTNIMG_ATTR(2);
814DEVICE_BTNIMG_ATTR(3);
815DEVICE_BTNIMG_ATTR(4);
816DEVICE_BTNIMG_ATTR(5);
817DEVICE_BTNIMG_ATTR(6);
818DEVICE_BTNIMG_ATTR(7);
819
Ping Cheng09e7d942011-10-04 23:51:14 -0700820static struct attribute *cintiq_led_attrs[] = {
821 &dev_attr_status_led0_select.attr,
822 &dev_attr_status_led1_select.attr,
823 NULL
824};
825
826static struct attribute_group cintiq_led_attr_group = {
827 .name = "wacom_led",
828 .attrs = cintiq_led_attrs,
829};
830
831static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700832 &dev_attr_status0_luminance.attr,
833 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700834 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700835 &dev_attr_buttons_luminance.attr,
836 &dev_attr_button0_rawimg.attr,
837 &dev_attr_button1_rawimg.attr,
838 &dev_attr_button2_rawimg.attr,
839 &dev_attr_button3_rawimg.attr,
840 &dev_attr_button4_rawimg.attr,
841 &dev_attr_button5_rawimg.attr,
842 &dev_attr_button6_rawimg.attr,
843 &dev_attr_button7_rawimg.attr,
844 NULL
845};
846
Ping Cheng09e7d942011-10-04 23:51:14 -0700847static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700848 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700849 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700850};
851
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700852static struct attribute *intuos5_led_attrs[] = {
853 &dev_attr_status0_luminance.attr,
854 &dev_attr_status_led0_select.attr,
855 NULL
856};
857
858static struct attribute_group intuos5_led_attr_group = {
859 .name = "wacom_led",
860 .attrs = intuos5_led_attrs,
861};
862
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700863static int wacom_initialize_leds(struct wacom *wacom)
864{
865 int error;
866
Jason Gerecke862cf552015-06-15 18:01:43 -0700867 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
868 return 0;
869
Ping Cheng09e7d942011-10-04 23:51:14 -0700870 /* Initialize default values */
871 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700872 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700873 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700874 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700875 case INTUOS4L:
876 wacom->led.select[0] = 0;
877 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700878 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700879 wacom->led.hlv = 20;
880 wacom->led.img_lum = 10;
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700881 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700882 &intuos4_led_attr_group);
883 break;
884
Jason Gerecke246835f2011-12-12 00:12:04 -0800885 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700886 case WACOM_21UX2:
887 wacom->led.select[0] = 0;
888 wacom->led.select[1] = 0;
889 wacom->led.llv = 0;
890 wacom->led.hlv = 0;
891 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700892
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700893 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700894 &cintiq_led_attr_group);
895 break;
896
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700897 case INTUOS5S:
898 case INTUOS5:
899 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700900 case INTUOSPS:
901 case INTUOSPM:
902 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -0700903 wacom->led.select[0] = 0;
904 wacom->led.select[1] = 0;
905 wacom->led.llv = 32;
906 wacom->led.hlv = 0;
907 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700908
Jason Gerecke862cf552015-06-15 18:01:43 -0700909 error = sysfs_create_group(&wacom->hdev->dev.kobj,
910 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700911 break;
912
Ping Cheng09e7d942011-10-04 23:51:14 -0700913 default:
914 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700915 }
916
Ping Cheng09e7d942011-10-04 23:51:14 -0700917 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700918 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -0700919 "cannot create sysfs group err: %d\n", error);
920 return error;
921 }
922 wacom_led_control(wacom);
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700923 wacom->led_initialized = true;
Ping Cheng09e7d942011-10-04 23:51:14 -0700924
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700925 return 0;
926}
927
928static void wacom_destroy_leds(struct wacom *wacom)
929{
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700930 if (!wacom->led_initialized)
931 return;
932
Jason Gerecke862cf552015-06-15 18:01:43 -0700933 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
934 return;
935
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700936 wacom->led_initialized = false;
937
Ping Cheng09e7d942011-10-04 23:51:14 -0700938 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700939 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700940 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700941 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700942 case INTUOS4L:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700943 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700944 &intuos4_led_attr_group);
945 break;
946
Jason Gerecke246835f2011-12-12 00:12:04 -0800947 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700948 case WACOM_21UX2:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700949 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700950 &cintiq_led_attr_group);
951 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700952
953 case INTUOS5S:
954 case INTUOS5:
955 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700956 case INTUOSPS:
957 case INTUOSPM:
958 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -0700959 sysfs_remove_group(&wacom->hdev->dev.kobj,
960 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700961 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700962 }
963}
964
Chris Bagwella1d552c2012-03-25 23:26:30 -0700965static enum power_supply_property wacom_battery_props[] = {
Jason Gerecke71fa6412015-03-11 10:25:41 -0700966 POWER_SUPPLY_PROP_PRESENT,
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700967 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700968 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700969 POWER_SUPPLY_PROP_CAPACITY
970};
971
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700972static enum power_supply_property wacom_ac_props[] = {
973 POWER_SUPPLY_PROP_PRESENT,
974 POWER_SUPPLY_PROP_ONLINE,
975 POWER_SUPPLY_PROP_SCOPE,
976};
977
Chris Bagwella1d552c2012-03-25 23:26:30 -0700978static int wacom_battery_get_property(struct power_supply *psy,
979 enum power_supply_property psp,
980 union power_supply_propval *val)
981{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100982 struct wacom *wacom = power_supply_get_drvdata(psy);
Chris Bagwella1d552c2012-03-25 23:26:30 -0700983 int ret = 0;
984
985 switch (psp) {
Jason Gerecke71fa6412015-03-11 10:25:41 -0700986 case POWER_SUPPLY_PROP_PRESENT:
987 val->intval = wacom->wacom_wac.bat_connected;
988 break;
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700989 case POWER_SUPPLY_PROP_SCOPE:
990 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
991 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700992 case POWER_SUPPLY_PROP_CAPACITY:
993 val->intval =
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700994 wacom->wacom_wac.battery_capacity;
995 break;
996 case POWER_SUPPLY_PROP_STATUS:
997 if (wacom->wacom_wac.bat_charging)
998 val->intval = POWER_SUPPLY_STATUS_CHARGING;
999 else if (wacom->wacom_wac.battery_capacity == 100 &&
1000 wacom->wacom_wac.ps_connected)
1001 val->intval = POWER_SUPPLY_STATUS_FULL;
Jason Gereckeb0882cb2015-03-06 11:47:43 -08001002 else if (wacom->wacom_wac.ps_connected)
1003 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001004 else
1005 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001006 break;
1007 default:
1008 ret = -EINVAL;
1009 break;
1010 }
1011
1012 return ret;
1013}
1014
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001015static int wacom_ac_get_property(struct power_supply *psy,
1016 enum power_supply_property psp,
1017 union power_supply_propval *val)
1018{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001019 struct wacom *wacom = power_supply_get_drvdata(psy);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001020 int ret = 0;
1021
1022 switch (psp) {
1023 case POWER_SUPPLY_PROP_PRESENT:
1024 /* fall through */
1025 case POWER_SUPPLY_PROP_ONLINE:
1026 val->intval = wacom->wacom_wac.ps_connected;
1027 break;
1028 case POWER_SUPPLY_PROP_SCOPE:
1029 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1030 break;
1031 default:
1032 ret = -EINVAL;
1033 break;
1034 }
1035 return ret;
1036}
1037
Chris Bagwella1d552c2012-03-25 23:26:30 -07001038static int wacom_initialize_battery(struct wacom *wacom)
1039{
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001040 static atomic_t battery_no = ATOMIC_INIT(0);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001041 struct power_supply_config psy_cfg = { .drv_data = wacom, };
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001042 unsigned long n;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001043
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001044 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001045 struct power_supply_desc *bat_desc = &wacom->battery_desc;
1046 struct power_supply_desc *ac_desc = &wacom->ac_desc;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001047 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001048
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001049 bat_desc->properties = wacom_battery_props;
1050 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1051 bat_desc->get_property = wacom_battery_get_property;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001052 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001053 bat_desc->name = wacom->wacom_wac.bat_name;
1054 bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1055 bat_desc->use_for_apm = 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001056
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001057 ac_desc->properties = wacom_ac_props;
1058 ac_desc->num_properties = ARRAY_SIZE(wacom_ac_props);
1059 ac_desc->get_property = wacom_ac_get_property;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001060 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001061 ac_desc->name = wacom->wacom_wac.ac_name;
1062 ac_desc->type = POWER_SUPPLY_TYPE_MAINS;
1063 ac_desc->use_for_apm = 0;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001064
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001065 wacom->battery = power_supply_register(&wacom->hdev->dev,
1066 &wacom->battery_desc, &psy_cfg);
1067 if (IS_ERR(wacom->battery))
1068 return PTR_ERR(wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001069
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001070 power_supply_powers(wacom->battery, &wacom->hdev->dev);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001071
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001072 wacom->ac = power_supply_register(&wacom->hdev->dev,
1073 &wacom->ac_desc,
1074 &psy_cfg);
1075 if (IS_ERR(wacom->ac)) {
1076 power_supply_unregister(wacom->battery);
1077 return PTR_ERR(wacom->ac);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001078 }
1079
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001080 power_supply_powers(wacom->ac, &wacom->hdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001081 }
1082
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001083 return 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001084}
1085
1086static void wacom_destroy_battery(struct wacom *wacom)
1087{
Linus Torvalds8de29a32015-04-14 08:25:26 -08001088 if (wacom->battery) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001089 power_supply_unregister(wacom->battery);
1090 wacom->battery = NULL;
1091 power_supply_unregister(wacom->ac);
1092 wacom->ac = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001093 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001094}
1095
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001096static ssize_t wacom_show_speed(struct device *dev,
1097 struct device_attribute
1098 *attr, char *buf)
1099{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001100 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001101 struct wacom *wacom = hid_get_drvdata(hdev);
1102
1103 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1104}
1105
1106static ssize_t wacom_store_speed(struct device *dev,
1107 struct device_attribute *attr,
1108 const char *buf, size_t count)
1109{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001110 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001111 struct wacom *wacom = hid_get_drvdata(hdev);
1112 u8 new_speed;
1113
1114 if (kstrtou8(buf, 0, &new_speed))
1115 return -EINVAL;
1116
1117 if (new_speed != 0 && new_speed != 1)
1118 return -EINVAL;
1119
1120 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1121
1122 return count;
1123}
1124
Ping Chenge0984bc2014-09-10 12:40:05 -07001125static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001126 wacom_show_speed, wacom_store_speed);
1127
Aaron Skomra72b236d2015-08-20 16:05:17 -07001128
1129static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1130 struct kobj_attribute *kattr,
1131 char *buf, int index)
1132{
Geliang Tang2cf83832015-12-27 17:25:24 +08001133 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001134 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001135 struct wacom *wacom = hid_get_drvdata(hdev);
1136 u8 mode;
1137
1138 mode = wacom->led.select[index];
1139 if (mode >= 0 && mode < 3)
1140 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
1141 else
1142 return snprintf(buf, PAGE_SIZE, "%d\n", -1);
1143}
1144
1145#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1146static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1147 struct kobj_attribute *kattr, char *buf) \
1148{ \
1149 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1150} \
1151static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1152 .attr = {.name = "remote_mode", \
1153 .mode = DEV_ATTR_RO_PERM}, \
1154 .show = wacom_show_remote##SET_ID##_mode, \
1155}; \
1156static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1157 &remote##SET_ID##_mode_attr.attr, \
1158 NULL \
1159}; \
1160static struct attribute_group remote##SET_ID##_serial_group = { \
1161 .name = NULL, \
1162 .attrs = remote##SET_ID##_serial_attrs, \
1163}
1164
1165DEVICE_EKR_ATTR_GROUP(0);
1166DEVICE_EKR_ATTR_GROUP(1);
1167DEVICE_EKR_ATTR_GROUP(2);
1168DEVICE_EKR_ATTR_GROUP(3);
1169DEVICE_EKR_ATTR_GROUP(4);
1170
1171int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial, int index)
1172{
1173 int error = 0;
1174 char *buf;
1175 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1176
1177 wacom_wac->serial[index] = serial;
1178
1179 buf = kzalloc(WAC_REMOTE_SERIAL_MAX_STRLEN, GFP_KERNEL);
1180 if (!buf)
1181 return -ENOMEM;
1182 snprintf(buf, WAC_REMOTE_SERIAL_MAX_STRLEN, "%d", serial);
1183 wacom->remote_group[index].name = buf;
1184
1185 error = sysfs_create_group(wacom->remote_dir,
1186 &wacom->remote_group[index]);
1187 if (error) {
1188 hid_err(wacom->hdev,
1189 "cannot create sysfs group err: %d\n", error);
1190 kobject_put(wacom->remote_dir);
1191 return error;
1192 }
1193
1194 return 0;
1195}
1196
1197void wacom_remote_destroy_attr_group(struct wacom *wacom, __u32 serial)
1198{
1199 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1200 int i;
1201
1202 if (!serial)
1203 return;
1204
1205 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1206 if (wacom_wac->serial[i] == serial) {
1207 wacom_wac->serial[i] = 0;
1208 wacom->led.select[i] = WACOM_STATUS_UNKNOWN;
1209 if (wacom->remote_group[i].name) {
1210 sysfs_remove_group(wacom->remote_dir,
1211 &wacom->remote_group[i]);
1212 kfree(wacom->remote_group[i].name);
1213 wacom->remote_group[i].name = NULL;
1214 }
1215 }
1216 }
1217}
1218
1219static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1220{
1221 const size_t buf_size = 2;
1222 unsigned char *buf;
1223 int retval;
1224
1225 buf = kzalloc(buf_size, GFP_KERNEL);
1226 if (!buf)
1227 return -ENOMEM;
1228
1229 buf[0] = WAC_CMD_DELETE_PAIRING;
1230 buf[1] = selector;
1231
1232 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1233 buf_size, WAC_CMD_RETRIES);
1234 kfree(buf);
1235
1236 return retval;
1237}
1238
1239static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1240 struct kobj_attribute *attr,
1241 const char *buf, size_t count)
1242{
1243 unsigned char selector = 0;
Geliang Tang2cf83832015-12-27 17:25:24 +08001244 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001245 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001246 struct wacom *wacom = hid_get_drvdata(hdev);
1247 int err;
1248
1249 if (!strncmp(buf, "*\n", 2)) {
1250 selector = WAC_CMD_UNPAIR_ALL;
1251 } else {
1252 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1253 buf);
1254 return -1;
1255 }
1256
1257 mutex_lock(&wacom->lock);
1258
1259 err = wacom_cmd_unpair_remote(wacom, selector);
1260 mutex_unlock(&wacom->lock);
1261
1262 return err < 0 ? err : count;
1263}
1264
1265static struct kobj_attribute unpair_remote_attr = {
1266 .attr = {.name = "unpair_remote", .mode = 0200},
1267 .store = wacom_store_unpair_remote,
1268};
1269
1270static const struct attribute *remote_unpair_attrs[] = {
1271 &unpair_remote_attr.attr,
1272 NULL
1273};
1274
1275static int wacom_initialize_remote(struct wacom *wacom)
1276{
1277 int error = 0;
1278 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1279 int i;
1280
1281 if (wacom->wacom_wac.features.type != REMOTE)
1282 return 0;
1283
1284 wacom->remote_group[0] = remote0_serial_group;
1285 wacom->remote_group[1] = remote1_serial_group;
1286 wacom->remote_group[2] = remote2_serial_group;
1287 wacom->remote_group[3] = remote3_serial_group;
1288 wacom->remote_group[4] = remote4_serial_group;
1289
1290 wacom->remote_dir = kobject_create_and_add("wacom_remote",
1291 &wacom->hdev->dev.kobj);
1292 if (!wacom->remote_dir)
1293 return -ENOMEM;
1294
1295 error = sysfs_create_files(wacom->remote_dir, remote_unpair_attrs);
1296
1297 if (error) {
1298 hid_err(wacom->hdev,
1299 "cannot create sysfs group err: %d\n", error);
1300 return error;
1301 }
1302
1303 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1304 wacom->led.select[i] = WACOM_STATUS_UNKNOWN;
1305 wacom_wac->serial[i] = 0;
1306 }
1307
1308 return 0;
1309}
1310
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001311static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001312{
1313 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001314 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001315 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001316
1317 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001318 if (!input_dev)
1319 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001320
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001321 input_dev->name = wacom_wac->features.name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001322 input_dev->phys = hdev->phys;
1323 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001324 input_dev->open = wacom_open;
1325 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001326 input_dev->uniq = hdev->uniq;
1327 input_dev->id.bustype = hdev->bus;
1328 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001329 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001330 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001331 input_set_drvdata(input_dev, wacom);
1332
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001333 return input_dev;
1334}
1335
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001336static void wacom_clean_inputs(struct wacom *wacom)
1337{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001338 if (wacom->wacom_wac.pen_input) {
1339 if (wacom->wacom_wac.pen_registered)
1340 input_unregister_device(wacom->wacom_wac.pen_input);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001341 else
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001342 input_free_device(wacom->wacom_wac.pen_input);
1343 }
1344 if (wacom->wacom_wac.touch_input) {
1345 if (wacom->wacom_wac.touch_registered)
1346 input_unregister_device(wacom->wacom_wac.touch_input);
1347 else
1348 input_free_device(wacom->wacom_wac.touch_input);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001349 }
1350 if (wacom->wacom_wac.pad_input) {
Ping Cheng954df6a2014-11-20 16:31:12 -08001351 if (wacom->wacom_wac.pad_registered)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001352 input_unregister_device(wacom->wacom_wac.pad_input);
1353 else
1354 input_free_device(wacom->wacom_wac.pad_input);
1355 }
Markus Elfring27ba1d52015-11-06 18:00:30 +01001356 kobject_put(wacom->remote_dir);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001357 wacom->wacom_wac.pen_input = NULL;
1358 wacom->wacom_wac.touch_input = NULL;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001359 wacom->wacom_wac.pad_input = NULL;
Benjamin Tissoires17f28472016-02-12 17:27:44 +01001360 wacom->wacom_wac.pen_registered = false;
1361 wacom->wacom_wac.touch_registered = false;
1362 wacom->wacom_wac.pad_registered = false;
Ping Cheng912ca212014-09-10 12:41:31 -07001363 wacom_destroy_leds(wacom);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001364}
1365
Jason Gerecked9f2d202015-07-13 18:03:44 -07001366static int wacom_allocate_inputs(struct wacom *wacom)
1367{
1368 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1369
1370 wacom_wac->pen_input = wacom_allocate_input(wacom);
1371 wacom_wac->touch_input = wacom_allocate_input(wacom);
1372 wacom_wac->pad_input = wacom_allocate_input(wacom);
1373 if (!wacom_wac->pen_input || !wacom_wac->touch_input || !wacom_wac->pad_input) {
1374 wacom_clean_inputs(wacom);
1375 return -ENOMEM;
1376 }
1377
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001378 wacom_wac->pen_input->name = wacom_wac->pen_name;
Jason Gerecked9f2d202015-07-13 18:03:44 -07001379 wacom_wac->touch_input->name = wacom_wac->touch_name;
1380 wacom_wac->pad_input->name = wacom_wac->pad_name;
1381
1382 return 0;
1383}
1384
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001385static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001386{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001387 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001388 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Jason Gerecke2636a3f2015-06-15 18:01:44 -07001389 int error = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001390
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001391 pen_input_dev = wacom_wac->pen_input;
1392 touch_input_dev = wacom_wac->touch_input;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001393 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001394
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001395 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001396 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001397
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001398 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1399 if (error) {
1400 /* no pen in use on this interface */
1401 input_free_device(pen_input_dev);
1402 wacom_wac->pen_input = NULL;
1403 pen_input_dev = NULL;
1404 } else {
1405 error = input_register_device(pen_input_dev);
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001406 if (error)
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001407 goto fail_register_pen_input;
1408 wacom_wac->pen_registered = true;
1409 }
1410
1411 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1412 if (error) {
1413 /* no touch in use on this interface */
1414 input_free_device(touch_input_dev);
1415 wacom_wac->touch_input = NULL;
1416 touch_input_dev = NULL;
1417 } else {
1418 error = input_register_device(touch_input_dev);
1419 if (error)
1420 goto fail_register_touch_input;
1421 wacom_wac->touch_registered = true;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001422 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001423
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001424 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1425 if (error) {
1426 /* no pad in use on this interface */
1427 input_free_device(pad_input_dev);
1428 wacom_wac->pad_input = NULL;
1429 pad_input_dev = NULL;
1430 } else {
1431 error = input_register_device(pad_input_dev);
1432 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001433 goto fail_register_pad_input;
Ping Cheng954df6a2014-11-20 16:31:12 -08001434 wacom_wac->pad_registered = true;
Ping Cheng912ca212014-09-10 12:41:31 -07001435
1436 error = wacom_initialize_leds(wacom);
1437 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001438 goto fail_leds;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001439
1440 error = wacom_initialize_remote(wacom);
1441 if (error)
1442 goto fail_remote;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001443 }
1444
Ping Cheng19635182012-04-29 21:09:18 -07001445 return 0;
1446
Aaron Skomra72b236d2015-08-20 16:05:17 -07001447fail_remote:
1448 wacom_destroy_leds(wacom);
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001449fail_leds:
Ping Cheng912ca212014-09-10 12:41:31 -07001450 input_unregister_device(pad_input_dev);
1451 pad_input_dev = NULL;
Ping Cheng954df6a2014-11-20 16:31:12 -08001452 wacom_wac->pad_registered = false;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001453fail_register_pad_input:
Dan Carpenter0fd72ff2015-06-24 17:27:43 +03001454 if (touch_input_dev)
1455 input_unregister_device(touch_input_dev);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001456 wacom_wac->touch_input = NULL;
1457 wacom_wac->touch_registered = false;
1458fail_register_touch_input:
Dan Carpenter0fd72ff2015-06-24 17:27:43 +03001459 if (pen_input_dev)
1460 input_unregister_device(pen_input_dev);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001461 wacom_wac->pen_input = NULL;
1462 wacom_wac->pen_registered = false;
1463fail_register_pen_input:
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001464 return error;
1465}
1466
Jason Gerecke0be01712015-08-05 15:44:53 -07001467/*
1468 * Not all devices report physical dimensions from HID.
1469 * Compute the default from hardcoded logical dimension
1470 * and resolution before driver overwrites them.
1471 */
1472static void wacom_set_default_phy(struct wacom_features *features)
1473{
1474 if (features->x_resolution) {
1475 features->x_phy = (features->x_max * 100) /
1476 features->x_resolution;
1477 features->y_phy = (features->y_max * 100) /
1478 features->y_resolution;
1479 }
1480}
1481
1482static void wacom_calculate_res(struct wacom_features *features)
1483{
1484 /* set unit to "100th of a mm" for devices not reported by HID */
1485 if (!features->unit) {
1486 features->unit = 0x11;
1487 features->unitExpo = -3;
1488 }
1489
1490 features->x_resolution = wacom_calc_hid_res(features->x_max,
1491 features->x_phy,
1492 features->unit,
1493 features->unitExpo);
1494 features->y_resolution = wacom_calc_hid_res(features->y_max,
1495 features->y_phy,
1496 features->unit,
1497 features->unitExpo);
1498}
1499
Jason Gereckefce99572015-03-06 11:47:40 -08001500void wacom_battery_work(struct work_struct *work)
1501{
1502 struct wacom *wacom = container_of(work, struct wacom, work);
1503
1504 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Linus Torvalds8de29a32015-04-14 08:25:26 -08001505 !wacom->battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001506 wacom_initialize_battery(wacom);
1507 }
1508 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Linus Torvalds8de29a32015-04-14 08:25:26 -08001509 wacom->battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001510 wacom_destroy_battery(wacom);
1511 }
1512}
1513
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001514static size_t wacom_compute_pktlen(struct hid_device *hdev)
1515{
1516 struct hid_report_enum *report_enum;
1517 struct hid_report *report;
1518 size_t size = 0;
1519
1520 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1521
1522 list_for_each_entry(report, &report_enum->report_list, list) {
Mathieu Magnaudetdabb05c62014-11-27 16:02:36 +01001523 size_t report_size = hid_report_len(report);
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001524 if (report_size > size)
1525 size = report_size;
1526 }
1527
1528 return size;
1529}
1530
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001531static void wacom_update_name(struct wacom *wacom, const char *suffix)
Ping Chengc24eab42015-04-24 15:32:51 -07001532{
1533 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1534 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke44b52502015-06-15 18:01:41 -07001535 char name[WACOM_NAME_MAX];
Ping Chengc24eab42015-04-24 15:32:51 -07001536
1537 /* Generic devices name unspecified */
1538 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1539 if (strstr(wacom->hdev->name, "Wacom") ||
1540 strstr(wacom->hdev->name, "wacom") ||
1541 strstr(wacom->hdev->name, "WACOM")) {
1542 /* name is in HID descriptor, use it */
Jason Gerecke44b52502015-06-15 18:01:41 -07001543 strlcpy(name, wacom->hdev->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001544
1545 /* strip out excess whitespaces */
1546 while (1) {
Jason Gerecke44b52502015-06-15 18:01:41 -07001547 char *gap = strstr(name, " ");
Ping Chengc24eab42015-04-24 15:32:51 -07001548 if (gap == NULL)
1549 break;
1550 /* shift everything including the terminator */
1551 memmove(gap, gap+1, strlen(gap));
1552 }
1553 /* get rid of trailing whitespace */
Jason Gerecke44b52502015-06-15 18:01:41 -07001554 if (name[strlen(name)-1] == ' ')
1555 name[strlen(name)-1] = '\0';
Ping Chengc24eab42015-04-24 15:32:51 -07001556 } else {
1557 /* no meaningful name retrieved. use product ID */
Jason Gerecke44b52502015-06-15 18:01:41 -07001558 snprintf(name, sizeof(name),
Ping Chengc24eab42015-04-24 15:32:51 -07001559 "%s %X", features->name, wacom->hdev->product);
1560 }
1561 } else {
Jason Gerecke44b52502015-06-15 18:01:41 -07001562 strlcpy(name, features->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001563 }
1564
1565 /* Append the device type to the name */
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001566 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001567 "%s%s Pen", name, suffix);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001568 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001569 "%s%s Finger", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07001570 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001571 "%s%s Pad", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07001572}
1573
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001574static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
Ping Cheng3bea7332006-07-13 18:01:36 -07001575{
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001576 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1577 struct wacom_features *features = &wacom_wac->features;
1578 struct hid_device *hdev = wacom->hdev;
Jason Childse33da8a2010-02-17 22:38:31 -08001579 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001580 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07001581
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001582 features->pktlen = wacom_compute_pktlen(hdev);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001583 if (features->pktlen > WACOM_PKGLEN_MAX)
1584 return -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001585
Jason Gerecke3f14a632015-08-03 10:17:05 -07001586 error = wacom_allocate_inputs(wacom);
1587 if (error)
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001588 return error;
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001589
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05001590 /*
1591 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
1592 * into debug mode for the touch part.
1593 * We ignore the other interfaces.
1594 */
1595 if (features->type == BAMBOO_PAD) {
1596 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
1597 features->type = HID_GENERIC;
1598 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
1599 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
1600 error = -ENODEV;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001601 goto fail_allocate_inputs;
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05001602 }
1603 }
1604
Ping Cheng401d7d12013-07-28 00:38:54 -07001605 /* set the default size in case we do not get them from hid */
1606 wacom_set_default_phy(features);
1607
Ping Chengf393ee22012-04-29 21:09:17 -07001608 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001609 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng42f4f272015-04-15 16:53:54 -07001610 wacom_setup_device_quirks(wacom);
Jason Gerecke042628a2015-04-30 17:51:54 -07001611
Jason Gereckeaa86b182015-06-15 18:01:42 -07001612 if (features->device_type == WACOM_DEVICETYPE_NONE &&
1613 features->type != WIRELESS) {
Jason Gerecke8e116d32015-04-30 17:51:55 -07001614 error = features->type == HID_GENERIC ? -ENODEV : 0;
1615
Jason Gerecke042628a2015-04-30 17:51:54 -07001616 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
Jason Gerecke8e116d32015-04-30 17:51:55 -07001617 hdev->name,
1618 error ? "Ignoring" : "Assuming pen");
1619
1620 if (error)
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001621 goto fail_parsed;
Jason Gerecke042628a2015-04-30 17:51:54 -07001622
Jason Gereckeaa86b182015-06-15 18:01:42 -07001623 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -07001624 }
1625
Ping Cheng401d7d12013-07-28 00:38:54 -07001626 wacom_calculate_res(features);
1627
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001628 wacom_update_name(wacom, wireless ? " (WL)" : "");
Ping Cheng4492eff2010-03-19 22:18:15 -07001629
Ping Chengf3586d22015-03-20 14:57:00 -07001630 error = wacom_add_shared_data(hdev);
1631 if (error)
1632 goto fail_shared_data;
Ping Cheng49b764a2010-02-20 00:53:49 -08001633
Jason Gereckeccad85c2015-08-03 10:17:04 -07001634 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001635 (features->quirks & WACOM_QUIRK_BATTERY)) {
1636 error = wacom_initialize_battery(wacom);
1637 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001638 goto fail_battery;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001639 }
1640
Jason Gerecke3f14a632015-08-03 10:17:05 -07001641 error = wacom_register_inputs(wacom);
1642 if (error)
1643 goto fail_register_inputs;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001644
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001645 if (features->type == HID_GENERIC)
1646 connect_mask |= HID_CONNECT_DRIVER;
1647
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001648 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001649 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001650 if (error) {
1651 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001652 goto fail_hw_start;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001653 }
1654
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001655 if (!wireless) {
1656 /* Note that if query fails it is not a hard failure */
1657 wacom_query_tablet_data(hdev, features);
1658 }
Jason Gerecke86e88f02015-11-03 16:57:58 -08001659
1660 /* touch only Bamboo doesn't support pen */
1661 if ((features->type == BAMBOO_TOUCH) &&
1662 (features->device_type & WACOM_DEVICETYPE_PEN)) {
1663 error = -ENODEV;
1664 goto fail_hw_start;
1665 }
1666
1667 /* pen only Bamboo neither support touch nor pad */
1668 if ((features->type == BAMBOO_PEN) &&
1669 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
1670 (features->device_type & WACOM_DEVICETYPE_PAD))) {
1671 error = -ENODEV;
1672 goto fail_hw_start;
1673 }
1674
Jason Gereckeccad85c2015-08-03 10:17:04 -07001675 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001676 error = hid_hw_open(hdev);
1677
Ping Chengeda01da2015-09-23 13:51:15 -07001678 if ((wacom_wac->features.type == INTUOSHT ||
1679 wacom_wac->features.type == INTUOSHT2) &&
1680 (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07001681 wacom_wac->shared->touch_input = wacom_wac->touch_input;
Ping Cheng961794a2013-12-05 12:54:53 -08001682 }
1683
Ping Cheng3bea7332006-07-13 18:01:36 -07001684 return 0;
1685
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001686fail_hw_start:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001687 hid_hw_stop(hdev);
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001688fail_register_inputs:
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001689 wacom_clean_inputs(wacom);
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001690 wacom_destroy_battery(wacom);
1691fail_battery:
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -05001692 wacom_remove_shared_data(wacom);
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001693fail_shared_data:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001694fail_parsed:
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001695fail_allocate_inputs:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001696 wacom_clean_inputs(wacom);
1697 return error;
1698}
1699
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001700static void wacom_wireless_work(struct work_struct *work)
1701{
1702 struct wacom *wacom = container_of(work, struct wacom, work);
1703 struct usb_device *usbdev = wacom->usbdev;
1704 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1705 struct hid_device *hdev1, *hdev2;
1706 struct wacom *wacom1, *wacom2;
1707 struct wacom_wac *wacom_wac1, *wacom_wac2;
1708 int error;
1709
1710 /*
1711 * Regardless if this is a disconnect or a new tablet,
1712 * remove any existing input and battery devices.
1713 */
1714
1715 wacom_destroy_battery(wacom);
1716
1717 /* Stylus interface */
1718 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1719 wacom1 = hid_get_drvdata(hdev1);
1720 wacom_wac1 = &(wacom1->wacom_wac);
1721 wacom_clean_inputs(wacom1);
1722
1723 /* Touch interface */
1724 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1725 wacom2 = hid_get_drvdata(hdev2);
1726 wacom_wac2 = &(wacom2->wacom_wac);
1727 wacom_clean_inputs(wacom2);
1728
1729 if (wacom_wac->pid == 0) {
1730 hid_info(wacom->hdev, "wireless tablet disconnected\n");
1731 wacom_wac1->shared->type = 0;
1732 } else {
1733 const struct hid_device_id *id = wacom_ids;
1734
1735 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1736 wacom_wac->pid);
1737
1738 while (id->bus) {
1739 if (id->vendor == USB_VENDOR_ID_WACOM &&
1740 id->product == wacom_wac->pid)
1741 break;
1742 id++;
1743 }
1744
1745 if (!id->bus) {
1746 hid_info(wacom->hdev, "ignoring unknown PID.\n");
1747 return;
1748 }
1749
1750 /* Stylus interface */
1751 wacom_wac1->features =
1752 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001753
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001754 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001755 hid_hw_stop(hdev1);
1756 error = wacom_parse_and_register(wacom1, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001757 if (error)
1758 goto fail;
1759
1760 /* Touch interface */
1761 if (wacom_wac1->features.touch_max ||
1762 (wacom_wac1->features.type >= INTUOSHT &&
1763 wacom_wac1->features.type <= BAMBOO_PT)) {
1764 wacom_wac2->features =
1765 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001766 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001767 hid_hw_stop(hdev2);
1768 error = wacom_parse_and_register(wacom2, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001769 if (error)
1770 goto fail;
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01001771 }
1772
1773 error = wacom_initialize_battery(wacom);
1774 if (error)
1775 goto fail;
1776 }
1777
1778 return;
1779
1780fail:
1781 wacom_clean_inputs(wacom1);
1782 wacom_clean_inputs(wacom2);
1783 return;
1784}
1785
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001786static int wacom_probe(struct hid_device *hdev,
1787 const struct hid_device_id *id)
1788{
1789 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1790 struct usb_device *dev = interface_to_usbdev(intf);
1791 struct wacom *wacom;
1792 struct wacom_wac *wacom_wac;
1793 struct wacom_features *features;
1794 int error;
1795
1796 if (!id->driver_data)
1797 return -EINVAL;
1798
1799 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1800
1801 /* hid-core sets this quirk for the boot interface */
1802 hdev->quirks &= ~HID_QUIRK_NOGET;
1803
1804 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1805 if (!wacom)
1806 return -ENOMEM;
1807
1808 hid_set_drvdata(hdev, wacom);
1809 wacom->hdev = hdev;
1810
1811 wacom_wac = &wacom->wacom_wac;
1812 wacom_wac->features = *((struct wacom_features *)id->driver_data);
1813 features = &wacom_wac->features;
1814
1815 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1816 error = -ENODEV;
1817 goto fail_type;
1818 }
1819
1820 wacom->usbdev = dev;
1821 wacom->intf = intf;
1822 mutex_init(&wacom->lock);
1823 INIT_WORK(&wacom->work, wacom_wireless_work);
1824
1825 /* ask for the report descriptor to be loaded by HID */
1826 error = hid_parse(hdev);
1827 if (error) {
1828 hid_err(hdev, "parse failed\n");
1829 goto fail_parse;
1830 }
1831
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01001832 error = wacom_parse_and_register(wacom, false);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01001833 if (error)
1834 goto fail_parse;
1835
1836 if (hdev->bus == BUS_BLUETOOTH) {
1837 error = device_create_file(&hdev->dev, &dev_attr_speed);
1838 if (error)
1839 hid_warn(hdev,
1840 "can't create sysfs speed attribute err: %d\n",
1841 error);
1842 }
1843
1844 return 0;
1845
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001846fail_type:
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001847fail_parse:
1848 kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001849 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001850 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001851}
1852
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001853static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001854{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001855 struct wacom *wacom = hid_get_drvdata(hdev);
Benjamin Tissoiresf6205162016-02-12 17:27:45 +01001856 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1857 struct wacom_features *features = &wacom_wac->features;
1858
1859 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
1860 hid_hw_close(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001861
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001862 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001863
Chris Bagwell16bf2882012-03-25 23:26:20 -07001864 cancel_work_sync(&wacom->work);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001865 wacom_clean_inputs(wacom);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001866 if (hdev->bus == BUS_BLUETOOTH)
1867 device_remove_file(&hdev->dev, &dev_attr_speed);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001868 wacom_destroy_battery(wacom);
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -05001869 wacom_remove_shared_data(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001870
1871 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001872 kfree(wacom);
1873}
1874
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001875#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001876static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001877{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001878 struct wacom *wacom = hid_get_drvdata(hdev);
1879 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001880
1881 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001882
1883 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001884 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001885 wacom_led_control(wacom);
1886
Oliver Neukume7224092008-04-15 01:31:57 -04001887 mutex_unlock(&wacom->lock);
1888
1889 return 0;
1890}
1891
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001892static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001893{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001894 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001895}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001896#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04001897
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001898static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001899 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001900 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001901 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001902 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001903 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001904#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001905 .resume = wacom_resume,
1906 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001907#endif
1908 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001909};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001910module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07001911
1912MODULE_VERSION(DRIVER_VERSION);
1913MODULE_AUTHOR(DRIVER_AUTHOR);
1914MODULE_DESCRIPTION(DRIVER_DESC);
1915MODULE_LICENSE(DRIVER_LICENSE);