blob: ac29a68ac53ebf375260d17d8fecae8e138d7453 [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
Chris Bagwell41343612011-10-26 22:32:52 -070031#define HID_COLLECTION 0xa1
32#define HID_COLLECTION_LOGICAL 0x02
33#define HID_COLLECTION_END 0xc0
Ping Cheng545f4e92008-11-24 11:44:27 -050034
35enum {
36 WCM_UNDEFINED = 0,
37 WCM_DESKTOP,
38 WCM_DIGITIZER,
39};
40
41struct hid_descriptor {
42 struct usb_descriptor_header header;
43 __le16 bcdHID;
44 u8 bCountryCode;
45 u8 bNumDescriptors;
46 u8 bDescriptorType;
47 __le16 wDescriptorLength;
48} __attribute__ ((packed));
49
50/* defines to get/set USB message */
Ping Cheng3bea7332006-07-13 18:01:36 -070051#define USB_REQ_GET_REPORT 0x01
52#define USB_REQ_SET_REPORT 0x09
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070053
Ping Cheng545f4e92008-11-24 11:44:27 -050054#define WAC_HID_FEATURE_REPORT 0x03
Ping Chenga417ea42011-08-16 00:17:56 -070055#define WAC_MSG_RETRIES 5
Ping Cheng3bea7332006-07-13 18:01:36 -070056
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070057#define WAC_CMD_LED_CONTROL 0x20
58#define WAC_CMD_ICON_START 0x21
59#define WAC_CMD_ICON_XFER 0x23
60#define WAC_CMD_RETRIES 10
61
62static int wacom_get_report(struct usb_interface *intf, u8 type, u8 id,
63 void *buf, size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070064{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070065 struct usb_device *dev = interface_to_usbdev(intf);
66 int retval;
67
68 do {
69 retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
70 USB_REQ_GET_REPORT,
Chris Bagwell6e8ec532011-10-26 22:24:22 -070071 USB_DIR_IN | USB_TYPE_CLASS |
72 USB_RECIP_INTERFACE,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070073 (type << 8) + id,
74 intf->altsetting[0].desc.bInterfaceNumber,
75 buf, size, 100);
76 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
77
78 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070079}
80
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070081static int wacom_set_report(struct usb_interface *intf, u8 type, u8 id,
82 void *buf, size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070083{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070084 struct usb_device *dev = interface_to_usbdev(intf);
85 int retval;
86
87 do {
88 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
89 USB_REQ_SET_REPORT,
90 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
91 (type << 8) + id,
92 intf->altsetting[0].desc.bInterfaceNumber,
93 buf, size, 1000);
94 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
95
96 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070097}
98
Adrian Bunk54c9b2262006-11-20 03:23:58 +010099static void wacom_sys_irq(struct urb *urb)
Ping Cheng3bea7332006-07-13 18:01:36 -0700100{
101 struct wacom *wacom = urb->context;
Greg Kroah-Hartman3b6aee22012-05-01 21:24:28 -0700102 struct device *dev = &wacom->wacom_wac.input->dev;
Ping Cheng3bea7332006-07-13 18:01:36 -0700103 int retval;
104
105 switch (urb->status) {
106 case 0:
107 /* success */
108 break;
109 case -ECONNRESET:
110 case -ENOENT:
111 case -ESHUTDOWN:
112 /* this urb is terminated, clean up */
Greg Kroah-Hartman3b6aee22012-05-01 21:24:28 -0700113 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
114 __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -0700115 return;
116 default:
Greg Kroah-Hartman3b6aee22012-05-01 21:24:28 -0700117 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
118 __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -0700119 goto exit;
120 }
121
Dmitry Torokhov95dd3b32010-03-19 22:18:15 -0700122 wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
Ping Cheng3bea7332006-07-13 18:01:36 -0700123
124 exit:
Oliver Neukume7224092008-04-15 01:31:57 -0400125 usb_mark_last_busy(wacom->usbdev);
Dmitry Torokhov73a97f42010-03-19 22:18:15 -0700126 retval = usb_submit_urb(urb, GFP_ATOMIC);
Ping Cheng3bea7332006-07-13 18:01:36 -0700127 if (retval)
Greg Kroah-Hartman3b6aee22012-05-01 21:24:28 -0700128 dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
Greg Kroah-Hartmanb3169fe2012-04-25 14:48:44 -0700129 __func__, retval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700130}
131
Ping Cheng3bea7332006-07-13 18:01:36 -0700132static int wacom_open(struct input_dev *dev)
133{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400134 struct wacom *wacom = input_get_drvdata(dev);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700135 int retval = 0;
136
137 if (usb_autopm_get_interface(wacom->intf) < 0)
138 return -EIO;
Ping Cheng3bea7332006-07-13 18:01:36 -0700139
Oliver Neukume7224092008-04-15 01:31:57 -0400140 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700141
Oliver Neukume7224092008-04-15 01:31:57 -0400142 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700143 retval = -EIO;
144 goto out;
Oliver Neukume7224092008-04-15 01:31:57 -0400145 }
146
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700147 wacom->open = true;
Oliver Neukume7224092008-04-15 01:31:57 -0400148 wacom->intf->needs_remote_wakeup = 1;
149
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700150out:
Oliver Neukume7224092008-04-15 01:31:57 -0400151 mutex_unlock(&wacom->lock);
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700152 usb_autopm_put_interface(wacom->intf);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700153 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -0700154}
155
156static void wacom_close(struct input_dev *dev)
157{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400158 struct wacom *wacom = input_get_drvdata(dev);
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700159 int autopm_error;
160
161 autopm_error = usb_autopm_get_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700162
Oliver Neukume7224092008-04-15 01:31:57 -0400163 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700164 usb_kill_urb(wacom->irq);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700165 wacom->open = false;
Oliver Neukume7224092008-04-15 01:31:57 -0400166 wacom->intf->needs_remote_wakeup = 0;
167 mutex_unlock(&wacom->lock);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700168
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700169 if (!autopm_error)
170 usb_autopm_put_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700171}
172
Chris Bagwell16bf2882012-03-25 23:26:20 -0700173/*
174 * Static values for max X/Y and resolution of Pen interface is stored in
175 * features. This mean physical size of active area can be computed.
176 * This is useful to do when Pen and Touch have same active area of tablet.
177 * This means for Touch device, we only need to find max X/Y value and we
178 * have enough information to compute resolution of touch.
179 */
180static void wacom_set_phy_from_res(struct wacom_features *features)
181{
182 features->x_phy = (features->x_max * 100) / features->x_resolution;
183 features->y_phy = (features->y_max * 100) / features->y_resolution;
184}
185
Chris Bagwell41343612011-10-26 22:32:52 -0700186static int wacom_parse_logical_collection(unsigned char *report,
187 struct wacom_features *features)
188{
189 int length = 0;
190
191 if (features->type == BAMBOO_PT) {
192
193 /* Logical collection is only used by 3rd gen Bamboo Touch */
194 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
Ping Cheng8b4a0c12012-01-31 00:07:33 -0800195 features->device_type = BTN_TOOL_FINGER;
Chris Bagwell41343612011-10-26 22:32:52 -0700196
Chris Bagwell16bf2882012-03-25 23:26:20 -0700197 wacom_set_phy_from_res(features);
Chris Bagwell41343612011-10-26 22:32:52 -0700198
199 features->x_max = features->y_max =
200 get_unaligned_le16(&report[10]);
201
202 length = 11;
203 }
204 return length;
205}
206
Chris Bagwell428f8582011-10-26 22:26:59 -0700207/*
208 * Interface Descriptor of wacom devices can be incomplete and
209 * inconsistent so wacom_features table is used to store stylus
210 * device's packet lengths, various maximum values, and tablet
211 * resolution based on product ID's.
212 *
213 * For devices that contain 2 interfaces, wacom_features table is
214 * inaccurate for the touch interface. Since the Interface Descriptor
215 * for touch interfaces has pretty complete data, this function exists
216 * to query tablet for this missing information instead of hard coding in
217 * an additional table.
218 *
219 * A typical Interface Descriptor for a stylus will contain a
220 * boot mouse application collection that is not of interest and this
221 * function will ignore it.
222 *
223 * It also contains a digitizer application collection that also is not
224 * of interest since any information it contains would be duplicate
225 * of what is in wacom_features. Usually it defines a report of an array
226 * of bytes that could be used as max length of the stylus packet returned.
227 * If it happens to define a Digitizer-Stylus Physical Collection then
228 * the X and Y logical values contain valid data but it is ignored.
229 *
230 * A typical Interface Descriptor for a touch interface will contain a
231 * Digitizer-Finger Physical Collection which will define both logical
232 * X/Y maximum as well as the physical size of tablet. Since touch
233 * interfaces haven't supported pressure or distance, this is enough
234 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700235 *
236 * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
237 * Collection. Instead they define a Logical Collection with a single
238 * Logical Maximum for both X and Y.
Chris Bagwell428f8582011-10-26 22:26:59 -0700239 */
240static int wacom_parse_hid(struct usb_interface *intf,
241 struct hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800242 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500243{
244 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800245 char limit = 0;
246 /* result has to be defined as int for some devices */
247 int result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500248 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
249 unsigned char *report;
250
251 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
252 if (!report)
253 return -ENOMEM;
254
255 /* retrive report descriptors */
256 do {
257 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
258 USB_REQ_GET_DESCRIPTOR,
259 USB_RECIP_INTERFACE | USB_DIR_IN,
260 HID_DEVICET_REPORT << 8,
261 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
262 report,
263 hid_desc->wDescriptorLength,
264 5000); /* 5 secs */
Ping Chenga417ea42011-08-16 00:17:56 -0700265 } while (result < 0 && limit++ < WAC_MSG_RETRIES);
Ping Cheng545f4e92008-11-24 11:44:27 -0500266
Ping Cheng384318e2009-04-28 07:49:54 -0700267 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500268 if (result < 0)
269 goto out;
270
271 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
272
273 switch (report[i]) {
274 case HID_USAGE_PAGE:
275 switch (report[i + 1]) {
276 case HID_USAGE_PAGE_DIGITIZER:
277 usage = WCM_DIGITIZER;
278 i++;
279 break;
280
281 case HID_USAGE_PAGE_DESKTOP:
282 usage = WCM_DESKTOP;
283 i++;
284 break;
285 }
286 break;
287
288 case HID_USAGE:
289 switch (report[i + 1]) {
290 case HID_USAGE_X:
291 if (usage == WCM_DESKTOP) {
292 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800293 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800294 if (features->type == TABLETPC2FG) {
295 /* need to reset back */
296 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Chengec67bbe2009-12-15 00:35:24 -0800297 }
Ping Cheng4a880812010-09-05 12:25:40 -0700298 if (features->type == BAMBOO_PT) {
299 /* need to reset back */
300 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng4a880812010-09-05 12:25:40 -0700301 features->x_phy =
302 get_unaligned_le16(&report[i + 5]);
303 features->x_max =
304 get_unaligned_le16(&report[i + 8]);
305 i += 15;
306 } else {
307 features->x_max =
308 get_unaligned_le16(&report[i + 3]);
309 features->x_phy =
310 get_unaligned_le16(&report[i + 6]);
311 features->unit = report[i + 9];
312 features->unitExpo = report[i + 11];
313 i += 12;
314 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500315 } else if (pen) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800316 /* penabled only accepts exact bytes of data */
317 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800318 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800319 features->device_type = BTN_TOOL_PEN;
Ping Cheng545f4e92008-11-24 11:44:27 -0500320 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700321 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500322 i += 4;
323 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500324 }
325 break;
326
327 case HID_USAGE_Y:
Ping Chengec67bbe2009-12-15 00:35:24 -0800328 if (usage == WCM_DESKTOP) {
329 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800330 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800331 if (features->type == TABLETPC2FG) {
332 /* need to reset back */
333 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Chengec67bbe2009-12-15 00:35:24 -0800334 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700335 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800336 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700337 get_unaligned_le16(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800338 i += 7;
Ping Cheng4a880812010-09-05 12:25:40 -0700339 } else if (features->type == BAMBOO_PT) {
340 /* need to reset back */
341 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng4a880812010-09-05 12:25:40 -0700342 features->y_phy =
343 get_unaligned_le16(&report[i + 3]);
344 features->y_max =
345 get_unaligned_le16(&report[i + 6]);
346 i += 12;
Ping Chengec67bbe2009-12-15 00:35:24 -0800347 } else {
348 features->y_max =
349 features->x_max;
350 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700351 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800352 i += 4;
353 }
354 } else if (pen) {
355 /* penabled only accepts exact bytes of data */
356 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800357 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800358 features->device_type = BTN_TOOL_PEN;
359 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700360 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800361 i += 4;
362 }
363 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500364 break;
365
366 case HID_USAGE_FINGER:
367 finger = 1;
368 i++;
369 break;
370
Chris Bagwell428f8582011-10-26 22:26:59 -0700371 /*
372 * Requiring Stylus Usage will ignore boot mouse
373 * X/Y values and some cases of invalid Digitizer X/Y
374 * values commonly reported.
375 */
Ping Cheng545f4e92008-11-24 11:44:27 -0500376 case HID_USAGE_STYLUS:
377 pen = 1;
378 i++;
379 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500380 }
381 break;
382
Chris Bagwell41343612011-10-26 22:32:52 -0700383 case HID_COLLECTION_END:
Ping Chengec67bbe2009-12-15 00:35:24 -0800384 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500385 finger = usage = 0;
386 break;
Chris Bagwell41343612011-10-26 22:32:52 -0700387
388 case HID_COLLECTION:
389 i++;
390 switch (report[i]) {
391 case HID_COLLECTION_LOGICAL:
392 i += wacom_parse_logical_collection(&report[i],
393 features);
394 break;
395 }
396 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500397 }
398 }
399
Ping Cheng545f4e92008-11-24 11:44:27 -0500400 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700401 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500402 kfree(report);
403 return result;
404}
405
Ping Chengec67bbe2009-12-15 00:35:24 -0800406static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700407{
408 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800409 int limit = 0, report_id = 2;
410 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700411
Ping Cheng3b48c912011-08-16 00:17:57 -0700412 rep_data = kmalloc(4, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700413 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800414 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700415
Ping Cheng3b48c912011-08-16 00:17:57 -0700416 /* ask to report tablet data if it is MT Tablet PC or
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700417 * not a Tablet PC */
418 if (features->type == TABLETPC2FG) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800419 do {
420 rep_data[0] = 3;
421 rep_data[1] = 4;
Ping Cheng3b48c912011-08-16 00:17:57 -0700422 rep_data[2] = 0;
423 rep_data[3] = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800424 report_id = 3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700425 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
426 report_id, rep_data, 4, 1);
Ping Chengec67bbe2009-12-15 00:35:24 -0800427 if (error >= 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700428 error = wacom_get_report(intf,
429 WAC_HID_FEATURE_REPORT,
430 report_id, rep_data, 4, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700431 } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
Chris Bagwell6e8ec532011-10-26 22:24:22 -0700432 } else if (features->type != TABLETPC &&
Chris Bagwelld3825d52012-03-25 23:26:11 -0700433 features->type != WIRELESS &&
Chris Bagwell6e8ec532011-10-26 22:24:22 -0700434 features->device_type == BTN_TOOL_PEN) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800435 do {
436 rep_data[0] = 2;
437 rep_data[1] = 2;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700438 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
439 report_id, rep_data, 2, 1);
Ping Chengec67bbe2009-12-15 00:35:24 -0800440 if (error >= 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700441 error = wacom_get_report(intf,
442 WAC_HID_FEATURE_REPORT,
443 report_id, rep_data, 2, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700444 } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
Ping Chengec67bbe2009-12-15 00:35:24 -0800445 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700446
447 kfree(rep_data);
448
449 return error < 0 ? error : 0;
450}
451
Ping Chengec67bbe2009-12-15 00:35:24 -0800452static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
453 struct wacom_features *features)
454{
455 int error = 0;
456 struct usb_host_interface *interface = intf->cur_altsetting;
457 struct hid_descriptor *hid_desc;
458
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700459 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800460 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700461 features->x_fuzz = 4;
462 features->y_fuzz = 4;
463 features->pressure_fuzz = 0;
464 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800465
Chris Bagwelld3825d52012-03-25 23:26:11 -0700466 /*
467 * The wireless device HID is basic and layout conflicts with
468 * other tablets (monitor and touch interface can look like pen).
469 * Skip the query for this type and modify defaults based on
470 * interface number.
471 */
472 if (features->type == WIRELESS) {
473 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
474 features->device_type = 0;
475 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
476 features->device_type = BTN_TOOL_DOUBLETAP;
477 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
478 }
479 }
480
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700481 /* only Tablet PCs and Bamboo P&T need to retrieve the info */
Ping Cheng4a880812010-09-05 12:25:40 -0700482 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
483 (features->type != BAMBOO_PT))
Ping Chengec67bbe2009-12-15 00:35:24 -0800484 goto out;
485
486 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
487 if (usb_get_extra_descriptor(&interface->endpoint[0],
488 HID_DEVICET_REPORT, &hid_desc)) {
489 printk("wacom: can not retrieve extra class descriptor\n");
490 error = 1;
491 goto out;
492 }
493 }
494 error = wacom_parse_hid(intf, hid_desc, features);
495 if (error)
496 goto out;
497
Ping Chengec67bbe2009-12-15 00:35:24 -0800498 out:
499 return error;
500}
501
Ping Cheng4492eff2010-03-19 22:18:15 -0700502struct wacom_usbdev_data {
503 struct list_head list;
504 struct kref kref;
505 struct usb_device *dev;
506 struct wacom_shared shared;
507};
508
509static LIST_HEAD(wacom_udev_list);
510static DEFINE_MUTEX(wacom_udev_list_lock);
511
512static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
513{
514 struct wacom_usbdev_data *data;
515
516 list_for_each_entry(data, &wacom_udev_list, list) {
517 if (data->dev == dev) {
518 kref_get(&data->kref);
519 return data;
520 }
521 }
522
523 return NULL;
524}
525
526static int wacom_add_shared_data(struct wacom_wac *wacom,
527 struct usb_device *dev)
528{
529 struct wacom_usbdev_data *data;
530 int retval = 0;
531
532 mutex_lock(&wacom_udev_list_lock);
533
534 data = wacom_get_usbdev_data(dev);
535 if (!data) {
536 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
537 if (!data) {
538 retval = -ENOMEM;
539 goto out;
540 }
541
542 kref_init(&data->kref);
543 data->dev = dev;
544 list_add_tail(&data->list, &wacom_udev_list);
545 }
546
547 wacom->shared = &data->shared;
548
549out:
550 mutex_unlock(&wacom_udev_list_lock);
551 return retval;
552}
553
554static void wacom_release_shared_data(struct kref *kref)
555{
556 struct wacom_usbdev_data *data =
557 container_of(kref, struct wacom_usbdev_data, kref);
558
559 mutex_lock(&wacom_udev_list_lock);
560 list_del(&data->list);
561 mutex_unlock(&wacom_udev_list_lock);
562
563 kfree(data);
564}
565
566static void wacom_remove_shared_data(struct wacom_wac *wacom)
567{
568 struct wacom_usbdev_data *data;
569
570 if (wacom->shared) {
571 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
572 kref_put(&data->kref, wacom_release_shared_data);
573 wacom->shared = NULL;
574 }
575}
576
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700577static int wacom_led_control(struct wacom *wacom)
578{
579 unsigned char *buf;
Ping Cheng09e7d942011-10-04 23:51:14 -0700580 int retval, led = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700581
582 buf = kzalloc(9, GFP_KERNEL);
583 if (!buf)
584 return -ENOMEM;
585
Jason Gerecke246835f2011-12-12 00:12:04 -0800586 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
587 wacom->wacom_wac.features.type == WACOM_24HD)
Ping Cheng09e7d942011-10-04 23:51:14 -0700588 led = (wacom->led.select[1] << 4) | 0x40;
589
590 led |= wacom->led.select[0] | 0x4;
591
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700592 buf[0] = WAC_CMD_LED_CONTROL;
Ping Cheng09e7d942011-10-04 23:51:14 -0700593 buf[1] = led;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700594 buf[2] = wacom->led.llv;
595 buf[3] = wacom->led.hlv;
596 buf[4] = wacom->led.img_lum;
597
598 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
599 buf, 9, WAC_CMD_RETRIES);
600 kfree(buf);
601
602 return retval;
603}
604
605static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
606{
607 unsigned char *buf;
608 int i, retval;
609
610 buf = kzalloc(259, GFP_KERNEL);
611 if (!buf)
612 return -ENOMEM;
613
614 /* Send 'start' command */
615 buf[0] = WAC_CMD_ICON_START;
616 buf[1] = 1;
617 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
618 buf, 2, WAC_CMD_RETRIES);
619 if (retval < 0)
620 goto out;
621
622 buf[0] = WAC_CMD_ICON_XFER;
623 buf[1] = button_id & 0x07;
624 for (i = 0; i < 4; i++) {
625 buf[2] = i;
626 memcpy(buf + 3, img + i * 256, 256);
627
628 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
629 buf, 259, WAC_CMD_RETRIES);
630 if (retval < 0)
631 break;
632 }
633
634 /* Send 'stop' */
635 buf[0] = WAC_CMD_ICON_START;
636 buf[1] = 0;
637 wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
638 buf, 2, WAC_CMD_RETRIES);
639
640out:
641 kfree(buf);
642 return retval;
643}
644
Ping Cheng09e7d942011-10-04 23:51:14 -0700645static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700646 const char *buf, size_t count)
647{
648 struct wacom *wacom = dev_get_drvdata(dev);
649 unsigned int id;
650 int err;
651
652 err = kstrtouint(buf, 10, &id);
653 if (err)
654 return err;
655
656 mutex_lock(&wacom->lock);
657
Ping Cheng09e7d942011-10-04 23:51:14 -0700658 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700659 err = wacom_led_control(wacom);
660
661 mutex_unlock(&wacom->lock);
662
663 return err < 0 ? err : count;
664}
665
Ping Cheng09e7d942011-10-04 23:51:14 -0700666#define DEVICE_LED_SELECT_ATTR(SET_ID) \
667static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
668 struct device_attribute *attr, const char *buf, size_t count) \
669{ \
670 return wacom_led_select_store(dev, SET_ID, buf, count); \
671} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700672static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
673 struct device_attribute *attr, char *buf) \
674{ \
675 struct wacom *wacom = dev_get_drvdata(dev); \
676 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
677} \
678static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
679 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700680 wacom_led##SET_ID##_select_store)
681
682DEVICE_LED_SELECT_ATTR(0);
683DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700684
685static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
686 const char *buf, size_t count)
687{
688 unsigned int value;
689 int err;
690
691 err = kstrtouint(buf, 10, &value);
692 if (err)
693 return err;
694
695 mutex_lock(&wacom->lock);
696
697 *dest = value & 0x7f;
698 err = wacom_led_control(wacom);
699
700 mutex_unlock(&wacom->lock);
701
702 return err < 0 ? err : count;
703}
704
705#define DEVICE_LUMINANCE_ATTR(name, field) \
706static ssize_t wacom_##name##_luminance_store(struct device *dev, \
707 struct device_attribute *attr, const char *buf, size_t count) \
708{ \
709 struct wacom *wacom = dev_get_drvdata(dev); \
710 \
711 return wacom_luminance_store(wacom, &wacom->led.field, \
712 buf, count); \
713} \
714static DEVICE_ATTR(name##_luminance, S_IWUSR, \
715 NULL, wacom_##name##_luminance_store)
716
717DEVICE_LUMINANCE_ATTR(status0, llv);
718DEVICE_LUMINANCE_ATTR(status1, hlv);
719DEVICE_LUMINANCE_ATTR(buttons, img_lum);
720
721static ssize_t wacom_button_image_store(struct device *dev, int button_id,
722 const char *buf, size_t count)
723{
724 struct wacom *wacom = dev_get_drvdata(dev);
725 int err;
726
727 if (count != 1024)
728 return -EINVAL;
729
730 mutex_lock(&wacom->lock);
731
732 err = wacom_led_putimage(wacom, button_id, buf);
733
734 mutex_unlock(&wacom->lock);
735
736 return err < 0 ? err : count;
737}
738
739#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
740static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
741 struct device_attribute *attr, const char *buf, size_t count) \
742{ \
743 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
744} \
745static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
746 NULL, wacom_btnimg##BUTTON_ID##_store)
747
748DEVICE_BTNIMG_ATTR(0);
749DEVICE_BTNIMG_ATTR(1);
750DEVICE_BTNIMG_ATTR(2);
751DEVICE_BTNIMG_ATTR(3);
752DEVICE_BTNIMG_ATTR(4);
753DEVICE_BTNIMG_ATTR(5);
754DEVICE_BTNIMG_ATTR(6);
755DEVICE_BTNIMG_ATTR(7);
756
Ping Cheng09e7d942011-10-04 23:51:14 -0700757static struct attribute *cintiq_led_attrs[] = {
758 &dev_attr_status_led0_select.attr,
759 &dev_attr_status_led1_select.attr,
760 NULL
761};
762
763static struct attribute_group cintiq_led_attr_group = {
764 .name = "wacom_led",
765 .attrs = cintiq_led_attrs,
766};
767
768static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700769 &dev_attr_status0_luminance.attr,
770 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700771 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700772 &dev_attr_buttons_luminance.attr,
773 &dev_attr_button0_rawimg.attr,
774 &dev_attr_button1_rawimg.attr,
775 &dev_attr_button2_rawimg.attr,
776 &dev_attr_button3_rawimg.attr,
777 &dev_attr_button4_rawimg.attr,
778 &dev_attr_button5_rawimg.attr,
779 &dev_attr_button6_rawimg.attr,
780 &dev_attr_button7_rawimg.attr,
781 NULL
782};
783
Ping Cheng09e7d942011-10-04 23:51:14 -0700784static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700785 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700786 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700787};
788
789static int wacom_initialize_leds(struct wacom *wacom)
790{
791 int error;
792
Ping Cheng09e7d942011-10-04 23:51:14 -0700793 /* Initialize default values */
794 switch (wacom->wacom_wac.features.type) {
795 case INTUOS4:
796 case INTUOS4L:
797 wacom->led.select[0] = 0;
798 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700799 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700800 wacom->led.hlv = 20;
801 wacom->led.img_lum = 10;
Ping Cheng09e7d942011-10-04 23:51:14 -0700802 error = sysfs_create_group(&wacom->intf->dev.kobj,
803 &intuos4_led_attr_group);
804 break;
805
Jason Gerecke246835f2011-12-12 00:12:04 -0800806 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700807 case WACOM_21UX2:
808 wacom->led.select[0] = 0;
809 wacom->led.select[1] = 0;
810 wacom->led.llv = 0;
811 wacom->led.hlv = 0;
812 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700813
814 error = sysfs_create_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700815 &cintiq_led_attr_group);
816 break;
817
818 default:
819 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700820 }
821
Ping Cheng09e7d942011-10-04 23:51:14 -0700822 if (error) {
Greg Kroah-Hartman1b5ca922012-05-01 20:57:07 -0400823 dev_err(&wacom->wacom_wac.input->dev,
Ping Cheng09e7d942011-10-04 23:51:14 -0700824 "cannot create sysfs group err: %d\n", error);
825 return error;
826 }
827 wacom_led_control(wacom);
828
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700829 return 0;
830}
831
832static void wacom_destroy_leds(struct wacom *wacom)
833{
Ping Cheng09e7d942011-10-04 23:51:14 -0700834 switch (wacom->wacom_wac.features.type) {
835 case INTUOS4:
836 case INTUOS4L:
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700837 sysfs_remove_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700838 &intuos4_led_attr_group);
839 break;
840
Jason Gerecke246835f2011-12-12 00:12:04 -0800841 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700842 case WACOM_21UX2:
843 sysfs_remove_group(&wacom->intf->dev.kobj,
844 &cintiq_led_attr_group);
845 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700846 }
847}
848
Chris Bagwella1d552c2012-03-25 23:26:30 -0700849static enum power_supply_property wacom_battery_props[] = {
850 POWER_SUPPLY_PROP_CAPACITY
851};
852
853static int wacom_battery_get_property(struct power_supply *psy,
854 enum power_supply_property psp,
855 union power_supply_propval *val)
856{
857 struct wacom *wacom = container_of(psy, struct wacom, battery);
858 int ret = 0;
859
860 switch (psp) {
861 case POWER_SUPPLY_PROP_CAPACITY:
862 val->intval =
863 wacom->wacom_wac.battery_capacity * 100 / 31;
864 break;
865 default:
866 ret = -EINVAL;
867 break;
868 }
869
870 return ret;
871}
872
873static int wacom_initialize_battery(struct wacom *wacom)
874{
875 int error = 0;
876
877 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) {
878 wacom->battery.properties = wacom_battery_props;
879 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
880 wacom->battery.get_property = wacom_battery_get_property;
881 wacom->battery.name = "wacom_battery";
882 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
883 wacom->battery.use_for_apm = 0;
884
885 error = power_supply_register(&wacom->usbdev->dev,
886 &wacom->battery);
887 }
888
889 return error;
890}
891
892static void wacom_destroy_battery(struct wacom *wacom)
893{
894 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR)
895 power_supply_unregister(&wacom->battery);
896}
897
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700898static int wacom_register_input(struct wacom *wacom)
899{
900 struct input_dev *input_dev;
901 struct usb_interface *intf = wacom->intf;
902 struct usb_device *dev = interface_to_usbdev(intf);
903 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
904 int error;
905
906 input_dev = input_allocate_device();
907 if (!input_dev)
908 return -ENOMEM;
909
910 input_dev->name = wacom_wac->name;
911 input_dev->dev.parent = &intf->dev;
912 input_dev->open = wacom_open;
913 input_dev->close = wacom_close;
914 usb_to_input_id(dev, &input_dev->id);
915 input_set_drvdata(input_dev, wacom);
916
917 wacom_wac->input = input_dev;
918 wacom_setup_input_capabilities(input_dev, wacom_wac);
919
920 error = input_register_device(input_dev);
921 if (error) {
922 input_free_device(input_dev);
923 wacom_wac->input = NULL;
924 }
925
926 return error;
927}
928
Chris Bagwell16bf2882012-03-25 23:26:20 -0700929static void wacom_wireless_work(struct work_struct *work)
930{
931 struct wacom *wacom = container_of(work, struct wacom, work);
932 struct usb_device *usbdev = wacom->usbdev;
933 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
934
935 /*
936 * Regardless if this is a disconnect or a new tablet,
937 * remove any existing input devices.
938 */
939
940 /* Stylus interface */
941 wacom = usb_get_intfdata(usbdev->config->interface[1]);
942 if (wacom->wacom_wac.input)
943 input_unregister_device(wacom->wacom_wac.input);
944 wacom->wacom_wac.input = 0;
945
946 /* Touch interface */
947 wacom = usb_get_intfdata(usbdev->config->interface[2]);
948 if (wacom->wacom_wac.input)
949 input_unregister_device(wacom->wacom_wac.input);
950 wacom->wacom_wac.input = 0;
951
952 if (wacom_wac->pid == 0) {
953 printk(KERN_INFO "wacom: wireless tablet disconnected\n");
954 } else {
955 const struct usb_device_id *id = wacom_ids;
956
957 printk(KERN_INFO
958 "wacom: wireless tablet connected with PID %x\n",
959 wacom_wac->pid);
960
961 while (id->match_flags) {
962 if (id->idVendor == USB_VENDOR_ID_WACOM &&
963 id->idProduct == wacom_wac->pid)
964 break;
965 id++;
966 }
967
968 if (!id->match_flags) {
969 printk(KERN_INFO
970 "wacom: ignorning unknown PID.\n");
971 return;
972 }
973
974 /* Stylus interface */
975 wacom = usb_get_intfdata(usbdev->config->interface[1]);
976 wacom_wac = &wacom->wacom_wac;
977 wacom_wac->features =
978 *((struct wacom_features *)id->driver_info);
979 wacom_wac->features.device_type = BTN_TOOL_PEN;
980 wacom_register_input(wacom);
981
982 /* Touch interface */
983 wacom = usb_get_intfdata(usbdev->config->interface[2]);
984 wacom_wac = &wacom->wacom_wac;
985 wacom_wac->features =
986 *((struct wacom_features *)id->driver_info);
987 wacom_wac->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
988 wacom_wac->features.device_type = BTN_TOOL_FINGER;
989 wacom_set_phy_from_res(&wacom_wac->features);
990 wacom_wac->features.x_max = wacom_wac->features.y_max = 4096;
991 wacom_register_input(wacom);
992 }
993}
994
Ping Cheng3bea7332006-07-13 18:01:36 -0700995static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
996{
997 struct usb_device *dev = interface_to_usbdev(intf);
998 struct usb_endpoint_descriptor *endpoint;
999 struct wacom *wacom;
1000 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001001 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001002 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001003
Jason Childse33da8a2010-02-17 22:38:31 -08001004 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001005 return -EINVAL;
1006
Ping Cheng3bea7332006-07-13 18:01:36 -07001007 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001008 if (!wacom)
1009 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001010
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001011 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001012 wacom_wac->features = *((struct wacom_features *)id->driver_info);
1013 features = &wacom_wac->features;
1014 if (features->pktlen > WACOM_PKGLEN_MAX) {
1015 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001016 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -08001017 }
1018
Daniel Mack997ea582010-04-12 13:17:25 +02001019 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
1020 GFP_KERNEL, &wacom->data_dma);
Jason Childse33da8a2010-02-17 22:38:31 -08001021 if (!wacom_wac->data) {
1022 error = -ENOMEM;
1023 goto fail1;
1024 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001025
1026 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -08001027 if (!wacom->irq) {
1028 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001029 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -08001030 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001031
1032 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001033 wacom->intf = intf;
1034 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001035 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001036 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
1037 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
1038
Ping Cheng545f4e92008-11-24 11:44:27 -05001039 endpoint = &intf->cur_altsetting->endpoint[0].desc;
1040
Ping Chengec67bbe2009-12-15 00:35:24 -08001041 /* Retrieve the physical and logical size for OEM devices */
1042 error = wacom_retrieve_hid_descriptor(intf, features);
1043 if (error)
Alexander Strakh4b6d4432011-02-11 00:44:41 -08001044 goto fail3;
Ping Cheng545f4e92008-11-24 11:44:27 -05001045
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001046 wacom_setup_device_quirks(features);
1047
Ping Cheng49b764a2010-02-20 00:53:49 -08001048 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1049
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001050 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -08001051 /* Append the device type to the name */
1052 strlcat(wacom_wac->name,
1053 features->device_type == BTN_TOOL_PEN ?
1054 " Pen" : " Finger",
1055 sizeof(wacom_wac->name));
Ping Cheng4492eff2010-03-19 22:18:15 -07001056
1057 error = wacom_add_shared_data(wacom_wac, dev);
1058 if (error)
1059 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -08001060 }
1061
Ping Cheng3bea7332006-07-13 18:01:36 -07001062 usb_fill_int_urb(wacom->irq, dev,
1063 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -08001064 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -07001065 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -07001066 wacom->irq->transfer_dma = wacom->data_dma;
1067 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1068
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001069 error = wacom_initialize_leds(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001070 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -07001071 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -07001072
Chris Bagwella1d552c2012-03-25 23:26:30 -07001073 error = wacom_initialize_battery(wacom);
1074 if (error)
1075 goto fail5;
1076
Chris Bagwelld3825d52012-03-25 23:26:11 -07001077 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1078 error = wacom_register_input(wacom);
1079 if (error)
Chris Bagwella1d552c2012-03-25 23:26:30 -07001080 goto fail6;
Chris Bagwelld3825d52012-03-25 23:26:11 -07001081 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001082
Ping Chengec67bbe2009-12-15 00:35:24 -08001083 /* Note that if query fails it is not a hard failure */
1084 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -07001085
1086 usb_set_intfdata(intf, wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001087
1088 if (features->quirks & WACOM_QUIRK_MONITOR) {
1089 if (usb_submit_urb(wacom->irq, GFP_KERNEL))
1090 goto fail5;
1091 }
1092
Ping Cheng3bea7332006-07-13 18:01:36 -07001093 return 0;
1094
Chris Bagwella1d552c2012-03-25 23:26:30 -07001095 fail6: wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001096 fail5: wacom_destroy_leds(wacom);
Ping Cheng4492eff2010-03-19 22:18:15 -07001097 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001098 fail3: usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +02001099 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001100 fail1: kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001101 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001102}
1103
1104static void wacom_disconnect(struct usb_interface *intf)
1105{
Oliver Neukume7224092008-04-15 01:31:57 -04001106 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001107
1108 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001109
1110 usb_kill_urb(wacom->irq);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001111 cancel_work_sync(&wacom->work);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001112 if (wacom->wacom_wac.input)
1113 input_unregister_device(wacom->wacom_wac.input);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001114 wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001115 wacom_destroy_leds(wacom);
Oliver Neukume7224092008-04-15 01:31:57 -04001116 usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +02001117 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001118 wacom->wacom_wac.data, wacom->data_dma);
1119 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -04001120 kfree(wacom);
1121}
1122
1123static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
1124{
1125 struct wacom *wacom = usb_get_intfdata(intf);
1126
1127 mutex_lock(&wacom->lock);
1128 usb_kill_urb(wacom->irq);
1129 mutex_unlock(&wacom->lock);
1130
1131 return 0;
1132}
1133
1134static int wacom_resume(struct usb_interface *intf)
1135{
1136 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001137 struct wacom_features *features = &wacom->wacom_wac.features;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001138 int rv = 0;
Oliver Neukume7224092008-04-15 01:31:57 -04001139
1140 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -07001141
1142 /* switch to wacom mode first */
1143 wacom_query_tablet_data(intf, features);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001144 wacom_led_control(wacom);
Ping Cheng38101472010-04-13 23:07:52 -07001145
Chris Bagwelld3825d52012-03-25 23:26:11 -07001146 if ((wacom->open || features->quirks & WACOM_QUIRK_MONITOR)
1147 && usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001148 rv = -EIO;
Ping Cheng38101472010-04-13 23:07:52 -07001149
Oliver Neukume7224092008-04-15 01:31:57 -04001150 mutex_unlock(&wacom->lock);
1151
1152 return rv;
1153}
1154
1155static int wacom_reset_resume(struct usb_interface *intf)
1156{
1157 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001158}
1159
1160static struct usb_driver wacom_driver = {
1161 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001162 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001163 .probe = wacom_probe,
1164 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -04001165 .suspend = wacom_suspend,
1166 .resume = wacom_resume,
1167 .reset_resume = wacom_reset_resume,
1168 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -07001169};
1170
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -08001171module_usb_driver(wacom_driver);