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