blob: 58bb763b60ddc65b8a578a48ef01b87a175b95f7 [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
73static struct input_dev * get_input_dev(struct wacom_combo *wcombo)
74{
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -070075 return wcombo->wacom->wacom_wac.input;
Ping Cheng3bea7332006-07-13 18:01:36 -070076}
77
Adrian Bunk54c9b222006-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
Dmitry Torokhov73a97f42010-03-19 22:18:15 -0700102 if (wacom_wac_irq(&wacom->wacom_wac, &wcombo))
Ping Cheng3bea7332006-07-13 18:01:36 -0700103 input_sync(get_input_dev(&wcombo));
104
105 exit:
Oliver Neukume7224092008-04-15 01:31:57 -0400106 usb_mark_last_busy(wacom->usbdev);
Dmitry Torokhov73a97f42010-03-19 22:18:15 -0700107 retval = usb_submit_urb(urb, GFP_ATOMIC);
Ping Cheng3bea7332006-07-13 18:01:36 -0700108 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
Ping Cheng3bea7332006-07-13 18:01:36 -0700113__u16 wacom_be16_to_cpu(unsigned char *data)
114{
115 __u16 value;
116 value = be16_to_cpu(*(__be16 *) data);
117 return value;
118}
119
120__u16 wacom_le16_to_cpu(unsigned char *data)
121{
122 __u16 value;
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700123 value = le16_to_cpu(*(__le16 *) data);
Ping Cheng3bea7332006-07-13 18:01:36 -0700124 return value;
125}
126
Ping Cheng3bea7332006-07-13 18:01:36 -0700127static int wacom_open(struct input_dev *dev)
128{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400129 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700130
Oliver Neukume7224092008-04-15 01:31:57 -0400131 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700132
Oliver Neukume7224092008-04-15 01:31:57 -0400133 wacom->irq->dev = wacom->usbdev;
134
135 if (usb_autopm_get_interface(wacom->intf) < 0) {
136 mutex_unlock(&wacom->lock);
137 return -EIO;
138 }
139
140 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
141 usb_autopm_put_interface(wacom->intf);
142 mutex_unlock(&wacom->lock);
143 return -EIO;
144 }
145
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700146 wacom->open = true;
Oliver Neukume7224092008-04-15 01:31:57 -0400147 wacom->intf->needs_remote_wakeup = 1;
148
149 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700150 return 0;
151}
152
153static void wacom_close(struct input_dev *dev)
154{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400155 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700156
Oliver Neukume7224092008-04-15 01:31:57 -0400157 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700158 usb_kill_urb(wacom->irq);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700159 wacom->open = false;
Oliver Neukume7224092008-04-15 01:31:57 -0400160 wacom->intf->needs_remote_wakeup = 0;
161 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700162}
163
Ping Cheng545f4e92008-11-24 11:44:27 -0500164static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800165 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500166{
167 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800168 char limit = 0;
169 /* result has to be defined as int for some devices */
170 int result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500171 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
172 unsigned char *report;
173
174 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
175 if (!report)
176 return -ENOMEM;
177
178 /* retrive report descriptors */
179 do {
180 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
181 USB_REQ_GET_DESCRIPTOR,
182 USB_RECIP_INTERFACE | USB_DIR_IN,
183 HID_DEVICET_REPORT << 8,
184 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
185 report,
186 hid_desc->wDescriptorLength,
187 5000); /* 5 secs */
188 } while (result < 0 && limit++ < 5);
189
Ping Cheng384318e2009-04-28 07:49:54 -0700190 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500191 if (result < 0)
192 goto out;
193
194 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
195
196 switch (report[i]) {
197 case HID_USAGE_PAGE:
198 switch (report[i + 1]) {
199 case HID_USAGE_PAGE_DIGITIZER:
200 usage = WCM_DIGITIZER;
201 i++;
202 break;
203
204 case HID_USAGE_PAGE_DESKTOP:
205 usage = WCM_DESKTOP;
206 i++;
207 break;
208 }
209 break;
210
211 case HID_USAGE:
212 switch (report[i + 1]) {
213 case HID_USAGE_X:
214 if (usage == WCM_DESKTOP) {
215 if (finger) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800216 features->device_type = BTN_TOOL_DOUBLETAP;
217 if (features->type == TABLETPC2FG) {
218 /* need to reset back */
219 features->pktlen = WACOM_PKGLEN_TPC2FG;
220 features->device_type = BTN_TOOL_TRIPLETAP;
221 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500222 features->x_max =
Ping Chengec67bbe2009-12-15 00:35:24 -0800223 wacom_le16_to_cpu(&report[i + 3]);
224 features->x_phy =
Ping Cheng545f4e92008-11-24 11:44:27 -0500225 wacom_le16_to_cpu(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800226 features->unit = report[i + 9];
227 features->unitExpo = report[i + 11];
228 i += 12;
Ping Cheng545f4e92008-11-24 11:44:27 -0500229 } else if (pen) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800230 /* penabled only accepts exact bytes of data */
231 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800232 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800233 features->device_type = BTN_TOOL_PEN;
Ping Cheng545f4e92008-11-24 11:44:27 -0500234 features->x_max =
235 wacom_le16_to_cpu(&report[i + 3]);
236 i += 4;
237 }
238 } else if (usage == WCM_DIGITIZER) {
239 /* max pressure isn't reported
240 features->pressure_max = (unsigned short)
241 (report[i+4] << 8 | report[i + 3]);
242 */
243 features->pressure_max = 255;
244 i += 4;
245 }
246 break;
247
248 case HID_USAGE_Y:
Ping Chengec67bbe2009-12-15 00:35:24 -0800249 if (usage == WCM_DESKTOP) {
250 if (finger) {
251 features->device_type = BTN_TOOL_DOUBLETAP;
252 if (features->type == TABLETPC2FG) {
253 /* need to reset back */
254 features->pktlen = WACOM_PKGLEN_TPC2FG;
255 features->device_type = BTN_TOOL_TRIPLETAP;
256 features->y_max =
257 wacom_le16_to_cpu(&report[i + 3]);
258 features->y_phy =
259 wacom_le16_to_cpu(&report[i + 6]);
260 i += 7;
261 } else {
262 features->y_max =
263 features->x_max;
264 features->y_phy =
265 wacom_le16_to_cpu(&report[i + 3]);
266 i += 4;
267 }
268 } else if (pen) {
269 /* penabled only accepts exact bytes of data */
270 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800271 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800272 features->device_type = BTN_TOOL_PEN;
273 features->y_max =
274 wacom_le16_to_cpu(&report[i + 3]);
275 i += 4;
276 }
277 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500278 break;
279
280 case HID_USAGE_FINGER:
281 finger = 1;
282 i++;
283 break;
284
285 case HID_USAGE_STYLUS:
286 pen = 1;
287 i++;
288 break;
289
290 case HID_USAGE_UNDEFINED:
291 if (usage == WCM_DESKTOP && finger) /* capacity */
292 features->pressure_max =
293 wacom_le16_to_cpu(&report[i + 3]);
294 i += 4;
295 break;
296 }
297 break;
298
299 case HID_COLLECTION:
Ping Chengec67bbe2009-12-15 00:35:24 -0800300 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500301 finger = usage = 0;
302 break;
303 }
304 }
305
Ping Cheng545f4e92008-11-24 11:44:27 -0500306 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700307 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500308 kfree(report);
309 return result;
310}
311
Ping Chengec67bbe2009-12-15 00:35:24 -0800312static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700313{
314 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800315 int limit = 0, report_id = 2;
316 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700317
318 rep_data = kmalloc(2, GFP_KERNEL);
319 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800320 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700321
Ping Chengec67bbe2009-12-15 00:35:24 -0800322 /* ask to report tablet data if it is 2FGT or not a Tablet PC */
323 if (features->device_type == BTN_TOOL_TRIPLETAP) {
324 do {
325 rep_data[0] = 3;
326 rep_data[1] = 4;
327 report_id = 3;
328 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
329 report_id, rep_data, 2);
330 if (error >= 0)
331 error = usb_get_report(intf,
332 WAC_HID_FEATURE_REPORT, report_id,
333 rep_data, 3);
334 } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
335 } else if (features->type != TABLETPC && features->type != TABLETPC2FG) {
336 do {
337 rep_data[0] = 2;
338 rep_data[1] = 2;
339 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
340 report_id, rep_data, 2);
341 if (error >= 0)
342 error = usb_get_report(intf,
343 WAC_HID_FEATURE_REPORT, report_id,
344 rep_data, 2);
345 } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
346 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700347
348 kfree(rep_data);
349
350 return error < 0 ? error : 0;
351}
352
Ping Chengec67bbe2009-12-15 00:35:24 -0800353static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
354 struct wacom_features *features)
355{
356 int error = 0;
357 struct usb_host_interface *interface = intf->cur_altsetting;
358 struct hid_descriptor *hid_desc;
359
360 /* default device to penabled */
361 features->device_type = BTN_TOOL_PEN;
362
363 /* only Tablet PCs need to retrieve the info */
364 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG))
365 goto out;
366
367 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
368 if (usb_get_extra_descriptor(&interface->endpoint[0],
369 HID_DEVICET_REPORT, &hid_desc)) {
370 printk("wacom: can not retrieve extra class descriptor\n");
371 error = 1;
372 goto out;
373 }
374 }
375 error = wacom_parse_hid(intf, hid_desc, features);
376 if (error)
377 goto out;
378
379 /* touch device found but size is not defined. use default */
380 if (features->device_type == BTN_TOOL_DOUBLETAP && !features->x_max) {
381 features->x_max = 1023;
382 features->y_max = 1023;
383 }
384
385 out:
386 return error;
387}
388
Ping Cheng4492eff2010-03-19 22:18:15 -0700389struct wacom_usbdev_data {
390 struct list_head list;
391 struct kref kref;
392 struct usb_device *dev;
393 struct wacom_shared shared;
394};
395
396static LIST_HEAD(wacom_udev_list);
397static DEFINE_MUTEX(wacom_udev_list_lock);
398
399static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
400{
401 struct wacom_usbdev_data *data;
402
403 list_for_each_entry(data, &wacom_udev_list, list) {
404 if (data->dev == dev) {
405 kref_get(&data->kref);
406 return data;
407 }
408 }
409
410 return NULL;
411}
412
413static int wacom_add_shared_data(struct wacom_wac *wacom,
414 struct usb_device *dev)
415{
416 struct wacom_usbdev_data *data;
417 int retval = 0;
418
419 mutex_lock(&wacom_udev_list_lock);
420
421 data = wacom_get_usbdev_data(dev);
422 if (!data) {
423 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
424 if (!data) {
425 retval = -ENOMEM;
426 goto out;
427 }
428
429 kref_init(&data->kref);
430 data->dev = dev;
431 list_add_tail(&data->list, &wacom_udev_list);
432 }
433
434 wacom->shared = &data->shared;
435
436out:
437 mutex_unlock(&wacom_udev_list_lock);
438 return retval;
439}
440
441static void wacom_release_shared_data(struct kref *kref)
442{
443 struct wacom_usbdev_data *data =
444 container_of(kref, struct wacom_usbdev_data, kref);
445
446 mutex_lock(&wacom_udev_list_lock);
447 list_del(&data->list);
448 mutex_unlock(&wacom_udev_list_lock);
449
450 kfree(data);
451}
452
453static void wacom_remove_shared_data(struct wacom_wac *wacom)
454{
455 struct wacom_usbdev_data *data;
456
457 if (wacom->shared) {
458 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
459 kref_put(&data->kref, wacom_release_shared_data);
460 wacom->shared = NULL;
461 }
462}
463
Ping Cheng3bea7332006-07-13 18:01:36 -0700464static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
465{
466 struct usb_device *dev = interface_to_usbdev(intf);
467 struct usb_endpoint_descriptor *endpoint;
468 struct wacom *wacom;
469 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800470 struct wacom_features *features;
Ping Cheng3bea7332006-07-13 18:01:36 -0700471 struct input_dev *input_dev;
Jason Childse33da8a2010-02-17 22:38:31 -0800472 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700473
Jason Childse33da8a2010-02-17 22:38:31 -0800474 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -0800475 return -EINVAL;
476
Ping Cheng3bea7332006-07-13 18:01:36 -0700477 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Ping Cheng3bea7332006-07-13 18:01:36 -0700478 input_dev = input_allocate_device();
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700479 if (!wacom || !input_dev) {
Jason Childse33da8a2010-02-17 22:38:31 -0800480 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700481 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800482 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700483
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700484 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800485 wacom_wac->features = *((struct wacom_features *)id->driver_info);
486 features = &wacom_wac->features;
487 if (features->pktlen > WACOM_PKGLEN_MAX) {
488 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -0700489 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800490 }
491
492 wacom_wac->data = usb_buffer_alloc(dev, WACOM_PKGLEN_MAX,
493 GFP_KERNEL, &wacom->data_dma);
494 if (!wacom_wac->data) {
495 error = -ENOMEM;
496 goto fail1;
497 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700498
499 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -0800500 if (!wacom->irq) {
501 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700502 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -0800503 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700504
505 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -0400506 wacom->intf = intf;
507 mutex_init(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700508 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
509 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
510
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700511 wacom_wac->input = input_dev;
Ping Cheng3bea7332006-07-13 18:01:36 -0700512
Ping Cheng545f4e92008-11-24 11:44:27 -0500513 endpoint = &intf->cur_altsetting->endpoint[0].desc;
514
Ping Chengec67bbe2009-12-15 00:35:24 -0800515 /* Retrieve the physical and logical size for OEM devices */
516 error = wacom_retrieve_hid_descriptor(intf, features);
517 if (error)
518 goto fail2;
Ping Cheng545f4e92008-11-24 11:44:27 -0500519
Ping Cheng49b764a2010-02-20 00:53:49 -0800520 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
521
522 if (features->type == TABLETPC || features->type == TABLETPC2FG) {
523 /* Append the device type to the name */
524 strlcat(wacom_wac->name,
525 features->device_type == BTN_TOOL_PEN ?
526 " Pen" : " Finger",
527 sizeof(wacom_wac->name));
Ping Cheng4492eff2010-03-19 22:18:15 -0700528
529 error = wacom_add_shared_data(wacom_wac, dev);
530 if (error)
531 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -0800532 }
533
534 input_dev->name = wacom_wac->name;
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700535 input_dev->name = wacom_wac->name;
536 input_dev->dev.parent = &intf->dev;
537 input_dev->open = wacom_open;
538 input_dev->close = wacom_close;
539 usb_to_input_id(dev, &input_dev->id);
540 input_set_drvdata(input_dev, wacom);
Jason Childse33da8a2010-02-17 22:38:31 -0800541
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700542 wacom_setup_input_capabilities(input_dev, wacom_wac);
Ping Cheng3bea7332006-07-13 18:01:36 -0700543
Ping Cheng3bea7332006-07-13 18:01:36 -0700544 usb_fill_int_urb(wacom->irq, dev,
545 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -0800546 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700547 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700548 wacom->irq->transfer_dma = wacom->data_dma;
549 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
550
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700551 error = input_register_device(input_dev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400552 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -0700553 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -0700554
Ping Chengec67bbe2009-12-15 00:35:24 -0800555 /* Note that if query fails it is not a hard failure */
556 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -0700557
558 usb_set_intfdata(intf, wacom);
559 return 0;
560
Ping Cheng4492eff2010-03-19 22:18:15 -0700561 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400562 fail3: usb_free_urb(wacom->irq);
Ping Chengec67bbe2009-12-15 00:35:24 -0800563 fail2: usb_buffer_free(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400564 fail1: input_free_device(input_dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700565 kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400566 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700567}
568
569static void wacom_disconnect(struct usb_interface *intf)
570{
Oliver Neukume7224092008-04-15 01:31:57 -0400571 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700572
573 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -0400574
575 usb_kill_urb(wacom->irq);
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700576 input_unregister_device(wacom->wacom_wac.input);
Oliver Neukume7224092008-04-15 01:31:57 -0400577 usb_free_urb(wacom->irq);
Ping Chengec67bbe2009-12-15 00:35:24 -0800578 usb_buffer_free(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700579 wacom->wacom_wac.data, wacom->data_dma);
580 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -0400581 kfree(wacom);
582}
583
584static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
585{
586 struct wacom *wacom = usb_get_intfdata(intf);
587
588 mutex_lock(&wacom->lock);
589 usb_kill_urb(wacom->irq);
590 mutex_unlock(&wacom->lock);
591
592 return 0;
593}
594
595static int wacom_resume(struct usb_interface *intf)
596{
597 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700598 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -0400599 int rv;
600
601 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -0700602
603 /* switch to wacom mode first */
604 wacom_query_tablet_data(intf, features);
605
606 if (wacom->open)
Oliver Neukume7224092008-04-15 01:31:57 -0400607 rv = usb_submit_urb(wacom->irq, GFP_NOIO);
Ping Cheng38101472010-04-13 23:07:52 -0700608 else
Oliver Neukume7224092008-04-15 01:31:57 -0400609 rv = 0;
Ping Cheng38101472010-04-13 23:07:52 -0700610
Oliver Neukume7224092008-04-15 01:31:57 -0400611 mutex_unlock(&wacom->lock);
612
613 return rv;
614}
615
616static int wacom_reset_resume(struct usb_interface *intf)
617{
618 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700619}
620
621static struct usb_driver wacom_driver = {
622 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -0800623 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -0700624 .probe = wacom_probe,
625 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -0400626 .suspend = wacom_suspend,
627 .resume = wacom_resume,
628 .reset_resume = wacom_reset_resume,
629 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -0700630};
631
632static int __init wacom_init(void)
633{
634 int result;
Bastian Blankb036f6f2010-02-10 23:06:23 -0800635
Ping Cheng3bea7332006-07-13 18:01:36 -0700636 result = usb_register(&wacom_driver);
637 if (result == 0)
Greg Kroah-Hartman899ef6e2008-08-18 13:21:04 -0700638 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
639 DRIVER_DESC "\n");
Ping Cheng3bea7332006-07-13 18:01:36 -0700640 return result;
641}
642
643static void __exit wacom_exit(void)
644{
645 usb_deregister(&wacom_driver);
646}
647
648module_init(wacom_init);
649module_exit(wacom_exit);