blob: b97665f9765e3b31f6b6c0fe6f2df1f44ecab6bf [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 Torokhov62ecae02010-10-10 14:24:16 -0700123 usb_autopm_put_interface(wacom->intf);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700124 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -0700125}
126
127static void wacom_close(struct input_dev *dev)
128{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400129 struct wacom *wacom = input_get_drvdata(dev);
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700130 int autopm_error;
131
132 autopm_error = usb_autopm_get_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700133
Oliver Neukume7224092008-04-15 01:31:57 -0400134 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700135 usb_kill_urb(wacom->irq);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700136 wacom->open = false;
Oliver Neukume7224092008-04-15 01:31:57 -0400137 wacom->intf->needs_remote_wakeup = 0;
138 mutex_unlock(&wacom->lock);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700139
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700140 if (!autopm_error)
141 usb_autopm_put_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700142}
143
Ping Cheng545f4e92008-11-24 11:44:27 -0500144static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800145 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500146{
147 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800148 char limit = 0;
149 /* result has to be defined as int for some devices */
150 int result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500151 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
152 unsigned char *report;
153
154 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
155 if (!report)
156 return -ENOMEM;
157
158 /* retrive report descriptors */
159 do {
160 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
161 USB_REQ_GET_DESCRIPTOR,
162 USB_RECIP_INTERFACE | USB_DIR_IN,
163 HID_DEVICET_REPORT << 8,
164 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
165 report,
166 hid_desc->wDescriptorLength,
167 5000); /* 5 secs */
168 } while (result < 0 && limit++ < 5);
169
Ping Cheng384318e2009-04-28 07:49:54 -0700170 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500171 if (result < 0)
172 goto out;
173
174 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
175
176 switch (report[i]) {
177 case HID_USAGE_PAGE:
178 switch (report[i + 1]) {
179 case HID_USAGE_PAGE_DIGITIZER:
180 usage = WCM_DIGITIZER;
181 i++;
182 break;
183
184 case HID_USAGE_PAGE_DESKTOP:
185 usage = WCM_DESKTOP;
186 i++;
187 break;
188 }
189 break;
190
191 case HID_USAGE:
192 switch (report[i + 1]) {
193 case HID_USAGE_X:
194 if (usage == WCM_DESKTOP) {
195 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800196 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800197 if (features->type == TABLETPC2FG) {
198 /* need to reset back */
199 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800200 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Chengec67bbe2009-12-15 00:35:24 -0800201 }
Ping Cheng4a880812010-09-05 12:25:40 -0700202 if (features->type == BAMBOO_PT) {
203 /* need to reset back */
204 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800205 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Cheng4a880812010-09-05 12:25:40 -0700206 features->x_phy =
207 get_unaligned_le16(&report[i + 5]);
208 features->x_max =
209 get_unaligned_le16(&report[i + 8]);
210 i += 15;
211 } else {
212 features->x_max =
213 get_unaligned_le16(&report[i + 3]);
214 features->x_phy =
215 get_unaligned_le16(&report[i + 6]);
216 features->unit = report[i + 9];
217 features->unitExpo = report[i + 11];
218 i += 12;
219 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500220 } else if (pen) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800221 /* penabled only accepts exact bytes of data */
222 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800223 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Cheng4a880812010-09-05 12:25:40 -0700224 if (features->type == BAMBOO_PT)
225 features->pktlen = WACOM_PKGLEN_BBFUN;
Ping Chengec67bbe2009-12-15 00:35:24 -0800226 features->device_type = BTN_TOOL_PEN;
Ping Cheng545f4e92008-11-24 11:44:27 -0500227 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700228 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500229 i += 4;
230 }
231 } else if (usage == WCM_DIGITIZER) {
232 /* max pressure isn't reported
233 features->pressure_max = (unsigned short)
234 (report[i+4] << 8 | report[i + 3]);
235 */
236 features->pressure_max = 255;
237 i += 4;
238 }
239 break;
240
241 case HID_USAGE_Y:
Ping Chengec67bbe2009-12-15 00:35:24 -0800242 if (usage == WCM_DESKTOP) {
243 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800244 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800245 if (features->type == TABLETPC2FG) {
246 /* need to reset back */
247 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800248 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Chengec67bbe2009-12-15 00:35:24 -0800249 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 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700252 get_unaligned_le16(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800253 i += 7;
Ping Cheng4a880812010-09-05 12:25:40 -0700254 } else if (features->type == BAMBOO_PT) {
255 /* need to reset back */
256 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800257 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Cheng4a880812010-09-05 12:25:40 -0700258 features->y_phy =
259 get_unaligned_le16(&report[i + 3]);
260 features->y_max =
261 get_unaligned_le16(&report[i + 6]);
262 i += 12;
Ping Chengec67bbe2009-12-15 00:35:24 -0800263 } else {
264 features->y_max =
265 features->x_max;
266 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700267 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800268 i += 4;
269 }
270 } else if (pen) {
271 /* penabled only accepts exact bytes of data */
272 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800273 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Cheng4a880812010-09-05 12:25:40 -0700274 if (features->type == BAMBOO_PT)
275 features->pktlen = WACOM_PKGLEN_BBFUN;
Ping Chengec67bbe2009-12-15 00:35:24 -0800276 features->device_type = BTN_TOOL_PEN;
277 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700278 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800279 i += 4;
280 }
281 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500282 break;
283
284 case HID_USAGE_FINGER:
285 finger = 1;
286 i++;
287 break;
288
289 case HID_USAGE_STYLUS:
290 pen = 1;
291 i++;
292 break;
293
294 case HID_USAGE_UNDEFINED:
295 if (usage == WCM_DESKTOP && finger) /* capacity */
296 features->pressure_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700297 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500298 i += 4;
299 break;
300 }
301 break;
302
303 case HID_COLLECTION:
Ping Chengec67bbe2009-12-15 00:35:24 -0800304 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500305 finger = usage = 0;
306 break;
307 }
308 }
309
Ping Cheng545f4e92008-11-24 11:44:27 -0500310 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700311 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500312 kfree(report);
313 return result;
314}
315
Ping Chengec67bbe2009-12-15 00:35:24 -0800316static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700317{
318 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800319 int limit = 0, report_id = 2;
320 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700321
322 rep_data = kmalloc(2, GFP_KERNEL);
323 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800324 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700325
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700326 /* ask to report tablet data if it is 2FGT Tablet PC or
327 * not a Tablet PC */
328 if (features->type == TABLETPC2FG) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800329 do {
330 rep_data[0] = 3;
331 rep_data[1] = 4;
332 report_id = 3;
333 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
334 report_id, rep_data, 2);
335 if (error >= 0)
336 error = usb_get_report(intf,
337 WAC_HID_FEATURE_REPORT, report_id,
338 rep_data, 3);
339 } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700340 } else if (features->type != TABLETPC) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800341 do {
342 rep_data[0] = 2;
343 rep_data[1] = 2;
344 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
345 report_id, rep_data, 2);
346 if (error >= 0)
347 error = usb_get_report(intf,
348 WAC_HID_FEATURE_REPORT, report_id,
349 rep_data, 2);
350 } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
351 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700352
353 kfree(rep_data);
354
355 return error < 0 ? error : 0;
356}
357
Ping Chengec67bbe2009-12-15 00:35:24 -0800358static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
359 struct wacom_features *features)
360{
361 int error = 0;
362 struct usb_host_interface *interface = intf->cur_altsetting;
363 struct hid_descriptor *hid_desc;
364
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700365 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800366 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700367 features->x_fuzz = 4;
368 features->y_fuzz = 4;
369 features->pressure_fuzz = 0;
370 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800371
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700372 /* only Tablet PCs and Bamboo P&T need to retrieve the info */
Ping Cheng4a880812010-09-05 12:25:40 -0700373 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
374 (features->type != BAMBOO_PT))
Ping Chengec67bbe2009-12-15 00:35:24 -0800375 goto out;
376
377 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
378 if (usb_get_extra_descriptor(&interface->endpoint[0],
379 HID_DEVICET_REPORT, &hid_desc)) {
380 printk("wacom: can not retrieve extra class descriptor\n");
381 error = 1;
382 goto out;
383 }
384 }
385 error = wacom_parse_hid(intf, hid_desc, features);
386 if (error)
387 goto out;
388
Ping Chengec67bbe2009-12-15 00:35:24 -0800389 out:
390 return error;
391}
392
Ping Cheng4492eff2010-03-19 22:18:15 -0700393struct wacom_usbdev_data {
394 struct list_head list;
395 struct kref kref;
396 struct usb_device *dev;
397 struct wacom_shared shared;
398};
399
400static LIST_HEAD(wacom_udev_list);
401static DEFINE_MUTEX(wacom_udev_list_lock);
402
403static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
404{
405 struct wacom_usbdev_data *data;
406
407 list_for_each_entry(data, &wacom_udev_list, list) {
408 if (data->dev == dev) {
409 kref_get(&data->kref);
410 return data;
411 }
412 }
413
414 return NULL;
415}
416
417static int wacom_add_shared_data(struct wacom_wac *wacom,
418 struct usb_device *dev)
419{
420 struct wacom_usbdev_data *data;
421 int retval = 0;
422
423 mutex_lock(&wacom_udev_list_lock);
424
425 data = wacom_get_usbdev_data(dev);
426 if (!data) {
427 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
428 if (!data) {
429 retval = -ENOMEM;
430 goto out;
431 }
432
433 kref_init(&data->kref);
434 data->dev = dev;
435 list_add_tail(&data->list, &wacom_udev_list);
436 }
437
438 wacom->shared = &data->shared;
439
440out:
441 mutex_unlock(&wacom_udev_list_lock);
442 return retval;
443}
444
445static void wacom_release_shared_data(struct kref *kref)
446{
447 struct wacom_usbdev_data *data =
448 container_of(kref, struct wacom_usbdev_data, kref);
449
450 mutex_lock(&wacom_udev_list_lock);
451 list_del(&data->list);
452 mutex_unlock(&wacom_udev_list_lock);
453
454 kfree(data);
455}
456
457static void wacom_remove_shared_data(struct wacom_wac *wacom)
458{
459 struct wacom_usbdev_data *data;
460
461 if (wacom->shared) {
462 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
463 kref_put(&data->kref, wacom_release_shared_data);
464 wacom->shared = NULL;
465 }
466}
467
Ping Cheng3bea7332006-07-13 18:01:36 -0700468static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
469{
470 struct usb_device *dev = interface_to_usbdev(intf);
471 struct usb_endpoint_descriptor *endpoint;
472 struct wacom *wacom;
473 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800474 struct wacom_features *features;
Ping Cheng3bea7332006-07-13 18:01:36 -0700475 struct input_dev *input_dev;
Jason Childse33da8a2010-02-17 22:38:31 -0800476 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700477
Jason Childse33da8a2010-02-17 22:38:31 -0800478 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -0800479 return -EINVAL;
480
Ping Cheng3bea7332006-07-13 18:01:36 -0700481 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Ping Cheng3bea7332006-07-13 18:01:36 -0700482 input_dev = input_allocate_device();
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700483 if (!wacom || !input_dev) {
Jason Childse33da8a2010-02-17 22:38:31 -0800484 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700485 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800486 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700487
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700488 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800489 wacom_wac->features = *((struct wacom_features *)id->driver_info);
490 features = &wacom_wac->features;
491 if (features->pktlen > WACOM_PKGLEN_MAX) {
492 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -0700493 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800494 }
495
Daniel Mack997ea582010-04-12 13:17:25 +0200496 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
497 GFP_KERNEL, &wacom->data_dma);
Jason Childse33da8a2010-02-17 22:38:31 -0800498 if (!wacom_wac->data) {
499 error = -ENOMEM;
500 goto fail1;
501 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700502
503 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -0800504 if (!wacom->irq) {
505 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700506 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -0800507 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700508
509 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -0400510 wacom->intf = intf;
511 mutex_init(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700512 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
513 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
514
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700515 wacom_wac->input = input_dev;
Ping Cheng3bea7332006-07-13 18:01:36 -0700516
Ping Cheng545f4e92008-11-24 11:44:27 -0500517 endpoint = &intf->cur_altsetting->endpoint[0].desc;
518
Ping Chengec67bbe2009-12-15 00:35:24 -0800519 /* Retrieve the physical and logical size for OEM devices */
520 error = wacom_retrieve_hid_descriptor(intf, features);
521 if (error)
522 goto fail2;
Ping Cheng545f4e92008-11-24 11:44:27 -0500523
Henrik Rydbergbc73dd32010-09-05 12:26:16 -0700524 wacom_setup_device_quirks(features);
525
Ping Cheng49b764a2010-02-20 00:53:49 -0800526 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
527
Henrik Rydbergbc73dd32010-09-05 12:26:16 -0700528 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -0800529 /* Append the device type to the name */
530 strlcat(wacom_wac->name,
531 features->device_type == BTN_TOOL_PEN ?
532 " Pen" : " Finger",
533 sizeof(wacom_wac->name));
Ping Cheng4492eff2010-03-19 22:18:15 -0700534
535 error = wacom_add_shared_data(wacom_wac, dev);
536 if (error)
537 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -0800538 }
539
540 input_dev->name = wacom_wac->name;
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700541 input_dev->dev.parent = &intf->dev;
542 input_dev->open = wacom_open;
543 input_dev->close = wacom_close;
544 usb_to_input_id(dev, &input_dev->id);
545 input_set_drvdata(input_dev, wacom);
Jason Childse33da8a2010-02-17 22:38:31 -0800546
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700547 wacom_setup_input_capabilities(input_dev, wacom_wac);
Ping Cheng3bea7332006-07-13 18:01:36 -0700548
Ping Cheng3bea7332006-07-13 18:01:36 -0700549 usb_fill_int_urb(wacom->irq, dev,
550 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -0800551 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700552 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700553 wacom->irq->transfer_dma = wacom->data_dma;
554 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
555
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700556 error = input_register_device(input_dev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400557 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -0700558 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -0700559
Ping Chengec67bbe2009-12-15 00:35:24 -0800560 /* Note that if query fails it is not a hard failure */
561 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -0700562
563 usb_set_intfdata(intf, wacom);
564 return 0;
565
Ping Cheng4492eff2010-03-19 22:18:15 -0700566 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400567 fail3: usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200568 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400569 fail1: input_free_device(input_dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700570 kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400571 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700572}
573
574static void wacom_disconnect(struct usb_interface *intf)
575{
Oliver Neukume7224092008-04-15 01:31:57 -0400576 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700577
578 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -0400579
580 usb_kill_urb(wacom->irq);
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700581 input_unregister_device(wacom->wacom_wac.input);
Oliver Neukume7224092008-04-15 01:31:57 -0400582 usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200583 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700584 wacom->wacom_wac.data, wacom->data_dma);
585 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -0400586 kfree(wacom);
587}
588
589static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
590{
591 struct wacom *wacom = usb_get_intfdata(intf);
592
593 mutex_lock(&wacom->lock);
594 usb_kill_urb(wacom->irq);
595 mutex_unlock(&wacom->lock);
596
597 return 0;
598}
599
600static int wacom_resume(struct usb_interface *intf)
601{
602 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700603 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -0400604 int rv;
605
606 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -0700607
608 /* switch to wacom mode first */
609 wacom_query_tablet_data(intf, features);
610
611 if (wacom->open)
Oliver Neukume7224092008-04-15 01:31:57 -0400612 rv = usb_submit_urb(wacom->irq, GFP_NOIO);
Ping Cheng38101472010-04-13 23:07:52 -0700613 else
Oliver Neukume7224092008-04-15 01:31:57 -0400614 rv = 0;
Ping Cheng38101472010-04-13 23:07:52 -0700615
Oliver Neukume7224092008-04-15 01:31:57 -0400616 mutex_unlock(&wacom->lock);
617
618 return rv;
619}
620
621static int wacom_reset_resume(struct usb_interface *intf)
622{
623 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700624}
625
626static struct usb_driver wacom_driver = {
627 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -0800628 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -0700629 .probe = wacom_probe,
630 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -0400631 .suspend = wacom_suspend,
632 .resume = wacom_resume,
633 .reset_resume = wacom_reset_resume,
634 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -0700635};
636
637static int __init wacom_init(void)
638{
639 int result;
Bastian Blankb036f6f2010-02-10 23:06:23 -0800640
Ping Cheng3bea7332006-07-13 18:01:36 -0700641 result = usb_register(&wacom_driver);
642 if (result == 0)
Greg Kroah-Hartman899ef6e2008-08-18 13:21:04 -0700643 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
644 DRIVER_DESC "\n");
Ping Cheng3bea7332006-07-13 18:01:36 -0700645 return result;
646}
647
648static void __exit wacom_exit(void)
649{
650 usb_deregister(&wacom_driver);
651}
652
653module_init(wacom_init);
654module_exit(wacom_exit);