blob: 5f6ce70c23fb5903a9fa6d6116d5470823e01a0b [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;
94 buf[2] = value;
95 hdev->hid_output_raw_report(hdev, buf, 9, HID_FEATURE_REPORT);
96 kfree(buf);
97 }
98
99 return;
100}
101
102static enum led_brightness wacom_leds_get_brightness(struct led_classdev *led_dev)
103{
104 struct wacom_data *wdata;
105 struct device *dev = led_dev->dev->parent;
106 int value = 0;
107 int i;
108
109 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
110
111 for (i = 0; i < 4; ++i) {
112 if (wdata->leds[i] == led_dev) {
113 value = wdata->leds[i]->brightness;
114 break;
115 }
116 }
117
118 return value;
119}
120
121
122static int wacom_initialize_leds(struct hid_device *hdev)
123{
124 struct wacom_data *wdata = hid_get_drvdata(hdev);
125 struct led_classdev *led;
126 struct device *dev = &hdev->dev;
127 size_t namesz = strlen(dev_name(dev)) + 12;
128 char *name;
129 int i, ret;
130
131 wdata->led_selector = 0;
132
133 for (i = 0; i < 4; i++) {
134 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
135 if (!led) {
136 hid_warn(hdev,
137 "can't allocate memory for LED selector\n");
138 ret = -ENOMEM;
139 goto err;
140 }
141
142 name = (void *)&led[1];
143 snprintf(name, namesz, "%s:selector:%d", dev_name(dev), i);
144 led->name = name;
145 led->brightness = 0;
146 led->max_brightness = 127;
147 led->brightness_get = wacom_leds_get_brightness;
148 led->brightness_set = wacom_leds_set_brightness;
149
150 wdata->leds[i] = led;
151
152 ret = led_classdev_register(dev, wdata->leds[i]);
153
154 if (ret) {
155 wdata->leds[i] = NULL;
156 kfree(led);
157 hid_warn(hdev, "can't register LED\n");
158 goto err;
159 }
160 }
161
162err:
163 return ret;
164}
165
166static void wacom_destroy_leds(struct hid_device *hdev)
167{
168 struct wacom_data *wdata = hid_get_drvdata(hdev);
169 struct led_classdev *led;
170 int i;
171
172 for (i = 0; i < 4; ++i) {
173 if (wdata->leds[i]) {
174 led = wdata->leds[i];
175 wdata->leds[i] = NULL;
176 led_classdev_unregister(led);
177 kfree(led);
178 }
179 }
180
181}
182
Przemo Firszt59d23342010-03-15 19:16:23 +0000183static int wacom_battery_get_property(struct power_supply *psy,
184 enum power_supply_property psp,
185 union power_supply_propval *val)
186{
187 struct wacom_data *wdata = container_of(psy,
188 struct wacom_data, battery);
Przemo Firszt59d23342010-03-15 19:16:23 +0000189 int ret = 0;
190
191 switch (psp) {
192 case POWER_SUPPLY_PROP_PRESENT:
193 val->intval = 1;
194 break;
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -0800195 case POWER_SUPPLY_PROP_SCOPE:
196 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
197 break;
Przemo Firszt59d23342010-03-15 19:16:23 +0000198 case POWER_SUPPLY_PROP_CAPACITY:
Przemo Firszte31e3832012-03-22 18:54:07 +0000199 val->intval = wdata->battery_capacity;
Przemo Firszt59d23342010-03-15 19:16:23 +0000200 break;
201 default:
202 ret = -EINVAL;
203 break;
204 }
205 return ret;
206}
207
208static int wacom_ac_get_property(struct power_supply *psy,
209 enum power_supply_property psp,
210 union power_supply_propval *val)
211{
212 struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
Przemo Firszt59d23342010-03-15 19:16:23 +0000213 int ret = 0;
214
215 switch (psp) {
216 case POWER_SUPPLY_PROP_PRESENT:
217 /* fall through */
218 case POWER_SUPPLY_PROP_ONLINE:
Przemo Firszte31e3832012-03-22 18:54:07 +0000219 val->intval = wdata->ps_connected;
Przemo Firszt59d23342010-03-15 19:16:23 +0000220 break;
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -0800221 case POWER_SUPPLY_PROP_SCOPE:
222 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
223 break;
Przemo Firszt59d23342010-03-15 19:16:23 +0000224 default:
225 ret = -EINVAL;
226 break;
227 }
228 return ret;
229}
Przemo Firszt59d23342010-03-15 19:16:23 +0000230
Przemo Firsztbe4925b2012-05-15 19:32:26 +0100231static void wacom_set_features(struct hid_device *hdev, u8 speed)
Przemo Firszt78761ff2011-11-05 11:28:22 +0000232{
Przemo Firsztbe4925b2012-05-15 19:32:26 +0100233 struct wacom_data *wdata = hid_get_drvdata(hdev);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000234 int ret;
235 __u8 rep_data[2];
236
Przemo Firsztbe4925b2012-05-15 19:32:26 +0100237 if (speed == 1)
238 wdata->features &= ~0x20;
239 else
240 wdata->features |= 0x20;
241
Przemo Firszt78761ff2011-11-05 11:28:22 +0000242 rep_data[0] = 0x03;
Przemo Firsztbe4925b2012-05-15 19:32:26 +0100243 rep_data[1] = wdata->features;
244
Przemo Firszt78761ff2011-11-05 11:28:22 +0000245 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
246 HID_FEATURE_REPORT);
Przemo Firsztbe4925b2012-05-15 19:32:26 +0100247 if (ret >= 0)
248 wdata->high_speed = speed;
249
Przemo Firszt78761ff2011-11-05 11:28:22 +0000250 return;
251}
252
Przemo Firszt06c7c312010-03-22 09:55:04 +0100253static void wacom_poke(struct hid_device *hdev, u8 speed)
254{
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000255 struct wacom_data *wdata = hid_get_drvdata(hdev);
Przemo Firszt06c7c312010-03-22 09:55:04 +0100256 int limit, ret;
257 char rep_data[2];
258
259 rep_data[0] = 0x03 ; rep_data[1] = 0x00;
260 limit = 3;
261 do {
262 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
263 HID_FEATURE_REPORT);
264 } while (ret < 0 && limit-- > 0);
265
266 if (ret >= 0) {
267 if (speed == 0)
268 rep_data[0] = 0x05;
269 else
270 rep_data[0] = 0x06;
271
272 rep_data[1] = 0x00;
273 limit = 3;
274 do {
275 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
276 HID_FEATURE_REPORT);
277 } while (ret < 0 && limit-- > 0);
278
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000279 if (ret >= 0) {
280 wdata->high_speed = speed;
Przemo Firszt06c7c312010-03-22 09:55:04 +0100281 return;
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000282 }
Przemo Firszt06c7c312010-03-22 09:55:04 +0100283 }
284
285 /*
286 * Note that if the raw queries fail, it's not a hard failure and it
287 * is safe to continue
288 */
Joe Perches4291ee32010-12-09 19:29:03 -0800289 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
290 rep_data[0], ret);
Przemo Firszt06c7c312010-03-22 09:55:04 +0100291 return;
292}
293
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000294static ssize_t wacom_show_speed(struct device *dev,
295 struct device_attribute
296 *attr, char *buf)
297{
298 struct wacom_data *wdata = dev_get_drvdata(dev);
299
300 return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
301}
302
303static ssize_t wacom_store_speed(struct device *dev,
304 struct device_attribute *attr,
305 const char *buf, size_t count)
306{
307 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
308 int new_speed;
309
310 if (sscanf(buf, "%1d", &new_speed ) != 1)
311 return -EINVAL;
312
313 if (new_speed == 0 || new_speed == 1) {
314 wacom_poke(hdev, new_speed);
315 return strnlen(buf, PAGE_SIZE);
316 } else
317 return -EINVAL;
318}
319
Jiri Kosinaedd21262010-11-18 16:28:43 +0100320static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000321 wacom_show_speed, wacom_store_speed);
322
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000323static int wacom_gr_parse_report(struct hid_device *hdev,
324 struct wacom_data *wdata,
325 struct input_dev *input, unsigned char *data)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200326{
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200327 int tool, x, y, rw;
328
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200329 tool = 0;
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200330 /* Get X & Y positions */
331 x = le16_to_cpu(*(__le16 *) &data[2]);
332 y = le16_to_cpu(*(__le16 *) &data[4]);
333
334 /* Get current tool identifier */
335 if (data[1] & 0x90) { /* If pen is in the in/active area */
336 switch ((data[1] >> 5) & 3) {
337 case 0: /* Pen */
338 tool = BTN_TOOL_PEN;
339 break;
340
341 case 1: /* Rubber */
342 tool = BTN_TOOL_RUBBER;
343 break;
344
345 case 2: /* Mouse with wheel */
346 case 3: /* Mouse without wheel */
347 tool = BTN_TOOL_MOUSE;
348 break;
349 }
350
351 /* Reset tool if out of active tablet area */
352 if (!(data[1] & 0x10))
353 tool = 0;
354 }
355
356 /* If tool changed, notify input subsystem */
357 if (wdata->tool != tool) {
358 if (wdata->tool) {
359 /* Completely reset old tool state */
360 if (wdata->tool == BTN_TOOL_MOUSE) {
361 input_report_key(input, BTN_LEFT, 0);
362 input_report_key(input, BTN_RIGHT, 0);
363 input_report_key(input, BTN_MIDDLE, 0);
364 input_report_abs(input, ABS_DISTANCE,
Daniel Mack987a6c02010-08-02 20:15:17 -0700365 input_abs_get_max(input, ABS_DISTANCE));
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200366 } else {
367 input_report_key(input, BTN_TOUCH, 0);
368 input_report_key(input, BTN_STYLUS, 0);
369 input_report_key(input, BTN_STYLUS2, 0);
370 input_report_abs(input, ABS_PRESSURE, 0);
371 }
372 input_report_key(input, wdata->tool, 0);
373 input_sync(input);
374 }
375 wdata->tool = tool;
376 if (tool)
377 input_report_key(input, tool, 1);
378 }
379
380 if (tool) {
381 input_report_abs(input, ABS_X, x);
382 input_report_abs(input, ABS_Y, y);
383
384 switch ((data[1] >> 5) & 3) {
385 case 2: /* Mouse with wheel */
386 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
387 rw = (data[6] & 0x01) ? -1 :
388 (data[6] & 0x02) ? 1 : 0;
389 input_report_rel(input, REL_WHEEL, rw);
390 /* fall through */
391
392 case 3: /* Mouse without wheel */
393 input_report_key(input, BTN_LEFT, data[1] & 0x01);
394 input_report_key(input, BTN_RIGHT, data[1] & 0x02);
395 /* Compute distance between mouse and tablet */
396 rw = 44 - (data[6] >> 2);
397 if (rw < 0)
398 rw = 0;
399 else if (rw > 31)
400 rw = 31;
401 input_report_abs(input, ABS_DISTANCE, rw);
402 break;
403
404 default:
405 input_report_abs(input, ABS_PRESSURE,
406 data[6] | (((__u16) (data[1] & 0x08)) << 5));
407 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
408 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
409 input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
410 break;
411 }
412
413 input_sync(input);
414 }
415
416 /* Report the state of the two buttons at the top of the tablet
417 * as two extra fingerpad keys (buttons 4 & 5). */
418 rw = data[7] & 0x03;
419 if (rw != wdata->butstate) {
420 wdata->butstate = rw;
421 input_report_key(input, BTN_0, rw & 0x02);
422 input_report_key(input, BTN_1, rw & 0x01);
Przemo Firszt0e253fd2010-01-09 15:20:03 +0100423 input_report_key(input, BTN_TOOL_FINGER, 0xf0);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200424 input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
425 input_sync(input);
426 }
427
Przemo Firszte31e3832012-03-22 18:54:07 +0000428 /* Store current battery capacity and power supply state*/
Przemo Firszt59d23342010-03-15 19:16:23 +0000429 rw = (data[7] >> 2 & 0x07);
Przemo Firszte31e3832012-03-22 18:54:07 +0000430 if (rw != wdata->power_raw) {
431 wdata->power_raw = rw;
432 wdata->battery_capacity = batcap_gr[rw];
433 if (rw == 7)
434 wdata->ps_connected = 1;
435 else
436 wdata->ps_connected = 0;
437 }
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200438 return 1;
439}
Przemo Firszt78761ff2011-11-05 11:28:22 +0000440
Przemo Firszt6245bde2012-02-28 17:19:05 +0000441static void wacom_i4_parse_button_report(struct wacom_data *wdata,
442 struct input_dev *input, unsigned char *data)
443{
444 __u16 new_butstate;
Przemo Firszt7e503a32012-03-14 19:55:04 +0000445 __u8 new_whlstate;
446 __u8 sync = 0;
447
448 new_whlstate = data[1];
449 if (new_whlstate != wdata->whlstate) {
450 wdata->whlstate = new_whlstate;
451 if (new_whlstate & 0x80) {
452 input_report_key(input, BTN_TOUCH, 1);
453 input_report_abs(input, ABS_WHEEL, (new_whlstate & 0x7f));
454 input_report_key(input, BTN_TOOL_FINGER, 1);
455 } else {
456 input_report_key(input, BTN_TOUCH, 0);
457 input_report_abs(input, ABS_WHEEL, 0);
458 input_report_key(input, BTN_TOOL_FINGER, 0);
459 }
460 sync = 1;
461 }
Przemo Firszt6245bde2012-02-28 17:19:05 +0000462
463 new_butstate = (data[3] << 1) | (data[2] & 0x01);
464 if (new_butstate != wdata->butstate) {
465 wdata->butstate = new_butstate;
466 input_report_key(input, BTN_0, new_butstate & 0x001);
467 input_report_key(input, BTN_1, new_butstate & 0x002);
468 input_report_key(input, BTN_2, new_butstate & 0x004);
469 input_report_key(input, BTN_3, new_butstate & 0x008);
470 input_report_key(input, BTN_4, new_butstate & 0x010);
471 input_report_key(input, BTN_5, new_butstate & 0x020);
472 input_report_key(input, BTN_6, new_butstate & 0x040);
473 input_report_key(input, BTN_7, new_butstate & 0x080);
474 input_report_key(input, BTN_8, new_butstate & 0x100);
475 input_report_key(input, BTN_TOOL_FINGER, 1);
Przemo Firszt7e503a32012-03-14 19:55:04 +0000476 sync = 1;
477 }
478
479 if (sync) {
Przemo Firszt6245bde2012-02-28 17:19:05 +0000480 input_report_abs(input, ABS_MISC, PAD_DEVICE_ID);
481 input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff);
482 input_sync(input);
483 }
484}
485
Przemo Firszt78761ff2011-11-05 11:28:22 +0000486static void wacom_i4_parse_pen_report(struct wacom_data *wdata,
487 struct input_dev *input, unsigned char *data)
488{
489 __u16 x, y, pressure;
Przemo Firszte0829e92012-02-28 17:19:04 +0000490 __u8 distance;
Przemo Firsztc653dab2012-05-04 18:00:10 +0100491 __u8 tilt_x, tilt_y;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000492
493 switch (data[1]) {
494 case 0x80: /* Out of proximity report */
Przemo Firszt78761ff2011-11-05 11:28:22 +0000495 input_report_key(input, BTN_TOUCH, 0);
496 input_report_abs(input, ABS_PRESSURE, 0);
Przemo Firszt693f45b2012-03-09 13:20:51 +0000497 input_report_key(input, BTN_STYLUS, 0);
498 input_report_key(input, BTN_STYLUS2, 0);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000499 input_report_key(input, wdata->tool, 0);
Przemo Firszt24709002012-02-24 13:52:32 +0000500 input_report_abs(input, ABS_MISC, 0);
501 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
Przemo Firszt32db7372012-02-19 20:20:29 +0000502 wdata->tool = 0;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000503 input_sync(input);
504 break;
505 case 0xC2: /* Tool report */
Przemo Firszt24709002012-02-24 13:52:32 +0000506 wdata->id = ((data[2] << 4) | (data[3] >> 4) |
Przemo Firszt78761ff2011-11-05 11:28:22 +0000507 ((data[7] & 0x0f) << 20) |
Przemo Firszt24709002012-02-24 13:52:32 +0000508 ((data[8] & 0xf0) << 12));
509 wdata->serial = ((data[3] & 0x0f) << 28) +
510 (data[4] << 20) + (data[5] << 12) +
511 (data[6] << 4) + (data[7] >> 4);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000512
Przemo Firszt24709002012-02-24 13:52:32 +0000513 switch (wdata->id) {
514 case 0x100802:
Przemo Firszt78761ff2011-11-05 11:28:22 +0000515 wdata->tool = BTN_TOOL_PEN;
516 break;
Przemo Firszt24709002012-02-24 13:52:32 +0000517 case 0x10080A:
Przemo Firszt78761ff2011-11-05 11:28:22 +0000518 wdata->tool = BTN_TOOL_RUBBER;
519 break;
520 }
521 break;
522 default: /* Position/pressure report */
523 x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1);
524 y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01);
525 pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5)
526 | (data[1] & 0x01);
Przemo Firszte0829e92012-02-28 17:19:04 +0000527 distance = (data[9] >> 2) & 0x3f;
Przemo Firsztc653dab2012-05-04 18:00:10 +0100528 tilt_x = ((data[7] << 1) & 0x7e) | (data[8] >> 7);
529 tilt_y = data[8] & 0x7f;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000530
531 input_report_key(input, BTN_TOUCH, pressure > 1);
532
533 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
534 input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
535 input_report_key(input, wdata->tool, 1);
536 input_report_abs(input, ABS_X, x);
537 input_report_abs(input, ABS_Y, y);
538 input_report_abs(input, ABS_PRESSURE, pressure);
Przemo Firszte0829e92012-02-28 17:19:04 +0000539 input_report_abs(input, ABS_DISTANCE, distance);
Przemo Firsztc653dab2012-05-04 18:00:10 +0100540 input_report_abs(input, ABS_TILT_X, tilt_x);
541 input_report_abs(input, ABS_TILT_Y, tilt_y);
Przemo Firszt24709002012-02-24 13:52:32 +0000542 input_report_abs(input, ABS_MISC, wdata->id);
543 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
544 input_report_key(input, wdata->tool, 1);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000545 input_sync(input);
546 break;
547 }
548
549 return;
550}
551
552static void wacom_i4_parse_report(struct hid_device *hdev,
553 struct wacom_data *wdata,
554 struct input_dev *input, unsigned char *data)
555{
556 switch (data[0]) {
557 case 0x00: /* Empty report */
558 break;
559 case 0x02: /* Pen report */
560 wacom_i4_parse_pen_report(wdata, input, data);
561 break;
562 case 0x03: /* Features Report */
563 wdata->features = data[2];
564 break;
565 case 0x0C: /* Button report */
Przemo Firszt6245bde2012-02-28 17:19:05 +0000566 wacom_i4_parse_button_report(wdata, input, data);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000567 break;
568 default:
569 hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]);
570 break;
571 }
572}
573
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000574static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
575 u8 *raw_data, int size)
576{
577 struct wacom_data *wdata = hid_get_drvdata(hdev);
578 struct hid_input *hidinput;
579 struct input_dev *input;
580 unsigned char *data = (unsigned char *) raw_data;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000581 int i;
Przemo Firszt4202f1d2012-03-22 18:54:08 +0000582 __u8 power_raw;
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000583
584 if (!(hdev->claimed & HID_CLAIMED_INPUT))
585 return 0;
586
587 hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
588 input = hidinput->input;
589
Przemo Firszt78761ff2011-11-05 11:28:22 +0000590 switch (hdev->product) {
591 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
Przemo Firszt163a6ae2012-05-15 19:32:25 +0100592 if (data[0] == 0x03) {
593 return wacom_gr_parse_report(hdev, wdata, input, data);
594 } else {
595 hid_err(hdev, "Unknown report: %d,%d size:%d\n",
596 data[0], data[1], size);
597 return 0;
598 }
Przemo Firszt78761ff2011-11-05 11:28:22 +0000599 break;
600 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
601 i = 1;
602
603 switch (data[0]) {
604 case 0x04:
605 wacom_i4_parse_report(hdev, wdata, input, data + i);
606 i += 10;
607 /* fall through */
608 case 0x03:
609 wacom_i4_parse_report(hdev, wdata, input, data + i);
610 i += 10;
611 wacom_i4_parse_report(hdev, wdata, input, data + i);
Przemo Firszt4202f1d2012-03-22 18:54:08 +0000612 power_raw = data[i+10];
613 if (power_raw != wdata->power_raw) {
614 wdata->power_raw = power_raw;
615 wdata->battery_capacity = batcap_i4[power_raw & 0x07];
616 wdata->ps_connected = power_raw & 0x08;
617 }
618
Przemo Firszt78761ff2011-11-05 11:28:22 +0000619 break;
620 default:
621 hid_err(hdev, "Unknown report: %d,%d size:%d\n",
622 data[0], data[1], size);
623 return 0;
624 }
625 }
626 return 1;
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000627}
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200628
David Herrmann3797ef62011-10-08 23:20:17 +0200629static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
630 struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
631 int *max)
632{
633 struct input_dev *input = hi->input;
634
Jiri Kosinaf6f12422011-10-25 09:58:12 +0200635 __set_bit(INPUT_PROP_POINTER, input->propbit);
636
David Herrmann3797ef62011-10-08 23:20:17 +0200637 /* Basics */
638 input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
639
640 __set_bit(REL_WHEEL, input->relbit);
641
642 __set_bit(BTN_TOOL_PEN, input->keybit);
643 __set_bit(BTN_TOUCH, input->keybit);
644 __set_bit(BTN_STYLUS, input->keybit);
645 __set_bit(BTN_STYLUS2, input->keybit);
646 __set_bit(BTN_LEFT, input->keybit);
647 __set_bit(BTN_RIGHT, input->keybit);
648 __set_bit(BTN_MIDDLE, input->keybit);
649
650 /* Pad */
Przemo Firszt9a911da2012-03-14 19:55:03 +0000651 input_set_capability(input, EV_MSC, MSC_SERIAL);
David Herrmann3797ef62011-10-08 23:20:17 +0200652
653 __set_bit(BTN_0, input->keybit);
654 __set_bit(BTN_1, input->keybit);
655 __set_bit(BTN_TOOL_FINGER, input->keybit);
656
657 /* Distance, rubber and mouse */
658 __set_bit(BTN_TOOL_RUBBER, input->keybit);
659 __set_bit(BTN_TOOL_MOUSE, input->keybit);
660
Przemo Firszt78761ff2011-11-05 11:28:22 +0000661 switch (hdev->product) {
662 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
663 input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
664 input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
665 input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
666 input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
667 break;
668 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
Przemo Firszt7e503a32012-03-14 19:55:04 +0000669 __set_bit(ABS_WHEEL, input->absbit);
Przemo Firszt2c653e62012-02-24 13:47:48 +0000670 __set_bit(ABS_MISC, input->absbit);
Przemo Firszt6245bde2012-02-28 17:19:05 +0000671 __set_bit(BTN_2, input->keybit);
672 __set_bit(BTN_3, input->keybit);
673 __set_bit(BTN_4, input->keybit);
674 __set_bit(BTN_5, input->keybit);
675 __set_bit(BTN_6, input->keybit);
676 __set_bit(BTN_7, input->keybit);
677 __set_bit(BTN_8, input->keybit);
Przemo Firszt7e503a32012-03-14 19:55:04 +0000678 input_set_abs_params(input, ABS_WHEEL, 0, 71, 0, 0);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000679 input_set_abs_params(input, ABS_X, 0, 40640, 4, 0);
680 input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0);
681 input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0);
Przemo Firszte0829e92012-02-28 17:19:04 +0000682 input_set_abs_params(input, ABS_DISTANCE, 0, 63, 0, 0);
Przemo Firsztc653dab2012-05-04 18:00:10 +0100683 input_set_abs_params(input, ABS_TILT_X, 0, 127, 0, 0);
684 input_set_abs_params(input, ABS_TILT_Y, 0, 127, 0, 0);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000685 break;
686 }
David Herrmann3797ef62011-10-08 23:20:17 +0200687
688 return 0;
689}
690
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200691static int wacom_probe(struct hid_device *hdev,
692 const struct hid_device_id *id)
693{
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200694 struct wacom_data *wdata;
695 int ret;
696
697 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
698 if (wdata == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -0800699 hid_err(hdev, "can't alloc wacom descriptor\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200700 return -ENOMEM;
701 }
702
703 hid_set_drvdata(hdev, wdata);
704
Bastien Nocera46a709b2010-01-20 12:00:53 +0000705 /* Parse the HID report now */
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200706 ret = hid_parse(hdev);
707 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800708 hid_err(hdev, "parse failed\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200709 goto err_free;
710 }
711
712 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
713 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800714 hid_err(hdev, "hw start failed\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200715 goto err_free;
716 }
717
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000718 ret = device_create_file(&hdev->dev, &dev_attr_speed);
719 if (ret)
Joe Perches4291ee32010-12-09 19:29:03 -0800720 hid_warn(hdev,
721 "can't create sysfs speed attribute err: %d\n", ret);
Jiri Kosina342f31e2010-02-03 15:52:31 +0100722
Przemo Firszt78761ff2011-11-05 11:28:22 +0000723 switch (hdev->product) {
724 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
725 /* Set Wacom mode 2 with high reporting speed */
726 wacom_poke(hdev, 1);
727 break;
728 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
Przemo Firszta72c5dd2012-02-17 17:47:10 +0000729 sprintf(hdev->name, "%s", "Wacom Intuos4 WL");
Przemo Firszt78761ff2011-11-05 11:28:22 +0000730 wdata->features = 0;
Przemo Firsztbe4925b2012-05-15 19:32:26 +0100731 wacom_set_features(hdev, 1);
Przemo Firsztd13f5452012-05-10 19:23:52 +0100732 ret = wacom_initialize_leds(hdev);
733 if (ret) {
734 hid_warn(hdev,
735 "can't create led attribute, err: %d\n", ret);
736 goto destroy_leds;
737 }
Przemo Firszt78761ff2011-11-05 11:28:22 +0000738 break;
739 }
Bastien Nocera46a709b2010-01-20 12:00:53 +0000740
Przemo Firszt59d23342010-03-15 19:16:23 +0000741 wdata->battery.properties = wacom_battery_props;
742 wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
743 wdata->battery.get_property = wacom_battery_get_property;
744 wdata->battery.name = "wacom_battery";
745 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
746 wdata->battery.use_for_apm = 0;
Bastien Nocera46a709b2010-01-20 12:00:53 +0000747
Jeremy Fitzhardinge35b4c012011-12-09 09:35:00 -0800748
Przemo Firszt59d23342010-03-15 19:16:23 +0000749 ret = power_supply_register(&hdev->dev, &wdata->battery);
750 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800751 hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n",
752 ret);
David Herrmanndde58cf2011-09-05 18:45:28 +0200753 goto err_battery;
Przemo Firszt59d23342010-03-15 19:16:23 +0000754 }
755
Przemo Firsztd7cb3db2012-02-05 22:35:24 +0000756 power_supply_powers(&wdata->battery, &hdev->dev);
757
Przemo Firszt59d23342010-03-15 19:16:23 +0000758 wdata->ac.properties = wacom_ac_props;
759 wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
760 wdata->ac.get_property = wacom_ac_get_property;
761 wdata->ac.name = "wacom_ac";
762 wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
763 wdata->ac.use_for_apm = 0;
764
765 ret = power_supply_register(&hdev->dev, &wdata->ac);
766 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800767 hid_warn(hdev,
768 "can't create ac battery attribute, err: %d\n", ret);
David Herrmanndde58cf2011-09-05 18:45:28 +0200769 goto err_ac;
Przemo Firszt59d23342010-03-15 19:16:23 +0000770 }
Przemo Firsztd7cb3db2012-02-05 22:35:24 +0000771
772 power_supply_powers(&wdata->ac, &hdev->dev);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200773 return 0;
Daniel Mack987a6c02010-08-02 20:15:17 -0700774
David Herrmanndde58cf2011-09-05 18:45:28 +0200775err_ac:
776 power_supply_unregister(&wdata->battery);
777err_battery:
778 device_remove_file(&hdev->dev, &dev_attr_speed);
779 hid_hw_stop(hdev);
Przemo Firsztd13f5452012-05-10 19:23:52 +0100780destroy_leds:
781 wacom_destroy_leds(hdev);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200782err_free:
783 kfree(wdata);
784 return ret;
785}
786
787static void wacom_remove(struct hid_device *hdev)
788{
Przemo Firszt59d23342010-03-15 19:16:23 +0000789 struct wacom_data *wdata = hid_get_drvdata(hdev);
Przemo Firsztd13f5452012-05-10 19:23:52 +0100790
791 wacom_destroy_leds(hdev);
David Herrmann90866172011-09-05 18:45:29 +0200792 device_remove_file(&hdev->dev, &dev_attr_speed);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200793 hid_hw_stop(hdev);
Przemo Firszt59d23342010-03-15 19:16:23 +0000794
Przemo Firszt59d23342010-03-15 19:16:23 +0000795 power_supply_unregister(&wdata->battery);
796 power_supply_unregister(&wdata->ac);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200797 kfree(hid_get_drvdata(hdev));
798}
799
800static const struct hid_device_id wacom_devices[] = {
801 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
Przemo Firszt78761ff2011-11-05 11:28:22 +0000802 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
Jiri Kosinad5e0a062010-07-20 17:48:48 +0200803
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200804 { }
805};
806MODULE_DEVICE_TABLE(hid, wacom_devices);
807
808static struct hid_driver wacom_driver = {
809 .name = "wacom",
810 .id_table = wacom_devices,
811 .probe = wacom_probe,
812 .remove = wacom_remove,
813 .raw_event = wacom_raw_event,
David Herrmann3797ef62011-10-08 23:20:17 +0200814 .input_mapped = wacom_input_mapped,
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200815};
816
Peter Huewea24f423b2009-07-02 19:08:38 +0200817static int __init wacom_init(void)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200818{
819 int ret;
820
821 ret = hid_register_driver(&wacom_driver);
822 if (ret)
Joe Perches4291ee32010-12-09 19:29:03 -0800823 pr_err("can't register wacom driver\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200824 return ret;
825}
826
Peter Huewea24f423b2009-07-02 19:08:38 +0200827static void __exit wacom_exit(void)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200828{
829 hid_unregister_driver(&wacom_driver);
830}
831
832module_init(wacom_init);
833module_exit(wacom_exit);
Przemo Firszt1fa2f722012-03-28 22:13:37 +0100834MODULE_DESCRIPTION("Driver for Wacom Graphire Bluetooth and Wacom Intuos4 WL");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200835MODULE_LICENSE("GPL");