blob: 02de65357233a6d59ff02c98ab42112ce6304f96 [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 -0700103static int wacom_open(struct input_dev *dev)
104{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400105 struct wacom *wacom = input_get_drvdata(dev);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700106 int retval = 0;
107
108 if (usb_autopm_get_interface(wacom->intf) < 0)
109 return -EIO;
Ping Cheng3bea7332006-07-13 18:01:36 -0700110
Oliver Neukume7224092008-04-15 01:31:57 -0400111 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700112
Oliver Neukume7224092008-04-15 01:31:57 -0400113 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700114 retval = -EIO;
115 goto out;
Oliver Neukume7224092008-04-15 01:31:57 -0400116 }
117
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700118 wacom->open = true;
Oliver Neukume7224092008-04-15 01:31:57 -0400119 wacom->intf->needs_remote_wakeup = 1;
120
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700121out:
Oliver Neukume7224092008-04-15 01:31:57 -0400122 mutex_unlock(&wacom->lock);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700123 if (retval)
124 usb_autopm_put_interface(wacom->intf);
125 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -0700126}
127
128static void wacom_close(struct input_dev *dev)
129{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400130 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700131
Oliver Neukume7224092008-04-15 01:31:57 -0400132 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700133 usb_kill_urb(wacom->irq);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700134 wacom->open = false;
Oliver Neukume7224092008-04-15 01:31:57 -0400135 wacom->intf->needs_remote_wakeup = 0;
136 mutex_unlock(&wacom->lock);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700137
138 usb_autopm_put_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700139}
140
Ping Cheng545f4e92008-11-24 11:44:27 -0500141static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800142 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500143{
144 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800145 char limit = 0;
146 /* result has to be defined as int for some devices */
147 int result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500148 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
Ping Cheng384318e2009-04-28 07:49:54 -0700167 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500168 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) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800193 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 }
Ping Cheng4a880812010-09-05 12:25:40 -0700199 if (features->type == BAMBOO_PT) {
200 /* need to reset back */
201 features->pktlen = WACOM_PKGLEN_BBTOUCH;
202 features->device_type = BTN_TOOL_TRIPLETAP;
203 features->x_phy =
204 get_unaligned_le16(&report[i + 5]);
205 features->x_max =
206 get_unaligned_le16(&report[i + 8]);
207 i += 15;
208 } else {
209 features->x_max =
210 get_unaligned_le16(&report[i + 3]);
211 features->x_phy =
212 get_unaligned_le16(&report[i + 6]);
213 features->unit = report[i + 9];
214 features->unitExpo = report[i + 11];
215 i += 12;
216 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500217 } else if (pen) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800218 /* penabled only accepts exact bytes of data */
219 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800220 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Cheng4a880812010-09-05 12:25:40 -0700221 if (features->type == BAMBOO_PT)
222 features->pktlen = WACOM_PKGLEN_BBFUN;
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 =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700225 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500226 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 =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700247 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800248 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700249 get_unaligned_le16(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800250 i += 7;
Ping Cheng4a880812010-09-05 12:25:40 -0700251 } else if (features->type == BAMBOO_PT) {
252 /* need to reset back */
253 features->pktlen = WACOM_PKGLEN_BBTOUCH;
254 features->device_type = BTN_TOOL_TRIPLETAP;
255 features->y_phy =
256 get_unaligned_le16(&report[i + 3]);
257 features->y_max =
258 get_unaligned_le16(&report[i + 6]);
259 i += 12;
Ping Chengec67bbe2009-12-15 00:35:24 -0800260 } else {
261 features->y_max =
262 features->x_max;
263 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700264 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800265 i += 4;
266 }
267 } else if (pen) {
268 /* penabled only accepts exact bytes of data */
269 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800270 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Cheng4a880812010-09-05 12:25:40 -0700271 if (features->type == BAMBOO_PT)
272 features->pktlen = WACOM_PKGLEN_BBFUN;
Ping Chengec67bbe2009-12-15 00:35:24 -0800273 features->device_type = BTN_TOOL_PEN;
274 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700275 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800276 i += 4;
277 }
278 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500279 break;
280
281 case HID_USAGE_FINGER:
282 finger = 1;
283 i++;
284 break;
285
286 case HID_USAGE_STYLUS:
287 pen = 1;
288 i++;
289 break;
290
291 case HID_USAGE_UNDEFINED:
292 if (usage == WCM_DESKTOP && finger) /* capacity */
293 features->pressure_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700294 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500295 i += 4;
296 break;
297 }
298 break;
299
300 case HID_COLLECTION:
Ping Chengec67bbe2009-12-15 00:35:24 -0800301 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500302 finger = usage = 0;
303 break;
304 }
305 }
306
Ping Cheng545f4e92008-11-24 11:44:27 -0500307 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700308 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500309 kfree(report);
310 return result;
311}
312
Ping Chengec67bbe2009-12-15 00:35:24 -0800313static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700314{
315 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800316 int limit = 0, report_id = 2;
317 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700318
319 rep_data = kmalloc(2, GFP_KERNEL);
320 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800321 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700322
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700323 /* ask to report tablet data if it is 2FGT Tablet PC or
324 * not a Tablet PC */
325 if (features->type == TABLETPC2FG) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800326 do {
327 rep_data[0] = 3;
328 rep_data[1] = 4;
329 report_id = 3;
330 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
331 report_id, rep_data, 2);
332 if (error >= 0)
333 error = usb_get_report(intf,
334 WAC_HID_FEATURE_REPORT, report_id,
335 rep_data, 3);
336 } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700337 } else if (features->type != TABLETPC) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800338 do {
339 rep_data[0] = 2;
340 rep_data[1] = 2;
341 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
342 report_id, rep_data, 2);
343 if (error >= 0)
344 error = usb_get_report(intf,
345 WAC_HID_FEATURE_REPORT, report_id,
346 rep_data, 2);
347 } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
348 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700349
350 kfree(rep_data);
351
352 return error < 0 ? error : 0;
353}
354
Ping Chengec67bbe2009-12-15 00:35:24 -0800355static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
356 struct wacom_features *features)
357{
358 int error = 0;
359 struct usb_host_interface *interface = intf->cur_altsetting;
360 struct hid_descriptor *hid_desc;
361
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700362 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800363 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700364 features->x_fuzz = 4;
365 features->y_fuzz = 4;
366 features->pressure_fuzz = 0;
367 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800368
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700369 /* only Tablet PCs and Bamboo P&T need to retrieve the info */
Ping Cheng4a880812010-09-05 12:25:40 -0700370 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
371 (features->type != BAMBOO_PT))
Ping Chengec67bbe2009-12-15 00:35:24 -0800372 goto out;
373
374 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
375 if (usb_get_extra_descriptor(&interface->endpoint[0],
376 HID_DEVICET_REPORT, &hid_desc)) {
377 printk("wacom: can not retrieve extra class descriptor\n");
378 error = 1;
379 goto out;
380 }
381 }
382 error = wacom_parse_hid(intf, hid_desc, features);
383 if (error)
384 goto out;
385
Ping Chengec67bbe2009-12-15 00:35:24 -0800386 out:
387 return error;
388}
389
Ping Cheng4492eff2010-03-19 22:18:15 -0700390struct wacom_usbdev_data {
391 struct list_head list;
392 struct kref kref;
393 struct usb_device *dev;
394 struct wacom_shared shared;
395};
396
397static LIST_HEAD(wacom_udev_list);
398static DEFINE_MUTEX(wacom_udev_list_lock);
399
400static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
401{
402 struct wacom_usbdev_data *data;
403
404 list_for_each_entry(data, &wacom_udev_list, list) {
405 if (data->dev == dev) {
406 kref_get(&data->kref);
407 return data;
408 }
409 }
410
411 return NULL;
412}
413
414static int wacom_add_shared_data(struct wacom_wac *wacom,
415 struct usb_device *dev)
416{
417 struct wacom_usbdev_data *data;
418 int retval = 0;
419
420 mutex_lock(&wacom_udev_list_lock);
421
422 data = wacom_get_usbdev_data(dev);
423 if (!data) {
424 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
425 if (!data) {
426 retval = -ENOMEM;
427 goto out;
428 }
429
430 kref_init(&data->kref);
431 data->dev = dev;
432 list_add_tail(&data->list, &wacom_udev_list);
433 }
434
435 wacom->shared = &data->shared;
436
437out:
438 mutex_unlock(&wacom_udev_list_lock);
439 return retval;
440}
441
442static void wacom_release_shared_data(struct kref *kref)
443{
444 struct wacom_usbdev_data *data =
445 container_of(kref, struct wacom_usbdev_data, kref);
446
447 mutex_lock(&wacom_udev_list_lock);
448 list_del(&data->list);
449 mutex_unlock(&wacom_udev_list_lock);
450
451 kfree(data);
452}
453
454static void wacom_remove_shared_data(struct wacom_wac *wacom)
455{
456 struct wacom_usbdev_data *data;
457
458 if (wacom->shared) {
459 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
460 kref_put(&data->kref, wacom_release_shared_data);
461 wacom->shared = NULL;
462 }
463}
464
Ping Cheng3bea7332006-07-13 18:01:36 -0700465static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
466{
467 struct usb_device *dev = interface_to_usbdev(intf);
468 struct usb_endpoint_descriptor *endpoint;
469 struct wacom *wacom;
470 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800471 struct wacom_features *features;
Ping Cheng3bea7332006-07-13 18:01:36 -0700472 struct input_dev *input_dev;
Jason Childse33da8a2010-02-17 22:38:31 -0800473 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700474
Jason Childse33da8a2010-02-17 22:38:31 -0800475 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -0800476 return -EINVAL;
477
Ping Cheng3bea7332006-07-13 18:01:36 -0700478 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Ping Cheng3bea7332006-07-13 18:01:36 -0700479 input_dev = input_allocate_device();
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700480 if (!wacom || !input_dev) {
Jason Childse33da8a2010-02-17 22:38:31 -0800481 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700482 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800483 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700484
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700485 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800486 wacom_wac->features = *((struct wacom_features *)id->driver_info);
487 features = &wacom_wac->features;
488 if (features->pktlen > WACOM_PKGLEN_MAX) {
489 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -0700490 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800491 }
492
Daniel Mack997ea582010-04-12 13:17:25 +0200493 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
494 GFP_KERNEL, &wacom->data_dma);
Jason Childse33da8a2010-02-17 22:38:31 -0800495 if (!wacom_wac->data) {
496 error = -ENOMEM;
497 goto fail1;
498 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700499
500 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -0800501 if (!wacom->irq) {
502 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700503 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -0800504 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700505
506 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -0400507 wacom->intf = intf;
508 mutex_init(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700509 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
510 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
511
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700512 wacom_wac->input = input_dev;
Ping Cheng3bea7332006-07-13 18:01:36 -0700513
Ping Cheng545f4e92008-11-24 11:44:27 -0500514 endpoint = &intf->cur_altsetting->endpoint[0].desc;
515
Ping Chengec67bbe2009-12-15 00:35:24 -0800516 /* Retrieve the physical and logical size for OEM devices */
517 error = wacom_retrieve_hid_descriptor(intf, features);
518 if (error)
519 goto fail2;
Ping Cheng545f4e92008-11-24 11:44:27 -0500520
Henrik Rydbergbc73dd32010-09-05 12:26:16 -0700521 wacom_setup_device_quirks(features);
522
Ping Cheng49b764a2010-02-20 00:53:49 -0800523 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
524
Henrik Rydbergbc73dd32010-09-05 12:26:16 -0700525 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -0800526 /* Append the device type to the name */
527 strlcat(wacom_wac->name,
528 features->device_type == BTN_TOOL_PEN ?
529 " Pen" : " Finger",
530 sizeof(wacom_wac->name));
Ping Cheng4492eff2010-03-19 22:18:15 -0700531
532 error = wacom_add_shared_data(wacom_wac, dev);
533 if (error)
534 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -0800535 }
536
537 input_dev->name = wacom_wac->name;
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700538 input_dev->dev.parent = &intf->dev;
539 input_dev->open = wacom_open;
540 input_dev->close = wacom_close;
541 usb_to_input_id(dev, &input_dev->id);
542 input_set_drvdata(input_dev, wacom);
Jason Childse33da8a2010-02-17 22:38:31 -0800543
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700544 wacom_setup_input_capabilities(input_dev, wacom_wac);
Ping Cheng3bea7332006-07-13 18:01:36 -0700545
Ping Cheng3bea7332006-07-13 18:01:36 -0700546 usb_fill_int_urb(wacom->irq, dev,
547 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -0800548 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700549 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700550 wacom->irq->transfer_dma = wacom->data_dma;
551 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
552
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700553 error = input_register_device(input_dev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400554 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -0700555 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -0700556
Ping Chengec67bbe2009-12-15 00:35:24 -0800557 /* Note that if query fails it is not a hard failure */
558 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -0700559
560 usb_set_intfdata(intf, wacom);
561 return 0;
562
Ping Cheng4492eff2010-03-19 22:18:15 -0700563 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400564 fail3: usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200565 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400566 fail1: input_free_device(input_dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700567 kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400568 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700569}
570
571static void wacom_disconnect(struct usb_interface *intf)
572{
Oliver Neukume7224092008-04-15 01:31:57 -0400573 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700574
575 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -0400576
577 usb_kill_urb(wacom->irq);
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700578 input_unregister_device(wacom->wacom_wac.input);
Oliver Neukume7224092008-04-15 01:31:57 -0400579 usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200580 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700581 wacom->wacom_wac.data, wacom->data_dma);
582 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -0400583 kfree(wacom);
584}
585
586static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
587{
588 struct wacom *wacom = usb_get_intfdata(intf);
589
590 mutex_lock(&wacom->lock);
591 usb_kill_urb(wacom->irq);
592 mutex_unlock(&wacom->lock);
593
594 return 0;
595}
596
597static int wacom_resume(struct usb_interface *intf)
598{
599 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700600 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -0400601 int rv;
602
603 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -0700604
605 /* switch to wacom mode first */
606 wacom_query_tablet_data(intf, features);
607
608 if (wacom->open)
Oliver Neukume7224092008-04-15 01:31:57 -0400609 rv = usb_submit_urb(wacom->irq, GFP_NOIO);
Ping Cheng38101472010-04-13 23:07:52 -0700610 else
Oliver Neukume7224092008-04-15 01:31:57 -0400611 rv = 0;
Ping Cheng38101472010-04-13 23:07:52 -0700612
Oliver Neukume7224092008-04-15 01:31:57 -0400613 mutex_unlock(&wacom->lock);
614
615 return rv;
616}
617
618static int wacom_reset_resume(struct usb_interface *intf)
619{
620 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700621}
622
623static struct usb_driver wacom_driver = {
624 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -0800625 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -0700626 .probe = wacom_probe,
627 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -0400628 .suspend = wacom_suspend,
629 .resume = wacom_resume,
630 .reset_resume = wacom_reset_resume,
631 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -0700632};
633
634static int __init wacom_init(void)
635{
636 int result;
Bastian Blankb036f6f2010-02-10 23:06:23 -0800637
Ping Cheng3bea7332006-07-13 18:01:36 -0700638 result = usb_register(&wacom_driver);
639 if (result == 0)
Greg Kroah-Hartman899ef6e2008-08-18 13:21:04 -0700640 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
641 DRIVER_DESC "\n");
Ping Cheng3bea7332006-07-13 18:01:36 -0700642 return result;
643}
644
645static void __exit wacom_exit(void)
646{
647 usb_deregister(&wacom_driver);
648}
649
650module_init(wacom_init);
651module_exit(wacom_exit);