]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/input/tablet/wacom_sys.c
Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mjg59/platf...
[net-next-2.6.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
31#define HID_COLLECTION 0xc0
32
33enum {
34 WCM_UNDEFINED = 0,
35 WCM_DESKTOP,
36 WCM_DIGITIZER,
37};
38
39struct 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
545f4e99 51#define WAC_HID_FEATURE_REPORT 0x03
3bea733a
PC
52
53static int usb_get_report(struct usb_interface *intf, unsigned char type,
54 unsigned char id, void *buf, int size)
55{
56 return usb_control_msg(interface_to_usbdev(intf),
57 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
58 USB_REQ_GET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
59 (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
60 buf, size, 100);
61}
62
63static int usb_set_report(struct usb_interface *intf, unsigned char type,
64 unsigned char id, void *buf, int size)
65{
66 return usb_control_msg(interface_to_usbdev(intf),
67 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
68 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
69 (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
70 buf, size, 1000);
71}
72
54c9b226 73static void wacom_sys_irq(struct urb *urb)
3bea733a
PC
74{
75 struct wacom *wacom = urb->context;
3bea733a
PC
76 int retval;
77
78 switch (urb->status) {
79 case 0:
80 /* success */
81 break;
82 case -ECONNRESET:
83 case -ENOENT:
84 case -ESHUTDOWN:
85 /* this urb is terminated, clean up */
ea3e6c59 86 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
3bea733a
PC
87 return;
88 default:
ea3e6c59 89 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
3bea733a
PC
90 goto exit;
91 }
92
95dd3b30 93 wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
3bea733a
PC
94
95 exit:
e7224094 96 usb_mark_last_busy(wacom->usbdev);
73a97f4f 97 retval = usb_submit_urb(urb, GFP_ATOMIC);
3bea733a
PC
98 if (retval)
99 err ("%s - usb_submit_urb failed with result %d",
ea3e6c59 100 __func__, retval);
3bea733a
PC
101}
102
3bea733a
PC
103static int wacom_open(struct input_dev *dev)
104{
7791bdae 105 struct wacom *wacom = input_get_drvdata(dev);
f6cd3783 106 int retval = 0;
3bea733a 107
f6cd3783 108 if (usb_autopm_get_interface(wacom->intf) < 0)
3bea733a 109 return -EIO;
f6cd3783
DT
110
111 mutex_lock(&wacom->lock);
e7224094
ON
112
113 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
f6cd3783
DT
114 retval = -EIO;
115 goto out;
e7224094
ON
116 }
117
51269fe8 118 wacom->open = true;
e7224094 119 wacom->intf->needs_remote_wakeup = 1;
3bea733a 120
f6cd3783 121out:
e7224094 122 mutex_unlock(&wacom->lock);
f6cd3783
DT
123 if (retval)
124 usb_autopm_put_interface(wacom->intf);
125 return retval;
3bea733a
PC
126}
127
128static void wacom_close(struct input_dev *dev)
129{
7791bdae 130 struct wacom *wacom = input_get_drvdata(dev);
3bea733a 131
e7224094 132 mutex_lock(&wacom->lock);
3bea733a 133 usb_kill_urb(wacom->irq);
51269fe8 134 wacom->open = false;
e7224094
ON
135 wacom->intf->needs_remote_wakeup = 0;
136 mutex_unlock(&wacom->lock);
f6cd3783
DT
137
138 usb_autopm_put_interface(wacom->intf);
3bea733a
PC
139}
140
545f4e99 141static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
ec67bbed 142 struct wacom_features *features)
545f4e99
PC
143{
144 struct usb_device *dev = interface_to_usbdev(intf);
ec67bbed
PC
145 char limit = 0;
146 /* result has to be defined as int for some devices */
147 int result = 0;
545f4e99
PC
148 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
149 unsigned char *report;
150
151 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
152 if (!report)
153 return -ENOMEM;
154
155 /* retrive report descriptors */
156 do {
157 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
158 USB_REQ_GET_DESCRIPTOR,
159 USB_RECIP_INTERFACE | USB_DIR_IN,
160 HID_DEVICET_REPORT << 8,
161 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
162 report,
163 hid_desc->wDescriptorLength,
164 5000); /* 5 secs */
165 } while (result < 0 && limit++ < 5);
166
384318ec 167 /* No need to parse the Descriptor. It isn't an error though */
545f4e99
PC
168 if (result < 0)
169 goto out;
170
171 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
172
173 switch (report[i]) {
174 case HID_USAGE_PAGE:
175 switch (report[i + 1]) {
176 case HID_USAGE_PAGE_DIGITIZER:
177 usage = WCM_DIGITIZER;
178 i++;
179 break;
180
181 case HID_USAGE_PAGE_DESKTOP:
182 usage = WCM_DESKTOP;
183 i++;
184 break;
185 }
186 break;
187
188 case HID_USAGE:
189 switch (report[i + 1]) {
190 case HID_USAGE_X:
191 if (usage == WCM_DESKTOP) {
192 if (finger) {
ec67bbed
PC
193 features->device_type = BTN_TOOL_DOUBLETAP;
194 if (features->type == TABLETPC2FG) {
195 /* need to reset back */
196 features->pktlen = WACOM_PKGLEN_TPC2FG;
197 features->device_type = BTN_TOOL_TRIPLETAP;
198 }
545f4e99 199 features->x_max =
252f7769 200 get_unaligned_le16(&report[i + 3]);
ec67bbed 201 features->x_phy =
252f7769 202 get_unaligned_le16(&report[i + 6]);
ec67bbed
PC
203 features->unit = report[i + 9];
204 features->unitExpo = report[i + 11];
205 i += 12;
545f4e99 206 } else if (pen) {
ec67bbed
PC
207 /* penabled only accepts exact bytes of data */
208 if (features->type == TABLETPC2FG)
57e413d9 209 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
ec67bbed 210 features->device_type = BTN_TOOL_PEN;
545f4e99 211 features->x_max =
252f7769 212 get_unaligned_le16(&report[i + 3]);
545f4e99
PC
213 i += 4;
214 }
215 } else if (usage == WCM_DIGITIZER) {
216 /* max pressure isn't reported
217 features->pressure_max = (unsigned short)
218 (report[i+4] << 8 | report[i + 3]);
219 */
220 features->pressure_max = 255;
221 i += 4;
222 }
223 break;
224
225 case HID_USAGE_Y:
ec67bbed
PC
226 if (usage == WCM_DESKTOP) {
227 if (finger) {
228 features->device_type = BTN_TOOL_DOUBLETAP;
229 if (features->type == TABLETPC2FG) {
230 /* need to reset back */
231 features->pktlen = WACOM_PKGLEN_TPC2FG;
232 features->device_type = BTN_TOOL_TRIPLETAP;
233 features->y_max =
252f7769 234 get_unaligned_le16(&report[i + 3]);
ec67bbed 235 features->y_phy =
252f7769 236 get_unaligned_le16(&report[i + 6]);
ec67bbed
PC
237 i += 7;
238 } else {
239 features->y_max =
240 features->x_max;
241 features->y_phy =
252f7769 242 get_unaligned_le16(&report[i + 3]);
ec67bbed
PC
243 i += 4;
244 }
245 } else if (pen) {
246 /* penabled only accepts exact bytes of data */
247 if (features->type == TABLETPC2FG)
57e413d9 248 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
ec67bbed
PC
249 features->device_type = BTN_TOOL_PEN;
250 features->y_max =
252f7769 251 get_unaligned_le16(&report[i + 3]);
ec67bbed
PC
252 i += 4;
253 }
254 }
545f4e99
PC
255 break;
256
257 case HID_USAGE_FINGER:
258 finger = 1;
259 i++;
260 break;
261
262 case HID_USAGE_STYLUS:
263 pen = 1;
264 i++;
265 break;
266
267 case HID_USAGE_UNDEFINED:
268 if (usage == WCM_DESKTOP && finger) /* capacity */
269 features->pressure_max =
252f7769 270 get_unaligned_le16(&report[i + 3]);
545f4e99
PC
271 i += 4;
272 break;
273 }
274 break;
275
276 case HID_COLLECTION:
ec67bbed 277 /* reset UsagePage and Finger */
545f4e99
PC
278 finger = usage = 0;
279 break;
280 }
281 }
282
545f4e99 283 out:
384318ec 284 result = 0;
545f4e99
PC
285 kfree(report);
286 return result;
287}
288
ec67bbed 289static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
3b7307c2
DT
290{
291 unsigned char *rep_data;
ec67bbed
PC
292 int limit = 0, report_id = 2;
293 int error = -ENOMEM;
3b7307c2
DT
294
295 rep_data = kmalloc(2, GFP_KERNEL);
296 if (!rep_data)
ec67bbed
PC
297 return error;
298
299 /* ask to report tablet data if it is 2FGT or not a Tablet PC */
300 if (features->device_type == BTN_TOOL_TRIPLETAP) {
301 do {
302 rep_data[0] = 3;
303 rep_data[1] = 4;
304 report_id = 3;
305 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
306 report_id, rep_data, 2);
307 if (error >= 0)
308 error = usb_get_report(intf,
309 WAC_HID_FEATURE_REPORT, report_id,
310 rep_data, 3);
311 } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
312 } else if (features->type != TABLETPC && features->type != TABLETPC2FG) {
313 do {
314 rep_data[0] = 2;
315 rep_data[1] = 2;
316 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
317 report_id, rep_data, 2);
318 if (error >= 0)
319 error = usb_get_report(intf,
320 WAC_HID_FEATURE_REPORT, report_id,
321 rep_data, 2);
322 } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
323 }
3b7307c2
DT
324
325 kfree(rep_data);
326
327 return error < 0 ? error : 0;
328}
329
ec67bbed
PC
330static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
331 struct wacom_features *features)
332{
333 int error = 0;
334 struct usb_host_interface *interface = intf->cur_altsetting;
335 struct hid_descriptor *hid_desc;
336
337 /* default device to penabled */
338 features->device_type = BTN_TOOL_PEN;
339
340 /* only Tablet PCs need to retrieve the info */
341 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG))
342 goto out;
343
344 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
345 if (usb_get_extra_descriptor(&interface->endpoint[0],
346 HID_DEVICET_REPORT, &hid_desc)) {
347 printk("wacom: can not retrieve extra class descriptor\n");
348 error = 1;
349 goto out;
350 }
351 }
352 error = wacom_parse_hid(intf, hid_desc, features);
353 if (error)
354 goto out;
355
356 /* touch device found but size is not defined. use default */
357 if (features->device_type == BTN_TOOL_DOUBLETAP && !features->x_max) {
358 features->x_max = 1023;
359 features->y_max = 1023;
360 }
361
362 out:
363 return error;
364}
365
4492efff
PC
366struct wacom_usbdev_data {
367 struct list_head list;
368 struct kref kref;
369 struct usb_device *dev;
370 struct wacom_shared shared;
371};
372
373static LIST_HEAD(wacom_udev_list);
374static DEFINE_MUTEX(wacom_udev_list_lock);
375
376static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
377{
378 struct wacom_usbdev_data *data;
379
380 list_for_each_entry(data, &wacom_udev_list, list) {
381 if (data->dev == dev) {
382 kref_get(&data->kref);
383 return data;
384 }
385 }
386
387 return NULL;
388}
389
390static int wacom_add_shared_data(struct wacom_wac *wacom,
391 struct usb_device *dev)
392{
393 struct wacom_usbdev_data *data;
394 int retval = 0;
395
396 mutex_lock(&wacom_udev_list_lock);
397
398 data = wacom_get_usbdev_data(dev);
399 if (!data) {
400 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
401 if (!data) {
402 retval = -ENOMEM;
403 goto out;
404 }
405
406 kref_init(&data->kref);
407 data->dev = dev;
408 list_add_tail(&data->list, &wacom_udev_list);
409 }
410
411 wacom->shared = &data->shared;
412
413out:
414 mutex_unlock(&wacom_udev_list_lock);
415 return retval;
416}
417
418static void wacom_release_shared_data(struct kref *kref)
419{
420 struct wacom_usbdev_data *data =
421 container_of(kref, struct wacom_usbdev_data, kref);
422
423 mutex_lock(&wacom_udev_list_lock);
424 list_del(&data->list);
425 mutex_unlock(&wacom_udev_list_lock);
426
427 kfree(data);
428}
429
430static void wacom_remove_shared_data(struct wacom_wac *wacom)
431{
432 struct wacom_usbdev_data *data;
433
434 if (wacom->shared) {
435 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
436 kref_put(&data->kref, wacom_release_shared_data);
437 wacom->shared = NULL;
438 }
439}
440
3bea733a
PC
441static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
442{
443 struct usb_device *dev = interface_to_usbdev(intf);
444 struct usb_endpoint_descriptor *endpoint;
445 struct wacom *wacom;
446 struct wacom_wac *wacom_wac;
e33da8a5 447 struct wacom_features *features;
3bea733a 448 struct input_dev *input_dev;
e33da8a5 449 int error;
3bea733a 450
e33da8a5 451 if (!id->driver_info)
b036f6fb
BB
452 return -EINVAL;
453
3bea733a 454 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
3bea733a 455 input_dev = input_allocate_device();
51269fe8 456 if (!wacom || !input_dev) {
e33da8a5
JC
457 error = -ENOMEM;
458 goto fail1;
459 }
460
51269fe8 461 wacom_wac = &wacom->wacom_wac;
e33da8a5
JC
462 wacom_wac->features = *((struct wacom_features *)id->driver_info);
463 features = &wacom_wac->features;
464 if (features->pktlen > WACOM_PKGLEN_MAX) {
465 error = -EINVAL;
3bea733a 466 goto fail1;
e33da8a5 467 }
3bea733a 468
997ea58e
DM
469 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
470 GFP_KERNEL, &wacom->data_dma);
e33da8a5
JC
471 if (!wacom_wac->data) {
472 error = -ENOMEM;
3bea733a 473 goto fail1;
e33da8a5 474 }
3bea733a
PC
475
476 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
e33da8a5
JC
477 if (!wacom->irq) {
478 error = -ENOMEM;
3bea733a 479 goto fail2;
e33da8a5 480 }
3bea733a
PC
481
482 wacom->usbdev = dev;
e7224094
ON
483 wacom->intf = intf;
484 mutex_init(&wacom->lock);
3bea733a
PC
485 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
486 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
487
8da23fc1 488 wacom_wac->input = input_dev;
3bea733a 489
545f4e99
PC
490 endpoint = &intf->cur_altsetting->endpoint[0].desc;
491
ec67bbed
PC
492 /* Retrieve the physical and logical size for OEM devices */
493 error = wacom_retrieve_hid_descriptor(intf, features);
494 if (error)
495 goto fail2;
545f4e99 496
49b764ae
PC
497 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
498
499 if (features->type == TABLETPC || features->type == TABLETPC2FG) {
500 /* Append the device type to the name */
501 strlcat(wacom_wac->name,
502 features->device_type == BTN_TOOL_PEN ?
503 " Pen" : " Finger",
504 sizeof(wacom_wac->name));
4492efff
PC
505
506 error = wacom_add_shared_data(wacom_wac, dev);
507 if (error)
508 goto fail3;
49b764ae
PC
509 }
510
8da23fc1
DT
511 input_dev->name = wacom_wac->name;
512 input_dev->dev.parent = &intf->dev;
513 input_dev->open = wacom_open;
514 input_dev->close = wacom_close;
515 usb_to_input_id(dev, &input_dev->id);
516 input_set_drvdata(input_dev, wacom);
e33da8a5 517
8da23fc1 518 wacom_setup_input_capabilities(input_dev, wacom_wac);
3bea733a 519
3bea733a
PC
520 usb_fill_int_urb(wacom->irq, dev,
521 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
ec67bbed 522 wacom_wac->data, features->pktlen,
8d32e3ae 523 wacom_sys_irq, wacom, endpoint->bInterval);
3bea733a
PC
524 wacom->irq->transfer_dma = wacom->data_dma;
525 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
526
8da23fc1 527 error = input_register_device(input_dev);
5014186d 528 if (error)
4492efff 529 goto fail4;
3bea733a 530
ec67bbed
PC
531 /* Note that if query fails it is not a hard failure */
532 wacom_query_tablet_data(intf, features);
3bea733a
PC
533
534 usb_set_intfdata(intf, wacom);
535 return 0;
536
4492efff 537 fail4: wacom_remove_shared_data(wacom_wac);
5014186d 538 fail3: usb_free_urb(wacom->irq);
997ea58e 539 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
5014186d 540 fail1: input_free_device(input_dev);
3bea733a 541 kfree(wacom);
5014186d 542 return error;
3bea733a
PC
543}
544
545static void wacom_disconnect(struct usb_interface *intf)
546{
e7224094 547 struct wacom *wacom = usb_get_intfdata(intf);
3bea733a
PC
548
549 usb_set_intfdata(intf, NULL);
e7224094
ON
550
551 usb_kill_urb(wacom->irq);
8da23fc1 552 input_unregister_device(wacom->wacom_wac.input);
e7224094 553 usb_free_urb(wacom->irq);
997ea58e 554 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
51269fe8
DT
555 wacom->wacom_wac.data, wacom->data_dma);
556 wacom_remove_shared_data(&wacom->wacom_wac);
e7224094
ON
557 kfree(wacom);
558}
559
560static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
561{
562 struct wacom *wacom = usb_get_intfdata(intf);
563
564 mutex_lock(&wacom->lock);
565 usb_kill_urb(wacom->irq);
566 mutex_unlock(&wacom->lock);
567
568 return 0;
569}
570
571static int wacom_resume(struct usb_interface *intf)
572{
573 struct wacom *wacom = usb_get_intfdata(intf);
51269fe8 574 struct wacom_features *features = &wacom->wacom_wac.features;
e7224094
ON
575 int rv;
576
577 mutex_lock(&wacom->lock);
38101475
PC
578
579 /* switch to wacom mode first */
580 wacom_query_tablet_data(intf, features);
581
582 if (wacom->open)
e7224094 583 rv = usb_submit_urb(wacom->irq, GFP_NOIO);
38101475 584 else
e7224094 585 rv = 0;
38101475 586
e7224094
ON
587 mutex_unlock(&wacom->lock);
588
589 return rv;
590}
591
592static int wacom_reset_resume(struct usb_interface *intf)
593{
594 return wacom_resume(intf);
3bea733a
PC
595}
596
597static struct usb_driver wacom_driver = {
598 .name = "wacom",
b036f6fb 599 .id_table = wacom_ids,
3bea733a
PC
600 .probe = wacom_probe,
601 .disconnect = wacom_disconnect,
e7224094
ON
602 .suspend = wacom_suspend,
603 .resume = wacom_resume,
604 .reset_resume = wacom_reset_resume,
605 .supports_autosuspend = 1,
3bea733a
PC
606};
607
608static int __init wacom_init(void)
609{
610 int result;
b036f6fb 611
3bea733a
PC
612 result = usb_register(&wacom_driver);
613 if (result == 0)
899ef6e7
GKH
614 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
615 DRIVER_DESC "\n");
3bea733a
PC
616 return result;
617}
618
619static void __exit wacom_exit(void)
620{
621 usb_deregister(&wacom_driver);
622}
623
624module_init(wacom_init);
625module_exit(wacom_exit);