blob: be4b76f264a785d5e3f4ad8938e81de7b033f99d [file] [log] [blame]
Ping Cheng3bea7332006-07-13 18:01:36 -07001/*
Dmitry Torokhov4104d132007-05-07 16:16:29 -04002 * drivers/input/tablet/wacom_sys.c
Ping Cheng3bea7332006-07-13 18:01:36 -07003 *
Ping Cheng232f5692009-12-15 00:35:24 -08004 * USB Wacom tablet support - system specific code
Ping Cheng3bea7332006-07-13 18:01:36 -07005 */
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
14#include "wacom.h"
15#include "wacom_wac.h"
16
Ping Cheng545f4e92008-11-24 11:44:27 -050017/* 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 */
Ping Cheng3bea7332006-07-13 18:01:36 -070049#define USB_REQ_GET_REPORT 0x01
50#define USB_REQ_SET_REPORT 0x09
Ping Cheng545f4e92008-11-24 11:44:27 -050051#define WAC_HID_FEATURE_REPORT 0x03
Ping Cheng3bea7332006-07-13 18:01:36 -070052
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
73static struct input_dev * get_input_dev(struct wacom_combo *wcombo)
74{
75 return wcombo->wacom->dev;
76}
77
Adrian Bunk54c9b2262006-11-20 03:23:58 +010078static void wacom_sys_irq(struct urb *urb)
Ping Cheng3bea7332006-07-13 18:01:36 -070079{
80 struct wacom *wacom = urb->context;
81 struct wacom_combo wcombo;
82 int retval;
83
84 switch (urb->status) {
85 case 0:
86 /* success */
87 break;
88 case -ECONNRESET:
89 case -ENOENT:
90 case -ESHUTDOWN:
91 /* this urb is terminated, clean up */
Harvey Harrisonea3e6c52008-05-05 11:36:18 -040092 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -070093 return;
94 default:
Harvey Harrisonea3e6c52008-05-05 11:36:18 -040095 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -070096 goto exit;
97 }
98
99 wcombo.wacom = wacom;
100 wcombo.urb = urb;
Ping Cheng3bea7332006-07-13 18:01:36 -0700101
102 if (wacom_wac_irq(wacom->wacom_wac, (void *)&wcombo))
103 input_sync(get_input_dev(&wcombo));
104
105 exit:
Oliver Neukume7224092008-04-15 01:31:57 -0400106 usb_mark_last_busy(wacom->usbdev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700107 retval = usb_submit_urb (urb, GFP_ATOMIC);
108 if (retval)
109 err ("%s - usb_submit_urb failed with result %d",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400110 __func__, retval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700111}
112
113void wacom_report_key(void *wcombo, unsigned int key_type, int key_data)
114{
115 input_report_key(get_input_dev((struct wacom_combo *)wcombo), key_type, key_data);
Ping Cheng3bea7332006-07-13 18:01:36 -0700116}
117
118void wacom_report_abs(void *wcombo, unsigned int abs_type, int abs_data)
119{
120 input_report_abs(get_input_dev((struct wacom_combo *)wcombo), abs_type, abs_data);
Ping Cheng3bea7332006-07-13 18:01:36 -0700121}
122
123void wacom_report_rel(void *wcombo, unsigned int rel_type, int rel_data)
124{
125 input_report_rel(get_input_dev((struct wacom_combo *)wcombo), rel_type, rel_data);
Ping Cheng3bea7332006-07-13 18:01:36 -0700126}
127
128void wacom_input_event(void *wcombo, unsigned int type, unsigned int code, int value)
129{
130 input_event(get_input_dev((struct wacom_combo *)wcombo), type, code, value);
Ping Cheng3bea7332006-07-13 18:01:36 -0700131}
132
133__u16 wacom_be16_to_cpu(unsigned char *data)
134{
135 __u16 value;
136 value = be16_to_cpu(*(__be16 *) data);
137 return value;
138}
139
140__u16 wacom_le16_to_cpu(unsigned char *data)
141{
142 __u16 value;
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700143 value = le16_to_cpu(*(__le16 *) data);
Ping Cheng3bea7332006-07-13 18:01:36 -0700144 return value;
145}
146
Ping Cheng3bea7332006-07-13 18:01:36 -0700147void wacom_input_sync(void *wcombo)
148{
149 input_sync(get_input_dev((struct wacom_combo *)wcombo));
Ping Cheng3bea7332006-07-13 18:01:36 -0700150}
151
152static int wacom_open(struct input_dev *dev)
153{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400154 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700155
Oliver Neukume7224092008-04-15 01:31:57 -0400156 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700157
Oliver Neukume7224092008-04-15 01:31:57 -0400158 wacom->irq->dev = wacom->usbdev;
159
160 if (usb_autopm_get_interface(wacom->intf) < 0) {
161 mutex_unlock(&wacom->lock);
162 return -EIO;
163 }
164
165 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
166 usb_autopm_put_interface(wacom->intf);
167 mutex_unlock(&wacom->lock);
168 return -EIO;
169 }
170
171 wacom->open = 1;
172 wacom->intf->needs_remote_wakeup = 1;
173
174 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700175 return 0;
176}
177
178static void wacom_close(struct input_dev *dev)
179{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400180 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700181
Oliver Neukume7224092008-04-15 01:31:57 -0400182 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700183 usb_kill_urb(wacom->irq);
Oliver Neukume7224092008-04-15 01:31:57 -0400184 wacom->open = 0;
185 wacom->intf->needs_remote_wakeup = 0;
186 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700187}
188
Ping Cheng7ecfbfd2007-06-14 23:32:48 -0400189void input_dev_mo(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
190{
Ping Cheng545f4e92008-11-24 11:44:27 -0500191 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_1) |
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700192 BIT_MASK(BTN_5);
Ping Cheng7ecfbfd2007-06-14 23:32:48 -0400193 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
194}
195
Ping Cheng3bea7332006-07-13 18:01:36 -0700196void input_dev_g4(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
197{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700198 input_dev->evbit[0] |= BIT_MASK(EV_MSC);
199 input_dev->mscbit[0] |= BIT_MASK(MSC_SERIAL);
200 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_FINGER);
Ping Cheng545f4e92008-11-24 11:44:27 -0500201 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_0) |
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700202 BIT_MASK(BTN_4);
Ping Cheng3bea7332006-07-13 18:01:36 -0700203}
204
205void input_dev_g(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
206{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700207 input_dev->evbit[0] |= BIT_MASK(EV_REL);
208 input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);
Ping Cheng545f4e92008-11-24 11:44:27 -0500209 input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_LEFT) |
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700210 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
211 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_RUBBER) |
Ping Chengec67bbe2009-12-15 00:35:24 -0800212 BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_STYLUS) |
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700213 BIT_MASK(BTN_TOOL_MOUSE) | BIT_MASK(BTN_STYLUS2);
Ping Cheng3bea7332006-07-13 18:01:36 -0700214 input_set_abs_params(input_dev, ABS_DISTANCE, 0, wacom_wac->features->distance_max, 0, 0);
215}
216
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700217void input_dev_i3s(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
Ping Cheng3bea7332006-07-13 18:01:36 -0700218{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700219 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_FINGER);
Ping Cheng545f4e92008-11-24 11:44:27 -0500220 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_0) |
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700221 BIT_MASK(BTN_1) | BIT_MASK(BTN_2) | BIT_MASK(BTN_3);
Ping Cheng071e0a22006-12-05 17:09:51 -0800222 input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
Ping Cheng0e1763f2008-03-13 16:46:46 -0400223 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700224}
225
226void input_dev_i3(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
227{
Ping Cheng545f4e92008-11-24 11:44:27 -0500228 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_4) |
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700229 BIT_MASK(BTN_5) | BIT_MASK(BTN_6) | BIT_MASK(BTN_7);
Ping Cheng071e0a22006-12-05 17:09:51 -0800230 input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
Ping Cheng3bea7332006-07-13 18:01:36 -0700231}
232
Ping Cheng6f660f12009-05-08 18:30:33 -0700233void input_dev_i4s(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
234{
235 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_FINGER);
236 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_0) | BIT_MASK(BTN_1) | BIT_MASK(BTN_2) | BIT_MASK(BTN_3);
237 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_4) | BIT_MASK(BTN_5) | BIT_MASK(BTN_6);
238 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
239}
240
241void input_dev_i4(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
242{
243 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_7) | BIT_MASK(BTN_8);
244}
245
Ping Cheng0e1763f2008-03-13 16:46:46 -0400246void input_dev_bee(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
247{
Ping Cheng545f4e92008-11-24 11:44:27 -0500248 input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_8) | BIT_MASK(BTN_9);
Ping Cheng0e1763f2008-03-13 16:46:46 -0400249}
250
Ping Cheng3bea7332006-07-13 18:01:36 -0700251void input_dev_i(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
252{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700253 input_dev->evbit[0] |= BIT_MASK(EV_MSC) | BIT_MASK(EV_REL);
254 input_dev->mscbit[0] |= BIT_MASK(MSC_SERIAL);
255 input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);
Ping Cheng545f4e92008-11-24 11:44:27 -0500256 input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_LEFT) |
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700257 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE) |
258 BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
259 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_RUBBER) |
Ping Chengec67bbe2009-12-15 00:35:24 -0800260 BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_STYLUS) |
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700261 BIT_MASK(BTN_TOOL_MOUSE) | BIT_MASK(BTN_TOOL_BRUSH) |
262 BIT_MASK(BTN_TOOL_PENCIL) | BIT_MASK(BTN_TOOL_AIRBRUSH) |
263 BIT_MASK(BTN_TOOL_LENS) | BIT_MASK(BTN_STYLUS2);
Ping Cheng3bea7332006-07-13 18:01:36 -0700264 input_set_abs_params(input_dev, ABS_DISTANCE, 0, wacom_wac->features->distance_max, 0, 0);
265 input_set_abs_params(input_dev, ABS_WHEEL, 0, 1023, 0, 0);
266 input_set_abs_params(input_dev, ABS_TILT_X, 0, 127, 0, 0);
267 input_set_abs_params(input_dev, ABS_TILT_Y, 0, 127, 0, 0);
268 input_set_abs_params(input_dev, ABS_RZ, -900, 899, 0, 0);
269 input_set_abs_params(input_dev, ABS_THROTTLE, -1023, 1023, 0, 0);
270}
271
272void input_dev_pl(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
273{
Ping Chengec67bbe2009-12-15 00:35:24 -0800274 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_PEN) |
275 BIT_MASK(BTN_STYLUS) | BIT_MASK(BTN_STYLUS2);
Ping Cheng3bea7332006-07-13 18:01:36 -0700276}
277
278void input_dev_pt(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
279{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700280 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_RUBBER);
Ping Cheng3bea7332006-07-13 18:01:36 -0700281}
282
Ping Chengec67bbe2009-12-15 00:35:24 -0800283void input_dev_tpc(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
284{
285 if (wacom_wac->features->device_type == BTN_TOOL_DOUBLETAP ||
286 wacom_wac->features->device_type == BTN_TOOL_TRIPLETAP) {
287 input_set_abs_params(input_dev, ABS_RX, 0, wacom_wac->features->x_phy, 0, 0);
288 input_set_abs_params(input_dev, ABS_RY, 0, wacom_wac->features->y_phy, 0, 0);
289 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_DOUBLETAP);
290 }
291}
292
293void input_dev_tpc2fg(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
294{
295 if (wacom_wac->features->device_type == BTN_TOOL_TRIPLETAP) {
296 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_TRIPLETAP);
297 input_dev->evbit[0] |= BIT_MASK(EV_MSC);
298 input_dev->mscbit[0] |= BIT_MASK(MSC_SERIAL);
299 }
300}
301
Ping Cheng545f4e92008-11-24 11:44:27 -0500302static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800303 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500304{
305 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800306 char limit = 0;
307 /* result has to be defined as int for some devices */
308 int result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500309 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
310 unsigned char *report;
311
312 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
313 if (!report)
314 return -ENOMEM;
315
316 /* retrive report descriptors */
317 do {
318 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
319 USB_REQ_GET_DESCRIPTOR,
320 USB_RECIP_INTERFACE | USB_DIR_IN,
321 HID_DEVICET_REPORT << 8,
322 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
323 report,
324 hid_desc->wDescriptorLength,
325 5000); /* 5 secs */
326 } while (result < 0 && limit++ < 5);
327
Ping Cheng384318e2009-04-28 07:49:54 -0700328 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500329 if (result < 0)
330 goto out;
331
332 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
333
334 switch (report[i]) {
335 case HID_USAGE_PAGE:
336 switch (report[i + 1]) {
337 case HID_USAGE_PAGE_DIGITIZER:
338 usage = WCM_DIGITIZER;
339 i++;
340 break;
341
342 case HID_USAGE_PAGE_DESKTOP:
343 usage = WCM_DESKTOP;
344 i++;
345 break;
346 }
347 break;
348
349 case HID_USAGE:
350 switch (report[i + 1]) {
351 case HID_USAGE_X:
352 if (usage == WCM_DESKTOP) {
353 if (finger) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800354 features->device_type = BTN_TOOL_DOUBLETAP;
355 if (features->type == TABLETPC2FG) {
356 /* need to reset back */
357 features->pktlen = WACOM_PKGLEN_TPC2FG;
358 features->device_type = BTN_TOOL_TRIPLETAP;
359 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500360 features->x_max =
Ping Chengec67bbe2009-12-15 00:35:24 -0800361 wacom_le16_to_cpu(&report[i + 3]);
362 features->x_phy =
Ping Cheng545f4e92008-11-24 11:44:27 -0500363 wacom_le16_to_cpu(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800364 features->unit = report[i + 9];
365 features->unitExpo = report[i + 11];
366 i += 12;
Ping Cheng545f4e92008-11-24 11:44:27 -0500367 } else if (pen) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800368 /* penabled only accepts exact bytes of data */
369 if (features->type == TABLETPC2FG)
370 features->pktlen = WACOM_PKGLEN_PENABLED;
371 features->device_type = BTN_TOOL_PEN;
Ping Cheng545f4e92008-11-24 11:44:27 -0500372 features->x_max =
373 wacom_le16_to_cpu(&report[i + 3]);
374 i += 4;
375 }
376 } else if (usage == WCM_DIGITIZER) {
377 /* max pressure isn't reported
378 features->pressure_max = (unsigned short)
379 (report[i+4] << 8 | report[i + 3]);
380 */
381 features->pressure_max = 255;
382 i += 4;
383 }
384 break;
385
386 case HID_USAGE_Y:
Ping Chengec67bbe2009-12-15 00:35:24 -0800387 if (usage == WCM_DESKTOP) {
388 if (finger) {
389 features->device_type = BTN_TOOL_DOUBLETAP;
390 if (features->type == TABLETPC2FG) {
391 /* need to reset back */
392 features->pktlen = WACOM_PKGLEN_TPC2FG;
393 features->device_type = BTN_TOOL_TRIPLETAP;
394 features->y_max =
395 wacom_le16_to_cpu(&report[i + 3]);
396 features->y_phy =
397 wacom_le16_to_cpu(&report[i + 6]);
398 i += 7;
399 } else {
400 features->y_max =
401 features->x_max;
402 features->y_phy =
403 wacom_le16_to_cpu(&report[i + 3]);
404 i += 4;
405 }
406 } else if (pen) {
407 /* penabled only accepts exact bytes of data */
408 if (features->type == TABLETPC2FG)
409 features->pktlen = WACOM_PKGLEN_PENABLED;
410 features->device_type = BTN_TOOL_PEN;
411 features->y_max =
412 wacom_le16_to_cpu(&report[i + 3]);
413 i += 4;
414 }
415 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500416 break;
417
418 case HID_USAGE_FINGER:
419 finger = 1;
420 i++;
421 break;
422
423 case HID_USAGE_STYLUS:
424 pen = 1;
425 i++;
426 break;
427
428 case HID_USAGE_UNDEFINED:
429 if (usage == WCM_DESKTOP && finger) /* capacity */
430 features->pressure_max =
431 wacom_le16_to_cpu(&report[i + 3]);
432 i += 4;
433 break;
434 }
435 break;
436
437 case HID_COLLECTION:
Ping Chengec67bbe2009-12-15 00:35:24 -0800438 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500439 finger = usage = 0;
440 break;
441 }
442 }
443
Ping Cheng545f4e92008-11-24 11:44:27 -0500444 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700445 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500446 kfree(report);
447 return result;
448}
449
Ping Chengec67bbe2009-12-15 00:35:24 -0800450static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700451{
452 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800453 int limit = 0, report_id = 2;
454 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700455
456 rep_data = kmalloc(2, GFP_KERNEL);
457 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800458 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700459
Ping Chengec67bbe2009-12-15 00:35:24 -0800460 /* ask to report tablet data if it is 2FGT or not a Tablet PC */
461 if (features->device_type == BTN_TOOL_TRIPLETAP) {
462 do {
463 rep_data[0] = 3;
464 rep_data[1] = 4;
465 report_id = 3;
466 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
467 report_id, rep_data, 2);
468 if (error >= 0)
469 error = usb_get_report(intf,
470 WAC_HID_FEATURE_REPORT, report_id,
471 rep_data, 3);
472 } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
473 } else if (features->type != TABLETPC && features->type != TABLETPC2FG) {
474 do {
475 rep_data[0] = 2;
476 rep_data[1] = 2;
477 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
478 report_id, rep_data, 2);
479 if (error >= 0)
480 error = usb_get_report(intf,
481 WAC_HID_FEATURE_REPORT, report_id,
482 rep_data, 2);
483 } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
484 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700485
486 kfree(rep_data);
487
488 return error < 0 ? error : 0;
489}
490
Ping Chengec67bbe2009-12-15 00:35:24 -0800491static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
492 struct wacom_features *features)
493{
494 int error = 0;
495 struct usb_host_interface *interface = intf->cur_altsetting;
496 struct hid_descriptor *hid_desc;
497
498 /* default device to penabled */
499 features->device_type = BTN_TOOL_PEN;
500
501 /* only Tablet PCs need to retrieve the info */
502 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG))
503 goto out;
504
505 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
506 if (usb_get_extra_descriptor(&interface->endpoint[0],
507 HID_DEVICET_REPORT, &hid_desc)) {
508 printk("wacom: can not retrieve extra class descriptor\n");
509 error = 1;
510 goto out;
511 }
512 }
513 error = wacom_parse_hid(intf, hid_desc, features);
514 if (error)
515 goto out;
516
517 /* touch device found but size is not defined. use default */
518 if (features->device_type == BTN_TOOL_DOUBLETAP && !features->x_max) {
519 features->x_max = 1023;
520 features->y_max = 1023;
521 }
522
523 out:
524 return error;
525}
526
Ping Cheng3bea7332006-07-13 18:01:36 -0700527static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
528{
529 struct usb_device *dev = interface_to_usbdev(intf);
530 struct usb_endpoint_descriptor *endpoint;
531 struct wacom *wacom;
532 struct wacom_wac *wacom_wac;
Bastian Blankb036f6f2010-02-10 23:06:23 -0800533 struct wacom_features *features = (void *)id->driver_info;
Ping Cheng3bea7332006-07-13 18:01:36 -0700534 struct input_dev *input_dev;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400535 int error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700536
Bastian Blankb036f6f2010-02-10 23:06:23 -0800537 if (!features)
538 return -EINVAL;
539
Ping Cheng3bea7332006-07-13 18:01:36 -0700540 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
541 wacom_wac = kzalloc(sizeof(struct wacom_wac), GFP_KERNEL);
542 input_dev = input_allocate_device();
543 if (!wacom || !input_dev || !wacom_wac)
544 goto fail1;
545
Ping Chengec67bbe2009-12-15 00:35:24 -0800546 wacom_wac->data = usb_buffer_alloc(dev, WACOM_PKGLEN_MAX, GFP_KERNEL, &wacom->data_dma);
Ping Cheng3bea7332006-07-13 18:01:36 -0700547 if (!wacom_wac->data)
548 goto fail1;
549
550 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
551 if (!wacom->irq)
552 goto fail2;
553
554 wacom->usbdev = dev;
555 wacom->dev = input_dev;
Oliver Neukume7224092008-04-15 01:31:57 -0400556 wacom->intf = intf;
557 mutex_init(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700558 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
559 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
560
Bastian Blankb036f6f2010-02-10 23:06:23 -0800561 wacom_wac->features = features;
Ping Chengec67bbe2009-12-15 00:35:24 -0800562 BUG_ON(features->pktlen > WACOM_PKGLEN_MAX);
Ping Cheng3bea7332006-07-13 18:01:36 -0700563
564 input_dev->name = wacom_wac->features->name;
565 wacom->wacom_wac = wacom_wac;
566 usb_to_input_id(dev, &input_dev->id);
567
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400568 input_dev->dev.parent = &intf->dev;
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400569
570 input_set_drvdata(input_dev, wacom);
571
Ping Cheng3bea7332006-07-13 18:01:36 -0700572 input_dev->open = wacom_open;
573 input_dev->close = wacom_close;
574
Ping Cheng545f4e92008-11-24 11:44:27 -0500575 endpoint = &intf->cur_altsetting->endpoint[0].desc;
576
Ping Chengec67bbe2009-12-15 00:35:24 -0800577 /* Retrieve the physical and logical size for OEM devices */
578 error = wacom_retrieve_hid_descriptor(intf, features);
579 if (error)
580 goto fail2;
Ping Cheng545f4e92008-11-24 11:44:27 -0500581
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700582 input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
Ping Chengec67bbe2009-12-15 00:35:24 -0800583 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOUCH);
584
Ping Cheng545f4e92008-11-24 11:44:27 -0500585 input_set_abs_params(input_dev, ABS_X, 0, features->x_max, 4, 0);
586 input_set_abs_params(input_dev, ABS_Y, 0, features->y_max, 4, 0);
587 input_set_abs_params(input_dev, ABS_PRESSURE, 0, features->pressure_max, 0, 0);
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700588 input_dev->absbit[BIT_WORD(ABS_MISC)] |= BIT_MASK(ABS_MISC);
Ping Cheng3bea7332006-07-13 18:01:36 -0700589
590 wacom_init_input_dev(input_dev, wacom_wac);
591
Ping Cheng3bea7332006-07-13 18:01:36 -0700592 usb_fill_int_urb(wacom->irq, dev,
593 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -0800594 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700595 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700596 wacom->irq->transfer_dma = wacom->data_dma;
597 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
598
Dmitry Torokhov50141862007-04-12 01:33:39 -0400599 error = input_register_device(wacom->dev);
600 if (error)
601 goto fail3;
Ping Cheng3bea7332006-07-13 18:01:36 -0700602
Ping Chengec67bbe2009-12-15 00:35:24 -0800603 /* Note that if query fails it is not a hard failure */
604 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -0700605
606 usb_set_intfdata(intf, wacom);
607 return 0;
608
Dmitry Torokhov50141862007-04-12 01:33:39 -0400609 fail3: usb_free_urb(wacom->irq);
Ping Chengec67bbe2009-12-15 00:35:24 -0800610 fail2: usb_buffer_free(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400611 fail1: input_free_device(input_dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700612 kfree(wacom);
613 kfree(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400614 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700615}
616
617static void wacom_disconnect(struct usb_interface *intf)
618{
Oliver Neukume7224092008-04-15 01:31:57 -0400619 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700620
621 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -0400622
623 usb_kill_urb(wacom->irq);
624 input_unregister_device(wacom->dev);
625 usb_free_urb(wacom->irq);
Ping Chengec67bbe2009-12-15 00:35:24 -0800626 usb_buffer_free(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Ping Cheng545f4e92008-11-24 11:44:27 -0500627 wacom->wacom_wac->data, wacom->data_dma);
Oliver Neukume7224092008-04-15 01:31:57 -0400628 kfree(wacom->wacom_wac);
629 kfree(wacom);
630}
631
632static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
633{
634 struct wacom *wacom = usb_get_intfdata(intf);
635
636 mutex_lock(&wacom->lock);
637 usb_kill_urb(wacom->irq);
638 mutex_unlock(&wacom->lock);
639
640 return 0;
641}
642
643static int wacom_resume(struct usb_interface *intf)
644{
645 struct wacom *wacom = usb_get_intfdata(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800646 struct wacom_features *features = wacom->wacom_wac->features;
Oliver Neukume7224092008-04-15 01:31:57 -0400647 int rv;
648
649 mutex_lock(&wacom->lock);
Ping Cheng232f5692009-12-15 00:35:24 -0800650 if (wacom->open) {
Oliver Neukume7224092008-04-15 01:31:57 -0400651 rv = usb_submit_urb(wacom->irq, GFP_NOIO);
Ping Chengec67bbe2009-12-15 00:35:24 -0800652 /* switch to wacom mode if needed */
653 if (!wacom_retrieve_hid_descriptor(intf, features))
654 wacom_query_tablet_data(intf, features);
Ping Cheng232f5692009-12-15 00:35:24 -0800655 } else
Oliver Neukume7224092008-04-15 01:31:57 -0400656 rv = 0;
657 mutex_unlock(&wacom->lock);
658
659 return rv;
660}
661
662static int wacom_reset_resume(struct usb_interface *intf)
663{
664 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700665}
666
667static struct usb_driver wacom_driver = {
668 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -0800669 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -0700670 .probe = wacom_probe,
671 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -0400672 .suspend = wacom_suspend,
673 .resume = wacom_resume,
674 .reset_resume = wacom_reset_resume,
675 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -0700676};
677
678static int __init wacom_init(void)
679{
680 int result;
Bastian Blankb036f6f2010-02-10 23:06:23 -0800681
Ping Cheng3bea7332006-07-13 18:01:36 -0700682 result = usb_register(&wacom_driver);
683 if (result == 0)
Greg Kroah-Hartman899ef6e2008-08-18 13:21:04 -0700684 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
685 DRIVER_DESC "\n");
Ping Cheng3bea7332006-07-13 18:01:36 -0700686 return result;
687}
688
689static void __exit wacom_exit(void)
690{
691 usb_deregister(&wacom_driver);
692}
693
694module_init(wacom_init);
695module_exit(wacom_exit);