]>
Commit | Line | Data |
---|---|---|
3bea733a | 1 | /* |
4104d13f | 2 | * drivers/input/tablet/wacom_sys.c |
3bea733a | 3 | * |
232f5693 | 4 | * USB Wacom tablet support - system specific code |
3bea733a PC |
5 | */ |
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 | ||
3bea733a | 14 | #include "wacom_wac.h" |
51269fe8 | 15 | #include "wacom.h" |
b58ba1ba | 16 | #include <linux/input/mt.h> |
3bea733a | 17 | |
a417ea44 | 18 | #define WAC_MSG_RETRIES 5 |
3bea733a | 19 | |
912ca216 | 20 | #define WAC_CMD_WL_LED_CONTROL 0x03 |
5d7e7d47 EH |
21 | #define WAC_CMD_LED_CONTROL 0x20 |
22 | #define WAC_CMD_ICON_START 0x21 | |
23 | #define WAC_CMD_ICON_XFER 0x23 | |
849e2f06 | 24 | #define WAC_CMD_ICON_BT_XFER 0x26 |
5d7e7d47 | 25 | #define WAC_CMD_RETRIES 10 |
72b236d6 AS |
26 | #define WAC_CMD_DELETE_PAIRING 0x20 |
27 | #define WAC_CMD_UNPAIR_ALL 0xFF | |
5d7e7d47 | 28 | |
e0984bc3 PC |
29 | #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP) |
30 | #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP) | |
72b236d6 | 31 | #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP) |
e0984bc3 | 32 | |
c64d8834 PC |
33 | static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf, |
34 | size_t size, unsigned int retries) | |
3bea733a | 35 | { |
5d7e7d47 EH |
36 | int retval; |
37 | ||
38 | do { | |
c64d8834 | 39 | retval = hid_hw_raw_request(hdev, buf[0], buf, size, type, |
27b20a9d | 40 | HID_REQ_GET_REPORT); |
aef3156d JG |
41 | } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries); |
42 | ||
43 | if (retval < 0) | |
44 | hid_err(hdev, "wacom_get_report: ran out of retries " | |
45 | "(last error = %d)\n", retval); | |
5d7e7d47 EH |
46 | |
47 | return retval; | |
3bea733a PC |
48 | } |
49 | ||
296b7378 PF |
50 | static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf, |
51 | size_t size, unsigned int retries) | |
3bea733a | 52 | { |
5d7e7d47 EH |
53 | int retval; |
54 | ||
55 | do { | |
296b7378 | 56 | retval = hid_hw_raw_request(hdev, buf[0], buf, size, type, |
27b20a9d | 57 | HID_REQ_SET_REPORT); |
aef3156d JG |
58 | } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries); |
59 | ||
60 | if (retval < 0) | |
61 | hid_err(hdev, "wacom_set_report: ran out of retries " | |
62 | "(last error = %d)\n", retval); | |
5d7e7d47 EH |
63 | |
64 | return retval; | |
3bea733a PC |
65 | } |
66 | ||
29b47391 BT |
67 | static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report, |
68 | u8 *raw_data, int size) | |
3bea733a | 69 | { |
29b47391 | 70 | struct wacom *wacom = hid_get_drvdata(hdev); |
3bea733a | 71 | |
29b47391 BT |
72 | if (size > WACOM_PKGLEN_MAX) |
73 | return 1; | |
3bea733a | 74 | |
29b47391 | 75 | memcpy(wacom->wacom_wac.data, raw_data, size); |
3bea733a | 76 | |
29b47391 BT |
77 | wacom_wac_irq(&wacom->wacom_wac, size); |
78 | ||
79 | return 0; | |
3bea733a PC |
80 | } |
81 | ||
3bea733a PC |
82 | static int wacom_open(struct input_dev *dev) |
83 | { | |
7791bdae | 84 | struct wacom *wacom = input_get_drvdata(dev); |
29b47391 | 85 | |
dff67416 | 86 | return hid_hw_open(wacom->hdev); |
3bea733a PC |
87 | } |
88 | ||
89 | static void wacom_close(struct input_dev *dev) | |
90 | { | |
7791bdae | 91 | struct wacom *wacom = input_get_drvdata(dev); |
3bea733a | 92 | |
3dad188e BT |
93 | /* |
94 | * wacom->hdev should never be null, but surprisingly, I had the case | |
95 | * once while unplugging the Wacom Wireless Receiver. | |
96 | */ | |
97 | if (wacom->hdev) | |
98 | hid_hw_close(wacom->hdev); | |
3bea733a PC |
99 | } |
100 | ||
115d5e12 | 101 | /* |
198fdee2 | 102 | * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res. |
115d5e12 JG |
103 | */ |
104 | static int wacom_calc_hid_res(int logical_extents, int physical_extents, | |
c669fb2b | 105 | unsigned unit, int exponent) |
115d5e12 | 106 | { |
198fdee2 BT |
107 | struct hid_field field = { |
108 | .logical_maximum = logical_extents, | |
109 | .physical_maximum = physical_extents, | |
110 | .unit = unit, | |
111 | .unit_exponent = exponent, | |
112 | }; | |
113 | ||
114 | return hidinput_calc_abs_res(&field, ABS_X); | |
115d5e12 JG |
115 | } |
116 | ||
c669fb2b BT |
117 | static void wacom_feature_mapping(struct hid_device *hdev, |
118 | struct hid_field *field, struct hid_usage *usage) | |
f393ee2b | 119 | { |
c669fb2b BT |
120 | struct wacom *wacom = hid_get_drvdata(hdev); |
121 | struct wacom_features *features = &wacom->wacom_wac.features; | |
5ae6e89f | 122 | struct hid_data *hid_data = &wacom->wacom_wac.hid_data; |
8ffffd52 BT |
123 | u8 *data; |
124 | int ret; | |
f393ee2b | 125 | |
c669fb2b BT |
126 | switch (usage->hid) { |
127 | case HID_DG_CONTACTMAX: | |
128 | /* leave touch_max as is if predefined */ | |
8ffffd52 BT |
129 | if (!features->touch_max) { |
130 | /* read manually */ | |
131 | data = kzalloc(2, GFP_KERNEL); | |
132 | if (!data) | |
133 | break; | |
134 | data[0] = field->report->id; | |
135 | ret = wacom_get_report(hdev, HID_FEATURE_REPORT, | |
05e8fd92 JG |
136 | data, 2, WAC_CMD_RETRIES); |
137 | if (ret == 2) { | |
8ffffd52 | 138 | features->touch_max = data[1]; |
05e8fd92 JG |
139 | } else { |
140 | features->touch_max = 16; | |
141 | hid_warn(hdev, "wacom_feature_mapping: " | |
142 | "could not get HID_DG_CONTACTMAX, " | |
143 | "defaulting to %d\n", | |
144 | features->touch_max); | |
145 | } | |
8ffffd52 BT |
146 | kfree(data); |
147 | } | |
c669fb2b | 148 | break; |
5ae6e89f BT |
149 | case HID_DG_INPUTMODE: |
150 | /* Ignore if value index is out of bounds. */ | |
151 | if (usage->usage_index >= field->report_count) { | |
152 | dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n"); | |
153 | break; | |
154 | } | |
155 | ||
156 | hid_data->inputmode = field->report->id; | |
157 | hid_data->inputmode_index = usage->usage_index; | |
158 | break; | |
326ea2a9 JG |
159 | |
160 | case HID_UP_DIGITIZER: | |
161 | if (field->report->id == 0x0B && | |
162 | (field->application == WACOM_G9_DIGITIZER || | |
163 | field->application == WACOM_G11_DIGITIZER)) { | |
164 | wacom->wacom_wac.mode_report = field->report->id; | |
165 | wacom->wacom_wac.mode_value = 0; | |
166 | } | |
167 | break; | |
168 | ||
169 | case WACOM_G9_PAGE: | |
170 | case WACOM_G11_PAGE: | |
171 | if (field->report->id == 0x03 && | |
172 | (field->application == WACOM_G9_TOUCHSCREEN || | |
173 | field->application == WACOM_G11_TOUCHSCREEN)) { | |
174 | wacom->wacom_wac.mode_report = field->report->id; | |
175 | wacom->wacom_wac.mode_value = 0; | |
176 | } | |
177 | break; | |
f393ee2b PC |
178 | } |
179 | } | |
180 | ||
428f8588 CB |
181 | /* |
182 | * Interface Descriptor of wacom devices can be incomplete and | |
183 | * inconsistent so wacom_features table is used to store stylus | |
184 | * device's packet lengths, various maximum values, and tablet | |
185 | * resolution based on product ID's. | |
186 | * | |
187 | * For devices that contain 2 interfaces, wacom_features table is | |
188 | * inaccurate for the touch interface. Since the Interface Descriptor | |
189 | * for touch interfaces has pretty complete data, this function exists | |
190 | * to query tablet for this missing information instead of hard coding in | |
191 | * an additional table. | |
192 | * | |
193 | * A typical Interface Descriptor for a stylus will contain a | |
194 | * boot mouse application collection that is not of interest and this | |
195 | * function will ignore it. | |
196 | * | |
197 | * It also contains a digitizer application collection that also is not | |
198 | * of interest since any information it contains would be duplicate | |
199 | * of what is in wacom_features. Usually it defines a report of an array | |
200 | * of bytes that could be used as max length of the stylus packet returned. | |
201 | * If it happens to define a Digitizer-Stylus Physical Collection then | |
202 | * the X and Y logical values contain valid data but it is ignored. | |
203 | * | |
204 | * A typical Interface Descriptor for a touch interface will contain a | |
205 | * Digitizer-Finger Physical Collection which will define both logical | |
206 | * X/Y maximum as well as the physical size of tablet. Since touch | |
207 | * interfaces haven't supported pressure or distance, this is enough | |
208 | * information to override invalid values in the wacom_features table. | |
4134361a | 209 | * |
c669fb2b BT |
210 | * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful |
211 | * data. We deal with them after returning from this function. | |
428f8588 | 212 | */ |
c669fb2b BT |
213 | static void wacom_usage_mapping(struct hid_device *hdev, |
214 | struct hid_field *field, struct hid_usage *usage) | |
545f4e99 | 215 | { |
c669fb2b BT |
216 | struct wacom *wacom = hid_get_drvdata(hdev); |
217 | struct wacom_features *features = &wacom->wacom_wac.features; | |
d97a5522 BT |
218 | bool finger = WACOM_FINGER_FIELD(field); |
219 | bool pen = WACOM_PEN_FIELD(field); | |
e9fc413f | 220 | |
c669fb2b BT |
221 | /* |
222 | * Requiring Stylus Usage will ignore boot mouse | |
223 | * X/Y values and some cases of invalid Digitizer X/Y | |
224 | * values commonly reported. | |
225 | */ | |
042628ab | 226 | if (pen) |
aa86b18c | 227 | features->device_type |= WACOM_DEVICETYPE_PEN; |
042628ab | 228 | else if (finger) |
aa86b18c | 229 | features->device_type |= WACOM_DEVICETYPE_TOUCH; |
042628ab | 230 | else |
c669fb2b BT |
231 | return; |
232 | ||
30ebc1ae PC |
233 | /* |
234 | * Bamboo models do not support HID_DG_CONTACTMAX. | |
235 | * And, Bamboo Pen only descriptor contains touch. | |
236 | */ | |
3b164a00 | 237 | if (features->type > BAMBOO_PT) { |
30ebc1ae PC |
238 | /* ISDv4 touch devices at least supports one touch point */ |
239 | if (finger && !features->touch_max) | |
240 | features->touch_max = 1; | |
241 | } | |
c669fb2b BT |
242 | |
243 | switch (usage->hid) { | |
244 | case HID_GD_X: | |
245 | features->x_max = field->logical_maximum; | |
246 | if (finger) { | |
c669fb2b | 247 | features->x_phy = field->physical_maximum; |
3b164a00 PC |
248 | if ((features->type != BAMBOO_PT) && |
249 | (features->type != BAMBOO_TOUCH)) { | |
c669fb2b BT |
250 | features->unit = field->unit; |
251 | features->unitExpo = field->unit_exponent; | |
545f4e99 | 252 | } |
c669fb2b BT |
253 | } |
254 | break; | |
255 | case HID_GD_Y: | |
256 | features->y_max = field->logical_maximum; | |
257 | if (finger) { | |
258 | features->y_phy = field->physical_maximum; | |
3b164a00 PC |
259 | if ((features->type != BAMBOO_PT) && |
260 | (features->type != BAMBOO_TOUCH)) { | |
c669fb2b BT |
261 | features->unit = field->unit; |
262 | features->unitExpo = field->unit_exponent; | |
263 | } | |
264 | } | |
265 | break; | |
266 | case HID_DG_TIPPRESSURE: | |
267 | if (pen) | |
268 | features->pressure_max = field->logical_maximum; | |
269 | break; | |
270 | } | |
7704ac93 BT |
271 | |
272 | if (features->type == HID_GENERIC) | |
273 | wacom_wac_usage_mapping(hdev, field, usage); | |
c669fb2b | 274 | } |
4134361a | 275 | |
b58ba1ba JG |
276 | static void wacom_post_parse_hid(struct hid_device *hdev, |
277 | struct wacom_features *features) | |
278 | { | |
279 | struct wacom *wacom = hid_get_drvdata(hdev); | |
280 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
281 | ||
282 | if (features->type == HID_GENERIC) { | |
283 | /* Any last-minute generic device setup */ | |
284 | if (features->touch_max > 1) { | |
2a6cdbdd | 285 | input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max, |
b58ba1ba JG |
286 | INPUT_MT_DIRECT); |
287 | } | |
288 | } | |
289 | } | |
290 | ||
c669fb2b BT |
291 | static void wacom_parse_hid(struct hid_device *hdev, |
292 | struct wacom_features *features) | |
293 | { | |
294 | struct hid_report_enum *rep_enum; | |
295 | struct hid_report *hreport; | |
296 | int i, j; | |
297 | ||
298 | /* check features first */ | |
299 | rep_enum = &hdev->report_enum[HID_FEATURE_REPORT]; | |
300 | list_for_each_entry(hreport, &rep_enum->report_list, list) { | |
301 | for (i = 0; i < hreport->maxfield; i++) { | |
302 | /* Ignore if report count is out of bounds. */ | |
303 | if (hreport->field[i]->report_count < 1) | |
304 | continue; | |
305 | ||
306 | for (j = 0; j < hreport->field[i]->maxusage; j++) { | |
307 | wacom_feature_mapping(hdev, hreport->field[i], | |
308 | hreport->field[i]->usage + j); | |
4134361a | 309 | } |
545f4e99 PC |
310 | } |
311 | } | |
312 | ||
c669fb2b BT |
313 | /* now check the input usages */ |
314 | rep_enum = &hdev->report_enum[HID_INPUT_REPORT]; | |
315 | list_for_each_entry(hreport, &rep_enum->report_list, list) { | |
316 | ||
317 | if (!hreport->maxfield) | |
318 | continue; | |
319 | ||
320 | for (i = 0; i < hreport->maxfield; i++) | |
321 | for (j = 0; j < hreport->field[i]->maxusage; j++) | |
322 | wacom_usage_mapping(hdev, hreport->field[i], | |
323 | hreport->field[i]->usage + j); | |
324 | } | |
b58ba1ba JG |
325 | |
326 | wacom_post_parse_hid(hdev, features); | |
545f4e99 PC |
327 | } |
328 | ||
5ae6e89f BT |
329 | static int wacom_hid_set_device_mode(struct hid_device *hdev) |
330 | { | |
331 | struct wacom *wacom = hid_get_drvdata(hdev); | |
332 | struct hid_data *hid_data = &wacom->wacom_wac.hid_data; | |
333 | struct hid_report *r; | |
334 | struct hid_report_enum *re; | |
335 | ||
336 | if (hid_data->inputmode < 0) | |
337 | return 0; | |
338 | ||
339 | re = &(hdev->report_enum[HID_FEATURE_REPORT]); | |
340 | r = re->report_id_hash[hid_data->inputmode]; | |
341 | if (r) { | |
342 | r->field[0]->value[hid_data->inputmode_index] = 2; | |
343 | hid_hw_request(hdev, r, HID_REQ_SET_REPORT); | |
344 | } | |
345 | return 0; | |
346 | } | |
347 | ||
326ea2a9 JG |
348 | static int wacom_set_device_mode(struct hid_device *hdev, |
349 | struct wacom_wac *wacom_wac) | |
3b7307c2 | 350 | { |
326ea2a9 JG |
351 | u8 *rep_data; |
352 | struct hid_report *r; | |
353 | struct hid_report_enum *re; | |
354 | int length; | |
fe494bc2 | 355 | int error = -ENOMEM, limit = 0; |
3b7307c2 | 356 | |
326ea2a9 JG |
357 | if (wacom_wac->mode_report < 0) |
358 | return 0; | |
359 | ||
360 | re = &(hdev->report_enum[HID_FEATURE_REPORT]); | |
361 | r = re->report_id_hash[wacom_wac->mode_report]; | |
362 | if (!r) | |
363 | return -EINVAL; | |
364 | ||
365 | rep_data = hid_alloc_report_buf(r, GFP_KERNEL); | |
3b7307c2 | 366 | if (!rep_data) |
326ea2a9 JG |
367 | return -ENOMEM; |
368 | ||
369 | length = hid_report_len(r); | |
ec67bbed | 370 | |
fe494bc2 | 371 | do { |
326ea2a9 JG |
372 | rep_data[0] = wacom_wac->mode_report; |
373 | rep_data[1] = wacom_wac->mode_value; | |
9937c026 | 374 | |
296b7378 PF |
375 | error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, |
376 | length, 1); | |
3cb83157 | 377 | if (error >= 0) |
27b20a9d | 378 | error = wacom_get_report(hdev, HID_FEATURE_REPORT, |
c64d8834 | 379 | rep_data, length, 1); |
326ea2a9 JG |
380 | } while (error >= 0 && |
381 | rep_data[1] != wacom_wac->mode_report && | |
382 | limit++ < WAC_MSG_RETRIES); | |
fe494bc2 JG |
383 | |
384 | kfree(rep_data); | |
385 | ||
386 | return error < 0 ? error : 0; | |
387 | } | |
388 | ||
f81a1295 BT |
389 | static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed, |
390 | struct wacom_features *features) | |
391 | { | |
387142bb BT |
392 | struct wacom *wacom = hid_get_drvdata(hdev); |
393 | int ret; | |
394 | u8 rep_data[2]; | |
395 | ||
396 | switch (features->type) { | |
397 | case GRAPHIRE_BT: | |
398 | rep_data[0] = 0x03; | |
399 | rep_data[1] = 0x00; | |
296b7378 PF |
400 | ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2, |
401 | 3); | |
387142bb BT |
402 | |
403 | if (ret >= 0) { | |
404 | rep_data[0] = speed == 0 ? 0x05 : 0x06; | |
405 | rep_data[1] = 0x00; | |
406 | ||
407 | ret = wacom_set_report(hdev, HID_FEATURE_REPORT, | |
296b7378 | 408 | rep_data, 2, 3); |
387142bb BT |
409 | |
410 | if (ret >= 0) { | |
411 | wacom->wacom_wac.bt_high_speed = speed; | |
412 | return 0; | |
413 | } | |
414 | } | |
415 | ||
416 | /* | |
417 | * Note that if the raw queries fail, it's not a hard failure | |
418 | * and it is safe to continue | |
419 | */ | |
420 | hid_warn(hdev, "failed to poke device, command %d, err %d\n", | |
421 | rep_data[0], ret); | |
422 | break; | |
81af7e61 BT |
423 | case INTUOS4WL: |
424 | if (speed == 1) | |
425 | wacom->wacom_wac.bt_features &= ~0x20; | |
426 | else | |
427 | wacom->wacom_wac.bt_features |= 0x20; | |
428 | ||
429 | rep_data[0] = 0x03; | |
430 | rep_data[1] = wacom->wacom_wac.bt_features; | |
431 | ||
296b7378 PF |
432 | ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2, |
433 | 1); | |
81af7e61 BT |
434 | if (ret >= 0) |
435 | wacom->wacom_wac.bt_high_speed = speed; | |
436 | break; | |
387142bb BT |
437 | } |
438 | ||
f81a1295 BT |
439 | return 0; |
440 | } | |
441 | ||
fe494bc2 JG |
442 | /* |
443 | * Switch the tablet into its most-capable mode. Wacom tablets are | |
444 | * typically configured to power-up in a mode which sends mouse-like | |
445 | * reports to the OS. To get absolute position, pressure data, etc. | |
446 | * from the tablet, it is necessary to switch the tablet out of this | |
447 | * mode and into one which sends the full range of tablet data. | |
448 | */ | |
27b20a9d BT |
449 | static int wacom_query_tablet_data(struct hid_device *hdev, |
450 | struct wacom_features *features) | |
fe494bc2 | 451 | { |
326ea2a9 JG |
452 | struct wacom *wacom = hid_get_drvdata(hdev); |
453 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
454 | ||
f81a1295 BT |
455 | if (hdev->bus == BUS_BLUETOOTH) |
456 | return wacom_bt_query_tablet_data(hdev, 1, features); | |
457 | ||
326ea2a9 JG |
458 | if (features->type != HID_GENERIC) { |
459 | if (features->device_type & WACOM_DEVICETYPE_TOUCH) { | |
460 | if (features->type > TABLETPC) { | |
461 | /* MT Tablet PC touch */ | |
462 | wacom_wac->mode_report = 3; | |
463 | wacom_wac->mode_value = 4; | |
464 | } else if (features->type == WACOM_24HDT) { | |
465 | wacom_wac->mode_report = 18; | |
466 | wacom_wac->mode_value = 2; | |
467 | } else if (features->type == WACOM_27QHDT) { | |
468 | wacom_wac->mode_report = 131; | |
469 | wacom_wac->mode_value = 2; | |
470 | } else if (features->type == BAMBOO_PAD) { | |
471 | wacom_wac->mode_report = 2; | |
472 | wacom_wac->mode_value = 2; | |
473 | } | |
474 | } else if (features->device_type & WACOM_DEVICETYPE_PEN) { | |
475 | if (features->type <= BAMBOO_PT) { | |
476 | wacom_wac->mode_report = 2; | |
477 | wacom_wac->mode_value = 2; | |
478 | } | |
1963518b | 479 | } |
ec67bbed | 480 | } |
3b7307c2 | 481 | |
326ea2a9 JG |
482 | wacom_set_device_mode(hdev, wacom_wac); |
483 | ||
484 | if (features->type == HID_GENERIC) | |
485 | return wacom_hid_set_device_mode(hdev); | |
486 | ||
fe494bc2 | 487 | return 0; |
3b7307c2 DT |
488 | } |
489 | ||
c669fb2b | 490 | static void wacom_retrieve_hid_descriptor(struct hid_device *hdev, |
1963518b | 491 | struct wacom_features *features) |
ec67bbed | 492 | { |
27b20a9d BT |
493 | struct wacom *wacom = hid_get_drvdata(hdev); |
494 | struct usb_interface *intf = wacom->intf; | |
ec67bbed | 495 | |
fed87e65 | 496 | /* default features */ |
fed87e65 HR |
497 | features->x_fuzz = 4; |
498 | features->y_fuzz = 4; | |
499 | features->pressure_fuzz = 0; | |
bef7e200 JG |
500 | features->distance_fuzz = 1; |
501 | features->tilt_fuzz = 1; | |
ec67bbed | 502 | |
d3825d51 CB |
503 | /* |
504 | * The wireless device HID is basic and layout conflicts with | |
505 | * other tablets (monitor and touch interface can look like pen). | |
506 | * Skip the query for this type and modify defaults based on | |
507 | * interface number. | |
508 | */ | |
509 | if (features->type == WIRELESS) { | |
3f14a63a | 510 | if (intf->cur_altsetting->desc.bInterfaceNumber == 0) |
ccad85cc | 511 | features->device_type = WACOM_DEVICETYPE_WL_MONITOR; |
3f14a63a | 512 | else |
aa86b18c | 513 | features->device_type = WACOM_DEVICETYPE_NONE; |
3f14a63a | 514 | return; |
d3825d51 CB |
515 | } |
516 | ||
c669fb2b | 517 | wacom_parse_hid(hdev, features); |
ec67bbed PC |
518 | } |
519 | ||
4451e088 | 520 | struct wacom_hdev_data { |
4492efff PC |
521 | struct list_head list; |
522 | struct kref kref; | |
4451e088 | 523 | struct hid_device *dev; |
4492efff PC |
524 | struct wacom_shared shared; |
525 | }; | |
526 | ||
527 | static LIST_HEAD(wacom_udev_list); | |
528 | static DEFINE_MUTEX(wacom_udev_list_lock); | |
529 | ||
4451e088 BT |
530 | static bool wacom_are_sibling(struct hid_device *hdev, |
531 | struct hid_device *sibling) | |
aea2bf6a | 532 | { |
4451e088 BT |
533 | struct wacom *wacom = hid_get_drvdata(hdev); |
534 | struct wacom_features *features = &wacom->wacom_wac.features; | |
535 | int vid = features->oVid; | |
536 | int pid = features->oPid; | |
537 | int n1,n2; | |
538 | ||
539 | if (vid == 0 && pid == 0) { | |
540 | vid = hdev->vendor; | |
541 | pid = hdev->product; | |
542 | } | |
aea2bf6a | 543 | |
4451e088 BT |
544 | if (vid != sibling->vendor || pid != sibling->product) |
545 | return false; | |
aea2bf6a | 546 | |
4451e088 BT |
547 | /* Compare the physical path. */ |
548 | n1 = strrchr(hdev->phys, '.') - hdev->phys; | |
549 | n2 = strrchr(sibling->phys, '.') - sibling->phys; | |
550 | if (n1 != n2 || n1 <= 0 || n2 <= 0) | |
551 | return false; | |
aea2bf6a | 552 | |
4451e088 | 553 | return !strncmp(hdev->phys, sibling->phys, n1); |
aea2bf6a JG |
554 | } |
555 | ||
4451e088 | 556 | static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev) |
4492efff | 557 | { |
4451e088 | 558 | struct wacom_hdev_data *data; |
4492efff PC |
559 | |
560 | list_for_each_entry(data, &wacom_udev_list, list) { | |
4451e088 | 561 | if (wacom_are_sibling(hdev, data->dev)) { |
4492efff PC |
562 | kref_get(&data->kref); |
563 | return data; | |
564 | } | |
565 | } | |
566 | ||
567 | return NULL; | |
568 | } | |
569 | ||
1c817c83 BT |
570 | static void wacom_release_shared_data(struct kref *kref) |
571 | { | |
572 | struct wacom_hdev_data *data = | |
573 | container_of(kref, struct wacom_hdev_data, kref); | |
574 | ||
575 | mutex_lock(&wacom_udev_list_lock); | |
576 | list_del(&data->list); | |
577 | mutex_unlock(&wacom_udev_list_lock); | |
578 | ||
579 | kfree(data); | |
580 | } | |
581 | ||
582 | static void wacom_remove_shared_data(void *res) | |
583 | { | |
584 | struct wacom *wacom = res; | |
585 | struct wacom_hdev_data *data; | |
586 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
587 | ||
588 | if (wacom_wac->shared) { | |
589 | data = container_of(wacom_wac->shared, struct wacom_hdev_data, | |
590 | shared); | |
591 | ||
592 | if (wacom_wac->shared->touch == wacom->hdev) | |
593 | wacom_wac->shared->touch = NULL; | |
594 | else if (wacom_wac->shared->pen == wacom->hdev) | |
595 | wacom_wac->shared->pen = NULL; | |
596 | ||
597 | kref_put(&data->kref, wacom_release_shared_data); | |
598 | wacom_wac->shared = NULL; | |
599 | } | |
600 | } | |
601 | ||
4451e088 | 602 | static int wacom_add_shared_data(struct hid_device *hdev) |
4492efff | 603 | { |
4451e088 BT |
604 | struct wacom *wacom = hid_get_drvdata(hdev); |
605 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
606 | struct wacom_hdev_data *data; | |
4492efff PC |
607 | int retval = 0; |
608 | ||
609 | mutex_lock(&wacom_udev_list_lock); | |
610 | ||
4451e088 | 611 | data = wacom_get_hdev_data(hdev); |
4492efff | 612 | if (!data) { |
4451e088 | 613 | data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL); |
4492efff PC |
614 | if (!data) { |
615 | retval = -ENOMEM; | |
616 | goto out; | |
617 | } | |
618 | ||
619 | kref_init(&data->kref); | |
4451e088 | 620 | data->dev = hdev; |
4492efff PC |
621 | list_add_tail(&data->list, &wacom_udev_list); |
622 | } | |
623 | ||
4451e088 | 624 | wacom_wac->shared = &data->shared; |
4492efff | 625 | |
1c817c83 BT |
626 | retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom); |
627 | if (retval) { | |
628 | mutex_unlock(&wacom_udev_list_lock); | |
629 | wacom_remove_shared_data(wacom); | |
630 | return retval; | |
631 | } | |
632 | ||
aa86b18c | 633 | if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) |
a97ac104 | 634 | wacom_wac->shared->touch = hdev; |
aa86b18c | 635 | else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN) |
a97ac104 BT |
636 | wacom_wac->shared->pen = hdev; |
637 | ||
4492efff PC |
638 | out: |
639 | mutex_unlock(&wacom_udev_list_lock); | |
640 | return retval; | |
641 | } | |
642 | ||
5d7e7d47 EH |
643 | static int wacom_led_control(struct wacom *wacom) |
644 | { | |
645 | unsigned char *buf; | |
9b5b95dd | 646 | int retval; |
912ca216 PC |
647 | unsigned char report_id = WAC_CMD_LED_CONTROL; |
648 | int buf_size = 9; | |
5d7e7d47 | 649 | |
97f5541f BT |
650 | if (!hid_get_drvdata(wacom->hdev)) |
651 | return -ENODEV; | |
652 | ||
a50aac71 BT |
653 | if (!wacom->led.groups) |
654 | return -ENOTSUPP; | |
655 | ||
912ca216 PC |
656 | if (wacom->wacom_wac.pid) { /* wireless connected */ |
657 | report_id = WAC_CMD_WL_LED_CONTROL; | |
658 | buf_size = 13; | |
659 | } | |
660 | buf = kzalloc(buf_size, GFP_KERNEL); | |
5d7e7d47 EH |
661 | if (!buf) |
662 | return -ENOMEM; | |
663 | ||
9b5b95dd | 664 | if (wacom->wacom_wac.features.type >= INTUOS5S && |
9a35c411 | 665 | wacom->wacom_wac.features.type <= INTUOSPL) { |
9b5b95dd JG |
666 | /* |
667 | * Touch Ring and crop mark LED luminance may take on | |
668 | * one of four values: | |
669 | * 0 = Low; 1 = Medium; 2 = High; 3 = Off | |
670 | */ | |
a50aac71 | 671 | int ring_led = wacom->led.groups[0].select & 0x03; |
9b5b95dd JG |
672 | int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03; |
673 | int crop_lum = 0; | |
912ca216 PC |
674 | unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led); |
675 | ||
676 | buf[0] = report_id; | |
677 | if (wacom->wacom_wac.pid) { | |
678 | wacom_get_report(wacom->hdev, HID_FEATURE_REPORT, | |
679 | buf, buf_size, WAC_CMD_RETRIES); | |
680 | buf[0] = report_id; | |
681 | buf[4] = led_bits; | |
682 | } else | |
683 | buf[1] = led_bits; | |
9b5b95dd JG |
684 | } |
685 | else { | |
a50aac71 | 686 | int led = wacom->led.groups[0].select | 0x4; |
9b5b95dd JG |
687 | |
688 | if (wacom->wacom_wac.features.type == WACOM_21UX2 || | |
689 | wacom->wacom_wac.features.type == WACOM_24HD) | |
a50aac71 | 690 | led |= (wacom->led.groups[1].select << 4) | 0x40; |
9b5b95dd | 691 | |
912ca216 | 692 | buf[0] = report_id; |
9b5b95dd JG |
693 | buf[1] = led; |
694 | buf[2] = wacom->led.llv; | |
695 | buf[3] = wacom->led.hlv; | |
696 | buf[4] = wacom->led.img_lum; | |
697 | } | |
5d7e7d47 | 698 | |
912ca216 | 699 | retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size, |
296b7378 | 700 | WAC_CMD_RETRIES); |
5d7e7d47 EH |
701 | kfree(buf); |
702 | ||
703 | return retval; | |
704 | } | |
705 | ||
849e2f06 BT |
706 | static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id, |
707 | const unsigned len, const void *img) | |
5d7e7d47 EH |
708 | { |
709 | unsigned char *buf; | |
710 | int i, retval; | |
849e2f06 | 711 | const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */ |
5d7e7d47 | 712 | |
849e2f06 | 713 | buf = kzalloc(chunk_len + 3 , GFP_KERNEL); |
5d7e7d47 EH |
714 | if (!buf) |
715 | return -ENOMEM; | |
716 | ||
717 | /* Send 'start' command */ | |
718 | buf[0] = WAC_CMD_ICON_START; | |
719 | buf[1] = 1; | |
296b7378 PF |
720 | retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2, |
721 | WAC_CMD_RETRIES); | |
5d7e7d47 EH |
722 | if (retval < 0) |
723 | goto out; | |
724 | ||
849e2f06 | 725 | buf[0] = xfer_id; |
5d7e7d47 EH |
726 | buf[1] = button_id & 0x07; |
727 | for (i = 0; i < 4; i++) { | |
728 | buf[2] = i; | |
849e2f06 | 729 | memcpy(buf + 3, img + i * chunk_len, chunk_len); |
5d7e7d47 | 730 | |
27b20a9d | 731 | retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, |
296b7378 | 732 | buf, chunk_len + 3, WAC_CMD_RETRIES); |
5d7e7d47 EH |
733 | if (retval < 0) |
734 | break; | |
735 | } | |
736 | ||
737 | /* Send 'stop' */ | |
738 | buf[0] = WAC_CMD_ICON_START; | |
739 | buf[1] = 0; | |
296b7378 PF |
740 | wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2, |
741 | WAC_CMD_RETRIES); | |
5d7e7d47 EH |
742 | |
743 | out: | |
744 | kfree(buf); | |
745 | return retval; | |
746 | } | |
747 | ||
09e7d941 | 748 | static ssize_t wacom_led_select_store(struct device *dev, int set_id, |
5d7e7d47 EH |
749 | const char *buf, size_t count) |
750 | { | |
ee79a8f8 | 751 | struct hid_device *hdev = to_hid_device(dev); |
29b47391 | 752 | struct wacom *wacom = hid_get_drvdata(hdev); |
5d7e7d47 EH |
753 | unsigned int id; |
754 | int err; | |
755 | ||
756 | err = kstrtouint(buf, 10, &id); | |
757 | if (err) | |
758 | return err; | |
759 | ||
760 | mutex_lock(&wacom->lock); | |
761 | ||
a50aac71 | 762 | wacom->led.groups[set_id].select = id & 0x3; |
5d7e7d47 EH |
763 | err = wacom_led_control(wacom); |
764 | ||
765 | mutex_unlock(&wacom->lock); | |
766 | ||
767 | return err < 0 ? err : count; | |
768 | } | |
769 | ||
09e7d941 PC |
770 | #define DEVICE_LED_SELECT_ATTR(SET_ID) \ |
771 | static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \ | |
772 | struct device_attribute *attr, const char *buf, size_t count) \ | |
773 | { \ | |
774 | return wacom_led_select_store(dev, SET_ID, buf, count); \ | |
775 | } \ | |
04c59abd PC |
776 | static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \ |
777 | struct device_attribute *attr, char *buf) \ | |
778 | { \ | |
ee79a8f8 | 779 | struct hid_device *hdev = to_hid_device(dev);\ |
29b47391 | 780 | struct wacom *wacom = hid_get_drvdata(hdev); \ |
37449adc | 781 | return scnprintf(buf, PAGE_SIZE, "%d\n", \ |
a50aac71 | 782 | wacom->led.groups[SET_ID].select); \ |
04c59abd | 783 | } \ |
e0984bc3 | 784 | static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \ |
04c59abd | 785 | wacom_led##SET_ID##_select_show, \ |
09e7d941 PC |
786 | wacom_led##SET_ID##_select_store) |
787 | ||
788 | DEVICE_LED_SELECT_ATTR(0); | |
789 | DEVICE_LED_SELECT_ATTR(1); | |
5d7e7d47 EH |
790 | |
791 | static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest, | |
792 | const char *buf, size_t count) | |
793 | { | |
794 | unsigned int value; | |
795 | int err; | |
796 | ||
797 | err = kstrtouint(buf, 10, &value); | |
798 | if (err) | |
799 | return err; | |
800 | ||
801 | mutex_lock(&wacom->lock); | |
802 | ||
803 | *dest = value & 0x7f; | |
804 | err = wacom_led_control(wacom); | |
805 | ||
806 | mutex_unlock(&wacom->lock); | |
807 | ||
808 | return err < 0 ? err : count; | |
809 | } | |
810 | ||
811 | #define DEVICE_LUMINANCE_ATTR(name, field) \ | |
812 | static ssize_t wacom_##name##_luminance_store(struct device *dev, \ | |
813 | struct device_attribute *attr, const char *buf, size_t count) \ | |
814 | { \ | |
ee79a8f8 | 815 | struct hid_device *hdev = to_hid_device(dev);\ |
29b47391 | 816 | struct wacom *wacom = hid_get_drvdata(hdev); \ |
5d7e7d47 EH |
817 | \ |
818 | return wacom_luminance_store(wacom, &wacom->led.field, \ | |
819 | buf, count); \ | |
820 | } \ | |
37449adc PC |
821 | static ssize_t wacom_##name##_luminance_show(struct device *dev, \ |
822 | struct device_attribute *attr, char *buf) \ | |
823 | { \ | |
824 | struct wacom *wacom = dev_get_drvdata(dev); \ | |
825 | return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \ | |
826 | } \ | |
e0984bc3 | 827 | static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \ |
37449adc PC |
828 | wacom_##name##_luminance_show, \ |
829 | wacom_##name##_luminance_store) | |
5d7e7d47 EH |
830 | |
831 | DEVICE_LUMINANCE_ATTR(status0, llv); | |
832 | DEVICE_LUMINANCE_ATTR(status1, hlv); | |
833 | DEVICE_LUMINANCE_ATTR(buttons, img_lum); | |
834 | ||
835 | static ssize_t wacom_button_image_store(struct device *dev, int button_id, | |
836 | const char *buf, size_t count) | |
837 | { | |
ee79a8f8 | 838 | struct hid_device *hdev = to_hid_device(dev); |
29b47391 | 839 | struct wacom *wacom = hid_get_drvdata(hdev); |
5d7e7d47 | 840 | int err; |
849e2f06 BT |
841 | unsigned len; |
842 | u8 xfer_id; | |
843 | ||
844 | if (hdev->bus == BUS_BLUETOOTH) { | |
845 | len = 256; | |
846 | xfer_id = WAC_CMD_ICON_BT_XFER; | |
847 | } else { | |
848 | len = 1024; | |
849 | xfer_id = WAC_CMD_ICON_XFER; | |
850 | } | |
5d7e7d47 | 851 | |
849e2f06 | 852 | if (count != len) |
5d7e7d47 EH |
853 | return -EINVAL; |
854 | ||
855 | mutex_lock(&wacom->lock); | |
856 | ||
849e2f06 | 857 | err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf); |
5d7e7d47 EH |
858 | |
859 | mutex_unlock(&wacom->lock); | |
860 | ||
861 | return err < 0 ? err : count; | |
862 | } | |
863 | ||
864 | #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \ | |
865 | static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \ | |
866 | struct device_attribute *attr, const char *buf, size_t count) \ | |
867 | { \ | |
868 | return wacom_button_image_store(dev, BUTTON_ID, buf, count); \ | |
869 | } \ | |
e0984bc3 | 870 | static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \ |
5d7e7d47 EH |
871 | NULL, wacom_btnimg##BUTTON_ID##_store) |
872 | ||
873 | DEVICE_BTNIMG_ATTR(0); | |
874 | DEVICE_BTNIMG_ATTR(1); | |
875 | DEVICE_BTNIMG_ATTR(2); | |
876 | DEVICE_BTNIMG_ATTR(3); | |
877 | DEVICE_BTNIMG_ATTR(4); | |
878 | DEVICE_BTNIMG_ATTR(5); | |
879 | DEVICE_BTNIMG_ATTR(6); | |
880 | DEVICE_BTNIMG_ATTR(7); | |
881 | ||
09e7d941 PC |
882 | static struct attribute *cintiq_led_attrs[] = { |
883 | &dev_attr_status_led0_select.attr, | |
884 | &dev_attr_status_led1_select.attr, | |
885 | NULL | |
886 | }; | |
887 | ||
888 | static struct attribute_group cintiq_led_attr_group = { | |
889 | .name = "wacom_led", | |
890 | .attrs = cintiq_led_attrs, | |
891 | }; | |
892 | ||
893 | static struct attribute *intuos4_led_attrs[] = { | |
5d7e7d47 EH |
894 | &dev_attr_status0_luminance.attr, |
895 | &dev_attr_status1_luminance.attr, | |
09e7d941 | 896 | &dev_attr_status_led0_select.attr, |
5d7e7d47 EH |
897 | &dev_attr_buttons_luminance.attr, |
898 | &dev_attr_button0_rawimg.attr, | |
899 | &dev_attr_button1_rawimg.attr, | |
900 | &dev_attr_button2_rawimg.attr, | |
901 | &dev_attr_button3_rawimg.attr, | |
902 | &dev_attr_button4_rawimg.attr, | |
903 | &dev_attr_button5_rawimg.attr, | |
904 | &dev_attr_button6_rawimg.attr, | |
905 | &dev_attr_button7_rawimg.attr, | |
906 | NULL | |
907 | }; | |
908 | ||
09e7d941 | 909 | static struct attribute_group intuos4_led_attr_group = { |
5d7e7d47 | 910 | .name = "wacom_led", |
09e7d941 | 911 | .attrs = intuos4_led_attrs, |
5d7e7d47 EH |
912 | }; |
913 | ||
9b5b95dd JG |
914 | static struct attribute *intuos5_led_attrs[] = { |
915 | &dev_attr_status0_luminance.attr, | |
916 | &dev_attr_status_led0_select.attr, | |
917 | NULL | |
918 | }; | |
919 | ||
920 | static struct attribute_group intuos5_led_attr_group = { | |
921 | .name = "wacom_led", | |
922 | .attrs = intuos5_led_attrs, | |
923 | }; | |
924 | ||
2df68a88 BT |
925 | struct wacom_sysfs_group_devres { |
926 | struct attribute_group *group; | |
927 | struct kobject *root; | |
928 | }; | |
929 | ||
930 | static void wacom_devm_sysfs_group_release(struct device *dev, void *res) | |
931 | { | |
932 | struct wacom_sysfs_group_devres *devres = res; | |
933 | struct kobject *kobj = devres->root; | |
934 | ||
935 | dev_dbg(dev, "%s: dropping reference to %s\n", | |
936 | __func__, devres->group->name); | |
937 | sysfs_remove_group(kobj, devres->group); | |
938 | } | |
939 | ||
f9036bd4 BT |
940 | static int __wacom_devm_sysfs_create_group(struct wacom *wacom, |
941 | struct kobject *root, | |
942 | struct attribute_group *group) | |
2df68a88 BT |
943 | { |
944 | struct wacom_sysfs_group_devres *devres; | |
945 | int error; | |
946 | ||
947 | devres = devres_alloc(wacom_devm_sysfs_group_release, | |
948 | sizeof(struct wacom_sysfs_group_devres), | |
949 | GFP_KERNEL); | |
950 | if (!devres) | |
951 | return -ENOMEM; | |
952 | ||
953 | devres->group = group; | |
f9036bd4 | 954 | devres->root = root; |
2df68a88 BT |
955 | |
956 | error = sysfs_create_group(devres->root, group); | |
957 | if (error) | |
958 | return error; | |
959 | ||
960 | devres_add(&wacom->hdev->dev, devres); | |
961 | ||
962 | return 0; | |
963 | } | |
964 | ||
f9036bd4 BT |
965 | static int wacom_devm_sysfs_create_group(struct wacom *wacom, |
966 | struct attribute_group *group) | |
967 | { | |
968 | return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj, | |
969 | group); | |
970 | } | |
971 | ||
34736aa9 | 972 | enum led_brightness wacom_leds_brightness_get(struct wacom_led *led) |
97f5541f BT |
973 | { |
974 | struct wacom *wacom = led->wacom; | |
975 | ||
976 | if (wacom->led.max_hlv) | |
977 | return led->hlv * LED_FULL / wacom->led.max_hlv; | |
978 | ||
979 | if (wacom->led.max_llv) | |
980 | return led->llv * LED_FULL / wacom->led.max_llv; | |
981 | ||
982 | /* device doesn't support brightness tuning */ | |
983 | return LED_FULL; | |
984 | } | |
985 | ||
986 | static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev) | |
987 | { | |
988 | struct wacom_led *led = container_of(cdev, struct wacom_led, cdev); | |
989 | struct wacom *wacom = led->wacom; | |
990 | ||
991 | if (wacom->led.groups[led->group].select != led->id) | |
992 | return LED_OFF; | |
993 | ||
994 | return wacom_leds_brightness_get(led); | |
995 | } | |
996 | ||
997 | static int wacom_led_brightness_set(struct led_classdev *cdev, | |
998 | enum led_brightness brightness) | |
999 | { | |
1000 | struct wacom_led *led = container_of(cdev, struct wacom_led, cdev); | |
1001 | struct wacom *wacom = led->wacom; | |
1002 | int error; | |
1003 | ||
1004 | mutex_lock(&wacom->lock); | |
1005 | ||
1006 | if (!wacom->led.groups || (brightness == LED_OFF && | |
1007 | wacom->led.groups[led->group].select != led->id)) { | |
1008 | error = 0; | |
1009 | goto out; | |
1010 | } | |
1011 | ||
1012 | led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL; | |
1013 | led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL; | |
1014 | ||
1015 | wacom->led.groups[led->group].select = led->id; | |
1016 | ||
1017 | error = wacom_led_control(wacom); | |
1018 | ||
1019 | out: | |
1020 | mutex_unlock(&wacom->lock); | |
1021 | ||
1022 | return error; | |
1023 | } | |
1024 | ||
1025 | static void wacom_led_readonly_brightness_set(struct led_classdev *cdev, | |
1026 | enum led_brightness brightness) | |
1027 | { | |
1028 | } | |
1029 | ||
1030 | static int wacom_led_register_one(struct device *dev, struct wacom *wacom, | |
1031 | struct wacom_led *led, unsigned int group, | |
1032 | unsigned int id, bool read_only) | |
1033 | { | |
1034 | int error; | |
1035 | char *name; | |
1036 | ||
1037 | name = devm_kasprintf(dev, GFP_KERNEL, | |
1038 | "%s::wacom-%d.%d", | |
1039 | dev_name(dev), | |
1040 | group, | |
1041 | id); | |
1042 | if (!name) | |
1043 | return -ENOMEM; | |
1044 | ||
34736aa9 BT |
1045 | if (!read_only) { |
1046 | led->trigger.name = name; | |
1047 | error = devm_led_trigger_register(dev, &led->trigger); | |
1048 | if (error) { | |
1049 | hid_err(wacom->hdev, | |
1050 | "failed to register LED trigger %s: %d\n", | |
1051 | led->cdev.name, error); | |
1052 | return error; | |
1053 | } | |
1054 | } | |
1055 | ||
97f5541f BT |
1056 | led->group = group; |
1057 | led->id = id; | |
1058 | led->wacom = wacom; | |
1059 | led->llv = wacom->led.llv; | |
1060 | led->hlv = wacom->led.hlv; | |
1061 | led->cdev.name = name; | |
1062 | led->cdev.max_brightness = LED_FULL; | |
1063 | led->cdev.flags = LED_HW_PLUGGABLE; | |
1064 | led->cdev.brightness_get = __wacom_led_brightness_get; | |
34736aa9 | 1065 | if (!read_only) { |
97f5541f | 1066 | led->cdev.brightness_set_blocking = wacom_led_brightness_set; |
34736aa9 BT |
1067 | led->cdev.default_trigger = led->cdev.name; |
1068 | } else { | |
97f5541f | 1069 | led->cdev.brightness_set = wacom_led_readonly_brightness_set; |
34736aa9 | 1070 | } |
97f5541f BT |
1071 | |
1072 | error = devm_led_classdev_register(dev, &led->cdev); | |
1073 | if (error) { | |
1074 | hid_err(wacom->hdev, | |
1075 | "failed to register LED %s: %d\n", | |
1076 | led->cdev.name, error); | |
1077 | led->cdev.name = NULL; | |
1078 | return error; | |
1079 | } | |
1080 | ||
1081 | return 0; | |
1082 | } | |
1083 | ||
589e5060 BT |
1084 | static void wacom_led_groups_release_one(void *data) |
1085 | { | |
1086 | struct wacom_group_leds *group = data; | |
1087 | ||
1088 | devres_release_group(group->dev, group); | |
1089 | } | |
1090 | ||
97f5541f BT |
1091 | static int wacom_led_groups_alloc_and_register_one(struct device *dev, |
1092 | struct wacom *wacom, | |
1093 | int group_id, int count, | |
1094 | bool read_only) | |
1095 | { | |
1096 | struct wacom_led *leds; | |
1097 | int i, error; | |
1098 | ||
1099 | if (group_id >= wacom->led.count || count <= 0) | |
1100 | return -EINVAL; | |
1101 | ||
1102 | if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL)) | |
1103 | return -ENOMEM; | |
1104 | ||
1105 | leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL); | |
1106 | if (!leds) { | |
1107 | error = -ENOMEM; | |
1108 | goto err; | |
1109 | } | |
1110 | ||
1111 | wacom->led.groups[group_id].leds = leds; | |
1112 | wacom->led.groups[group_id].count = count; | |
1113 | ||
1114 | for (i = 0; i < count; i++) { | |
1115 | error = wacom_led_register_one(dev, wacom, &leds[i], | |
1116 | group_id, i, read_only); | |
1117 | if (error) | |
1118 | goto err; | |
1119 | } | |
1120 | ||
589e5060 BT |
1121 | wacom->led.groups[group_id].dev = dev; |
1122 | ||
1123 | devres_close_group(dev, &wacom->led.groups[group_id]); | |
1124 | ||
1125 | /* | |
1126 | * There is a bug (?) in devm_led_classdev_register() in which its | |
1127 | * increments the refcount of the parent. If the parent is an input | |
1128 | * device, that means the ref count never reaches 0 when | |
1129 | * devm_input_device_release() gets called. | |
1130 | * This means that the LEDs are still there after disconnect. | |
1131 | * Manually force the release of the group so that the leds are released | |
1132 | * once we are done using them. | |
1133 | */ | |
1134 | error = devm_add_action_or_reset(&wacom->hdev->dev, | |
1135 | wacom_led_groups_release_one, | |
1136 | &wacom->led.groups[group_id]); | |
1137 | if (error) | |
1138 | return error; | |
1139 | ||
97f5541f BT |
1140 | return 0; |
1141 | ||
1142 | err: | |
1143 | devres_release_group(dev, &wacom->led.groups[group_id]); | |
1144 | return error; | |
1145 | } | |
1146 | ||
34736aa9 BT |
1147 | struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id, |
1148 | unsigned int id) | |
1149 | { | |
1150 | struct wacom_group_leds *group; | |
1151 | ||
1152 | if (group_id >= wacom->led.count) | |
1153 | return NULL; | |
1154 | ||
1155 | group = &wacom->led.groups[group_id]; | |
1156 | ||
1157 | if (!group->leds) | |
1158 | return NULL; | |
1159 | ||
1160 | id %= group->count; | |
1161 | ||
1162 | return &group->leds[id]; | |
1163 | } | |
1164 | ||
1165 | /** | |
1166 | * wacom_led_next: gives the next available led with a wacom trigger. | |
1167 | * | |
1168 | * returns the next available struct wacom_led which has its default trigger | |
1169 | * or the current one if none is available. | |
1170 | */ | |
1171 | struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur) | |
1172 | { | |
1173 | struct wacom_led *next_led; | |
1174 | int group, next; | |
1175 | ||
1176 | if (!wacom || !cur) | |
1177 | return NULL; | |
1178 | ||
1179 | group = cur->group; | |
1180 | next = cur->id; | |
1181 | ||
1182 | do { | |
1183 | next_led = wacom_led_find(wacom, group, ++next); | |
1184 | if (!next_led || next_led == cur) | |
1185 | return next_led; | |
1186 | } while (next_led->cdev.trigger != &next_led->trigger); | |
1187 | ||
1188 | return next_led; | |
1189 | } | |
1190 | ||
a50aac71 BT |
1191 | static void wacom_led_groups_release(void *data) |
1192 | { | |
1193 | struct wacom *wacom = data; | |
1194 | ||
1195 | wacom->led.groups = NULL; | |
97f5541f | 1196 | wacom->led.count = 0; |
a50aac71 BT |
1197 | } |
1198 | ||
1199 | static int wacom_led_groups_allocate(struct wacom *wacom, int count) | |
1200 | { | |
97f5541f | 1201 | struct device *dev = &wacom->hdev->dev; |
a50aac71 BT |
1202 | struct wacom_group_leds *groups; |
1203 | int error; | |
1204 | ||
97f5541f | 1205 | groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count, |
a50aac71 BT |
1206 | GFP_KERNEL); |
1207 | if (!groups) | |
1208 | return -ENOMEM; | |
1209 | ||
97f5541f | 1210 | error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom); |
a50aac71 BT |
1211 | if (error) |
1212 | return error; | |
1213 | ||
1214 | wacom->led.groups = groups; | |
97f5541f BT |
1215 | wacom->led.count = count; |
1216 | ||
1217 | return 0; | |
1218 | } | |
1219 | ||
1220 | static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count, | |
1221 | int led_per_group, bool read_only) | |
1222 | { | |
1223 | struct device *dev; | |
1224 | int i, error; | |
1225 | ||
1226 | if (!wacom->wacom_wac.pad_input) | |
1227 | return -EINVAL; | |
1228 | ||
1229 | dev = &wacom->wacom_wac.pad_input->dev; | |
1230 | ||
1231 | error = wacom_led_groups_allocate(wacom, group_count); | |
1232 | if (error) | |
1233 | return error; | |
1234 | ||
1235 | for (i = 0; i < group_count; i++) { | |
1236 | error = wacom_led_groups_alloc_and_register_one(dev, wacom, i, | |
1237 | led_per_group, | |
1238 | read_only); | |
1239 | if (error) | |
1240 | return error; | |
1241 | } | |
a50aac71 BT |
1242 | |
1243 | return 0; | |
1244 | } | |
1245 | ||
5d7e7d47 EH |
1246 | static int wacom_initialize_leds(struct wacom *wacom) |
1247 | { | |
1248 | int error; | |
1249 | ||
862cf553 JG |
1250 | if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD)) |
1251 | return 0; | |
1252 | ||
09e7d941 PC |
1253 | /* Initialize default values */ |
1254 | switch (wacom->wacom_wac.features.type) { | |
a19fc986 | 1255 | case INTUOS4S: |
09e7d941 | 1256 | case INTUOS4: |
81af7e61 | 1257 | case INTUOS4WL: |
09e7d941 | 1258 | case INTUOS4L: |
f4fa9a6d | 1259 | wacom->led.llv = 10; |
5d7e7d47 | 1260 | wacom->led.hlv = 20; |
97f5541f BT |
1261 | wacom->led.max_llv = 127; |
1262 | wacom->led.max_hlv = 127; | |
5d7e7d47 | 1263 | wacom->led.img_lum = 10; |
a50aac71 | 1264 | |
97f5541f | 1265 | error = wacom_leds_alloc_and_register(wacom, 1, 4, false); |
a50aac71 BT |
1266 | if (error) { |
1267 | hid_err(wacom->hdev, | |
1268 | "cannot create leds err: %d\n", error); | |
1269 | return error; | |
1270 | } | |
1271 | ||
2df68a88 BT |
1272 | error = wacom_devm_sysfs_create_group(wacom, |
1273 | &intuos4_led_attr_group); | |
09e7d941 PC |
1274 | break; |
1275 | ||
246835fc | 1276 | case WACOM_24HD: |
09e7d941 | 1277 | case WACOM_21UX2: |
09e7d941 PC |
1278 | wacom->led.llv = 0; |
1279 | wacom->led.hlv = 0; | |
1280 | wacom->led.img_lum = 0; | |
5d7e7d47 | 1281 | |
97f5541f | 1282 | error = wacom_leds_alloc_and_register(wacom, 2, 4, false); |
a50aac71 BT |
1283 | if (error) { |
1284 | hid_err(wacom->hdev, | |
1285 | "cannot create leds err: %d\n", error); | |
1286 | return error; | |
1287 | } | |
1288 | ||
2df68a88 BT |
1289 | error = wacom_devm_sysfs_create_group(wacom, |
1290 | &cintiq_led_attr_group); | |
09e7d941 PC |
1291 | break; |
1292 | ||
9b5b95dd JG |
1293 | case INTUOS5S: |
1294 | case INTUOS5: | |
1295 | case INTUOS5L: | |
9a35c411 PC |
1296 | case INTUOSPS: |
1297 | case INTUOSPM: | |
1298 | case INTUOSPL: | |
862cf553 | 1299 | wacom->led.llv = 32; |
97f5541f | 1300 | wacom->led.max_llv = 96; |
862cf553 | 1301 | |
97f5541f | 1302 | error = wacom_leds_alloc_and_register(wacom, 1, 4, false); |
a50aac71 BT |
1303 | if (error) { |
1304 | hid_err(wacom->hdev, | |
1305 | "cannot create leds err: %d\n", error); | |
1306 | return error; | |
1307 | } | |
1308 | ||
2df68a88 BT |
1309 | error = wacom_devm_sysfs_create_group(wacom, |
1310 | &intuos5_led_attr_group); | |
9b5b95dd JG |
1311 | break; |
1312 | ||
a50aac71 | 1313 | case REMOTE: |
97f5541f BT |
1314 | wacom->led.llv = 255; |
1315 | wacom->led.max_llv = 255; | |
a50aac71 BT |
1316 | error = wacom_led_groups_allocate(wacom, 5); |
1317 | if (error) { | |
1318 | hid_err(wacom->hdev, | |
1319 | "cannot create leds err: %d\n", error); | |
1320 | return error; | |
1321 | } | |
1322 | return 0; | |
1323 | ||
09e7d941 PC |
1324 | default: |
1325 | return 0; | |
5d7e7d47 EH |
1326 | } |
1327 | ||
09e7d941 | 1328 | if (error) { |
c31a408f | 1329 | hid_err(wacom->hdev, |
09e7d941 PC |
1330 | "cannot create sysfs group err: %d\n", error); |
1331 | return error; | |
1332 | } | |
1333 | wacom_led_control(wacom); | |
1334 | ||
5d7e7d47 EH |
1335 | return 0; |
1336 | } | |
1337 | ||
a1d552cc | 1338 | static enum power_supply_property wacom_battery_props[] = { |
9956953e | 1339 | POWER_SUPPLY_PROP_MODEL_NAME, |
71fa641e | 1340 | POWER_SUPPLY_PROP_PRESENT, |
ac8d1010 | 1341 | POWER_SUPPLY_PROP_STATUS, |
6e2a6e80 | 1342 | POWER_SUPPLY_PROP_SCOPE, |
a1d552cc CB |
1343 | POWER_SUPPLY_PROP_CAPACITY |
1344 | }; | |
1345 | ||
1346 | static int wacom_battery_get_property(struct power_supply *psy, | |
1347 | enum power_supply_property psp, | |
1348 | union power_supply_propval *val) | |
1349 | { | |
59d69bc8 | 1350 | struct wacom_battery *battery = power_supply_get_drvdata(psy); |
a1d552cc CB |
1351 | int ret = 0; |
1352 | ||
1353 | switch (psp) { | |
9956953e BT |
1354 | case POWER_SUPPLY_PROP_MODEL_NAME: |
1355 | val->strval = battery->wacom->wacom_wac.name; | |
1356 | break; | |
71fa641e | 1357 | case POWER_SUPPLY_PROP_PRESENT: |
59d69bc8 | 1358 | val->intval = battery->bat_connected; |
71fa641e | 1359 | break; |
6e2a6e80 BN |
1360 | case POWER_SUPPLY_PROP_SCOPE: |
1361 | val->intval = POWER_SUPPLY_SCOPE_DEVICE; | |
1362 | break; | |
a1d552cc | 1363 | case POWER_SUPPLY_PROP_CAPACITY: |
59d69bc8 | 1364 | val->intval = battery->battery_capacity; |
ac8d1010 BT |
1365 | break; |
1366 | case POWER_SUPPLY_PROP_STATUS: | |
59d69bc8 | 1367 | if (battery->bat_charging) |
ac8d1010 | 1368 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
59d69bc8 BT |
1369 | else if (battery->battery_capacity == 100 && |
1370 | battery->ps_connected) | |
ac8d1010 | 1371 | val->intval = POWER_SUPPLY_STATUS_FULL; |
59d69bc8 | 1372 | else if (battery->ps_connected) |
b0882cb7 | 1373 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
ac8d1010 BT |
1374 | else |
1375 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | |
a1d552cc CB |
1376 | break; |
1377 | default: | |
1378 | ret = -EINVAL; | |
1379 | break; | |
1380 | } | |
1381 | ||
1382 | return ret; | |
1383 | } | |
1384 | ||
59d69bc8 BT |
1385 | static int __wacom_initialize_battery(struct wacom *wacom, |
1386 | struct wacom_battery *battery) | |
a1d552cc | 1387 | { |
d70420b9 | 1388 | static atomic_t battery_no = ATOMIC_INIT(0); |
b189da90 | 1389 | struct device *dev = &wacom->hdev->dev; |
59d69bc8 | 1390 | struct power_supply_config psy_cfg = { .drv_data = battery, }; |
136ae5e9 | 1391 | struct power_supply *ps_bat; |
59d69bc8 | 1392 | struct power_supply_desc *bat_desc = &battery->bat_desc; |
d70420b9 | 1393 | unsigned long n; |
b189da90 BT |
1394 | int error; |
1395 | ||
1396 | if (!devres_open_group(dev, bat_desc, GFP_KERNEL)) | |
1397 | return -ENOMEM; | |
a1d552cc | 1398 | |
9956953e BT |
1399 | battery->wacom = wacom; |
1400 | ||
59d69bc8 BT |
1401 | n = atomic_inc_return(&battery_no) - 1; |
1402 | ||
1403 | bat_desc->properties = wacom_battery_props; | |
1404 | bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props); | |
1405 | bat_desc->get_property = wacom_battery_get_property; | |
1406 | sprintf(battery->bat_name, "wacom_battery_%ld", n); | |
1407 | bat_desc->name = battery->bat_name; | |
96983296 | 1408 | bat_desc->type = POWER_SUPPLY_TYPE_USB; |
59d69bc8 BT |
1409 | bat_desc->use_for_apm = 0; |
1410 | ||
59d69bc8 BT |
1411 | ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg); |
1412 | if (IS_ERR(ps_bat)) { | |
1413 | error = PTR_ERR(ps_bat); | |
1414 | goto err; | |
1415 | } | |
297d716f | 1416 | |
59d69bc8 | 1417 | power_supply_powers(ps_bat, &wacom->hdev->dev); |
7dbd229e | 1418 | |
59d69bc8 | 1419 | battery->battery = ps_bat; |
a1d552cc | 1420 | |
b189da90 | 1421 | devres_close_group(dev, bat_desc); |
7dbd229e | 1422 | return 0; |
b189da90 BT |
1423 | |
1424 | err: | |
1425 | devres_release_group(dev, bat_desc); | |
1426 | return error; | |
a1d552cc CB |
1427 | } |
1428 | ||
59d69bc8 BT |
1429 | static int wacom_initialize_battery(struct wacom *wacom) |
1430 | { | |
1431 | if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) | |
1432 | return __wacom_initialize_battery(wacom, &wacom->battery); | |
1433 | ||
1434 | return 0; | |
1435 | } | |
1436 | ||
a1d552cc CB |
1437 | static void wacom_destroy_battery(struct wacom *wacom) |
1438 | { | |
59d69bc8 BT |
1439 | if (wacom->battery.battery) { |
1440 | devres_release_group(&wacom->hdev->dev, | |
1441 | &wacom->battery.bat_desc); | |
1442 | wacom->battery.battery = NULL; | |
b7af2bb8 | 1443 | } |
a1d552cc CB |
1444 | } |
1445 | ||
f81a1295 BT |
1446 | static ssize_t wacom_show_speed(struct device *dev, |
1447 | struct device_attribute | |
1448 | *attr, char *buf) | |
1449 | { | |
ee79a8f8 | 1450 | struct hid_device *hdev = to_hid_device(dev); |
f81a1295 BT |
1451 | struct wacom *wacom = hid_get_drvdata(hdev); |
1452 | ||
1453 | return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed); | |
1454 | } | |
1455 | ||
1456 | static ssize_t wacom_store_speed(struct device *dev, | |
1457 | struct device_attribute *attr, | |
1458 | const char *buf, size_t count) | |
1459 | { | |
ee79a8f8 | 1460 | struct hid_device *hdev = to_hid_device(dev); |
f81a1295 BT |
1461 | struct wacom *wacom = hid_get_drvdata(hdev); |
1462 | u8 new_speed; | |
1463 | ||
1464 | if (kstrtou8(buf, 0, &new_speed)) | |
1465 | return -EINVAL; | |
1466 | ||
1467 | if (new_speed != 0 && new_speed != 1) | |
1468 | return -EINVAL; | |
1469 | ||
1470 | wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features); | |
1471 | ||
1472 | return count; | |
1473 | } | |
1474 | ||
e0984bc3 | 1475 | static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM, |
f81a1295 BT |
1476 | wacom_show_speed, wacom_store_speed); |
1477 | ||
72b236d6 AS |
1478 | |
1479 | static ssize_t wacom_show_remote_mode(struct kobject *kobj, | |
1480 | struct kobj_attribute *kattr, | |
1481 | char *buf, int index) | |
1482 | { | |
2cf83833 | 1483 | struct device *dev = kobj_to_dev(kobj->parent); |
ee79a8f8 | 1484 | struct hid_device *hdev = to_hid_device(dev); |
72b236d6 AS |
1485 | struct wacom *wacom = hid_get_drvdata(hdev); |
1486 | u8 mode; | |
1487 | ||
a50aac71 | 1488 | mode = wacom->led.groups[index].select; |
72b236d6 AS |
1489 | if (mode >= 0 && mode < 3) |
1490 | return snprintf(buf, PAGE_SIZE, "%d\n", mode); | |
1491 | else | |
1492 | return snprintf(buf, PAGE_SIZE, "%d\n", -1); | |
1493 | } | |
1494 | ||
1495 | #define DEVICE_EKR_ATTR_GROUP(SET_ID) \ | |
1496 | static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \ | |
1497 | struct kobj_attribute *kattr, char *buf) \ | |
1498 | { \ | |
1499 | return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \ | |
1500 | } \ | |
1501 | static struct kobj_attribute remote##SET_ID##_mode_attr = { \ | |
1502 | .attr = {.name = "remote_mode", \ | |
1503 | .mode = DEV_ATTR_RO_PERM}, \ | |
1504 | .show = wacom_show_remote##SET_ID##_mode, \ | |
1505 | }; \ | |
1506 | static struct attribute *remote##SET_ID##_serial_attrs[] = { \ | |
1507 | &remote##SET_ID##_mode_attr.attr, \ | |
1508 | NULL \ | |
1509 | }; \ | |
1510 | static struct attribute_group remote##SET_ID##_serial_group = { \ | |
1511 | .name = NULL, \ | |
1512 | .attrs = remote##SET_ID##_serial_attrs, \ | |
1513 | } | |
1514 | ||
1515 | DEVICE_EKR_ATTR_GROUP(0); | |
1516 | DEVICE_EKR_ATTR_GROUP(1); | |
1517 | DEVICE_EKR_ATTR_GROUP(2); | |
1518 | DEVICE_EKR_ATTR_GROUP(3); | |
1519 | DEVICE_EKR_ATTR_GROUP(4); | |
1520 | ||
e6f2813a BT |
1521 | static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial, |
1522 | int index) | |
72b236d6 AS |
1523 | { |
1524 | int error = 0; | |
83e6b40e | 1525 | struct wacom_remote *remote = wacom->remote; |
72b236d6 | 1526 | |
e7749f6e | 1527 | remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev, |
83e6b40e BT |
1528 | GFP_KERNEL, |
1529 | "%d", serial); | |
e7749f6e | 1530 | if (!remote->remotes[index].group.name) |
72b236d6 | 1531 | return -ENOMEM; |
72b236d6 | 1532 | |
f9036bd4 | 1533 | error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir, |
e7749f6e | 1534 | &remote->remotes[index].group); |
72b236d6 | 1535 | if (error) { |
e7749f6e | 1536 | remote->remotes[index].group.name = NULL; |
72b236d6 AS |
1537 | hid_err(wacom->hdev, |
1538 | "cannot create sysfs group err: %d\n", error); | |
72b236d6 AS |
1539 | return error; |
1540 | } | |
1541 | ||
1542 | return 0; | |
1543 | } | |
1544 | ||
72b236d6 AS |
1545 | static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector) |
1546 | { | |
1547 | const size_t buf_size = 2; | |
1548 | unsigned char *buf; | |
1549 | int retval; | |
1550 | ||
1551 | buf = kzalloc(buf_size, GFP_KERNEL); | |
1552 | if (!buf) | |
1553 | return -ENOMEM; | |
1554 | ||
1555 | buf[0] = WAC_CMD_DELETE_PAIRING; | |
1556 | buf[1] = selector; | |
1557 | ||
1558 | retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf, | |
1559 | buf_size, WAC_CMD_RETRIES); | |
1560 | kfree(buf); | |
1561 | ||
1562 | return retval; | |
1563 | } | |
1564 | ||
1565 | static ssize_t wacom_store_unpair_remote(struct kobject *kobj, | |
1566 | struct kobj_attribute *attr, | |
1567 | const char *buf, size_t count) | |
1568 | { | |
1569 | unsigned char selector = 0; | |
2cf83833 | 1570 | struct device *dev = kobj_to_dev(kobj->parent); |
ee79a8f8 | 1571 | struct hid_device *hdev = to_hid_device(dev); |
72b236d6 AS |
1572 | struct wacom *wacom = hid_get_drvdata(hdev); |
1573 | int err; | |
1574 | ||
1575 | if (!strncmp(buf, "*\n", 2)) { | |
1576 | selector = WAC_CMD_UNPAIR_ALL; | |
1577 | } else { | |
1578 | hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n", | |
1579 | buf); | |
1580 | return -1; | |
1581 | } | |
1582 | ||
1583 | mutex_lock(&wacom->lock); | |
1584 | ||
1585 | err = wacom_cmd_unpair_remote(wacom, selector); | |
1586 | mutex_unlock(&wacom->lock); | |
1587 | ||
1588 | return err < 0 ? err : count; | |
1589 | } | |
1590 | ||
1591 | static struct kobj_attribute unpair_remote_attr = { | |
1592 | .attr = {.name = "unpair_remote", .mode = 0200}, | |
1593 | .store = wacom_store_unpair_remote, | |
1594 | }; | |
1595 | ||
1596 | static const struct attribute *remote_unpair_attrs[] = { | |
1597 | &unpair_remote_attr.attr, | |
1598 | NULL | |
1599 | }; | |
1600 | ||
83e6b40e BT |
1601 | static void wacom_remotes_destroy(void *data) |
1602 | { | |
1603 | struct wacom *wacom = data; | |
1604 | struct wacom_remote *remote = wacom->remote; | |
1605 | ||
1606 | if (!remote) | |
1607 | return; | |
1608 | ||
1609 | kobject_put(remote->remote_dir); | |
1610 | kfifo_free(&remote->remote_fifo); | |
1611 | wacom->remote = NULL; | |
1612 | } | |
1613 | ||
1614 | static int wacom_initialize_remotes(struct wacom *wacom) | |
72b236d6 AS |
1615 | { |
1616 | int error = 0; | |
83e6b40e | 1617 | struct wacom_remote *remote; |
72b236d6 AS |
1618 | int i; |
1619 | ||
1620 | if (wacom->wacom_wac.features.type != REMOTE) | |
1621 | return 0; | |
1622 | ||
83e6b40e BT |
1623 | remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote), |
1624 | GFP_KERNEL); | |
1625 | if (!remote) | |
1626 | return -ENOMEM; | |
72b236d6 | 1627 | |
83e6b40e BT |
1628 | wacom->remote = remote; |
1629 | ||
1630 | spin_lock_init(&remote->remote_lock); | |
1631 | ||
1632 | error = kfifo_alloc(&remote->remote_fifo, | |
1633 | 5 * sizeof(struct wacom_remote_data), | |
1634 | GFP_KERNEL); | |
1635 | if (error) { | |
1636 | hid_err(wacom->hdev, "failed allocating remote_fifo\n"); | |
72b236d6 | 1637 | return -ENOMEM; |
83e6b40e | 1638 | } |
72b236d6 | 1639 | |
e7749f6e BT |
1640 | remote->remotes[0].group = remote0_serial_group; |
1641 | remote->remotes[1].group = remote1_serial_group; | |
1642 | remote->remotes[2].group = remote2_serial_group; | |
1643 | remote->remotes[3].group = remote3_serial_group; | |
1644 | remote->remotes[4].group = remote4_serial_group; | |
83e6b40e BT |
1645 | |
1646 | remote->remote_dir = kobject_create_and_add("wacom_remote", | |
1647 | &wacom->hdev->dev.kobj); | |
1648 | if (!remote->remote_dir) | |
1649 | return -ENOMEM; | |
1650 | ||
1651 | error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs); | |
72b236d6 AS |
1652 | |
1653 | if (error) { | |
1654 | hid_err(wacom->hdev, | |
1655 | "cannot create sysfs group err: %d\n", error); | |
1656 | return error; | |
1657 | } | |
1658 | ||
1659 | for (i = 0; i < WACOM_MAX_REMOTES; i++) { | |
a50aac71 | 1660 | wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN; |
e7749f6e | 1661 | remote->remotes[i].serial = 0; |
72b236d6 AS |
1662 | } |
1663 | ||
83e6b40e BT |
1664 | error = devm_add_action_or_reset(&wacom->hdev->dev, |
1665 | wacom_remotes_destroy, wacom); | |
1666 | if (error) | |
1667 | return error; | |
1668 | ||
72b236d6 AS |
1669 | return 0; |
1670 | } | |
1671 | ||
d2d13f18 | 1672 | static struct input_dev *wacom_allocate_input(struct wacom *wacom) |
3aac0ef1 CB |
1673 | { |
1674 | struct input_dev *input_dev; | |
b6c79f2c | 1675 | struct hid_device *hdev = wacom->hdev; |
3aac0ef1 | 1676 | struct wacom_wac *wacom_wac = &(wacom->wacom_wac); |
3aac0ef1 | 1677 | |
3dad188e | 1678 | input_dev = devm_input_allocate_device(&hdev->dev); |
d2d13f18 BT |
1679 | if (!input_dev) |
1680 | return NULL; | |
3aac0ef1 | 1681 | |
2bdd163c | 1682 | input_dev->name = wacom_wac->features.name; |
b6c79f2c BT |
1683 | input_dev->phys = hdev->phys; |
1684 | input_dev->dev.parent = &hdev->dev; | |
3aac0ef1 CB |
1685 | input_dev->open = wacom_open; |
1686 | input_dev->close = wacom_close; | |
b6c79f2c BT |
1687 | input_dev->uniq = hdev->uniq; |
1688 | input_dev->id.bustype = hdev->bus; | |
1689 | input_dev->id.vendor = hdev->vendor; | |
12969e3b | 1690 | input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product; |
b6c79f2c | 1691 | input_dev->id.version = hdev->version; |
3aac0ef1 CB |
1692 | input_set_drvdata(input_dev, wacom); |
1693 | ||
d2d13f18 BT |
1694 | return input_dev; |
1695 | } | |
1696 | ||
d9f2d203 JG |
1697 | static int wacom_allocate_inputs(struct wacom *wacom) |
1698 | { | |
1699 | struct wacom_wac *wacom_wac = &(wacom->wacom_wac); | |
1700 | ||
1701 | wacom_wac->pen_input = wacom_allocate_input(wacom); | |
1702 | wacom_wac->touch_input = wacom_allocate_input(wacom); | |
1703 | wacom_wac->pad_input = wacom_allocate_input(wacom); | |
3dad188e BT |
1704 | if (!wacom_wac->pen_input || |
1705 | !wacom_wac->touch_input || | |
1706 | !wacom_wac->pad_input) | |
d9f2d203 | 1707 | return -ENOMEM; |
d9f2d203 | 1708 | |
2bdd163c | 1709 | wacom_wac->pen_input->name = wacom_wac->pen_name; |
d9f2d203 JG |
1710 | wacom_wac->touch_input->name = wacom_wac->touch_name; |
1711 | wacom_wac->pad_input->name = wacom_wac->pad_name; | |
1712 | ||
1713 | return 0; | |
1714 | } | |
1715 | ||
2546dacd BT |
1716 | static int wacom_register_inputs(struct wacom *wacom) |
1717 | { | |
2a6cdbdd | 1718 | struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev; |
2546dacd | 1719 | struct wacom_wac *wacom_wac = &(wacom->wacom_wac); |
2636a3f2 | 1720 | int error = 0; |
2546dacd | 1721 | |
2a6cdbdd JG |
1722 | pen_input_dev = wacom_wac->pen_input; |
1723 | touch_input_dev = wacom_wac->touch_input; | |
2546dacd BT |
1724 | pad_input_dev = wacom_wac->pad_input; |
1725 | ||
2a6cdbdd | 1726 | if (!pen_input_dev || !touch_input_dev || !pad_input_dev) |
2546dacd BT |
1727 | return -EINVAL; |
1728 | ||
2a6cdbdd JG |
1729 | error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac); |
1730 | if (error) { | |
1731 | /* no pen in use on this interface */ | |
1732 | input_free_device(pen_input_dev); | |
1733 | wacom_wac->pen_input = NULL; | |
1734 | pen_input_dev = NULL; | |
1735 | } else { | |
1736 | error = input_register_device(pen_input_dev); | |
1737 | if (error) | |
3dad188e | 1738 | goto fail; |
2a6cdbdd JG |
1739 | } |
1740 | ||
1741 | error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac); | |
1742 | if (error) { | |
1743 | /* no touch in use on this interface */ | |
1744 | input_free_device(touch_input_dev); | |
1745 | wacom_wac->touch_input = NULL; | |
1746 | touch_input_dev = NULL; | |
1747 | } else { | |
1748 | error = input_register_device(touch_input_dev); | |
30ebc1ae | 1749 | if (error) |
3dad188e | 1750 | goto fail; |
30ebc1ae | 1751 | } |
1963518b | 1752 | |
d2d13f18 BT |
1753 | error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac); |
1754 | if (error) { | |
1755 | /* no pad in use on this interface */ | |
1756 | input_free_device(pad_input_dev); | |
1757 | wacom_wac->pad_input = NULL; | |
1758 | pad_input_dev = NULL; | |
1759 | } else { | |
1760 | error = input_register_device(pad_input_dev); | |
1761 | if (error) | |
3dad188e | 1762 | goto fail; |
d2d13f18 BT |
1763 | } |
1764 | ||
1963518b | 1765 | return 0; |
3aac0ef1 | 1766 | |
3dad188e BT |
1767 | fail: |
1768 | wacom_wac->pad_input = NULL; | |
2a6cdbdd | 1769 | wacom_wac->touch_input = NULL; |
2a6cdbdd | 1770 | wacom_wac->pen_input = NULL; |
3aac0ef1 CB |
1771 | return error; |
1772 | } | |
1773 | ||
0be01712 JG |
1774 | /* |
1775 | * Not all devices report physical dimensions from HID. | |
1776 | * Compute the default from hardcoded logical dimension | |
1777 | * and resolution before driver overwrites them. | |
1778 | */ | |
1779 | static void wacom_set_default_phy(struct wacom_features *features) | |
1780 | { | |
1781 | if (features->x_resolution) { | |
1782 | features->x_phy = (features->x_max * 100) / | |
1783 | features->x_resolution; | |
1784 | features->y_phy = (features->y_max * 100) / | |
1785 | features->y_resolution; | |
1786 | } | |
1787 | } | |
1788 | ||
1789 | static void wacom_calculate_res(struct wacom_features *features) | |
1790 | { | |
1791 | /* set unit to "100th of a mm" for devices not reported by HID */ | |
1792 | if (!features->unit) { | |
1793 | features->unit = 0x11; | |
1794 | features->unitExpo = -3; | |
1795 | } | |
1796 | ||
1797 | features->x_resolution = wacom_calc_hid_res(features->x_max, | |
1798 | features->x_phy, | |
1799 | features->unit, | |
1800 | features->unitExpo); | |
1801 | features->y_resolution = wacom_calc_hid_res(features->y_max, | |
1802 | features->y_phy, | |
1803 | features->unit, | |
1804 | features->unitExpo); | |
1805 | } | |
1806 | ||
fce9957d JG |
1807 | void wacom_battery_work(struct work_struct *work) |
1808 | { | |
d17d1f17 | 1809 | struct wacom *wacom = container_of(work, struct wacom, battery_work); |
fce9957d JG |
1810 | |
1811 | if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) && | |
59d69bc8 | 1812 | !wacom->battery.battery) { |
fce9957d JG |
1813 | wacom_initialize_battery(wacom); |
1814 | } | |
1815 | else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) && | |
59d69bc8 | 1816 | wacom->battery.battery) { |
fce9957d JG |
1817 | wacom_destroy_battery(wacom); |
1818 | } | |
1819 | } | |
1820 | ||
01c846f9 BT |
1821 | static size_t wacom_compute_pktlen(struct hid_device *hdev) |
1822 | { | |
1823 | struct hid_report_enum *report_enum; | |
1824 | struct hid_report *report; | |
1825 | size_t size = 0; | |
1826 | ||
1827 | report_enum = hdev->report_enum + HID_INPUT_REPORT; | |
1828 | ||
1829 | list_for_each_entry(report, &report_enum->report_list, list) { | |
dabb05c6 | 1830 | size_t report_size = hid_report_len(report); |
01c846f9 BT |
1831 | if (report_size > size) |
1832 | size = report_size; | |
1833 | } | |
1834 | ||
1835 | return size; | |
1836 | } | |
1837 | ||
fd5f92b6 | 1838 | static void wacom_update_name(struct wacom *wacom, const char *suffix) |
c24eab4e PC |
1839 | { |
1840 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
1841 | struct wacom_features *features = &wacom_wac->features; | |
44b5250b | 1842 | char name[WACOM_NAME_MAX]; |
c24eab4e PC |
1843 | |
1844 | /* Generic devices name unspecified */ | |
1845 | if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) { | |
1846 | if (strstr(wacom->hdev->name, "Wacom") || | |
1847 | strstr(wacom->hdev->name, "wacom") || | |
1848 | strstr(wacom->hdev->name, "WACOM")) { | |
1849 | /* name is in HID descriptor, use it */ | |
44b5250b | 1850 | strlcpy(name, wacom->hdev->name, sizeof(name)); |
c24eab4e PC |
1851 | |
1852 | /* strip out excess whitespaces */ | |
1853 | while (1) { | |
44b5250b | 1854 | char *gap = strstr(name, " "); |
c24eab4e PC |
1855 | if (gap == NULL) |
1856 | break; | |
1857 | /* shift everything including the terminator */ | |
1858 | memmove(gap, gap+1, strlen(gap)); | |
1859 | } | |
1860 | /* get rid of trailing whitespace */ | |
44b5250b JG |
1861 | if (name[strlen(name)-1] == ' ') |
1862 | name[strlen(name)-1] = '\0'; | |
c24eab4e PC |
1863 | } else { |
1864 | /* no meaningful name retrieved. use product ID */ | |
44b5250b | 1865 | snprintf(name, sizeof(name), |
c24eab4e PC |
1866 | "%s %X", features->name, wacom->hdev->product); |
1867 | } | |
1868 | } else { | |
44b5250b | 1869 | strlcpy(name, features->name, sizeof(name)); |
c24eab4e PC |
1870 | } |
1871 | ||
9956953e BT |
1872 | snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s", |
1873 | name, suffix); | |
1874 | ||
c24eab4e | 1875 | /* Append the device type to the name */ |
2a6cdbdd | 1876 | snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name), |
fd5f92b6 | 1877 | "%s%s Pen", name, suffix); |
2a6cdbdd | 1878 | snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name), |
fd5f92b6 | 1879 | "%s%s Finger", name, suffix); |
c24eab4e | 1880 | snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name), |
fd5f92b6 | 1881 | "%s%s Pad", name, suffix); |
c24eab4e PC |
1882 | } |
1883 | ||
84dfbd7f BT |
1884 | static void wacom_release_resources(struct wacom *wacom) |
1885 | { | |
1886 | struct hid_device *hdev = wacom->hdev; | |
1887 | ||
1888 | if (!wacom->resources) | |
1889 | return; | |
1890 | ||
1891 | devres_release_group(&hdev->dev, wacom); | |
1892 | ||
1893 | wacom->resources = false; | |
1894 | ||
1895 | wacom->wacom_wac.pen_input = NULL; | |
1896 | wacom->wacom_wac.touch_input = NULL; | |
1897 | wacom->wacom_wac.pad_input = NULL; | |
1898 | } | |
1899 | ||
fd5f92b6 | 1900 | static int wacom_parse_and_register(struct wacom *wacom, bool wireless) |
3bea733a | 1901 | { |
c58ac3a8 BT |
1902 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; |
1903 | struct wacom_features *features = &wacom_wac->features; | |
1904 | struct hid_device *hdev = wacom->hdev; | |
e33da8a5 | 1905 | int error; |
7704ac93 | 1906 | unsigned int connect_mask = HID_CONNECT_HIDRAW; |
3bea733a | 1907 | |
01c846f9 | 1908 | features->pktlen = wacom_compute_pktlen(hdev); |
c58ac3a8 BT |
1909 | if (features->pktlen > WACOM_PKGLEN_MAX) |
1910 | return -EINVAL; | |
3bea733a | 1911 | |
84dfbd7f BT |
1912 | if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL)) |
1913 | return -ENOMEM; | |
1914 | ||
1915 | wacom->resources = true; | |
1916 | ||
3f14a63a JG |
1917 | error = wacom_allocate_inputs(wacom); |
1918 | if (error) | |
3888b0d5 | 1919 | goto fail; |
494078b0 | 1920 | |
8c97a765 BT |
1921 | /* |
1922 | * Bamboo Pad has a generic hid handling for the Pen, and we switch it | |
1923 | * into debug mode for the touch part. | |
1924 | * We ignore the other interfaces. | |
1925 | */ | |
1926 | if (features->type == BAMBOO_PAD) { | |
1927 | if (features->pktlen == WACOM_PKGLEN_PENABLED) { | |
1928 | features->type = HID_GENERIC; | |
1929 | } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) && | |
1930 | (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) { | |
1931 | error = -ENODEV; | |
3888b0d5 | 1932 | goto fail; |
8c97a765 BT |
1933 | } |
1934 | } | |
1935 | ||
401d7d10 PC |
1936 | /* set the default size in case we do not get them from hid */ |
1937 | wacom_set_default_phy(features); | |
1938 | ||
f393ee2b | 1939 | /* Retrieve the physical and logical size for touch devices */ |
c669fb2b | 1940 | wacom_retrieve_hid_descriptor(hdev, features); |
42f4f272 | 1941 | wacom_setup_device_quirks(wacom); |
042628ab | 1942 | |
aa86b18c JG |
1943 | if (features->device_type == WACOM_DEVICETYPE_NONE && |
1944 | features->type != WIRELESS) { | |
8e116d31 JG |
1945 | error = features->type == HID_GENERIC ? -ENODEV : 0; |
1946 | ||
042628ab | 1947 | dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.", |
8e116d31 JG |
1948 | hdev->name, |
1949 | error ? "Ignoring" : "Assuming pen"); | |
1950 | ||
1951 | if (error) | |
3888b0d5 | 1952 | goto fail; |
042628ab | 1953 | |
aa86b18c | 1954 | features->device_type |= WACOM_DEVICETYPE_PEN; |
042628ab JG |
1955 | } |
1956 | ||
401d7d10 PC |
1957 | wacom_calculate_res(features); |
1958 | ||
fd5f92b6 | 1959 | wacom_update_name(wacom, wireless ? " (WL)" : ""); |
4492efff | 1960 | |
f3586d2f PC |
1961 | error = wacom_add_shared_data(hdev); |
1962 | if (error) | |
3888b0d5 | 1963 | goto fail; |
49b764ae | 1964 | |
ccad85cc | 1965 | if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) && |
f81a1295 BT |
1966 | (features->quirks & WACOM_QUIRK_BATTERY)) { |
1967 | error = wacom_initialize_battery(wacom); | |
1968 | if (error) | |
3888b0d5 | 1969 | goto fail; |
f81a1295 BT |
1970 | } |
1971 | ||
3f14a63a JG |
1972 | error = wacom_register_inputs(wacom); |
1973 | if (error) | |
3888b0d5 | 1974 | goto fail; |
f81a1295 | 1975 | |
b62f6465 | 1976 | if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) { |
85d2c77b BT |
1977 | error = wacom_initialize_leds(wacom); |
1978 | if (error) | |
3888b0d5 | 1979 | goto fail; |
85d2c77b | 1980 | |
83e6b40e | 1981 | error = wacom_initialize_remotes(wacom); |
b62f6465 | 1982 | if (error) |
3888b0d5 | 1983 | goto fail; |
b62f6465 BT |
1984 | } |
1985 | ||
7704ac93 BT |
1986 | if (features->type == HID_GENERIC) |
1987 | connect_mask |= HID_CONNECT_DRIVER; | |
1988 | ||
29b47391 | 1989 | /* Regular HID work starts now */ |
7704ac93 | 1990 | error = hid_hw_start(hdev, connect_mask); |
29b47391 BT |
1991 | if (error) { |
1992 | hid_err(hdev, "hw start failed\n"); | |
3888b0d5 | 1993 | goto fail; |
d3825d51 | 1994 | } |
961794a0 | 1995 | |
fd5f92b6 BT |
1996 | if (!wireless) { |
1997 | /* Note that if query fails it is not a hard failure */ | |
1998 | wacom_query_tablet_data(hdev, features); | |
1999 | } | |
86e88f0e JG |
2000 | |
2001 | /* touch only Bamboo doesn't support pen */ | |
2002 | if ((features->type == BAMBOO_TOUCH) && | |
2003 | (features->device_type & WACOM_DEVICETYPE_PEN)) { | |
2004 | error = -ENODEV; | |
b62f6465 | 2005 | goto fail_quirks; |
86e88f0e JG |
2006 | } |
2007 | ||
2008 | /* pen only Bamboo neither support touch nor pad */ | |
2009 | if ((features->type == BAMBOO_PEN) && | |
2010 | ((features->device_type & WACOM_DEVICETYPE_TOUCH) || | |
2011 | (features->device_type & WACOM_DEVICETYPE_PAD))) { | |
2012 | error = -ENODEV; | |
b62f6465 | 2013 | goto fail_quirks; |
86e88f0e JG |
2014 | } |
2015 | ||
ccad85cc | 2016 | if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR) |
29b47391 BT |
2017 | error = hid_hw_open(hdev); |
2018 | ||
eda01dab | 2019 | if ((wacom_wac->features.type == INTUOSHT || |
97f9afa4 | 2020 | wacom_wac->features.type == INTUOSHT2) && |
eda01dab | 2021 | (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) { |
97f9afa4 BT |
2022 | wacom_wac->shared->type = wacom_wac->features.type; |
2023 | wacom_wac->shared->touch_input = wacom_wac->touch_input; | |
961794a0 PC |
2024 | } |
2025 | ||
84dfbd7f BT |
2026 | devres_close_group(&hdev->dev, wacom); |
2027 | ||
3bea733a PC |
2028 | return 0; |
2029 | ||
b62f6465 | 2030 | fail_quirks: |
c58ac3a8 | 2031 | hid_hw_stop(hdev); |
3888b0d5 | 2032 | fail: |
84dfbd7f | 2033 | wacom_release_resources(wacom); |
c58ac3a8 BT |
2034 | return error; |
2035 | } | |
2036 | ||
a2f091af BT |
2037 | static void wacom_wireless_work(struct work_struct *work) |
2038 | { | |
d17d1f17 | 2039 | struct wacom *wacom = container_of(work, struct wacom, wireless_work); |
a2f091af BT |
2040 | struct usb_device *usbdev = wacom->usbdev; |
2041 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
2042 | struct hid_device *hdev1, *hdev2; | |
2043 | struct wacom *wacom1, *wacom2; | |
2044 | struct wacom_wac *wacom_wac1, *wacom_wac2; | |
2045 | int error; | |
2046 | ||
2047 | /* | |
2048 | * Regardless if this is a disconnect or a new tablet, | |
2049 | * remove any existing input and battery devices. | |
2050 | */ | |
2051 | ||
2052 | wacom_destroy_battery(wacom); | |
2053 | ||
2054 | /* Stylus interface */ | |
2055 | hdev1 = usb_get_intfdata(usbdev->config->interface[1]); | |
2056 | wacom1 = hid_get_drvdata(hdev1); | |
2057 | wacom_wac1 = &(wacom1->wacom_wac); | |
84dfbd7f | 2058 | wacom_release_resources(wacom1); |
a2f091af BT |
2059 | |
2060 | /* Touch interface */ | |
2061 | hdev2 = usb_get_intfdata(usbdev->config->interface[2]); | |
2062 | wacom2 = hid_get_drvdata(hdev2); | |
2063 | wacom_wac2 = &(wacom2->wacom_wac); | |
84dfbd7f | 2064 | wacom_release_resources(wacom2); |
a2f091af BT |
2065 | |
2066 | if (wacom_wac->pid == 0) { | |
2067 | hid_info(wacom->hdev, "wireless tablet disconnected\n"); | |
a2f091af BT |
2068 | } else { |
2069 | const struct hid_device_id *id = wacom_ids; | |
2070 | ||
2071 | hid_info(wacom->hdev, "wireless tablet connected with PID %x\n", | |
2072 | wacom_wac->pid); | |
2073 | ||
2074 | while (id->bus) { | |
2075 | if (id->vendor == USB_VENDOR_ID_WACOM && | |
2076 | id->product == wacom_wac->pid) | |
2077 | break; | |
2078 | id++; | |
2079 | } | |
2080 | ||
2081 | if (!id->bus) { | |
2082 | hid_info(wacom->hdev, "ignoring unknown PID.\n"); | |
2083 | return; | |
2084 | } | |
2085 | ||
2086 | /* Stylus interface */ | |
2087 | wacom_wac1->features = | |
2088 | *((struct wacom_features *)id->driver_data); | |
fd5f92b6 | 2089 | |
a2f091af | 2090 | wacom_wac1->pid = wacom_wac->pid; |
fd5f92b6 BT |
2091 | hid_hw_stop(hdev1); |
2092 | error = wacom_parse_and_register(wacom1, true); | |
a2f091af BT |
2093 | if (error) |
2094 | goto fail; | |
2095 | ||
2096 | /* Touch interface */ | |
2097 | if (wacom_wac1->features.touch_max || | |
2098 | (wacom_wac1->features.type >= INTUOSHT && | |
2099 | wacom_wac1->features.type <= BAMBOO_PT)) { | |
2100 | wacom_wac2->features = | |
2101 | *((struct wacom_features *)id->driver_data); | |
a2f091af | 2102 | wacom_wac2->pid = wacom_wac->pid; |
fd5f92b6 BT |
2103 | hid_hw_stop(hdev2); |
2104 | error = wacom_parse_and_register(wacom2, true); | |
a2f091af BT |
2105 | if (error) |
2106 | goto fail; | |
a2f091af BT |
2107 | } |
2108 | ||
9956953e BT |
2109 | strlcpy(wacom_wac->name, wacom_wac1->name, |
2110 | sizeof(wacom_wac->name)); | |
a2f091af BT |
2111 | error = wacom_initialize_battery(wacom); |
2112 | if (error) | |
2113 | goto fail; | |
2114 | } | |
2115 | ||
2116 | return; | |
2117 | ||
2118 | fail: | |
84dfbd7f | 2119 | wacom_release_resources(wacom1); |
84dfbd7f | 2120 | wacom_release_resources(wacom2); |
a2f091af BT |
2121 | return; |
2122 | } | |
2123 | ||
04bfa27b BT |
2124 | static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index) |
2125 | { | |
2126 | struct wacom_remote *remote = wacom->remote; | |
e7749f6e | 2127 | u32 serial = remote->remotes[index].serial; |
04bfa27b | 2128 | int i; |
7c35dc3c BT |
2129 | unsigned long flags; |
2130 | ||
2131 | spin_lock_irqsave(&remote->remote_lock, flags); | |
2132 | remote->remotes[index].registered = false; | |
2133 | spin_unlock_irqrestore(&remote->remote_lock, flags); | |
04bfa27b | 2134 | |
9f1015d4 BT |
2135 | if (remote->remotes[index].battery.battery) |
2136 | devres_release_group(&wacom->hdev->dev, | |
2137 | &remote->remotes[index].battery.bat_desc); | |
2138 | ||
e7749f6e BT |
2139 | if (remote->remotes[index].group.name) |
2140 | devres_release_group(&wacom->hdev->dev, | |
2141 | &remote->remotes[index]); | |
04bfa27b BT |
2142 | |
2143 | for (i = 0; i < WACOM_MAX_REMOTES; i++) { | |
e7749f6e BT |
2144 | if (remote->remotes[i].serial == serial) { |
2145 | remote->remotes[i].serial = 0; | |
2146 | remote->remotes[i].group.name = NULL; | |
7c35dc3c | 2147 | remote->remotes[i].registered = false; |
9f1015d4 | 2148 | remote->remotes[i].battery.battery = NULL; |
04bfa27b BT |
2149 | wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN; |
2150 | } | |
2151 | } | |
2152 | } | |
2153 | ||
2154 | static int wacom_remote_create_one(struct wacom *wacom, u32 serial, | |
2155 | unsigned int index) | |
2156 | { | |
2157 | struct wacom_remote *remote = wacom->remote; | |
f9036bd4 | 2158 | struct device *dev = &wacom->hdev->dev; |
04bfa27b BT |
2159 | int error, k; |
2160 | ||
2161 | /* A remote can pair more than once with an EKR, | |
2162 | * check to make sure this serial isn't already paired. | |
2163 | */ | |
2164 | for (k = 0; k < WACOM_MAX_REMOTES; k++) { | |
e7749f6e | 2165 | if (remote->remotes[k].serial == serial) |
04bfa27b BT |
2166 | break; |
2167 | } | |
2168 | ||
2169 | if (k < WACOM_MAX_REMOTES) { | |
e7749f6e | 2170 | remote->remotes[index].serial = serial; |
04bfa27b BT |
2171 | return 0; |
2172 | } | |
2173 | ||
e7749f6e | 2174 | if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL)) |
f9036bd4 BT |
2175 | return -ENOMEM; |
2176 | ||
04bfa27b BT |
2177 | error = wacom_remote_create_attr_group(wacom, serial, index); |
2178 | if (error) | |
f9036bd4 | 2179 | goto fail; |
04bfa27b | 2180 | |
7c35dc3c BT |
2181 | remote->remotes[index].input = wacom_allocate_input(wacom); |
2182 | if (!remote->remotes[index].input) { | |
2183 | error = -ENOMEM; | |
2184 | goto fail; | |
2185 | } | |
2186 | remote->remotes[index].input->uniq = remote->remotes[index].group.name; | |
2187 | remote->remotes[index].input->name = wacom->wacom_wac.pad_name; | |
2188 | ||
2189 | if (!remote->remotes[index].input->name) { | |
2190 | error = -EINVAL; | |
2191 | goto fail; | |
2192 | } | |
2193 | ||
2194 | error = wacom_setup_pad_input_capabilities(remote->remotes[index].input, | |
2195 | &wacom->wacom_wac); | |
2196 | if (error) | |
2197 | goto fail; | |
2198 | ||
e7749f6e | 2199 | remote->remotes[index].serial = serial; |
04bfa27b | 2200 | |
7c35dc3c BT |
2201 | error = input_register_device(remote->remotes[index].input); |
2202 | if (error) | |
2203 | goto fail; | |
2204 | ||
97f5541f BT |
2205 | error = wacom_led_groups_alloc_and_register_one( |
2206 | &remote->remotes[index].input->dev, | |
2207 | wacom, index, 3, true); | |
2208 | if (error) | |
2209 | goto fail; | |
2210 | ||
7c35dc3c BT |
2211 | remote->remotes[index].registered = true; |
2212 | ||
e7749f6e | 2213 | devres_close_group(dev, &remote->remotes[index]); |
04bfa27b | 2214 | return 0; |
f9036bd4 BT |
2215 | |
2216 | fail: | |
e7749f6e BT |
2217 | devres_release_group(dev, &remote->remotes[index]); |
2218 | remote->remotes[index].serial = 0; | |
f9036bd4 | 2219 | return error; |
04bfa27b BT |
2220 | } |
2221 | ||
9f1015d4 BT |
2222 | static int wacom_remote_attach_battery(struct wacom *wacom, int index) |
2223 | { | |
2224 | struct wacom_remote *remote = wacom->remote; | |
2225 | int error; | |
2226 | ||
2227 | if (!remote->remotes[index].registered) | |
2228 | return 0; | |
2229 | ||
2230 | if (remote->remotes[index].battery.battery) | |
2231 | return 0; | |
2232 | ||
2233 | if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN) | |
2234 | return 0; | |
2235 | ||
2236 | error = __wacom_initialize_battery(wacom, | |
2237 | &wacom->remote->remotes[index].battery); | |
2238 | if (error) | |
2239 | return error; | |
2240 | ||
2241 | return 0; | |
2242 | } | |
2243 | ||
e6f2813a BT |
2244 | static void wacom_remote_work(struct work_struct *work) |
2245 | { | |
2246 | struct wacom *wacom = container_of(work, struct wacom, remote_work); | |
83e6b40e | 2247 | struct wacom_remote *remote = wacom->remote; |
e6f2813a BT |
2248 | struct wacom_remote_data data; |
2249 | unsigned long flags; | |
2250 | unsigned int count; | |
2251 | u32 serial; | |
04bfa27b | 2252 | int i; |
e6f2813a | 2253 | |
83e6b40e | 2254 | spin_lock_irqsave(&remote->remote_lock, flags); |
e6f2813a | 2255 | |
83e6b40e | 2256 | count = kfifo_out(&remote->remote_fifo, &data, sizeof(data)); |
e6f2813a BT |
2257 | |
2258 | if (count != sizeof(data)) { | |
2259 | hid_err(wacom->hdev, | |
2260 | "workitem triggered without status available\n"); | |
83e6b40e | 2261 | spin_unlock_irqrestore(&remote->remote_lock, flags); |
e6f2813a BT |
2262 | return; |
2263 | } | |
2264 | ||
83e6b40e | 2265 | if (!kfifo_is_empty(&remote->remote_fifo)) |
e6f2813a BT |
2266 | wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE); |
2267 | ||
83e6b40e | 2268 | spin_unlock_irqrestore(&remote->remote_lock, flags); |
e6f2813a BT |
2269 | |
2270 | for (i = 0; i < WACOM_MAX_REMOTES; i++) { | |
2271 | serial = data.remote[i].serial; | |
2272 | if (data.remote[i].connected) { | |
2273 | ||
9f1015d4 BT |
2274 | if (remote->remotes[i].serial == serial) { |
2275 | wacom_remote_attach_battery(wacom, i); | |
e6f2813a | 2276 | continue; |
9f1015d4 | 2277 | } |
e6f2813a | 2278 | |
e7749f6e | 2279 | if (remote->remotes[i].serial) |
04bfa27b | 2280 | wacom_remote_destroy_one(wacom, i); |
e6f2813a | 2281 | |
04bfa27b | 2282 | wacom_remote_create_one(wacom, serial, i); |
e6f2813a | 2283 | |
e7749f6e | 2284 | } else if (remote->remotes[i].serial) { |
04bfa27b | 2285 | wacom_remote_destroy_one(wacom, i); |
e6f2813a BT |
2286 | } |
2287 | } | |
2288 | } | |
2289 | ||
c58ac3a8 BT |
2290 | static int wacom_probe(struct hid_device *hdev, |
2291 | const struct hid_device_id *id) | |
2292 | { | |
2293 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
2294 | struct usb_device *dev = interface_to_usbdev(intf); | |
2295 | struct wacom *wacom; | |
2296 | struct wacom_wac *wacom_wac; | |
2297 | struct wacom_features *features; | |
2298 | int error; | |
2299 | ||
2300 | if (!id->driver_data) | |
2301 | return -EINVAL; | |
2302 | ||
2303 | hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS; | |
2304 | ||
2305 | /* hid-core sets this quirk for the boot interface */ | |
2306 | hdev->quirks &= ~HID_QUIRK_NOGET; | |
2307 | ||
19b64330 | 2308 | wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL); |
c58ac3a8 BT |
2309 | if (!wacom) |
2310 | return -ENOMEM; | |
2311 | ||
2312 | hid_set_drvdata(hdev, wacom); | |
2313 | wacom->hdev = hdev; | |
2314 | ||
2315 | wacom_wac = &wacom->wacom_wac; | |
2316 | wacom_wac->features = *((struct wacom_features *)id->driver_data); | |
2317 | features = &wacom_wac->features; | |
2318 | ||
2319 | if (features->check_for_hid_type && features->hid_type != hdev->type) { | |
2320 | error = -ENODEV; | |
3888b0d5 | 2321 | goto fail; |
c58ac3a8 BT |
2322 | } |
2323 | ||
c6fa1aeb | 2324 | wacom_wac->hid_data.inputmode = -1; |
326ea2a9 | 2325 | wacom_wac->mode_report = -1; |
c6fa1aeb | 2326 | |
c58ac3a8 BT |
2327 | wacom->usbdev = dev; |
2328 | wacom->intf = intf; | |
2329 | mutex_init(&wacom->lock); | |
d17d1f17 BT |
2330 | INIT_WORK(&wacom->wireless_work, wacom_wireless_work); |
2331 | INIT_WORK(&wacom->battery_work, wacom_battery_work); | |
e6f2813a | 2332 | INIT_WORK(&wacom->remote_work, wacom_remote_work); |
c58ac3a8 BT |
2333 | |
2334 | /* ask for the report descriptor to be loaded by HID */ | |
2335 | error = hid_parse(hdev); | |
2336 | if (error) { | |
2337 | hid_err(hdev, "parse failed\n"); | |
3888b0d5 | 2338 | goto fail; |
c58ac3a8 BT |
2339 | } |
2340 | ||
fd5f92b6 | 2341 | error = wacom_parse_and_register(wacom, false); |
c58ac3a8 | 2342 | if (error) |
3888b0d5 | 2343 | goto fail; |
c58ac3a8 BT |
2344 | |
2345 | if (hdev->bus == BUS_BLUETOOTH) { | |
2346 | error = device_create_file(&hdev->dev, &dev_attr_speed); | |
2347 | if (error) | |
2348 | hid_warn(hdev, | |
2349 | "can't create sysfs speed attribute err: %d\n", | |
2350 | error); | |
2351 | } | |
2352 | ||
2353 | return 0; | |
2354 | ||
3888b0d5 | 2355 | fail: |
29b47391 | 2356 | hid_set_drvdata(hdev, NULL); |
5014186d | 2357 | return error; |
3bea733a PC |
2358 | } |
2359 | ||
29b47391 | 2360 | static void wacom_remove(struct hid_device *hdev) |
3bea733a | 2361 | { |
29b47391 | 2362 | struct wacom *wacom = hid_get_drvdata(hdev); |
f6205161 BT |
2363 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; |
2364 | struct wacom_features *features = &wacom_wac->features; | |
2365 | ||
2366 | if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR) | |
2367 | hid_hw_close(hdev); | |
3bea733a | 2368 | |
29b47391 | 2369 | hid_hw_stop(hdev); |
e7224094 | 2370 | |
d17d1f17 BT |
2371 | cancel_work_sync(&wacom->wireless_work); |
2372 | cancel_work_sync(&wacom->battery_work); | |
e6f2813a | 2373 | cancel_work_sync(&wacom->remote_work); |
f81a1295 BT |
2374 | if (hdev->bus == BUS_BLUETOOTH) |
2375 | device_remove_file(&hdev->dev, &dev_attr_speed); | |
e7224094 | 2376 | |
29b47391 | 2377 | hid_set_drvdata(hdev, NULL); |
e7224094 ON |
2378 | } |
2379 | ||
41a74581 | 2380 | #ifdef CONFIG_PM |
29b47391 | 2381 | static int wacom_resume(struct hid_device *hdev) |
e7224094 | 2382 | { |
29b47391 | 2383 | struct wacom *wacom = hid_get_drvdata(hdev); |
51269fe8 | 2384 | struct wacom_features *features = &wacom->wacom_wac.features; |
e7224094 ON |
2385 | |
2386 | mutex_lock(&wacom->lock); | |
38101475 PC |
2387 | |
2388 | /* switch to wacom mode first */ | |
27b20a9d | 2389 | wacom_query_tablet_data(hdev, features); |
5d7e7d47 | 2390 | wacom_led_control(wacom); |
38101475 | 2391 | |
e7224094 ON |
2392 | mutex_unlock(&wacom->lock); |
2393 | ||
29b47391 | 2394 | return 0; |
e7224094 ON |
2395 | } |
2396 | ||
29b47391 | 2397 | static int wacom_reset_resume(struct hid_device *hdev) |
e7224094 | 2398 | { |
29b47391 | 2399 | return wacom_resume(hdev); |
3bea733a | 2400 | } |
41a74581 | 2401 | #endif /* CONFIG_PM */ |
3bea733a | 2402 | |
29b47391 | 2403 | static struct hid_driver wacom_driver = { |
3bea733a | 2404 | .name = "wacom", |
b036f6fb | 2405 | .id_table = wacom_ids, |
3bea733a | 2406 | .probe = wacom_probe, |
29b47391 | 2407 | .remove = wacom_remove, |
7704ac93 | 2408 | .report = wacom_wac_report, |
29b47391 | 2409 | #ifdef CONFIG_PM |
e7224094 ON |
2410 | .resume = wacom_resume, |
2411 | .reset_resume = wacom_reset_resume, | |
29b47391 BT |
2412 | #endif |
2413 | .raw_event = wacom_raw_event, | |
3bea733a | 2414 | }; |
29b47391 | 2415 | module_hid_driver(wacom_driver); |
f2e0a7d4 BT |
2416 | |
2417 | MODULE_VERSION(DRIVER_VERSION); | |
2418 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
2419 | MODULE_DESCRIPTION(DRIVER_DESC); | |
2420 | MODULE_LICENSE(DRIVER_LICENSE); |