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