blob: eb5531657e4111cd4946e4a1d6705e0c9ca55eca [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"
Ping Cheng3bea7332006-07-13 18:01:36 -070016
Ping Chenga417ea42011-08-16 00:17:56 -070017#define WAC_MSG_RETRIES 5
Ping Cheng3bea7332006-07-13 18:01:36 -070018
Ping Cheng912ca212014-09-10 12:41:31 -070019#define WAC_CMD_WL_LED_CONTROL 0x03
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070020#define WAC_CMD_LED_CONTROL 0x20
21#define WAC_CMD_ICON_START 0x21
22#define WAC_CMD_ICON_XFER 0x23
Benjamin Tissoires849e2f02014-08-06 13:58:25 -070023#define WAC_CMD_ICON_BT_XFER 0x26
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070024#define WAC_CMD_RETRIES 10
25
Ping Chenge0984bc2014-09-10 12:40:05 -070026#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
27#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
28
Ping Chengc64d8832014-09-10 12:41:04 -070029static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
30 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070031{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070032 int retval;
33
34 do {
Ping Chengc64d8832014-09-10 12:41:04 -070035 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070036 HID_REQ_GET_REPORT);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070037 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
38
39 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070040}
41
Przemo Firszt296b7372014-08-06 14:00:38 -070042static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
43 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070044{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070045 int retval;
46
47 do {
Przemo Firszt296b7372014-08-06 14:00:38 -070048 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070049 HID_REQ_SET_REPORT);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070050 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
51
52 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070053}
54
Benjamin Tissoires29b47392014-07-24 12:52:23 -070055static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
56 u8 *raw_data, int size)
Ping Cheng3bea7332006-07-13 18:01:36 -070057{
Benjamin Tissoires29b47392014-07-24 12:52:23 -070058 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070059
Benjamin Tissoires29b47392014-07-24 12:52:23 -070060 if (size > WACOM_PKGLEN_MAX)
61 return 1;
Ping Cheng3bea7332006-07-13 18:01:36 -070062
Benjamin Tissoires29b47392014-07-24 12:52:23 -070063 memcpy(wacom->wacom_wac.data, raw_data, size);
Ping Cheng3bea7332006-07-13 18:01:36 -070064
Benjamin Tissoires29b47392014-07-24 12:52:23 -070065 wacom_wac_irq(&wacom->wacom_wac, size);
66
67 return 0;
Ping Cheng3bea7332006-07-13 18:01:36 -070068}
69
Ping Cheng3bea7332006-07-13 18:01:36 -070070static int wacom_open(struct input_dev *dev)
71{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040072 struct wacom *wacom = input_get_drvdata(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070073 int retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070074
Oliver Neukume7224092008-04-15 01:31:57 -040075 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070076 retval = hid_hw_open(wacom->hdev);
Oliver Neukume7224092008-04-15 01:31:57 -040077 mutex_unlock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070078
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -070079 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070080}
81
82static void wacom_close(struct input_dev *dev)
83{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040084 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -070085
Oliver Neukume7224092008-04-15 01:31:57 -040086 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070087 hid_hw_close(wacom->hdev);
Oliver Neukume7224092008-04-15 01:31:57 -040088 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -070089}
90
Chris Bagwell16bf2882012-03-25 23:26:20 -070091/*
Benjamin Tissoires198fdee2014-07-24 13:03:05 -070092 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
Jason Gerecke115d5e12012-10-03 17:24:32 -070093 */
94static int wacom_calc_hid_res(int logical_extents, int physical_extents,
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -070095 unsigned unit, int exponent)
Jason Gerecke115d5e12012-10-03 17:24:32 -070096{
Benjamin Tissoires198fdee2014-07-24 13:03:05 -070097 struct hid_field field = {
98 .logical_maximum = logical_extents,
99 .physical_maximum = physical_extents,
100 .unit = unit,
101 .unit_exponent = exponent,
102 };
Jason Gerecke115d5e12012-10-03 17:24:32 -0700103
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700104 return hidinput_calc_abs_res(&field, ABS_X);
Jason Gerecke115d5e12012-10-03 17:24:32 -0700105}
106
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700107static void wacom_feature_mapping(struct hid_device *hdev,
108 struct hid_field *field, struct hid_usage *usage)
Chris Bagwell41343612011-10-26 22:32:52 -0700109{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700110 struct wacom *wacom = hid_get_drvdata(hdev);
111 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400112 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400113 u8 *data;
114 int ret;
Chris Bagwell41343612011-10-26 22:32:52 -0700115
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700116 switch (usage->hid) {
117 case HID_DG_CONTACTMAX:
118 /* leave touch_max as is if predefined */
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400119 if (!features->touch_max) {
120 /* read manually */
121 data = kzalloc(2, GFP_KERNEL);
122 if (!data)
123 break;
124 data[0] = field->report->id;
125 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
126 data, 2, 0);
127 if (ret == 2)
128 features->touch_max = data[1];
129 kfree(data);
130 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700131 break;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400132 case HID_DG_INPUTMODE:
133 /* Ignore if value index is out of bounds. */
134 if (usage->usage_index >= field->report_count) {
135 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
136 break;
137 }
138
139 hid_data->inputmode = field->report->id;
140 hid_data->inputmode_index = usage->usage_index;
141 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700142 }
143}
144
Chris Bagwell428f8582011-10-26 22:26:59 -0700145/*
146 * Interface Descriptor of wacom devices can be incomplete and
147 * inconsistent so wacom_features table is used to store stylus
148 * device's packet lengths, various maximum values, and tablet
149 * resolution based on product ID's.
150 *
151 * For devices that contain 2 interfaces, wacom_features table is
152 * inaccurate for the touch interface. Since the Interface Descriptor
153 * for touch interfaces has pretty complete data, this function exists
154 * to query tablet for this missing information instead of hard coding in
155 * an additional table.
156 *
157 * A typical Interface Descriptor for a stylus will contain a
158 * boot mouse application collection that is not of interest and this
159 * function will ignore it.
160 *
161 * It also contains a digitizer application collection that also is not
162 * of interest since any information it contains would be duplicate
163 * of what is in wacom_features. Usually it defines a report of an array
164 * of bytes that could be used as max length of the stylus packet returned.
165 * If it happens to define a Digitizer-Stylus Physical Collection then
166 * the X and Y logical values contain valid data but it is ignored.
167 *
168 * A typical Interface Descriptor for a touch interface will contain a
169 * Digitizer-Finger Physical Collection which will define both logical
170 * X/Y maximum as well as the physical size of tablet. Since touch
171 * interfaces haven't supported pressure or distance, this is enough
172 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700173 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700174 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
175 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700176 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700177static void wacom_usage_mapping(struct hid_device *hdev,
178 struct hid_field *field, struct hid_usage *usage)
179{
180 struct wacom *wacom = hid_get_drvdata(hdev);
181 struct wacom_features *features = &wacom->wacom_wac.features;
182 bool finger = (field->logical == HID_DG_FINGER) ||
183 (field->physical == HID_DG_FINGER);
184 bool pen = (field->logical == HID_DG_STYLUS) ||
185 (field->physical == HID_DG_STYLUS);
186
187 /*
188 * Requiring Stylus Usage will ignore boot mouse
189 * X/Y values and some cases of invalid Digitizer X/Y
190 * values commonly reported.
191 */
192 if (!pen && !finger)
193 return;
194
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800195 /*
196 * Bamboo models do not support HID_DG_CONTACTMAX.
197 * And, Bamboo Pen only descriptor contains touch.
198 */
199 if (features->type != BAMBOO_PT) {
200 /* ISDv4 touch devices at least supports one touch point */
201 if (finger && !features->touch_max)
202 features->touch_max = 1;
203 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700204
205 switch (usage->hid) {
206 case HID_GD_X:
207 features->x_max = field->logical_maximum;
208 if (finger) {
209 features->device_type = BTN_TOOL_FINGER;
210 features->x_phy = field->physical_maximum;
211 if (features->type != BAMBOO_PT) {
212 features->unit = field->unit;
213 features->unitExpo = field->unit_exponent;
214 }
215 } else {
216 features->device_type = BTN_TOOL_PEN;
217 }
218 break;
219 case HID_GD_Y:
220 features->y_max = field->logical_maximum;
221 if (finger) {
222 features->y_phy = field->physical_maximum;
223 if (features->type != BAMBOO_PT) {
224 features->unit = field->unit;
225 features->unitExpo = field->unit_exponent;
226 }
227 }
228 break;
229 case HID_DG_TIPPRESSURE:
230 if (pen)
231 features->pressure_max = field->logical_maximum;
232 break;
233 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400234
235 if (features->type == HID_GENERIC)
236 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700237}
238
239static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800240 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500241{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700242 struct hid_report_enum *rep_enum;
243 struct hid_report *hreport;
244 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500245
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700246 /* check features first */
247 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
248 list_for_each_entry(hreport, &rep_enum->report_list, list) {
249 for (i = 0; i < hreport->maxfield; i++) {
250 /* Ignore if report count is out of bounds. */
251 if (hreport->field[i]->report_count < 1)
252 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500253
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700254 for (j = 0; j < hreport->field[i]->maxusage; j++) {
255 wacom_feature_mapping(hdev, hreport->field[i],
256 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500257 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500258 }
259 }
260
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700261 /* now check the input usages */
262 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
263 list_for_each_entry(hreport, &rep_enum->report_list, list) {
264
265 if (!hreport->maxfield)
266 continue;
267
268 for (i = 0; i < hreport->maxfield; i++)
269 for (j = 0; j < hreport->field[i]->maxusage; j++)
270 wacom_usage_mapping(hdev, hreport->field[i],
271 hreport->field[i]->usage + j);
272 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500273}
274
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400275static int wacom_hid_set_device_mode(struct hid_device *hdev)
276{
277 struct wacom *wacom = hid_get_drvdata(hdev);
278 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
279 struct hid_report *r;
280 struct hid_report_enum *re;
281
282 if (hid_data->inputmode < 0)
283 return 0;
284
285 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
286 r = re->report_id_hash[hid_data->inputmode];
287 if (r) {
288 r->field[0]->value[hid_data->inputmode_index] = 2;
289 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
290 }
291 return 0;
292}
293
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700294static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
295 int length, int mode)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700296{
297 unsigned char *rep_data;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700298 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700299
Jason Gereckefe494bc2012-10-03 17:25:35 -0700300 rep_data = kzalloc(length, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700301 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800302 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700303
Jason Gereckefe494bc2012-10-03 17:25:35 -0700304 do {
Chris Bagwell9937c022013-01-23 19:37:34 -0800305 rep_data[0] = report_id;
306 rep_data[1] = mode;
307
Przemo Firszt296b7372014-08-06 14:00:38 -0700308 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
309 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700310 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700311 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700312 rep_data, length, 1);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700313 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700314
315 kfree(rep_data);
316
317 return error < 0 ? error : 0;
318}
319
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700320static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
321 struct wacom_features *features)
322{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700323 struct wacom *wacom = hid_get_drvdata(hdev);
324 int ret;
325 u8 rep_data[2];
326
327 switch (features->type) {
328 case GRAPHIRE_BT:
329 rep_data[0] = 0x03;
330 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700331 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
332 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700333
334 if (ret >= 0) {
335 rep_data[0] = speed == 0 ? 0x05 : 0x06;
336 rep_data[1] = 0x00;
337
338 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700339 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700340
341 if (ret >= 0) {
342 wacom->wacom_wac.bt_high_speed = speed;
343 return 0;
344 }
345 }
346
347 /*
348 * Note that if the raw queries fail, it's not a hard failure
349 * and it is safe to continue
350 */
351 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
352 rep_data[0], ret);
353 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700354 case INTUOS4WL:
355 if (speed == 1)
356 wacom->wacom_wac.bt_features &= ~0x20;
357 else
358 wacom->wacom_wac.bt_features |= 0x20;
359
360 rep_data[0] = 0x03;
361 rep_data[1] = wacom->wacom_wac.bt_features;
362
Przemo Firszt296b7372014-08-06 14:00:38 -0700363 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
364 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700365 if (ret >= 0)
366 wacom->wacom_wac.bt_high_speed = speed;
367 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700368 }
369
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700370 return 0;
371}
372
Jason Gereckefe494bc2012-10-03 17:25:35 -0700373/*
374 * Switch the tablet into its most-capable mode. Wacom tablets are
375 * typically configured to power-up in a mode which sends mouse-like
376 * reports to the OS. To get absolute position, pressure data, etc.
377 * from the tablet, it is necessary to switch the tablet out of this
378 * mode and into one which sends the full range of tablet data.
379 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700380static int wacom_query_tablet_data(struct hid_device *hdev,
381 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700382{
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700383 if (hdev->bus == BUS_BLUETOOTH)
384 return wacom_bt_query_tablet_data(hdev, 1, features);
385
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400386 if (features->type == HID_GENERIC)
387 return wacom_hid_set_device_mode(hdev);
388
Jason Gereckefe494bc2012-10-03 17:25:35 -0700389 if (features->device_type == BTN_TOOL_FINGER) {
390 if (features->type > TABLETPC) {
391 /* MT Tablet PC touch */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700392 return wacom_set_device_mode(hdev, 3, 4, 4);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700393 }
Jason Gerecke36d3c512013-09-20 09:47:35 -0700394 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700395 return wacom_set_device_mode(hdev, 18, 3, 2);
Jason Gereckeb1e42792012-10-21 00:38:04 -0700396 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700397 } else if (features->device_type == BTN_TOOL_PEN) {
398 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700399 return wacom_set_device_mode(hdev, 2, 2, 2);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700400 }
401 }
402
403 return 0;
404}
405
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700406static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700407 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800408{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700409 struct wacom *wacom = hid_get_drvdata(hdev);
410 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800411
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700412 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800413 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700414 features->x_fuzz = 4;
415 features->y_fuzz = 4;
416 features->pressure_fuzz = 0;
417 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800418
Chris Bagwelld3825d52012-03-25 23:26:11 -0700419 /*
420 * The wireless device HID is basic and layout conflicts with
421 * other tablets (monitor and touch interface can look like pen).
422 * Skip the query for this type and modify defaults based on
423 * interface number.
424 */
425 if (features->type == WIRELESS) {
426 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
427 features->device_type = 0;
428 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
Ping Chengadad0042012-06-28 16:48:17 -0700429 features->device_type = BTN_TOOL_FINGER;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700430 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
431 }
432 }
433
Ping Cheng19635182012-04-29 21:09:18 -0700434 /* only devices that support touch need to retrieve the info */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700435 if (features->type < BAMBOO_PT)
436 return;
Ping Chengec67bbe2009-12-15 00:35:24 -0800437
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700438 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800439}
440
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700441struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700442 struct list_head list;
443 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700444 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700445 struct wacom_shared shared;
446};
447
448static LIST_HEAD(wacom_udev_list);
449static DEFINE_MUTEX(wacom_udev_list_lock);
450
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700451static bool wacom_are_sibling(struct hid_device *hdev,
452 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700453{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700454 struct wacom *wacom = hid_get_drvdata(hdev);
455 struct wacom_features *features = &wacom->wacom_wac.features;
456 int vid = features->oVid;
457 int pid = features->oPid;
458 int n1,n2;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700459
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700460 if (vid == 0 && pid == 0) {
461 vid = hdev->vendor;
462 pid = hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700463 }
464
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700465 if (vid != sibling->vendor || pid != sibling->product)
466 return false;
467
468 /* Compare the physical path. */
469 n1 = strrchr(hdev->phys, '.') - hdev->phys;
470 n2 = strrchr(sibling->phys, '.') - sibling->phys;
471 if (n1 != n2 || n1 <= 0 || n2 <= 0)
472 return false;
473
474 return !strncmp(hdev->phys, sibling->phys, n1);
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700475}
476
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700477static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700478{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700479 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700480
481 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700482 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700483 kref_get(&data->kref);
484 return data;
485 }
486 }
487
488 return NULL;
489}
490
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700491static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700492{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700493 struct wacom *wacom = hid_get_drvdata(hdev);
494 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
495 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700496 int retval = 0;
497
498 mutex_lock(&wacom_udev_list_lock);
499
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700500 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700501 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700502 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700503 if (!data) {
504 retval = -ENOMEM;
505 goto out;
506 }
507
508 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700509 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700510 list_add_tail(&data->list, &wacom_udev_list);
511 }
512
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700513 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700514
515out:
516 mutex_unlock(&wacom_udev_list_lock);
517 return retval;
518}
519
520static void wacom_release_shared_data(struct kref *kref)
521{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700522 struct wacom_hdev_data *data =
523 container_of(kref, struct wacom_hdev_data, kref);
Ping Cheng4492eff2010-03-19 22:18:15 -0700524
525 mutex_lock(&wacom_udev_list_lock);
526 list_del(&data->list);
527 mutex_unlock(&wacom_udev_list_lock);
528
529 kfree(data);
530}
531
532static void wacom_remove_shared_data(struct wacom_wac *wacom)
533{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700534 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700535
536 if (wacom->shared) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700537 data = container_of(wacom->shared, struct wacom_hdev_data, shared);
Ping Cheng4492eff2010-03-19 22:18:15 -0700538 kref_put(&data->kref, wacom_release_shared_data);
539 wacom->shared = NULL;
540 }
541}
542
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700543static int wacom_led_control(struct wacom *wacom)
544{
545 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700546 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700547 unsigned char report_id = WAC_CMD_LED_CONTROL;
548 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700549
Ping Cheng912ca212014-09-10 12:41:31 -0700550 if (wacom->wacom_wac.pid) { /* wireless connected */
551 report_id = WAC_CMD_WL_LED_CONTROL;
552 buf_size = 13;
553 }
554 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700555 if (!buf)
556 return -ENOMEM;
557
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700558 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700559 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700560 /*
561 * Touch Ring and crop mark LED luminance may take on
562 * one of four values:
563 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
564 */
565 int ring_led = wacom->led.select[0] & 0x03;
566 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
567 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700568 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700569
Ping Cheng912ca212014-09-10 12:41:31 -0700570 buf[0] = report_id;
571 if (wacom->wacom_wac.pid) {
572 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
573 buf, buf_size, WAC_CMD_RETRIES);
574 buf[0] = report_id;
575 buf[4] = led_bits;
576 } else
577 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700578 }
579 else {
580 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700581
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700582 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
583 wacom->wacom_wac.features.type == WACOM_24HD)
584 led |= (wacom->led.select[1] << 4) | 0x40;
585
Ping Cheng912ca212014-09-10 12:41:31 -0700586 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700587 buf[1] = led;
588 buf[2] = wacom->led.llv;
589 buf[3] = wacom->led.hlv;
590 buf[4] = wacom->led.img_lum;
591 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700592
Ping Cheng912ca212014-09-10 12:41:31 -0700593 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700594 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700595 kfree(buf);
596
597 return retval;
598}
599
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700600static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
601 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700602{
603 unsigned char *buf;
604 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700605 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700606
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700607 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700608 if (!buf)
609 return -ENOMEM;
610
611 /* Send 'start' command */
612 buf[0] = WAC_CMD_ICON_START;
613 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700614 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
615 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700616 if (retval < 0)
617 goto out;
618
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700619 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700620 buf[1] = button_id & 0x07;
621 for (i = 0; i < 4; i++) {
622 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700623 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700624
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700625 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700626 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700627 if (retval < 0)
628 break;
629 }
630
631 /* Send 'stop' */
632 buf[0] = WAC_CMD_ICON_START;
633 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700634 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
635 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700636
637out:
638 kfree(buf);
639 return retval;
640}
641
Ping Cheng09e7d942011-10-04 23:51:14 -0700642static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700643 const char *buf, size_t count)
644{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700645 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700646 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700647 unsigned int id;
648 int err;
649
650 err = kstrtouint(buf, 10, &id);
651 if (err)
652 return err;
653
654 mutex_lock(&wacom->lock);
655
Ping Cheng09e7d942011-10-04 23:51:14 -0700656 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700657 err = wacom_led_control(wacom);
658
659 mutex_unlock(&wacom->lock);
660
661 return err < 0 ? err : count;
662}
663
Ping Cheng09e7d942011-10-04 23:51:14 -0700664#define DEVICE_LED_SELECT_ATTR(SET_ID) \
665static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
666 struct device_attribute *attr, const char *buf, size_t count) \
667{ \
668 return wacom_led_select_store(dev, SET_ID, buf, count); \
669} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700670static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
671 struct device_attribute *attr, char *buf) \
672{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700673 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700674 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700675 return scnprintf(buf, PAGE_SIZE, "%d\n", \
676 wacom->led.select[SET_ID]); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700677} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700678static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700679 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700680 wacom_led##SET_ID##_select_store)
681
682DEVICE_LED_SELECT_ATTR(0);
683DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700684
685static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
686 const char *buf, size_t count)
687{
688 unsigned int value;
689 int err;
690
691 err = kstrtouint(buf, 10, &value);
692 if (err)
693 return err;
694
695 mutex_lock(&wacom->lock);
696
697 *dest = value & 0x7f;
698 err = wacom_led_control(wacom);
699
700 mutex_unlock(&wacom->lock);
701
702 return err < 0 ? err : count;
703}
704
705#define DEVICE_LUMINANCE_ATTR(name, field) \
706static ssize_t wacom_##name##_luminance_store(struct device *dev, \
707 struct device_attribute *attr, const char *buf, size_t count) \
708{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700709 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700710 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700711 \
712 return wacom_luminance_store(wacom, &wacom->led.field, \
713 buf, count); \
714} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700715static ssize_t wacom_##name##_luminance_show(struct device *dev, \
716 struct device_attribute *attr, char *buf) \
717{ \
718 struct wacom *wacom = dev_get_drvdata(dev); \
719 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
720} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700721static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700722 wacom_##name##_luminance_show, \
723 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700724
725DEVICE_LUMINANCE_ATTR(status0, llv);
726DEVICE_LUMINANCE_ATTR(status1, hlv);
727DEVICE_LUMINANCE_ATTR(buttons, img_lum);
728
729static ssize_t wacom_button_image_store(struct device *dev, int button_id,
730 const char *buf, size_t count)
731{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700732 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700733 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700734 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700735 unsigned len;
736 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700737
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700738 if (hdev->bus == BUS_BLUETOOTH) {
739 len = 256;
740 xfer_id = WAC_CMD_ICON_BT_XFER;
741 } else {
742 len = 1024;
743 xfer_id = WAC_CMD_ICON_XFER;
744 }
745
746 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700747 return -EINVAL;
748
749 mutex_lock(&wacom->lock);
750
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700751 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700752
753 mutex_unlock(&wacom->lock);
754
755 return err < 0 ? err : count;
756}
757
758#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
759static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
760 struct device_attribute *attr, const char *buf, size_t count) \
761{ \
762 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
763} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700764static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700765 NULL, wacom_btnimg##BUTTON_ID##_store)
766
767DEVICE_BTNIMG_ATTR(0);
768DEVICE_BTNIMG_ATTR(1);
769DEVICE_BTNIMG_ATTR(2);
770DEVICE_BTNIMG_ATTR(3);
771DEVICE_BTNIMG_ATTR(4);
772DEVICE_BTNIMG_ATTR(5);
773DEVICE_BTNIMG_ATTR(6);
774DEVICE_BTNIMG_ATTR(7);
775
Ping Cheng09e7d942011-10-04 23:51:14 -0700776static struct attribute *cintiq_led_attrs[] = {
777 &dev_attr_status_led0_select.attr,
778 &dev_attr_status_led1_select.attr,
779 NULL
780};
781
782static struct attribute_group cintiq_led_attr_group = {
783 .name = "wacom_led",
784 .attrs = cintiq_led_attrs,
785};
786
787static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700788 &dev_attr_status0_luminance.attr,
789 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700790 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700791 &dev_attr_buttons_luminance.attr,
792 &dev_attr_button0_rawimg.attr,
793 &dev_attr_button1_rawimg.attr,
794 &dev_attr_button2_rawimg.attr,
795 &dev_attr_button3_rawimg.attr,
796 &dev_attr_button4_rawimg.attr,
797 &dev_attr_button5_rawimg.attr,
798 &dev_attr_button6_rawimg.attr,
799 &dev_attr_button7_rawimg.attr,
800 NULL
801};
802
Ping Cheng09e7d942011-10-04 23:51:14 -0700803static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700804 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700805 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700806};
807
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700808static struct attribute *intuos5_led_attrs[] = {
809 &dev_attr_status0_luminance.attr,
810 &dev_attr_status_led0_select.attr,
811 NULL
812};
813
814static struct attribute_group intuos5_led_attr_group = {
815 .name = "wacom_led",
816 .attrs = intuos5_led_attrs,
817};
818
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700819static int wacom_initialize_leds(struct wacom *wacom)
820{
821 int error;
822
Ping Cheng09e7d942011-10-04 23:51:14 -0700823 /* Initialize default values */
824 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700825 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700826 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700827 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700828 case INTUOS4L:
829 wacom->led.select[0] = 0;
830 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700831 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700832 wacom->led.hlv = 20;
833 wacom->led.img_lum = 10;
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700834 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700835 &intuos4_led_attr_group);
836 break;
837
Jason Gerecke246835f2011-12-12 00:12:04 -0800838 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700839 case WACOM_21UX2:
840 wacom->led.select[0] = 0;
841 wacom->led.select[1] = 0;
842 wacom->led.llv = 0;
843 wacom->led.hlv = 0;
844 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700845
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700846 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700847 &cintiq_led_attr_group);
848 break;
849
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700850 case INTUOS5S:
851 case INTUOS5:
852 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700853 case INTUOSPS:
854 case INTUOSPM:
855 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700856 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
857 wacom->led.select[0] = 0;
858 wacom->led.select[1] = 0;
859 wacom->led.llv = 32;
860 wacom->led.hlv = 0;
861 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700862
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700863 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700864 &intuos5_led_attr_group);
865 } else
866 return 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700867 break;
868
Ping Cheng09e7d942011-10-04 23:51:14 -0700869 default:
870 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700871 }
872
Ping Cheng09e7d942011-10-04 23:51:14 -0700873 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700874 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -0700875 "cannot create sysfs group err: %d\n", error);
876 return error;
877 }
878 wacom_led_control(wacom);
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700879 wacom->led_initialized = true;
Ping Cheng09e7d942011-10-04 23:51:14 -0700880
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700881 return 0;
882}
883
884static void wacom_destroy_leds(struct wacom *wacom)
885{
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700886 if (!wacom->led_initialized)
887 return;
888
889 wacom->led_initialized = false;
890
Ping Cheng09e7d942011-10-04 23:51:14 -0700891 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700892 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700893 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700894 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700895 case INTUOS4L:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700896 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700897 &intuos4_led_attr_group);
898 break;
899
Jason Gerecke246835f2011-12-12 00:12:04 -0800900 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700901 case WACOM_21UX2:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700902 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700903 &cintiq_led_attr_group);
904 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700905
906 case INTUOS5S:
907 case INTUOS5:
908 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700909 case INTUOSPS:
910 case INTUOSPM:
911 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700912 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700913 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700914 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700915 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700916 }
917}
918
Chris Bagwella1d552c2012-03-25 23:26:30 -0700919static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700920 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700921 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700922 POWER_SUPPLY_PROP_CAPACITY
923};
924
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700925static enum power_supply_property wacom_ac_props[] = {
926 POWER_SUPPLY_PROP_PRESENT,
927 POWER_SUPPLY_PROP_ONLINE,
928 POWER_SUPPLY_PROP_SCOPE,
929};
930
Chris Bagwella1d552c2012-03-25 23:26:30 -0700931static int wacom_battery_get_property(struct power_supply *psy,
932 enum power_supply_property psp,
933 union power_supply_propval *val)
934{
935 struct wacom *wacom = container_of(psy, struct wacom, battery);
936 int ret = 0;
937
938 switch (psp) {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700939 case POWER_SUPPLY_PROP_SCOPE:
940 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
941 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700942 case POWER_SUPPLY_PROP_CAPACITY:
943 val->intval =
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700944 wacom->wacom_wac.battery_capacity;
945 break;
946 case POWER_SUPPLY_PROP_STATUS:
947 if (wacom->wacom_wac.bat_charging)
948 val->intval = POWER_SUPPLY_STATUS_CHARGING;
949 else if (wacom->wacom_wac.battery_capacity == 100 &&
950 wacom->wacom_wac.ps_connected)
951 val->intval = POWER_SUPPLY_STATUS_FULL;
952 else
953 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700954 break;
955 default:
956 ret = -EINVAL;
957 break;
958 }
959
960 return ret;
961}
962
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700963static int wacom_ac_get_property(struct power_supply *psy,
964 enum power_supply_property psp,
965 union power_supply_propval *val)
966{
967 struct wacom *wacom = container_of(psy, struct wacom, ac);
968 int ret = 0;
969
970 switch (psp) {
971 case POWER_SUPPLY_PROP_PRESENT:
972 /* fall through */
973 case POWER_SUPPLY_PROP_ONLINE:
974 val->intval = wacom->wacom_wac.ps_connected;
975 break;
976 case POWER_SUPPLY_PROP_SCOPE:
977 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
978 break;
979 default:
980 ret = -EINVAL;
981 break;
982 }
983 return ret;
984}
985
Chris Bagwella1d552c2012-03-25 23:26:30 -0700986static int wacom_initialize_battery(struct wacom *wacom)
987{
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700988 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700989 int error;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700990 unsigned long n;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700991
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700992 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700993 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700994
Chris Bagwella1d552c2012-03-25 23:26:30 -0700995 wacom->battery.properties = wacom_battery_props;
996 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
997 wacom->battery.get_property = wacom_battery_get_property;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700998 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
999 wacom->battery.name = wacom->wacom_wac.bat_name;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001000 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1001 wacom->battery.use_for_apm = 0;
1002
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001003 wacom->ac.properties = wacom_ac_props;
1004 wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
1005 wacom->ac.get_property = wacom_ac_get_property;
1006 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
1007 wacom->ac.name = wacom->wacom_wac.ac_name;
1008 wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
1009 wacom->ac.use_for_apm = 0;
1010
Benjamin Tissoiresdd3181a2014-07-24 13:01:40 -07001011 error = power_supply_register(&wacom->hdev->dev,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001012 &wacom->battery);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001013 if (error)
1014 return error;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001015
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001016 power_supply_powers(&wacom->battery, &wacom->hdev->dev);
1017
1018 error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
1019 if (error) {
1020 power_supply_unregister(&wacom->battery);
1021 return error;
1022 }
1023
1024 power_supply_powers(&wacom->ac, &wacom->hdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001025 }
1026
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001027 return 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001028}
1029
1030static void wacom_destroy_battery(struct wacom *wacom)
1031{
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001032 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1033 wacom->battery.dev) {
Chris Bagwella1d552c2012-03-25 23:26:30 -07001034 power_supply_unregister(&wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001035 wacom->battery.dev = NULL;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001036 power_supply_unregister(&wacom->ac);
1037 wacom->ac.dev = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001038 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001039}
1040
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001041static ssize_t wacom_show_speed(struct device *dev,
1042 struct device_attribute
1043 *attr, char *buf)
1044{
1045 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1046 struct wacom *wacom = hid_get_drvdata(hdev);
1047
1048 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1049}
1050
1051static ssize_t wacom_store_speed(struct device *dev,
1052 struct device_attribute *attr,
1053 const char *buf, size_t count)
1054{
1055 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1056 struct wacom *wacom = hid_get_drvdata(hdev);
1057 u8 new_speed;
1058
1059 if (kstrtou8(buf, 0, &new_speed))
1060 return -EINVAL;
1061
1062 if (new_speed != 0 && new_speed != 1)
1063 return -EINVAL;
1064
1065 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1066
1067 return count;
1068}
1069
Ping Chenge0984bc2014-09-10 12:40:05 -07001070static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001071 wacom_show_speed, wacom_store_speed);
1072
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001073static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001074{
1075 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001076 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001077 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001078
1079 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001080 if (!input_dev)
1081 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001082
1083 input_dev->name = wacom_wac->name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001084 input_dev->phys = hdev->phys;
1085 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001086 input_dev->open = wacom_open;
1087 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001088 input_dev->uniq = hdev->uniq;
1089 input_dev->id.bustype = hdev->bus;
1090 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001091 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001092 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001093 input_set_drvdata(input_dev, wacom);
1094
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001095 return input_dev;
1096}
1097
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001098static void wacom_free_inputs(struct wacom *wacom)
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001099{
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001100 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1101
1102 if (wacom_wac->input)
1103 input_free_device(wacom_wac->input);
1104 if (wacom_wac->pad_input)
1105 input_free_device(wacom_wac->pad_input);
1106 wacom_wac->input = NULL;
1107 wacom_wac->pad_input = NULL;
1108}
1109
1110static int wacom_allocate_inputs(struct wacom *wacom)
1111{
1112 struct input_dev *input_dev, *pad_input_dev;
1113 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1114
1115 input_dev = wacom_allocate_input(wacom);
1116 pad_input_dev = wacom_allocate_input(wacom);
1117 if (!input_dev || !pad_input_dev) {
1118 wacom_free_inputs(wacom);
1119 return -ENOMEM;
1120 }
1121
1122 wacom_wac->input = input_dev;
1123 wacom_wac->pad_input = pad_input_dev;
1124 wacom_wac->pad_input->name = wacom_wac->pad_name;
1125
1126 return 0;
1127}
1128
1129static void wacom_clean_inputs(struct wacom *wacom)
1130{
1131 if (wacom->wacom_wac.input) {
1132 if (wacom->wacom_wac.input_registered)
1133 input_unregister_device(wacom->wacom_wac.input);
1134 else
1135 input_free_device(wacom->wacom_wac.input);
1136 }
1137 if (wacom->wacom_wac.pad_input) {
Ping Cheng954df6a2014-11-20 16:31:12 -08001138 if (wacom->wacom_wac.pad_registered)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001139 input_unregister_device(wacom->wacom_wac.pad_input);
1140 else
1141 input_free_device(wacom->wacom_wac.pad_input);
1142 }
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001143 wacom->wacom_wac.input = NULL;
1144 wacom->wacom_wac.pad_input = NULL;
Ping Cheng912ca212014-09-10 12:41:31 -07001145 wacom_destroy_leds(wacom);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001146}
1147
1148static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001149{
1150 struct input_dev *input_dev, *pad_input_dev;
1151 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1152 int error;
1153
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001154 input_dev = wacom_wac->input;
1155 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001156
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001157 if (!input_dev || !pad_input_dev)
1158 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001159
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001160 error = wacom_setup_pentouch_input_capabilities(input_dev, wacom_wac);
1161 if (!error) {
1162 error = input_register_device(input_dev);
1163 if (error)
1164 return error;
Ping Cheng954df6a2014-11-20 16:31:12 -08001165 wacom_wac->input_registered = true;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08001166 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001167
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001168 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1169 if (error) {
1170 /* no pad in use on this interface */
1171 input_free_device(pad_input_dev);
1172 wacom_wac->pad_input = NULL;
1173 pad_input_dev = NULL;
1174 } else {
1175 error = input_register_device(pad_input_dev);
1176 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001177 goto fail_register_pad_input;
Ping Cheng954df6a2014-11-20 16:31:12 -08001178 wacom_wac->pad_registered = true;
Ping Cheng912ca212014-09-10 12:41:31 -07001179
1180 error = wacom_initialize_leds(wacom);
1181 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001182 goto fail_leds;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001183 }
1184
Ping Cheng19635182012-04-29 21:09:18 -07001185 return 0;
1186
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001187fail_leds:
Ping Cheng912ca212014-09-10 12:41:31 -07001188 input_unregister_device(pad_input_dev);
1189 pad_input_dev = NULL;
Ping Cheng954df6a2014-11-20 16:31:12 -08001190 wacom_wac->pad_registered = false;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001191fail_register_pad_input:
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001192 input_unregister_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001193 wacom_wac->input = NULL;
Ping Cheng954df6a2014-11-20 16:31:12 -08001194 wacom_wac->input_registered = false;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001195 return error;
1196}
1197
Chris Bagwell16bf2882012-03-25 23:26:20 -07001198static void wacom_wireless_work(struct work_struct *work)
1199{
1200 struct wacom *wacom = container_of(work, struct wacom, work);
1201 struct usb_device *usbdev = wacom->usbdev;
1202 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001203 struct hid_device *hdev1, *hdev2;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001204 struct wacom *wacom1, *wacom2;
1205 struct wacom_wac *wacom_wac1, *wacom_wac2;
1206 int error;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001207
1208 /*
1209 * Regardless if this is a disconnect or a new tablet,
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001210 * remove any existing input and battery devices.
Chris Bagwell16bf2882012-03-25 23:26:20 -07001211 */
1212
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001213 wacom_destroy_battery(wacom);
1214
Chris Bagwell16bf2882012-03-25 23:26:20 -07001215 /* Stylus interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001216 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1217 wacom1 = hid_get_drvdata(hdev1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001218 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001219 wacom_clean_inputs(wacom1);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001220
1221 /* Touch interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001222 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1223 wacom2 = hid_get_drvdata(hdev2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001224 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001225 wacom_clean_inputs(wacom2);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001226
1227 if (wacom_wac->pid == 0) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001228 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001229 wacom_wac1->shared->type = 0;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001230 } else {
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001231 const struct hid_device_id *id = wacom_ids;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001232
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001233 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001234 wacom_wac->pid);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001235
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001236 while (id->bus) {
1237 if (id->vendor == USB_VENDOR_ID_WACOM &&
1238 id->product == wacom_wac->pid)
Chris Bagwell16bf2882012-03-25 23:26:20 -07001239 break;
1240 id++;
1241 }
1242
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001243 if (!id->bus) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001244 hid_info(wacom->hdev, "ignoring unknown PID.\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001245 return;
1246 }
1247
1248 /* Stylus interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001249 wacom_wac1->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001250 *((struct wacom_features *)id->driver_data);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001251 wacom_wac1->features.device_type = BTN_TOOL_PEN;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001252 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1253 wacom_wac1->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001254 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1255 wacom_wac1->features.name);
Ping Cheng961794a2013-12-05 12:54:53 -08001256 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1257 wacom_wac1->shared->type = wacom_wac1->features.type;
Ping Cheng912ca212014-09-10 12:41:31 -07001258 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001259 error = wacom_allocate_inputs(wacom1) ||
1260 wacom_register_inputs(wacom1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001261 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001262 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001263
1264 /* Touch interface */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001265 if (wacom_wac1->features.touch_max ||
1266 wacom_wac1->features.type == INTUOSHT) {
Ping Cheng57bcfce2013-10-15 23:44:00 -07001267 wacom_wac2->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001268 *((struct wacom_features *)id->driver_data);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001269 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1270 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1271 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1272 if (wacom_wac2->features.touch_max)
1273 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1274 "%s (WL) Finger",wacom_wac2->features.name);
1275 else
1276 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1277 "%s (WL) Pad",wacom_wac2->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001278 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1279 "%s (WL) Pad", wacom_wac2->features.name);
Ping Cheng912ca212014-09-10 12:41:31 -07001280 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001281 error = wacom_allocate_inputs(wacom2) ||
1282 wacom_register_inputs(wacom2);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001283 if (error)
1284 goto fail;
Ping Cheng961794a2013-12-05 12:54:53 -08001285
1286 if (wacom_wac1->features.type == INTUOSHT &&
1287 wacom_wac1->features.touch_max)
1288 wacom_wac->shared->touch_input = wacom_wac2->input;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001289 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001290
1291 error = wacom_initialize_battery(wacom);
1292 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001293 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001294 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001295
1296 return;
1297
Ping Cheng57bcfce2013-10-15 23:44:00 -07001298fail:
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001299 wacom_clean_inputs(wacom1);
1300 wacom_clean_inputs(wacom2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001301 return;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001302}
1303
Ping Cheng401d7d12013-07-28 00:38:54 -07001304/*
1305 * Not all devices report physical dimensions from HID.
1306 * Compute the default from hardcoded logical dimension
1307 * and resolution before driver overwrites them.
1308 */
1309static void wacom_set_default_phy(struct wacom_features *features)
1310{
1311 if (features->x_resolution) {
1312 features->x_phy = (features->x_max * 100) /
1313 features->x_resolution;
1314 features->y_phy = (features->y_max * 100) /
1315 features->y_resolution;
1316 }
1317}
1318
1319static void wacom_calculate_res(struct wacom_features *features)
1320{
1321 features->x_resolution = wacom_calc_hid_res(features->x_max,
1322 features->x_phy,
1323 features->unit,
1324 features->unitExpo);
1325 features->y_resolution = wacom_calc_hid_res(features->y_max,
1326 features->y_phy,
1327 features->unit,
1328 features->unitExpo);
1329}
1330
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001331static int wacom_hid_report_len(struct hid_report *report)
1332{
1333 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1334 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1335}
1336
1337static size_t wacom_compute_pktlen(struct hid_device *hdev)
1338{
1339 struct hid_report_enum *report_enum;
1340 struct hid_report *report;
1341 size_t size = 0;
1342
1343 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1344
1345 list_for_each_entry(report, &report_enum->report_list, list) {
1346 size_t report_size = wacom_hid_report_len(report);
1347 if (report_size > size)
1348 size = report_size;
1349 }
1350
1351 return size;
1352}
1353
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001354static int wacom_probe(struct hid_device *hdev,
1355 const struct hid_device_id *id)
Ping Cheng3bea7332006-07-13 18:01:36 -07001356{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001357 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Ping Cheng3bea7332006-07-13 18:01:36 -07001358 struct usb_device *dev = interface_to_usbdev(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001359 struct wacom *wacom;
1360 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001361 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001362 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001363 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07001364
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001365 if (!id->driver_data)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001366 return -EINVAL;
1367
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -04001368 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1369
Ping Cheng3bea7332006-07-13 18:01:36 -07001370 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001371 if (!wacom)
1372 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001373
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001374 hid_set_drvdata(hdev, wacom);
1375 wacom->hdev = hdev;
1376
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001377 /* ask for the report descriptor to be loaded by HID */
1378 error = hid_parse(hdev);
1379 if (error) {
1380 hid_err(hdev, "parse failed\n");
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001381 goto fail_parse;
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001382 }
1383
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001384 wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001385 wacom_wac->features = *((struct wacom_features *)id->driver_data);
Jason Childse33da8a2010-02-17 22:38:31 -08001386 features = &wacom_wac->features;
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001387 features->pktlen = wacom_compute_pktlen(hdev);
Jason Childse33da8a2010-02-17 22:38:31 -08001388 if (features->pktlen > WACOM_PKGLEN_MAX) {
1389 error = -EINVAL;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001390 goto fail_pktlen;
Jason Childse33da8a2010-02-17 22:38:31 -08001391 }
1392
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001393 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1394 error = -ENODEV;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001395 goto fail_type;
Jason Childse33da8a2010-02-17 22:38:31 -08001396 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001397
Ping Cheng3bea7332006-07-13 18:01:36 -07001398 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001399 wacom->intf = intf;
1400 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001401 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001402
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001403 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1404 error = wacom_allocate_inputs(wacom);
1405 if (error)
1406 goto fail_allocate_inputs;
1407 }
1408
Ping Cheng401d7d12013-07-28 00:38:54 -07001409 /* set the default size in case we do not get them from hid */
1410 wacom_set_default_phy(features);
1411
Ping Chengf393ee22012-04-29 21:09:17 -07001412 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001413 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -05001414
Jason Gereckeae584ca2012-04-03 15:50:40 -07001415 /*
1416 * Intuos5 has no useful data about its touch interface in its
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001417 * HID descriptor. If this is the touch interface (PacketSize
Jason Gereckeae584ca2012-04-03 15:50:40 -07001418 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1419 */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001420 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001421 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
Jason Gereckeae584ca2012-04-03 15:50:40 -07001422 features->device_type = BTN_TOOL_FINGER;
Jason Gereckeae584ca2012-04-03 15:50:40 -07001423
Jason Gereckeae584ca2012-04-03 15:50:40 -07001424 features->x_max = 4096;
1425 features->y_max = 4096;
1426 } else {
1427 features->device_type = BTN_TOOL_PEN;
1428 }
1429 }
1430
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001431 /*
1432 * Same thing for Bamboo 3rd gen.
1433 */
1434 if ((features->type == BAMBOO_PT) &&
1435 (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1436 (features->device_type == BTN_TOOL_PEN)) {
1437 features->device_type = BTN_TOOL_FINGER;
1438
1439 features->x_max = 4096;
1440 features->y_max = 4096;
1441 }
1442
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001443 if (hdev->bus == BUS_BLUETOOTH)
1444 features->quirks |= WACOM_QUIRK_BATTERY;
1445
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001446 wacom_setup_device_quirks(features);
1447
Ping Cheng401d7d12013-07-28 00:38:54 -07001448 /* set unit to "100th of a mm" for devices not reported by HID */
1449 if (!features->unit) {
1450 features->unit = 0x11;
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001451 features->unitExpo = -3;
Ping Cheng401d7d12013-07-28 00:38:54 -07001452 }
1453 wacom_calculate_res(features);
1454
Ping Cheng49b764a2010-02-20 00:53:49 -08001455 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001456 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1457 "%s Pad", features->name);
Ping Cheng49b764a2010-02-20 00:53:49 -08001458
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001459 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -08001460 /* Append the device type to the name */
Ping Cheng57bcfce2013-10-15 23:44:00 -07001461 if (features->device_type != BTN_TOOL_FINGER)
1462 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1463 else if (features->touch_max)
1464 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1465 else
1466 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
Ping Cheng4492eff2010-03-19 22:18:15 -07001467
Benjamin Tissoires4451e082014-07-24 13:01:05 -07001468 error = wacom_add_shared_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -07001469 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001470 goto fail_shared_data;
Ping Cheng49b764a2010-02-20 00:53:49 -08001471 }
1472
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001473 if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1474 (features->quirks & WACOM_QUIRK_BATTERY)) {
1475 error = wacom_initialize_battery(wacom);
1476 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001477 goto fail_battery;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001478 }
1479
Chris Bagwelld3825d52012-03-25 23:26:11 -07001480 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001481 error = wacom_register_inputs(wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001482 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001483 goto fail_register_inputs;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001484 }
1485
1486 if (hdev->bus == BUS_BLUETOOTH) {
1487 error = device_create_file(&hdev->dev, &dev_attr_speed);
1488 if (error)
1489 hid_warn(hdev,
1490 "can't create sysfs speed attribute err: %d\n",
1491 error);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001492 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001493
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001494 if (features->type == HID_GENERIC)
1495 connect_mask |= HID_CONNECT_DRIVER;
1496
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001497 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001498 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001499 if (error) {
1500 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001501 goto fail_hw_start;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001502 }
1503
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -04001504 /* Note that if query fails it is not a hard failure */
1505 wacom_query_tablet_data(hdev, features);
1506
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001507 if (features->quirks & WACOM_QUIRK_MONITOR)
1508 error = hid_hw_open(hdev);
1509
Ping Cheng961794a2013-12-05 12:54:53 -08001510 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1511 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1512 wacom_wac->shared->touch_input = wacom_wac->input;
1513 }
1514
Ping Cheng3bea7332006-07-13 18:01:36 -07001515 return 0;
1516
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001517fail_hw_start:
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001518 if (hdev->bus == BUS_BLUETOOTH)
1519 device_remove_file(&hdev->dev, &dev_attr_speed);
1520fail_register_inputs:
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001521 wacom_clean_inputs(wacom);
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001522 wacom_destroy_battery(wacom);
1523fail_battery:
1524 wacom_remove_shared_data(wacom_wac);
1525fail_shared_data:
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001526 wacom_clean_inputs(wacom);
1527fail_allocate_inputs:
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001528fail_type:
1529fail_pktlen:
1530fail_parse:
1531 kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001532 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001533 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001534}
1535
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001536static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001537{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001538 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001539
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001540 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001541
Chris Bagwell16bf2882012-03-25 23:26:20 -07001542 cancel_work_sync(&wacom->work);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001543 wacom_clean_inputs(wacom);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001544 if (hdev->bus == BUS_BLUETOOTH)
1545 device_remove_file(&hdev->dev, &dev_attr_speed);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001546 wacom_destroy_battery(wacom);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001547 wacom_remove_shared_data(&wacom->wacom_wac);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001548
1549 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001550 kfree(wacom);
1551}
1552
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001553#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001554static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001555{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001556 struct wacom *wacom = hid_get_drvdata(hdev);
1557 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001558
1559 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001560
1561 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001562 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001563 wacom_led_control(wacom);
1564
Oliver Neukume7224092008-04-15 01:31:57 -04001565 mutex_unlock(&wacom->lock);
1566
1567 return 0;
1568}
1569
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001570static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001571{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001572 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001573}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001574#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04001575
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001576static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001577 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001578 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001579 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001580 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001581 .event = wacom_wac_event,
1582 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001583#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001584 .resume = wacom_resume,
1585 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001586#endif
1587 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001588};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001589module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07001590
1591MODULE_VERSION(DRIVER_VERSION);
1592MODULE_AUTHOR(DRIVER_AUTHOR);
1593MODULE_DESCRIPTION(DRIVER_DESC);
1594MODULE_LICENSE(DRIVER_LICENSE);