blob: 9879c73ee517f2a4d3aec3f9c9bb1ce2b1fb5cb5 [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 Chenga417ea42011-08-16 00:17:56 -070052#define WAC_MSG_RETRIES 5
Ping Cheng3bea7332006-07-13 18:01:36 -070053
54static int usb_get_report(struct usb_interface *intf, unsigned char type,
55 unsigned char id, void *buf, int size)
56{
57 return usb_control_msg(interface_to_usbdev(intf),
58 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
59 USB_REQ_GET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
60 (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
61 buf, size, 100);
62}
63
64static int usb_set_report(struct usb_interface *intf, unsigned char type,
65 unsigned char id, void *buf, int size)
66{
67 return usb_control_msg(interface_to_usbdev(intf),
68 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
69 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
70 (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
71 buf, size, 1000);
72}
73
Adrian Bunk54c9b222006-11-20 03:23:58 +010074static void wacom_sys_irq(struct urb *urb)
Ping Cheng3bea7332006-07-13 18:01:36 -070075{
76 struct wacom *wacom = urb->context;
Ping Cheng3bea7332006-07-13 18:01:36 -070077 int retval;
78
79 switch (urb->status) {
80 case 0:
81 /* success */
82 break;
83 case -ECONNRESET:
84 case -ENOENT:
85 case -ESHUTDOWN:
86 /* this urb is terminated, clean up */
Harvey Harrisonea3e6c52008-05-05 11:36:18 -040087 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -070088 return;
89 default:
Harvey Harrisonea3e6c52008-05-05 11:36:18 -040090 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -070091 goto exit;
92 }
93
Dmitry Torokhov95dd3b32010-03-19 22:18:15 -070094 wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
Ping Cheng3bea7332006-07-13 18:01:36 -070095
96 exit:
Oliver Neukume7224092008-04-15 01:31:57 -040097 usb_mark_last_busy(wacom->usbdev);
Dmitry Torokhov73a97f42010-03-19 22:18:15 -070098 retval = usb_submit_urb(urb, GFP_ATOMIC);
Ping Cheng3bea7332006-07-13 18:01:36 -070099 if (retval)
100 err ("%s - usb_submit_urb failed with result %d",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400101 __func__, retval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700102}
103
Ping Cheng3bea7332006-07-13 18:01:36 -0700104static int wacom_open(struct input_dev *dev)
105{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400106 struct wacom *wacom = input_get_drvdata(dev);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700107 int retval = 0;
108
109 if (usb_autopm_get_interface(wacom->intf) < 0)
110 return -EIO;
Ping Cheng3bea7332006-07-13 18:01:36 -0700111
Oliver Neukume7224092008-04-15 01:31:57 -0400112 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700113
Oliver Neukume7224092008-04-15 01:31:57 -0400114 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700115 retval = -EIO;
116 goto out;
Oliver Neukume7224092008-04-15 01:31:57 -0400117 }
118
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700119 wacom->open = true;
Oliver Neukume7224092008-04-15 01:31:57 -0400120 wacom->intf->needs_remote_wakeup = 1;
121
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700122out:
Oliver Neukume7224092008-04-15 01:31:57 -0400123 mutex_unlock(&wacom->lock);
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700124 usb_autopm_put_interface(wacom->intf);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700125 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);
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700131 int autopm_error;
132
133 autopm_error = usb_autopm_get_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700134
Oliver Neukume7224092008-04-15 01:31:57 -0400135 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700136 usb_kill_urb(wacom->irq);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700137 wacom->open = false;
Oliver Neukume7224092008-04-15 01:31:57 -0400138 wacom->intf->needs_remote_wakeup = 0;
139 mutex_unlock(&wacom->lock);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700140
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700141 if (!autopm_error)
142 usb_autopm_put_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700143}
144
Ping Cheng545f4e92008-11-24 11:44:27 -0500145static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800146 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500147{
148 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800149 char limit = 0;
150 /* result has to be defined as int for some devices */
151 int result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500152 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
153 unsigned char *report;
154
155 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
156 if (!report)
157 return -ENOMEM;
158
159 /* retrive report descriptors */
160 do {
161 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
162 USB_REQ_GET_DESCRIPTOR,
163 USB_RECIP_INTERFACE | USB_DIR_IN,
164 HID_DEVICET_REPORT << 8,
165 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
166 report,
167 hid_desc->wDescriptorLength,
168 5000); /* 5 secs */
Ping Chenga417ea42011-08-16 00:17:56 -0700169 } while (result < 0 && limit++ < WAC_MSG_RETRIES);
Ping Cheng545f4e92008-11-24 11:44:27 -0500170
Ping Cheng384318e2009-04-28 07:49:54 -0700171 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500172 if (result < 0)
173 goto out;
174
175 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
176
177 switch (report[i]) {
178 case HID_USAGE_PAGE:
179 switch (report[i + 1]) {
180 case HID_USAGE_PAGE_DIGITIZER:
181 usage = WCM_DIGITIZER;
182 i++;
183 break;
184
185 case HID_USAGE_PAGE_DESKTOP:
186 usage = WCM_DESKTOP;
187 i++;
188 break;
189 }
190 break;
191
192 case HID_USAGE:
193 switch (report[i + 1]) {
194 case HID_USAGE_X:
195 if (usage == WCM_DESKTOP) {
196 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800197 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800198 if (features->type == TABLETPC2FG) {
199 /* need to reset back */
200 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800201 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Chengec67bbe2009-12-15 00:35:24 -0800202 }
Ping Cheng4a880812010-09-05 12:25:40 -0700203 if (features->type == BAMBOO_PT) {
204 /* need to reset back */
205 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800206 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Cheng4a880812010-09-05 12:25:40 -0700207 features->x_phy =
208 get_unaligned_le16(&report[i + 5]);
209 features->x_max =
210 get_unaligned_le16(&report[i + 8]);
211 i += 15;
212 } else {
213 features->x_max =
214 get_unaligned_le16(&report[i + 3]);
215 features->x_phy =
216 get_unaligned_le16(&report[i + 6]);
217 features->unit = report[i + 9];
218 features->unitExpo = report[i + 11];
219 i += 12;
220 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500221 } else if (pen) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800222 /* penabled only accepts exact bytes of data */
223 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800224 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Cheng4a880812010-09-05 12:25:40 -0700225 if (features->type == BAMBOO_PT)
226 features->pktlen = WACOM_PKGLEN_BBFUN;
Ping Chengec67bbe2009-12-15 00:35:24 -0800227 features->device_type = BTN_TOOL_PEN;
Ping Cheng545f4e92008-11-24 11:44:27 -0500228 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700229 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500230 i += 4;
231 }
232 } else if (usage == WCM_DIGITIZER) {
233 /* max pressure isn't reported
234 features->pressure_max = (unsigned short)
235 (report[i+4] << 8 | report[i + 3]);
236 */
237 features->pressure_max = 255;
238 i += 4;
239 }
240 break;
241
242 case HID_USAGE_Y:
Ping Chengec67bbe2009-12-15 00:35:24 -0800243 if (usage == WCM_DESKTOP) {
244 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800245 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800246 if (features->type == TABLETPC2FG) {
247 /* need to reset back */
248 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800249 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Chengec67bbe2009-12-15 00:35:24 -0800250 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700251 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800252 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700253 get_unaligned_le16(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800254 i += 7;
Ping Cheng4a880812010-09-05 12:25:40 -0700255 } else if (features->type == BAMBOO_PT) {
256 /* need to reset back */
257 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800258 features->device_type = BTN_TOOL_DOUBLETAP;
Ping Cheng4a880812010-09-05 12:25:40 -0700259 features->y_phy =
260 get_unaligned_le16(&report[i + 3]);
261 features->y_max =
262 get_unaligned_le16(&report[i + 6]);
263 i += 12;
Ping Chengec67bbe2009-12-15 00:35:24 -0800264 } else {
265 features->y_max =
266 features->x_max;
267 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700268 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800269 i += 4;
270 }
271 } else if (pen) {
272 /* penabled only accepts exact bytes of data */
273 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800274 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Cheng4a880812010-09-05 12:25:40 -0700275 if (features->type == BAMBOO_PT)
276 features->pktlen = WACOM_PKGLEN_BBFUN;
Ping Chengec67bbe2009-12-15 00:35:24 -0800277 features->device_type = BTN_TOOL_PEN;
278 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700279 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800280 i += 4;
281 }
282 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500283 break;
284
285 case HID_USAGE_FINGER:
286 finger = 1;
287 i++;
288 break;
289
290 case HID_USAGE_STYLUS:
291 pen = 1;
292 i++;
293 break;
294
295 case HID_USAGE_UNDEFINED:
296 if (usage == WCM_DESKTOP && finger) /* capacity */
297 features->pressure_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700298 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500299 i += 4;
300 break;
301 }
302 break;
303
304 case HID_COLLECTION:
Ping Chengec67bbe2009-12-15 00:35:24 -0800305 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500306 finger = usage = 0;
307 break;
308 }
309 }
310
Ping Cheng545f4e92008-11-24 11:44:27 -0500311 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700312 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500313 kfree(report);
314 return result;
315}
316
Ping Chengec67bbe2009-12-15 00:35:24 -0800317static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700318{
319 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800320 int limit = 0, report_id = 2;
321 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700322
323 rep_data = kmalloc(2, GFP_KERNEL);
324 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800325 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700326
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700327 /* ask to report tablet data if it is 2FGT Tablet PC or
328 * not a Tablet PC */
329 if (features->type == TABLETPC2FG) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800330 do {
331 rep_data[0] = 3;
332 rep_data[1] = 4;
333 report_id = 3;
334 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
335 report_id, rep_data, 2);
336 if (error >= 0)
337 error = usb_get_report(intf,
338 WAC_HID_FEATURE_REPORT, report_id,
339 rep_data, 3);
Ping Chenga417ea42011-08-16 00:17:56 -0700340 } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700341 } else if (features->type != TABLETPC) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800342 do {
343 rep_data[0] = 2;
344 rep_data[1] = 2;
345 error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
346 report_id, rep_data, 2);
347 if (error >= 0)
348 error = usb_get_report(intf,
349 WAC_HID_FEATURE_REPORT, report_id,
350 rep_data, 2);
Ping Chenga417ea42011-08-16 00:17:56 -0700351 } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
Ping Chengec67bbe2009-12-15 00:35:24 -0800352 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700353
354 kfree(rep_data);
355
356 return error < 0 ? error : 0;
357}
358
Ping Chengec67bbe2009-12-15 00:35:24 -0800359static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
360 struct wacom_features *features)
361{
362 int error = 0;
363 struct usb_host_interface *interface = intf->cur_altsetting;
364 struct hid_descriptor *hid_desc;
365
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700366 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800367 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700368 features->x_fuzz = 4;
369 features->y_fuzz = 4;
370 features->pressure_fuzz = 0;
371 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800372
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700373 /* only Tablet PCs and Bamboo P&T need to retrieve the info */
Ping Cheng4a880812010-09-05 12:25:40 -0700374 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
375 (features->type != BAMBOO_PT))
Ping Chengec67bbe2009-12-15 00:35:24 -0800376 goto out;
377
378 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
379 if (usb_get_extra_descriptor(&interface->endpoint[0],
380 HID_DEVICET_REPORT, &hid_desc)) {
381 printk("wacom: can not retrieve extra class descriptor\n");
382 error = 1;
383 goto out;
384 }
385 }
386 error = wacom_parse_hid(intf, hid_desc, features);
387 if (error)
388 goto out;
389
Ping Chengec67bbe2009-12-15 00:35:24 -0800390 out:
391 return error;
392}
393
Ping Cheng4492eff2010-03-19 22:18:15 -0700394struct wacom_usbdev_data {
395 struct list_head list;
396 struct kref kref;
397 struct usb_device *dev;
398 struct wacom_shared shared;
399};
400
401static LIST_HEAD(wacom_udev_list);
402static DEFINE_MUTEX(wacom_udev_list_lock);
403
404static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
405{
406 struct wacom_usbdev_data *data;
407
408 list_for_each_entry(data, &wacom_udev_list, list) {
409 if (data->dev == dev) {
410 kref_get(&data->kref);
411 return data;
412 }
413 }
414
415 return NULL;
416}
417
418static int wacom_add_shared_data(struct wacom_wac *wacom,
419 struct usb_device *dev)
420{
421 struct wacom_usbdev_data *data;
422 int retval = 0;
423
424 mutex_lock(&wacom_udev_list_lock);
425
426 data = wacom_get_usbdev_data(dev);
427 if (!data) {
428 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
429 if (!data) {
430 retval = -ENOMEM;
431 goto out;
432 }
433
434 kref_init(&data->kref);
435 data->dev = dev;
436 list_add_tail(&data->list, &wacom_udev_list);
437 }
438
439 wacom->shared = &data->shared;
440
441out:
442 mutex_unlock(&wacom_udev_list_lock);
443 return retval;
444}
445
446static void wacom_release_shared_data(struct kref *kref)
447{
448 struct wacom_usbdev_data *data =
449 container_of(kref, struct wacom_usbdev_data, kref);
450
451 mutex_lock(&wacom_udev_list_lock);
452 list_del(&data->list);
453 mutex_unlock(&wacom_udev_list_lock);
454
455 kfree(data);
456}
457
458static void wacom_remove_shared_data(struct wacom_wac *wacom)
459{
460 struct wacom_usbdev_data *data;
461
462 if (wacom->shared) {
463 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
464 kref_put(&data->kref, wacom_release_shared_data);
465 wacom->shared = NULL;
466 }
467}
468
Ping Cheng3bea7332006-07-13 18:01:36 -0700469static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
470{
471 struct usb_device *dev = interface_to_usbdev(intf);
472 struct usb_endpoint_descriptor *endpoint;
473 struct wacom *wacom;
474 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800475 struct wacom_features *features;
Ping Cheng3bea7332006-07-13 18:01:36 -0700476 struct input_dev *input_dev;
Jason Childse33da8a2010-02-17 22:38:31 -0800477 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700478
Jason Childse33da8a2010-02-17 22:38:31 -0800479 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -0800480 return -EINVAL;
481
Ping Cheng3bea7332006-07-13 18:01:36 -0700482 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Ping Cheng3bea7332006-07-13 18:01:36 -0700483 input_dev = input_allocate_device();
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700484 if (!wacom || !input_dev) {
Jason Childse33da8a2010-02-17 22:38:31 -0800485 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700486 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800487 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700488
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700489 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -0800490 wacom_wac->features = *((struct wacom_features *)id->driver_info);
491 features = &wacom_wac->features;
492 if (features->pktlen > WACOM_PKGLEN_MAX) {
493 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -0700494 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -0800495 }
496
Daniel Mack997ea582010-04-12 13:17:25 +0200497 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
498 GFP_KERNEL, &wacom->data_dma);
Jason Childse33da8a2010-02-17 22:38:31 -0800499 if (!wacom_wac->data) {
500 error = -ENOMEM;
501 goto fail1;
502 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700503
504 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -0800505 if (!wacom->irq) {
506 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700507 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -0800508 }
Ping Cheng3bea7332006-07-13 18:01:36 -0700509
510 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -0400511 wacom->intf = intf;
512 mutex_init(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700513 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
514 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
515
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700516 wacom_wac->input = input_dev;
Ping Cheng3bea7332006-07-13 18:01:36 -0700517
Ping Cheng545f4e92008-11-24 11:44:27 -0500518 endpoint = &intf->cur_altsetting->endpoint[0].desc;
519
Ping Chengec67bbe2009-12-15 00:35:24 -0800520 /* Retrieve the physical and logical size for OEM devices */
521 error = wacom_retrieve_hid_descriptor(intf, features);
522 if (error)
Alexander Strakh4b6d4432011-02-11 00:44:41 -0800523 goto fail3;
Ping Cheng545f4e92008-11-24 11:44:27 -0500524
Henrik Rydbergbc73dd32010-09-05 12:26:16 -0700525 wacom_setup_device_quirks(features);
526
Ping Cheng49b764a2010-02-20 00:53:49 -0800527 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
528
Henrik Rydbergbc73dd32010-09-05 12:26:16 -0700529 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -0800530 /* Append the device type to the name */
531 strlcat(wacom_wac->name,
532 features->device_type == BTN_TOOL_PEN ?
533 " Pen" : " Finger",
534 sizeof(wacom_wac->name));
Ping Cheng4492eff2010-03-19 22:18:15 -0700535
536 error = wacom_add_shared_data(wacom_wac, dev);
537 if (error)
538 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -0800539 }
540
541 input_dev->name = wacom_wac->name;
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700542 input_dev->dev.parent = &intf->dev;
543 input_dev->open = wacom_open;
544 input_dev->close = wacom_close;
545 usb_to_input_id(dev, &input_dev->id);
546 input_set_drvdata(input_dev, wacom);
Jason Childse33da8a2010-02-17 22:38:31 -0800547
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700548 wacom_setup_input_capabilities(input_dev, wacom_wac);
Ping Cheng3bea7332006-07-13 18:01:36 -0700549
Ping Cheng3bea7332006-07-13 18:01:36 -0700550 usb_fill_int_urb(wacom->irq, dev,
551 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -0800552 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700553 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700554 wacom->irq->transfer_dma = wacom->data_dma;
555 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
556
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700557 error = input_register_device(input_dev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400558 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -0700559 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -0700560
Ping Chengec67bbe2009-12-15 00:35:24 -0800561 /* Note that if query fails it is not a hard failure */
562 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -0700563
564 usb_set_intfdata(intf, wacom);
565 return 0;
566
Ping Cheng4492eff2010-03-19 22:18:15 -0700567 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400568 fail3: usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200569 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400570 fail1: input_free_device(input_dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700571 kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400572 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700573}
574
575static void wacom_disconnect(struct usb_interface *intf)
576{
Oliver Neukume7224092008-04-15 01:31:57 -0400577 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700578
579 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -0400580
581 usb_kill_urb(wacom->irq);
Dmitry Torokhov8da23fc2010-03-19 22:18:15 -0700582 input_unregister_device(wacom->wacom_wac.input);
Oliver Neukume7224092008-04-15 01:31:57 -0400583 usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200584 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700585 wacom->wacom_wac.data, wacom->data_dma);
586 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -0400587 kfree(wacom);
588}
589
590static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
591{
592 struct wacom *wacom = usb_get_intfdata(intf);
593
594 mutex_lock(&wacom->lock);
595 usb_kill_urb(wacom->irq);
596 mutex_unlock(&wacom->lock);
597
598 return 0;
599}
600
601static int wacom_resume(struct usb_interface *intf)
602{
603 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700604 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -0400605 int rv;
606
607 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -0700608
609 /* switch to wacom mode first */
610 wacom_query_tablet_data(intf, features);
611
612 if (wacom->open)
Oliver Neukume7224092008-04-15 01:31:57 -0400613 rv = usb_submit_urb(wacom->irq, GFP_NOIO);
Ping Cheng38101472010-04-13 23:07:52 -0700614 else
Oliver Neukume7224092008-04-15 01:31:57 -0400615 rv = 0;
Ping Cheng38101472010-04-13 23:07:52 -0700616
Oliver Neukume7224092008-04-15 01:31:57 -0400617 mutex_unlock(&wacom->lock);
618
619 return rv;
620}
621
622static int wacom_reset_resume(struct usb_interface *intf)
623{
624 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700625}
626
627static struct usb_driver wacom_driver = {
628 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -0800629 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -0700630 .probe = wacom_probe,
631 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -0400632 .suspend = wacom_suspend,
633 .resume = wacom_resume,
634 .reset_resume = wacom_reset_resume,
635 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -0700636};
637
638static int __init wacom_init(void)
639{
640 int result;
Bastian Blankb036f6f2010-02-10 23:06:23 -0800641
Ping Cheng3bea7332006-07-13 18:01:36 -0700642 result = usb_register(&wacom_driver);
643 if (result == 0)
Greg Kroah-Hartman899ef6e2008-08-18 13:21:04 -0700644 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
645 DRIVER_DESC "\n");
Ping Cheng3bea7332006-07-13 18:01:36 -0700646 return result;
647}
648
649static void __exit wacom_exit(void)
650{
651 usb_deregister(&wacom_driver);
652}
653
654module_init(wacom_init);
655module_exit(wacom_exit);