blob: 848842e0df1401442a719bd229c023707437923e [file] [log] [blame]
Bastien Noceraca2dcd42009-05-11 17:18:12 +02001/*
2 * Bluetooth Wacom Tablet support
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby <jirislaby@gmail.com>
10 * Copyright (c) 2006 Andrew Zabolotny <zap@homelink.ru>
11 * Copyright (c) 2009 Bastien Nocera <hadess@hadess.net>
Przemo Firszt78761ff2011-11-05 11:28:22 +000012 * Copyright (c) 2011 Przemysław Firszt <przemo@firszt.eu>
Bastien Noceraca2dcd42009-05-11 17:18:12 +020013 */
14
15/*
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
19 * any later version.
20 */
21
Joe Perches4291ee32010-12-09 19:29:03 -080022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Bastien Noceraca2dcd42009-05-11 17:18:12 +020024#include <linux/device.h>
25#include <linux/hid.h>
26#include <linux/module.h>
Przemo Firsztd13f5452012-05-10 19:23:52 +010027#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Przemo Firszt59d23342010-03-15 19:16:23 +000029#include <linux/power_supply.h>
Bastien Noceraca2dcd42009-05-11 17:18:12 +020030
31#include "hid-ids.h"
32
Przemo Firszt6245bde2012-02-28 17:19:05 +000033#define PAD_DEVICE_ID 0x0F
34
Przemo Firsztd13f5452012-05-10 19:23:52 +010035#define WAC_CMD_LED_CONTROL 0x20
36
Bastien Noceraca2dcd42009-05-11 17:18:12 +020037struct wacom_data {
38 __u16 tool;
Przemo Firszt6245bde2012-02-28 17:19:05 +000039 __u16 butstate;
Przemo Firszt7e503a32012-03-14 19:55:04 +000040 __u8 whlstate;
Przemo Firszt78761ff2011-11-05 11:28:22 +000041 __u8 features;
Przemo Firszt24709002012-02-24 13:52:32 +000042 __u32 id;
43 __u32 serial;
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +000044 unsigned char high_speed;
Przemo Firszte31e3832012-03-22 18:54:07 +000045 __u8 battery_capacity;
46 __u8 power_raw;
47 __u8 ps_connected;
Przemo Firszt59d23342010-03-15 19:16:23 +000048 struct power_supply battery;
49 struct power_supply ac;
Przemo Firsztd13f5452012-05-10 19:23:52 +010050 __u8 led_selector;
51 struct led_classdev *leds[4];
Bastien Noceraca2dcd42009-05-11 17:18:12 +020052};
53
Przemo Firszte31e3832012-03-22 18:54:07 +000054/*percent of battery capacity for Graphire
55 8th value means AC online and show 100% capacity */
56static unsigned short batcap_gr[8] = { 1, 15, 25, 35, 50, 70, 100, 100 };
Przemo Firszt4202f1d2012-03-22 18:54:08 +000057/*percent of battery capacity for Intuos4 WL, AC has a separate bit*/
58static unsigned short batcap_i4[8] = { 1, 15, 30, 45, 60, 70, 85, 100 };
Przemo Firszt59d23342010-03-15 19:16:23 +000059
60static enum power_supply_property wacom_battery_props[] = {
61 POWER_SUPPLY_PROP_PRESENT,
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -080062 POWER_SUPPLY_PROP_CAPACITY,
63 POWER_SUPPLY_PROP_SCOPE,
Przemo Firszt59d23342010-03-15 19:16:23 +000064};
65
66static enum power_supply_property wacom_ac_props[] = {
67 POWER_SUPPLY_PROP_PRESENT,
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -080068 POWER_SUPPLY_PROP_ONLINE,
69 POWER_SUPPLY_PROP_SCOPE,
Przemo Firszt59d23342010-03-15 19:16:23 +000070};
71
Przemo Firsztd13f5452012-05-10 19:23:52 +010072static void wacom_leds_set_brightness(struct led_classdev *led_dev,
73 enum led_brightness value)
74{
75 struct device *dev = led_dev->dev->parent;
76 struct hid_device *hdev;
77 struct wacom_data *wdata;
78 unsigned char *buf;
79 __u8 led = 0;
80 int i;
81
82 hdev = container_of(dev, struct hid_device, dev);
83 wdata = hid_get_drvdata(hdev);
84 for (i = 0; i < 4; ++i) {
85 if (wdata->leds[i] == led_dev)
86 wdata->led_selector = i;
87 }
88
89 led = wdata->led_selector | 0x04;
90 buf = kzalloc(9, GFP_KERNEL);
91 if (buf) {
92 buf[0] = WAC_CMD_LED_CONTROL;
93 buf[1] = led;
Przemo Firszt530a76c2012-07-31 18:27:55 +010094 buf[2] = value >> 2;
95 buf[3] = value;
Przemo Firsztd13f5452012-05-10 19:23:52 +010096 hdev->hid_output_raw_report(hdev, buf, 9, HID_FEATURE_REPORT);
97 kfree(buf);
98 }
99
100 return;
101}
102
103static enum led_brightness wacom_leds_get_brightness(struct led_classdev *led_dev)
104{
105 struct wacom_data *wdata;
106 struct device *dev = led_dev->dev->parent;
107 int value = 0;
108 int i;
109
110 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
111
112 for (i = 0; i < 4; ++i) {
113 if (wdata->leds[i] == led_dev) {
114 value = wdata->leds[i]->brightness;
115 break;
116 }
117 }
118
119 return value;
120}
121
122
123static int wacom_initialize_leds(struct hid_device *hdev)
124{
125 struct wacom_data *wdata = hid_get_drvdata(hdev);
126 struct led_classdev *led;
127 struct device *dev = &hdev->dev;
128 size_t namesz = strlen(dev_name(dev)) + 12;
129 char *name;
130 int i, ret;
131
132 wdata->led_selector = 0;
133
134 for (i = 0; i < 4; i++) {
135 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
136 if (!led) {
137 hid_warn(hdev,
138 "can't allocate memory for LED selector\n");
139 ret = -ENOMEM;
140 goto err;
141 }
142
143 name = (void *)&led[1];
144 snprintf(name, namesz, "%s:selector:%d", dev_name(dev), i);
145 led->name = name;
146 led->brightness = 0;
147 led->max_brightness = 127;
148 led->brightness_get = wacom_leds_get_brightness;
149 led->brightness_set = wacom_leds_set_brightness;
150
151 wdata->leds[i] = led;
152
153 ret = led_classdev_register(dev, wdata->leds[i]);
154
155 if (ret) {
156 wdata->leds[i] = NULL;
157 kfree(led);
158 hid_warn(hdev, "can't register LED\n");
159 goto err;
160 }
161 }
162
163err:
164 return ret;
165}
166
167static void wacom_destroy_leds(struct hid_device *hdev)
168{
169 struct wacom_data *wdata = hid_get_drvdata(hdev);
170 struct led_classdev *led;
171 int i;
172
173 for (i = 0; i < 4; ++i) {
174 if (wdata->leds[i]) {
175 led = wdata->leds[i];
176 wdata->leds[i] = NULL;
177 led_classdev_unregister(led);
178 kfree(led);
179 }
180 }
181
182}
183
Przemo Firszt59d23342010-03-15 19:16:23 +0000184static int wacom_battery_get_property(struct power_supply *psy,
185 enum power_supply_property psp,
186 union power_supply_propval *val)
187{
188 struct wacom_data *wdata = container_of(psy,
189 struct wacom_data, battery);
Przemo Firszt59d23342010-03-15 19:16:23 +0000190 int ret = 0;
191
192 switch (psp) {
193 case POWER_SUPPLY_PROP_PRESENT:
194 val->intval = 1;
195 break;
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -0800196 case POWER_SUPPLY_PROP_SCOPE:
197 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
198 break;
Przemo Firszt59d23342010-03-15 19:16:23 +0000199 case POWER_SUPPLY_PROP_CAPACITY:
Przemo Firszte31e3832012-03-22 18:54:07 +0000200 val->intval = wdata->battery_capacity;
Przemo Firszt59d23342010-03-15 19:16:23 +0000201 break;
202 default:
203 ret = -EINVAL;
204 break;
205 }
206 return ret;
207}
208
209static int wacom_ac_get_property(struct power_supply *psy,
210 enum power_supply_property psp,
211 union power_supply_propval *val)
212{
213 struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
Przemo Firszt59d23342010-03-15 19:16:23 +0000214 int ret = 0;
215
216 switch (psp) {
217 case POWER_SUPPLY_PROP_PRESENT:
218 /* fall through */
219 case POWER_SUPPLY_PROP_ONLINE:
Przemo Firszte31e3832012-03-22 18:54:07 +0000220 val->intval = wdata->ps_connected;
Przemo Firszt59d23342010-03-15 19:16:23 +0000221 break;
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -0800222 case POWER_SUPPLY_PROP_SCOPE:
223 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
224 break;
Przemo Firszt59d23342010-03-15 19:16:23 +0000225 default:
226 ret = -EINVAL;
227 break;
228 }
229 return ret;
230}
Przemo Firszt59d23342010-03-15 19:16:23 +0000231
Przemo Firsztbe4925b2012-05-15 19:32:26 +0100232static void wacom_set_features(struct hid_device *hdev, u8 speed)
Przemo Firszt78761ff2011-11-05 11:28:22 +0000233{
Przemo Firsztbe4925b2012-05-15 19:32:26 +0100234 struct wacom_data *wdata = hid_get_drvdata(hdev);
Przemo Firsztd70a2ff2012-05-15 19:32:28 +0100235 int limit, ret;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000236 __u8 rep_data[2];
237
Przemo Firsztd70a2ff2012-05-15 19:32:28 +0100238 switch (hdev->product) {
239 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
240 rep_data[0] = 0x03 ; rep_data[1] = 0x00;
Przemo Firszt06c7c312010-03-22 09:55:04 +0100241 limit = 3;
242 do {
243 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
244 HID_FEATURE_REPORT);
245 } while (ret < 0 && limit-- > 0);
246
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000247 if (ret >= 0) {
Przemo Firsztd70a2ff2012-05-15 19:32:28 +0100248 if (speed == 0)
249 rep_data[0] = 0x05;
250 else
251 rep_data[0] = 0x06;
252
253 rep_data[1] = 0x00;
254 limit = 3;
255 do {
256 ret = hdev->hid_output_raw_report(hdev,
257 rep_data, 2, HID_FEATURE_REPORT);
258 } while (ret < 0 && limit-- > 0);
259
260 if (ret >= 0) {
261 wdata->high_speed = speed;
262 return;
263 }
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000264 }
Przemo Firsztd70a2ff2012-05-15 19:32:28 +0100265
266 /*
267 * Note that if the raw queries fail, it's not a hard failure
268 * and it is safe to continue
269 */
270 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
271 rep_data[0], ret);
272 break;
273 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
274 if (speed == 1)
275 wdata->features &= ~0x20;
276 else
277 wdata->features |= 0x20;
278
279 rep_data[0] = 0x03;
280 rep_data[1] = wdata->features;
281
282 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
283 HID_FEATURE_REPORT);
284 if (ret >= 0)
285 wdata->high_speed = speed;
286 break;
Przemo Firszt06c7c312010-03-22 09:55:04 +0100287 }
288
Przemo Firszt06c7c312010-03-22 09:55:04 +0100289 return;
290}
291
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000292static ssize_t wacom_show_speed(struct device *dev,
293 struct device_attribute
294 *attr, char *buf)
295{
296 struct wacom_data *wdata = dev_get_drvdata(dev);
297
298 return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
299}
300
301static ssize_t wacom_store_speed(struct device *dev,
302 struct device_attribute *attr,
303 const char *buf, size_t count)
304{
305 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
306 int new_speed;
307
308 if (sscanf(buf, "%1d", &new_speed ) != 1)
309 return -EINVAL;
310
311 if (new_speed == 0 || new_speed == 1) {
Przemo Firsztd70a2ff2012-05-15 19:32:28 +0100312 wacom_set_features(hdev, new_speed);
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000313 return strnlen(buf, PAGE_SIZE);
314 } else
315 return -EINVAL;
316}
317
Jiri Kosinaedd21262010-11-18 16:28:43 +0100318static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000319 wacom_show_speed, wacom_store_speed);
320
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000321static int wacom_gr_parse_report(struct hid_device *hdev,
322 struct wacom_data *wdata,
323 struct input_dev *input, unsigned char *data)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200324{
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200325 int tool, x, y, rw;
326
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200327 tool = 0;
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200328 /* Get X & Y positions */
329 x = le16_to_cpu(*(__le16 *) &data[2]);
330 y = le16_to_cpu(*(__le16 *) &data[4]);
331
332 /* Get current tool identifier */
333 if (data[1] & 0x90) { /* If pen is in the in/active area */
334 switch ((data[1] >> 5) & 3) {
335 case 0: /* Pen */
336 tool = BTN_TOOL_PEN;
337 break;
338
339 case 1: /* Rubber */
340 tool = BTN_TOOL_RUBBER;
341 break;
342
343 case 2: /* Mouse with wheel */
344 case 3: /* Mouse without wheel */
345 tool = BTN_TOOL_MOUSE;
346 break;
347 }
348
349 /* Reset tool if out of active tablet area */
350 if (!(data[1] & 0x10))
351 tool = 0;
352 }
353
354 /* If tool changed, notify input subsystem */
355 if (wdata->tool != tool) {
356 if (wdata->tool) {
357 /* Completely reset old tool state */
358 if (wdata->tool == BTN_TOOL_MOUSE) {
359 input_report_key(input, BTN_LEFT, 0);
360 input_report_key(input, BTN_RIGHT, 0);
361 input_report_key(input, BTN_MIDDLE, 0);
362 input_report_abs(input, ABS_DISTANCE,
Daniel Mack987a6c02010-08-02 20:15:17 -0700363 input_abs_get_max(input, ABS_DISTANCE));
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200364 } else {
365 input_report_key(input, BTN_TOUCH, 0);
366 input_report_key(input, BTN_STYLUS, 0);
367 input_report_key(input, BTN_STYLUS2, 0);
368 input_report_abs(input, ABS_PRESSURE, 0);
369 }
370 input_report_key(input, wdata->tool, 0);
371 input_sync(input);
372 }
373 wdata->tool = tool;
374 if (tool)
375 input_report_key(input, tool, 1);
376 }
377
378 if (tool) {
379 input_report_abs(input, ABS_X, x);
380 input_report_abs(input, ABS_Y, y);
381
382 switch ((data[1] >> 5) & 3) {
383 case 2: /* Mouse with wheel */
384 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
385 rw = (data[6] & 0x01) ? -1 :
386 (data[6] & 0x02) ? 1 : 0;
387 input_report_rel(input, REL_WHEEL, rw);
388 /* fall through */
389
390 case 3: /* Mouse without wheel */
391 input_report_key(input, BTN_LEFT, data[1] & 0x01);
392 input_report_key(input, BTN_RIGHT, data[1] & 0x02);
393 /* Compute distance between mouse and tablet */
394 rw = 44 - (data[6] >> 2);
395 if (rw < 0)
396 rw = 0;
397 else if (rw > 31)
398 rw = 31;
399 input_report_abs(input, ABS_DISTANCE, rw);
400 break;
401
402 default:
403 input_report_abs(input, ABS_PRESSURE,
404 data[6] | (((__u16) (data[1] & 0x08)) << 5));
405 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
406 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
407 input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
408 break;
409 }
410
411 input_sync(input);
412 }
413
414 /* Report the state of the two buttons at the top of the tablet
415 * as two extra fingerpad keys (buttons 4 & 5). */
416 rw = data[7] & 0x03;
417 if (rw != wdata->butstate) {
418 wdata->butstate = rw;
419 input_report_key(input, BTN_0, rw & 0x02);
420 input_report_key(input, BTN_1, rw & 0x01);
Przemo Firszt0e253fd2010-01-09 15:20:03 +0100421 input_report_key(input, BTN_TOOL_FINGER, 0xf0);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200422 input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
423 input_sync(input);
424 }
425
Przemo Firszte31e3832012-03-22 18:54:07 +0000426 /* Store current battery capacity and power supply state*/
Przemo Firszt59d23342010-03-15 19:16:23 +0000427 rw = (data[7] >> 2 & 0x07);
Przemo Firszte31e3832012-03-22 18:54:07 +0000428 if (rw != wdata->power_raw) {
429 wdata->power_raw = rw;
430 wdata->battery_capacity = batcap_gr[rw];
431 if (rw == 7)
432 wdata->ps_connected = 1;
433 else
434 wdata->ps_connected = 0;
435 }
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200436 return 1;
437}
Przemo Firszt78761ff2011-11-05 11:28:22 +0000438
Przemo Firszt6245bde2012-02-28 17:19:05 +0000439static void wacom_i4_parse_button_report(struct wacom_data *wdata,
440 struct input_dev *input, unsigned char *data)
441{
442 __u16 new_butstate;
Przemo Firszt7e503a32012-03-14 19:55:04 +0000443 __u8 new_whlstate;
444 __u8 sync = 0;
445
446 new_whlstate = data[1];
447 if (new_whlstate != wdata->whlstate) {
448 wdata->whlstate = new_whlstate;
449 if (new_whlstate & 0x80) {
450 input_report_key(input, BTN_TOUCH, 1);
451 input_report_abs(input, ABS_WHEEL, (new_whlstate & 0x7f));
452 input_report_key(input, BTN_TOOL_FINGER, 1);
453 } else {
454 input_report_key(input, BTN_TOUCH, 0);
455 input_report_abs(input, ABS_WHEEL, 0);
456 input_report_key(input, BTN_TOOL_FINGER, 0);
457 }
458 sync = 1;
459 }
Przemo Firszt6245bde2012-02-28 17:19:05 +0000460
461 new_butstate = (data[3] << 1) | (data[2] & 0x01);
462 if (new_butstate != wdata->butstate) {
463 wdata->butstate = new_butstate;
464 input_report_key(input, BTN_0, new_butstate & 0x001);
465 input_report_key(input, BTN_1, new_butstate & 0x002);
466 input_report_key(input, BTN_2, new_butstate & 0x004);
467 input_report_key(input, BTN_3, new_butstate & 0x008);
468 input_report_key(input, BTN_4, new_butstate & 0x010);
469 input_report_key(input, BTN_5, new_butstate & 0x020);
470 input_report_key(input, BTN_6, new_butstate & 0x040);
471 input_report_key(input, BTN_7, new_butstate & 0x080);
472 input_report_key(input, BTN_8, new_butstate & 0x100);
473 input_report_key(input, BTN_TOOL_FINGER, 1);
Przemo Firszt7e503a32012-03-14 19:55:04 +0000474 sync = 1;
475 }
476
477 if (sync) {
Przemo Firszt6245bde2012-02-28 17:19:05 +0000478 input_report_abs(input, ABS_MISC, PAD_DEVICE_ID);
479 input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff);
480 input_sync(input);
481 }
482}
483
Przemo Firszt78761ff2011-11-05 11:28:22 +0000484static void wacom_i4_parse_pen_report(struct wacom_data *wdata,
485 struct input_dev *input, unsigned char *data)
486{
487 __u16 x, y, pressure;
Przemo Firszte0829e92012-02-28 17:19:04 +0000488 __u8 distance;
Przemo Firsztc653dab2012-05-04 18:00:10 +0100489 __u8 tilt_x, tilt_y;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000490
491 switch (data[1]) {
492 case 0x80: /* Out of proximity report */
Przemo Firszt78761ff2011-11-05 11:28:22 +0000493 input_report_key(input, BTN_TOUCH, 0);
494 input_report_abs(input, ABS_PRESSURE, 0);
Przemo Firszt693f45b2012-03-09 13:20:51 +0000495 input_report_key(input, BTN_STYLUS, 0);
496 input_report_key(input, BTN_STYLUS2, 0);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000497 input_report_key(input, wdata->tool, 0);
Przemo Firszt24709002012-02-24 13:52:32 +0000498 input_report_abs(input, ABS_MISC, 0);
499 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
Przemo Firszt32db7372012-02-19 20:20:29 +0000500 wdata->tool = 0;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000501 input_sync(input);
502 break;
503 case 0xC2: /* Tool report */
Przemo Firszt24709002012-02-24 13:52:32 +0000504 wdata->id = ((data[2] << 4) | (data[3] >> 4) |
Przemo Firszt78761ff2011-11-05 11:28:22 +0000505 ((data[7] & 0x0f) << 20) |
Przemo Firszt24709002012-02-24 13:52:32 +0000506 ((data[8] & 0xf0) << 12));
507 wdata->serial = ((data[3] & 0x0f) << 28) +
508 (data[4] << 20) + (data[5] << 12) +
509 (data[6] << 4) + (data[7] >> 4);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000510
Przemo Firszt24709002012-02-24 13:52:32 +0000511 switch (wdata->id) {
512 case 0x100802:
Przemo Firszt78761ff2011-11-05 11:28:22 +0000513 wdata->tool = BTN_TOOL_PEN;
514 break;
Przemo Firszt24709002012-02-24 13:52:32 +0000515 case 0x10080A:
Przemo Firszt78761ff2011-11-05 11:28:22 +0000516 wdata->tool = BTN_TOOL_RUBBER;
517 break;
518 }
519 break;
520 default: /* Position/pressure report */
521 x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1);
522 y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01);
523 pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5)
524 | (data[1] & 0x01);
Przemo Firszte0829e92012-02-28 17:19:04 +0000525 distance = (data[9] >> 2) & 0x3f;
Przemo Firsztc653dab2012-05-04 18:00:10 +0100526 tilt_x = ((data[7] << 1) & 0x7e) | (data[8] >> 7);
527 tilt_y = data[8] & 0x7f;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000528
529 input_report_key(input, BTN_TOUCH, pressure > 1);
530
531 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
532 input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
533 input_report_key(input, wdata->tool, 1);
534 input_report_abs(input, ABS_X, x);
535 input_report_abs(input, ABS_Y, y);
536 input_report_abs(input, ABS_PRESSURE, pressure);
Przemo Firszte0829e92012-02-28 17:19:04 +0000537 input_report_abs(input, ABS_DISTANCE, distance);
Przemo Firsztc653dab2012-05-04 18:00:10 +0100538 input_report_abs(input, ABS_TILT_X, tilt_x);
539 input_report_abs(input, ABS_TILT_Y, tilt_y);
Przemo Firszt24709002012-02-24 13:52:32 +0000540 input_report_abs(input, ABS_MISC, wdata->id);
541 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
542 input_report_key(input, wdata->tool, 1);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000543 input_sync(input);
544 break;
545 }
546
547 return;
548}
549
550static void wacom_i4_parse_report(struct hid_device *hdev,
551 struct wacom_data *wdata,
552 struct input_dev *input, unsigned char *data)
553{
554 switch (data[0]) {
555 case 0x00: /* Empty report */
556 break;
557 case 0x02: /* Pen report */
558 wacom_i4_parse_pen_report(wdata, input, data);
559 break;
560 case 0x03: /* Features Report */
561 wdata->features = data[2];
562 break;
563 case 0x0C: /* Button report */
Przemo Firszt6245bde2012-02-28 17:19:05 +0000564 wacom_i4_parse_button_report(wdata, input, data);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000565 break;
566 default:
567 hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]);
568 break;
569 }
570}
571
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000572static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
573 u8 *raw_data, int size)
574{
575 struct wacom_data *wdata = hid_get_drvdata(hdev);
576 struct hid_input *hidinput;
577 struct input_dev *input;
578 unsigned char *data = (unsigned char *) raw_data;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000579 int i;
Przemo Firszt4202f1d2012-03-22 18:54:08 +0000580 __u8 power_raw;
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000581
582 if (!(hdev->claimed & HID_CLAIMED_INPUT))
583 return 0;
584
585 hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
586 input = hidinput->input;
587
Przemo Firszt78761ff2011-11-05 11:28:22 +0000588 switch (hdev->product) {
589 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
Przemo Firszt163a6ae2012-05-15 19:32:25 +0100590 if (data[0] == 0x03) {
591 return wacom_gr_parse_report(hdev, wdata, input, data);
592 } else {
593 hid_err(hdev, "Unknown report: %d,%d size:%d\n",
594 data[0], data[1], size);
595 return 0;
596 }
Przemo Firszt78761ff2011-11-05 11:28:22 +0000597 break;
598 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
599 i = 1;
600
601 switch (data[0]) {
602 case 0x04:
603 wacom_i4_parse_report(hdev, wdata, input, data + i);
604 i += 10;
605 /* fall through */
606 case 0x03:
607 wacom_i4_parse_report(hdev, wdata, input, data + i);
608 i += 10;
609 wacom_i4_parse_report(hdev, wdata, input, data + i);
Przemo Firszt4202f1d2012-03-22 18:54:08 +0000610 power_raw = data[i+10];
611 if (power_raw != wdata->power_raw) {
612 wdata->power_raw = power_raw;
613 wdata->battery_capacity = batcap_i4[power_raw & 0x07];
614 wdata->ps_connected = power_raw & 0x08;
615 }
616
Przemo Firszt78761ff2011-11-05 11:28:22 +0000617 break;
618 default:
619 hid_err(hdev, "Unknown report: %d,%d size:%d\n",
620 data[0], data[1], size);
621 return 0;
622 }
623 }
624 return 1;
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000625}
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200626
David Herrmann3797ef62011-10-08 23:20:17 +0200627static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
628 struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
629 int *max)
630{
631 struct input_dev *input = hi->input;
632
Jiri Kosinaf6f12422011-10-25 09:58:12 +0200633 __set_bit(INPUT_PROP_POINTER, input->propbit);
634
David Herrmann3797ef62011-10-08 23:20:17 +0200635 /* Basics */
636 input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
637
638 __set_bit(REL_WHEEL, input->relbit);
639
640 __set_bit(BTN_TOOL_PEN, input->keybit);
641 __set_bit(BTN_TOUCH, input->keybit);
642 __set_bit(BTN_STYLUS, input->keybit);
643 __set_bit(BTN_STYLUS2, input->keybit);
644 __set_bit(BTN_LEFT, input->keybit);
645 __set_bit(BTN_RIGHT, input->keybit);
646 __set_bit(BTN_MIDDLE, input->keybit);
647
648 /* Pad */
Przemo Firszt9a911da2012-03-14 19:55:03 +0000649 input_set_capability(input, EV_MSC, MSC_SERIAL);
David Herrmann3797ef62011-10-08 23:20:17 +0200650
651 __set_bit(BTN_0, input->keybit);
652 __set_bit(BTN_1, input->keybit);
653 __set_bit(BTN_TOOL_FINGER, input->keybit);
654
655 /* Distance, rubber and mouse */
656 __set_bit(BTN_TOOL_RUBBER, input->keybit);
657 __set_bit(BTN_TOOL_MOUSE, input->keybit);
658
Przemo Firszt78761ff2011-11-05 11:28:22 +0000659 switch (hdev->product) {
660 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
661 input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
662 input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
663 input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
664 input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
665 break;
666 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
Przemo Firszt7e503a32012-03-14 19:55:04 +0000667 __set_bit(ABS_WHEEL, input->absbit);
Przemo Firszt2c653e62012-02-24 13:47:48 +0000668 __set_bit(ABS_MISC, input->absbit);
Przemo Firszt6245bde2012-02-28 17:19:05 +0000669 __set_bit(BTN_2, input->keybit);
670 __set_bit(BTN_3, input->keybit);
671 __set_bit(BTN_4, input->keybit);
672 __set_bit(BTN_5, input->keybit);
673 __set_bit(BTN_6, input->keybit);
674 __set_bit(BTN_7, input->keybit);
675 __set_bit(BTN_8, input->keybit);
Przemo Firszt7e503a32012-03-14 19:55:04 +0000676 input_set_abs_params(input, ABS_WHEEL, 0, 71, 0, 0);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000677 input_set_abs_params(input, ABS_X, 0, 40640, 4, 0);
678 input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0);
679 input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0);
Przemo Firszte0829e92012-02-28 17:19:04 +0000680 input_set_abs_params(input, ABS_DISTANCE, 0, 63, 0, 0);
Przemo Firsztc653dab2012-05-04 18:00:10 +0100681 input_set_abs_params(input, ABS_TILT_X, 0, 127, 0, 0);
682 input_set_abs_params(input, ABS_TILT_Y, 0, 127, 0, 0);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000683 break;
684 }
David Herrmann3797ef62011-10-08 23:20:17 +0200685
686 return 0;
687}
688
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200689static int wacom_probe(struct hid_device *hdev,
690 const struct hid_device_id *id)
691{
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200692 struct wacom_data *wdata;
693 int ret;
694
695 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
696 if (wdata == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -0800697 hid_err(hdev, "can't alloc wacom descriptor\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200698 return -ENOMEM;
699 }
700
701 hid_set_drvdata(hdev, wdata);
702
Bastien Nocera46a709b2010-01-20 12:00:53 +0000703 /* Parse the HID report now */
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200704 ret = hid_parse(hdev);
705 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800706 hid_err(hdev, "parse failed\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200707 goto err_free;
708 }
709
710 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
711 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800712 hid_err(hdev, "hw start failed\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200713 goto err_free;
714 }
715
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000716 ret = device_create_file(&hdev->dev, &dev_attr_speed);
717 if (ret)
Joe Perches4291ee32010-12-09 19:29:03 -0800718 hid_warn(hdev,
719 "can't create sysfs speed attribute err: %d\n", ret);
Jiri Kosina342f31e2010-02-03 15:52:31 +0100720
Przemo Firsztd70a2ff2012-05-15 19:32:28 +0100721 wdata->features = 0;
722 wacom_set_features(hdev, 1);
723
724 if (hdev->product == USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) {
Przemo Firszta72c5dd2012-02-17 17:47:10 +0000725 sprintf(hdev->name, "%s", "Wacom Intuos4 WL");
Przemo Firsztd13f5452012-05-10 19:23:52 +0100726 ret = wacom_initialize_leds(hdev);
727 if (ret) {
728 hid_warn(hdev,
729 "can't create led attribute, err: %d\n", ret);
730 goto destroy_leds;
731 }
Przemo Firszt78761ff2011-11-05 11:28:22 +0000732 }
Bastien Nocera46a709b2010-01-20 12:00:53 +0000733
Przemo Firszt59d23342010-03-15 19:16:23 +0000734 wdata->battery.properties = wacom_battery_props;
735 wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
736 wdata->battery.get_property = wacom_battery_get_property;
737 wdata->battery.name = "wacom_battery";
738 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
739 wdata->battery.use_for_apm = 0;
Bastien Nocera46a709b2010-01-20 12:00:53 +0000740
Jeremy Fitzhardinge35b4c012011-12-09 09:35:00 -0800741
Przemo Firszt59d23342010-03-15 19:16:23 +0000742 ret = power_supply_register(&hdev->dev, &wdata->battery);
743 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800744 hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n",
745 ret);
David Herrmanndde58cf2011-09-05 18:45:28 +0200746 goto err_battery;
Przemo Firszt59d23342010-03-15 19:16:23 +0000747 }
748
Przemo Firsztd7cb3db2012-02-05 22:35:24 +0000749 power_supply_powers(&wdata->battery, &hdev->dev);
750
Przemo Firszt59d23342010-03-15 19:16:23 +0000751 wdata->ac.properties = wacom_ac_props;
752 wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
753 wdata->ac.get_property = wacom_ac_get_property;
754 wdata->ac.name = "wacom_ac";
755 wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
756 wdata->ac.use_for_apm = 0;
757
758 ret = power_supply_register(&hdev->dev, &wdata->ac);
759 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800760 hid_warn(hdev,
761 "can't create ac battery attribute, err: %d\n", ret);
David Herrmanndde58cf2011-09-05 18:45:28 +0200762 goto err_ac;
Przemo Firszt59d23342010-03-15 19:16:23 +0000763 }
Przemo Firsztd7cb3db2012-02-05 22:35:24 +0000764
765 power_supply_powers(&wdata->ac, &hdev->dev);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200766 return 0;
Daniel Mack987a6c02010-08-02 20:15:17 -0700767
David Herrmanndde58cf2011-09-05 18:45:28 +0200768err_ac:
769 power_supply_unregister(&wdata->battery);
770err_battery:
771 device_remove_file(&hdev->dev, &dev_attr_speed);
772 hid_hw_stop(hdev);
Przemo Firsztd13f5452012-05-10 19:23:52 +0100773destroy_leds:
774 wacom_destroy_leds(hdev);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200775err_free:
776 kfree(wdata);
777 return ret;
778}
779
780static void wacom_remove(struct hid_device *hdev)
781{
Przemo Firszt59d23342010-03-15 19:16:23 +0000782 struct wacom_data *wdata = hid_get_drvdata(hdev);
Przemo Firsztd13f5452012-05-10 19:23:52 +0100783
784 wacom_destroy_leds(hdev);
David Herrmann90866172011-09-05 18:45:29 +0200785 device_remove_file(&hdev->dev, &dev_attr_speed);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200786 hid_hw_stop(hdev);
Przemo Firszt59d23342010-03-15 19:16:23 +0000787
Przemo Firszt59d23342010-03-15 19:16:23 +0000788 power_supply_unregister(&wdata->battery);
789 power_supply_unregister(&wdata->ac);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200790 kfree(hid_get_drvdata(hdev));
791}
792
793static const struct hid_device_id wacom_devices[] = {
794 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
Przemo Firszt78761ff2011-11-05 11:28:22 +0000795 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
Jiri Kosinad5e0a062010-07-20 17:48:48 +0200796
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200797 { }
798};
799MODULE_DEVICE_TABLE(hid, wacom_devices);
800
801static struct hid_driver wacom_driver = {
802 .name = "wacom",
803 .id_table = wacom_devices,
804 .probe = wacom_probe,
805 .remove = wacom_remove,
806 .raw_event = wacom_raw_event,
David Herrmann3797ef62011-10-08 23:20:17 +0200807 .input_mapped = wacom_input_mapped,
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200808};
809
Peter Huewea24f423b2009-07-02 19:08:38 +0200810static int __init wacom_init(void)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200811{
812 int ret;
813
814 ret = hid_register_driver(&wacom_driver);
815 if (ret)
Joe Perches4291ee32010-12-09 19:29:03 -0800816 pr_err("can't register wacom driver\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200817 return ret;
818}
819
Peter Huewea24f423b2009-07-02 19:08:38 +0200820static void __exit wacom_exit(void)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200821{
822 hid_unregister_driver(&wacom_driver);
823}
824
825module_init(wacom_init);
826module_exit(wacom_exit);
Przemo Firszt1fa2f722012-03-28 22:13:37 +0100827MODULE_DESCRIPTION("Driver for Wacom Graphire Bluetooth and Wacom Intuos4 WL");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200828MODULE_LICENSE("GPL");