blob: c37e22b968d95092a7ae3630e838d6b52e3d0d2b [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
Ping Cheng3bea7332006-07-13 18:01:36 -070014#include "wacom_wac.h"
Dmitry Torokhov51269fe2010-03-19 22:18:15 -070015#include "wacom.h"
Ping Cheng3bea7332006-07-13 18:01:36 -070016
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
Adrian Bunk54c9b2262006-11-20 03:23:58 +010073static void wacom_sys_irq(struct urb *urb)
Ping Cheng3bea7332006-07-13 18:01:36 -070074{
75 struct wacom *wacom = urb->context;
Ping Cheng3bea7332006-07-13 18:01:36 -070076 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 */
Harvey Harrisonea3e6c52008-05-05 11:36:18 -040086 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -070087 return;
88 default:
Harvey Harrisonea3e6c52008-05-05 11:36:18 -040089 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -070090 goto exit;
91 }
92
Dmitry Torokhov95dd3b32010-03-19 22:18:15 -070093 wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
Ping Cheng3bea7332006-07-13 18:01:36 -070094
95 exit:
Oliver Neukume7224092008-04-15 01:31:57 -040096 usb_mark_last_busy(wacom->usbdev);
Dmitry Torokhov73a97f42010-03-19 22:18:15 -070097 retval = usb_submit_urb(urb, GFP_ATOMIC);
Ping Cheng3bea7332006-07-13 18:01:36 -070098 if (retval)
99 err ("%s - usb_submit_urb failed with result %d",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400100 __func__, retval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700101}
102
Ping Cheng3bea7332006-07-13 18:01:36 -0700103__u16 wacom_be16_to_cpu(unsigned char *data)
104{
105 __u16 value;
106 value = be16_to_cpu(*(__be16 *) data);
107 return value;
108}
109
110__u16 wacom_le16_to_cpu(unsigned char *data)
111{
112 __u16 value;
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700113 value = le16_to_cpu(*(__le16 *) data);
Ping Cheng3bea7332006-07-13 18:01:36 -0700114 return value;
115}
116
Ping Cheng3bea7332006-07-13 18:01:36 -0700117static int wacom_open(struct input_dev *dev)
118{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400119 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700120
Oliver Neukume7224092008-04-15 01:31:57 -0400121 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700122
Oliver Neukume7224092008-04-15 01:31:57 -0400123 wacom->irq->dev = wacom->usbdev;
124
125 if (usb_autopm_get_interface(wacom->intf) < 0) {
126 mutex_unlock(&wacom->lock);
127 return -EIO;
128 }
129
130 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
131 usb_autopm_put_interface(wacom->intf);
132 mutex_unlock(&wacom->lock);
133 return -EIO;
134 }
135
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700136 wacom->open = true;
Oliver Neukume7224092008-04-15 01:31:57 -0400137 wacom->intf->needs_remote_wakeup = 1;
138
139 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700140 return 0;
141}
142
143static void wacom_close(struct input_dev *dev)
144{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400145 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700146
Oliver Neukume7224092008-04-15 01:31:57 -0400147 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700148 usb_kill_urb(wacom->irq);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700149 wacom->open = false;
Oliver Neukume7224092008-04-15 01:31:57 -0400150 wacom->intf->needs_remote_wakeup = 0;
151 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700152}
153
Ping Cheng545f4e92008-11-24 11:44:27 -0500154static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800155 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500156{
157 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800158 char limit = 0;
159 /* result has to be defined as int for some devices */
160 int result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500161 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
162 unsigned char *report;
163
164 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
165 if (!report)
166 return -ENOMEM;
167
168 /* retrive report descriptors */
169 do {
170 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
171 USB_REQ_GET_DESCRIPTOR,
172 USB_RECIP_INTERFACE | USB_DIR_IN,
173 HID_DEVICET_REPORT << 8,
174 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
175 report,
176 hid_desc->wDescriptorLength,
177 5000); /* 5 secs */
178 } while (result < 0 && limit++ < 5);
179
Ping Cheng384318e2009-04-28 07:49:54 -0700180 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500181 if (result < 0)
182 goto out;
183
184 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
185
186 switch (report[i]) {
187 case HID_USAGE_PAGE:
188 switch (report[i + 1]) {
189 case HID_USAGE_PAGE_DIGITIZER:
190 usage = WCM_DIGITIZER;
191 i++;
192 break;
193
194 case HID_USAGE_PAGE_DESKTOP:
195 usage = WCM_DESKTOP;
196 i++;
197 break;
198 }
199 break;
200
201 case HID_USAGE:
202 switch (report[i + 1]) {
203 case HID_USAGE_X:
204 if (usage == WCM_DESKTOP) {
205 if (finger) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800206 features->device_type = BTN_TOOL_DOUBLETAP;
207 if (features->type == TABLETPC2FG) {
208 /* need to reset back */
209 features->pktlen = WACOM_PKGLEN_TPC2FG;
210 features->device_type = BTN_TOOL_TRIPLETAP;
211 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500212 features->x_max =
Ping Chengec67bbe2009-12-15 00:35:24 -0800213 wacom_le16_to_cpu(&report[i + 3]);
214 features->x_phy =
Ping Cheng545f4e92008-11-24 11:44:27 -0500215 wacom_le16_to_cpu(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800216 features->unit = report[i + 9];
217 features->unitExpo = report[i + 11];
218 i += 12;
Ping Cheng545f4e92008-11-24 11:44:27 -0500219 } else if (pen) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800220 /* penabled only accepts exact bytes of data */
221 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800222 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800223 features->device_type = BTN_TOOL_PEN;
Ping Cheng545f4e92008-11-24 11:44:27 -0500224 features->x_max =
225 wacom_le16_to_cpu(&report[i + 3]);
226 i += 4;
227 }
228 } else if (usage == WCM_DIGITIZER) {
229 /* max pressure isn't reported
230 features->pressure_max = (unsigned short)
231 (report[i+4] << 8 | report[i + 3]);
232 */
233 features->pressure_max = 255;
234 i += 4;
235 }
236 break;
237
238 case HID_USAGE_Y:
Ping Chengec67bbe2009-12-15 00:35:24 -0800239 if (usage == WCM_DESKTOP) {
240 if (finger) {
241 features->device_type = BTN_TOOL_DOUBLETAP;
242 if (features->type == TABLETPC2FG) {
243 /* need to reset back */
244 features->pktlen = WACOM_PKGLEN_TPC2FG;
245 features->device_type = BTN_TOOL_TRIPLETAP;
246 features->y_max =
247 wacom_le16_to_cpu(&report[i + 3]);
248 features->y_phy =
249 wacom_le16_to_cpu(&report[i + 6]);
250 i += 7;
251 } else {
252 features->y_max =
253 features->x_max;
254 features->y_phy =
255 wacom_le16_to_cpu(&report[i + 3]);
256 i += 4;
257 }
258 } else if (pen) {
259 /* penabled only accepts exact bytes of data */
260 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800261 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800262 features->device_type = BTN_TOOL_PEN;
263 features->y_max =
264 wacom_le16_to_cpu(&report[i + 3]);
265 i += 4;
266 }
267 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500268 break;
269
270 case HID_USAGE_FINGER:
271 finger = 1;
272 i++;
273 break;
274
275 case HID_USAGE_STYLUS:
276 pen = 1;
277 i++;
278 break;
279
280 case HID_USAGE_UNDEFINED:
281 if (usage == WCM_DESKTOP && finger) /* capacity */
282 features->pressure_max =
283 wacom_le16_to_cpu(&report[i + 3]);
284 i += 4;
285 break;
286 }
287 break;
288
289 case HID_COLLECTION:
Ping Chengec67bbe2009-12-15 00:35:24 -0800290 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500291 finger = usage = 0;
292 break;
293 }
294 }
295
Ping Cheng545f4e92008-11-24 11:44:27 -0500296 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700297 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500298 kfree(report);
299 return result;
300}
301
Ping Chengec67bbe2009-12-15 00:35:24 -0800302static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700303{
304 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800305 int limit = 0, report_id = 2;
306 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700307
308 rep_data = kmalloc(2, GFP_KERNEL);
309 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800310 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700311
Ping Chengec67bbe2009-12-15 00:35:24 -0800312 /* ask to report tablet data if it is 2FGT or not a Tablet PC */
313 if (features->device_type == BTN_TOOL_TRIPLETAP) {
314 do {
315 rep_data[0] = 3;
316 rep_data[1] = 4;
317 report_id = 3;
318 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
319 report_id, rep_data, 2);
320 if (error >= 0)
321 error = usb_get_report(intf,
322 WAC_HID_FEATURE_REPORT, report_id,
323 rep_data, 3);
324 } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
325 } else if (features->type != TABLETPC && features->type != TABLETPC2FG) {
326 do {
327 rep_data[0] = 2;
328 rep_data[1] = 2;
329 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
330 report_id, rep_data, 2);
331 if (error >= 0)
332 error = usb_get_report(intf,
333 WAC_HID_FEATURE_REPORT, report_id,
334 rep_data, 2);
335 } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
336 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700337
338 kfree(rep_data);
339
340 return error < 0 ? error : 0;
341}
342
Ping Chengec67bbe2009-12-15 00:35:24 -0800343static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
344 struct wacom_features *features)
345{
346 int error = 0;
347 struct usb_host_interface *interface = intf->cur_altsetting;
348 struct hid_descriptor *hid_desc;
349
350 /* default device to penabled */
351 features->device_type = BTN_TOOL_PEN;
352
353 /* only Tablet PCs need to retrieve the info */
354 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG))
355 goto out;
356
357 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
358 if (usb_get_extra_descriptor(&interface->endpoint[0],
359 HID_DEVICET_REPORT, &hid_desc)) {
360 printk("wacom: can not retrieve extra class descriptor\n");
361 error = 1;
362 goto out;
363 }
364 }
365 error = wacom_parse_hid(intf, hid_desc, features);
366 if (error)
367 goto out;
368
369 /* touch device found but size is not defined. use default */
370 if (features->device_type == BTN_TOOL_DOUBLETAP && !features->x_max) {
371 features->x_max = 1023;
372 features->y_max = 1023;
373 }
374
375 out:
376 return error;
377}
378
Ping Cheng4492eff2010-03-19 22:18:15 -0700379struct wacom_usbdev_data {
380 struct list_head list;
381 struct kref kref;
382 struct usb_device *dev;
383 struct wacom_shared shared;
384};
385
386static LIST_HEAD(wacom_udev_list);
387static DEFINE_MUTEX(wacom_udev_list_lock);
388
389static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
390{
391 struct wacom_usbdev_data *data;
392
393 list_for_each_entry(data, &wacom_udev_list, list) {
394 if (data->dev == dev) {
395 kref_get(&data->kref);
396 return data;
397 }
398 }
399
400 return NULL;
401}
402
403static int wacom_add_shared_data(struct wacom_wac *wacom,
404 struct usb_device *dev)
405{
406 struct wacom_usbdev_data *data;
407 int retval = 0;
408
409 mutex_lock(&wacom_udev_list_lock);
410
411 data = wacom_get_usbdev_data(dev);
412 if (!data) {
413 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
414 if (!data) {
415 retval = -ENOMEM;
416 goto out;
417 }
418
419 kref_init(&data->kref);
420 data->dev = dev;
421 list_add_tail(&data->list, &wacom_udev_list);
422 }
423
424 wacom->shared = &data->shared;
425
426out:
427 mutex_unlock(&wacom_udev_list_lock);
428 return retval;
429}
430
431static void wacom_release_shared_data(struct kref *kref)
432{
433 struct wacom_usbdev_data *data =
434 container_of(kref, struct wacom_usbdev_data, kref);
435
436 mutex_lock(&wacom_udev_list_lock);
437 list_del(&data->list);
438 mutex_unlock(&wacom_udev_list_lock);
439
440 kfree(data);
441}
442
443static void wacom_remove_shared_data(struct wacom_wac *wacom)
444{
445 struct wacom_usbdev_data *data;
446
447 if (wacom->shared) {
448 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
449 kref_put(&data->kref, wacom_release_shared_data);
450 wacom->shared = NULL;
451 }
452}
453
Ping Cheng3bea7332006-07-13 18:01:36 -0700454static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
455{
456 struct usb_device *dev = interface_to_usbdev(intf);
457 struct usb_endpoint_descriptor *endpoint;
458 struct wacom *wacom;
459 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800460 struct wacom_features *features;
Ping Cheng3bea7332006-07-13 18:01:36 -0700461 struct input_dev *input_dev;
Jason Childse33da8a2010-02-17 22:38:31 -0800462 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700463
Jason Childse33da8a2010-02-17 22:38:31 -0800464 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -0800465 return -EINVAL;
466
Ping Cheng3bea7332006-07-13 18:01:36 -0700467 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Ping Cheng3bea7332006-07-13 18:01:36 -0700468 input_dev = input_allocate_device();
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700469 if (!wacom || !input_dev) {
Jason Childse33da8a2010-02-17 22:38:31 -0800470 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700471 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800472 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700473
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700474 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800475 wacom_wac->features = *((struct wacom_features *)id->driver_info);
476 features = &wacom_wac->features;
477 if (features->pktlen > WACOM_PKGLEN_MAX) {
478 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -0700479 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800480 }
481
482 wacom_wac->data = usb_buffer_alloc(dev, WACOM_PKGLEN_MAX,
483 GFP_KERNEL, &wacom->data_dma);
484 if (!wacom_wac->data) {
485 error = -ENOMEM;
486 goto fail1;
487 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700488
489 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -0800490 if (!wacom->irq) {
491 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700492 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -0800493 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700494
495 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -0400496 wacom->intf = intf;
497 mutex_init(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700498 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
499 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
500
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700501 wacom_wac->input = input_dev;
Ping Cheng3bea7332006-07-13 18:01:36 -0700502
Ping Cheng545f4e92008-11-24 11:44:27 -0500503 endpoint = &intf->cur_altsetting->endpoint[0].desc;
504
Ping Chengec67bbe2009-12-15 00:35:24 -0800505 /* Retrieve the physical and logical size for OEM devices */
506 error = wacom_retrieve_hid_descriptor(intf, features);
507 if (error)
508 goto fail2;
Ping Cheng545f4e92008-11-24 11:44:27 -0500509
Ping Cheng49b764a2010-02-20 00:53:49 -0800510 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
511
512 if (features->type == TABLETPC || features->type == TABLETPC2FG) {
513 /* Append the device type to the name */
514 strlcat(wacom_wac->name,
515 features->device_type == BTN_TOOL_PEN ?
516 " Pen" : " Finger",
517 sizeof(wacom_wac->name));
Ping Cheng4492eff2010-03-19 22:18:15 -0700518
519 error = wacom_add_shared_data(wacom_wac, dev);
520 if (error)
521 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -0800522 }
523
524 input_dev->name = wacom_wac->name;
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700525 input_dev->name = wacom_wac->name;
526 input_dev->dev.parent = &intf->dev;
527 input_dev->open = wacom_open;
528 input_dev->close = wacom_close;
529 usb_to_input_id(dev, &input_dev->id);
530 input_set_drvdata(input_dev, wacom);
Jason Childse33da8a2010-02-17 22:38:31 -0800531
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700532 wacom_setup_input_capabilities(input_dev, wacom_wac);
Ping Cheng3bea7332006-07-13 18:01:36 -0700533
Ping Cheng3bea7332006-07-13 18:01:36 -0700534 usb_fill_int_urb(wacom->irq, dev,
535 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -0800536 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700537 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700538 wacom->irq->transfer_dma = wacom->data_dma;
539 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
540
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700541 error = input_register_device(input_dev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400542 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -0700543 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -0700544
Ping Chengec67bbe2009-12-15 00:35:24 -0800545 /* Note that if query fails it is not a hard failure */
546 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -0700547
548 usb_set_intfdata(intf, wacom);
549 return 0;
550
Ping Cheng4492eff2010-03-19 22:18:15 -0700551 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400552 fail3: usb_free_urb(wacom->irq);
Ping Chengec67bbe2009-12-15 00:35:24 -0800553 fail2: usb_buffer_free(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400554 fail1: input_free_device(input_dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700555 kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400556 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700557}
558
559static void wacom_disconnect(struct usb_interface *intf)
560{
Oliver Neukume7224092008-04-15 01:31:57 -0400561 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700562
563 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -0400564
565 usb_kill_urb(wacom->irq);
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700566 input_unregister_device(wacom->wacom_wac.input);
Oliver Neukume7224092008-04-15 01:31:57 -0400567 usb_free_urb(wacom->irq);
Ping Chengec67bbe2009-12-15 00:35:24 -0800568 usb_buffer_free(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700569 wacom->wacom_wac.data, wacom->data_dma);
570 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -0400571 kfree(wacom);
572}
573
574static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
575{
576 struct wacom *wacom = usb_get_intfdata(intf);
577
578 mutex_lock(&wacom->lock);
579 usb_kill_urb(wacom->irq);
580 mutex_unlock(&wacom->lock);
581
582 return 0;
583}
584
585static int wacom_resume(struct usb_interface *intf)
586{
587 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700588 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -0400589 int rv;
590
591 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -0700592
593 /* switch to wacom mode first */
594 wacom_query_tablet_data(intf, features);
595
596 if (wacom->open)
Oliver Neukume7224092008-04-15 01:31:57 -0400597 rv = usb_submit_urb(wacom->irq, GFP_NOIO);
Ping Cheng38101472010-04-13 23:07:52 -0700598 else
Oliver Neukume7224092008-04-15 01:31:57 -0400599 rv = 0;
Ping Cheng38101472010-04-13 23:07:52 -0700600
Oliver Neukume7224092008-04-15 01:31:57 -0400601 mutex_unlock(&wacom->lock);
602
603 return rv;
604}
605
606static int wacom_reset_resume(struct usb_interface *intf)
607{
608 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700609}
610
611static struct usb_driver wacom_driver = {
612 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -0800613 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -0700614 .probe = wacom_probe,
615 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -0400616 .suspend = wacom_suspend,
617 .resume = wacom_resume,
618 .reset_resume = wacom_reset_resume,
619 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -0700620};
621
622static int __init wacom_init(void)
623{
624 int result;
Bastian Blankb036f6f2010-02-10 23:06:23 -0800625
Ping Cheng3bea7332006-07-13 18:01:36 -0700626 result = usb_register(&wacom_driver);
627 if (result == 0)
Greg Kroah-Hartman899ef6e2008-08-18 13:21:04 -0700628 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
629 DRIVER_DESC "\n");
Ping Cheng3bea7332006-07-13 18:01:36 -0700630 return result;
631}
632
633static void __exit wacom_exit(void)
634{
635 usb_deregister(&wacom_driver);
636}
637
638module_init(wacom_init);
639module_exit(wacom_exit);