blob: e510e4fb54f7379ae862f1f3979140d961a2d1ae [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);
Ping Cheng3bea7332006-07-13 18:01:36 -0700106
Oliver Neukume7224092008-04-15 01:31:57 -0400107 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700108
Oliver Neukume7224092008-04-15 01:31:57 -0400109 wacom->irq->dev = wacom->usbdev;
110
111 if (usb_autopm_get_interface(wacom->intf) < 0) {
112 mutex_unlock(&wacom->lock);
113 return -EIO;
114 }
115
116 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
117 usb_autopm_put_interface(wacom->intf);
118 mutex_unlock(&wacom->lock);
119 return -EIO;
120 }
121
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700122 wacom->open = true;
Oliver Neukume7224092008-04-15 01:31:57 -0400123 wacom->intf->needs_remote_wakeup = 1;
124
125 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700126 return 0;
127}
128
129static void wacom_close(struct input_dev *dev)
130{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400131 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700132
Oliver Neukume7224092008-04-15 01:31:57 -0400133 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700134 usb_kill_urb(wacom->irq);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700135 wacom->open = false;
Oliver Neukume7224092008-04-15 01:31:57 -0400136 wacom->intf->needs_remote_wakeup = 0;
137 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700138}
139
Ping Cheng545f4e92008-11-24 11:44:27 -0500140static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800141 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500142{
143 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800144 char limit = 0;
145 /* result has to be defined as int for some devices */
146 int result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500147 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
148 unsigned char *report;
149
150 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
151 if (!report)
152 return -ENOMEM;
153
154 /* retrive report descriptors */
155 do {
156 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
157 USB_REQ_GET_DESCRIPTOR,
158 USB_RECIP_INTERFACE | USB_DIR_IN,
159 HID_DEVICET_REPORT << 8,
160 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
161 report,
162 hid_desc->wDescriptorLength,
163 5000); /* 5 secs */
164 } while (result < 0 && limit++ < 5);
165
Ping Cheng384318e2009-04-28 07:49:54 -0700166 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500167 if (result < 0)
168 goto out;
169
170 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
171
172 switch (report[i]) {
173 case HID_USAGE_PAGE:
174 switch (report[i + 1]) {
175 case HID_USAGE_PAGE_DIGITIZER:
176 usage = WCM_DIGITIZER;
177 i++;
178 break;
179
180 case HID_USAGE_PAGE_DESKTOP:
181 usage = WCM_DESKTOP;
182 i++;
183 break;
184 }
185 break;
186
187 case HID_USAGE:
188 switch (report[i + 1]) {
189 case HID_USAGE_X:
190 if (usage == WCM_DESKTOP) {
191 if (finger) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800192 features->device_type = BTN_TOOL_DOUBLETAP;
193 if (features->type == TABLETPC2FG) {
194 /* need to reset back */
195 features->pktlen = WACOM_PKGLEN_TPC2FG;
196 features->device_type = BTN_TOOL_TRIPLETAP;
197 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500198 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700199 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800200 features->x_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700201 get_unaligned_le16(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800202 features->unit = report[i + 9];
203 features->unitExpo = report[i + 11];
204 i += 12;
Ping Cheng545f4e92008-11-24 11:44:27 -0500205 } else if (pen) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800206 /* penabled only accepts exact bytes of data */
207 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800208 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800209 features->device_type = BTN_TOOL_PEN;
Ping Cheng545f4e92008-11-24 11:44:27 -0500210 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700211 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500212 i += 4;
213 }
214 } else if (usage == WCM_DIGITIZER) {
215 /* max pressure isn't reported
216 features->pressure_max = (unsigned short)
217 (report[i+4] << 8 | report[i + 3]);
218 */
219 features->pressure_max = 255;
220 i += 4;
221 }
222 break;
223
224 case HID_USAGE_Y:
Ping Chengec67bbe2009-12-15 00:35:24 -0800225 if (usage == WCM_DESKTOP) {
226 if (finger) {
227 features->device_type = BTN_TOOL_DOUBLETAP;
228 if (features->type == TABLETPC2FG) {
229 /* need to reset back */
230 features->pktlen = WACOM_PKGLEN_TPC2FG;
231 features->device_type = BTN_TOOL_TRIPLETAP;
232 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700233 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800234 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700235 get_unaligned_le16(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800236 i += 7;
237 } else {
238 features->y_max =
239 features->x_max;
240 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700241 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800242 i += 4;
243 }
244 } else if (pen) {
245 /* penabled only accepts exact bytes of data */
246 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800247 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800248 features->device_type = BTN_TOOL_PEN;
249 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700250 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800251 i += 4;
252 }
253 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500254 break;
255
256 case HID_USAGE_FINGER:
257 finger = 1;
258 i++;
259 break;
260
261 case HID_USAGE_STYLUS:
262 pen = 1;
263 i++;
264 break;
265
266 case HID_USAGE_UNDEFINED:
267 if (usage == WCM_DESKTOP && finger) /* capacity */
268 features->pressure_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700269 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500270 i += 4;
271 break;
272 }
273 break;
274
275 case HID_COLLECTION:
Ping Chengec67bbe2009-12-15 00:35:24 -0800276 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500277 finger = usage = 0;
278 break;
279 }
280 }
281
Ping Cheng545f4e92008-11-24 11:44:27 -0500282 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700283 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500284 kfree(report);
285 return result;
286}
287
Ping Chengec67bbe2009-12-15 00:35:24 -0800288static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700289{
290 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800291 int limit = 0, report_id = 2;
292 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700293
294 rep_data = kmalloc(2, GFP_KERNEL);
295 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800296 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700297
Ping Chengec67bbe2009-12-15 00:35:24 -0800298 /* ask to report tablet data if it is 2FGT or not a Tablet PC */
299 if (features->device_type == BTN_TOOL_TRIPLETAP) {
300 do {
301 rep_data[0] = 3;
302 rep_data[1] = 4;
303 report_id = 3;
304 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
305 report_id, rep_data, 2);
306 if (error >= 0)
307 error = usb_get_report(intf,
308 WAC_HID_FEATURE_REPORT, report_id,
309 rep_data, 3);
310 } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
311 } else if (features->type != TABLETPC && features->type != TABLETPC2FG) {
312 do {
313 rep_data[0] = 2;
314 rep_data[1] = 2;
315 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
316 report_id, rep_data, 2);
317 if (error >= 0)
318 error = usb_get_report(intf,
319 WAC_HID_FEATURE_REPORT, report_id,
320 rep_data, 2);
321 } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
322 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700323
324 kfree(rep_data);
325
326 return error < 0 ? error : 0;
327}
328
Ping Chengec67bbe2009-12-15 00:35:24 -0800329static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
330 struct wacom_features *features)
331{
332 int error = 0;
333 struct usb_host_interface *interface = intf->cur_altsetting;
334 struct hid_descriptor *hid_desc;
335
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700336 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800337 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700338 features->x_fuzz = 4;
339 features->y_fuzz = 4;
340 features->pressure_fuzz = 0;
341 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800342
343 /* only Tablet PCs need to retrieve the info */
344 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG))
345 goto out;
346
347 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
348 if (usb_get_extra_descriptor(&interface->endpoint[0],
349 HID_DEVICET_REPORT, &hid_desc)) {
350 printk("wacom: can not retrieve extra class descriptor\n");
351 error = 1;
352 goto out;
353 }
354 }
355 error = wacom_parse_hid(intf, hid_desc, features);
356 if (error)
357 goto out;
358
359 /* touch device found but size is not defined. use default */
360 if (features->device_type == BTN_TOOL_DOUBLETAP && !features->x_max) {
361 features->x_max = 1023;
362 features->y_max = 1023;
363 }
364
365 out:
366 return error;
367}
368
Ping Cheng4492eff2010-03-19 22:18:15 -0700369struct wacom_usbdev_data {
370 struct list_head list;
371 struct kref kref;
372 struct usb_device *dev;
373 struct wacom_shared shared;
374};
375
376static LIST_HEAD(wacom_udev_list);
377static DEFINE_MUTEX(wacom_udev_list_lock);
378
379static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
380{
381 struct wacom_usbdev_data *data;
382
383 list_for_each_entry(data, &wacom_udev_list, list) {
384 if (data->dev == dev) {
385 kref_get(&data->kref);
386 return data;
387 }
388 }
389
390 return NULL;
391}
392
393static int wacom_add_shared_data(struct wacom_wac *wacom,
394 struct usb_device *dev)
395{
396 struct wacom_usbdev_data *data;
397 int retval = 0;
398
399 mutex_lock(&wacom_udev_list_lock);
400
401 data = wacom_get_usbdev_data(dev);
402 if (!data) {
403 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
404 if (!data) {
405 retval = -ENOMEM;
406 goto out;
407 }
408
409 kref_init(&data->kref);
410 data->dev = dev;
411 list_add_tail(&data->list, &wacom_udev_list);
412 }
413
414 wacom->shared = &data->shared;
415
416out:
417 mutex_unlock(&wacom_udev_list_lock);
418 return retval;
419}
420
421static void wacom_release_shared_data(struct kref *kref)
422{
423 struct wacom_usbdev_data *data =
424 container_of(kref, struct wacom_usbdev_data, kref);
425
426 mutex_lock(&wacom_udev_list_lock);
427 list_del(&data->list);
428 mutex_unlock(&wacom_udev_list_lock);
429
430 kfree(data);
431}
432
433static void wacom_remove_shared_data(struct wacom_wac *wacom)
434{
435 struct wacom_usbdev_data *data;
436
437 if (wacom->shared) {
438 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
439 kref_put(&data->kref, wacom_release_shared_data);
440 wacom->shared = NULL;
441 }
442}
443
Ping Cheng3bea7332006-07-13 18:01:36 -0700444static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
445{
446 struct usb_device *dev = interface_to_usbdev(intf);
447 struct usb_endpoint_descriptor *endpoint;
448 struct wacom *wacom;
449 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800450 struct wacom_features *features;
Ping Cheng3bea7332006-07-13 18:01:36 -0700451 struct input_dev *input_dev;
Jason Childse33da8a2010-02-17 22:38:31 -0800452 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700453
Jason Childse33da8a2010-02-17 22:38:31 -0800454 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -0800455 return -EINVAL;
456
Ping Cheng3bea7332006-07-13 18:01:36 -0700457 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Ping Cheng3bea7332006-07-13 18:01:36 -0700458 input_dev = input_allocate_device();
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700459 if (!wacom || !input_dev) {
Jason Childse33da8a2010-02-17 22:38:31 -0800460 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700461 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800462 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700463
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700464 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800465 wacom_wac->features = *((struct wacom_features *)id->driver_info);
466 features = &wacom_wac->features;
467 if (features->pktlen > WACOM_PKGLEN_MAX) {
468 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -0700469 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800470 }
471
Daniel Mack997ea582010-04-12 13:17:25 +0200472 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
473 GFP_KERNEL, &wacom->data_dma);
Jason Childse33da8a2010-02-17 22:38:31 -0800474 if (!wacom_wac->data) {
475 error = -ENOMEM;
476 goto fail1;
477 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700478
479 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -0800480 if (!wacom->irq) {
481 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700482 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -0800483 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700484
485 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -0400486 wacom->intf = intf;
487 mutex_init(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700488 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
489 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
490
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700491 wacom_wac->input = input_dev;
Ping Cheng3bea7332006-07-13 18:01:36 -0700492
Ping Cheng545f4e92008-11-24 11:44:27 -0500493 endpoint = &intf->cur_altsetting->endpoint[0].desc;
494
Ping Chengec67bbe2009-12-15 00:35:24 -0800495 /* Retrieve the physical and logical size for OEM devices */
496 error = wacom_retrieve_hid_descriptor(intf, features);
497 if (error)
498 goto fail2;
Ping Cheng545f4e92008-11-24 11:44:27 -0500499
Ping Cheng49b764a2010-02-20 00:53:49 -0800500 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
501
502 if (features->type == TABLETPC || features->type == TABLETPC2FG) {
503 /* Append the device type to the name */
504 strlcat(wacom_wac->name,
505 features->device_type == BTN_TOOL_PEN ?
506 " Pen" : " Finger",
507 sizeof(wacom_wac->name));
Ping Cheng4492eff2010-03-19 22:18:15 -0700508
509 error = wacom_add_shared_data(wacom_wac, dev);
510 if (error)
511 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -0800512 }
513
514 input_dev->name = wacom_wac->name;
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700515 input_dev->dev.parent = &intf->dev;
516 input_dev->open = wacom_open;
517 input_dev->close = wacom_close;
518 usb_to_input_id(dev, &input_dev->id);
519 input_set_drvdata(input_dev, wacom);
Jason Childse33da8a2010-02-17 22:38:31 -0800520
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700521 wacom_setup_input_capabilities(input_dev, wacom_wac);
Ping Cheng3bea7332006-07-13 18:01:36 -0700522
Ping Cheng3bea7332006-07-13 18:01:36 -0700523 usb_fill_int_urb(wacom->irq, dev,
524 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -0800525 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700526 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700527 wacom->irq->transfer_dma = wacom->data_dma;
528 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
529
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700530 error = input_register_device(input_dev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400531 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -0700532 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -0700533
Ping Chengec67bbe2009-12-15 00:35:24 -0800534 /* Note that if query fails it is not a hard failure */
535 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -0700536
537 usb_set_intfdata(intf, wacom);
538 return 0;
539
Ping Cheng4492eff2010-03-19 22:18:15 -0700540 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400541 fail3: usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200542 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400543 fail1: input_free_device(input_dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700544 kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400545 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700546}
547
548static void wacom_disconnect(struct usb_interface *intf)
549{
Oliver Neukume7224092008-04-15 01:31:57 -0400550 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700551
552 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -0400553
554 usb_kill_urb(wacom->irq);
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700555 input_unregister_device(wacom->wacom_wac.input);
Oliver Neukume7224092008-04-15 01:31:57 -0400556 usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200557 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700558 wacom->wacom_wac.data, wacom->data_dma);
559 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -0400560 kfree(wacom);
561}
562
563static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
564{
565 struct wacom *wacom = usb_get_intfdata(intf);
566
567 mutex_lock(&wacom->lock);
568 usb_kill_urb(wacom->irq);
569 mutex_unlock(&wacom->lock);
570
571 return 0;
572}
573
574static int wacom_resume(struct usb_interface *intf)
575{
576 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700577 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -0400578 int rv;
579
580 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -0700581
582 /* switch to wacom mode first */
583 wacom_query_tablet_data(intf, features);
584
585 if (wacom->open)
Oliver Neukume7224092008-04-15 01:31:57 -0400586 rv = usb_submit_urb(wacom->irq, GFP_NOIO);
Ping Cheng38101472010-04-13 23:07:52 -0700587 else
Oliver Neukume7224092008-04-15 01:31:57 -0400588 rv = 0;
Ping Cheng38101472010-04-13 23:07:52 -0700589
Oliver Neukume7224092008-04-15 01:31:57 -0400590 mutex_unlock(&wacom->lock);
591
592 return rv;
593}
594
595static int wacom_reset_resume(struct usb_interface *intf)
596{
597 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700598}
599
600static struct usb_driver wacom_driver = {
601 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -0800602 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -0700603 .probe = wacom_probe,
604 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -0400605 .suspend = wacom_suspend,
606 .resume = wacom_resume,
607 .reset_resume = wacom_reset_resume,
608 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -0700609};
610
611static int __init wacom_init(void)
612{
613 int result;
Bastian Blankb036f6f2010-02-10 23:06:23 -0800614
Ping Cheng3bea7332006-07-13 18:01:36 -0700615 result = usb_register(&wacom_driver);
616 if (result == 0)
Greg Kroah-Hartman899ef6e2008-08-18 13:21:04 -0700617 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
618 DRIVER_DESC "\n");
Ping Cheng3bea7332006-07-13 18:01:36 -0700619 return result;
620}
621
622static void __exit wacom_exit(void)
623{
624 usb_deregister(&wacom_driver);
625}
626
627module_init(wacom_init);
628module_exit(wacom_exit);