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" |
f54bc61c | 16 | #include <linux/hid.h> |
3bea733a | 17 | |
a417ea44 | 18 | #define WAC_MSG_RETRIES 5 |
3bea733a | 19 | |
5d7e7d47 EH |
20 | #define WAC_CMD_LED_CONTROL 0x20 |
21 | #define WAC_CMD_ICON_START 0x21 | |
22 | #define WAC_CMD_ICON_XFER 0x23 | |
23 | #define WAC_CMD_RETRIES 10 | |
24 | ||
27b20a9d | 25 | static int wacom_get_report(struct hid_device *hdev, u8 type, u8 id, |
5d7e7d47 | 26 | void *buf, size_t size, unsigned int retries) |
3bea733a | 27 | { |
5d7e7d47 EH |
28 | int retval; |
29 | ||
30 | do { | |
27b20a9d BT |
31 | retval = hid_hw_raw_request(hdev, id, buf, size, type, |
32 | HID_REQ_GET_REPORT); | |
5d7e7d47 EH |
33 | } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries); |
34 | ||
35 | return retval; | |
3bea733a PC |
36 | } |
37 | ||
27b20a9d | 38 | static int wacom_set_report(struct hid_device *hdev, u8 type, u8 id, |
5d7e7d47 | 39 | void *buf, size_t size, unsigned int retries) |
3bea733a | 40 | { |
5d7e7d47 EH |
41 | int retval; |
42 | ||
43 | do { | |
27b20a9d BT |
44 | retval = hid_hw_raw_request(hdev, id, buf, size, type, |
45 | HID_REQ_SET_REPORT); | |
5d7e7d47 EH |
46 | } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries); |
47 | ||
48 | return retval; | |
3bea733a PC |
49 | } |
50 | ||
29b47391 BT |
51 | static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report, |
52 | u8 *raw_data, int size) | |
3bea733a | 53 | { |
29b47391 | 54 | struct wacom *wacom = hid_get_drvdata(hdev); |
3bea733a | 55 | |
29b47391 BT |
56 | if (size > WACOM_PKGLEN_MAX) |
57 | return 1; | |
3bea733a | 58 | |
29b47391 | 59 | memcpy(wacom->wacom_wac.data, raw_data, size); |
3bea733a | 60 | |
29b47391 BT |
61 | wacom_wac_irq(&wacom->wacom_wac, size); |
62 | ||
63 | return 0; | |
3bea733a PC |
64 | } |
65 | ||
3bea733a PC |
66 | static int wacom_open(struct input_dev *dev) |
67 | { | |
7791bdae | 68 | struct wacom *wacom = input_get_drvdata(dev); |
29b47391 | 69 | int retval; |
f6cd3783 DT |
70 | |
71 | mutex_lock(&wacom->lock); | |
29b47391 | 72 | retval = hid_hw_open(wacom->hdev); |
e7224094 | 73 | mutex_unlock(&wacom->lock); |
29b47391 | 74 | |
f6cd3783 | 75 | return retval; |
3bea733a PC |
76 | } |
77 | ||
78 | static void wacom_close(struct input_dev *dev) | |
79 | { | |
7791bdae | 80 | struct wacom *wacom = input_get_drvdata(dev); |
3bea733a | 81 | |
e7224094 | 82 | mutex_lock(&wacom->lock); |
29b47391 | 83 | hid_hw_close(wacom->hdev); |
e7224094 | 84 | mutex_unlock(&wacom->lock); |
3bea733a PC |
85 | } |
86 | ||
115d5e12 JG |
87 | /* |
88 | * Calculate the resolution of the X or Y axis, given appropriate HID data. | |
89 | * This function is little more than hidinput_calc_abs_res stripped down. | |
90 | */ | |
91 | static int wacom_calc_hid_res(int logical_extents, int physical_extents, | |
c669fb2b | 92 | unsigned unit, int exponent) |
115d5e12 | 93 | { |
c669fb2b BT |
94 | int prev; |
95 | int unit_exponent = exponent; | |
115d5e12 JG |
96 | |
97 | /* Check if the extents are sane */ | |
98 | if (logical_extents <= 0 || physical_extents <= 0) | |
99 | return 0; | |
100 | ||
115d5e12 JG |
101 | /* Convert physical_extents to millimeters */ |
102 | if (unit == 0x11) { /* If centimeters */ | |
103 | unit_exponent += 1; | |
104 | } else if (unit == 0x13) { /* If inches */ | |
105 | prev = physical_extents; | |
106 | physical_extents *= 254; | |
107 | if (physical_extents < prev) | |
108 | return 0; | |
109 | unit_exponent -= 1; | |
110 | } else { | |
111 | return 0; | |
112 | } | |
113 | ||
114 | /* Apply negative unit exponent */ | |
115 | for (; unit_exponent < 0; unit_exponent++) { | |
116 | prev = logical_extents; | |
117 | logical_extents *= 10; | |
118 | if (logical_extents < prev) | |
119 | return 0; | |
120 | } | |
121 | /* Apply positive unit exponent */ | |
122 | for (; unit_exponent > 0; unit_exponent--) { | |
123 | prev = physical_extents; | |
124 | physical_extents *= 10; | |
125 | if (physical_extents < prev) | |
126 | return 0; | |
127 | } | |
128 | ||
129 | /* Calculate resolution */ | |
130 | return logical_extents / physical_extents; | |
131 | } | |
132 | ||
c669fb2b BT |
133 | static void wacom_feature_mapping(struct hid_device *hdev, |
134 | struct hid_field *field, struct hid_usage *usage) | |
f393ee2b | 135 | { |
c669fb2b BT |
136 | struct wacom *wacom = hid_get_drvdata(hdev); |
137 | struct wacom_features *features = &wacom->wacom_wac.features; | |
f393ee2b | 138 | |
c669fb2b BT |
139 | switch (usage->hid) { |
140 | case HID_DG_CONTACTMAX: | |
141 | /* leave touch_max as is if predefined */ | |
142 | if (!features->touch_max) | |
143 | features->touch_max = field->value[0]; | |
144 | break; | |
f393ee2b PC |
145 | } |
146 | } | |
147 | ||
428f8588 CB |
148 | /* |
149 | * Interface Descriptor of wacom devices can be incomplete and | |
150 | * inconsistent so wacom_features table is used to store stylus | |
151 | * device's packet lengths, various maximum values, and tablet | |
152 | * resolution based on product ID's. | |
153 | * | |
154 | * For devices that contain 2 interfaces, wacom_features table is | |
155 | * inaccurate for the touch interface. Since the Interface Descriptor | |
156 | * for touch interfaces has pretty complete data, this function exists | |
157 | * to query tablet for this missing information instead of hard coding in | |
158 | * an additional table. | |
159 | * | |
160 | * A typical Interface Descriptor for a stylus will contain a | |
161 | * boot mouse application collection that is not of interest and this | |
162 | * function will ignore it. | |
163 | * | |
164 | * It also contains a digitizer application collection that also is not | |
165 | * of interest since any information it contains would be duplicate | |
166 | * of what is in wacom_features. Usually it defines a report of an array | |
167 | * of bytes that could be used as max length of the stylus packet returned. | |
168 | * If it happens to define a Digitizer-Stylus Physical Collection then | |
169 | * the X and Y logical values contain valid data but it is ignored. | |
170 | * | |
171 | * A typical Interface Descriptor for a touch interface will contain a | |
172 | * Digitizer-Finger Physical Collection which will define both logical | |
173 | * X/Y maximum as well as the physical size of tablet. Since touch | |
174 | * interfaces haven't supported pressure or distance, this is enough | |
175 | * information to override invalid values in the wacom_features table. | |
4134361a | 176 | * |
c669fb2b BT |
177 | * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful |
178 | * data. We deal with them after returning from this function. | |
428f8588 | 179 | */ |
c669fb2b BT |
180 | static void wacom_usage_mapping(struct hid_device *hdev, |
181 | struct hid_field *field, struct hid_usage *usage) | |
545f4e99 | 182 | { |
c669fb2b BT |
183 | struct wacom *wacom = hid_get_drvdata(hdev); |
184 | struct wacom_features *features = &wacom->wacom_wac.features; | |
185 | bool finger = (field->logical == HID_DG_FINGER) || | |
186 | (field->physical == HID_DG_FINGER); | |
187 | bool pen = (field->logical == HID_DG_STYLUS) || | |
188 | (field->physical == HID_DG_STYLUS); | |
e9fc413f | 189 | |
c669fb2b BT |
190 | /* |
191 | * Requiring Stylus Usage will ignore boot mouse | |
192 | * X/Y values and some cases of invalid Digitizer X/Y | |
193 | * values commonly reported. | |
194 | */ | |
195 | if (!pen && !finger) | |
196 | return; | |
197 | ||
198 | if (finger && !features->touch_max) | |
199 | /* touch device at least supports one touch point */ | |
200 | features->touch_max = 1; | |
201 | ||
202 | switch (usage->hid) { | |
203 | case HID_GD_X: | |
204 | features->x_max = field->logical_maximum; | |
205 | if (finger) { | |
206 | features->device_type = BTN_TOOL_FINGER; | |
207 | features->x_phy = field->physical_maximum; | |
208 | if (features->type != BAMBOO_PT) { | |
209 | features->unit = field->unit; | |
210 | features->unitExpo = field->unit_exponent; | |
545f4e99 | 211 | } |
c669fb2b BT |
212 | } else { |
213 | features->device_type = BTN_TOOL_PEN; | |
214 | } | |
215 | break; | |
216 | case HID_GD_Y: | |
217 | features->y_max = field->logical_maximum; | |
218 | if (finger) { | |
219 | features->y_phy = field->physical_maximum; | |
220 | if (features->type != BAMBOO_PT) { | |
221 | features->unit = field->unit; | |
222 | features->unitExpo = field->unit_exponent; | |
223 | } | |
224 | } | |
225 | break; | |
226 | case HID_DG_TIPPRESSURE: | |
227 | if (pen) | |
228 | features->pressure_max = field->logical_maximum; | |
229 | break; | |
230 | } | |
231 | } | |
4134361a | 232 | |
c669fb2b BT |
233 | static void wacom_parse_hid(struct hid_device *hdev, |
234 | struct wacom_features *features) | |
235 | { | |
236 | struct hid_report_enum *rep_enum; | |
237 | struct hid_report *hreport; | |
238 | int i, j; | |
239 | ||
240 | /* check features first */ | |
241 | rep_enum = &hdev->report_enum[HID_FEATURE_REPORT]; | |
242 | list_for_each_entry(hreport, &rep_enum->report_list, list) { | |
243 | for (i = 0; i < hreport->maxfield; i++) { | |
244 | /* Ignore if report count is out of bounds. */ | |
245 | if (hreport->field[i]->report_count < 1) | |
246 | continue; | |
247 | ||
248 | for (j = 0; j < hreport->field[i]->maxusage; j++) { | |
249 | wacom_feature_mapping(hdev, hreport->field[i], | |
250 | hreport->field[i]->usage + j); | |
4134361a | 251 | } |
545f4e99 PC |
252 | } |
253 | } | |
254 | ||
c669fb2b BT |
255 | /* now check the input usages */ |
256 | rep_enum = &hdev->report_enum[HID_INPUT_REPORT]; | |
257 | list_for_each_entry(hreport, &rep_enum->report_list, list) { | |
258 | ||
259 | if (!hreport->maxfield) | |
260 | continue; | |
261 | ||
262 | for (i = 0; i < hreport->maxfield; i++) | |
263 | for (j = 0; j < hreport->field[i]->maxusage; j++) | |
264 | wacom_usage_mapping(hdev, hreport->field[i], | |
265 | hreport->field[i]->usage + j); | |
266 | } | |
545f4e99 PC |
267 | } |
268 | ||
27b20a9d BT |
269 | static int wacom_set_device_mode(struct hid_device *hdev, int report_id, |
270 | int length, int mode) | |
3b7307c2 DT |
271 | { |
272 | unsigned char *rep_data; | |
fe494bc2 | 273 | int error = -ENOMEM, limit = 0; |
3b7307c2 | 274 | |
fe494bc2 | 275 | rep_data = kzalloc(length, GFP_KERNEL); |
3b7307c2 | 276 | if (!rep_data) |
ec67bbed PC |
277 | return error; |
278 | ||
fe494bc2 | 279 | do { |
9937c026 CB |
280 | rep_data[0] = report_id; |
281 | rep_data[1] = mode; | |
282 | ||
27b20a9d | 283 | error = wacom_set_report(hdev, HID_FEATURE_REPORT, |
fe494bc2 | 284 | report_id, rep_data, length, 1); |
3cb83157 | 285 | if (error >= 0) |
27b20a9d | 286 | error = wacom_get_report(hdev, HID_FEATURE_REPORT, |
3cb83157 | 287 | report_id, rep_data, length, 1); |
fe494bc2 JG |
288 | } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES); |
289 | ||
290 | kfree(rep_data); | |
291 | ||
292 | return error < 0 ? error : 0; | |
293 | } | |
294 | ||
295 | /* | |
296 | * Switch the tablet into its most-capable mode. Wacom tablets are | |
297 | * typically configured to power-up in a mode which sends mouse-like | |
298 | * reports to the OS. To get absolute position, pressure data, etc. | |
299 | * from the tablet, it is necessary to switch the tablet out of this | |
300 | * mode and into one which sends the full range of tablet data. | |
301 | */ | |
27b20a9d BT |
302 | static int wacom_query_tablet_data(struct hid_device *hdev, |
303 | struct wacom_features *features) | |
fe494bc2 | 304 | { |
1963518b | 305 | if (features->device_type == BTN_TOOL_FINGER) { |
ea2e6024 | 306 | if (features->type > TABLETPC) { |
fe494bc2 | 307 | /* MT Tablet PC touch */ |
27b20a9d | 308 | return wacom_set_device_mode(hdev, 3, 4, 4); |
fe494bc2 | 309 | } |
36d3c510 | 310 | else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) { |
27b20a9d | 311 | return wacom_set_device_mode(hdev, 18, 3, 2); |
b1e4279e | 312 | } |
fe494bc2 JG |
313 | } else if (features->device_type == BTN_TOOL_PEN) { |
314 | if (features->type <= BAMBOO_PT && features->type != WIRELESS) { | |
27b20a9d | 315 | return wacom_set_device_mode(hdev, 2, 2, 2); |
1963518b | 316 | } |
ec67bbed | 317 | } |
3b7307c2 | 318 | |
fe494bc2 | 319 | return 0; |
3b7307c2 DT |
320 | } |
321 | ||
c669fb2b | 322 | static void wacom_retrieve_hid_descriptor(struct hid_device *hdev, |
1963518b | 323 | struct wacom_features *features) |
ec67bbed | 324 | { |
27b20a9d BT |
325 | struct wacom *wacom = hid_get_drvdata(hdev); |
326 | struct usb_interface *intf = wacom->intf; | |
ec67bbed | 327 | |
fed87e65 | 328 | /* default features */ |
ec67bbed | 329 | features->device_type = BTN_TOOL_PEN; |
fed87e65 HR |
330 | features->x_fuzz = 4; |
331 | features->y_fuzz = 4; | |
332 | features->pressure_fuzz = 0; | |
333 | features->distance_fuzz = 0; | |
ec67bbed | 334 | |
d3825d51 CB |
335 | /* |
336 | * The wireless device HID is basic and layout conflicts with | |
337 | * other tablets (monitor and touch interface can look like pen). | |
338 | * Skip the query for this type and modify defaults based on | |
339 | * interface number. | |
340 | */ | |
341 | if (features->type == WIRELESS) { | |
342 | if (intf->cur_altsetting->desc.bInterfaceNumber == 0) { | |
343 | features->device_type = 0; | |
344 | } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) { | |
adad004e | 345 | features->device_type = BTN_TOOL_FINGER; |
d3825d51 CB |
346 | features->pktlen = WACOM_PKGLEN_BBTOUCH3; |
347 | } | |
348 | } | |
349 | ||
1963518b | 350 | /* only devices that support touch need to retrieve the info */ |
c669fb2b BT |
351 | if (features->type < BAMBOO_PT) |
352 | return; | |
ec67bbed | 353 | |
c669fb2b | 354 | wacom_parse_hid(hdev, features); |
ec67bbed PC |
355 | } |
356 | ||
4451e088 | 357 | struct wacom_hdev_data { |
4492efff PC |
358 | struct list_head list; |
359 | struct kref kref; | |
4451e088 | 360 | struct hid_device *dev; |
4492efff PC |
361 | struct wacom_shared shared; |
362 | }; | |
363 | ||
364 | static LIST_HEAD(wacom_udev_list); | |
365 | static DEFINE_MUTEX(wacom_udev_list_lock); | |
366 | ||
4451e088 BT |
367 | static bool wacom_are_sibling(struct hid_device *hdev, |
368 | struct hid_device *sibling) | |
aea2bf6a | 369 | { |
4451e088 BT |
370 | struct wacom *wacom = hid_get_drvdata(hdev); |
371 | struct wacom_features *features = &wacom->wacom_wac.features; | |
372 | int vid = features->oVid; | |
373 | int pid = features->oPid; | |
374 | int n1,n2; | |
375 | ||
376 | if (vid == 0 && pid == 0) { | |
377 | vid = hdev->vendor; | |
378 | pid = hdev->product; | |
379 | } | |
aea2bf6a | 380 | |
4451e088 BT |
381 | if (vid != sibling->vendor || pid != sibling->product) |
382 | return false; | |
aea2bf6a | 383 | |
4451e088 BT |
384 | /* Compare the physical path. */ |
385 | n1 = strrchr(hdev->phys, '.') - hdev->phys; | |
386 | n2 = strrchr(sibling->phys, '.') - sibling->phys; | |
387 | if (n1 != n2 || n1 <= 0 || n2 <= 0) | |
388 | return false; | |
aea2bf6a | 389 | |
4451e088 | 390 | return !strncmp(hdev->phys, sibling->phys, n1); |
aea2bf6a JG |
391 | } |
392 | ||
4451e088 | 393 | static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev) |
4492efff | 394 | { |
4451e088 | 395 | struct wacom_hdev_data *data; |
4492efff PC |
396 | |
397 | list_for_each_entry(data, &wacom_udev_list, list) { | |
4451e088 | 398 | if (wacom_are_sibling(hdev, data->dev)) { |
4492efff PC |
399 | kref_get(&data->kref); |
400 | return data; | |
401 | } | |
402 | } | |
403 | ||
404 | return NULL; | |
405 | } | |
406 | ||
4451e088 | 407 | static int wacom_add_shared_data(struct hid_device *hdev) |
4492efff | 408 | { |
4451e088 BT |
409 | struct wacom *wacom = hid_get_drvdata(hdev); |
410 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
411 | struct wacom_hdev_data *data; | |
4492efff PC |
412 | int retval = 0; |
413 | ||
414 | mutex_lock(&wacom_udev_list_lock); | |
415 | ||
4451e088 | 416 | data = wacom_get_hdev_data(hdev); |
4492efff | 417 | if (!data) { |
4451e088 | 418 | data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL); |
4492efff PC |
419 | if (!data) { |
420 | retval = -ENOMEM; | |
421 | goto out; | |
422 | } | |
423 | ||
424 | kref_init(&data->kref); | |
4451e088 | 425 | data->dev = hdev; |
4492efff PC |
426 | list_add_tail(&data->list, &wacom_udev_list); |
427 | } | |
428 | ||
4451e088 | 429 | wacom_wac->shared = &data->shared; |
4492efff PC |
430 | |
431 | out: | |
432 | mutex_unlock(&wacom_udev_list_lock); | |
433 | return retval; | |
434 | } | |
435 | ||
436 | static void wacom_release_shared_data(struct kref *kref) | |
437 | { | |
4451e088 BT |
438 | struct wacom_hdev_data *data = |
439 | container_of(kref, struct wacom_hdev_data, kref); | |
4492efff PC |
440 | |
441 | mutex_lock(&wacom_udev_list_lock); | |
442 | list_del(&data->list); | |
443 | mutex_unlock(&wacom_udev_list_lock); | |
444 | ||
445 | kfree(data); | |
446 | } | |
447 | ||
448 | static void wacom_remove_shared_data(struct wacom_wac *wacom) | |
449 | { | |
4451e088 | 450 | struct wacom_hdev_data *data; |
4492efff PC |
451 | |
452 | if (wacom->shared) { | |
4451e088 | 453 | data = container_of(wacom->shared, struct wacom_hdev_data, shared); |
4492efff PC |
454 | kref_put(&data->kref, wacom_release_shared_data); |
455 | wacom->shared = NULL; | |
456 | } | |
457 | } | |
458 | ||
5d7e7d47 EH |
459 | static int wacom_led_control(struct wacom *wacom) |
460 | { | |
461 | unsigned char *buf; | |
9b5b95dd | 462 | int retval; |
5d7e7d47 EH |
463 | |
464 | buf = kzalloc(9, GFP_KERNEL); | |
465 | if (!buf) | |
466 | return -ENOMEM; | |
467 | ||
9b5b95dd | 468 | if (wacom->wacom_wac.features.type >= INTUOS5S && |
9a35c411 | 469 | wacom->wacom_wac.features.type <= INTUOSPL) { |
9b5b95dd JG |
470 | /* |
471 | * Touch Ring and crop mark LED luminance may take on | |
472 | * one of four values: | |
473 | * 0 = Low; 1 = Medium; 2 = High; 3 = Off | |
474 | */ | |
475 | int ring_led = wacom->led.select[0] & 0x03; | |
476 | int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03; | |
477 | int crop_lum = 0; | |
478 | ||
479 | buf[0] = WAC_CMD_LED_CONTROL; | |
480 | buf[1] = (crop_lum << 4) | (ring_lum << 2) | (ring_led); | |
481 | } | |
482 | else { | |
483 | int led = wacom->led.select[0] | 0x4; | |
484 | ||
485 | if (wacom->wacom_wac.features.type == WACOM_21UX2 || | |
486 | wacom->wacom_wac.features.type == WACOM_24HD) | |
487 | led |= (wacom->led.select[1] << 4) | 0x40; | |
488 | ||
489 | buf[0] = WAC_CMD_LED_CONTROL; | |
490 | buf[1] = led; | |
491 | buf[2] = wacom->led.llv; | |
492 | buf[3] = wacom->led.hlv; | |
493 | buf[4] = wacom->led.img_lum; | |
494 | } | |
5d7e7d47 | 495 | |
27b20a9d BT |
496 | retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, |
497 | WAC_CMD_LED_CONTROL, buf, 9, WAC_CMD_RETRIES); | |
5d7e7d47 EH |
498 | kfree(buf); |
499 | ||
500 | return retval; | |
501 | } | |
502 | ||
503 | static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img) | |
504 | { | |
505 | unsigned char *buf; | |
506 | int i, retval; | |
507 | ||
508 | buf = kzalloc(259, GFP_KERNEL); | |
509 | if (!buf) | |
510 | return -ENOMEM; | |
511 | ||
512 | /* Send 'start' command */ | |
513 | buf[0] = WAC_CMD_ICON_START; | |
514 | buf[1] = 1; | |
27b20a9d BT |
515 | retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, |
516 | WAC_CMD_ICON_START, buf, 2, WAC_CMD_RETRIES); | |
5d7e7d47 EH |
517 | if (retval < 0) |
518 | goto out; | |
519 | ||
520 | buf[0] = WAC_CMD_ICON_XFER; | |
521 | buf[1] = button_id & 0x07; | |
522 | for (i = 0; i < 4; i++) { | |
523 | buf[2] = i; | |
524 | memcpy(buf + 3, img + i * 256, 256); | |
525 | ||
27b20a9d BT |
526 | retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, |
527 | WAC_CMD_ICON_XFER, | |
5d7e7d47 EH |
528 | buf, 259, WAC_CMD_RETRIES); |
529 | if (retval < 0) | |
530 | break; | |
531 | } | |
532 | ||
533 | /* Send 'stop' */ | |
534 | buf[0] = WAC_CMD_ICON_START; | |
535 | buf[1] = 0; | |
27b20a9d | 536 | wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, WAC_CMD_ICON_START, |
5d7e7d47 EH |
537 | buf, 2, WAC_CMD_RETRIES); |
538 | ||
539 | out: | |
540 | kfree(buf); | |
541 | return retval; | |
542 | } | |
543 | ||
09e7d941 | 544 | static ssize_t wacom_led_select_store(struct device *dev, int set_id, |
5d7e7d47 EH |
545 | const char *buf, size_t count) |
546 | { | |
c31a408f | 547 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
29b47391 | 548 | struct wacom *wacom = hid_get_drvdata(hdev); |
5d7e7d47 EH |
549 | unsigned int id; |
550 | int err; | |
551 | ||
552 | err = kstrtouint(buf, 10, &id); | |
553 | if (err) | |
554 | return err; | |
555 | ||
556 | mutex_lock(&wacom->lock); | |
557 | ||
09e7d941 | 558 | wacom->led.select[set_id] = id & 0x3; |
5d7e7d47 EH |
559 | err = wacom_led_control(wacom); |
560 | ||
561 | mutex_unlock(&wacom->lock); | |
562 | ||
563 | return err < 0 ? err : count; | |
564 | } | |
565 | ||
09e7d941 PC |
566 | #define DEVICE_LED_SELECT_ATTR(SET_ID) \ |
567 | static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \ | |
568 | struct device_attribute *attr, const char *buf, size_t count) \ | |
569 | { \ | |
570 | return wacom_led_select_store(dev, SET_ID, buf, count); \ | |
571 | } \ | |
04c59abd PC |
572 | static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \ |
573 | struct device_attribute *attr, char *buf) \ | |
574 | { \ | |
c31a408f | 575 | struct hid_device *hdev = container_of(dev, struct hid_device, dev);\ |
29b47391 | 576 | struct wacom *wacom = hid_get_drvdata(hdev); \ |
04c59abd PC |
577 | return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \ |
578 | } \ | |
579 | static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \ | |
580 | wacom_led##SET_ID##_select_show, \ | |
09e7d941 PC |
581 | wacom_led##SET_ID##_select_store) |
582 | ||
583 | DEVICE_LED_SELECT_ATTR(0); | |
584 | DEVICE_LED_SELECT_ATTR(1); | |
5d7e7d47 EH |
585 | |
586 | static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest, | |
587 | const char *buf, size_t count) | |
588 | { | |
589 | unsigned int value; | |
590 | int err; | |
591 | ||
592 | err = kstrtouint(buf, 10, &value); | |
593 | if (err) | |
594 | return err; | |
595 | ||
596 | mutex_lock(&wacom->lock); | |
597 | ||
598 | *dest = value & 0x7f; | |
599 | err = wacom_led_control(wacom); | |
600 | ||
601 | mutex_unlock(&wacom->lock); | |
602 | ||
603 | return err < 0 ? err : count; | |
604 | } | |
605 | ||
606 | #define DEVICE_LUMINANCE_ATTR(name, field) \ | |
607 | static ssize_t wacom_##name##_luminance_store(struct device *dev, \ | |
608 | struct device_attribute *attr, const char *buf, size_t count) \ | |
609 | { \ | |
c31a408f | 610 | struct hid_device *hdev = container_of(dev, struct hid_device, dev);\ |
29b47391 | 611 | struct wacom *wacom = hid_get_drvdata(hdev); \ |
5d7e7d47 EH |
612 | \ |
613 | return wacom_luminance_store(wacom, &wacom->led.field, \ | |
614 | buf, count); \ | |
615 | } \ | |
616 | static DEVICE_ATTR(name##_luminance, S_IWUSR, \ | |
617 | NULL, wacom_##name##_luminance_store) | |
618 | ||
619 | DEVICE_LUMINANCE_ATTR(status0, llv); | |
620 | DEVICE_LUMINANCE_ATTR(status1, hlv); | |
621 | DEVICE_LUMINANCE_ATTR(buttons, img_lum); | |
622 | ||
623 | static ssize_t wacom_button_image_store(struct device *dev, int button_id, | |
624 | const char *buf, size_t count) | |
625 | { | |
c31a408f | 626 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
29b47391 | 627 | struct wacom *wacom = hid_get_drvdata(hdev); |
5d7e7d47 EH |
628 | int err; |
629 | ||
630 | if (count != 1024) | |
631 | return -EINVAL; | |
632 | ||
633 | mutex_lock(&wacom->lock); | |
634 | ||
635 | err = wacom_led_putimage(wacom, button_id, buf); | |
636 | ||
637 | mutex_unlock(&wacom->lock); | |
638 | ||
639 | return err < 0 ? err : count; | |
640 | } | |
641 | ||
642 | #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \ | |
643 | static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \ | |
644 | struct device_attribute *attr, const char *buf, size_t count) \ | |
645 | { \ | |
646 | return wacom_button_image_store(dev, BUTTON_ID, buf, count); \ | |
647 | } \ | |
648 | static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \ | |
649 | NULL, wacom_btnimg##BUTTON_ID##_store) | |
650 | ||
651 | DEVICE_BTNIMG_ATTR(0); | |
652 | DEVICE_BTNIMG_ATTR(1); | |
653 | DEVICE_BTNIMG_ATTR(2); | |
654 | DEVICE_BTNIMG_ATTR(3); | |
655 | DEVICE_BTNIMG_ATTR(4); | |
656 | DEVICE_BTNIMG_ATTR(5); | |
657 | DEVICE_BTNIMG_ATTR(6); | |
658 | DEVICE_BTNIMG_ATTR(7); | |
659 | ||
09e7d941 PC |
660 | static struct attribute *cintiq_led_attrs[] = { |
661 | &dev_attr_status_led0_select.attr, | |
662 | &dev_attr_status_led1_select.attr, | |
663 | NULL | |
664 | }; | |
665 | ||
666 | static struct attribute_group cintiq_led_attr_group = { | |
667 | .name = "wacom_led", | |
668 | .attrs = cintiq_led_attrs, | |
669 | }; | |
670 | ||
671 | static struct attribute *intuos4_led_attrs[] = { | |
5d7e7d47 EH |
672 | &dev_attr_status0_luminance.attr, |
673 | &dev_attr_status1_luminance.attr, | |
09e7d941 | 674 | &dev_attr_status_led0_select.attr, |
5d7e7d47 EH |
675 | &dev_attr_buttons_luminance.attr, |
676 | &dev_attr_button0_rawimg.attr, | |
677 | &dev_attr_button1_rawimg.attr, | |
678 | &dev_attr_button2_rawimg.attr, | |
679 | &dev_attr_button3_rawimg.attr, | |
680 | &dev_attr_button4_rawimg.attr, | |
681 | &dev_attr_button5_rawimg.attr, | |
682 | &dev_attr_button6_rawimg.attr, | |
683 | &dev_attr_button7_rawimg.attr, | |
684 | NULL | |
685 | }; | |
686 | ||
09e7d941 | 687 | static struct attribute_group intuos4_led_attr_group = { |
5d7e7d47 | 688 | .name = "wacom_led", |
09e7d941 | 689 | .attrs = intuos4_led_attrs, |
5d7e7d47 EH |
690 | }; |
691 | ||
9b5b95dd JG |
692 | static struct attribute *intuos5_led_attrs[] = { |
693 | &dev_attr_status0_luminance.attr, | |
694 | &dev_attr_status_led0_select.attr, | |
695 | NULL | |
696 | }; | |
697 | ||
698 | static struct attribute_group intuos5_led_attr_group = { | |
699 | .name = "wacom_led", | |
700 | .attrs = intuos5_led_attrs, | |
701 | }; | |
702 | ||
5d7e7d47 EH |
703 | static int wacom_initialize_leds(struct wacom *wacom) |
704 | { | |
705 | int error; | |
706 | ||
09e7d941 PC |
707 | /* Initialize default values */ |
708 | switch (wacom->wacom_wac.features.type) { | |
a19fc986 | 709 | case INTUOS4S: |
09e7d941 PC |
710 | case INTUOS4: |
711 | case INTUOS4L: | |
712 | wacom->led.select[0] = 0; | |
713 | wacom->led.select[1] = 0; | |
f4fa9a6d | 714 | wacom->led.llv = 10; |
5d7e7d47 EH |
715 | wacom->led.hlv = 20; |
716 | wacom->led.img_lum = 10; | |
c31a408f | 717 | error = sysfs_create_group(&wacom->hdev->dev.kobj, |
09e7d941 PC |
718 | &intuos4_led_attr_group); |
719 | break; | |
720 | ||
246835fc | 721 | case WACOM_24HD: |
09e7d941 PC |
722 | case WACOM_21UX2: |
723 | wacom->led.select[0] = 0; | |
724 | wacom->led.select[1] = 0; | |
725 | wacom->led.llv = 0; | |
726 | wacom->led.hlv = 0; | |
727 | wacom->led.img_lum = 0; | |
5d7e7d47 | 728 | |
c31a408f | 729 | error = sysfs_create_group(&wacom->hdev->dev.kobj, |
09e7d941 PC |
730 | &cintiq_led_attr_group); |
731 | break; | |
732 | ||
9b5b95dd JG |
733 | case INTUOS5S: |
734 | case INTUOS5: | |
735 | case INTUOS5L: | |
9a35c411 PC |
736 | case INTUOSPS: |
737 | case INTUOSPM: | |
738 | case INTUOSPL: | |
c2b0c273 PC |
739 | if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) { |
740 | wacom->led.select[0] = 0; | |
741 | wacom->led.select[1] = 0; | |
742 | wacom->led.llv = 32; | |
743 | wacom->led.hlv = 0; | |
744 | wacom->led.img_lum = 0; | |
745 | ||
c31a408f | 746 | error = sysfs_create_group(&wacom->hdev->dev.kobj, |
c2b0c273 PC |
747 | &intuos5_led_attr_group); |
748 | } else | |
749 | return 0; | |
9b5b95dd JG |
750 | break; |
751 | ||
09e7d941 PC |
752 | default: |
753 | return 0; | |
5d7e7d47 EH |
754 | } |
755 | ||
09e7d941 | 756 | if (error) { |
c31a408f | 757 | hid_err(wacom->hdev, |
09e7d941 PC |
758 | "cannot create sysfs group err: %d\n", error); |
759 | return error; | |
760 | } | |
761 | wacom_led_control(wacom); | |
762 | ||
5d7e7d47 EH |
763 | return 0; |
764 | } | |
765 | ||
766 | static void wacom_destroy_leds(struct wacom *wacom) | |
767 | { | |
09e7d941 | 768 | switch (wacom->wacom_wac.features.type) { |
a19fc986 | 769 | case INTUOS4S: |
09e7d941 PC |
770 | case INTUOS4: |
771 | case INTUOS4L: | |
c31a408f | 772 | sysfs_remove_group(&wacom->hdev->dev.kobj, |
09e7d941 PC |
773 | &intuos4_led_attr_group); |
774 | break; | |
775 | ||
246835fc | 776 | case WACOM_24HD: |
09e7d941 | 777 | case WACOM_21UX2: |
c31a408f | 778 | sysfs_remove_group(&wacom->hdev->dev.kobj, |
09e7d941 PC |
779 | &cintiq_led_attr_group); |
780 | break; | |
9b5b95dd JG |
781 | |
782 | case INTUOS5S: | |
783 | case INTUOS5: | |
784 | case INTUOS5L: | |
9a35c411 PC |
785 | case INTUOSPS: |
786 | case INTUOSPM: | |
787 | case INTUOSPL: | |
c2b0c273 | 788 | if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) |
c31a408f | 789 | sysfs_remove_group(&wacom->hdev->dev.kobj, |
c2b0c273 | 790 | &intuos5_led_attr_group); |
9b5b95dd | 791 | break; |
5d7e7d47 EH |
792 | } |
793 | } | |
794 | ||
a1d552cc | 795 | static enum power_supply_property wacom_battery_props[] = { |
6e2a6e80 | 796 | POWER_SUPPLY_PROP_SCOPE, |
a1d552cc CB |
797 | POWER_SUPPLY_PROP_CAPACITY |
798 | }; | |
799 | ||
800 | static int wacom_battery_get_property(struct power_supply *psy, | |
801 | enum power_supply_property psp, | |
802 | union power_supply_propval *val) | |
803 | { | |
804 | struct wacom *wacom = container_of(psy, struct wacom, battery); | |
805 | int ret = 0; | |
806 | ||
807 | switch (psp) { | |
6e2a6e80 BN |
808 | case POWER_SUPPLY_PROP_SCOPE: |
809 | val->intval = POWER_SUPPLY_SCOPE_DEVICE; | |
810 | break; | |
a1d552cc CB |
811 | case POWER_SUPPLY_PROP_CAPACITY: |
812 | val->intval = | |
813 | wacom->wacom_wac.battery_capacity * 100 / 31; | |
814 | break; | |
815 | default: | |
816 | ret = -EINVAL; | |
817 | break; | |
818 | } | |
819 | ||
820 | return ret; | |
821 | } | |
822 | ||
823 | static int wacom_initialize_battery(struct wacom *wacom) | |
824 | { | |
825 | int error = 0; | |
826 | ||
827 | if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) { | |
828 | wacom->battery.properties = wacom_battery_props; | |
829 | wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props); | |
830 | wacom->battery.get_property = wacom_battery_get_property; | |
831 | wacom->battery.name = "wacom_battery"; | |
832 | wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY; | |
833 | wacom->battery.use_for_apm = 0; | |
834 | ||
dd3181a7 | 835 | error = power_supply_register(&wacom->hdev->dev, |
a1d552cc | 836 | &wacom->battery); |
b7af2bb8 CB |
837 | |
838 | if (!error) | |
839 | power_supply_powers(&wacom->battery, | |
dd3181a7 | 840 | &wacom->hdev->dev); |
a1d552cc CB |
841 | } |
842 | ||
843 | return error; | |
844 | } | |
845 | ||
846 | static void wacom_destroy_battery(struct wacom *wacom) | |
847 | { | |
b7af2bb8 CB |
848 | if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR && |
849 | wacom->battery.dev) { | |
a1d552cc | 850 | power_supply_unregister(&wacom->battery); |
b7af2bb8 CB |
851 | wacom->battery.dev = NULL; |
852 | } | |
a1d552cc CB |
853 | } |
854 | ||
d2d13f18 | 855 | static struct input_dev *wacom_allocate_input(struct wacom *wacom) |
3aac0ef1 CB |
856 | { |
857 | struct input_dev *input_dev; | |
b6c79f2c | 858 | struct hid_device *hdev = wacom->hdev; |
3aac0ef1 | 859 | struct wacom_wac *wacom_wac = &(wacom->wacom_wac); |
3aac0ef1 CB |
860 | |
861 | input_dev = input_allocate_device(); | |
d2d13f18 BT |
862 | if (!input_dev) |
863 | return NULL; | |
3aac0ef1 CB |
864 | |
865 | input_dev->name = wacom_wac->name; | |
b6c79f2c BT |
866 | input_dev->phys = hdev->phys; |
867 | input_dev->dev.parent = &hdev->dev; | |
3aac0ef1 CB |
868 | input_dev->open = wacom_open; |
869 | input_dev->close = wacom_close; | |
b6c79f2c BT |
870 | input_dev->uniq = hdev->uniq; |
871 | input_dev->id.bustype = hdev->bus; | |
872 | input_dev->id.vendor = hdev->vendor; | |
873 | input_dev->id.product = hdev->product; | |
874 | input_dev->id.version = hdev->version; | |
3aac0ef1 CB |
875 | input_set_drvdata(input_dev, wacom); |
876 | ||
d2d13f18 BT |
877 | return input_dev; |
878 | } | |
879 | ||
008f4d9e BT |
880 | static void wacom_unregister_inputs(struct wacom *wacom) |
881 | { | |
882 | if (wacom->wacom_wac.input) | |
883 | input_unregister_device(wacom->wacom_wac.input); | |
884 | if (wacom->wacom_wac.pad_input) | |
885 | input_unregister_device(wacom->wacom_wac.pad_input); | |
886 | wacom->wacom_wac.input = NULL; | |
887 | wacom->wacom_wac.pad_input = NULL; | |
888 | } | |
889 | ||
890 | static int wacom_register_inputs(struct wacom *wacom) | |
d2d13f18 BT |
891 | { |
892 | struct input_dev *input_dev, *pad_input_dev; | |
893 | struct wacom_wac *wacom_wac = &(wacom->wacom_wac); | |
894 | int error; | |
895 | ||
896 | input_dev = wacom_allocate_input(wacom); | |
897 | pad_input_dev = wacom_allocate_input(wacom); | |
898 | if (!input_dev || !pad_input_dev) { | |
899 | error = -ENOMEM; | |
900 | goto fail1; | |
901 | } | |
902 | ||
3aac0ef1 | 903 | wacom_wac->input = input_dev; |
d2d13f18 BT |
904 | wacom_wac->pad_input = pad_input_dev; |
905 | wacom_wac->pad_input->name = wacom_wac->pad_name; | |
906 | ||
1963518b PC |
907 | error = wacom_setup_input_capabilities(input_dev, wacom_wac); |
908 | if (error) | |
d2d13f18 | 909 | goto fail2; |
3aac0ef1 CB |
910 | |
911 | error = input_register_device(input_dev); | |
1963518b PC |
912 | if (error) |
913 | goto fail2; | |
914 | ||
d2d13f18 BT |
915 | error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac); |
916 | if (error) { | |
917 | /* no pad in use on this interface */ | |
918 | input_free_device(pad_input_dev); | |
919 | wacom_wac->pad_input = NULL; | |
920 | pad_input_dev = NULL; | |
921 | } else { | |
922 | error = input_register_device(pad_input_dev); | |
923 | if (error) | |
924 | goto fail3; | |
925 | } | |
926 | ||
1963518b | 927 | return 0; |
3aac0ef1 | 928 | |
d2d13f18 BT |
929 | fail3: |
930 | input_unregister_device(input_dev); | |
931 | input_dev = NULL; | |
1963518b | 932 | fail2: |
1963518b | 933 | wacom_wac->input = NULL; |
d2d13f18 | 934 | wacom_wac->pad_input = NULL; |
1963518b | 935 | fail1: |
d2d13f18 BT |
936 | if (input_dev) |
937 | input_free_device(input_dev); | |
938 | if (pad_input_dev) | |
939 | input_free_device(pad_input_dev); | |
3aac0ef1 CB |
940 | return error; |
941 | } | |
942 | ||
16bf288c CB |
943 | static void wacom_wireless_work(struct work_struct *work) |
944 | { | |
945 | struct wacom *wacom = container_of(work, struct wacom, work); | |
946 | struct usb_device *usbdev = wacom->usbdev; | |
947 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; | |
29b47391 | 948 | struct hid_device *hdev1, *hdev2; |
b7af2bb8 CB |
949 | struct wacom *wacom1, *wacom2; |
950 | struct wacom_wac *wacom_wac1, *wacom_wac2; | |
951 | int error; | |
16bf288c CB |
952 | |
953 | /* | |
954 | * Regardless if this is a disconnect or a new tablet, | |
b7af2bb8 | 955 | * remove any existing input and battery devices. |
16bf288c CB |
956 | */ |
957 | ||
b7af2bb8 CB |
958 | wacom_destroy_battery(wacom); |
959 | ||
16bf288c | 960 | /* Stylus interface */ |
29b47391 BT |
961 | hdev1 = usb_get_intfdata(usbdev->config->interface[1]); |
962 | wacom1 = hid_get_drvdata(hdev1); | |
b7af2bb8 | 963 | wacom_wac1 = &(wacom1->wacom_wac); |
008f4d9e | 964 | wacom_unregister_inputs(wacom1); |
16bf288c CB |
965 | |
966 | /* Touch interface */ | |
29b47391 BT |
967 | hdev2 = usb_get_intfdata(usbdev->config->interface[2]); |
968 | wacom2 = hid_get_drvdata(hdev2); | |
b7af2bb8 | 969 | wacom_wac2 = &(wacom2->wacom_wac); |
008f4d9e | 970 | wacom_unregister_inputs(wacom2); |
16bf288c CB |
971 | |
972 | if (wacom_wac->pid == 0) { | |
e2114ce1 | 973 | hid_info(wacom->hdev, "wireless tablet disconnected\n"); |
16bf288c | 974 | } else { |
29b47391 | 975 | const struct hid_device_id *id = wacom_ids; |
16bf288c | 976 | |
e2114ce1 | 977 | hid_info(wacom->hdev, "wireless tablet connected with PID %x\n", |
eb71d1bb | 978 | wacom_wac->pid); |
16bf288c | 979 | |
29b47391 BT |
980 | while (id->bus) { |
981 | if (id->vendor == USB_VENDOR_ID_WACOM && | |
982 | id->product == wacom_wac->pid) | |
16bf288c CB |
983 | break; |
984 | id++; | |
985 | } | |
986 | ||
29b47391 | 987 | if (!id->bus) { |
e2114ce1 | 988 | hid_info(wacom->hdev, "ignoring unknown PID.\n"); |
16bf288c CB |
989 | return; |
990 | } | |
991 | ||
992 | /* Stylus interface */ | |
b7af2bb8 | 993 | wacom_wac1->features = |
29b47391 | 994 | *((struct wacom_features *)id->driver_data); |
b7af2bb8 | 995 | wacom_wac1->features.device_type = BTN_TOOL_PEN; |
57bcfce3 PC |
996 | snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen", |
997 | wacom_wac1->features.name); | |
008f4d9e BT |
998 | snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad", |
999 | wacom_wac1->features.name); | |
961794a0 PC |
1000 | wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max; |
1001 | wacom_wac1->shared->type = wacom_wac1->features.type; | |
008f4d9e | 1002 | error = wacom_register_inputs(wacom1); |
b7af2bb8 | 1003 | if (error) |
57bcfce3 | 1004 | goto fail; |
16bf288c CB |
1005 | |
1006 | /* Touch interface */ | |
b5fd2a3e PC |
1007 | if (wacom_wac1->features.touch_max || |
1008 | wacom_wac1->features.type == INTUOSHT) { | |
57bcfce3 | 1009 | wacom_wac2->features = |
29b47391 | 1010 | *((struct wacom_features *)id->driver_data); |
57bcfce3 PC |
1011 | wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3; |
1012 | wacom_wac2->features.device_type = BTN_TOOL_FINGER; | |
1013 | wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096; | |
1014 | if (wacom_wac2->features.touch_max) | |
1015 | snprintf(wacom_wac2->name, WACOM_NAME_MAX, | |
1016 | "%s (WL) Finger",wacom_wac2->features.name); | |
1017 | else | |
1018 | snprintf(wacom_wac2->name, WACOM_NAME_MAX, | |
1019 | "%s (WL) Pad",wacom_wac2->features.name); | |
008f4d9e BT |
1020 | snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX, |
1021 | "%s (WL) Pad", wacom_wac2->features.name); | |
1022 | error = wacom_register_inputs(wacom2); | |
57bcfce3 PC |
1023 | if (error) |
1024 | goto fail; | |
961794a0 PC |
1025 | |
1026 | if (wacom_wac1->features.type == INTUOSHT && | |
1027 | wacom_wac1->features.touch_max) | |
1028 | wacom_wac->shared->touch_input = wacom_wac2->input; | |
57bcfce3 | 1029 | } |
b7af2bb8 CB |
1030 | |
1031 | error = wacom_initialize_battery(wacom); | |
1032 | if (error) | |
57bcfce3 | 1033 | goto fail; |
16bf288c | 1034 | } |
b7af2bb8 CB |
1035 | |
1036 | return; | |
1037 | ||
57bcfce3 | 1038 | fail: |
008f4d9e BT |
1039 | wacom_unregister_inputs(wacom1); |
1040 | wacom_unregister_inputs(wacom2); | |
b7af2bb8 | 1041 | return; |
16bf288c CB |
1042 | } |
1043 | ||
401d7d10 PC |
1044 | /* |
1045 | * Not all devices report physical dimensions from HID. | |
1046 | * Compute the default from hardcoded logical dimension | |
1047 | * and resolution before driver overwrites them. | |
1048 | */ | |
1049 | static void wacom_set_default_phy(struct wacom_features *features) | |
1050 | { | |
1051 | if (features->x_resolution) { | |
1052 | features->x_phy = (features->x_max * 100) / | |
1053 | features->x_resolution; | |
1054 | features->y_phy = (features->y_max * 100) / | |
1055 | features->y_resolution; | |
1056 | } | |
1057 | } | |
1058 | ||
1059 | static void wacom_calculate_res(struct wacom_features *features) | |
1060 | { | |
1061 | features->x_resolution = wacom_calc_hid_res(features->x_max, | |
1062 | features->x_phy, | |
1063 | features->unit, | |
1064 | features->unitExpo); | |
1065 | features->y_resolution = wacom_calc_hid_res(features->y_max, | |
1066 | features->y_phy, | |
1067 | features->unit, | |
1068 | features->unitExpo); | |
1069 | } | |
1070 | ||
01c846f9 BT |
1071 | static int wacom_hid_report_len(struct hid_report *report) |
1072 | { | |
1073 | /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */ | |
1074 | return ((report->size - 1) >> 3) + 1 + (report->id > 0); | |
1075 | } | |
1076 | ||
1077 | static size_t wacom_compute_pktlen(struct hid_device *hdev) | |
1078 | { | |
1079 | struct hid_report_enum *report_enum; | |
1080 | struct hid_report *report; | |
1081 | size_t size = 0; | |
1082 | ||
1083 | report_enum = hdev->report_enum + HID_INPUT_REPORT; | |
1084 | ||
1085 | list_for_each_entry(report, &report_enum->report_list, list) { | |
1086 | size_t report_size = wacom_hid_report_len(report); | |
1087 | if (report_size > size) | |
1088 | size = report_size; | |
1089 | } | |
1090 | ||
1091 | return size; | |
1092 | } | |
1093 | ||
29b47391 BT |
1094 | static int wacom_probe(struct hid_device *hdev, |
1095 | const struct hid_device_id *id) | |
3bea733a | 1096 | { |
29b47391 | 1097 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
3bea733a | 1098 | struct usb_device *dev = interface_to_usbdev(intf); |
3bea733a PC |
1099 | struct wacom *wacom; |
1100 | struct wacom_wac *wacom_wac; | |
e33da8a5 | 1101 | struct wacom_features *features; |
e33da8a5 | 1102 | int error; |
3bea733a | 1103 | |
29b47391 | 1104 | if (!id->driver_data) |
b036f6fb BB |
1105 | return -EINVAL; |
1106 | ||
3bea733a | 1107 | wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL); |
f1823940 DC |
1108 | if (!wacom) |
1109 | return -ENOMEM; | |
e33da8a5 | 1110 | |
29b47391 BT |
1111 | hid_set_drvdata(hdev, wacom); |
1112 | wacom->hdev = hdev; | |
1113 | ||
ba9a3541 BT |
1114 | /* ask for the report descriptor to be loaded by HID */ |
1115 | error = hid_parse(hdev); | |
1116 | if (error) { | |
1117 | hid_err(hdev, "parse failed\n"); | |
1118 | goto fail1; | |
1119 | } | |
1120 | ||
51269fe8 | 1121 | wacom_wac = &wacom->wacom_wac; |
29b47391 | 1122 | wacom_wac->features = *((struct wacom_features *)id->driver_data); |
e33da8a5 | 1123 | features = &wacom_wac->features; |
01c846f9 | 1124 | features->pktlen = wacom_compute_pktlen(hdev); |
e33da8a5 JC |
1125 | if (features->pktlen > WACOM_PKGLEN_MAX) { |
1126 | error = -EINVAL; | |
3bea733a | 1127 | goto fail1; |
e33da8a5 | 1128 | } |
3bea733a | 1129 | |
29b47391 BT |
1130 | if (features->check_for_hid_type && features->hid_type != hdev->type) { |
1131 | error = -ENODEV; | |
3bea733a | 1132 | goto fail1; |
e33da8a5 | 1133 | } |
3bea733a | 1134 | |
3bea733a | 1135 | wacom->usbdev = dev; |
e7224094 ON |
1136 | wacom->intf = intf; |
1137 | mutex_init(&wacom->lock); | |
16bf288c | 1138 | INIT_WORK(&wacom->work, wacom_wireless_work); |
3bea733a | 1139 | |
401d7d10 PC |
1140 | /* set the default size in case we do not get them from hid */ |
1141 | wacom_set_default_phy(features); | |
1142 | ||
f393ee2b | 1143 | /* Retrieve the physical and logical size for touch devices */ |
c669fb2b | 1144 | wacom_retrieve_hid_descriptor(hdev, features); |
545f4e99 | 1145 | |
ae584ca4 JG |
1146 | /* |
1147 | * Intuos5 has no useful data about its touch interface in its | |
01c846f9 | 1148 | * HID descriptor. If this is the touch interface (PacketSize |
ae584ca4 JG |
1149 | * of WACOM_PKGLEN_BBTOUCH3), override the table values. |
1150 | */ | |
b5fd2a3e | 1151 | if (features->type >= INTUOS5S && features->type <= INTUOSHT) { |
01c846f9 | 1152 | if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) { |
ae584ca4 | 1153 | features->device_type = BTN_TOOL_FINGER; |
ae584ca4 | 1154 | |
ae584ca4 JG |
1155 | features->x_max = 4096; |
1156 | features->y_max = 4096; | |
1157 | } else { | |
1158 | features->device_type = BTN_TOOL_PEN; | |
1159 | } | |
1160 | } | |
1161 | ||
c669fb2b BT |
1162 | /* |
1163 | * Same thing for Bamboo 3rd gen. | |
1164 | */ | |
1165 | if ((features->type == BAMBOO_PT) && | |
1166 | (features->pktlen == WACOM_PKGLEN_BBTOUCH3) && | |
1167 | (features->device_type == BTN_TOOL_PEN)) { | |
1168 | features->device_type = BTN_TOOL_FINGER; | |
1169 | ||
1170 | features->x_max = 4096; | |
1171 | features->y_max = 4096; | |
1172 | } | |
1173 | ||
bc73dd39 HR |
1174 | wacom_setup_device_quirks(features); |
1175 | ||
401d7d10 PC |
1176 | /* set unit to "100th of a mm" for devices not reported by HID */ |
1177 | if (!features->unit) { | |
1178 | features->unit = 0x11; | |
c669fb2b | 1179 | features->unitExpo = -3; |
401d7d10 PC |
1180 | } |
1181 | wacom_calculate_res(features); | |
1182 | ||
49b764ae | 1183 | strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name)); |
d2d13f18 BT |
1184 | snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name), |
1185 | "%s Pad", features->name); | |
49b764ae | 1186 | |
bc73dd39 | 1187 | if (features->quirks & WACOM_QUIRK_MULTI_INPUT) { |
49b764ae | 1188 | /* Append the device type to the name */ |
57bcfce3 PC |
1189 | if (features->device_type != BTN_TOOL_FINGER) |
1190 | strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX); | |
1191 | else if (features->touch_max) | |
1192 | strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX); | |
1193 | else | |
1194 | strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX); | |
4492efff | 1195 | |
4451e088 | 1196 | error = wacom_add_shared_data(hdev); |
4492efff | 1197 | if (error) |
29b47391 | 1198 | goto fail1; |
49b764ae PC |
1199 | } |
1200 | ||
5d7e7d47 | 1201 | error = wacom_initialize_leds(wacom); |
5014186d | 1202 | if (error) |
29b47391 | 1203 | goto fail2; |
3bea733a | 1204 | |
d3825d51 | 1205 | if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) { |
008f4d9e | 1206 | error = wacom_register_inputs(wacom); |
d3825d51 | 1207 | if (error) |
29b47391 | 1208 | goto fail3; |
d3825d51 | 1209 | } |
5d7e7d47 | 1210 | |
ec67bbed | 1211 | /* Note that if query fails it is not a hard failure */ |
27b20a9d | 1212 | wacom_query_tablet_data(hdev, features); |
3bea733a | 1213 | |
29b47391 | 1214 | /* Regular HID work starts now */ |
29b47391 BT |
1215 | error = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
1216 | if (error) { | |
1217 | hid_err(hdev, "hw start failed\n"); | |
1218 | goto fail4; | |
d3825d51 | 1219 | } |
961794a0 | 1220 | |
29b47391 BT |
1221 | if (features->quirks & WACOM_QUIRK_MONITOR) |
1222 | error = hid_hw_open(hdev); | |
1223 | ||
961794a0 PC |
1224 | if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) { |
1225 | if (wacom_wac->features.device_type == BTN_TOOL_FINGER) | |
1226 | wacom_wac->shared->touch_input = wacom_wac->input; | |
1227 | } | |
1228 | ||
3bea733a PC |
1229 | return 0; |
1230 | ||
29b47391 BT |
1231 | fail4: wacom_unregister_inputs(wacom); |
1232 | fail3: wacom_destroy_leds(wacom); | |
1233 | fail2: wacom_remove_shared_data(wacom_wac); | |
3aac0ef1 | 1234 | fail1: kfree(wacom); |
29b47391 | 1235 | hid_set_drvdata(hdev, NULL); |
5014186d | 1236 | return error; |
3bea733a PC |
1237 | } |
1238 | ||
29b47391 | 1239 | static void wacom_remove(struct hid_device *hdev) |
3bea733a | 1240 | { |
29b47391 | 1241 | struct wacom *wacom = hid_get_drvdata(hdev); |
3bea733a | 1242 | |
29b47391 | 1243 | hid_hw_stop(hdev); |
e7224094 | 1244 | |
16bf288c | 1245 | cancel_work_sync(&wacom->work); |
008f4d9e | 1246 | wacom_unregister_inputs(wacom); |
a1d552cc | 1247 | wacom_destroy_battery(wacom); |
5d7e7d47 | 1248 | wacom_destroy_leds(wacom); |
51269fe8 | 1249 | wacom_remove_shared_data(&wacom->wacom_wac); |
e7224094 | 1250 | |
29b47391 BT |
1251 | hid_set_drvdata(hdev, NULL); |
1252 | kfree(wacom); | |
e7224094 ON |
1253 | } |
1254 | ||
29b47391 | 1255 | static int wacom_resume(struct hid_device *hdev) |
e7224094 | 1256 | { |
29b47391 | 1257 | struct wacom *wacom = hid_get_drvdata(hdev); |
51269fe8 | 1258 | struct wacom_features *features = &wacom->wacom_wac.features; |
e7224094 ON |
1259 | |
1260 | mutex_lock(&wacom->lock); | |
38101475 PC |
1261 | |
1262 | /* switch to wacom mode first */ | |
27b20a9d | 1263 | wacom_query_tablet_data(hdev, features); |
5d7e7d47 | 1264 | wacom_led_control(wacom); |
38101475 | 1265 | |
e7224094 ON |
1266 | mutex_unlock(&wacom->lock); |
1267 | ||
29b47391 | 1268 | return 0; |
e7224094 ON |
1269 | } |
1270 | ||
29b47391 | 1271 | static int wacom_reset_resume(struct hid_device *hdev) |
e7224094 | 1272 | { |
29b47391 | 1273 | return wacom_resume(hdev); |
3bea733a PC |
1274 | } |
1275 | ||
29b47391 | 1276 | static struct hid_driver wacom_driver = { |
3bea733a | 1277 | .name = "wacom", |
b036f6fb | 1278 | .id_table = wacom_ids, |
3bea733a | 1279 | .probe = wacom_probe, |
29b47391 BT |
1280 | .remove = wacom_remove, |
1281 | #ifdef CONFIG_PM | |
e7224094 ON |
1282 | .resume = wacom_resume, |
1283 | .reset_resume = wacom_reset_resume, | |
29b47391 BT |
1284 | #endif |
1285 | .raw_event = wacom_raw_event, | |
3bea733a | 1286 | }; |
29b47391 | 1287 | module_hid_driver(wacom_driver); |