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