blob: d7713388a953d518d24d672649e5e1bcf24e8aef [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;
Ping Cheng3bea7332006-07-13 18:01:36 -0700102 int retval;
103
104 switch (urb->status) {
105 case 0:
106 /* success */
107 break;
108 case -ECONNRESET:
109 case -ENOENT:
110 case -ESHUTDOWN:
111 /* this urb is terminated, clean up */
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400112 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -0700113 return;
114 default:
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400115 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
Ping Cheng3bea7332006-07-13 18:01:36 -0700116 goto exit;
117 }
118
Dmitry Torokhov95dd3b32010-03-19 22:18:15 -0700119 wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
Ping Cheng3bea7332006-07-13 18:01:36 -0700120
121 exit:
Oliver Neukume7224092008-04-15 01:31:57 -0400122 usb_mark_last_busy(wacom->usbdev);
Dmitry Torokhov73a97f42010-03-19 22:18:15 -0700123 retval = usb_submit_urb(urb, GFP_ATOMIC);
Ping Cheng3bea7332006-07-13 18:01:36 -0700124 if (retval)
125 err ("%s - usb_submit_urb failed with result %d",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400126 __func__, retval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700127}
128
Ping Cheng3bea7332006-07-13 18:01:36 -0700129static int wacom_open(struct input_dev *dev)
130{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400131 struct wacom *wacom = input_get_drvdata(dev);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700132 int retval = 0;
133
134 if (usb_autopm_get_interface(wacom->intf) < 0)
135 return -EIO;
Ping Cheng3bea7332006-07-13 18:01:36 -0700136
Oliver Neukume7224092008-04-15 01:31:57 -0400137 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700138
Oliver Neukume7224092008-04-15 01:31:57 -0400139 if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700140 retval = -EIO;
141 goto out;
Oliver Neukume7224092008-04-15 01:31:57 -0400142 }
143
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700144 wacom->open = true;
Oliver Neukume7224092008-04-15 01:31:57 -0400145 wacom->intf->needs_remote_wakeup = 1;
146
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700147out:
Oliver Neukume7224092008-04-15 01:31:57 -0400148 mutex_unlock(&wacom->lock);
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700149 usb_autopm_put_interface(wacom->intf);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700150 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -0700151}
152
153static void wacom_close(struct input_dev *dev)
154{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400155 struct wacom *wacom = input_get_drvdata(dev);
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700156 int autopm_error;
157
158 autopm_error = usb_autopm_get_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700159
Oliver Neukume7224092008-04-15 01:31:57 -0400160 mutex_lock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -0700161 usb_kill_urb(wacom->irq);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -0700162 wacom->open = false;
Oliver Neukume7224092008-04-15 01:31:57 -0400163 wacom->intf->needs_remote_wakeup = 0;
164 mutex_unlock(&wacom->lock);
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -0700165
Dmitry Torokhov62ecae02010-10-10 14:24:16 -0700166 if (!autopm_error)
167 usb_autopm_put_interface(wacom->intf);
Ping Cheng3bea7332006-07-13 18:01:36 -0700168}
169
Chris Bagwell16bf2882012-03-25 23:26:20 -0700170/*
171 * Static values for max X/Y and resolution of Pen interface is stored in
172 * features. This mean physical size of active area can be computed.
173 * This is useful to do when Pen and Touch have same active area of tablet.
174 * This means for Touch device, we only need to find max X/Y value and we
175 * have enough information to compute resolution of touch.
176 */
177static void wacom_set_phy_from_res(struct wacom_features *features)
178{
179 features->x_phy = (features->x_max * 100) / features->x_resolution;
180 features->y_phy = (features->y_max * 100) / features->y_resolution;
181}
182
Chris Bagwell41343612011-10-26 22:32:52 -0700183static int wacom_parse_logical_collection(unsigned char *report,
184 struct wacom_features *features)
185{
186 int length = 0;
187
188 if (features->type == BAMBOO_PT) {
189
190 /* Logical collection is only used by 3rd gen Bamboo Touch */
191 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
Ping Cheng8b4a0c1f2012-01-31 00:07:33 -0800192 features->device_type = BTN_TOOL_FINGER;
Chris Bagwell41343612011-10-26 22:32:52 -0700193
Chris Bagwell16bf2882012-03-25 23:26:20 -0700194 wacom_set_phy_from_res(features);
Chris Bagwell41343612011-10-26 22:32:52 -0700195
196 features->x_max = features->y_max =
197 get_unaligned_le16(&report[10]);
198
199 length = 11;
200 }
201 return length;
202}
203
Chris Bagwell428f8582011-10-26 22:26:59 -0700204/*
205 * Interface Descriptor of wacom devices can be incomplete and
206 * inconsistent so wacom_features table is used to store stylus
207 * device's packet lengths, various maximum values, and tablet
208 * resolution based on product ID's.
209 *
210 * For devices that contain 2 interfaces, wacom_features table is
211 * inaccurate for the touch interface. Since the Interface Descriptor
212 * for touch interfaces has pretty complete data, this function exists
213 * to query tablet for this missing information instead of hard coding in
214 * an additional table.
215 *
216 * A typical Interface Descriptor for a stylus will contain a
217 * boot mouse application collection that is not of interest and this
218 * function will ignore it.
219 *
220 * It also contains a digitizer application collection that also is not
221 * of interest since any information it contains would be duplicate
222 * of what is in wacom_features. Usually it defines a report of an array
223 * of bytes that could be used as max length of the stylus packet returned.
224 * If it happens to define a Digitizer-Stylus Physical Collection then
225 * the X and Y logical values contain valid data but it is ignored.
226 *
227 * A typical Interface Descriptor for a touch interface will contain a
228 * Digitizer-Finger Physical Collection which will define both logical
229 * X/Y maximum as well as the physical size of tablet. Since touch
230 * interfaces haven't supported pressure or distance, this is enough
231 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700232 *
233 * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
234 * Collection. Instead they define a Logical Collection with a single
235 * Logical Maximum for both X and Y.
Chris Bagwell428f8582011-10-26 22:26:59 -0700236 */
237static int wacom_parse_hid(struct usb_interface *intf,
238 struct hid_descriptor *hid_desc,
Ping Chengec67bbe2009-12-15 00:35:24 -0800239 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500240{
241 struct usb_device *dev = interface_to_usbdev(intf);
Ping Chengec67bbe2009-12-15 00:35:24 -0800242 char limit = 0;
243 /* result has to be defined as int for some devices */
244 int result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500245 int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
246 unsigned char *report;
247
248 report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
249 if (!report)
250 return -ENOMEM;
251
252 /* retrive report descriptors */
253 do {
254 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
255 USB_REQ_GET_DESCRIPTOR,
256 USB_RECIP_INTERFACE | USB_DIR_IN,
257 HID_DEVICET_REPORT << 8,
258 intf->altsetting[0].desc.bInterfaceNumber, /* interface */
259 report,
260 hid_desc->wDescriptorLength,
261 5000); /* 5 secs */
Ping Chenga417ea42011-08-16 00:17:56 -0700262 } while (result < 0 && limit++ < WAC_MSG_RETRIES);
Ping Cheng545f4e92008-11-24 11:44:27 -0500263
Ping Cheng384318e2009-04-28 07:49:54 -0700264 /* No need to parse the Descriptor. It isn't an error though */
Ping Cheng545f4e92008-11-24 11:44:27 -0500265 if (result < 0)
266 goto out;
267
268 for (i = 0; i < hid_desc->wDescriptorLength; i++) {
269
270 switch (report[i]) {
271 case HID_USAGE_PAGE:
272 switch (report[i + 1]) {
273 case HID_USAGE_PAGE_DIGITIZER:
274 usage = WCM_DIGITIZER;
275 i++;
276 break;
277
278 case HID_USAGE_PAGE_DESKTOP:
279 usage = WCM_DESKTOP;
280 i++;
281 break;
282 }
283 break;
284
285 case HID_USAGE:
286 switch (report[i + 1]) {
287 case HID_USAGE_X:
288 if (usage == WCM_DESKTOP) {
289 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800290 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800291 if (features->type == TABLETPC2FG) {
292 /* need to reset back */
293 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Chengec67bbe2009-12-15 00:35:24 -0800294 }
Ping Cheng4a880812010-09-05 12:25:40 -0700295 if (features->type == BAMBOO_PT) {
296 /* need to reset back */
297 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng4a880812010-09-05 12:25:40 -0700298 features->x_phy =
299 get_unaligned_le16(&report[i + 5]);
300 features->x_max =
301 get_unaligned_le16(&report[i + 8]);
302 i += 15;
303 } else {
304 features->x_max =
305 get_unaligned_le16(&report[i + 3]);
306 features->x_phy =
307 get_unaligned_le16(&report[i + 6]);
308 features->unit = report[i + 9];
309 features->unitExpo = report[i + 11];
310 i += 12;
311 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500312 } else if (pen) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800313 /* penabled only accepts exact bytes of data */
314 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800315 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800316 features->device_type = BTN_TOOL_PEN;
Ping Cheng545f4e92008-11-24 11:44:27 -0500317 features->x_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700318 get_unaligned_le16(&report[i + 3]);
Ping Cheng545f4e92008-11-24 11:44:27 -0500319 i += 4;
320 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500321 }
322 break;
323
324 case HID_USAGE_Y:
Ping Chengec67bbe2009-12-15 00:35:24 -0800325 if (usage == WCM_DESKTOP) {
326 if (finger) {
Ping Cheng84eb5aa2011-03-12 20:35:18 -0800327 features->device_type = BTN_TOOL_FINGER;
Ping Chengec67bbe2009-12-15 00:35:24 -0800328 if (features->type == TABLETPC2FG) {
329 /* need to reset back */
330 features->pktlen = WACOM_PKGLEN_TPC2FG;
Ping Chengec67bbe2009-12-15 00:35:24 -0800331 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700332 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800333 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700334 get_unaligned_le16(&report[i + 6]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800335 i += 7;
Ping Cheng4a880812010-09-05 12:25:40 -0700336 } else if (features->type == BAMBOO_PT) {
337 /* need to reset back */
338 features->pktlen = WACOM_PKGLEN_BBTOUCH;
Ping Cheng4a880812010-09-05 12:25:40 -0700339 features->y_phy =
340 get_unaligned_le16(&report[i + 3]);
341 features->y_max =
342 get_unaligned_le16(&report[i + 6]);
343 i += 12;
Ping Chengec67bbe2009-12-15 00:35:24 -0800344 } else {
345 features->y_max =
346 features->x_max;
347 features->y_phy =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700348 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800349 i += 4;
350 }
351 } else if (pen) {
352 /* penabled only accepts exact bytes of data */
353 if (features->type == TABLETPC2FG)
Ping Cheng57e413d2010-03-01 23:50:24 -0800354 features->pktlen = WACOM_PKGLEN_GRAPHIRE;
Ping Chengec67bbe2009-12-15 00:35:24 -0800355 features->device_type = BTN_TOOL_PEN;
356 features->y_max =
Dmitry Torokhov252f7762010-03-19 22:33:38 -0700357 get_unaligned_le16(&report[i + 3]);
Ping Chengec67bbe2009-12-15 00:35:24 -0800358 i += 4;
359 }
360 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500361 break;
362
363 case HID_USAGE_FINGER:
364 finger = 1;
365 i++;
366 break;
367
Chris Bagwell428f8582011-10-26 22:26:59 -0700368 /*
369 * Requiring Stylus Usage will ignore boot mouse
370 * X/Y values and some cases of invalid Digitizer X/Y
371 * values commonly reported.
372 */
Ping Cheng545f4e92008-11-24 11:44:27 -0500373 case HID_USAGE_STYLUS:
374 pen = 1;
375 i++;
376 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500377 }
378 break;
379
Chris Bagwell41343612011-10-26 22:32:52 -0700380 case HID_COLLECTION_END:
Ping Chengec67bbe2009-12-15 00:35:24 -0800381 /* reset UsagePage and Finger */
Ping Cheng545f4e92008-11-24 11:44:27 -0500382 finger = usage = 0;
383 break;
Chris Bagwell41343612011-10-26 22:32:52 -0700384
385 case HID_COLLECTION:
386 i++;
387 switch (report[i]) {
388 case HID_COLLECTION_LOGICAL:
389 i += wacom_parse_logical_collection(&report[i],
390 features);
391 break;
392 }
393 break;
Ping Cheng545f4e92008-11-24 11:44:27 -0500394 }
395 }
396
Ping Cheng545f4e92008-11-24 11:44:27 -0500397 out:
Ping Cheng384318e2009-04-28 07:49:54 -0700398 result = 0;
Ping Cheng545f4e92008-11-24 11:44:27 -0500399 kfree(report);
400 return result;
401}
402
Ping Chengec67bbe2009-12-15 00:35:24 -0800403static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700404{
405 unsigned char *rep_data;
Ping Chengec67bbe2009-12-15 00:35:24 -0800406 int limit = 0, report_id = 2;
407 int error = -ENOMEM;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700408
Ping Cheng3b48c912011-08-16 00:17:57 -0700409 rep_data = kmalloc(4, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700410 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800411 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700412
Ping Cheng3b48c912011-08-16 00:17:57 -0700413 /* ask to report tablet data if it is MT Tablet PC or
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700414 * not a Tablet PC */
415 if (features->type == TABLETPC2FG) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800416 do {
417 rep_data[0] = 3;
418 rep_data[1] = 4;
Ping Cheng3b48c912011-08-16 00:17:57 -0700419 rep_data[2] = 0;
420 rep_data[3] = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800421 report_id = 3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700422 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
423 report_id, rep_data, 4, 1);
Ping Chengec67bbe2009-12-15 00:35:24 -0800424 if (error >= 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700425 error = wacom_get_report(intf,
426 WAC_HID_FEATURE_REPORT,
427 report_id, rep_data, 4, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700428 } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
Chris Bagwell6e8ec532011-10-26 22:24:22 -0700429 } else if (features->type != TABLETPC &&
Chris Bagwelld3825d52012-03-25 23:26:11 -0700430 features->type != WIRELESS &&
Chris Bagwell6e8ec532011-10-26 22:24:22 -0700431 features->device_type == BTN_TOOL_PEN) {
Ping Chengec67bbe2009-12-15 00:35:24 -0800432 do {
433 rep_data[0] = 2;
434 rep_data[1] = 2;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700435 error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
436 report_id, rep_data, 2, 1);
Ping Chengec67bbe2009-12-15 00:35:24 -0800437 if (error >= 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700438 error = wacom_get_report(intf,
439 WAC_HID_FEATURE_REPORT,
440 report_id, rep_data, 2, 1);
Ping Chenga417ea42011-08-16 00:17:56 -0700441 } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
Ping Chengec67bbe2009-12-15 00:35:24 -0800442 }
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700443
444 kfree(rep_data);
445
446 return error < 0 ? error : 0;
447}
448
Ping Chengec67bbe2009-12-15 00:35:24 -0800449static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
450 struct wacom_features *features)
451{
452 int error = 0;
453 struct usb_host_interface *interface = intf->cur_altsetting;
454 struct hid_descriptor *hid_desc;
455
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700456 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800457 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700458 features->x_fuzz = 4;
459 features->y_fuzz = 4;
460 features->pressure_fuzz = 0;
461 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800462
Chris Bagwelld3825d52012-03-25 23:26:11 -0700463 /*
464 * The wireless device HID is basic and layout conflicts with
465 * other tablets (monitor and touch interface can look like pen).
466 * Skip the query for this type and modify defaults based on
467 * interface number.
468 */
469 if (features->type == WIRELESS) {
470 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
471 features->device_type = 0;
472 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
473 features->device_type = BTN_TOOL_DOUBLETAP;
474 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
475 }
476 }
477
Chris Bagwell3dc9f402010-09-12 00:08:40 -0700478 /* only Tablet PCs and Bamboo P&T need to retrieve the info */
Ping Cheng4a880812010-09-05 12:25:40 -0700479 if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
480 (features->type != BAMBOO_PT))
Ping Chengec67bbe2009-12-15 00:35:24 -0800481 goto out;
482
483 if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
484 if (usb_get_extra_descriptor(&interface->endpoint[0],
485 HID_DEVICET_REPORT, &hid_desc)) {
486 printk("wacom: can not retrieve extra class descriptor\n");
487 error = 1;
488 goto out;
489 }
490 }
491 error = wacom_parse_hid(intf, hid_desc, features);
492 if (error)
493 goto out;
494
Ping Chengec67bbe2009-12-15 00:35:24 -0800495 out:
496 return error;
497}
498
Ping Cheng4492eff2010-03-19 22:18:15 -0700499struct wacom_usbdev_data {
500 struct list_head list;
501 struct kref kref;
502 struct usb_device *dev;
503 struct wacom_shared shared;
504};
505
506static LIST_HEAD(wacom_udev_list);
507static DEFINE_MUTEX(wacom_udev_list_lock);
508
509static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
510{
511 struct wacom_usbdev_data *data;
512
513 list_for_each_entry(data, &wacom_udev_list, list) {
514 if (data->dev == dev) {
515 kref_get(&data->kref);
516 return data;
517 }
518 }
519
520 return NULL;
521}
522
523static int wacom_add_shared_data(struct wacom_wac *wacom,
524 struct usb_device *dev)
525{
526 struct wacom_usbdev_data *data;
527 int retval = 0;
528
529 mutex_lock(&wacom_udev_list_lock);
530
531 data = wacom_get_usbdev_data(dev);
532 if (!data) {
533 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
534 if (!data) {
535 retval = -ENOMEM;
536 goto out;
537 }
538
539 kref_init(&data->kref);
540 data->dev = dev;
541 list_add_tail(&data->list, &wacom_udev_list);
542 }
543
544 wacom->shared = &data->shared;
545
546out:
547 mutex_unlock(&wacom_udev_list_lock);
548 return retval;
549}
550
551static void wacom_release_shared_data(struct kref *kref)
552{
553 struct wacom_usbdev_data *data =
554 container_of(kref, struct wacom_usbdev_data, kref);
555
556 mutex_lock(&wacom_udev_list_lock);
557 list_del(&data->list);
558 mutex_unlock(&wacom_udev_list_lock);
559
560 kfree(data);
561}
562
563static void wacom_remove_shared_data(struct wacom_wac *wacom)
564{
565 struct wacom_usbdev_data *data;
566
567 if (wacom->shared) {
568 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
569 kref_put(&data->kref, wacom_release_shared_data);
570 wacom->shared = NULL;
571 }
572}
573
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700574static int wacom_led_control(struct wacom *wacom)
575{
576 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700577 int retval;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700578
579 buf = kzalloc(9, GFP_KERNEL);
580 if (!buf)
581 return -ENOMEM;
582
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700583 if (wacom->wacom_wac.features.type >= INTUOS5S &&
584 wacom->wacom_wac.features.type <= INTUOS5L) {
585 /*
586 * Touch Ring and crop mark LED luminance may take on
587 * one of four values:
588 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
589 */
590 int ring_led = wacom->led.select[0] & 0x03;
591 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
592 int crop_lum = 0;
Ping Cheng09e7d942011-10-04 23:51:14 -0700593
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700594 buf[0] = WAC_CMD_LED_CONTROL;
595 buf[1] = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
596 }
597 else {
598 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700599
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700600 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
601 wacom->wacom_wac.features.type == WACOM_24HD)
602 led |= (wacom->led.select[1] << 4) | 0x40;
603
604 buf[0] = WAC_CMD_LED_CONTROL;
605 buf[1] = led;
606 buf[2] = wacom->led.llv;
607 buf[3] = wacom->led.hlv;
608 buf[4] = wacom->led.img_lum;
609 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700610
611 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
612 buf, 9, WAC_CMD_RETRIES);
613 kfree(buf);
614
615 return retval;
616}
617
618static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
619{
620 unsigned char *buf;
621 int i, retval;
622
623 buf = kzalloc(259, GFP_KERNEL);
624 if (!buf)
625 return -ENOMEM;
626
627 /* Send 'start' command */
628 buf[0] = WAC_CMD_ICON_START;
629 buf[1] = 1;
630 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
631 buf, 2, WAC_CMD_RETRIES);
632 if (retval < 0)
633 goto out;
634
635 buf[0] = WAC_CMD_ICON_XFER;
636 buf[1] = button_id & 0x07;
637 for (i = 0; i < 4; i++) {
638 buf[2] = i;
639 memcpy(buf + 3, img + i * 256, 256);
640
641 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
642 buf, 259, WAC_CMD_RETRIES);
643 if (retval < 0)
644 break;
645 }
646
647 /* Send 'stop' */
648 buf[0] = WAC_CMD_ICON_START;
649 buf[1] = 0;
650 wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
651 buf, 2, WAC_CMD_RETRIES);
652
653out:
654 kfree(buf);
655 return retval;
656}
657
Ping Cheng09e7d942011-10-04 23:51:14 -0700658static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700659 const char *buf, size_t count)
660{
661 struct wacom *wacom = dev_get_drvdata(dev);
662 unsigned int id;
663 int err;
664
665 err = kstrtouint(buf, 10, &id);
666 if (err)
667 return err;
668
669 mutex_lock(&wacom->lock);
670
Ping Cheng09e7d942011-10-04 23:51:14 -0700671 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700672 err = wacom_led_control(wacom);
673
674 mutex_unlock(&wacom->lock);
675
676 return err < 0 ? err : count;
677}
678
Ping Cheng09e7d942011-10-04 23:51:14 -0700679#define DEVICE_LED_SELECT_ATTR(SET_ID) \
680static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
681 struct device_attribute *attr, const char *buf, size_t count) \
682{ \
683 return wacom_led_select_store(dev, SET_ID, buf, count); \
684} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700685static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
686 struct device_attribute *attr, char *buf) \
687{ \
688 struct wacom *wacom = dev_get_drvdata(dev); \
689 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
690} \
691static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
692 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700693 wacom_led##SET_ID##_select_store)
694
695DEVICE_LED_SELECT_ATTR(0);
696DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700697
698static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
699 const char *buf, size_t count)
700{
701 unsigned int value;
702 int err;
703
704 err = kstrtouint(buf, 10, &value);
705 if (err)
706 return err;
707
708 mutex_lock(&wacom->lock);
709
710 *dest = value & 0x7f;
711 err = wacom_led_control(wacom);
712
713 mutex_unlock(&wacom->lock);
714
715 return err < 0 ? err : count;
716}
717
718#define DEVICE_LUMINANCE_ATTR(name, field) \
719static ssize_t wacom_##name##_luminance_store(struct device *dev, \
720 struct device_attribute *attr, const char *buf, size_t count) \
721{ \
722 struct wacom *wacom = dev_get_drvdata(dev); \
723 \
724 return wacom_luminance_store(wacom, &wacom->led.field, \
725 buf, count); \
726} \
727static DEVICE_ATTR(name##_luminance, S_IWUSR, \
728 NULL, wacom_##name##_luminance_store)
729
730DEVICE_LUMINANCE_ATTR(status0, llv);
731DEVICE_LUMINANCE_ATTR(status1, hlv);
732DEVICE_LUMINANCE_ATTR(buttons, img_lum);
733
734static ssize_t wacom_button_image_store(struct device *dev, int button_id,
735 const char *buf, size_t count)
736{
737 struct wacom *wacom = dev_get_drvdata(dev);
738 int err;
739
740 if (count != 1024)
741 return -EINVAL;
742
743 mutex_lock(&wacom->lock);
744
745 err = wacom_led_putimage(wacom, button_id, buf);
746
747 mutex_unlock(&wacom->lock);
748
749 return err < 0 ? err : count;
750}
751
752#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
753static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
754 struct device_attribute *attr, const char *buf, size_t count) \
755{ \
756 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
757} \
758static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
759 NULL, wacom_btnimg##BUTTON_ID##_store)
760
761DEVICE_BTNIMG_ATTR(0);
762DEVICE_BTNIMG_ATTR(1);
763DEVICE_BTNIMG_ATTR(2);
764DEVICE_BTNIMG_ATTR(3);
765DEVICE_BTNIMG_ATTR(4);
766DEVICE_BTNIMG_ATTR(5);
767DEVICE_BTNIMG_ATTR(6);
768DEVICE_BTNIMG_ATTR(7);
769
Ping Cheng09e7d942011-10-04 23:51:14 -0700770static struct attribute *cintiq_led_attrs[] = {
771 &dev_attr_status_led0_select.attr,
772 &dev_attr_status_led1_select.attr,
773 NULL
774};
775
776static struct attribute_group cintiq_led_attr_group = {
777 .name = "wacom_led",
778 .attrs = cintiq_led_attrs,
779};
780
781static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700782 &dev_attr_status0_luminance.attr,
783 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700784 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700785 &dev_attr_buttons_luminance.attr,
786 &dev_attr_button0_rawimg.attr,
787 &dev_attr_button1_rawimg.attr,
788 &dev_attr_button2_rawimg.attr,
789 &dev_attr_button3_rawimg.attr,
790 &dev_attr_button4_rawimg.attr,
791 &dev_attr_button5_rawimg.attr,
792 &dev_attr_button6_rawimg.attr,
793 &dev_attr_button7_rawimg.attr,
794 NULL
795};
796
Ping Cheng09e7d942011-10-04 23:51:14 -0700797static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700798 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700799 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700800};
801
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700802static struct attribute *intuos5_led_attrs[] = {
803 &dev_attr_status0_luminance.attr,
804 &dev_attr_status_led0_select.attr,
805 NULL
806};
807
808static struct attribute_group intuos5_led_attr_group = {
809 .name = "wacom_led",
810 .attrs = intuos5_led_attrs,
811};
812
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700813static int wacom_initialize_leds(struct wacom *wacom)
814{
815 int error;
816
Ping Cheng09e7d942011-10-04 23:51:14 -0700817 /* Initialize default values */
818 switch (wacom->wacom_wac.features.type) {
819 case INTUOS4:
820 case INTUOS4L:
821 wacom->led.select[0] = 0;
822 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700823 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700824 wacom->led.hlv = 20;
825 wacom->led.img_lum = 10;
Ping Cheng09e7d942011-10-04 23:51:14 -0700826 error = sysfs_create_group(&wacom->intf->dev.kobj,
827 &intuos4_led_attr_group);
828 break;
829
Jason Gerecke246835f2011-12-12 00:12:04 -0800830 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700831 case WACOM_21UX2:
832 wacom->led.select[0] = 0;
833 wacom->led.select[1] = 0;
834 wacom->led.llv = 0;
835 wacom->led.hlv = 0;
836 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700837
838 error = sysfs_create_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700839 &cintiq_led_attr_group);
840 break;
841
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700842 case INTUOS5S:
843 case INTUOS5:
844 case INTUOS5L:
845 wacom->led.select[0] = 0;
846 wacom->led.select[1] = 0;
847 wacom->led.llv = 32;
848 wacom->led.hlv = 0;
849 wacom->led.img_lum = 0;
850
851 error = sysfs_create_group(&wacom->intf->dev.kobj,
852 &intuos5_led_attr_group);
853 break;
854
Ping Cheng09e7d942011-10-04 23:51:14 -0700855 default:
856 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700857 }
858
Ping Cheng09e7d942011-10-04 23:51:14 -0700859 if (error) {
860 dev_err(&wacom->intf->dev,
861 "cannot create sysfs group err: %d\n", error);
862 return error;
863 }
864 wacom_led_control(wacom);
865
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700866 return 0;
867}
868
869static void wacom_destroy_leds(struct wacom *wacom)
870{
Ping Cheng09e7d942011-10-04 23:51:14 -0700871 switch (wacom->wacom_wac.features.type) {
872 case INTUOS4:
873 case INTUOS4L:
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700874 sysfs_remove_group(&wacom->intf->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700875 &intuos4_led_attr_group);
876 break;
877
Jason Gerecke246835f2011-12-12 00:12:04 -0800878 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700879 case WACOM_21UX2:
880 sysfs_remove_group(&wacom->intf->dev.kobj,
881 &cintiq_led_attr_group);
882 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700883
884 case INTUOS5S:
885 case INTUOS5:
886 case INTUOS5L:
887 sysfs_remove_group(&wacom->intf->dev.kobj,
888 &intuos5_led_attr_group);
889 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700890 }
891}
892
Chris Bagwella1d552c2012-03-25 23:26:30 -0700893static enum power_supply_property wacom_battery_props[] = {
894 POWER_SUPPLY_PROP_CAPACITY
895};
896
897static int wacom_battery_get_property(struct power_supply *psy,
898 enum power_supply_property psp,
899 union power_supply_propval *val)
900{
901 struct wacom *wacom = container_of(psy, struct wacom, battery);
902 int ret = 0;
903
904 switch (psp) {
905 case POWER_SUPPLY_PROP_CAPACITY:
906 val->intval =
907 wacom->wacom_wac.battery_capacity * 100 / 31;
908 break;
909 default:
910 ret = -EINVAL;
911 break;
912 }
913
914 return ret;
915}
916
917static int wacom_initialize_battery(struct wacom *wacom)
918{
919 int error = 0;
920
921 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) {
922 wacom->battery.properties = wacom_battery_props;
923 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
924 wacom->battery.get_property = wacom_battery_get_property;
925 wacom->battery.name = "wacom_battery";
926 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
927 wacom->battery.use_for_apm = 0;
928
929 error = power_supply_register(&wacom->usbdev->dev,
930 &wacom->battery);
931 }
932
933 return error;
934}
935
936static void wacom_destroy_battery(struct wacom *wacom)
937{
938 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR)
939 power_supply_unregister(&wacom->battery);
940}
941
Chris Bagwell3aac0ef2012-03-25 23:25:45 -0700942static int wacom_register_input(struct wacom *wacom)
943{
944 struct input_dev *input_dev;
945 struct usb_interface *intf = wacom->intf;
946 struct usb_device *dev = interface_to_usbdev(intf);
947 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
948 int error;
949
950 input_dev = input_allocate_device();
951 if (!input_dev)
952 return -ENOMEM;
953
954 input_dev->name = wacom_wac->name;
955 input_dev->dev.parent = &intf->dev;
956 input_dev->open = wacom_open;
957 input_dev->close = wacom_close;
958 usb_to_input_id(dev, &input_dev->id);
959 input_set_drvdata(input_dev, wacom);
960
961 wacom_wac->input = input_dev;
962 wacom_setup_input_capabilities(input_dev, wacom_wac);
963
964 error = input_register_device(input_dev);
965 if (error) {
966 input_free_device(input_dev);
967 wacom_wac->input = NULL;
968 }
969
970 return error;
971}
972
Chris Bagwell16bf2882012-03-25 23:26:20 -0700973static void wacom_wireless_work(struct work_struct *work)
974{
975 struct wacom *wacom = container_of(work, struct wacom, work);
976 struct usb_device *usbdev = wacom->usbdev;
977 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
978
979 /*
980 * Regardless if this is a disconnect or a new tablet,
981 * remove any existing input devices.
982 */
983
984 /* Stylus interface */
985 wacom = usb_get_intfdata(usbdev->config->interface[1]);
986 if (wacom->wacom_wac.input)
987 input_unregister_device(wacom->wacom_wac.input);
988 wacom->wacom_wac.input = 0;
989
990 /* Touch interface */
991 wacom = usb_get_intfdata(usbdev->config->interface[2]);
992 if (wacom->wacom_wac.input)
993 input_unregister_device(wacom->wacom_wac.input);
994 wacom->wacom_wac.input = 0;
995
996 if (wacom_wac->pid == 0) {
997 printk(KERN_INFO "wacom: wireless tablet disconnected\n");
998 } else {
999 const struct usb_device_id *id = wacom_ids;
1000
1001 printk(KERN_INFO
1002 "wacom: wireless tablet connected with PID %x\n",
1003 wacom_wac->pid);
1004
1005 while (id->match_flags) {
1006 if (id->idVendor == USB_VENDOR_ID_WACOM &&
1007 id->idProduct == wacom_wac->pid)
1008 break;
1009 id++;
1010 }
1011
1012 if (!id->match_flags) {
1013 printk(KERN_INFO
1014 "wacom: ignorning unknown PID.\n");
1015 return;
1016 }
1017
1018 /* Stylus interface */
1019 wacom = usb_get_intfdata(usbdev->config->interface[1]);
1020 wacom_wac = &wacom->wacom_wac;
1021 wacom_wac->features =
1022 *((struct wacom_features *)id->driver_info);
1023 wacom_wac->features.device_type = BTN_TOOL_PEN;
1024 wacom_register_input(wacom);
1025
1026 /* Touch interface */
1027 wacom = usb_get_intfdata(usbdev->config->interface[2]);
1028 wacom_wac = &wacom->wacom_wac;
1029 wacom_wac->features =
1030 *((struct wacom_features *)id->driver_info);
1031 wacom_wac->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1032 wacom_wac->features.device_type = BTN_TOOL_FINGER;
1033 wacom_set_phy_from_res(&wacom_wac->features);
1034 wacom_wac->features.x_max = wacom_wac->features.y_max = 4096;
1035 wacom_register_input(wacom);
1036 }
1037}
1038
Ping Cheng3bea7332006-07-13 18:01:36 -07001039static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
1040{
1041 struct usb_device *dev = interface_to_usbdev(intf);
1042 struct usb_endpoint_descriptor *endpoint;
1043 struct wacom *wacom;
1044 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001045 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001046 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001047
Jason Childse33da8a2010-02-17 22:38:31 -08001048 if (!id->driver_info)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001049 return -EINVAL;
1050
Ping Cheng3bea7332006-07-13 18:01:36 -07001051 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Ping Cheng3bea7332006-07-13 18:01:36 -07001052
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001053 wacom_wac = &wacom->wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001054 wacom_wac->features = *((struct wacom_features *)id->driver_info);
1055 features = &wacom_wac->features;
1056 if (features->pktlen > WACOM_PKGLEN_MAX) {
1057 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001058 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -08001059 }
1060
Daniel Mack997ea582010-04-12 13:17:25 +02001061 wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
1062 GFP_KERNEL, &wacom->data_dma);
Jason Childse33da8a2010-02-17 22:38:31 -08001063 if (!wacom_wac->data) {
1064 error = -ENOMEM;
1065 goto fail1;
1066 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001067
1068 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
Jason Childse33da8a2010-02-17 22:38:31 -08001069 if (!wacom->irq) {
1070 error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001071 goto fail2;
Jason Childse33da8a2010-02-17 22:38:31 -08001072 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001073
1074 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001075 wacom->intf = intf;
1076 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001077 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001078 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
1079 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
1080
Ping Cheng545f4e92008-11-24 11:44:27 -05001081 endpoint = &intf->cur_altsetting->endpoint[0].desc;
1082
Ping Chengec67bbe2009-12-15 00:35:24 -08001083 /* Retrieve the physical and logical size for OEM devices */
1084 error = wacom_retrieve_hid_descriptor(intf, features);
1085 if (error)
Alexander Strakh4b6d4432011-02-11 00:44:41 -08001086 goto fail3;
Ping Cheng545f4e92008-11-24 11:44:27 -05001087
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001088 wacom_setup_device_quirks(features);
1089
Ping Cheng49b764a2010-02-20 00:53:49 -08001090 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1091
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001092 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -08001093 /* Append the device type to the name */
1094 strlcat(wacom_wac->name,
1095 features->device_type == BTN_TOOL_PEN ?
1096 " Pen" : " Finger",
1097 sizeof(wacom_wac->name));
Ping Cheng4492eff2010-03-19 22:18:15 -07001098
1099 error = wacom_add_shared_data(wacom_wac, dev);
1100 if (error)
1101 goto fail3;
Ping Cheng49b764a2010-02-20 00:53:49 -08001102 }
1103
Ping Cheng3bea7332006-07-13 18:01:36 -07001104 usb_fill_int_urb(wacom->irq, dev,
1105 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Ping Chengec67bbe2009-12-15 00:35:24 -08001106 wacom_wac->data, features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -07001107 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -07001108 wacom->irq->transfer_dma = wacom->data_dma;
1109 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1110
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001111 error = wacom_initialize_leds(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001112 if (error)
Ping Cheng4492eff2010-03-19 22:18:15 -07001113 goto fail4;
Ping Cheng3bea7332006-07-13 18:01:36 -07001114
Chris Bagwella1d552c2012-03-25 23:26:30 -07001115 error = wacom_initialize_battery(wacom);
1116 if (error)
1117 goto fail5;
1118
Chris Bagwelld3825d52012-03-25 23:26:11 -07001119 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1120 error = wacom_register_input(wacom);
1121 if (error)
Chris Bagwella1d552c2012-03-25 23:26:30 -07001122 goto fail6;
Chris Bagwelld3825d52012-03-25 23:26:11 -07001123 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001124
Ping Chengec67bbe2009-12-15 00:35:24 -08001125 /* Note that if query fails it is not a hard failure */
1126 wacom_query_tablet_data(intf, features);
Ping Cheng3bea7332006-07-13 18:01:36 -07001127
1128 usb_set_intfdata(intf, wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001129
1130 if (features->quirks & WACOM_QUIRK_MONITOR) {
1131 if (usb_submit_urb(wacom->irq, GFP_KERNEL))
1132 goto fail5;
1133 }
1134
Ping Cheng3bea7332006-07-13 18:01:36 -07001135 return 0;
1136
Chris Bagwella1d552c2012-03-25 23:26:30 -07001137 fail6: wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001138 fail5: wacom_destroy_leds(wacom);
Ping Cheng4492eff2010-03-19 22:18:15 -07001139 fail4: wacom_remove_shared_data(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001140 fail3: usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +02001141 fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001142 fail1: kfree(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001143 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001144}
1145
1146static void wacom_disconnect(struct usb_interface *intf)
1147{
Oliver Neukume7224092008-04-15 01:31:57 -04001148 struct wacom *wacom = usb_get_intfdata(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001149
1150 usb_set_intfdata(intf, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001151
1152 usb_kill_urb(wacom->irq);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001153 cancel_work_sync(&wacom->work);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001154 if (wacom->wacom_wac.input)
1155 input_unregister_device(wacom->wacom_wac.input);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001156 wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001157 wacom_destroy_leds(wacom);
Oliver Neukume7224092008-04-15 01:31:57 -04001158 usb_free_urb(wacom->irq);
Daniel Mack997ea582010-04-12 13:17:25 +02001159 usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001160 wacom->wacom_wac.data, wacom->data_dma);
1161 wacom_remove_shared_data(&wacom->wacom_wac);
Oliver Neukume7224092008-04-15 01:31:57 -04001162 kfree(wacom);
1163}
1164
1165static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
1166{
1167 struct wacom *wacom = usb_get_intfdata(intf);
1168
1169 mutex_lock(&wacom->lock);
1170 usb_kill_urb(wacom->irq);
1171 mutex_unlock(&wacom->lock);
1172
1173 return 0;
1174}
1175
1176static int wacom_resume(struct usb_interface *intf)
1177{
1178 struct wacom *wacom = usb_get_intfdata(intf);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001179 struct wacom_features *features = &wacom->wacom_wac.features;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001180 int rv = 0;
Oliver Neukume7224092008-04-15 01:31:57 -04001181
1182 mutex_lock(&wacom->lock);
Ping Cheng38101472010-04-13 23:07:52 -07001183
1184 /* switch to wacom mode first */
1185 wacom_query_tablet_data(intf, features);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001186 wacom_led_control(wacom);
Ping Cheng38101472010-04-13 23:07:52 -07001187
Chris Bagwelld3825d52012-03-25 23:26:11 -07001188 if ((wacom->open || features->quirks & WACOM_QUIRK_MONITOR)
1189 && usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001190 rv = -EIO;
Ping Cheng38101472010-04-13 23:07:52 -07001191
Oliver Neukume7224092008-04-15 01:31:57 -04001192 mutex_unlock(&wacom->lock);
1193
1194 return rv;
1195}
1196
1197static int wacom_reset_resume(struct usb_interface *intf)
1198{
1199 return wacom_resume(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001200}
1201
1202static struct usb_driver wacom_driver = {
1203 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001204 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001205 .probe = wacom_probe,
1206 .disconnect = wacom_disconnect,
Oliver Neukume7224092008-04-15 01:31:57 -04001207 .suspend = wacom_suspend,
1208 .resume = wacom_resume,
1209 .reset_resume = wacom_reset_resume,
1210 .supports_autosuspend = 1,
Ping Cheng3bea7332006-07-13 18:01:36 -07001211};
1212
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -08001213module_usb_driver(wacom_driver);