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 | |
28 | #define WAC_REMOTE_SERIAL_MAX_STRLEN 9 | |
5d7e7d47 | 29 | |
e0984bc3 PC |
30 | #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP) |
31 | #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP) | |
72b236d6 | 32 | #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP) |
e0984bc3 | 33 | |
c64d8834 PC |
34 | static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf, |
35 | size_t size, unsigned int retries) | |
3bea733a | 36 | { |
5d7e7d47 EH |
37 | int retval; |
38 | ||
39 | do { | |
c64d8834 | 40 | retval = hid_hw_raw_request(hdev, buf[0], buf, size, type, |
27b20a9d | 41 | HID_REQ_GET_REPORT); |
aef3156d JG |
42 | } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries); |
43 | ||
44 | if (retval < 0) | |
45 | hid_err(hdev, "wacom_get_report: ran out of retries " | |
46 | "(last error = %d)\n", retval); | |
5d7e7d47 EH |
47 | |
48 | return retval; | |
3bea733a PC |
49 | } |
50 | ||
296b7378 PF |
51 | static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf, |
52 | size_t size, unsigned int retries) | |
3bea733a | 53 | { |
5d7e7d47 EH |
54 | int retval; |
55 | ||
56 | do { | |
296b7378 | 57 | retval = hid_hw_raw_request(hdev, buf[0], buf, size, type, |
27b20a9d | 58 | HID_REQ_SET_REPORT); |
aef3156d JG |
59 | } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries); |
60 | ||
61 | if (retval < 0) | |
62 | hid_err(hdev, "wacom_set_report: ran out of retries " | |
63 | "(last error = %d)\n", retval); | |
5d7e7d47 EH |
64 | |
65 | return retval; | |
3bea733a PC |
66 | } |
67 | ||
29b47391 BT |
68 | static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report, |
69 | u8 *raw_data, int size) | |
3bea733a | 70 | { |
29b47391 | 71 | struct wacom *wacom = hid_get_drvdata(hdev); |
3bea733a | 72 | |
29b47391 BT |
73 | if (size > WACOM_PKGLEN_MAX) |
74 | return 1; | |
3bea733a | 75 | |
29b47391 | 76 | memcpy(wacom->wacom_wac.data, raw_data, size); |
3bea733a | 77 | |
29b47391 BT |
78 | wacom_wac_irq(&wacom->wacom_wac, size); |
79 | ||
80 | return 0; | |
3bea733a PC |
81 | } |
82 | ||
3bea733a PC |
83 | static int wacom_open(struct input_dev *dev) |
84 | { | |
7791bdae | 85 | struct wacom *wacom = input_get_drvdata(dev); |
29b47391 | 86 | |
dff67416 | 87 | return hid_hw_open(wacom->hdev); |
3bea733a PC |
88 | } |
89 | ||
90 | static void wacom_close(struct input_dev *dev) | |
91 | { | |
7791bdae | 92 | struct wacom *wacom = input_get_drvdata(dev); |
3bea733a | 93 | |
29b47391 | 94 | hid_hw_close(wacom->hdev); |
3bea733a PC |
95 | } |
96 | ||
115d5e12 | 97 | /* |
198fdee2 | 98 | * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res. |
115d5e12 JG |
99 | */ |
100 | static int wacom_calc_hid_res(int logical_extents, int physical_extents, | |
c669fb2b | 101 | unsigned unit, int exponent) |
115d5e12 | 102 | { |
198fdee2 BT |
103 | struct hid_field field = { |
104 | .logical_maximum = logical_extents, | |
105 | .physical_maximum = physical_extents, | |
106 | .unit = unit, | |
107 | .unit_exponent = exponent, | |
108 | }; | |
109 | ||
110 | return hidinput_calc_abs_res(&field, ABS_X); | |
115d5e12 JG |
111 | } |
112 | ||
c669fb2b BT |
113 | static void wacom_feature_mapping(struct hid_device *hdev, |
114 | struct hid_field *field, struct hid_usage *usage) | |
f393ee2b | 115 | { |
c669fb2b BT |
116 | struct wacom *wacom = hid_get_drvdata(hdev); |
117 | struct wacom_features *features = &wacom->wacom_wac.features; | |
5ae6e89f | 118 | struct hid_data *hid_data = &wacom->wacom_wac.hid_data; |
8ffffd52 BT |
119 | u8 *data; |
120 | int ret; | |
f393ee2b | 121 | |
c669fb2b BT |
122 | switch (usage->hid) { |
123 | case HID_DG_CONTACTMAX: | |
124 | /* leave touch_max as is if predefined */ | |
8ffffd52 BT |
125 | if (!features->touch_max) { |
126 | /* read manually */ | |
127 | data = kzalloc(2, GFP_KERNEL); | |
128 | if (!data) | |
129 | break; | |
130 | data[0] = field->report->id; | |
131 | ret = wacom_get_report(hdev, HID_FEATURE_REPORT, | |
05e8fd92 JG |
132 | data, 2, WAC_CMD_RETRIES); |
133 | if (ret == 2) { | |
8ffffd52 | 134 | features->touch_max = data[1]; |
05e8fd92 JG |
135 | } else { |
136 | features->touch_max = 16; | |
137 | hid_warn(hdev, "wacom_feature_mapping: " | |
138 | "could not get HID_DG_CONTACTMAX, " | |
139 | "defaulting to %d\n", | |
140 | features->touch_max); | |
141 | } | |
8ffffd52 BT |
142 | kfree(data); |
143 | } | |
c669fb2b | 144 | break; |
5ae6e89f BT |
145 | case HID_DG_INPUTMODE: |
146 | /* Ignore if value index is out of bounds. */ | |
147 | if (usage->usage_index >= field->report_count) { | |
148 | dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n"); | |
149 | break; | |
150 | } | |
151 | ||
152 | hid_data->inputmode = field->report->id; | |
153 | hid_data->inputmode_index = usage->usage_index; | |
154 | break; | |
f393ee2b PC |
155 | } |
156 | } | |
157 | ||
428f8588 CB |
158 | /* |
159 | * Interface Descriptor of wacom devices can be incomplete and | |
160 | * inconsistent so wacom_features table is used to store stylus | |
161 | * device's packet lengths, various maximum values, and tablet | |
162 | * resolution based on product ID's. | |
163 | * | |
164 | * For devices that contain 2 interfaces, wacom_features table is | |
165 | * inaccurate for the touch interface. Since the Interface Descriptor | |
166 | * for touch interfaces has pretty complete data, this function exists | |
167 | * to query tablet for this missing information instead of hard coding in | |
168 | * an additional table. | |
169 | * | |
170 | * A typical Interface Descriptor for a stylus will contain a | |
171 | * boot mouse application collection that is not of interest and this | |
172 | * function will ignore it. | |
173 | * | |
174 | * It also contains a digitizer application collection that also is not | |
175 | * of interest since any information it contains would be duplicate | |
176 | * of what is in wacom_features. Usually it defines a report of an array | |
177 | * of bytes that could be used as max length of the stylus packet returned. | |
178 | * If it happens to define a Digitizer-Stylus Physical Collection then | |
179 | * the X and Y logical values contain valid data but it is ignored. | |
180 | * | |
181 | * A typical Interface Descriptor for a touch interface will contain a | |
182 | * Digitizer-Finger Physical Collection which will define both logical | |
183 | * X/Y maximum as well as the physical size of tablet. Since touch | |
184 | * interfaces haven't supported pressure or distance, this is enough | |
185 | * information to override invalid values in the wacom_features table. | |
4134361a | 186 | * |
c669fb2b BT |
187 | * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful |
188 | * data. We deal with them after returning from this function. | |
428f8588 | 189 | */ |
c669fb2b BT |
190 | static void wacom_usage_mapping(struct hid_device *hdev, |
191 | struct hid_field *field, struct hid_usage *usage) | |
545f4e99 | 192 | { |
c669fb2b BT |
193 | struct wacom *wacom = hid_get_drvdata(hdev); |
194 | struct wacom_features *features = &wacom->wacom_wac.features; | |
d97a5522 BT |
195 | bool finger = WACOM_FINGER_FIELD(field); |
196 | bool pen = WACOM_PEN_FIELD(field); | |
e9fc413f | 197 | |
c669fb2b BT |
198 | /* |
199 | * Requiring Stylus Usage will ignore boot mouse | |
200 | * X/Y values and some cases of invalid Digitizer X/Y | |
201 | * values commonly reported. | |
202 | */ | |
042628ab | 203 | if (pen) |
aa86b18c | 204 | features->device_type |= WACOM_DEVICETYPE_PEN; |
042628ab | 205 | else if (finger) |
aa86b18c | 206 | features->device_type |= WACOM_DEVICETYPE_TOUCH; |
042628ab | 207 | else |
c669fb2b BT |
208 | return; |
209 | ||
30ebc1ae PC |
210 | /* |
211 | * Bamboo models do not support HID_DG_CONTACTMAX. | |
212 | * And, Bamboo Pen only descriptor contains touch. | |
213 | */ | |
3b164a00 | 214 | if (features->type > BAMBOO_PT) { |
30ebc1ae PC |
215 | /* ISDv4 touch devices at least supports one touch point */ |
216 | if (finger && !features->touch_max) | |
217 | features->touch_max = 1; | |
218 | } | |
c669fb2b BT |
219 | |
220 | switch (usage->hid) { | |
221 | case HID_GD_X: | |
222 | features->x_max = field->logical_maximum; | |
223 | if (finger) { | |
c669fb2b | 224 | features->x_phy = field->physical_maximum; |
3b164a00 PC |
225 | if ((features->type != BAMBOO_PT) && |
226 | (features->type != BAMBOO_TOUCH)) { | |
c669fb2b BT |
227 | features->unit = field->unit; |
228 | features->unitExpo = field->unit_exponent; | |
545f4e99 | 229 | } |
c669fb2b BT |
230 | } |
231 | break; | |
232 | case HID_GD_Y: | |
233 | features->y_max = field->logical_maximum; | |
234 | if (finger) { | |
235 | features->y_phy = field->physical_maximum; | |
3b164a00 PC |
236 | if ((features->type != BAMBOO_PT) && |
237 | (features->type != BAMBOO_TOUCH)) { | |
c669fb2b BT |
238 | features->unit = field->unit; |
239 | features->unitExpo = field->unit_exponent; | |
240 | } | |
241 | } | |
242 | break; | |
243 | case HID_DG_TIPPRESSURE: | |
244 | if (pen) | |
245 | features->pressure_max = field->logical_maximum; | |
246 | break; | |
247 | } | |
7704ac93 BT |
248 | |
249 | if (features->type == HID_GENERIC) | |
250 | wacom_wac_usage_mapping(hdev, field, usage); | |
c669fb2b | 251 | } |
4134361a | 252 | |
b58ba1ba JG |
253 | static void wacom_post_parse_hid(struct hid_device *hdev, |
254 | struct wacom_features *features) | |
255 | { | |
256 | struct wacom *wacom = hid_get_drvdata(hdev); | |
257 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
258 | ||
259 | if (features->type == HID_GENERIC) { | |
260 | /* Any last-minute generic device setup */ | |
261 | if (features->touch_max > 1) { | |
2a6cdbdd | 262 | input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max, |
b58ba1ba JG |
263 | INPUT_MT_DIRECT); |
264 | } | |
265 | } | |
266 | } | |
267 | ||
c669fb2b BT |
268 | static void wacom_parse_hid(struct hid_device *hdev, |
269 | struct wacom_features *features) | |
270 | { | |
271 | struct hid_report_enum *rep_enum; | |
272 | struct hid_report *hreport; | |
273 | int i, j; | |
274 | ||
275 | /* check features first */ | |
276 | rep_enum = &hdev->report_enum[HID_FEATURE_REPORT]; | |
277 | list_for_each_entry(hreport, &rep_enum->report_list, list) { | |
278 | for (i = 0; i < hreport->maxfield; i++) { | |
279 | /* Ignore if report count is out of bounds. */ | |
280 | if (hreport->field[i]->report_count < 1) | |
281 | continue; | |
282 | ||
283 | for (j = 0; j < hreport->field[i]->maxusage; j++) { | |
284 | wacom_feature_mapping(hdev, hreport->field[i], | |
285 | hreport->field[i]->usage + j); | |
4134361a | 286 | } |
545f4e99 PC |
287 | } |
288 | } | |
289 | ||
c669fb2b BT |
290 | /* now check the input usages */ |
291 | rep_enum = &hdev->report_enum[HID_INPUT_REPORT]; | |
292 | list_for_each_entry(hreport, &rep_enum->report_list, list) { | |
293 | ||
294 | if (!hreport->maxfield) | |
295 | continue; | |
296 | ||
297 | for (i = 0; i < hreport->maxfield; i++) | |
298 | for (j = 0; j < hreport->field[i]->maxusage; j++) | |
299 | wacom_usage_mapping(hdev, hreport->field[i], | |
300 | hreport->field[i]->usage + j); | |
301 | } | |
b58ba1ba JG |
302 | |
303 | wacom_post_parse_hid(hdev, features); | |
545f4e99 PC |
304 | } |
305 | ||
5ae6e89f BT |
306 | static int wacom_hid_set_device_mode(struct hid_device *hdev) |
307 | { | |
308 | struct wacom *wacom = hid_get_drvdata(hdev); | |
309 | struct hid_data *hid_data = &wacom->wacom_wac.hid_data; | |
310 | struct hid_report *r; | |
311 | struct hid_report_enum *re; | |
312 | ||
313 | if (hid_data->inputmode < 0) | |
314 | return 0; | |
315 | ||
316 | re = &(hdev->report_enum[HID_FEATURE_REPORT]); | |
317 | r = re->report_id_hash[hid_data->inputmode]; | |
318 | if (r) { | |
319 | r->field[0]->value[hid_data->inputmode_index] = 2; | |
320 | hid_hw_request(hdev, r, HID_REQ_SET_REPORT); | |
321 | } | |
322 | return 0; | |
323 | } | |
324 | ||
27b20a9d BT |
325 | static int wacom_set_device_mode(struct hid_device *hdev, int report_id, |
326 | int length, int mode) | |
3b7307c2 DT |
327 | { |
328 | unsigned char *rep_data; | |
fe494bc2 | 329 | int error = -ENOMEM, limit = 0; |
3b7307c2 | 330 | |
fe494bc2 | 331 | rep_data = kzalloc(length, GFP_KERNEL); |
3b7307c2 | 332 | if (!rep_data) |
ec67bbed PC |
333 | return error; |
334 | ||
fe494bc2 | 335 | do { |
9937c026 CB |
336 | rep_data[0] = report_id; |
337 | rep_data[1] = mode; | |
338 | ||
296b7378 PF |
339 | error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, |
340 | length, 1); | |
3cb83157 | 341 | if (error >= 0) |
27b20a9d | 342 | error = wacom_get_report(hdev, HID_FEATURE_REPORT, |
c64d8834 | 343 | rep_data, length, 1); |
a1c173da | 344 | } while (error >= 0 && rep_data[1] != mode && limit++ < WAC_MSG_RETRIES); |
fe494bc2 JG |
345 | |
346 | kfree(rep_data); | |
347 | ||
348 | return error < 0 ? error : 0; | |
349 | } | |
350 | ||
f81a1295 BT |
351 | static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed, |
352 | struct wacom_features *features) | |
353 | { | |
387142bb BT |
354 | struct wacom *wacom = hid_get_drvdata(hdev); |
355 | int ret; | |
356 | u8 rep_data[2]; | |
357 | ||
358 | switch (features->type) { | |
359 | case GRAPHIRE_BT: | |
360 | rep_data[0] = 0x03; | |
361 | rep_data[1] = 0x00; | |
296b7378 PF |
362 | ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2, |
363 | 3); | |
387142bb BT |
364 | |
365 | if (ret >= 0) { | |
366 | rep_data[0] = speed == 0 ? 0x05 : 0x06; | |
367 | rep_data[1] = 0x00; | |
368 | ||
369 | ret = wacom_set_report(hdev, HID_FEATURE_REPORT, | |
296b7378 | 370 | rep_data, 2, 3); |
387142bb BT |
371 | |
372 | if (ret >= 0) { | |
373 | wacom->wacom_wac.bt_high_speed = speed; | |
374 | return 0; | |
375 | } | |
376 | } | |
377 | ||
378 | /* | |
379 | * Note that if the raw queries fail, it's not a hard failure | |
380 | * and it is safe to continue | |
381 | */ | |
382 | hid_warn(hdev, "failed to poke device, command %d, err %d\n", | |
383 | rep_data[0], ret); | |
384 | break; | |
81af7e61 BT |
385 | case INTUOS4WL: |
386 | if (speed == 1) | |
387 | wacom->wacom_wac.bt_features &= ~0x20; | |
388 | else | |
389 | wacom->wacom_wac.bt_features |= 0x20; | |
390 | ||
391 | rep_data[0] = 0x03; | |
392 | rep_data[1] = wacom->wacom_wac.bt_features; | |
393 | ||
296b7378 PF |
394 | ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2, |
395 | 1); | |
81af7e61 BT |
396 | if (ret >= 0) |
397 | wacom->wacom_wac.bt_high_speed = speed; | |
398 | break; | |
387142bb BT |
399 | } |
400 | ||
f81a1295 BT |
401 | return 0; |
402 | } | |
403 | ||
fe494bc2 JG |
404 | /* |
405 | * Switch the tablet into its most-capable mode. Wacom tablets are | |
406 | * typically configured to power-up in a mode which sends mouse-like | |
407 | * reports to the OS. To get absolute position, pressure data, etc. | |
408 | * from the tablet, it is necessary to switch the tablet out of this | |
409 | * mode and into one which sends the full range of tablet data. | |
410 | */ | |
27b20a9d BT |
411 | static int wacom_query_tablet_data(struct hid_device *hdev, |
412 | struct wacom_features *features) | |
fe494bc2 | 413 | { |
f81a1295 BT |
414 | if (hdev->bus == BUS_BLUETOOTH) |
415 | return wacom_bt_query_tablet_data(hdev, 1, features); | |
416 | ||
5ae6e89f BT |
417 | if (features->type == HID_GENERIC) |
418 | return wacom_hid_set_device_mode(hdev); | |
419 | ||
aa86b18c | 420 | if (features->device_type & WACOM_DEVICETYPE_TOUCH) { |
ea2e6024 | 421 | if (features->type > TABLETPC) { |
fe494bc2 | 422 | /* MT Tablet PC touch */ |
27b20a9d | 423 | return wacom_set_device_mode(hdev, 3, 4, 4); |
fe494bc2 | 424 | } |
6212aae0 | 425 | else if (features->type == WACOM_24HDT) { |
27b20a9d | 426 | return wacom_set_device_mode(hdev, 18, 3, 2); |
b1e4279e | 427 | } |
500d4160 PC |
428 | else if (features->type == WACOM_27QHDT) { |
429 | return wacom_set_device_mode(hdev, 131, 3, 2); | |
430 | } | |
8c97a765 BT |
431 | else if (features->type == BAMBOO_PAD) { |
432 | return wacom_set_device_mode(hdev, 2, 2, 2); | |
433 | } | |
aa86b18c | 434 | } else if (features->device_type & WACOM_DEVICETYPE_PEN) { |
7adb91bd | 435 | if (features->type <= BAMBOO_PT) { |
27b20a9d | 436 | return wacom_set_device_mode(hdev, 2, 2, 2); |
1963518b | 437 | } |
ec67bbed | 438 | } |
3b7307c2 | 439 | |
fe494bc2 | 440 | return 0; |
3b7307c2 DT |
441 | } |
442 | ||
c669fb2b | 443 | static void wacom_retrieve_hid_descriptor(struct hid_device *hdev, |
1963518b | 444 | struct wacom_features *features) |
ec67bbed | 445 | { |
27b20a9d BT |
446 | struct wacom *wacom = hid_get_drvdata(hdev); |
447 | struct usb_interface *intf = wacom->intf; | |
ec67bbed | 448 | |
fed87e65 | 449 | /* default features */ |
fed87e65 HR |
450 | features->x_fuzz = 4; |
451 | features->y_fuzz = 4; | |
452 | features->pressure_fuzz = 0; | |
453 | features->distance_fuzz = 0; | |
ec67bbed | 454 | |
d3825d51 CB |
455 | /* |
456 | * The wireless device HID is basic and layout conflicts with | |
457 | * other tablets (monitor and touch interface can look like pen). | |
458 | * Skip the query for this type and modify defaults based on | |
459 | * interface number. | |
460 | */ | |
461 | if (features->type == WIRELESS) { | |
3f14a63a | 462 | if (intf->cur_altsetting->desc.bInterfaceNumber == 0) |
ccad85cc | 463 | features->device_type = WACOM_DEVICETYPE_WL_MONITOR; |
3f14a63a | 464 | else |
aa86b18c | 465 | features->device_type = WACOM_DEVICETYPE_NONE; |
3f14a63a | 466 | return; |
d3825d51 CB |
467 | } |
468 | ||
c669fb2b | 469 | wacom_parse_hid(hdev, features); |
ec67bbed PC |
470 | } |
471 | ||
4451e088 | 472 | struct wacom_hdev_data { |
4492efff PC |
473 | struct list_head list; |
474 | struct kref kref; | |
4451e088 | 475 | struct hid_device *dev; |
4492efff PC |
476 | struct wacom_shared shared; |
477 | }; | |
478 | ||
479 | static LIST_HEAD(wacom_udev_list); | |
480 | static DEFINE_MUTEX(wacom_udev_list_lock); | |
481 | ||
4451e088 BT |
482 | static bool wacom_are_sibling(struct hid_device *hdev, |
483 | struct hid_device *sibling) | |
aea2bf6a | 484 | { |
4451e088 BT |
485 | struct wacom *wacom = hid_get_drvdata(hdev); |
486 | struct wacom_features *features = &wacom->wacom_wac.features; | |
487 | int vid = features->oVid; | |
488 | int pid = features->oPid; | |
489 | int n1,n2; | |
490 | ||
491 | if (vid == 0 && pid == 0) { | |
492 | vid = hdev->vendor; | |
493 | pid = hdev->product; | |
494 | } | |
aea2bf6a | 495 | |
4451e088 BT |
496 | if (vid != sibling->vendor || pid != sibling->product) |
497 | return false; | |
aea2bf6a | 498 | |
4451e088 BT |
499 | /* Compare the physical path. */ |
500 | n1 = strrchr(hdev->phys, '.') - hdev->phys; | |
501 | n2 = strrchr(sibling->phys, '.') - sibling->phys; | |
502 | if (n1 != n2 || n1 <= 0 || n2 <= 0) | |
503 | return false; | |
aea2bf6a | 504 | |
4451e088 | 505 | return !strncmp(hdev->phys, sibling->phys, n1); |
aea2bf6a JG |
506 | } |
507 | ||
4451e088 | 508 | static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev) |
4492efff | 509 | { |
4451e088 | 510 | struct wacom_hdev_data *data; |
4492efff PC |
511 | |
512 | list_for_each_entry(data, &wacom_udev_list, list) { | |
4451e088 | 513 | if (wacom_are_sibling(hdev, data->dev)) { |
4492efff PC |
514 | kref_get(&data->kref); |
515 | return data; | |
516 | } | |
517 | } | |
518 | ||
519 | return NULL; | |
520 | } | |
521 | ||
4451e088 | 522 | static int wacom_add_shared_data(struct hid_device *hdev) |
4492efff | 523 | { |
4451e088 BT |
524 | struct wacom *wacom = hid_get_drvdata(hdev); |
525 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
526 | struct wacom_hdev_data *data; | |
4492efff PC |
527 | int retval = 0; |
528 | ||
529 | mutex_lock(&wacom_udev_list_lock); | |
530 | ||
4451e088 | 531 | data = wacom_get_hdev_data(hdev); |
4492efff | 532 | if (!data) { |
4451e088 | 533 | data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL); |
4492efff PC |
534 | if (!data) { |
535 | retval = -ENOMEM; | |
536 | goto out; | |
537 | } | |
538 | ||
539 | kref_init(&data->kref); | |
4451e088 | 540 | data->dev = hdev; |
4492efff PC |
541 | list_add_tail(&data->list, &wacom_udev_list); |
542 | } | |
543 | ||
4451e088 | 544 | wacom_wac->shared = &data->shared; |
4492efff | 545 | |
aa86b18c | 546 | if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) |
a97ac104 | 547 | wacom_wac->shared->touch = hdev; |
aa86b18c | 548 | else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN) |
a97ac104 BT |
549 | wacom_wac->shared->pen = hdev; |
550 | ||
4492efff PC |
551 | out: |
552 | mutex_unlock(&wacom_udev_list_lock); | |
553 | return retval; | |
554 | } | |
555 | ||
556 | static void wacom_release_shared_data(struct kref *kref) | |
557 | { | |
4451e088 BT |
558 | struct wacom_hdev_data *data = |
559 | container_of(kref, struct wacom_hdev_data, kref); | |
4492efff PC |
560 | |
561 | mutex_lock(&wacom_udev_list_lock); | |
562 | list_del(&data->list); | |
563 | mutex_unlock(&wacom_udev_list_lock); | |
564 | ||
565 | kfree(data); | |
566 | } | |
567 | ||
a97ac104 | 568 | static void wacom_remove_shared_data(struct wacom *wacom) |
4492efff | 569 | { |
4451e088 | 570 | struct wacom_hdev_data *data; |
a97ac104 BT |
571 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; |
572 | ||
573 | if (wacom_wac->shared) { | |
574 | data = container_of(wacom_wac->shared, struct wacom_hdev_data, | |
575 | shared); | |
576 | ||
577 | if (wacom_wac->shared->touch == wacom->hdev) | |
578 | wacom_wac->shared->touch = NULL; | |
579 | else if (wacom_wac->shared->pen == wacom->hdev) | |
580 | wacom_wac->shared->pen = NULL; | |
4492efff | 581 | |
4492efff | 582 | kref_put(&data->kref, wacom_release_shared_data); |
a97ac104 | 583 | wacom_wac->shared = NULL; |
4492efff PC |
584 | } |
585 | } | |
586 | ||
5d7e7d47 EH |
587 | static int wacom_led_control(struct wacom *wacom) |
588 | { | |
589 | unsigned char *buf; | |
9b5b95dd | 590 | int retval; |
912ca216 PC |
591 | unsigned char report_id = WAC_CMD_LED_CONTROL; |
592 | int buf_size = 9; | |
5d7e7d47 | 593 | |
912ca216 PC |
594 | if (wacom->wacom_wac.pid) { /* wireless connected */ |
595 | report_id = WAC_CMD_WL_LED_CONTROL; | |
596 | buf_size = 13; | |
597 | } | |
598 | buf = kzalloc(buf_size, GFP_KERNEL); | |
5d7e7d47 EH |
599 | if (!buf) |
600 | return -ENOMEM; | |
601 | ||
9b5b95dd | 602 | if (wacom->wacom_wac.features.type >= INTUOS5S && |
9a35c411 | 603 | wacom->wacom_wac.features.type <= INTUOSPL) { |
9b5b95dd JG |
604 | /* |
605 | * Touch Ring and crop mark LED luminance may take on | |
606 | * one of four values: | |
607 | * 0 = Low; 1 = Medium; 2 = High; 3 = Off | |
608 | */ | |
609 | int ring_led = wacom->led.select[0] & 0x03; | |
610 | int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03; | |
611 | int crop_lum = 0; | |
912ca216 PC |
612 | unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led); |
613 | ||
614 | buf[0] = report_id; | |
615 | if (wacom->wacom_wac.pid) { | |
616 | wacom_get_report(wacom->hdev, HID_FEATURE_REPORT, | |
617 | buf, buf_size, WAC_CMD_RETRIES); | |
618 | buf[0] = report_id; | |
619 | buf[4] = led_bits; | |
620 | } else | |
621 | buf[1] = led_bits; | |
9b5b95dd JG |
622 | } |
623 | else { | |
624 | int led = wacom->led.select[0] | 0x4; | |
625 | ||
626 | if (wacom->wacom_wac.features.type == WACOM_21UX2 || | |
627 | wacom->wacom_wac.features.type == WACOM_24HD) | |
628 | led |= (wacom->led.select[1] << 4) | 0x40; | |
629 | ||
912ca216 | 630 | buf[0] = report_id; |
9b5b95dd JG |
631 | buf[1] = led; |
632 | buf[2] = wacom->led.llv; | |
633 | buf[3] = wacom->led.hlv; | |
634 | buf[4] = wacom->led.img_lum; | |
635 | } | |
5d7e7d47 | 636 | |
912ca216 | 637 | retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size, |
296b7378 | 638 | WAC_CMD_RETRIES); |
5d7e7d47 EH |
639 | kfree(buf); |
640 | ||
641 | return retval; | |
642 | } | |
643 | ||
849e2f06 BT |
644 | static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id, |
645 | const unsigned len, const void *img) | |
5d7e7d47 EH |
646 | { |
647 | unsigned char *buf; | |
648 | int i, retval; | |
849e2f06 | 649 | const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */ |
5d7e7d47 | 650 | |
849e2f06 | 651 | buf = kzalloc(chunk_len + 3 , GFP_KERNEL); |
5d7e7d47 EH |
652 | if (!buf) |
653 | return -ENOMEM; | |
654 | ||
655 | /* Send 'start' command */ | |
656 | buf[0] = WAC_CMD_ICON_START; | |
657 | buf[1] = 1; | |
296b7378 PF |
658 | retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2, |
659 | WAC_CMD_RETRIES); | |
5d7e7d47 EH |
660 | if (retval < 0) |
661 | goto out; | |
662 | ||
849e2f06 | 663 | buf[0] = xfer_id; |
5d7e7d47 EH |
664 | buf[1] = button_id & 0x07; |
665 | for (i = 0; i < 4; i++) { | |
666 | buf[2] = i; | |
849e2f06 | 667 | memcpy(buf + 3, img + i * chunk_len, chunk_len); |
5d7e7d47 | 668 | |
27b20a9d | 669 | retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, |
296b7378 | 670 | buf, chunk_len + 3, WAC_CMD_RETRIES); |
5d7e7d47 EH |
671 | if (retval < 0) |
672 | break; | |
673 | } | |
674 | ||
675 | /* Send 'stop' */ | |
676 | buf[0] = WAC_CMD_ICON_START; | |
677 | buf[1] = 0; | |
296b7378 PF |
678 | wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2, |
679 | WAC_CMD_RETRIES); | |
5d7e7d47 EH |
680 | |
681 | out: | |
682 | kfree(buf); | |
683 | return retval; | |
684 | } | |
685 | ||
09e7d941 | 686 | static ssize_t wacom_led_select_store(struct device *dev, int set_id, |
5d7e7d47 EH |
687 | const char *buf, size_t count) |
688 | { | |
ee79a8f8 | 689 | struct hid_device *hdev = to_hid_device(dev); |
29b47391 | 690 | struct wacom *wacom = hid_get_drvdata(hdev); |
5d7e7d47 EH |
691 | unsigned int id; |
692 | int err; | |
693 | ||
694 | err = kstrtouint(buf, 10, &id); | |
695 | if (err) | |
696 | return err; | |
697 | ||
698 | mutex_lock(&wacom->lock); | |
699 | ||
09e7d941 | 700 | wacom->led.select[set_id] = id & 0x3; |
5d7e7d47 EH |
701 | err = wacom_led_control(wacom); |
702 | ||
703 | mutex_unlock(&wacom->lock); | |
704 | ||
705 | return err < 0 ? err : count; | |
706 | } | |
707 | ||
09e7d941 PC |
708 | #define DEVICE_LED_SELECT_ATTR(SET_ID) \ |
709 | static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \ | |
710 | struct device_attribute *attr, const char *buf, size_t count) \ | |
711 | { \ | |
712 | return wacom_led_select_store(dev, SET_ID, buf, count); \ | |
713 | } \ | |
04c59abd PC |
714 | static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \ |
715 | struct device_attribute *attr, char *buf) \ | |
716 | { \ | |
ee79a8f8 | 717 | struct hid_device *hdev = to_hid_device(dev);\ |
29b47391 | 718 | struct wacom *wacom = hid_get_drvdata(hdev); \ |
37449adc PC |
719 | return scnprintf(buf, PAGE_SIZE, "%d\n", \ |
720 | wacom->led.select[SET_ID]); \ | |
04c59abd | 721 | } \ |
e0984bc3 | 722 | static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \ |
04c59abd | 723 | wacom_led##SET_ID##_select_show, \ |
09e7d941 PC |
724 | wacom_led##SET_ID##_select_store) |
725 | ||
726 | DEVICE_LED_SELECT_ATTR(0); | |
727 | DEVICE_LED_SELECT_ATTR(1); | |
5d7e7d47 EH |
728 | |
729 | static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest, | |
730 | const char *buf, size_t count) | |
731 | { | |
732 | unsigned int value; | |
733 | int err; | |
734 | ||
735 | err = kstrtouint(buf, 10, &value); | |
736 | if (err) | |
737 | return err; | |
738 | ||
739 | mutex_lock(&wacom->lock); | |
740 | ||
741 | *dest = value & 0x7f; | |
742 | err = wacom_led_control(wacom); | |
743 | ||
744 | mutex_unlock(&wacom->lock); | |
745 | ||
746 | return err < 0 ? err : count; | |
747 | } | |
748 | ||
749 | #define DEVICE_LUMINANCE_ATTR(name, field) \ | |
750 | static ssize_t wacom_##name##_luminance_store(struct device *dev, \ | |
751 | struct device_attribute *attr, const char *buf, size_t count) \ | |
752 | { \ | |
ee79a8f8 | 753 | struct hid_device *hdev = to_hid_device(dev);\ |
29b47391 | 754 | struct wacom *wacom = hid_get_drvdata(hdev); \ |
5d7e7d47 EH |
755 | \ |
756 | return wacom_luminance_store(wacom, &wacom->led.field, \ | |
757 | buf, count); \ | |
758 | } \ | |
37449adc PC |
759 | static ssize_t wacom_##name##_luminance_show(struct device *dev, \ |
760 | struct device_attribute *attr, char *buf) \ | |
761 | { \ | |
762 | struct wacom *wacom = dev_get_drvdata(dev); \ | |
763 | return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \ | |
764 | } \ | |
e0984bc3 | 765 | static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \ |
37449adc PC |
766 | wacom_##name##_luminance_show, \ |
767 | wacom_##name##_luminance_store) | |
5d7e7d47 EH |
768 | |
769 | DEVICE_LUMINANCE_ATTR(status0, llv); | |
770 | DEVICE_LUMINANCE_ATTR(status1, hlv); | |
771 | DEVICE_LUMINANCE_ATTR(buttons, img_lum); | |
772 | ||
773 | static ssize_t wacom_button_image_store(struct device *dev, int button_id, | |
774 | const char *buf, size_t count) | |
775 | { | |
ee79a8f8 | 776 | struct hid_device *hdev = to_hid_device(dev); |
29b47391 | 777 | struct wacom *wacom = hid_get_drvdata(hdev); |
5d7e7d47 | 778 | int err; |
849e2f06 BT |
779 | unsigned len; |
780 | u8 xfer_id; | |
781 | ||
782 | if (hdev->bus == BUS_BLUETOOTH) { | |
783 | len = 256; | |
784 | xfer_id = WAC_CMD_ICON_BT_XFER; | |
785 | } else { | |
786 | len = 1024; | |
787 | xfer_id = WAC_CMD_ICON_XFER; | |
788 | } | |
5d7e7d47 | 789 | |
849e2f06 | 790 | if (count != len) |
5d7e7d47 EH |
791 | return -EINVAL; |
792 | ||
793 | mutex_lock(&wacom->lock); | |
794 | ||
849e2f06 | 795 | err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf); |
5d7e7d47 EH |
796 | |
797 | mutex_unlock(&wacom->lock); | |
798 | ||
799 | return err < 0 ? err : count; | |
800 | } | |
801 | ||
802 | #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \ | |
803 | static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \ | |
804 | struct device_attribute *attr, const char *buf, size_t count) \ | |
805 | { \ | |
806 | return wacom_button_image_store(dev, BUTTON_ID, buf, count); \ | |
807 | } \ | |
e0984bc3 | 808 | static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \ |
5d7e7d47 EH |
809 | NULL, wacom_btnimg##BUTTON_ID##_store) |
810 | ||
811 | DEVICE_BTNIMG_ATTR(0); | |
812 | DEVICE_BTNIMG_ATTR(1); | |
813 | DEVICE_BTNIMG_ATTR(2); | |
814 | DEVICE_BTNIMG_ATTR(3); | |
815 | DEVICE_BTNIMG_ATTR(4); | |
816 | DEVICE_BTNIMG_ATTR(5); | |
817 | DEVICE_BTNIMG_ATTR(6); | |
818 | DEVICE_BTNIMG_ATTR(7); | |
819 | ||
09e7d941 PC |
820 | static struct attribute *cintiq_led_attrs[] = { |
821 | &dev_attr_status_led0_select.attr, | |
822 | &dev_attr_status_led1_select.attr, | |
823 | NULL | |
824 | }; | |
825 | ||
826 | static struct attribute_group cintiq_led_attr_group = { | |
827 | .name = "wacom_led", | |
828 | .attrs = cintiq_led_attrs, | |
829 | }; | |
830 | ||
831 | static struct attribute *intuos4_led_attrs[] = { | |
5d7e7d47 EH |
832 | &dev_attr_status0_luminance.attr, |
833 | &dev_attr_status1_luminance.attr, | |
09e7d941 | 834 | &dev_attr_status_led0_select.attr, |
5d7e7d47 EH |
835 | &dev_attr_buttons_luminance.attr, |
836 | &dev_attr_button0_rawimg.attr, | |
837 | &dev_attr_button1_rawimg.attr, | |
838 | &dev_attr_button2_rawimg.attr, | |
839 | &dev_attr_button3_rawimg.attr, | |
840 | &dev_attr_button4_rawimg.attr, | |
841 | &dev_attr_button5_rawimg.attr, | |
842 | &dev_attr_button6_rawimg.attr, | |
843 | &dev_attr_button7_rawimg.attr, | |
844 | NULL | |
845 | }; | |
846 | ||
09e7d941 | 847 | static struct attribute_group intuos4_led_attr_group = { |
5d7e7d47 | 848 | .name = "wacom_led", |
09e7d941 | 849 | .attrs = intuos4_led_attrs, |
5d7e7d47 EH |
850 | }; |
851 | ||
9b5b95dd JG |
852 | static struct attribute *intuos5_led_attrs[] = { |
853 | &dev_attr_status0_luminance.attr, | |
854 | &dev_attr_status_led0_select.attr, | |
855 | NULL | |
856 | }; | |
857 | ||
858 | static struct attribute_group intuos5_led_attr_group = { | |
859 | .name = "wacom_led", | |
860 | .attrs = intuos5_led_attrs, | |
861 | }; | |
862 | ||
5d7e7d47 EH |
863 | static int wacom_initialize_leds(struct wacom *wacom) |
864 | { | |
865 | int error; | |
866 | ||
862cf553 JG |
867 | if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD)) |
868 | return 0; | |
869 | ||
09e7d941 PC |
870 | /* Initialize default values */ |
871 | switch (wacom->wacom_wac.features.type) { | |
a19fc986 | 872 | case INTUOS4S: |
09e7d941 | 873 | case INTUOS4: |
81af7e61 | 874 | case INTUOS4WL: |
09e7d941 PC |
875 | case INTUOS4L: |
876 | wacom->led.select[0] = 0; | |
877 | wacom->led.select[1] = 0; | |
f4fa9a6d | 878 | wacom->led.llv = 10; |
5d7e7d47 EH |
879 | wacom->led.hlv = 20; |
880 | wacom->led.img_lum = 10; | |
c31a408f | 881 | error = sysfs_create_group(&wacom->hdev->dev.kobj, |
09e7d941 PC |
882 | &intuos4_led_attr_group); |
883 | break; | |
884 | ||
246835fc | 885 | case WACOM_24HD: |
09e7d941 PC |
886 | case WACOM_21UX2: |
887 | wacom->led.select[0] = 0; | |
888 | wacom->led.select[1] = 0; | |
889 | wacom->led.llv = 0; | |
890 | wacom->led.hlv = 0; | |
891 | wacom->led.img_lum = 0; | |
5d7e7d47 | 892 | |
c31a408f | 893 | error = sysfs_create_group(&wacom->hdev->dev.kobj, |
09e7d941 PC |
894 | &cintiq_led_attr_group); |
895 | break; | |
896 | ||
9b5b95dd JG |
897 | case INTUOS5S: |
898 | case INTUOS5: | |
899 | case INTUOS5L: | |
9a35c411 PC |
900 | case INTUOSPS: |
901 | case INTUOSPM: | |
902 | case INTUOSPL: | |
862cf553 JG |
903 | wacom->led.select[0] = 0; |
904 | wacom->led.select[1] = 0; | |
905 | wacom->led.llv = 32; | |
906 | wacom->led.hlv = 0; | |
907 | wacom->led.img_lum = 0; | |
908 | ||
909 | error = sysfs_create_group(&wacom->hdev->dev.kobj, | |
910 | &intuos5_led_attr_group); | |
9b5b95dd JG |
911 | break; |
912 | ||
09e7d941 PC |
913 | default: |
914 | return 0; | |
5d7e7d47 EH |
915 | } |
916 | ||
09e7d941 | 917 | if (error) { |
c31a408f | 918 | hid_err(wacom->hdev, |
09e7d941 PC |
919 | "cannot create sysfs group err: %d\n", error); |
920 | return error; | |
921 | } | |
922 | wacom_led_control(wacom); | |
c757cbaf | 923 | wacom->led_initialized = true; |
09e7d941 | 924 | |
5d7e7d47 EH |
925 | return 0; |
926 | } | |
927 | ||
928 | static void wacom_destroy_leds(struct wacom *wacom) | |
929 | { | |
c757cbaf BT |
930 | if (!wacom->led_initialized) |
931 | return; | |
932 | ||
862cf553 JG |
933 | if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD)) |
934 | return; | |
935 | ||
c757cbaf BT |
936 | wacom->led_initialized = false; |
937 | ||
09e7d941 | 938 | switch (wacom->wacom_wac.features.type) { |
a19fc986 | 939 | case INTUOS4S: |
09e7d941 | 940 | case INTUOS4: |
81af7e61 | 941 | case INTUOS4WL: |
09e7d941 | 942 | case INTUOS4L: |
c31a408f | 943 | sysfs_remove_group(&wacom->hdev->dev.kobj, |
09e7d941 PC |
944 | &intuos4_led_attr_group); |
945 | break; | |
946 | ||
246835fc | 947 | case WACOM_24HD: |
09e7d941 | 948 | case WACOM_21UX2: |
c31a408f | 949 | sysfs_remove_group(&wacom->hdev->dev.kobj, |
09e7d941 PC |
950 | &cintiq_led_attr_group); |
951 | break; | |
9b5b95dd JG |
952 | |
953 | case INTUOS5S: | |
954 | case INTUOS5: | |
955 | case INTUOS5L: | |
9a35c411 PC |
956 | case INTUOSPS: |
957 | case INTUOSPM: | |
958 | case INTUOSPL: | |
862cf553 JG |
959 | sysfs_remove_group(&wacom->hdev->dev.kobj, |
960 | &intuos5_led_attr_group); | |
9b5b95dd | 961 | break; |
5d7e7d47 EH |
962 | } |
963 | } | |
964 | ||
a1d552cc | 965 | static enum power_supply_property wacom_battery_props[] = { |
71fa641e | 966 | POWER_SUPPLY_PROP_PRESENT, |
ac8d1010 | 967 | POWER_SUPPLY_PROP_STATUS, |
6e2a6e80 | 968 | POWER_SUPPLY_PROP_SCOPE, |
a1d552cc CB |
969 | POWER_SUPPLY_PROP_CAPACITY |
970 | }; | |
971 | ||
7dbd229e BT |
972 | static enum power_supply_property wacom_ac_props[] = { |
973 | POWER_SUPPLY_PROP_PRESENT, | |
974 | POWER_SUPPLY_PROP_ONLINE, | |
975 | POWER_SUPPLY_PROP_SCOPE, | |
976 | }; | |
977 | ||
a1d552cc CB |
978 | static int wacom_battery_get_property(struct power_supply *psy, |
979 | enum power_supply_property psp, | |
980 | union power_supply_propval *val) | |
981 | { | |
297d716f | 982 | struct wacom *wacom = power_supply_get_drvdata(psy); |
a1d552cc CB |
983 | int ret = 0; |
984 | ||
985 | switch (psp) { | |
71fa641e JG |
986 | case POWER_SUPPLY_PROP_PRESENT: |
987 | val->intval = wacom->wacom_wac.bat_connected; | |
988 | break; | |
6e2a6e80 BN |
989 | case POWER_SUPPLY_PROP_SCOPE: |
990 | val->intval = POWER_SUPPLY_SCOPE_DEVICE; | |
991 | break; | |
a1d552cc CB |
992 | case POWER_SUPPLY_PROP_CAPACITY: |
993 | val->intval = | |
ac8d1010 BT |
994 | wacom->wacom_wac.battery_capacity; |
995 | break; | |
996 | case POWER_SUPPLY_PROP_STATUS: | |
997 | if (wacom->wacom_wac.bat_charging) | |
998 | val->intval = POWER_SUPPLY_STATUS_CHARGING; | |
999 | else if (wacom->wacom_wac.battery_capacity == 100 && | |
1000 | wacom->wacom_wac.ps_connected) | |
1001 | val->intval = POWER_SUPPLY_STATUS_FULL; | |
b0882cb7 JG |
1002 | else if (wacom->wacom_wac.ps_connected) |
1003 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; | |
ac8d1010 BT |
1004 | else |
1005 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | |
a1d552cc CB |
1006 | break; |
1007 | default: | |
1008 | ret = -EINVAL; | |
1009 | break; | |
1010 | } | |
1011 | ||
1012 | return ret; | |
1013 | } | |
1014 | ||
7dbd229e BT |
1015 | static int wacom_ac_get_property(struct power_supply *psy, |
1016 | enum power_supply_property psp, | |
1017 | union power_supply_propval *val) | |
1018 | { | |
297d716f | 1019 | struct wacom *wacom = power_supply_get_drvdata(psy); |
7dbd229e BT |
1020 | int ret = 0; |
1021 | ||
1022 | switch (psp) { | |
1023 | case POWER_SUPPLY_PROP_PRESENT: | |
1024 | /* fall through */ | |
1025 | case POWER_SUPPLY_PROP_ONLINE: | |
1026 | val->intval = wacom->wacom_wac.ps_connected; | |
1027 | break; | |
1028 | case POWER_SUPPLY_PROP_SCOPE: | |
1029 | val->intval = POWER_SUPPLY_SCOPE_DEVICE; | |
1030 | break; | |
1031 | default: | |
1032 | ret = -EINVAL; | |
1033 | break; | |
1034 | } | |
1035 | return ret; | |
1036 | } | |
1037 | ||
a1d552cc CB |
1038 | static int wacom_initialize_battery(struct wacom *wacom) |
1039 | { | |
d70420b9 | 1040 | static atomic_t battery_no = ATOMIC_INIT(0); |
297d716f | 1041 | struct power_supply_config psy_cfg = { .drv_data = wacom, }; |
d70420b9 | 1042 | unsigned long n; |
a1d552cc | 1043 | |
ac8d1010 | 1044 | if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) { |
297d716f KK |
1045 | struct power_supply_desc *bat_desc = &wacom->battery_desc; |
1046 | struct power_supply_desc *ac_desc = &wacom->ac_desc; | |
d70420b9 | 1047 | n = atomic_inc_return(&battery_no) - 1; |
7dbd229e | 1048 | |
297d716f KK |
1049 | bat_desc->properties = wacom_battery_props; |
1050 | bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props); | |
1051 | bat_desc->get_property = wacom_battery_get_property; | |
d70420b9 | 1052 | sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n); |
297d716f KK |
1053 | bat_desc->name = wacom->wacom_wac.bat_name; |
1054 | bat_desc->type = POWER_SUPPLY_TYPE_BATTERY; | |
1055 | bat_desc->use_for_apm = 0; | |
a1d552cc | 1056 | |
297d716f KK |
1057 | ac_desc->properties = wacom_ac_props; |
1058 | ac_desc->num_properties = ARRAY_SIZE(wacom_ac_props); | |
1059 | ac_desc->get_property = wacom_ac_get_property; | |
7dbd229e | 1060 | sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n); |
297d716f KK |
1061 | ac_desc->name = wacom->wacom_wac.ac_name; |
1062 | ac_desc->type = POWER_SUPPLY_TYPE_MAINS; | |
1063 | ac_desc->use_for_apm = 0; | |
1064 | ||
1065 | wacom->battery = power_supply_register(&wacom->hdev->dev, | |
1066 | &wacom->battery_desc, &psy_cfg); | |
1067 | if (IS_ERR(wacom->battery)) | |
1068 | return PTR_ERR(wacom->battery); | |
1069 | ||
1070 | power_supply_powers(wacom->battery, &wacom->hdev->dev); | |
1071 | ||
1072 | wacom->ac = power_supply_register(&wacom->hdev->dev, | |
1073 | &wacom->ac_desc, | |
1074 | &psy_cfg); | |
1075 | if (IS_ERR(wacom->ac)) { | |
1076 | power_supply_unregister(wacom->battery); | |
1077 | return PTR_ERR(wacom->ac); | |
7dbd229e BT |
1078 | } |
1079 | ||
297d716f | 1080 | power_supply_powers(wacom->ac, &wacom->hdev->dev); |
a1d552cc CB |
1081 | } |
1082 | ||
7dbd229e | 1083 | return 0; |
a1d552cc CB |
1084 | } |
1085 | ||
1086 | static void wacom_destroy_battery(struct wacom *wacom) | |
1087 | { | |
8de29a35 | 1088 | if (wacom->battery) { |
297d716f KK |
1089 | power_supply_unregister(wacom->battery); |
1090 | wacom->battery = NULL; | |
1091 | power_supply_unregister(wacom->ac); | |
1092 | wacom->ac = NULL; | |
b7af2bb8 | 1093 | } |
a1d552cc CB |
1094 | } |
1095 | ||
f81a1295 BT |
1096 | static ssize_t wacom_show_speed(struct device *dev, |
1097 | struct device_attribute | |
1098 | *attr, char *buf) | |
1099 | { | |
ee79a8f8 | 1100 | struct hid_device *hdev = to_hid_device(dev); |
f81a1295 BT |
1101 | struct wacom *wacom = hid_get_drvdata(hdev); |
1102 | ||
1103 | return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed); | |
1104 | } | |
1105 | ||
1106 | static ssize_t wacom_store_speed(struct device *dev, | |
1107 | struct device_attribute *attr, | |
1108 | const char *buf, size_t count) | |
1109 | { | |
ee79a8f8 | 1110 | struct hid_device *hdev = to_hid_device(dev); |
f81a1295 BT |
1111 | struct wacom *wacom = hid_get_drvdata(hdev); |
1112 | u8 new_speed; | |
1113 | ||
1114 | if (kstrtou8(buf, 0, &new_speed)) | |
1115 | return -EINVAL; | |
1116 | ||
1117 | if (new_speed != 0 && new_speed != 1) | |
1118 | return -EINVAL; | |
1119 | ||
1120 | wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features); | |
1121 | ||
1122 | return count; | |
1123 | } | |
1124 | ||
e0984bc3 | 1125 | static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM, |
f81a1295 BT |
1126 | wacom_show_speed, wacom_store_speed); |
1127 | ||
72b236d6 AS |
1128 | |
1129 | static ssize_t wacom_show_remote_mode(struct kobject *kobj, | |
1130 | struct kobj_attribute *kattr, | |
1131 | char *buf, int index) | |
1132 | { | |
2cf83833 | 1133 | struct device *dev = kobj_to_dev(kobj->parent); |
ee79a8f8 | 1134 | struct hid_device *hdev = to_hid_device(dev); |
72b236d6 AS |
1135 | struct wacom *wacom = hid_get_drvdata(hdev); |
1136 | u8 mode; | |
1137 | ||
1138 | mode = wacom->led.select[index]; | |
1139 | if (mode >= 0 && mode < 3) | |
1140 | return snprintf(buf, PAGE_SIZE, "%d\n", mode); | |
1141 | else | |
1142 | return snprintf(buf, PAGE_SIZE, "%d\n", -1); | |
1143 | } | |
1144 | ||
1145 | #define DEVICE_EKR_ATTR_GROUP(SET_ID) \ | |
1146 | static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \ | |
1147 | struct kobj_attribute *kattr, char *buf) \ | |
1148 | { \ | |
1149 | return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \ | |
1150 | } \ | |
1151 | static struct kobj_attribute remote##SET_ID##_mode_attr = { \ | |
1152 | .attr = {.name = "remote_mode", \ | |
1153 | .mode = DEV_ATTR_RO_PERM}, \ | |
1154 | .show = wacom_show_remote##SET_ID##_mode, \ | |
1155 | }; \ | |
1156 | static struct attribute *remote##SET_ID##_serial_attrs[] = { \ | |
1157 | &remote##SET_ID##_mode_attr.attr, \ | |
1158 | NULL \ | |
1159 | }; \ | |
1160 | static struct attribute_group remote##SET_ID##_serial_group = { \ | |
1161 | .name = NULL, \ | |
1162 | .attrs = remote##SET_ID##_serial_attrs, \ | |
1163 | } | |
1164 | ||
1165 | DEVICE_EKR_ATTR_GROUP(0); | |
1166 | DEVICE_EKR_ATTR_GROUP(1); | |
1167 | DEVICE_EKR_ATTR_GROUP(2); | |
1168 | DEVICE_EKR_ATTR_GROUP(3); | |
1169 | DEVICE_EKR_ATTR_GROUP(4); | |
1170 | ||
1171 | int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial, int index) | |
1172 | { | |
1173 | int error = 0; | |
1174 | char *buf; | |
1175 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
1176 | ||
1177 | wacom_wac->serial[index] = serial; | |
1178 | ||
1179 | buf = kzalloc(WAC_REMOTE_SERIAL_MAX_STRLEN, GFP_KERNEL); | |
1180 | if (!buf) | |
1181 | return -ENOMEM; | |
1182 | snprintf(buf, WAC_REMOTE_SERIAL_MAX_STRLEN, "%d", serial); | |
1183 | wacom->remote_group[index].name = buf; | |
1184 | ||
1185 | error = sysfs_create_group(wacom->remote_dir, | |
1186 | &wacom->remote_group[index]); | |
1187 | if (error) { | |
1188 | hid_err(wacom->hdev, | |
1189 | "cannot create sysfs group err: %d\n", error); | |
1190 | kobject_put(wacom->remote_dir); | |
1191 | return error; | |
1192 | } | |
1193 | ||
1194 | return 0; | |
1195 | } | |
1196 | ||
1197 | void wacom_remote_destroy_attr_group(struct wacom *wacom, __u32 serial) | |
1198 | { | |
1199 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
1200 | int i; | |
1201 | ||
1202 | if (!serial) | |
1203 | return; | |
1204 | ||
1205 | for (i = 0; i < WACOM_MAX_REMOTES; i++) { | |
1206 | if (wacom_wac->serial[i] == serial) { | |
1207 | wacom_wac->serial[i] = 0; | |
1208 | wacom->led.select[i] = WACOM_STATUS_UNKNOWN; | |
1209 | if (wacom->remote_group[i].name) { | |
1210 | sysfs_remove_group(wacom->remote_dir, | |
1211 | &wacom->remote_group[i]); | |
1212 | kfree(wacom->remote_group[i].name); | |
1213 | wacom->remote_group[i].name = NULL; | |
1214 | } | |
1215 | } | |
1216 | } | |
1217 | } | |
1218 | ||
1219 | static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector) | |
1220 | { | |
1221 | const size_t buf_size = 2; | |
1222 | unsigned char *buf; | |
1223 | int retval; | |
1224 | ||
1225 | buf = kzalloc(buf_size, GFP_KERNEL); | |
1226 | if (!buf) | |
1227 | return -ENOMEM; | |
1228 | ||
1229 | buf[0] = WAC_CMD_DELETE_PAIRING; | |
1230 | buf[1] = selector; | |
1231 | ||
1232 | retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf, | |
1233 | buf_size, WAC_CMD_RETRIES); | |
1234 | kfree(buf); | |
1235 | ||
1236 | return retval; | |
1237 | } | |
1238 | ||
1239 | static ssize_t wacom_store_unpair_remote(struct kobject *kobj, | |
1240 | struct kobj_attribute *attr, | |
1241 | const char *buf, size_t count) | |
1242 | { | |
1243 | unsigned char selector = 0; | |
2cf83833 | 1244 | struct device *dev = kobj_to_dev(kobj->parent); |
ee79a8f8 | 1245 | struct hid_device *hdev = to_hid_device(dev); |
72b236d6 AS |
1246 | struct wacom *wacom = hid_get_drvdata(hdev); |
1247 | int err; | |
1248 | ||
1249 | if (!strncmp(buf, "*\n", 2)) { | |
1250 | selector = WAC_CMD_UNPAIR_ALL; | |
1251 | } else { | |
1252 | hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n", | |
1253 | buf); | |
1254 | return -1; | |
1255 | } | |
1256 | ||
1257 | mutex_lock(&wacom->lock); | |
1258 | ||
1259 | err = wacom_cmd_unpair_remote(wacom, selector); | |
1260 | mutex_unlock(&wacom->lock); | |
1261 | ||
1262 | return err < 0 ? err : count; | |
1263 | } | |
1264 | ||
1265 | static struct kobj_attribute unpair_remote_attr = { | |
1266 | .attr = {.name = "unpair_remote", .mode = 0200}, | |
1267 | .store = wacom_store_unpair_remote, | |
1268 | }; | |
1269 | ||
1270 | static const struct attribute *remote_unpair_attrs[] = { | |
1271 | &unpair_remote_attr.attr, | |
1272 | NULL | |
1273 | }; | |
1274 | ||
1275 | static int wacom_initialize_remote(struct wacom *wacom) | |
1276 | { | |
1277 | int error = 0; | |
1278 | struct wacom_wac *wacom_wac = &(wacom->wacom_wac); | |
1279 | int i; | |
1280 | ||
1281 | if (wacom->wacom_wac.features.type != REMOTE) | |
1282 | return 0; | |
1283 | ||
1284 | wacom->remote_group[0] = remote0_serial_group; | |
1285 | wacom->remote_group[1] = remote1_serial_group; | |
1286 | wacom->remote_group[2] = remote2_serial_group; | |
1287 | wacom->remote_group[3] = remote3_serial_group; | |
1288 | wacom->remote_group[4] = remote4_serial_group; | |
1289 | ||
1290 | wacom->remote_dir = kobject_create_and_add("wacom_remote", | |
1291 | &wacom->hdev->dev.kobj); | |
1292 | if (!wacom->remote_dir) | |
1293 | return -ENOMEM; | |
1294 | ||
1295 | error = sysfs_create_files(wacom->remote_dir, remote_unpair_attrs); | |
1296 | ||
1297 | if (error) { | |
1298 | hid_err(wacom->hdev, | |
1299 | "cannot create sysfs group err: %d\n", error); | |
1300 | return error; | |
1301 | } | |
1302 | ||
1303 | for (i = 0; i < WACOM_MAX_REMOTES; i++) { | |
1304 | wacom->led.select[i] = WACOM_STATUS_UNKNOWN; | |
1305 | wacom_wac->serial[i] = 0; | |
1306 | } | |
1307 | ||
1308 | return 0; | |
1309 | } | |
1310 | ||
d2d13f18 | 1311 | static struct input_dev *wacom_allocate_input(struct wacom *wacom) |
3aac0ef1 CB |
1312 | { |
1313 | struct input_dev *input_dev; | |
b6c79f2c | 1314 | struct hid_device *hdev = wacom->hdev; |
3aac0ef1 | 1315 | struct wacom_wac *wacom_wac = &(wacom->wacom_wac); |
3aac0ef1 CB |
1316 | |
1317 | input_dev = input_allocate_device(); | |
d2d13f18 BT |
1318 | if (!input_dev) |
1319 | return NULL; | |
3aac0ef1 | 1320 | |
2bdd163c | 1321 | input_dev->name = wacom_wac->features.name; |
b6c79f2c BT |
1322 | input_dev->phys = hdev->phys; |
1323 | input_dev->dev.parent = &hdev->dev; | |
3aac0ef1 CB |
1324 | input_dev->open = wacom_open; |
1325 | input_dev->close = wacom_close; | |
b6c79f2c BT |
1326 | input_dev->uniq = hdev->uniq; |
1327 | input_dev->id.bustype = hdev->bus; | |
1328 | input_dev->id.vendor = hdev->vendor; | |
12969e3b | 1329 | input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product; |
b6c79f2c | 1330 | input_dev->id.version = hdev->version; |
3aac0ef1 CB |
1331 | input_set_drvdata(input_dev, wacom); |
1332 | ||
d2d13f18 BT |
1333 | return input_dev; |
1334 | } | |
1335 | ||
2546dacd BT |
1336 | static void wacom_clean_inputs(struct wacom *wacom) |
1337 | { | |
2a6cdbdd JG |
1338 | if (wacom->wacom_wac.pen_input) { |
1339 | if (wacom->wacom_wac.pen_registered) | |
1340 | input_unregister_device(wacom->wacom_wac.pen_input); | |
2546dacd | 1341 | else |
2a6cdbdd JG |
1342 | input_free_device(wacom->wacom_wac.pen_input); |
1343 | } | |
1344 | if (wacom->wacom_wac.touch_input) { | |
1345 | if (wacom->wacom_wac.touch_registered) | |
1346 | input_unregister_device(wacom->wacom_wac.touch_input); | |
1347 | else | |
1348 | input_free_device(wacom->wacom_wac.touch_input); | |
2546dacd BT |
1349 | } |
1350 | if (wacom->wacom_wac.pad_input) { | |
954df6ad | 1351 | if (wacom->wacom_wac.pad_registered) |
2546dacd BT |
1352 | input_unregister_device(wacom->wacom_wac.pad_input); |
1353 | else | |
1354 | input_free_device(wacom->wacom_wac.pad_input); | |
1355 | } | |
27ba1d56 | 1356 | kobject_put(wacom->remote_dir); |
2a6cdbdd JG |
1357 | wacom->wacom_wac.pen_input = NULL; |
1358 | wacom->wacom_wac.touch_input = NULL; | |
2546dacd BT |
1359 | wacom->wacom_wac.pad_input = NULL; |
1360 | wacom_destroy_leds(wacom); | |
1361 | } | |
1362 | ||
d9f2d203 JG |
1363 | static int wacom_allocate_inputs(struct wacom *wacom) |
1364 | { | |
1365 | struct wacom_wac *wacom_wac = &(wacom->wacom_wac); | |
1366 | ||
1367 | wacom_wac->pen_input = wacom_allocate_input(wacom); | |
1368 | wacom_wac->touch_input = wacom_allocate_input(wacom); | |
1369 | wacom_wac->pad_input = wacom_allocate_input(wacom); | |
1370 | if (!wacom_wac->pen_input || !wacom_wac->touch_input || !wacom_wac->pad_input) { | |
1371 | wacom_clean_inputs(wacom); | |
1372 | return -ENOMEM; | |
1373 | } | |
1374 | ||
2bdd163c | 1375 | wacom_wac->pen_input->name = wacom_wac->pen_name; |
d9f2d203 JG |
1376 | wacom_wac->touch_input->name = wacom_wac->touch_name; |
1377 | wacom_wac->pad_input->name = wacom_wac->pad_name; | |
1378 | ||
1379 | return 0; | |
1380 | } | |
1381 | ||
2546dacd BT |
1382 | static int wacom_register_inputs(struct wacom *wacom) |
1383 | { | |
2a6cdbdd | 1384 | struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev; |
2546dacd | 1385 | struct wacom_wac *wacom_wac = &(wacom->wacom_wac); |
2636a3f2 | 1386 | int error = 0; |
2546dacd | 1387 | |
2a6cdbdd JG |
1388 | pen_input_dev = wacom_wac->pen_input; |
1389 | touch_input_dev = wacom_wac->touch_input; | |
2546dacd BT |
1390 | pad_input_dev = wacom_wac->pad_input; |
1391 | ||
2a6cdbdd | 1392 | if (!pen_input_dev || !touch_input_dev || !pad_input_dev) |
2546dacd BT |
1393 | return -EINVAL; |
1394 | ||
2a6cdbdd JG |
1395 | error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac); |
1396 | if (error) { | |
1397 | /* no pen in use on this interface */ | |
1398 | input_free_device(pen_input_dev); | |
1399 | wacom_wac->pen_input = NULL; | |
1400 | pen_input_dev = NULL; | |
1401 | } else { | |
1402 | error = input_register_device(pen_input_dev); | |
1403 | if (error) | |
1404 | goto fail_register_pen_input; | |
1405 | wacom_wac->pen_registered = true; | |
1406 | } | |
1407 | ||
1408 | error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac); | |
1409 | if (error) { | |
1410 | /* no touch in use on this interface */ | |
1411 | input_free_device(touch_input_dev); | |
1412 | wacom_wac->touch_input = NULL; | |
1413 | touch_input_dev = NULL; | |
1414 | } else { | |
1415 | error = input_register_device(touch_input_dev); | |
30ebc1ae | 1416 | if (error) |
2a6cdbdd JG |
1417 | goto fail_register_touch_input; |
1418 | wacom_wac->touch_registered = true; | |
30ebc1ae | 1419 | } |
1963518b | 1420 | |
d2d13f18 BT |
1421 | error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac); |
1422 | if (error) { | |
1423 | /* no pad in use on this interface */ | |
1424 | input_free_device(pad_input_dev); | |
1425 | wacom_wac->pad_input = NULL; | |
1426 | pad_input_dev = NULL; | |
1427 | } else { | |
1428 | error = input_register_device(pad_input_dev); | |
1429 | if (error) | |
7fefeec5 | 1430 | goto fail_register_pad_input; |
954df6ad | 1431 | wacom_wac->pad_registered = true; |
912ca216 PC |
1432 | |
1433 | error = wacom_initialize_leds(wacom); | |
1434 | if (error) | |
7fefeec5 | 1435 | goto fail_leds; |
72b236d6 AS |
1436 | |
1437 | error = wacom_initialize_remote(wacom); | |
1438 | if (error) | |
1439 | goto fail_remote; | |
d2d13f18 BT |
1440 | } |
1441 | ||
1963518b | 1442 | return 0; |
3aac0ef1 | 1443 | |
72b236d6 AS |
1444 | fail_remote: |
1445 | wacom_destroy_leds(wacom); | |
7fefeec5 | 1446 | fail_leds: |
912ca216 PC |
1447 | input_unregister_device(pad_input_dev); |
1448 | pad_input_dev = NULL; | |
954df6ad | 1449 | wacom_wac->pad_registered = false; |
7fefeec5 | 1450 | fail_register_pad_input: |
0fd72ff9 DC |
1451 | if (touch_input_dev) |
1452 | input_unregister_device(touch_input_dev); | |
2a6cdbdd JG |
1453 | wacom_wac->touch_input = NULL; |
1454 | wacom_wac->touch_registered = false; | |
1455 | fail_register_touch_input: | |
0fd72ff9 DC |
1456 | if (pen_input_dev) |
1457 | input_unregister_device(pen_input_dev); | |
2a6cdbdd JG |
1458 | wacom_wac->pen_input = NULL; |
1459 | wacom_wac->pen_registered = false; | |
1460 | fail_register_pen_input: | |
3aac0ef1 CB |
1461 | return error; |
1462 | } | |
1463 | ||
0be01712 JG |
1464 | /* |
1465 | * Not all devices report physical dimensions from HID. | |
1466 | * Compute the default from hardcoded logical dimension | |
1467 | * and resolution before driver overwrites them. | |
1468 | */ | |
1469 | static void wacom_set_default_phy(struct wacom_features *features) | |
1470 | { | |
1471 | if (features->x_resolution) { | |
1472 | features->x_phy = (features->x_max * 100) / | |
1473 | features->x_resolution; | |
1474 | features->y_phy = (features->y_max * 100) / | |
1475 | features->y_resolution; | |
1476 | } | |
1477 | } | |
1478 | ||
1479 | static void wacom_calculate_res(struct wacom_features *features) | |
1480 | { | |
1481 | /* set unit to "100th of a mm" for devices not reported by HID */ | |
1482 | if (!features->unit) { | |
1483 | features->unit = 0x11; | |
1484 | features->unitExpo = -3; | |
1485 | } | |
1486 | ||
1487 | features->x_resolution = wacom_calc_hid_res(features->x_max, | |
1488 | features->x_phy, | |
1489 | features->unit, | |
1490 | features->unitExpo); | |
1491 | features->y_resolution = wacom_calc_hid_res(features->y_max, | |
1492 | features->y_phy, | |
1493 | features->unit, | |
1494 | features->unitExpo); | |
1495 | } | |
1496 | ||
fce9957d JG |
1497 | void wacom_battery_work(struct work_struct *work) |
1498 | { | |
1499 | struct wacom *wacom = container_of(work, struct wacom, work); | |
1500 | ||
1501 | if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) && | |
8de29a35 | 1502 | !wacom->battery) { |
fce9957d JG |
1503 | wacom_initialize_battery(wacom); |
1504 | } | |
1505 | else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) && | |
8de29a35 | 1506 | wacom->battery) { |
fce9957d JG |
1507 | wacom_destroy_battery(wacom); |
1508 | } | |
1509 | } | |
1510 | ||
01c846f9 BT |
1511 | static size_t wacom_compute_pktlen(struct hid_device *hdev) |
1512 | { | |
1513 | struct hid_report_enum *report_enum; | |
1514 | struct hid_report *report; | |
1515 | size_t size = 0; | |
1516 | ||
1517 | report_enum = hdev->report_enum + HID_INPUT_REPORT; | |
1518 | ||
1519 | list_for_each_entry(report, &report_enum->report_list, list) { | |
dabb05c6 | 1520 | size_t report_size = hid_report_len(report); |
01c846f9 BT |
1521 | if (report_size > size) |
1522 | size = report_size; | |
1523 | } | |
1524 | ||
1525 | return size; | |
1526 | } | |
1527 | ||
fd5f92b6 | 1528 | static void wacom_update_name(struct wacom *wacom, const char *suffix) |
c24eab4e PC |
1529 | { |
1530 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
1531 | struct wacom_features *features = &wacom_wac->features; | |
44b5250b | 1532 | char name[WACOM_NAME_MAX]; |
c24eab4e PC |
1533 | |
1534 | /* Generic devices name unspecified */ | |
1535 | if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) { | |
1536 | if (strstr(wacom->hdev->name, "Wacom") || | |
1537 | strstr(wacom->hdev->name, "wacom") || | |
1538 | strstr(wacom->hdev->name, "WACOM")) { | |
1539 | /* name is in HID descriptor, use it */ | |
44b5250b | 1540 | strlcpy(name, wacom->hdev->name, sizeof(name)); |
c24eab4e PC |
1541 | |
1542 | /* strip out excess whitespaces */ | |
1543 | while (1) { | |
44b5250b | 1544 | char *gap = strstr(name, " "); |
c24eab4e PC |
1545 | if (gap == NULL) |
1546 | break; | |
1547 | /* shift everything including the terminator */ | |
1548 | memmove(gap, gap+1, strlen(gap)); | |
1549 | } | |
1550 | /* get rid of trailing whitespace */ | |
44b5250b JG |
1551 | if (name[strlen(name)-1] == ' ') |
1552 | name[strlen(name)-1] = '\0'; | |
c24eab4e PC |
1553 | } else { |
1554 | /* no meaningful name retrieved. use product ID */ | |
44b5250b | 1555 | snprintf(name, sizeof(name), |
c24eab4e PC |
1556 | "%s %X", features->name, wacom->hdev->product); |
1557 | } | |
1558 | } else { | |
44b5250b | 1559 | strlcpy(name, features->name, sizeof(name)); |
c24eab4e PC |
1560 | } |
1561 | ||
1562 | /* Append the device type to the name */ | |
2a6cdbdd | 1563 | snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name), |
fd5f92b6 | 1564 | "%s%s Pen", name, suffix); |
2a6cdbdd | 1565 | snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name), |
fd5f92b6 | 1566 | "%s%s Finger", name, suffix); |
c24eab4e | 1567 | snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name), |
fd5f92b6 | 1568 | "%s%s Pad", name, suffix); |
c24eab4e PC |
1569 | } |
1570 | ||
fd5f92b6 | 1571 | static int wacom_parse_and_register(struct wacom *wacom, bool wireless) |
3bea733a | 1572 | { |
c58ac3a8 BT |
1573 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; |
1574 | struct wacom_features *features = &wacom_wac->features; | |
1575 | struct hid_device *hdev = wacom->hdev; | |
e33da8a5 | 1576 | int error; |
7704ac93 | 1577 | unsigned int connect_mask = HID_CONNECT_HIDRAW; |
3bea733a | 1578 | |
01c846f9 | 1579 | features->pktlen = wacom_compute_pktlen(hdev); |
c58ac3a8 BT |
1580 | if (features->pktlen > WACOM_PKGLEN_MAX) |
1581 | return -EINVAL; | |
3bea733a | 1582 | |
3f14a63a JG |
1583 | error = wacom_allocate_inputs(wacom); |
1584 | if (error) | |
c58ac3a8 | 1585 | return error; |
494078b0 | 1586 | |
8c97a765 BT |
1587 | /* |
1588 | * Bamboo Pad has a generic hid handling for the Pen, and we switch it | |
1589 | * into debug mode for the touch part. | |
1590 | * We ignore the other interfaces. | |
1591 | */ | |
1592 | if (features->type == BAMBOO_PAD) { | |
1593 | if (features->pktlen == WACOM_PKGLEN_PENABLED) { | |
1594 | features->type = HID_GENERIC; | |
1595 | } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) && | |
1596 | (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) { | |
1597 | error = -ENODEV; | |
c58ac3a8 | 1598 | goto fail_allocate_inputs; |
8c97a765 BT |
1599 | } |
1600 | } | |
1601 | ||
401d7d10 PC |
1602 | /* set the default size in case we do not get them from hid */ |
1603 | wacom_set_default_phy(features); | |
1604 | ||
f393ee2b | 1605 | /* Retrieve the physical and logical size for touch devices */ |
c669fb2b | 1606 | wacom_retrieve_hid_descriptor(hdev, features); |
42f4f272 | 1607 | wacom_setup_device_quirks(wacom); |
042628ab | 1608 | |
aa86b18c JG |
1609 | if (features->device_type == WACOM_DEVICETYPE_NONE && |
1610 | features->type != WIRELESS) { | |
8e116d31 JG |
1611 | error = features->type == HID_GENERIC ? -ENODEV : 0; |
1612 | ||
042628ab | 1613 | dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.", |
8e116d31 JG |
1614 | hdev->name, |
1615 | error ? "Ignoring" : "Assuming pen"); | |
1616 | ||
1617 | if (error) | |
c58ac3a8 | 1618 | goto fail_parsed; |
042628ab | 1619 | |
aa86b18c | 1620 | features->device_type |= WACOM_DEVICETYPE_PEN; |
042628ab JG |
1621 | } |
1622 | ||
401d7d10 PC |
1623 | wacom_calculate_res(features); |
1624 | ||
fd5f92b6 | 1625 | wacom_update_name(wacom, wireless ? " (WL)" : ""); |
4492efff | 1626 | |
f3586d2f PC |
1627 | error = wacom_add_shared_data(hdev); |
1628 | if (error) | |
1629 | goto fail_shared_data; | |
49b764ae | 1630 | |
ccad85cc | 1631 | if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) && |
f81a1295 BT |
1632 | (features->quirks & WACOM_QUIRK_BATTERY)) { |
1633 | error = wacom_initialize_battery(wacom); | |
1634 | if (error) | |
7fefeec5 | 1635 | goto fail_battery; |
f81a1295 BT |
1636 | } |
1637 | ||
3f14a63a JG |
1638 | error = wacom_register_inputs(wacom); |
1639 | if (error) | |
1640 | goto fail_register_inputs; | |
f81a1295 | 1641 | |
7704ac93 BT |
1642 | if (features->type == HID_GENERIC) |
1643 | connect_mask |= HID_CONNECT_DRIVER; | |
1644 | ||
29b47391 | 1645 | /* Regular HID work starts now */ |
7704ac93 | 1646 | error = hid_hw_start(hdev, connect_mask); |
29b47391 BT |
1647 | if (error) { |
1648 | hid_err(hdev, "hw start failed\n"); | |
7fefeec5 | 1649 | goto fail_hw_start; |
d3825d51 | 1650 | } |
961794a0 | 1651 | |
fd5f92b6 BT |
1652 | if (!wireless) { |
1653 | /* Note that if query fails it is not a hard failure */ | |
1654 | wacom_query_tablet_data(hdev, features); | |
1655 | } | |
86e88f0e JG |
1656 | |
1657 | /* touch only Bamboo doesn't support pen */ | |
1658 | if ((features->type == BAMBOO_TOUCH) && | |
1659 | (features->device_type & WACOM_DEVICETYPE_PEN)) { | |
1660 | error = -ENODEV; | |
1661 | goto fail_hw_start; | |
1662 | } | |
1663 | ||
1664 | /* pen only Bamboo neither support touch nor pad */ | |
1665 | if ((features->type == BAMBOO_PEN) && | |
1666 | ((features->device_type & WACOM_DEVICETYPE_TOUCH) || | |
1667 | (features->device_type & WACOM_DEVICETYPE_PAD))) { | |
1668 | error = -ENODEV; | |
1669 | goto fail_hw_start; | |
1670 | } | |
1671 | ||
ccad85cc | 1672 | if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR) |
29b47391 BT |
1673 | error = hid_hw_open(hdev); |
1674 | ||
eda01dab PC |
1675 | if ((wacom_wac->features.type == INTUOSHT || |
1676 | wacom_wac->features.type == INTUOSHT2) && | |
1677 | (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) { | |
2a6cdbdd | 1678 | wacom_wac->shared->touch_input = wacom_wac->touch_input; |
961794a0 PC |
1679 | } |
1680 | ||
3bea733a PC |
1681 | return 0; |
1682 | ||
7fefeec5 | 1683 | fail_hw_start: |
c58ac3a8 | 1684 | hid_hw_stop(hdev); |
7fefeec5 | 1685 | fail_register_inputs: |
2546dacd | 1686 | wacom_clean_inputs(wacom); |
7fefeec5 BT |
1687 | wacom_destroy_battery(wacom); |
1688 | fail_battery: | |
a97ac104 | 1689 | wacom_remove_shared_data(wacom); |
7fefeec5 | 1690 | fail_shared_data: |
c58ac3a8 | 1691 | fail_parsed: |
494078b0 | 1692 | fail_allocate_inputs: |
c58ac3a8 BT |
1693 | wacom_clean_inputs(wacom); |
1694 | return error; | |
1695 | } | |
1696 | ||
a2f091af BT |
1697 | static void wacom_wireless_work(struct work_struct *work) |
1698 | { | |
1699 | struct wacom *wacom = container_of(work, struct wacom, work); | |
1700 | struct usb_device *usbdev = wacom->usbdev; | |
1701 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
1702 | struct hid_device *hdev1, *hdev2; | |
1703 | struct wacom *wacom1, *wacom2; | |
1704 | struct wacom_wac *wacom_wac1, *wacom_wac2; | |
1705 | int error; | |
1706 | ||
1707 | /* | |
1708 | * Regardless if this is a disconnect or a new tablet, | |
1709 | * remove any existing input and battery devices. | |
1710 | */ | |
1711 | ||
1712 | wacom_destroy_battery(wacom); | |
1713 | ||
1714 | /* Stylus interface */ | |
1715 | hdev1 = usb_get_intfdata(usbdev->config->interface[1]); | |
1716 | wacom1 = hid_get_drvdata(hdev1); | |
1717 | wacom_wac1 = &(wacom1->wacom_wac); | |
1718 | wacom_clean_inputs(wacom1); | |
1719 | ||
1720 | /* Touch interface */ | |
1721 | hdev2 = usb_get_intfdata(usbdev->config->interface[2]); | |
1722 | wacom2 = hid_get_drvdata(hdev2); | |
1723 | wacom_wac2 = &(wacom2->wacom_wac); | |
1724 | wacom_clean_inputs(wacom2); | |
1725 | ||
1726 | if (wacom_wac->pid == 0) { | |
1727 | hid_info(wacom->hdev, "wireless tablet disconnected\n"); | |
1728 | wacom_wac1->shared->type = 0; | |
1729 | } else { | |
1730 | const struct hid_device_id *id = wacom_ids; | |
1731 | ||
1732 | hid_info(wacom->hdev, "wireless tablet connected with PID %x\n", | |
1733 | wacom_wac->pid); | |
1734 | ||
1735 | while (id->bus) { | |
1736 | if (id->vendor == USB_VENDOR_ID_WACOM && | |
1737 | id->product == wacom_wac->pid) | |
1738 | break; | |
1739 | id++; | |
1740 | } | |
1741 | ||
1742 | if (!id->bus) { | |
1743 | hid_info(wacom->hdev, "ignoring unknown PID.\n"); | |
1744 | return; | |
1745 | } | |
1746 | ||
1747 | /* Stylus interface */ | |
1748 | wacom_wac1->features = | |
1749 | *((struct wacom_features *)id->driver_data); | |
fd5f92b6 | 1750 | |
a2f091af | 1751 | wacom_wac1->pid = wacom_wac->pid; |
fd5f92b6 BT |
1752 | hid_hw_stop(hdev1); |
1753 | error = wacom_parse_and_register(wacom1, true); | |
a2f091af BT |
1754 | if (error) |
1755 | goto fail; | |
1756 | ||
1757 | /* Touch interface */ | |
1758 | if (wacom_wac1->features.touch_max || | |
1759 | (wacom_wac1->features.type >= INTUOSHT && | |
1760 | wacom_wac1->features.type <= BAMBOO_PT)) { | |
1761 | wacom_wac2->features = | |
1762 | *((struct wacom_features *)id->driver_data); | |
a2f091af | 1763 | wacom_wac2->pid = wacom_wac->pid; |
fd5f92b6 BT |
1764 | hid_hw_stop(hdev2); |
1765 | error = wacom_parse_and_register(wacom2, true); | |
a2f091af BT |
1766 | if (error) |
1767 | goto fail; | |
a2f091af BT |
1768 | } |
1769 | ||
1770 | error = wacom_initialize_battery(wacom); | |
1771 | if (error) | |
1772 | goto fail; | |
1773 | } | |
1774 | ||
1775 | return; | |
1776 | ||
1777 | fail: | |
1778 | wacom_clean_inputs(wacom1); | |
1779 | wacom_clean_inputs(wacom2); | |
1780 | return; | |
1781 | } | |
1782 | ||
c58ac3a8 BT |
1783 | static int wacom_probe(struct hid_device *hdev, |
1784 | const struct hid_device_id *id) | |
1785 | { | |
1786 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
1787 | struct usb_device *dev = interface_to_usbdev(intf); | |
1788 | struct wacom *wacom; | |
1789 | struct wacom_wac *wacom_wac; | |
1790 | struct wacom_features *features; | |
1791 | int error; | |
1792 | ||
1793 | if (!id->driver_data) | |
1794 | return -EINVAL; | |
1795 | ||
1796 | hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS; | |
1797 | ||
1798 | /* hid-core sets this quirk for the boot interface */ | |
1799 | hdev->quirks &= ~HID_QUIRK_NOGET; | |
1800 | ||
1801 | wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL); | |
1802 | if (!wacom) | |
1803 | return -ENOMEM; | |
1804 | ||
1805 | hid_set_drvdata(hdev, wacom); | |
1806 | wacom->hdev = hdev; | |
1807 | ||
1808 | wacom_wac = &wacom->wacom_wac; | |
1809 | wacom_wac->features = *((struct wacom_features *)id->driver_data); | |
1810 | features = &wacom_wac->features; | |
1811 | ||
1812 | if (features->check_for_hid_type && features->hid_type != hdev->type) { | |
1813 | error = -ENODEV; | |
1814 | goto fail_type; | |
1815 | } | |
1816 | ||
1817 | wacom->usbdev = dev; | |
1818 | wacom->intf = intf; | |
1819 | mutex_init(&wacom->lock); | |
1820 | INIT_WORK(&wacom->work, wacom_wireless_work); | |
1821 | ||
1822 | /* ask for the report descriptor to be loaded by HID */ | |
1823 | error = hid_parse(hdev); | |
1824 | if (error) { | |
1825 | hid_err(hdev, "parse failed\n"); | |
1826 | goto fail_parse; | |
1827 | } | |
1828 | ||
fd5f92b6 | 1829 | error = wacom_parse_and_register(wacom, false); |
c58ac3a8 BT |
1830 | if (error) |
1831 | goto fail_parse; | |
1832 | ||
1833 | if (hdev->bus == BUS_BLUETOOTH) { | |
1834 | error = device_create_file(&hdev->dev, &dev_attr_speed); | |
1835 | if (error) | |
1836 | hid_warn(hdev, | |
1837 | "can't create sysfs speed attribute err: %d\n", | |
1838 | error); | |
1839 | } | |
1840 | ||
1841 | return 0; | |
1842 | ||
7fefeec5 | 1843 | fail_type: |
7fefeec5 BT |
1844 | fail_parse: |
1845 | kfree(wacom); | |
29b47391 | 1846 | hid_set_drvdata(hdev, NULL); |
5014186d | 1847 | return error; |
3bea733a PC |
1848 | } |
1849 | ||
29b47391 | 1850 | static void wacom_remove(struct hid_device *hdev) |
3bea733a | 1851 | { |
29b47391 | 1852 | struct wacom *wacom = hid_get_drvdata(hdev); |
3bea733a | 1853 | |
29b47391 | 1854 | hid_hw_stop(hdev); |
e7224094 | 1855 | |
16bf288c | 1856 | cancel_work_sync(&wacom->work); |
2546dacd | 1857 | wacom_clean_inputs(wacom); |
f81a1295 BT |
1858 | if (hdev->bus == BUS_BLUETOOTH) |
1859 | device_remove_file(&hdev->dev, &dev_attr_speed); | |
a1d552cc | 1860 | wacom_destroy_battery(wacom); |
a97ac104 | 1861 | wacom_remove_shared_data(wacom); |
e7224094 | 1862 | |
29b47391 BT |
1863 | hid_set_drvdata(hdev, NULL); |
1864 | kfree(wacom); | |
e7224094 ON |
1865 | } |
1866 | ||
41a74581 | 1867 | #ifdef CONFIG_PM |
29b47391 | 1868 | static int wacom_resume(struct hid_device *hdev) |
e7224094 | 1869 | { |
29b47391 | 1870 | struct wacom *wacom = hid_get_drvdata(hdev); |
51269fe8 | 1871 | struct wacom_features *features = &wacom->wacom_wac.features; |
e7224094 ON |
1872 | |
1873 | mutex_lock(&wacom->lock); | |
38101475 PC |
1874 | |
1875 | /* switch to wacom mode first */ | |
27b20a9d | 1876 | wacom_query_tablet_data(hdev, features); |
5d7e7d47 | 1877 | wacom_led_control(wacom); |
38101475 | 1878 | |
e7224094 ON |
1879 | mutex_unlock(&wacom->lock); |
1880 | ||
29b47391 | 1881 | return 0; |
e7224094 ON |
1882 | } |
1883 | ||
29b47391 | 1884 | static int wacom_reset_resume(struct hid_device *hdev) |
e7224094 | 1885 | { |
29b47391 | 1886 | return wacom_resume(hdev); |
3bea733a | 1887 | } |
41a74581 | 1888 | #endif /* CONFIG_PM */ |
3bea733a | 1889 | |
29b47391 | 1890 | static struct hid_driver wacom_driver = { |
3bea733a | 1891 | .name = "wacom", |
b036f6fb | 1892 | .id_table = wacom_ids, |
3bea733a | 1893 | .probe = wacom_probe, |
29b47391 | 1894 | .remove = wacom_remove, |
7704ac93 | 1895 | .report = wacom_wac_report, |
29b47391 | 1896 | #ifdef CONFIG_PM |
e7224094 ON |
1897 | .resume = wacom_resume, |
1898 | .reset_resume = wacom_reset_resume, | |
29b47391 BT |
1899 | #endif |
1900 | .raw_event = wacom_raw_event, | |
3bea733a | 1901 | }; |
29b47391 | 1902 | module_hid_driver(wacom_driver); |
f2e0a7d4 BT |
1903 | |
1904 | MODULE_VERSION(DRIVER_VERSION); | |
1905 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
1906 | MODULE_DESCRIPTION(DRIVER_DESC); | |
1907 | MODULE_LICENSE(DRIVER_LICENSE); |