]>
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" |
3bea733a | 16 | |
545f4e99 PC |
17 | /* defines to get HID report descriptor */ |
18 | #define HID_DEVICET_HID (USB_TYPE_CLASS | 0x01) | |
19 | #define HID_DEVICET_REPORT (USB_TYPE_CLASS | 0x02) | |
20 | #define HID_USAGE_UNDEFINED 0x00 | |
21 | #define HID_USAGE_PAGE 0x05 | |
22 | #define HID_USAGE_PAGE_DIGITIZER 0x0d | |
23 | #define HID_USAGE_PAGE_DESKTOP 0x01 | |
24 | #define HID_USAGE 0x09 | |
25 | #define HID_USAGE_X 0x30 | |
26 | #define HID_USAGE_Y 0x31 | |
27 | #define HID_USAGE_X_TILT 0x3d | |
28 | #define HID_USAGE_Y_TILT 0x3e | |
29 | #define HID_USAGE_FINGER 0x22 | |
30 | #define HID_USAGE_STYLUS 0x20 | |
31 | #define HID_COLLECTION 0xc0 | |
32 | ||
33 | enum { | |
34 | WCM_UNDEFINED = 0, | |
35 | WCM_DESKTOP, | |
36 | WCM_DIGITIZER, | |
37 | }; | |
38 | ||
39 | struct hid_descriptor { | |
40 | struct usb_descriptor_header header; | |
41 | __le16 bcdHID; | |
42 | u8 bCountryCode; | |
43 | u8 bNumDescriptors; | |
44 | u8 bDescriptorType; | |
45 | __le16 wDescriptorLength; | |
46 | } __attribute__ ((packed)); | |
47 | ||
48 | /* defines to get/set USB message */ | |
3bea733a PC |
49 | #define USB_REQ_GET_REPORT 0x01 |
50 | #define USB_REQ_SET_REPORT 0x09 | |
5d7e7d47 | 51 | |
545f4e99 | 52 | #define WAC_HID_FEATURE_REPORT 0x03 |
a417ea44 | 53 | #define WAC_MSG_RETRIES 5 |
3bea733a | 54 | |
5d7e7d47 EH |
55 | #define WAC_CMD_LED_CONTROL 0x20 |
56 | #define WAC_CMD_ICON_START 0x21 | |
57 | #define WAC_CMD_ICON_XFER 0x23 | |
58 | #define WAC_CMD_RETRIES 10 | |
59 | ||
60 | static int wacom_get_report(struct usb_interface *intf, u8 type, u8 id, | |
61 | void *buf, size_t size, unsigned int retries) | |
3bea733a | 62 | { |
5d7e7d47 EH |
63 | struct usb_device *dev = interface_to_usbdev(intf); |
64 | int retval; | |
65 | ||
66 | do { | |
67 | retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
68 | USB_REQ_GET_REPORT, | |
6e8ec537 CB |
69 | USB_DIR_IN | USB_TYPE_CLASS | |
70 | USB_RECIP_INTERFACE, | |
5d7e7d47 EH |
71 | (type << 8) + id, |
72 | intf->altsetting[0].desc.bInterfaceNumber, | |
73 | buf, size, 100); | |
74 | } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries); | |
75 | ||
76 | return retval; | |
3bea733a PC |
77 | } |
78 | ||
5d7e7d47 EH |
79 | static int wacom_set_report(struct usb_interface *intf, u8 type, u8 id, |
80 | void *buf, size_t size, unsigned int retries) | |
3bea733a | 81 | { |
5d7e7d47 EH |
82 | struct usb_device *dev = interface_to_usbdev(intf); |
83 | int retval; | |
84 | ||
85 | do { | |
86 | retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | |
87 | USB_REQ_SET_REPORT, | |
88 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, | |
89 | (type << 8) + id, | |
90 | intf->altsetting[0].desc.bInterfaceNumber, | |
91 | buf, size, 1000); | |
92 | } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries); | |
93 | ||
94 | return retval; | |
3bea733a PC |
95 | } |
96 | ||
54c9b226 | 97 | static void wacom_sys_irq(struct urb *urb) |
3bea733a PC |
98 | { |
99 | struct wacom *wacom = urb->context; | |
3bea733a PC |
100 | int retval; |
101 | ||
102 | switch (urb->status) { | |
103 | case 0: | |
104 | /* success */ | |
105 | break; | |
106 | case -ECONNRESET: | |
107 | case -ENOENT: | |
108 | case -ESHUTDOWN: | |
109 | /* this urb is terminated, clean up */ | |
ea3e6c59 | 110 | dbg("%s - urb shutting down with status: %d", __func__, urb->status); |
3bea733a PC |
111 | return; |
112 | default: | |
ea3e6c59 | 113 | dbg("%s - nonzero urb status received: %d", __func__, urb->status); |
3bea733a PC |
114 | goto exit; |
115 | } | |
116 | ||
95dd3b30 | 117 | wacom_wac_irq(&wacom->wacom_wac, urb->actual_length); |
3bea733a PC |
118 | |
119 | exit: | |
e7224094 | 120 | usb_mark_last_busy(wacom->usbdev); |
73a97f4f | 121 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
3bea733a PC |
122 | if (retval) |
123 | err ("%s - usb_submit_urb failed with result %d", | |
ea3e6c59 | 124 | __func__, retval); |
3bea733a PC |
125 | } |
126 | ||
3bea733a PC |
127 | static int wacom_open(struct input_dev *dev) |
128 | { | |
7791bdae | 129 | struct wacom *wacom = input_get_drvdata(dev); |
f6cd3783 | 130 | int retval = 0; |
3bea733a | 131 | |
f6cd3783 | 132 | if (usb_autopm_get_interface(wacom->intf) < 0) |
3bea733a | 133 | return -EIO; |
f6cd3783 DT |
134 | |
135 | mutex_lock(&wacom->lock); | |
e7224094 ON |
136 | |
137 | if (usb_submit_urb(wacom->irq, GFP_KERNEL)) { | |
f6cd3783 DT |
138 | retval = -EIO; |
139 | goto out; | |
e7224094 ON |
140 | } |
141 | ||
51269fe8 | 142 | wacom->open = true; |
e7224094 | 143 | wacom->intf->needs_remote_wakeup = 1; |
3bea733a | 144 | |
f6cd3783 | 145 | out: |
e7224094 | 146 | mutex_unlock(&wacom->lock); |
62ecae09 | 147 | usb_autopm_put_interface(wacom->intf); |
f6cd3783 | 148 | return retval; |
3bea733a PC |
149 | } |
150 | ||
151 | static void wacom_close(struct input_dev *dev) | |
152 | { | |
7791bdae | 153 | struct wacom *wacom = input_get_drvdata(dev); |
62ecae09 DT |
154 | int autopm_error; |
155 | ||
156 | autopm_error = usb_autopm_get_interface(wacom->intf); | |
3bea733a | 157 | |
e7224094 | 158 | mutex_lock(&wacom->lock); |
3bea733a | 159 | usb_kill_urb(wacom->irq); |
51269fe8 | 160 | wacom->open = false; |
e7224094 ON |
161 | wacom->intf->needs_remote_wakeup = 0; |
162 | mutex_unlock(&wacom->lock); | |
f6cd3783 | 163 | |
62ecae09 DT |
164 | if (!autopm_error) |
165 | usb_autopm_put_interface(wacom->intf); | |
3bea733a PC |
166 | } |
167 | ||
545f4e99 | 168 | static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc, |
ec67bbed | 169 | struct wacom_features *features) |
545f4e99 PC |
170 | { |
171 | struct usb_device *dev = interface_to_usbdev(intf); | |
ec67bbed PC |
172 | char limit = 0; |
173 | /* result has to be defined as int for some devices */ | |
174 | int result = 0; | |
545f4e99 PC |
175 | int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0; |
176 | unsigned char *report; | |
177 | ||
178 | report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL); | |
179 | if (!report) | |
180 | return -ENOMEM; | |
181 | ||
182 | /* retrive report descriptors */ | |
183 | do { | |
184 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
185 | USB_REQ_GET_DESCRIPTOR, | |
186 | USB_RECIP_INTERFACE | USB_DIR_IN, | |
187 | HID_DEVICET_REPORT << 8, | |
188 | intf->altsetting[0].desc.bInterfaceNumber, /* interface */ | |
189 | report, | |
190 | hid_desc->wDescriptorLength, | |
191 | 5000); /* 5 secs */ | |
a417ea44 | 192 | } while (result < 0 && limit++ < WAC_MSG_RETRIES); |
545f4e99 | 193 | |
384318ec | 194 | /* No need to parse the Descriptor. It isn't an error though */ |
545f4e99 PC |
195 | if (result < 0) |
196 | goto out; | |
197 | ||
198 | for (i = 0; i < hid_desc->wDescriptorLength; i++) { | |
199 | ||
200 | switch (report[i]) { | |
201 | case HID_USAGE_PAGE: | |
202 | switch (report[i + 1]) { | |
203 | case HID_USAGE_PAGE_DIGITIZER: | |
204 | usage = WCM_DIGITIZER; | |
205 | i++; | |
206 | break; | |
207 | ||
208 | case HID_USAGE_PAGE_DESKTOP: | |
209 | usage = WCM_DESKTOP; | |
210 | i++; | |
211 | break; | |
212 | } | |
213 | break; | |
214 | ||
215 | case HID_USAGE: | |
216 | switch (report[i + 1]) { | |
217 | case HID_USAGE_X: | |
218 | if (usage == WCM_DESKTOP) { | |
219 | if (finger) { | |
84eb5aa6 | 220 | features->device_type = BTN_TOOL_FINGER; |
ec67bbed PC |
221 | if (features->type == TABLETPC2FG) { |
222 | /* need to reset back */ | |
223 | features->pktlen = WACOM_PKGLEN_TPC2FG; | |
84eb5aa6 | 224 | features->device_type = BTN_TOOL_DOUBLETAP; |
ec67bbed | 225 | } |
4a88081e PC |
226 | if (features->type == BAMBOO_PT) { |
227 | /* need to reset back */ | |
228 | features->pktlen = WACOM_PKGLEN_BBTOUCH; | |
84eb5aa6 | 229 | features->device_type = BTN_TOOL_DOUBLETAP; |
4a88081e PC |
230 | features->x_phy = |
231 | get_unaligned_le16(&report[i + 5]); | |
232 | features->x_max = | |
233 | get_unaligned_le16(&report[i + 8]); | |
234 | i += 15; | |
235 | } else { | |
236 | features->x_max = | |
237 | get_unaligned_le16(&report[i + 3]); | |
238 | features->x_phy = | |
239 | get_unaligned_le16(&report[i + 6]); | |
240 | features->unit = report[i + 9]; | |
241 | features->unitExpo = report[i + 11]; | |
242 | i += 12; | |
243 | } | |
545f4e99 | 244 | } else if (pen) { |
ec67bbed PC |
245 | /* penabled only accepts exact bytes of data */ |
246 | if (features->type == TABLETPC2FG) | |
57e413d9 | 247 | features->pktlen = WACOM_PKGLEN_GRAPHIRE; |
ec67bbed | 248 | features->device_type = BTN_TOOL_PEN; |
545f4e99 | 249 | features->x_max = |
252f7769 | 250 | get_unaligned_le16(&report[i + 3]); |
545f4e99 PC |
251 | i += 4; |
252 | } | |
253 | } else if (usage == WCM_DIGITIZER) { | |
254 | /* max pressure isn't reported | |
255 | features->pressure_max = (unsigned short) | |
256 | (report[i+4] << 8 | report[i + 3]); | |
257 | */ | |
258 | features->pressure_max = 255; | |
259 | i += 4; | |
260 | } | |
261 | break; | |
262 | ||
263 | case HID_USAGE_Y: | |
ec67bbed PC |
264 | if (usage == WCM_DESKTOP) { |
265 | if (finger) { | |
84eb5aa6 | 266 | features->device_type = BTN_TOOL_FINGER; |
ec67bbed PC |
267 | if (features->type == TABLETPC2FG) { |
268 | /* need to reset back */ | |
269 | features->pktlen = WACOM_PKGLEN_TPC2FG; | |
84eb5aa6 | 270 | features->device_type = BTN_TOOL_DOUBLETAP; |
ec67bbed | 271 | features->y_max = |
252f7769 | 272 | get_unaligned_le16(&report[i + 3]); |
ec67bbed | 273 | features->y_phy = |
252f7769 | 274 | get_unaligned_le16(&report[i + 6]); |
ec67bbed | 275 | i += 7; |
4a88081e PC |
276 | } else if (features->type == BAMBOO_PT) { |
277 | /* need to reset back */ | |
278 | features->pktlen = WACOM_PKGLEN_BBTOUCH; | |
84eb5aa6 | 279 | features->device_type = BTN_TOOL_DOUBLETAP; |
4a88081e PC |
280 | features->y_phy = |
281 | get_unaligned_le16(&report[i + 3]); | |
282 | features->y_max = | |
283 | get_unaligned_le16(&report[i + 6]); | |
284 | i += 12; | |
ec67bbed PC |
285 | } else { |
286 | features->y_max = | |
287 | features->x_max; | |
288 | features->y_phy = | |
252f7769 | 289 | get_unaligned_le16(&report[i + 3]); |
ec67bbed PC |
290 | i += 4; |
291 | } | |
292 | } else if (pen) { | |
293 | /* penabled only accepts exact bytes of data */ | |
294 | if (features->type == TABLETPC2FG) | |
57e413d9 | 295 | features->pktlen = WACOM_PKGLEN_GRAPHIRE; |
ec67bbed PC |
296 | features->device_type = BTN_TOOL_PEN; |
297 | features->y_max = | |
252f7769 | 298 | get_unaligned_le16(&report[i + 3]); |
ec67bbed PC |
299 | i += 4; |
300 | } | |
301 | } | |
545f4e99 PC |
302 | break; |
303 | ||
304 | case HID_USAGE_FINGER: | |
305 | finger = 1; | |
306 | i++; | |
307 | break; | |
308 | ||
309 | case HID_USAGE_STYLUS: | |
310 | pen = 1; | |
311 | i++; | |
312 | break; | |
313 | ||
314 | case HID_USAGE_UNDEFINED: | |
315 | if (usage == WCM_DESKTOP && finger) /* capacity */ | |
316 | features->pressure_max = | |
252f7769 | 317 | get_unaligned_le16(&report[i + 3]); |
545f4e99 PC |
318 | i += 4; |
319 | break; | |
320 | } | |
321 | break; | |
322 | ||
323 | case HID_COLLECTION: | |
ec67bbed | 324 | /* reset UsagePage and Finger */ |
545f4e99 PC |
325 | finger = usage = 0; |
326 | break; | |
327 | } | |
328 | } | |
329 | ||
545f4e99 | 330 | out: |
384318ec | 331 | result = 0; |
545f4e99 PC |
332 | kfree(report); |
333 | return result; | |
334 | } | |
335 | ||
ec67bbed | 336 | static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features) |
3b7307c2 DT |
337 | { |
338 | unsigned char *rep_data; | |
ec67bbed PC |
339 | int limit = 0, report_id = 2; |
340 | int error = -ENOMEM; | |
3b7307c2 | 341 | |
3b48c91c | 342 | rep_data = kmalloc(4, GFP_KERNEL); |
3b7307c2 | 343 | if (!rep_data) |
ec67bbed PC |
344 | return error; |
345 | ||
3b48c91c | 346 | /* ask to report tablet data if it is MT Tablet PC or |
3dc9f40d CB |
347 | * not a Tablet PC */ |
348 | if (features->type == TABLETPC2FG) { | |
ec67bbed PC |
349 | do { |
350 | rep_data[0] = 3; | |
351 | rep_data[1] = 4; | |
3b48c91c PC |
352 | rep_data[2] = 0; |
353 | rep_data[3] = 0; | |
ec67bbed | 354 | report_id = 3; |
5d7e7d47 EH |
355 | error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT, |
356 | report_id, rep_data, 4, 1); | |
ec67bbed | 357 | if (error >= 0) |
5d7e7d47 EH |
358 | error = wacom_get_report(intf, |
359 | WAC_HID_FEATURE_REPORT, | |
360 | report_id, rep_data, 4, 1); | |
a417ea44 | 361 | } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES); |
6e8ec537 CB |
362 | } else if (features->type != TABLETPC && |
363 | features->device_type == BTN_TOOL_PEN) { | |
ec67bbed PC |
364 | do { |
365 | rep_data[0] = 2; | |
366 | rep_data[1] = 2; | |
5d7e7d47 EH |
367 | error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT, |
368 | report_id, rep_data, 2, 1); | |
ec67bbed | 369 | if (error >= 0) |
5d7e7d47 EH |
370 | error = wacom_get_report(intf, |
371 | WAC_HID_FEATURE_REPORT, | |
372 | report_id, rep_data, 2, 1); | |
a417ea44 | 373 | } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES); |
ec67bbed | 374 | } |
3b7307c2 DT |
375 | |
376 | kfree(rep_data); | |
377 | ||
378 | return error < 0 ? error : 0; | |
379 | } | |
380 | ||
ec67bbed PC |
381 | static int wacom_retrieve_hid_descriptor(struct usb_interface *intf, |
382 | struct wacom_features *features) | |
383 | { | |
384 | int error = 0; | |
385 | struct usb_host_interface *interface = intf->cur_altsetting; | |
386 | struct hid_descriptor *hid_desc; | |
387 | ||
fed87e65 | 388 | /* default features */ |
ec67bbed | 389 | features->device_type = BTN_TOOL_PEN; |
fed87e65 HR |
390 | features->x_fuzz = 4; |
391 | features->y_fuzz = 4; | |
392 | features->pressure_fuzz = 0; | |
393 | features->distance_fuzz = 0; | |
ec67bbed | 394 | |
3dc9f40d | 395 | /* only Tablet PCs and Bamboo P&T need to retrieve the info */ |
4a88081e PC |
396 | if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) && |
397 | (features->type != BAMBOO_PT)) | |
ec67bbed PC |
398 | goto out; |
399 | ||
400 | if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) { | |
401 | if (usb_get_extra_descriptor(&interface->endpoint[0], | |
402 | HID_DEVICET_REPORT, &hid_desc)) { | |
403 | printk("wacom: can not retrieve extra class descriptor\n"); | |
404 | error = 1; | |
405 | goto out; | |
406 | } | |
407 | } | |
408 | error = wacom_parse_hid(intf, hid_desc, features); | |
409 | if (error) | |
410 | goto out; | |
411 | ||
ec67bbed PC |
412 | out: |
413 | return error; | |
414 | } | |
415 | ||
4492efff PC |
416 | struct wacom_usbdev_data { |
417 | struct list_head list; | |
418 | struct kref kref; | |
419 | struct usb_device *dev; | |
420 | struct wacom_shared shared; | |
421 | }; | |
422 | ||
423 | static LIST_HEAD(wacom_udev_list); | |
424 | static DEFINE_MUTEX(wacom_udev_list_lock); | |
425 | ||
426 | static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev) | |
427 | { | |
428 | struct wacom_usbdev_data *data; | |
429 | ||
430 | list_for_each_entry(data, &wacom_udev_list, list) { | |
431 | if (data->dev == dev) { | |
432 | kref_get(&data->kref); | |
433 | return data; | |
434 | } | |
435 | } | |
436 | ||
437 | return NULL; | |
438 | } | |
439 | ||
440 | static int wacom_add_shared_data(struct wacom_wac *wacom, | |
441 | struct usb_device *dev) | |
442 | { | |
443 | struct wacom_usbdev_data *data; | |
444 | int retval = 0; | |
445 | ||
446 | mutex_lock(&wacom_udev_list_lock); | |
447 | ||
448 | data = wacom_get_usbdev_data(dev); | |
449 | if (!data) { | |
450 | data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL); | |
451 | if (!data) { | |
452 | retval = -ENOMEM; | |
453 | goto out; | |
454 | } | |
455 | ||
456 | kref_init(&data->kref); | |
457 | data->dev = dev; | |
458 | list_add_tail(&data->list, &wacom_udev_list); | |
459 | } | |
460 | ||
461 | wacom->shared = &data->shared; | |
462 | ||
463 | out: | |
464 | mutex_unlock(&wacom_udev_list_lock); | |
465 | return retval; | |
466 | } | |
467 | ||
468 | static void wacom_release_shared_data(struct kref *kref) | |
469 | { | |
470 | struct wacom_usbdev_data *data = | |
471 | container_of(kref, struct wacom_usbdev_data, kref); | |
472 | ||
473 | mutex_lock(&wacom_udev_list_lock); | |
474 | list_del(&data->list); | |
475 | mutex_unlock(&wacom_udev_list_lock); | |
476 | ||
477 | kfree(data); | |
478 | } | |
479 | ||
480 | static void wacom_remove_shared_data(struct wacom_wac *wacom) | |
481 | { | |
482 | struct wacom_usbdev_data *data; | |
483 | ||
484 | if (wacom->shared) { | |
485 | data = container_of(wacom->shared, struct wacom_usbdev_data, shared); | |
486 | kref_put(&data->kref, wacom_release_shared_data); | |
487 | wacom->shared = NULL; | |
488 | } | |
489 | } | |
490 | ||
5d7e7d47 EH |
491 | static int wacom_led_control(struct wacom *wacom) |
492 | { | |
493 | unsigned char *buf; | |
09e7d941 | 494 | int retval, led = 0; |
5d7e7d47 EH |
495 | |
496 | buf = kzalloc(9, GFP_KERNEL); | |
497 | if (!buf) | |
498 | return -ENOMEM; | |
499 | ||
09e7d941 PC |
500 | if (wacom->wacom_wac.features.type == WACOM_21UX2) |
501 | led = (wacom->led.select[1] << 4) | 0x40; | |
502 | ||
503 | led |= wacom->led.select[0] | 0x4; | |
504 | ||
5d7e7d47 | 505 | buf[0] = WAC_CMD_LED_CONTROL; |
09e7d941 | 506 | buf[1] = led; |
5d7e7d47 EH |
507 | buf[2] = wacom->led.llv; |
508 | buf[3] = wacom->led.hlv; | |
509 | buf[4] = wacom->led.img_lum; | |
510 | ||
511 | retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL, | |
512 | buf, 9, WAC_CMD_RETRIES); | |
513 | kfree(buf); | |
514 | ||
515 | return retval; | |
516 | } | |
517 | ||
518 | static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img) | |
519 | { | |
520 | unsigned char *buf; | |
521 | int i, retval; | |
522 | ||
523 | buf = kzalloc(259, GFP_KERNEL); | |
524 | if (!buf) | |
525 | return -ENOMEM; | |
526 | ||
527 | /* Send 'start' command */ | |
528 | buf[0] = WAC_CMD_ICON_START; | |
529 | buf[1] = 1; | |
530 | retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START, | |
531 | buf, 2, WAC_CMD_RETRIES); | |
532 | if (retval < 0) | |
533 | goto out; | |
534 | ||
535 | buf[0] = WAC_CMD_ICON_XFER; | |
536 | buf[1] = button_id & 0x07; | |
537 | for (i = 0; i < 4; i++) { | |
538 | buf[2] = i; | |
539 | memcpy(buf + 3, img + i * 256, 256); | |
540 | ||
541 | retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER, | |
542 | buf, 259, WAC_CMD_RETRIES); | |
543 | if (retval < 0) | |
544 | break; | |
545 | } | |
546 | ||
547 | /* Send 'stop' */ | |
548 | buf[0] = WAC_CMD_ICON_START; | |
549 | buf[1] = 0; | |
550 | wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START, | |
551 | buf, 2, WAC_CMD_RETRIES); | |
552 | ||
553 | out: | |
554 | kfree(buf); | |
555 | return retval; | |
556 | } | |
557 | ||
09e7d941 | 558 | static ssize_t wacom_led_select_store(struct device *dev, int set_id, |
5d7e7d47 EH |
559 | const char *buf, size_t count) |
560 | { | |
561 | struct wacom *wacom = dev_get_drvdata(dev); | |
562 | unsigned int id; | |
563 | int err; | |
564 | ||
565 | err = kstrtouint(buf, 10, &id); | |
566 | if (err) | |
567 | return err; | |
568 | ||
569 | mutex_lock(&wacom->lock); | |
570 | ||
09e7d941 | 571 | wacom->led.select[set_id] = id & 0x3; |
5d7e7d47 EH |
572 | err = wacom_led_control(wacom); |
573 | ||
574 | mutex_unlock(&wacom->lock); | |
575 | ||
576 | return err < 0 ? err : count; | |
577 | } | |
578 | ||
09e7d941 PC |
579 | #define DEVICE_LED_SELECT_ATTR(SET_ID) \ |
580 | static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \ | |
581 | struct device_attribute *attr, const char *buf, size_t count) \ | |
582 | { \ | |
583 | return wacom_led_select_store(dev, SET_ID, buf, count); \ | |
584 | } \ | |
04c59abd PC |
585 | static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \ |
586 | struct device_attribute *attr, char *buf) \ | |
587 | { \ | |
588 | struct wacom *wacom = dev_get_drvdata(dev); \ | |
589 | return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \ | |
590 | } \ | |
591 | static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \ | |
592 | wacom_led##SET_ID##_select_show, \ | |
09e7d941 PC |
593 | wacom_led##SET_ID##_select_store) |
594 | ||
595 | DEVICE_LED_SELECT_ATTR(0); | |
596 | DEVICE_LED_SELECT_ATTR(1); | |
5d7e7d47 EH |
597 | |
598 | static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest, | |
599 | const char *buf, size_t count) | |
600 | { | |
601 | unsigned int value; | |
602 | int err; | |
603 | ||
604 | err = kstrtouint(buf, 10, &value); | |
605 | if (err) | |
606 | return err; | |
607 | ||
608 | mutex_lock(&wacom->lock); | |
609 | ||
610 | *dest = value & 0x7f; | |
611 | err = wacom_led_control(wacom); | |
612 | ||
613 | mutex_unlock(&wacom->lock); | |
614 | ||
615 | return err < 0 ? err : count; | |
616 | } | |
617 | ||
618 | #define DEVICE_LUMINANCE_ATTR(name, field) \ | |
619 | static ssize_t wacom_##name##_luminance_store(struct device *dev, \ | |
620 | struct device_attribute *attr, const char *buf, size_t count) \ | |
621 | { \ | |
622 | struct wacom *wacom = dev_get_drvdata(dev); \ | |
623 | \ | |
624 | return wacom_luminance_store(wacom, &wacom->led.field, \ | |
625 | buf, count); \ | |
626 | } \ | |
627 | static DEVICE_ATTR(name##_luminance, S_IWUSR, \ | |
628 | NULL, wacom_##name##_luminance_store) | |
629 | ||
630 | DEVICE_LUMINANCE_ATTR(status0, llv); | |
631 | DEVICE_LUMINANCE_ATTR(status1, hlv); | |
632 | DEVICE_LUMINANCE_ATTR(buttons, img_lum); | |
633 | ||
634 | static ssize_t wacom_button_image_store(struct device *dev, int button_id, | |
635 | const char *buf, size_t count) | |
636 | { | |
637 | struct wacom *wacom = dev_get_drvdata(dev); | |
638 | int err; | |
639 | ||
640 | if (count != 1024) | |
641 | return -EINVAL; | |
642 | ||
643 | mutex_lock(&wacom->lock); | |
644 | ||
645 | err = wacom_led_putimage(wacom, button_id, buf); | |
646 | ||
647 | mutex_unlock(&wacom->lock); | |
648 | ||
649 | return err < 0 ? err : count; | |
650 | } | |
651 | ||
652 | #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \ | |
653 | static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \ | |
654 | struct device_attribute *attr, const char *buf, size_t count) \ | |
655 | { \ | |
656 | return wacom_button_image_store(dev, BUTTON_ID, buf, count); \ | |
657 | } \ | |
658 | static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \ | |
659 | NULL, wacom_btnimg##BUTTON_ID##_store) | |
660 | ||
661 | DEVICE_BTNIMG_ATTR(0); | |
662 | DEVICE_BTNIMG_ATTR(1); | |
663 | DEVICE_BTNIMG_ATTR(2); | |
664 | DEVICE_BTNIMG_ATTR(3); | |
665 | DEVICE_BTNIMG_ATTR(4); | |
666 | DEVICE_BTNIMG_ATTR(5); | |
667 | DEVICE_BTNIMG_ATTR(6); | |
668 | DEVICE_BTNIMG_ATTR(7); | |
669 | ||
09e7d941 PC |
670 | static struct attribute *cintiq_led_attrs[] = { |
671 | &dev_attr_status_led0_select.attr, | |
672 | &dev_attr_status_led1_select.attr, | |
673 | NULL | |
674 | }; | |
675 | ||
676 | static struct attribute_group cintiq_led_attr_group = { | |
677 | .name = "wacom_led", | |
678 | .attrs = cintiq_led_attrs, | |
679 | }; | |
680 | ||
681 | static struct attribute *intuos4_led_attrs[] = { | |
5d7e7d47 EH |
682 | &dev_attr_status0_luminance.attr, |
683 | &dev_attr_status1_luminance.attr, | |
09e7d941 | 684 | &dev_attr_status_led0_select.attr, |
5d7e7d47 EH |
685 | &dev_attr_buttons_luminance.attr, |
686 | &dev_attr_button0_rawimg.attr, | |
687 | &dev_attr_button1_rawimg.attr, | |
688 | &dev_attr_button2_rawimg.attr, | |
689 | &dev_attr_button3_rawimg.attr, | |
690 | &dev_attr_button4_rawimg.attr, | |
691 | &dev_attr_button5_rawimg.attr, | |
692 | &dev_attr_button6_rawimg.attr, | |
693 | &dev_attr_button7_rawimg.attr, | |
694 | NULL | |
695 | }; | |
696 | ||
09e7d941 | 697 | static struct attribute_group intuos4_led_attr_group = { |
5d7e7d47 | 698 | .name = "wacom_led", |
09e7d941 | 699 | .attrs = intuos4_led_attrs, |
5d7e7d47 EH |
700 | }; |
701 | ||
702 | static int wacom_initialize_leds(struct wacom *wacom) | |
703 | { | |
704 | int error; | |
705 | ||
09e7d941 PC |
706 | /* Initialize default values */ |
707 | switch (wacom->wacom_wac.features.type) { | |
708 | case INTUOS4: | |
709 | case INTUOS4L: | |
710 | wacom->led.select[0] = 0; | |
711 | wacom->led.select[1] = 0; | |
f4fa9a6d | 712 | wacom->led.llv = 10; |
5d7e7d47 EH |
713 | wacom->led.hlv = 20; |
714 | wacom->led.img_lum = 10; | |
09e7d941 PC |
715 | error = sysfs_create_group(&wacom->intf->dev.kobj, |
716 | &intuos4_led_attr_group); | |
717 | break; | |
718 | ||
719 | case WACOM_21UX2: | |
720 | wacom->led.select[0] = 0; | |
721 | wacom->led.select[1] = 0; | |
722 | wacom->led.llv = 0; | |
723 | wacom->led.hlv = 0; | |
724 | wacom->led.img_lum = 0; | |
5d7e7d47 EH |
725 | |
726 | error = sysfs_create_group(&wacom->intf->dev.kobj, | |
09e7d941 PC |
727 | &cintiq_led_attr_group); |
728 | break; | |
729 | ||
730 | default: | |
731 | return 0; | |
5d7e7d47 EH |
732 | } |
733 | ||
09e7d941 PC |
734 | if (error) { |
735 | dev_err(&wacom->intf->dev, | |
736 | "cannot create sysfs group err: %d\n", error); | |
737 | return error; | |
738 | } | |
739 | wacom_led_control(wacom); | |
740 | ||
5d7e7d47 EH |
741 | return 0; |
742 | } | |
743 | ||
744 | static void wacom_destroy_leds(struct wacom *wacom) | |
745 | { | |
09e7d941 PC |
746 | switch (wacom->wacom_wac.features.type) { |
747 | case INTUOS4: | |
748 | case INTUOS4L: | |
5d7e7d47 | 749 | sysfs_remove_group(&wacom->intf->dev.kobj, |
09e7d941 PC |
750 | &intuos4_led_attr_group); |
751 | break; | |
752 | ||
753 | case WACOM_21UX2: | |
754 | sysfs_remove_group(&wacom->intf->dev.kobj, | |
755 | &cintiq_led_attr_group); | |
756 | break; | |
5d7e7d47 EH |
757 | } |
758 | } | |
759 | ||
3bea733a PC |
760 | static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id) |
761 | { | |
762 | struct usb_device *dev = interface_to_usbdev(intf); | |
763 | struct usb_endpoint_descriptor *endpoint; | |
764 | struct wacom *wacom; | |
765 | struct wacom_wac *wacom_wac; | |
e33da8a5 | 766 | struct wacom_features *features; |
3bea733a | 767 | struct input_dev *input_dev; |
e33da8a5 | 768 | int error; |
3bea733a | 769 | |
e33da8a5 | 770 | if (!id->driver_info) |
b036f6fb BB |
771 | return -EINVAL; |
772 | ||
3bea733a | 773 | wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL); |
3bea733a | 774 | input_dev = input_allocate_device(); |
51269fe8 | 775 | if (!wacom || !input_dev) { |
e33da8a5 JC |
776 | error = -ENOMEM; |
777 | goto fail1; | |
778 | } | |
779 | ||
51269fe8 | 780 | wacom_wac = &wacom->wacom_wac; |
e33da8a5 JC |
781 | wacom_wac->features = *((struct wacom_features *)id->driver_info); |
782 | features = &wacom_wac->features; | |
783 | if (features->pktlen > WACOM_PKGLEN_MAX) { | |
784 | error = -EINVAL; | |
3bea733a | 785 | goto fail1; |
e33da8a5 | 786 | } |
3bea733a | 787 | |
997ea58e DM |
788 | wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX, |
789 | GFP_KERNEL, &wacom->data_dma); | |
e33da8a5 JC |
790 | if (!wacom_wac->data) { |
791 | error = -ENOMEM; | |
3bea733a | 792 | goto fail1; |
e33da8a5 | 793 | } |
3bea733a PC |
794 | |
795 | wacom->irq = usb_alloc_urb(0, GFP_KERNEL); | |
e33da8a5 JC |
796 | if (!wacom->irq) { |
797 | error = -ENOMEM; | |
3bea733a | 798 | goto fail2; |
e33da8a5 | 799 | } |
3bea733a PC |
800 | |
801 | wacom->usbdev = dev; | |
e7224094 ON |
802 | wacom->intf = intf; |
803 | mutex_init(&wacom->lock); | |
3bea733a PC |
804 | usb_make_path(dev, wacom->phys, sizeof(wacom->phys)); |
805 | strlcat(wacom->phys, "/input0", sizeof(wacom->phys)); | |
806 | ||
8da23fc1 | 807 | wacom_wac->input = input_dev; |
3bea733a | 808 | |
545f4e99 PC |
809 | endpoint = &intf->cur_altsetting->endpoint[0].desc; |
810 | ||
ec67bbed PC |
811 | /* Retrieve the physical and logical size for OEM devices */ |
812 | error = wacom_retrieve_hid_descriptor(intf, features); | |
813 | if (error) | |
4b6d4434 | 814 | goto fail3; |
545f4e99 | 815 | |
bc73dd39 HR |
816 | wacom_setup_device_quirks(features); |
817 | ||
49b764ae PC |
818 | strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name)); |
819 | ||
bc73dd39 | 820 | if (features->quirks & WACOM_QUIRK_MULTI_INPUT) { |
49b764ae PC |
821 | /* Append the device type to the name */ |
822 | strlcat(wacom_wac->name, | |
823 | features->device_type == BTN_TOOL_PEN ? | |
824 | " Pen" : " Finger", | |
825 | sizeof(wacom_wac->name)); | |
4492efff PC |
826 | |
827 | error = wacom_add_shared_data(wacom_wac, dev); | |
828 | if (error) | |
829 | goto fail3; | |
49b764ae PC |
830 | } |
831 | ||
8da23fc1 DT |
832 | input_dev->name = wacom_wac->name; |
833 | input_dev->dev.parent = &intf->dev; | |
834 | input_dev->open = wacom_open; | |
835 | input_dev->close = wacom_close; | |
836 | usb_to_input_id(dev, &input_dev->id); | |
837 | input_set_drvdata(input_dev, wacom); | |
e33da8a5 | 838 | |
8da23fc1 | 839 | wacom_setup_input_capabilities(input_dev, wacom_wac); |
3bea733a | 840 | |
3bea733a PC |
841 | usb_fill_int_urb(wacom->irq, dev, |
842 | usb_rcvintpipe(dev, endpoint->bEndpointAddress), | |
ec67bbed | 843 | wacom_wac->data, features->pktlen, |
8d32e3ae | 844 | wacom_sys_irq, wacom, endpoint->bInterval); |
3bea733a PC |
845 | wacom->irq->transfer_dma = wacom->data_dma; |
846 | wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
847 | ||
5d7e7d47 | 848 | error = wacom_initialize_leds(wacom); |
5014186d | 849 | if (error) |
4492efff | 850 | goto fail4; |
3bea733a | 851 | |
5d7e7d47 EH |
852 | error = input_register_device(input_dev); |
853 | if (error) | |
854 | goto fail5; | |
855 | ||
ec67bbed PC |
856 | /* Note that if query fails it is not a hard failure */ |
857 | wacom_query_tablet_data(intf, features); | |
3bea733a PC |
858 | |
859 | usb_set_intfdata(intf, wacom); | |
860 | return 0; | |
861 | ||
5d7e7d47 | 862 | fail5: wacom_destroy_leds(wacom); |
4492efff | 863 | fail4: wacom_remove_shared_data(wacom_wac); |
5014186d | 864 | fail3: usb_free_urb(wacom->irq); |
997ea58e | 865 | fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma); |
5014186d | 866 | fail1: input_free_device(input_dev); |
3bea733a | 867 | kfree(wacom); |
5014186d | 868 | return error; |
3bea733a PC |
869 | } |
870 | ||
871 | static void wacom_disconnect(struct usb_interface *intf) | |
872 | { | |
e7224094 | 873 | struct wacom *wacom = usb_get_intfdata(intf); |
3bea733a PC |
874 | |
875 | usb_set_intfdata(intf, NULL); | |
e7224094 ON |
876 | |
877 | usb_kill_urb(wacom->irq); | |
8da23fc1 | 878 | input_unregister_device(wacom->wacom_wac.input); |
5d7e7d47 | 879 | wacom_destroy_leds(wacom); |
e7224094 | 880 | usb_free_urb(wacom->irq); |
997ea58e | 881 | usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX, |
51269fe8 DT |
882 | wacom->wacom_wac.data, wacom->data_dma); |
883 | wacom_remove_shared_data(&wacom->wacom_wac); | |
e7224094 ON |
884 | kfree(wacom); |
885 | } | |
886 | ||
887 | static int wacom_suspend(struct usb_interface *intf, pm_message_t message) | |
888 | { | |
889 | struct wacom *wacom = usb_get_intfdata(intf); | |
890 | ||
891 | mutex_lock(&wacom->lock); | |
892 | usb_kill_urb(wacom->irq); | |
893 | mutex_unlock(&wacom->lock); | |
894 | ||
895 | return 0; | |
896 | } | |
897 | ||
898 | static int wacom_resume(struct usb_interface *intf) | |
899 | { | |
900 | struct wacom *wacom = usb_get_intfdata(intf); | |
51269fe8 | 901 | struct wacom_features *features = &wacom->wacom_wac.features; |
5d7e7d47 | 902 | int rv = 0; |
e7224094 ON |
903 | |
904 | mutex_lock(&wacom->lock); | |
38101475 PC |
905 | |
906 | /* switch to wacom mode first */ | |
907 | wacom_query_tablet_data(intf, features); | |
5d7e7d47 | 908 | wacom_led_control(wacom); |
38101475 | 909 | |
5d7e7d47 EH |
910 | if (wacom->open && usb_submit_urb(wacom->irq, GFP_NOIO) < 0) |
911 | rv = -EIO; | |
38101475 | 912 | |
e7224094 ON |
913 | mutex_unlock(&wacom->lock); |
914 | ||
915 | return rv; | |
916 | } | |
917 | ||
918 | static int wacom_reset_resume(struct usb_interface *intf) | |
919 | { | |
920 | return wacom_resume(intf); | |
3bea733a PC |
921 | } |
922 | ||
923 | static struct usb_driver wacom_driver = { | |
924 | .name = "wacom", | |
b036f6fb | 925 | .id_table = wacom_ids, |
3bea733a PC |
926 | .probe = wacom_probe, |
927 | .disconnect = wacom_disconnect, | |
e7224094 ON |
928 | .suspend = wacom_suspend, |
929 | .resume = wacom_resume, | |
930 | .reset_resume = wacom_reset_resume, | |
931 | .supports_autosuspend = 1, | |
3bea733a PC |
932 | }; |
933 | ||
934 | static int __init wacom_init(void) | |
935 | { | |
936 | int result; | |
b036f6fb | 937 | |
3bea733a PC |
938 | result = usb_register(&wacom_driver); |
939 | if (result == 0) | |
899ef6e7 GKH |
940 | printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" |
941 | DRIVER_DESC "\n"); | |
3bea733a PC |
942 | return result; |
943 | } | |
944 | ||
945 | static void __exit wacom_exit(void) | |
946 | { | |
947 | usb_deregister(&wacom_driver); | |
948 | } | |
949 | ||
950 | module_init(wacom_init); | |
951 | module_exit(wacom_exit); |