blob: ca15c7f59dc7cfdc6651c82e1586395e18ac23b4 [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
26
Ping Chenge0984bc2014-09-10 12:40:05 -070027#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
28#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
29
Ping Chengc64d8832014-09-10 12:41:04 -070030static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
31 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070032{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070033 int retval;
34
35 do {
Ping Chengc64d8832014-09-10 12:41:04 -070036 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070037 HID_REQ_GET_REPORT);
Jason Gereckeaef31562015-05-21 10:44:31 -070038 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
39
40 if (retval < 0)
41 hid_err(hdev, "wacom_get_report: ran out of retries "
42 "(last error = %d)\n", retval);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070043
44 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070045}
46
Przemo Firszt296b7372014-08-06 14:00:38 -070047static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
48 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070049{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070050 int retval;
51
52 do {
Przemo Firszt296b7372014-08-06 14:00:38 -070053 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070054 HID_REQ_SET_REPORT);
Jason Gereckeaef31562015-05-21 10:44:31 -070055 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
56
57 if (retval < 0)
58 hid_err(hdev, "wacom_set_report: ran out of retries "
59 "(last error = %d)\n", retval);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070060
61 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070062}
63
Benjamin Tissoires29b47392014-07-24 12:52:23 -070064static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
65 u8 *raw_data, int size)
Ping Cheng3bea7332006-07-13 18:01:36 -070066{
Benjamin Tissoires29b47392014-07-24 12:52:23 -070067 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070068
Benjamin Tissoires29b47392014-07-24 12:52:23 -070069 if (size > WACOM_PKGLEN_MAX)
70 return 1;
Ping Cheng3bea7332006-07-13 18:01:36 -070071
Benjamin Tissoires29b47392014-07-24 12:52:23 -070072 memcpy(wacom->wacom_wac.data, raw_data, size);
Ping Cheng3bea7332006-07-13 18:01:36 -070073
Benjamin Tissoires29b47392014-07-24 12:52:23 -070074 wacom_wac_irq(&wacom->wacom_wac, size);
75
76 return 0;
Ping Cheng3bea7332006-07-13 18:01:36 -070077}
78
Ping Cheng3bea7332006-07-13 18:01:36 -070079static int wacom_open(struct input_dev *dev)
80{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040081 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -070082
Benjamin Tissoiresdff67412014-12-01 11:52:40 -050083 return hid_hw_open(wacom->hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070084}
85
86static void wacom_close(struct input_dev *dev)
87{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040088 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -070089
Benjamin Tissoires29b47392014-07-24 12:52:23 -070090 hid_hw_close(wacom->hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070091}
92
Chris Bagwell16bf2882012-03-25 23:26:20 -070093/*
Benjamin Tissoires198fdee2014-07-24 13:03:05 -070094 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
Jason Gerecke115d5e12012-10-03 17:24:32 -070095 */
96static int wacom_calc_hid_res(int logical_extents, int physical_extents,
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -070097 unsigned unit, int exponent)
Jason Gerecke115d5e12012-10-03 17:24:32 -070098{
Benjamin Tissoires198fdee2014-07-24 13:03:05 -070099 struct hid_field field = {
100 .logical_maximum = logical_extents,
101 .physical_maximum = physical_extents,
102 .unit = unit,
103 .unit_exponent = exponent,
104 };
Jason Gerecke115d5e12012-10-03 17:24:32 -0700105
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700106 return hidinput_calc_abs_res(&field, ABS_X);
Jason Gerecke115d5e12012-10-03 17:24:32 -0700107}
108
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700109static void wacom_feature_mapping(struct hid_device *hdev,
110 struct hid_field *field, struct hid_usage *usage)
Chris Bagwell41343612011-10-26 22:32:52 -0700111{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700112 struct wacom *wacom = hid_get_drvdata(hdev);
113 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400114 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400115 u8 *data;
116 int ret;
Chris Bagwell41343612011-10-26 22:32:52 -0700117
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700118 switch (usage->hid) {
119 case HID_DG_CONTACTMAX:
120 /* leave touch_max as is if predefined */
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400121 if (!features->touch_max) {
122 /* read manually */
123 data = kzalloc(2, GFP_KERNEL);
124 if (!data)
125 break;
126 data[0] = field->report->id;
127 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700128 data, 2, WAC_CMD_RETRIES);
129 if (ret == 2) {
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400130 features->touch_max = data[1];
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700131 } else {
132 features->touch_max = 16;
133 hid_warn(hdev, "wacom_feature_mapping: "
134 "could not get HID_DG_CONTACTMAX, "
135 "defaulting to %d\n",
136 features->touch_max);
137 }
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400138 kfree(data);
139 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700140 break;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400141 case HID_DG_INPUTMODE:
142 /* Ignore if value index is out of bounds. */
143 if (usage->usage_index >= field->report_count) {
144 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
145 break;
146 }
147
148 hid_data->inputmode = field->report->id;
149 hid_data->inputmode_index = usage->usage_index;
150 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700151 }
152}
153
Chris Bagwell428f8582011-10-26 22:26:59 -0700154/*
155 * Interface Descriptor of wacom devices can be incomplete and
156 * inconsistent so wacom_features table is used to store stylus
157 * device's packet lengths, various maximum values, and tablet
158 * resolution based on product ID's.
159 *
160 * For devices that contain 2 interfaces, wacom_features table is
161 * inaccurate for the touch interface. Since the Interface Descriptor
162 * for touch interfaces has pretty complete data, this function exists
163 * to query tablet for this missing information instead of hard coding in
164 * an additional table.
165 *
166 * A typical Interface Descriptor for a stylus will contain a
167 * boot mouse application collection that is not of interest and this
168 * function will ignore it.
169 *
170 * It also contains a digitizer application collection that also is not
171 * of interest since any information it contains would be duplicate
172 * of what is in wacom_features. Usually it defines a report of an array
173 * of bytes that could be used as max length of the stylus packet returned.
174 * If it happens to define a Digitizer-Stylus Physical Collection then
175 * the X and Y logical values contain valid data but it is ignored.
176 *
177 * A typical Interface Descriptor for a touch interface will contain a
178 * Digitizer-Finger Physical Collection which will define both logical
179 * X/Y maximum as well as the physical size of tablet. Since touch
180 * interfaces haven't supported pressure or distance, this is enough
181 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700182 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700183 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
184 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700185 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700186static void wacom_usage_mapping(struct hid_device *hdev,
187 struct hid_field *field, struct hid_usage *usage)
188{
189 struct wacom *wacom = hid_get_drvdata(hdev);
190 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoiresd97a5522015-01-05 16:32:12 -0500191 bool finger = WACOM_FINGER_FIELD(field);
192 bool pen = WACOM_PEN_FIELD(field);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700193
194 /*
195 * Requiring Stylus Usage will ignore boot mouse
196 * X/Y values and some cases of invalid Digitizer X/Y
197 * values commonly reported.
198 */
Jason Gerecke042628a2015-04-30 17:51:54 -0700199 if (pen)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700200 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -0700201 else if (finger)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700202 features->device_type |= WACOM_DEVICETYPE_TOUCH;
Jason Gerecke042628a2015-04-30 17:51:54 -0700203 else
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700204 return;
205
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800206 /*
207 * Bamboo models do not support HID_DG_CONTACTMAX.
208 * And, Bamboo Pen only descriptor contains touch.
209 */
210 if (features->type != BAMBOO_PT) {
211 /* ISDv4 touch devices at least supports one touch point */
212 if (finger && !features->touch_max)
213 features->touch_max = 1;
214 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700215
216 switch (usage->hid) {
217 case HID_GD_X:
218 features->x_max = field->logical_maximum;
219 if (finger) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700220 features->x_phy = field->physical_maximum;
221 if (features->type != BAMBOO_PT) {
222 features->unit = field->unit;
223 features->unitExpo = field->unit_exponent;
224 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700225 }
226 break;
227 case HID_GD_Y:
228 features->y_max = field->logical_maximum;
229 if (finger) {
230 features->y_phy = field->physical_maximum;
231 if (features->type != BAMBOO_PT) {
232 features->unit = field->unit;
233 features->unitExpo = field->unit_exponent;
234 }
235 }
236 break;
237 case HID_DG_TIPPRESSURE:
238 if (pen)
239 features->pressure_max = field->logical_maximum;
240 break;
241 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400242
243 if (features->type == HID_GENERIC)
244 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700245}
246
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800247static void wacom_post_parse_hid(struct hid_device *hdev,
248 struct wacom_features *features)
249{
250 struct wacom *wacom = hid_get_drvdata(hdev);
251 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
252
253 if (features->type == HID_GENERIC) {
254 /* Any last-minute generic device setup */
255 if (features->touch_max > 1) {
256 input_mt_init_slots(wacom_wac->input, wacom_wac->features.touch_max,
257 INPUT_MT_DIRECT);
258 }
259 }
260}
261
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700262static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800263 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500264{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700265 struct hid_report_enum *rep_enum;
266 struct hid_report *hreport;
267 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500268
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700269 /* check features first */
270 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
271 list_for_each_entry(hreport, &rep_enum->report_list, list) {
272 for (i = 0; i < hreport->maxfield; i++) {
273 /* Ignore if report count is out of bounds. */
274 if (hreport->field[i]->report_count < 1)
275 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500276
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700277 for (j = 0; j < hreport->field[i]->maxusage; j++) {
278 wacom_feature_mapping(hdev, hreport->field[i],
279 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500280 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500281 }
282 }
283
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700284 /* now check the input usages */
285 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
286 list_for_each_entry(hreport, &rep_enum->report_list, list) {
287
288 if (!hreport->maxfield)
289 continue;
290
291 for (i = 0; i < hreport->maxfield; i++)
292 for (j = 0; j < hreport->field[i]->maxusage; j++)
293 wacom_usage_mapping(hdev, hreport->field[i],
294 hreport->field[i]->usage + j);
295 }
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800296
297 wacom_post_parse_hid(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -0500298}
299
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400300static int wacom_hid_set_device_mode(struct hid_device *hdev)
301{
302 struct wacom *wacom = hid_get_drvdata(hdev);
303 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
304 struct hid_report *r;
305 struct hid_report_enum *re;
306
307 if (hid_data->inputmode < 0)
308 return 0;
309
310 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
311 r = re->report_id_hash[hid_data->inputmode];
312 if (r) {
313 r->field[0]->value[hid_data->inputmode_index] = 2;
314 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
315 }
316 return 0;
317}
318
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700319static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
320 int length, int mode)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700321{
322 unsigned char *rep_data;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700323 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700324
Jason Gereckefe494bc2012-10-03 17:25:35 -0700325 rep_data = kzalloc(length, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700326 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800327 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700328
Jason Gereckefe494bc2012-10-03 17:25:35 -0700329 do {
Chris Bagwell9937c022013-01-23 19:37:34 -0800330 rep_data[0] = report_id;
331 rep_data[1] = mode;
332
Przemo Firszt296b7372014-08-06 14:00:38 -0700333 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
334 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700335 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700336 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700337 rep_data, length, 1);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700338 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700339
340 kfree(rep_data);
341
342 return error < 0 ? error : 0;
343}
344
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700345static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
346 struct wacom_features *features)
347{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700348 struct wacom *wacom = hid_get_drvdata(hdev);
349 int ret;
350 u8 rep_data[2];
351
352 switch (features->type) {
353 case GRAPHIRE_BT:
354 rep_data[0] = 0x03;
355 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700356 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
357 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700358
359 if (ret >= 0) {
360 rep_data[0] = speed == 0 ? 0x05 : 0x06;
361 rep_data[1] = 0x00;
362
363 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700364 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700365
366 if (ret >= 0) {
367 wacom->wacom_wac.bt_high_speed = speed;
368 return 0;
369 }
370 }
371
372 /*
373 * Note that if the raw queries fail, it's not a hard failure
374 * and it is safe to continue
375 */
376 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
377 rep_data[0], ret);
378 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700379 case INTUOS4WL:
380 if (speed == 1)
381 wacom->wacom_wac.bt_features &= ~0x20;
382 else
383 wacom->wacom_wac.bt_features |= 0x20;
384
385 rep_data[0] = 0x03;
386 rep_data[1] = wacom->wacom_wac.bt_features;
387
Przemo Firszt296b7372014-08-06 14:00:38 -0700388 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
389 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700390 if (ret >= 0)
391 wacom->wacom_wac.bt_high_speed = speed;
392 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700393 }
394
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700395 return 0;
396}
397
Jason Gereckefe494bc2012-10-03 17:25:35 -0700398/*
399 * Switch the tablet into its most-capable mode. Wacom tablets are
400 * typically configured to power-up in a mode which sends mouse-like
401 * reports to the OS. To get absolute position, pressure data, etc.
402 * from the tablet, it is necessary to switch the tablet out of this
403 * mode and into one which sends the full range of tablet data.
404 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700405static int wacom_query_tablet_data(struct hid_device *hdev,
406 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700407{
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700408 if (hdev->bus == BUS_BLUETOOTH)
409 return wacom_bt_query_tablet_data(hdev, 1, features);
410
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400411 if (features->type == HID_GENERIC)
412 return wacom_hid_set_device_mode(hdev);
413
Jason Gereckeaa86b182015-06-15 18:01:42 -0700414 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
Jason Gereckefe494bc2012-10-03 17:25:35 -0700415 if (features->type > TABLETPC) {
416 /* MT Tablet PC touch */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700417 return wacom_set_device_mode(hdev, 3, 4, 4);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700418 }
Jason Gerecke36d3c512013-09-20 09:47:35 -0700419 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700420 return wacom_set_device_mode(hdev, 18, 3, 2);
Jason Gereckeb1e42792012-10-21 00:38:04 -0700421 }
Ping Cheng500d4162015-01-27 13:30:03 -0800422 else if (features->type == WACOM_27QHDT) {
423 return wacom_set_device_mode(hdev, 131, 3, 2);
424 }
Benjamin Tissoires8c97a762015-02-26 11:28:50 -0500425 else if (features->type == BAMBOO_PAD) {
426 return wacom_set_device_mode(hdev, 2, 2, 2);
427 }
Jason Gereckeaa86b182015-06-15 18:01:42 -0700428 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
Jason Gereckefe494bc2012-10-03 17:25:35 -0700429 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700430 return wacom_set_device_mode(hdev, 2, 2, 2);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700431 }
432 }
433
434 return 0;
435}
436
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700437static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700438 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800439{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700440 struct wacom *wacom = hid_get_drvdata(hdev);
441 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800442
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700443 /* default features */
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700444 features->x_fuzz = 4;
445 features->y_fuzz = 4;
446 features->pressure_fuzz = 0;
447 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800448
Chris Bagwelld3825d52012-03-25 23:26:11 -0700449 /*
450 * The wireless device HID is basic and layout conflicts with
451 * other tablets (monitor and touch interface can look like pen).
452 * Skip the query for this type and modify defaults based on
453 * interface number.
454 */
455 if (features->type == WIRELESS) {
456 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
Jason Gereckeaa86b182015-06-15 18:01:42 -0700457 features->device_type = WACOM_DEVICETYPE_NONE;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700458 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
Jason Gereckeaa86b182015-06-15 18:01:42 -0700459 features->device_type |= WACOM_DEVICETYPE_TOUCH;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700460 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
461 }
462 }
463
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700464 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800465}
466
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700467struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700468 struct list_head list;
469 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700470 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700471 struct wacom_shared shared;
472};
473
474static LIST_HEAD(wacom_udev_list);
475static DEFINE_MUTEX(wacom_udev_list_lock);
476
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700477static bool wacom_are_sibling(struct hid_device *hdev,
478 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700479{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700480 struct wacom *wacom = hid_get_drvdata(hdev);
481 struct wacom_features *features = &wacom->wacom_wac.features;
482 int vid = features->oVid;
483 int pid = features->oPid;
484 int n1,n2;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700485
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700486 if (vid == 0 && pid == 0) {
487 vid = hdev->vendor;
488 pid = hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700489 }
490
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700491 if (vid != sibling->vendor || pid != sibling->product)
492 return false;
493
494 /* Compare the physical path. */
495 n1 = strrchr(hdev->phys, '.') - hdev->phys;
496 n2 = strrchr(sibling->phys, '.') - sibling->phys;
497 if (n1 != n2 || n1 <= 0 || n2 <= 0)
498 return false;
499
500 return !strncmp(hdev->phys, sibling->phys, n1);
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700501}
502
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700503static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700504{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700505 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700506
507 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700508 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700509 kref_get(&data->kref);
510 return data;
511 }
512 }
513
514 return NULL;
515}
516
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700517static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700518{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700519 struct wacom *wacom = hid_get_drvdata(hdev);
520 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
521 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700522 int retval = 0;
523
524 mutex_lock(&wacom_udev_list_lock);
525
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700526 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700527 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700528 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700529 if (!data) {
530 retval = -ENOMEM;
531 goto out;
532 }
533
534 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700535 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700536 list_add_tail(&data->list, &wacom_udev_list);
537 }
538
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700539 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700540
Jason Gereckeaa86b182015-06-15 18:01:42 -0700541 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500542 wacom_wac->shared->touch = hdev;
Jason Gereckeaa86b182015-06-15 18:01:42 -0700543 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500544 wacom_wac->shared->pen = hdev;
545
Ping Cheng4492eff2010-03-19 22:18:15 -0700546out:
547 mutex_unlock(&wacom_udev_list_lock);
548 return retval;
549}
550
551static void wacom_release_shared_data(struct kref *kref)
552{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700553 struct wacom_hdev_data *data =
554 container_of(kref, struct wacom_hdev_data, kref);
Ping Cheng4492eff2010-03-19 22:18:15 -0700555
556 mutex_lock(&wacom_udev_list_lock);
557 list_del(&data->list);
558 mutex_unlock(&wacom_udev_list_lock);
559
560 kfree(data);
561}
562
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500563static void wacom_remove_shared_data(struct wacom *wacom)
Ping Cheng4492eff2010-03-19 22:18:15 -0700564{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700565 struct wacom_hdev_data *data;
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500566 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Ping Cheng4492eff2010-03-19 22:18:15 -0700567
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500568 if (wacom_wac->shared) {
569 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
570 shared);
571
572 if (wacom_wac->shared->touch == wacom->hdev)
573 wacom_wac->shared->touch = NULL;
574 else if (wacom_wac->shared->pen == wacom->hdev)
575 wacom_wac->shared->pen = NULL;
576
Ping Cheng4492eff2010-03-19 22:18:15 -0700577 kref_put(&data->kref, wacom_release_shared_data);
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -0500578 wacom_wac->shared = NULL;
Ping Cheng4492eff2010-03-19 22:18:15 -0700579 }
580}
581
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700582static int wacom_led_control(struct wacom *wacom)
583{
584 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700585 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700586 unsigned char report_id = WAC_CMD_LED_CONTROL;
587 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700588
Ping Cheng912ca212014-09-10 12:41:31 -0700589 if (wacom->wacom_wac.pid) { /* wireless connected */
590 report_id = WAC_CMD_WL_LED_CONTROL;
591 buf_size = 13;
592 }
593 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700594 if (!buf)
595 return -ENOMEM;
596
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700597 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700598 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700599 /*
600 * Touch Ring and crop mark LED luminance may take on
601 * one of four values:
602 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
603 */
604 int ring_led = wacom->led.select[0] & 0x03;
605 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
606 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700607 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700608
Ping Cheng912ca212014-09-10 12:41:31 -0700609 buf[0] = report_id;
610 if (wacom->wacom_wac.pid) {
611 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
612 buf, buf_size, WAC_CMD_RETRIES);
613 buf[0] = report_id;
614 buf[4] = led_bits;
615 } else
616 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700617 }
618 else {
619 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700620
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700621 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
622 wacom->wacom_wac.features.type == WACOM_24HD)
623 led |= (wacom->led.select[1] << 4) | 0x40;
624
Ping Cheng912ca212014-09-10 12:41:31 -0700625 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700626 buf[1] = led;
627 buf[2] = wacom->led.llv;
628 buf[3] = wacom->led.hlv;
629 buf[4] = wacom->led.img_lum;
630 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700631
Ping Cheng912ca212014-09-10 12:41:31 -0700632 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700633 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700634 kfree(buf);
635
636 return retval;
637}
638
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700639static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
640 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700641{
642 unsigned char *buf;
643 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700644 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700645
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700646 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700647 if (!buf)
648 return -ENOMEM;
649
650 /* Send 'start' command */
651 buf[0] = WAC_CMD_ICON_START;
652 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700653 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
654 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700655 if (retval < 0)
656 goto out;
657
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700658 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700659 buf[1] = button_id & 0x07;
660 for (i = 0; i < 4; i++) {
661 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700662 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700663
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700664 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700665 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700666 if (retval < 0)
667 break;
668 }
669
670 /* Send 'stop' */
671 buf[0] = WAC_CMD_ICON_START;
672 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700673 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
674 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700675
676out:
677 kfree(buf);
678 return retval;
679}
680
Ping Cheng09e7d942011-10-04 23:51:14 -0700681static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700682 const char *buf, size_t count)
683{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700684 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700685 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700686 unsigned int id;
687 int err;
688
689 err = kstrtouint(buf, 10, &id);
690 if (err)
691 return err;
692
693 mutex_lock(&wacom->lock);
694
Ping Cheng09e7d942011-10-04 23:51:14 -0700695 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700696 err = wacom_led_control(wacom);
697
698 mutex_unlock(&wacom->lock);
699
700 return err < 0 ? err : count;
701}
702
Ping Cheng09e7d942011-10-04 23:51:14 -0700703#define DEVICE_LED_SELECT_ATTR(SET_ID) \
704static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
705 struct device_attribute *attr, const char *buf, size_t count) \
706{ \
707 return wacom_led_select_store(dev, SET_ID, buf, count); \
708} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700709static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
710 struct device_attribute *attr, char *buf) \
711{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700712 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700713 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700714 return scnprintf(buf, PAGE_SIZE, "%d\n", \
715 wacom->led.select[SET_ID]); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700716} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700717static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700718 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700719 wacom_led##SET_ID##_select_store)
720
721DEVICE_LED_SELECT_ATTR(0);
722DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700723
724static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
725 const char *buf, size_t count)
726{
727 unsigned int value;
728 int err;
729
730 err = kstrtouint(buf, 10, &value);
731 if (err)
732 return err;
733
734 mutex_lock(&wacom->lock);
735
736 *dest = value & 0x7f;
737 err = wacom_led_control(wacom);
738
739 mutex_unlock(&wacom->lock);
740
741 return err < 0 ? err : count;
742}
743
744#define DEVICE_LUMINANCE_ATTR(name, field) \
745static ssize_t wacom_##name##_luminance_store(struct device *dev, \
746 struct device_attribute *attr, const char *buf, size_t count) \
747{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700748 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700749 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700750 \
751 return wacom_luminance_store(wacom, &wacom->led.field, \
752 buf, count); \
753} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700754static ssize_t wacom_##name##_luminance_show(struct device *dev, \
755 struct device_attribute *attr, char *buf) \
756{ \
757 struct wacom *wacom = dev_get_drvdata(dev); \
758 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
759} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700760static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700761 wacom_##name##_luminance_show, \
762 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700763
764DEVICE_LUMINANCE_ATTR(status0, llv);
765DEVICE_LUMINANCE_ATTR(status1, hlv);
766DEVICE_LUMINANCE_ATTR(buttons, img_lum);
767
768static ssize_t wacom_button_image_store(struct device *dev, int button_id,
769 const char *buf, size_t count)
770{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700771 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700772 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700773 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700774 unsigned len;
775 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700776
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700777 if (hdev->bus == BUS_BLUETOOTH) {
778 len = 256;
779 xfer_id = WAC_CMD_ICON_BT_XFER;
780 } else {
781 len = 1024;
782 xfer_id = WAC_CMD_ICON_XFER;
783 }
784
785 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700786 return -EINVAL;
787
788 mutex_lock(&wacom->lock);
789
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700790 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700791
792 mutex_unlock(&wacom->lock);
793
794 return err < 0 ? err : count;
795}
796
797#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
798static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
799 struct device_attribute *attr, const char *buf, size_t count) \
800{ \
801 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
802} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700803static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700804 NULL, wacom_btnimg##BUTTON_ID##_store)
805
806DEVICE_BTNIMG_ATTR(0);
807DEVICE_BTNIMG_ATTR(1);
808DEVICE_BTNIMG_ATTR(2);
809DEVICE_BTNIMG_ATTR(3);
810DEVICE_BTNIMG_ATTR(4);
811DEVICE_BTNIMG_ATTR(5);
812DEVICE_BTNIMG_ATTR(6);
813DEVICE_BTNIMG_ATTR(7);
814
Ping Cheng09e7d942011-10-04 23:51:14 -0700815static struct attribute *cintiq_led_attrs[] = {
816 &dev_attr_status_led0_select.attr,
817 &dev_attr_status_led1_select.attr,
818 NULL
819};
820
821static struct attribute_group cintiq_led_attr_group = {
822 .name = "wacom_led",
823 .attrs = cintiq_led_attrs,
824};
825
826static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700827 &dev_attr_status0_luminance.attr,
828 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700829 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700830 &dev_attr_buttons_luminance.attr,
831 &dev_attr_button0_rawimg.attr,
832 &dev_attr_button1_rawimg.attr,
833 &dev_attr_button2_rawimg.attr,
834 &dev_attr_button3_rawimg.attr,
835 &dev_attr_button4_rawimg.attr,
836 &dev_attr_button5_rawimg.attr,
837 &dev_attr_button6_rawimg.attr,
838 &dev_attr_button7_rawimg.attr,
839 NULL
840};
841
Ping Cheng09e7d942011-10-04 23:51:14 -0700842static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700843 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700844 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700845};
846
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700847static struct attribute *intuos5_led_attrs[] = {
848 &dev_attr_status0_luminance.attr,
849 &dev_attr_status_led0_select.attr,
850 NULL
851};
852
853static struct attribute_group intuos5_led_attr_group = {
854 .name = "wacom_led",
855 .attrs = intuos5_led_attrs,
856};
857
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700858static int wacom_initialize_leds(struct wacom *wacom)
859{
860 int error;
861
Jason Gerecke862cf552015-06-15 18:01:43 -0700862 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
863 return 0;
864
Ping Cheng09e7d942011-10-04 23:51:14 -0700865 /* Initialize default values */
866 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700867 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700868 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700869 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700870 case INTUOS4L:
871 wacom->led.select[0] = 0;
872 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700873 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700874 wacom->led.hlv = 20;
875 wacom->led.img_lum = 10;
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700876 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700877 &intuos4_led_attr_group);
878 break;
879
Jason Gerecke246835f2011-12-12 00:12:04 -0800880 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700881 case WACOM_21UX2:
882 wacom->led.select[0] = 0;
883 wacom->led.select[1] = 0;
884 wacom->led.llv = 0;
885 wacom->led.hlv = 0;
886 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700887
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700888 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700889 &cintiq_led_attr_group);
890 break;
891
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700892 case INTUOS5S:
893 case INTUOS5:
894 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700895 case INTUOSPS:
896 case INTUOSPM:
897 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -0700898 wacom->led.select[0] = 0;
899 wacom->led.select[1] = 0;
900 wacom->led.llv = 32;
901 wacom->led.hlv = 0;
902 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700903
Jason Gerecke862cf552015-06-15 18:01:43 -0700904 error = sysfs_create_group(&wacom->hdev->dev.kobj,
905 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700906 break;
907
Ping Cheng09e7d942011-10-04 23:51:14 -0700908 default:
909 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700910 }
911
Ping Cheng09e7d942011-10-04 23:51:14 -0700912 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700913 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -0700914 "cannot create sysfs group err: %d\n", error);
915 return error;
916 }
917 wacom_led_control(wacom);
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700918 wacom->led_initialized = true;
Ping Cheng09e7d942011-10-04 23:51:14 -0700919
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700920 return 0;
921}
922
923static void wacom_destroy_leds(struct wacom *wacom)
924{
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700925 if (!wacom->led_initialized)
926 return;
927
Jason Gerecke862cf552015-06-15 18:01:43 -0700928 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
929 return;
930
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700931 wacom->led_initialized = false;
932
Ping Cheng09e7d942011-10-04 23:51:14 -0700933 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700934 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700935 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700936 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700937 case INTUOS4L:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700938 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700939 &intuos4_led_attr_group);
940 break;
941
Jason Gerecke246835f2011-12-12 00:12:04 -0800942 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700943 case WACOM_21UX2:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700944 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700945 &cintiq_led_attr_group);
946 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700947
948 case INTUOS5S:
949 case INTUOS5:
950 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700951 case INTUOSPS:
952 case INTUOSPM:
953 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -0700954 sysfs_remove_group(&wacom->hdev->dev.kobj,
955 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700956 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700957 }
958}
959
Chris Bagwella1d552c2012-03-25 23:26:30 -0700960static enum power_supply_property wacom_battery_props[] = {
Jason Gerecke71fa6412015-03-11 10:25:41 -0700961 POWER_SUPPLY_PROP_PRESENT,
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700962 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700963 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700964 POWER_SUPPLY_PROP_CAPACITY
965};
966
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700967static enum power_supply_property wacom_ac_props[] = {
968 POWER_SUPPLY_PROP_PRESENT,
969 POWER_SUPPLY_PROP_ONLINE,
970 POWER_SUPPLY_PROP_SCOPE,
971};
972
Chris Bagwella1d552c2012-03-25 23:26:30 -0700973static int wacom_battery_get_property(struct power_supply *psy,
974 enum power_supply_property psp,
975 union power_supply_propval *val)
976{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100977 struct wacom *wacom = power_supply_get_drvdata(psy);
Chris Bagwella1d552c2012-03-25 23:26:30 -0700978 int ret = 0;
979
980 switch (psp) {
Jason Gerecke71fa6412015-03-11 10:25:41 -0700981 case POWER_SUPPLY_PROP_PRESENT:
982 val->intval = wacom->wacom_wac.bat_connected;
983 break;
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700984 case POWER_SUPPLY_PROP_SCOPE:
985 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
986 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700987 case POWER_SUPPLY_PROP_CAPACITY:
988 val->intval =
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700989 wacom->wacom_wac.battery_capacity;
990 break;
991 case POWER_SUPPLY_PROP_STATUS:
992 if (wacom->wacom_wac.bat_charging)
993 val->intval = POWER_SUPPLY_STATUS_CHARGING;
994 else if (wacom->wacom_wac.battery_capacity == 100 &&
995 wacom->wacom_wac.ps_connected)
996 val->intval = POWER_SUPPLY_STATUS_FULL;
Jason Gereckeb0882cb2015-03-06 11:47:43 -0800997 else if (wacom->wacom_wac.ps_connected)
998 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700999 else
1000 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001001 break;
1002 default:
1003 ret = -EINVAL;
1004 break;
1005 }
1006
1007 return ret;
1008}
1009
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001010static int wacom_ac_get_property(struct power_supply *psy,
1011 enum power_supply_property psp,
1012 union power_supply_propval *val)
1013{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001014 struct wacom *wacom = power_supply_get_drvdata(psy);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001015 int ret = 0;
1016
1017 switch (psp) {
1018 case POWER_SUPPLY_PROP_PRESENT:
1019 /* fall through */
1020 case POWER_SUPPLY_PROP_ONLINE:
1021 val->intval = wacom->wacom_wac.ps_connected;
1022 break;
1023 case POWER_SUPPLY_PROP_SCOPE:
1024 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1025 break;
1026 default:
1027 ret = -EINVAL;
1028 break;
1029 }
1030 return ret;
1031}
1032
Chris Bagwella1d552c2012-03-25 23:26:30 -07001033static int wacom_initialize_battery(struct wacom *wacom)
1034{
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001035 static atomic_t battery_no = ATOMIC_INIT(0);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001036 struct power_supply_config psy_cfg = { .drv_data = wacom, };
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001037 unsigned long n;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001038
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001039 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001040 struct power_supply_desc *bat_desc = &wacom->battery_desc;
1041 struct power_supply_desc *ac_desc = &wacom->ac_desc;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001042 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001043
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001044 bat_desc->properties = wacom_battery_props;
1045 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1046 bat_desc->get_property = wacom_battery_get_property;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -07001047 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001048 bat_desc->name = wacom->wacom_wac.bat_name;
1049 bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1050 bat_desc->use_for_apm = 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001051
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001052 ac_desc->properties = wacom_ac_props;
1053 ac_desc->num_properties = ARRAY_SIZE(wacom_ac_props);
1054 ac_desc->get_property = wacom_ac_get_property;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001055 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001056 ac_desc->name = wacom->wacom_wac.ac_name;
1057 ac_desc->type = POWER_SUPPLY_TYPE_MAINS;
1058 ac_desc->use_for_apm = 0;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001059
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001060 wacom->battery = power_supply_register(&wacom->hdev->dev,
1061 &wacom->battery_desc, &psy_cfg);
1062 if (IS_ERR(wacom->battery))
1063 return PTR_ERR(wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001064
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001065 power_supply_powers(wacom->battery, &wacom->hdev->dev);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001066
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001067 wacom->ac = power_supply_register(&wacom->hdev->dev,
1068 &wacom->ac_desc,
1069 &psy_cfg);
1070 if (IS_ERR(wacom->ac)) {
1071 power_supply_unregister(wacom->battery);
1072 return PTR_ERR(wacom->ac);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001073 }
1074
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001075 power_supply_powers(wacom->ac, &wacom->hdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001076 }
1077
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001078 return 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001079}
1080
1081static void wacom_destroy_battery(struct wacom *wacom)
1082{
Linus Torvalds8de29a32015-04-14 08:25:26 -08001083 if (wacom->battery) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001084 power_supply_unregister(wacom->battery);
1085 wacom->battery = NULL;
1086 power_supply_unregister(wacom->ac);
1087 wacom->ac = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001088 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001089}
1090
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001091static ssize_t wacom_show_speed(struct device *dev,
1092 struct device_attribute
1093 *attr, char *buf)
1094{
1095 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1096 struct wacom *wacom = hid_get_drvdata(hdev);
1097
1098 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1099}
1100
1101static ssize_t wacom_store_speed(struct device *dev,
1102 struct device_attribute *attr,
1103 const char *buf, size_t count)
1104{
1105 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1106 struct wacom *wacom = hid_get_drvdata(hdev);
1107 u8 new_speed;
1108
1109 if (kstrtou8(buf, 0, &new_speed))
1110 return -EINVAL;
1111
1112 if (new_speed != 0 && new_speed != 1)
1113 return -EINVAL;
1114
1115 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1116
1117 return count;
1118}
1119
Ping Chenge0984bc2014-09-10 12:40:05 -07001120static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001121 wacom_show_speed, wacom_store_speed);
1122
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001123static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001124{
1125 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001126 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001127 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001128
1129 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001130 if (!input_dev)
1131 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001132
1133 input_dev->name = wacom_wac->name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001134 input_dev->phys = hdev->phys;
1135 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001136 input_dev->open = wacom_open;
1137 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001138 input_dev->uniq = hdev->uniq;
1139 input_dev->id.bustype = hdev->bus;
1140 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001141 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001142 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001143 input_set_drvdata(input_dev, wacom);
1144
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001145 return input_dev;
1146}
1147
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001148static void wacom_free_inputs(struct wacom *wacom)
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001149{
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001150 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1151
1152 if (wacom_wac->input)
1153 input_free_device(wacom_wac->input);
1154 if (wacom_wac->pad_input)
1155 input_free_device(wacom_wac->pad_input);
1156 wacom_wac->input = NULL;
1157 wacom_wac->pad_input = NULL;
1158}
1159
1160static int wacom_allocate_inputs(struct wacom *wacom)
1161{
1162 struct input_dev *input_dev, *pad_input_dev;
1163 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1164
1165 input_dev = wacom_allocate_input(wacom);
1166 pad_input_dev = wacom_allocate_input(wacom);
1167 if (!input_dev || !pad_input_dev) {
1168 wacom_free_inputs(wacom);
1169 return -ENOMEM;
1170 }
1171
1172 wacom_wac->input = input_dev;
1173 wacom_wac->pad_input = pad_input_dev;
1174 wacom_wac->pad_input->name = wacom_wac->pad_name;
1175
1176 return 0;
1177}
1178
1179static void wacom_clean_inputs(struct wacom *wacom)
1180{
1181 if (wacom->wacom_wac.input) {
1182 if (wacom->wacom_wac.input_registered)
1183 input_unregister_device(wacom->wacom_wac.input);
1184 else
1185 input_free_device(wacom->wacom_wac.input);
1186 }
1187 if (wacom->wacom_wac.pad_input) {
Ping Cheng954df6a2014-11-20 16:31:12 -08001188 if (wacom->wacom_wac.pad_registered)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001189 input_unregister_device(wacom->wacom_wac.pad_input);
1190 else
1191 input_free_device(wacom->wacom_wac.pad_input);
1192 }
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001193 wacom->wacom_wac.input = NULL;
1194 wacom->wacom_wac.pad_input = NULL;
Ping Cheng912ca212014-09-10 12:41:31 -07001195 wacom_destroy_leds(wacom);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001196}
1197
1198static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001199{
1200 struct input_dev *input_dev, *pad_input_dev;
1201 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Jason Gerecke2636a3f2015-06-15 18:01:44 -07001202 struct wacom_features *features = &wacom_wac->features;
1203 int error = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001204
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001205 input_dev = wacom_wac->input;
1206 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001207
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001208 if (!input_dev || !pad_input_dev)
1209 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001210
Jason Gerecke2636a3f2015-06-15 18:01:44 -07001211 if (features->device_type & WACOM_DEVICETYPE_PEN)
1212 error = wacom_setup_pen_input_capabilities(input_dev, wacom_wac);
1213 if (!error && features->device_type & WACOM_DEVICETYPE_TOUCH)
1214 error = wacom_setup_touch_input_capabilities(input_dev, wacom_wac);
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001215 if (!error) {
1216 error = input_register_device(input_dev);
1217 if (error)
1218 return error;
Ping Cheng954df6a2014-11-20 16:31:12 -08001219 wacom_wac->input_registered = true;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001220 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001221
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001222 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1223 if (error) {
1224 /* no pad in use on this interface */
1225 input_free_device(pad_input_dev);
1226 wacom_wac->pad_input = NULL;
1227 pad_input_dev = NULL;
1228 } else {
1229 error = input_register_device(pad_input_dev);
1230 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001231 goto fail_register_pad_input;
Ping Cheng954df6a2014-11-20 16:31:12 -08001232 wacom_wac->pad_registered = true;
Ping Cheng912ca212014-09-10 12:41:31 -07001233
1234 error = wacom_initialize_leds(wacom);
1235 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001236 goto fail_leds;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001237 }
1238
Ping Cheng19635182012-04-29 21:09:18 -07001239 return 0;
1240
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001241fail_leds:
Ping Cheng912ca212014-09-10 12:41:31 -07001242 input_unregister_device(pad_input_dev);
1243 pad_input_dev = NULL;
Ping Cheng954df6a2014-11-20 16:31:12 -08001244 wacom_wac->pad_registered = false;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001245fail_register_pad_input:
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001246 input_unregister_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001247 wacom_wac->input = NULL;
Ping Cheng954df6a2014-11-20 16:31:12 -08001248 wacom_wac->input_registered = false;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001249 return error;
1250}
1251
Chris Bagwell16bf2882012-03-25 23:26:20 -07001252static void wacom_wireless_work(struct work_struct *work)
1253{
1254 struct wacom *wacom = container_of(work, struct wacom, work);
1255 struct usb_device *usbdev = wacom->usbdev;
1256 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001257 struct hid_device *hdev1, *hdev2;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001258 struct wacom *wacom1, *wacom2;
1259 struct wacom_wac *wacom_wac1, *wacom_wac2;
1260 int error;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001261
1262 /*
1263 * Regardless if this is a disconnect or a new tablet,
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001264 * remove any existing input and battery devices.
Chris Bagwell16bf2882012-03-25 23:26:20 -07001265 */
1266
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001267 wacom_destroy_battery(wacom);
1268
Chris Bagwell16bf2882012-03-25 23:26:20 -07001269 /* Stylus interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001270 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1271 wacom1 = hid_get_drvdata(hdev1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001272 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001273 wacom_clean_inputs(wacom1);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001274
1275 /* Touch interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001276 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1277 wacom2 = hid_get_drvdata(hdev2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001278 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001279 wacom_clean_inputs(wacom2);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001280
1281 if (wacom_wac->pid == 0) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001282 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001283 wacom_wac1->shared->type = 0;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001284 } else {
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001285 const struct hid_device_id *id = wacom_ids;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001286
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001287 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001288 wacom_wac->pid);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001289
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001290 while (id->bus) {
1291 if (id->vendor == USB_VENDOR_ID_WACOM &&
1292 id->product == wacom_wac->pid)
Chris Bagwell16bf2882012-03-25 23:26:20 -07001293 break;
1294 id++;
1295 }
1296
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001297 if (!id->bus) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001298 hid_info(wacom->hdev, "ignoring unknown PID.\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001299 return;
1300 }
1301
1302 /* Stylus interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001303 wacom_wac1->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001304 *((struct wacom_features *)id->driver_data);
Jason Gereckeaa86b182015-06-15 18:01:42 -07001305 wacom_wac1->features.device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke862cf552015-06-15 18:01:43 -07001306 if (wacom_wac1->features.type != INTUOSHT &&
1307 wacom_wac1->features.type != BAMBOO_PT)
1308 wacom_wac1->features.device_type |= WACOM_DEVICETYPE_PAD;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001309 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1310 wacom_wac1->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001311 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1312 wacom_wac1->features.name);
Ping Cheng961794a2013-12-05 12:54:53 -08001313 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1314 wacom_wac1->shared->type = wacom_wac1->features.type;
Ping Cheng912ca212014-09-10 12:41:31 -07001315 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001316 error = wacom_allocate_inputs(wacom1) ||
1317 wacom_register_inputs(wacom1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001318 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001319 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001320
1321 /* Touch interface */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001322 if (wacom_wac1->features.touch_max ||
1323 wacom_wac1->features.type == INTUOSHT) {
Ping Cheng57bcfce2013-10-15 23:44:00 -07001324 wacom_wac2->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001325 *((struct wacom_features *)id->driver_data);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001326 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001327 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
Jason Gerecke862cf552015-06-15 18:01:43 -07001328 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1329 "%s (WL) Finger",wacom_wac2->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001330 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
Jason Gerecke862cf552015-06-15 18:01:43 -07001331 "%s (WL) Pad",wacom_wac2->features.name);
1332 if (wacom_wac1->features.touch_max)
1333 wacom_wac2->features.device_type |= WACOM_DEVICETYPE_TOUCH;
1334 if (wacom_wac1->features.type == INTUOSHT ||
1335 wacom_wac1->features.type == BAMBOO_PT)
1336 wacom_wac2->features.device_type |= WACOM_DEVICETYPE_PAD;
Ping Cheng912ca212014-09-10 12:41:31 -07001337 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001338 error = wacom_allocate_inputs(wacom2) ||
1339 wacom_register_inputs(wacom2);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001340 if (error)
1341 goto fail;
Ping Cheng961794a2013-12-05 12:54:53 -08001342
1343 if (wacom_wac1->features.type == INTUOSHT &&
1344 wacom_wac1->features.touch_max)
1345 wacom_wac->shared->touch_input = wacom_wac2->input;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001346 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001347
1348 error = wacom_initialize_battery(wacom);
1349 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001350 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001351 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001352
1353 return;
1354
Ping Cheng57bcfce2013-10-15 23:44:00 -07001355fail:
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001356 wacom_clean_inputs(wacom1);
1357 wacom_clean_inputs(wacom2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001358 return;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001359}
1360
Jason Gereckefce99572015-03-06 11:47:40 -08001361void wacom_battery_work(struct work_struct *work)
1362{
1363 struct wacom *wacom = container_of(work, struct wacom, work);
1364
1365 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Linus Torvalds8de29a32015-04-14 08:25:26 -08001366 !wacom->battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001367 wacom_initialize_battery(wacom);
1368 }
1369 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Linus Torvalds8de29a32015-04-14 08:25:26 -08001370 wacom->battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08001371 wacom_destroy_battery(wacom);
1372 }
1373}
1374
Ping Cheng401d7d12013-07-28 00:38:54 -07001375/*
1376 * Not all devices report physical dimensions from HID.
1377 * Compute the default from hardcoded logical dimension
1378 * and resolution before driver overwrites them.
1379 */
1380static void wacom_set_default_phy(struct wacom_features *features)
1381{
1382 if (features->x_resolution) {
1383 features->x_phy = (features->x_max * 100) /
1384 features->x_resolution;
1385 features->y_phy = (features->y_max * 100) /
1386 features->y_resolution;
1387 }
1388}
1389
1390static void wacom_calculate_res(struct wacom_features *features)
1391{
Ping Cheng3d64f542015-04-15 16:54:14 -07001392 /* set unit to "100th of a mm" for devices not reported by HID */
1393 if (!features->unit) {
1394 features->unit = 0x11;
1395 features->unitExpo = -3;
1396 }
1397
Ping Cheng401d7d12013-07-28 00:38:54 -07001398 features->x_resolution = wacom_calc_hid_res(features->x_max,
1399 features->x_phy,
1400 features->unit,
1401 features->unitExpo);
1402 features->y_resolution = wacom_calc_hid_res(features->y_max,
1403 features->y_phy,
1404 features->unit,
1405 features->unitExpo);
1406}
1407
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001408static size_t wacom_compute_pktlen(struct hid_device *hdev)
1409{
1410 struct hid_report_enum *report_enum;
1411 struct hid_report *report;
1412 size_t size = 0;
1413
1414 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1415
1416 list_for_each_entry(report, &report_enum->report_list, list) {
Mathieu Magnaudetdabb05c62014-11-27 16:02:36 +01001417 size_t report_size = hid_report_len(report);
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001418 if (report_size > size)
1419 size = report_size;
1420 }
1421
1422 return size;
1423}
1424
Ping Chengc24eab42015-04-24 15:32:51 -07001425static void wacom_update_name(struct wacom *wacom)
1426{
1427 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1428 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke44b52502015-06-15 18:01:41 -07001429 char name[WACOM_NAME_MAX];
Ping Chengc24eab42015-04-24 15:32:51 -07001430
1431 /* Generic devices name unspecified */
1432 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1433 if (strstr(wacom->hdev->name, "Wacom") ||
1434 strstr(wacom->hdev->name, "wacom") ||
1435 strstr(wacom->hdev->name, "WACOM")) {
1436 /* name is in HID descriptor, use it */
Jason Gerecke44b52502015-06-15 18:01:41 -07001437 strlcpy(name, wacom->hdev->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001438
1439 /* strip out excess whitespaces */
1440 while (1) {
Jason Gerecke44b52502015-06-15 18:01:41 -07001441 char *gap = strstr(name, " ");
Ping Chengc24eab42015-04-24 15:32:51 -07001442 if (gap == NULL)
1443 break;
1444 /* shift everything including the terminator */
1445 memmove(gap, gap+1, strlen(gap));
1446 }
1447 /* get rid of trailing whitespace */
Jason Gerecke44b52502015-06-15 18:01:41 -07001448 if (name[strlen(name)-1] == ' ')
1449 name[strlen(name)-1] = '\0';
Ping Chengc24eab42015-04-24 15:32:51 -07001450 } else {
1451 /* no meaningful name retrieved. use product ID */
Jason Gerecke44b52502015-06-15 18:01:41 -07001452 snprintf(name, sizeof(name),
Ping Chengc24eab42015-04-24 15:32:51 -07001453 "%s %X", features->name, wacom->hdev->product);
1454 }
1455 } else {
Jason Gerecke44b52502015-06-15 18:01:41 -07001456 strlcpy(name, features->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07001457 }
1458
1459 /* Append the device type to the name */
1460 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
Jason Gerecke44b52502015-06-15 18:01:41 -07001461 "%s Pad", name);
Ping Chengc24eab42015-04-24 15:32:51 -07001462
Jason Gereckeaa86b182015-06-15 18:01:42 -07001463 if (features->device_type & WACOM_DEVICETYPE_PEN) {
Jason Gerecke44b52502015-06-15 18:01:41 -07001464 snprintf(wacom_wac->name, sizeof(wacom_wac->name),
1465 "%s Pen", name);
Jason Gerecke8d80f792015-04-30 17:51:53 -07001466 }
Jason Gereckeaa86b182015-06-15 18:01:42 -07001467 else if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
Jason Gerecke862cf552015-06-15 18:01:43 -07001468 snprintf(wacom_wac->name, sizeof(wacom_wac->name),
1469 "%s Finger", name);
1470 }
1471 else if (features->device_type & WACOM_DEVICETYPE_PAD) {
1472 snprintf(wacom_wac->name, sizeof(wacom_wac->name),
1473 "%s Pad", name);
Jason Gerecke8d80f792015-04-30 17:51:53 -07001474 }
Ping Chengc24eab42015-04-24 15:32:51 -07001475}
1476
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001477static int wacom_probe(struct hid_device *hdev,
1478 const struct hid_device_id *id)
Ping Cheng3bea7332006-07-13 18:01:36 -07001479{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001480 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Ping Cheng3bea7332006-07-13 18:01:36 -07001481 struct usb_device *dev = interface_to_usbdev(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001482 struct wacom *wacom;
1483 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001484 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001485 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001486 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07001487
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001488 if (!id->driver_data)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001489 return -EINVAL;
1490
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -04001491 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1492
Benjamin Tissoires5fcad162015-03-05 17:36:54 -05001493 /* hid-core sets this quirk for the boot interface */
1494 hdev->quirks &= ~HID_QUIRK_NOGET;
1495
Ping Cheng3bea7332006-07-13 18:01:36 -07001496 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001497 if (!wacom)
1498 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001499
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001500 hid_set_drvdata(hdev, wacom);
1501 wacom->hdev = hdev;
1502
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001503 /* ask for the report descriptor to be loaded by HID */
1504 error = hid_parse(hdev);
1505 if (error) {
1506 hid_err(hdev, "parse failed\n");
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001507 goto fail_parse;
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001508 }
1509
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001510 wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001511 wacom_wac->features = *((struct wacom_features *)id->driver_data);
Jason Childse33da8a2010-02-17 22:38:31 -08001512 features = &wacom_wac->features;
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001513 features->pktlen = wacom_compute_pktlen(hdev);
Jason Childse33da8a2010-02-17 22:38:31 -08001514 if (features->pktlen > WACOM_PKGLEN_MAX) {
1515 error = -EINVAL;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001516 goto fail_pktlen;
Jason Childse33da8a2010-02-17 22:38:31 -08001517 }
1518
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001519 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1520 error = -ENODEV;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001521 goto fail_type;
Jason Childse33da8a2010-02-17 22:38:31 -08001522 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001523
Ping Cheng3bea7332006-07-13 18:01:36 -07001524 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001525 wacom->intf = intf;
1526 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001527 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001528
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001529 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1530 error = wacom_allocate_inputs(wacom);
1531 if (error)
1532 goto fail_allocate_inputs;
1533 }
1534
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05001535 /*
1536 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
1537 * into debug mode for the touch part.
1538 * We ignore the other interfaces.
1539 */
1540 if (features->type == BAMBOO_PAD) {
1541 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
1542 features->type = HID_GENERIC;
1543 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
1544 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
1545 error = -ENODEV;
1546 goto fail_shared_data;
1547 }
1548 }
1549
Ping Cheng401d7d12013-07-28 00:38:54 -07001550 /* set the default size in case we do not get them from hid */
1551 wacom_set_default_phy(features);
1552
Ping Chengf393ee22012-04-29 21:09:17 -07001553 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001554 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng42f4f272015-04-15 16:53:54 -07001555 wacom_setup_device_quirks(wacom);
Jason Gerecke042628a2015-04-30 17:51:54 -07001556
Jason Gereckeaa86b182015-06-15 18:01:42 -07001557 if (features->device_type == WACOM_DEVICETYPE_NONE &&
1558 features->type != WIRELESS) {
Jason Gerecke8e116d32015-04-30 17:51:55 -07001559 error = features->type == HID_GENERIC ? -ENODEV : 0;
1560
Jason Gerecke042628a2015-04-30 17:51:54 -07001561 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
Jason Gerecke8e116d32015-04-30 17:51:55 -07001562 hdev->name,
1563 error ? "Ignoring" : "Assuming pen");
1564
1565 if (error)
1566 goto fail_shared_data;
Jason Gerecke042628a2015-04-30 17:51:54 -07001567
Jason Gereckeaa86b182015-06-15 18:01:42 -07001568 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -07001569 }
1570
Ping Cheng401d7d12013-07-28 00:38:54 -07001571 wacom_calculate_res(features);
1572
Ping Chengc24eab42015-04-24 15:32:51 -07001573 wacom_update_name(wacom);
Ping Cheng4492eff2010-03-19 22:18:15 -07001574
Ping Chengf3586d22015-03-20 14:57:00 -07001575 error = wacom_add_shared_data(hdev);
1576 if (error)
1577 goto fail_shared_data;
Ping Cheng49b764a2010-02-20 00:53:49 -08001578
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001579 if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1580 (features->quirks & WACOM_QUIRK_BATTERY)) {
1581 error = wacom_initialize_battery(wacom);
1582 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001583 goto fail_battery;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001584 }
1585
Chris Bagwelld3825d52012-03-25 23:26:11 -07001586 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001587 error = wacom_register_inputs(wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001588 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001589 goto fail_register_inputs;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001590 }
1591
1592 if (hdev->bus == BUS_BLUETOOTH) {
1593 error = device_create_file(&hdev->dev, &dev_attr_speed);
1594 if (error)
1595 hid_warn(hdev,
1596 "can't create sysfs speed attribute err: %d\n",
1597 error);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001598 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001599
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001600 if (features->type == HID_GENERIC)
1601 connect_mask |= HID_CONNECT_DRIVER;
1602
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001603 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001604 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001605 if (error) {
1606 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001607 goto fail_hw_start;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001608 }
1609
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -04001610 /* Note that if query fails it is not a hard failure */
1611 wacom_query_tablet_data(hdev, features);
1612
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001613 if (features->quirks & WACOM_QUIRK_MONITOR)
1614 error = hid_hw_open(hdev);
1615
Jason Gerecke862cf552015-06-15 18:01:43 -07001616 if (wacom_wac->features.type == INTUOSHT &&
1617 wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
Ping Cheng961794a2013-12-05 12:54:53 -08001618 wacom_wac->shared->touch_input = wacom_wac->input;
1619 }
1620
Ping Cheng3bea7332006-07-13 18:01:36 -07001621 return 0;
1622
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001623fail_hw_start:
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001624 if (hdev->bus == BUS_BLUETOOTH)
1625 device_remove_file(&hdev->dev, &dev_attr_speed);
1626fail_register_inputs:
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001627 wacom_clean_inputs(wacom);
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001628 wacom_destroy_battery(wacom);
1629fail_battery:
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -05001630 wacom_remove_shared_data(wacom);
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001631fail_shared_data:
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001632 wacom_clean_inputs(wacom);
1633fail_allocate_inputs:
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001634fail_type:
1635fail_pktlen:
1636fail_parse:
1637 kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001638 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001639 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001640}
1641
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001642static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001643{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001644 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001645
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001646 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001647
Chris Bagwell16bf2882012-03-25 23:26:20 -07001648 cancel_work_sync(&wacom->work);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001649 wacom_clean_inputs(wacom);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001650 if (hdev->bus == BUS_BLUETOOTH)
1651 device_remove_file(&hdev->dev, &dev_attr_speed);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001652 wacom_destroy_battery(wacom);
Benjamin Tissoiresa97ac102015-02-25 11:43:39 -05001653 wacom_remove_shared_data(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001654
1655 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001656 kfree(wacom);
1657}
1658
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001659#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001660static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001661{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001662 struct wacom *wacom = hid_get_drvdata(hdev);
1663 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001664
1665 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001666
1667 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001668 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001669 wacom_led_control(wacom);
1670
Oliver Neukume7224092008-04-15 01:31:57 -04001671 mutex_unlock(&wacom->lock);
1672
1673 return 0;
1674}
1675
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001676static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001677{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001678 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001679}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001680#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04001681
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001682static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001683 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001684 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001685 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001686 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001687 .event = wacom_wac_event,
1688 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001689#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001690 .resume = wacom_resume,
1691 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001692#endif
1693 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001694};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001695module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07001696
1697MODULE_VERSION(DRIVER_VERSION);
1698MODULE_AUTHOR(DRIVER_AUTHOR);
1699MODULE_DESCRIPTION(DRIVER_DESC);
1700MODULE_LICENSE(DRIVER_LICENSE);