blob: a66e1aa25caca3003b107416db4e3dffb0e45d56 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Przemo Firszt59d23342010-03-15 19:16:23 +000028#include <linux/power_supply.h>
Bastien Noceraca2dcd42009-05-11 17:18:12 +020029
30#include "hid-ids.h"
31
Przemo Firszt6245bde2012-02-28 17:19:05 +000032#define PAD_DEVICE_ID 0x0F
33
Bastien Noceraca2dcd42009-05-11 17:18:12 +020034struct wacom_data {
35 __u16 tool;
Przemo Firszt6245bde2012-02-28 17:19:05 +000036 __u16 butstate;
Przemo Firszt7e503a32012-03-14 19:55:04 +000037 __u8 whlstate;
Przemo Firszt78761ff2011-11-05 11:28:22 +000038 __u8 features;
Przemo Firszt24709002012-02-24 13:52:32 +000039 __u32 id;
40 __u32 serial;
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +000041 unsigned char high_speed;
Przemo Firszte31e3832012-03-22 18:54:07 +000042 __u8 battery_capacity;
43 __u8 power_raw;
44 __u8 ps_connected;
Przemo Firszt59d23342010-03-15 19:16:23 +000045 struct power_supply battery;
46 struct power_supply ac;
Bastien Noceraca2dcd42009-05-11 17:18:12 +020047};
48
Przemo Firszte31e3832012-03-22 18:54:07 +000049/*percent of battery capacity for Graphire
50 8th value means AC online and show 100% capacity */
51static unsigned short batcap_gr[8] = { 1, 15, 25, 35, 50, 70, 100, 100 };
Przemo Firszt4202f1d2012-03-22 18:54:08 +000052/*percent of battery capacity for Intuos4 WL, AC has a separate bit*/
53static unsigned short batcap_i4[8] = { 1, 15, 30, 45, 60, 70, 85, 100 };
Przemo Firszt59d23342010-03-15 19:16:23 +000054
55static enum power_supply_property wacom_battery_props[] = {
56 POWER_SUPPLY_PROP_PRESENT,
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -080057 POWER_SUPPLY_PROP_CAPACITY,
58 POWER_SUPPLY_PROP_SCOPE,
Przemo Firszt59d23342010-03-15 19:16:23 +000059};
60
61static enum power_supply_property wacom_ac_props[] = {
62 POWER_SUPPLY_PROP_PRESENT,
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -080063 POWER_SUPPLY_PROP_ONLINE,
64 POWER_SUPPLY_PROP_SCOPE,
Przemo Firszt59d23342010-03-15 19:16:23 +000065};
66
67static int wacom_battery_get_property(struct power_supply *psy,
68 enum power_supply_property psp,
69 union power_supply_propval *val)
70{
71 struct wacom_data *wdata = container_of(psy,
72 struct wacom_data, battery);
Przemo Firszt59d23342010-03-15 19:16:23 +000073 int ret = 0;
74
75 switch (psp) {
76 case POWER_SUPPLY_PROP_PRESENT:
77 val->intval = 1;
78 break;
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -080079 case POWER_SUPPLY_PROP_SCOPE:
80 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
81 break;
Przemo Firszt59d23342010-03-15 19:16:23 +000082 case POWER_SUPPLY_PROP_CAPACITY:
Przemo Firszte31e3832012-03-22 18:54:07 +000083 val->intval = wdata->battery_capacity;
Przemo Firszt59d23342010-03-15 19:16:23 +000084 break;
85 default:
86 ret = -EINVAL;
87 break;
88 }
89 return ret;
90}
91
92static int wacom_ac_get_property(struct power_supply *psy,
93 enum power_supply_property psp,
94 union power_supply_propval *val)
95{
96 struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
Przemo Firszt59d23342010-03-15 19:16:23 +000097 int ret = 0;
98
99 switch (psp) {
100 case POWER_SUPPLY_PROP_PRESENT:
101 /* fall through */
102 case POWER_SUPPLY_PROP_ONLINE:
Przemo Firszte31e3832012-03-22 18:54:07 +0000103 val->intval = wdata->ps_connected;
Przemo Firszt59d23342010-03-15 19:16:23 +0000104 break;
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -0800105 case POWER_SUPPLY_PROP_SCOPE:
106 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
107 break;
Przemo Firszt59d23342010-03-15 19:16:23 +0000108 default:
109 ret = -EINVAL;
110 break;
111 }
112 return ret;
113}
Przemo Firszt59d23342010-03-15 19:16:23 +0000114
Przemo Firszt78761ff2011-11-05 11:28:22 +0000115static void wacom_set_features(struct hid_device *hdev)
116{
117 int ret;
118 __u8 rep_data[2];
119
120 /*set high speed, tablet mode*/
121 rep_data[0] = 0x03;
122 rep_data[1] = 0x20;
123 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
124 HID_FEATURE_REPORT);
125 return;
126}
127
Przemo Firszt06c7c312010-03-22 09:55:04 +0100128static void wacom_poke(struct hid_device *hdev, u8 speed)
129{
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000130 struct wacom_data *wdata = hid_get_drvdata(hdev);
Przemo Firszt06c7c312010-03-22 09:55:04 +0100131 int limit, ret;
132 char rep_data[2];
133
134 rep_data[0] = 0x03 ; rep_data[1] = 0x00;
135 limit = 3;
136 do {
137 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
138 HID_FEATURE_REPORT);
139 } while (ret < 0 && limit-- > 0);
140
141 if (ret >= 0) {
142 if (speed == 0)
143 rep_data[0] = 0x05;
144 else
145 rep_data[0] = 0x06;
146
147 rep_data[1] = 0x00;
148 limit = 3;
149 do {
150 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
151 HID_FEATURE_REPORT);
152 } while (ret < 0 && limit-- > 0);
153
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000154 if (ret >= 0) {
155 wdata->high_speed = speed;
Przemo Firszt06c7c312010-03-22 09:55:04 +0100156 return;
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000157 }
Przemo Firszt06c7c312010-03-22 09:55:04 +0100158 }
159
160 /*
161 * Note that if the raw queries fail, it's not a hard failure and it
162 * is safe to continue
163 */
Joe Perches4291ee32010-12-09 19:29:03 -0800164 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
165 rep_data[0], ret);
Przemo Firszt06c7c312010-03-22 09:55:04 +0100166 return;
167}
168
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000169static ssize_t wacom_show_speed(struct device *dev,
170 struct device_attribute
171 *attr, char *buf)
172{
173 struct wacom_data *wdata = dev_get_drvdata(dev);
174
175 return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
176}
177
178static ssize_t wacom_store_speed(struct device *dev,
179 struct device_attribute *attr,
180 const char *buf, size_t count)
181{
182 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
183 int new_speed;
184
185 if (sscanf(buf, "%1d", &new_speed ) != 1)
186 return -EINVAL;
187
188 if (new_speed == 0 || new_speed == 1) {
189 wacom_poke(hdev, new_speed);
190 return strnlen(buf, PAGE_SIZE);
191 } else
192 return -EINVAL;
193}
194
Jiri Kosinaedd21262010-11-18 16:28:43 +0100195static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000196 wacom_show_speed, wacom_store_speed);
197
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000198static int wacom_gr_parse_report(struct hid_device *hdev,
199 struct wacom_data *wdata,
200 struct input_dev *input, unsigned char *data)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200201{
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200202 int tool, x, y, rw;
203
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200204 tool = 0;
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200205 /* Get X & Y positions */
206 x = le16_to_cpu(*(__le16 *) &data[2]);
207 y = le16_to_cpu(*(__le16 *) &data[4]);
208
209 /* Get current tool identifier */
210 if (data[1] & 0x90) { /* If pen is in the in/active area */
211 switch ((data[1] >> 5) & 3) {
212 case 0: /* Pen */
213 tool = BTN_TOOL_PEN;
214 break;
215
216 case 1: /* Rubber */
217 tool = BTN_TOOL_RUBBER;
218 break;
219
220 case 2: /* Mouse with wheel */
221 case 3: /* Mouse without wheel */
222 tool = BTN_TOOL_MOUSE;
223 break;
224 }
225
226 /* Reset tool if out of active tablet area */
227 if (!(data[1] & 0x10))
228 tool = 0;
229 }
230
231 /* If tool changed, notify input subsystem */
232 if (wdata->tool != tool) {
233 if (wdata->tool) {
234 /* Completely reset old tool state */
235 if (wdata->tool == BTN_TOOL_MOUSE) {
236 input_report_key(input, BTN_LEFT, 0);
237 input_report_key(input, BTN_RIGHT, 0);
238 input_report_key(input, BTN_MIDDLE, 0);
239 input_report_abs(input, ABS_DISTANCE,
Daniel Mack987a6c02010-08-02 20:15:17 -0700240 input_abs_get_max(input, ABS_DISTANCE));
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200241 } else {
242 input_report_key(input, BTN_TOUCH, 0);
243 input_report_key(input, BTN_STYLUS, 0);
244 input_report_key(input, BTN_STYLUS2, 0);
245 input_report_abs(input, ABS_PRESSURE, 0);
246 }
247 input_report_key(input, wdata->tool, 0);
248 input_sync(input);
249 }
250 wdata->tool = tool;
251 if (tool)
252 input_report_key(input, tool, 1);
253 }
254
255 if (tool) {
256 input_report_abs(input, ABS_X, x);
257 input_report_abs(input, ABS_Y, y);
258
259 switch ((data[1] >> 5) & 3) {
260 case 2: /* Mouse with wheel */
261 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
262 rw = (data[6] & 0x01) ? -1 :
263 (data[6] & 0x02) ? 1 : 0;
264 input_report_rel(input, REL_WHEEL, rw);
265 /* fall through */
266
267 case 3: /* Mouse without wheel */
268 input_report_key(input, BTN_LEFT, data[1] & 0x01);
269 input_report_key(input, BTN_RIGHT, data[1] & 0x02);
270 /* Compute distance between mouse and tablet */
271 rw = 44 - (data[6] >> 2);
272 if (rw < 0)
273 rw = 0;
274 else if (rw > 31)
275 rw = 31;
276 input_report_abs(input, ABS_DISTANCE, rw);
277 break;
278
279 default:
280 input_report_abs(input, ABS_PRESSURE,
281 data[6] | (((__u16) (data[1] & 0x08)) << 5));
282 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
283 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
284 input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
285 break;
286 }
287
288 input_sync(input);
289 }
290
291 /* Report the state of the two buttons at the top of the tablet
292 * as two extra fingerpad keys (buttons 4 & 5). */
293 rw = data[7] & 0x03;
294 if (rw != wdata->butstate) {
295 wdata->butstate = rw;
296 input_report_key(input, BTN_0, rw & 0x02);
297 input_report_key(input, BTN_1, rw & 0x01);
Przemo Firszt0e253fd2010-01-09 15:20:03 +0100298 input_report_key(input, BTN_TOOL_FINGER, 0xf0);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200299 input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
300 input_sync(input);
301 }
302
Przemo Firszte31e3832012-03-22 18:54:07 +0000303 /* Store current battery capacity and power supply state*/
Przemo Firszt59d23342010-03-15 19:16:23 +0000304 rw = (data[7] >> 2 & 0x07);
Przemo Firszte31e3832012-03-22 18:54:07 +0000305 if (rw != wdata->power_raw) {
306 wdata->power_raw = rw;
307 wdata->battery_capacity = batcap_gr[rw];
308 if (rw == 7)
309 wdata->ps_connected = 1;
310 else
311 wdata->ps_connected = 0;
312 }
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200313 return 1;
314}
Przemo Firszt78761ff2011-11-05 11:28:22 +0000315
Przemo Firszt6245bde2012-02-28 17:19:05 +0000316static void wacom_i4_parse_button_report(struct wacom_data *wdata,
317 struct input_dev *input, unsigned char *data)
318{
319 __u16 new_butstate;
Przemo Firszt7e503a32012-03-14 19:55:04 +0000320 __u8 new_whlstate;
321 __u8 sync = 0;
322
323 new_whlstate = data[1];
324 if (new_whlstate != wdata->whlstate) {
325 wdata->whlstate = new_whlstate;
326 if (new_whlstate & 0x80) {
327 input_report_key(input, BTN_TOUCH, 1);
328 input_report_abs(input, ABS_WHEEL, (new_whlstate & 0x7f));
329 input_report_key(input, BTN_TOOL_FINGER, 1);
330 } else {
331 input_report_key(input, BTN_TOUCH, 0);
332 input_report_abs(input, ABS_WHEEL, 0);
333 input_report_key(input, BTN_TOOL_FINGER, 0);
334 }
335 sync = 1;
336 }
Przemo Firszt6245bde2012-02-28 17:19:05 +0000337
338 new_butstate = (data[3] << 1) | (data[2] & 0x01);
339 if (new_butstate != wdata->butstate) {
340 wdata->butstate = new_butstate;
341 input_report_key(input, BTN_0, new_butstate & 0x001);
342 input_report_key(input, BTN_1, new_butstate & 0x002);
343 input_report_key(input, BTN_2, new_butstate & 0x004);
344 input_report_key(input, BTN_3, new_butstate & 0x008);
345 input_report_key(input, BTN_4, new_butstate & 0x010);
346 input_report_key(input, BTN_5, new_butstate & 0x020);
347 input_report_key(input, BTN_6, new_butstate & 0x040);
348 input_report_key(input, BTN_7, new_butstate & 0x080);
349 input_report_key(input, BTN_8, new_butstate & 0x100);
350 input_report_key(input, BTN_TOOL_FINGER, 1);
Przemo Firszt7e503a32012-03-14 19:55:04 +0000351 sync = 1;
352 }
353
354 if (sync) {
Przemo Firszt6245bde2012-02-28 17:19:05 +0000355 input_report_abs(input, ABS_MISC, PAD_DEVICE_ID);
356 input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff);
357 input_sync(input);
358 }
359}
360
Przemo Firszt78761ff2011-11-05 11:28:22 +0000361static void wacom_i4_parse_pen_report(struct wacom_data *wdata,
362 struct input_dev *input, unsigned char *data)
363{
364 __u16 x, y, pressure;
Przemo Firszte0829e92012-02-28 17:19:04 +0000365 __u8 distance;
Przemo Firsztc653dab2012-05-04 18:00:10 +0100366 __u8 tilt_x, tilt_y;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000367
368 switch (data[1]) {
369 case 0x80: /* Out of proximity report */
Przemo Firszt78761ff2011-11-05 11:28:22 +0000370 input_report_key(input, BTN_TOUCH, 0);
371 input_report_abs(input, ABS_PRESSURE, 0);
Przemo Firszt693f45b2012-03-09 13:20:51 +0000372 input_report_key(input, BTN_STYLUS, 0);
373 input_report_key(input, BTN_STYLUS2, 0);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000374 input_report_key(input, wdata->tool, 0);
Przemo Firszt24709002012-02-24 13:52:32 +0000375 input_report_abs(input, ABS_MISC, 0);
376 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
Przemo Firszt32db7372012-02-19 20:20:29 +0000377 wdata->tool = 0;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000378 input_sync(input);
379 break;
380 case 0xC2: /* Tool report */
Przemo Firszt24709002012-02-24 13:52:32 +0000381 wdata->id = ((data[2] << 4) | (data[3] >> 4) |
Przemo Firszt78761ff2011-11-05 11:28:22 +0000382 ((data[7] & 0x0f) << 20) |
Przemo Firszt24709002012-02-24 13:52:32 +0000383 ((data[8] & 0xf0) << 12));
384 wdata->serial = ((data[3] & 0x0f) << 28) +
385 (data[4] << 20) + (data[5] << 12) +
386 (data[6] << 4) + (data[7] >> 4);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000387
Przemo Firszt24709002012-02-24 13:52:32 +0000388 switch (wdata->id) {
389 case 0x100802:
Przemo Firszt78761ff2011-11-05 11:28:22 +0000390 wdata->tool = BTN_TOOL_PEN;
391 break;
Przemo Firszt24709002012-02-24 13:52:32 +0000392 case 0x10080A:
Przemo Firszt78761ff2011-11-05 11:28:22 +0000393 wdata->tool = BTN_TOOL_RUBBER;
394 break;
395 }
396 break;
397 default: /* Position/pressure report */
398 x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1);
399 y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01);
400 pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5)
401 | (data[1] & 0x01);
Przemo Firszte0829e92012-02-28 17:19:04 +0000402 distance = (data[9] >> 2) & 0x3f;
Przemo Firsztc653dab2012-05-04 18:00:10 +0100403 tilt_x = ((data[7] << 1) & 0x7e) | (data[8] >> 7);
404 tilt_y = data[8] & 0x7f;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000405
406 input_report_key(input, BTN_TOUCH, pressure > 1);
407
408 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
409 input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
410 input_report_key(input, wdata->tool, 1);
411 input_report_abs(input, ABS_X, x);
412 input_report_abs(input, ABS_Y, y);
413 input_report_abs(input, ABS_PRESSURE, pressure);
Przemo Firszte0829e92012-02-28 17:19:04 +0000414 input_report_abs(input, ABS_DISTANCE, distance);
Przemo Firsztc653dab2012-05-04 18:00:10 +0100415 input_report_abs(input, ABS_TILT_X, tilt_x);
416 input_report_abs(input, ABS_TILT_Y, tilt_y);
Przemo Firszt24709002012-02-24 13:52:32 +0000417 input_report_abs(input, ABS_MISC, wdata->id);
418 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
419 input_report_key(input, wdata->tool, 1);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000420 input_sync(input);
421 break;
422 }
423
424 return;
425}
426
427static void wacom_i4_parse_report(struct hid_device *hdev,
428 struct wacom_data *wdata,
429 struct input_dev *input, unsigned char *data)
430{
431 switch (data[0]) {
432 case 0x00: /* Empty report */
433 break;
434 case 0x02: /* Pen report */
435 wacom_i4_parse_pen_report(wdata, input, data);
436 break;
437 case 0x03: /* Features Report */
438 wdata->features = data[2];
439 break;
440 case 0x0C: /* Button report */
Przemo Firszt6245bde2012-02-28 17:19:05 +0000441 wacom_i4_parse_button_report(wdata, input, data);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000442 break;
443 default:
444 hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]);
445 break;
446 }
447}
448
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000449static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
450 u8 *raw_data, int size)
451{
452 struct wacom_data *wdata = hid_get_drvdata(hdev);
453 struct hid_input *hidinput;
454 struct input_dev *input;
455 unsigned char *data = (unsigned char *) raw_data;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000456 int i;
Przemo Firszt4202f1d2012-03-22 18:54:08 +0000457 __u8 power_raw;
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000458
459 if (!(hdev->claimed & HID_CLAIMED_INPUT))
460 return 0;
461
462 hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
463 input = hidinput->input;
464
465 /* Check if this is a tablet report */
466 if (data[0] != 0x03)
467 return 0;
468
Przemo Firszt78761ff2011-11-05 11:28:22 +0000469 switch (hdev->product) {
470 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
471 return wacom_gr_parse_report(hdev, wdata, input, data);
472 break;
473 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
474 i = 1;
475
476 switch (data[0]) {
477 case 0x04:
478 wacom_i4_parse_report(hdev, wdata, input, data + i);
479 i += 10;
480 /* fall through */
481 case 0x03:
482 wacom_i4_parse_report(hdev, wdata, input, data + i);
483 i += 10;
484 wacom_i4_parse_report(hdev, wdata, input, data + i);
Przemo Firszt4202f1d2012-03-22 18:54:08 +0000485 power_raw = data[i+10];
486 if (power_raw != wdata->power_raw) {
487 wdata->power_raw = power_raw;
488 wdata->battery_capacity = batcap_i4[power_raw & 0x07];
489 wdata->ps_connected = power_raw & 0x08;
490 }
491
Przemo Firszt78761ff2011-11-05 11:28:22 +0000492 break;
493 default:
494 hid_err(hdev, "Unknown report: %d,%d size:%d\n",
495 data[0], data[1], size);
496 return 0;
497 }
498 }
499 return 1;
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000500}
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200501
David Herrmann3797ef62011-10-08 23:20:17 +0200502static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
503 struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
504 int *max)
505{
506 struct input_dev *input = hi->input;
507
Jiri Kosinaf6f12422011-10-25 09:58:12 +0200508 __set_bit(INPUT_PROP_POINTER, input->propbit);
509
David Herrmann3797ef62011-10-08 23:20:17 +0200510 /* Basics */
511 input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
512
513 __set_bit(REL_WHEEL, input->relbit);
514
515 __set_bit(BTN_TOOL_PEN, input->keybit);
516 __set_bit(BTN_TOUCH, input->keybit);
517 __set_bit(BTN_STYLUS, input->keybit);
518 __set_bit(BTN_STYLUS2, input->keybit);
519 __set_bit(BTN_LEFT, input->keybit);
520 __set_bit(BTN_RIGHT, input->keybit);
521 __set_bit(BTN_MIDDLE, input->keybit);
522
523 /* Pad */
Przemo Firszt9a911da2012-03-14 19:55:03 +0000524 input_set_capability(input, EV_MSC, MSC_SERIAL);
David Herrmann3797ef62011-10-08 23:20:17 +0200525
526 __set_bit(BTN_0, input->keybit);
527 __set_bit(BTN_1, input->keybit);
528 __set_bit(BTN_TOOL_FINGER, input->keybit);
529
530 /* Distance, rubber and mouse */
531 __set_bit(BTN_TOOL_RUBBER, input->keybit);
532 __set_bit(BTN_TOOL_MOUSE, input->keybit);
533
Przemo Firszt78761ff2011-11-05 11:28:22 +0000534 switch (hdev->product) {
535 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
536 input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
537 input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
538 input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
539 input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
540 break;
541 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
Przemo Firszt7e503a32012-03-14 19:55:04 +0000542 __set_bit(ABS_WHEEL, input->absbit);
Przemo Firszt2c653e62012-02-24 13:47:48 +0000543 __set_bit(ABS_MISC, input->absbit);
Przemo Firszt6245bde2012-02-28 17:19:05 +0000544 __set_bit(BTN_2, input->keybit);
545 __set_bit(BTN_3, input->keybit);
546 __set_bit(BTN_4, input->keybit);
547 __set_bit(BTN_5, input->keybit);
548 __set_bit(BTN_6, input->keybit);
549 __set_bit(BTN_7, input->keybit);
550 __set_bit(BTN_8, input->keybit);
Przemo Firszt7e503a32012-03-14 19:55:04 +0000551 input_set_abs_params(input, ABS_WHEEL, 0, 71, 0, 0);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000552 input_set_abs_params(input, ABS_X, 0, 40640, 4, 0);
553 input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0);
554 input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0);
Przemo Firszte0829e92012-02-28 17:19:04 +0000555 input_set_abs_params(input, ABS_DISTANCE, 0, 63, 0, 0);
Przemo Firsztc653dab2012-05-04 18:00:10 +0100556 input_set_abs_params(input, ABS_TILT_X, 0, 127, 0, 0);
557 input_set_abs_params(input, ABS_TILT_Y, 0, 127, 0, 0);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000558 break;
559 }
David Herrmann3797ef62011-10-08 23:20:17 +0200560
561 return 0;
562}
563
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200564static int wacom_probe(struct hid_device *hdev,
565 const struct hid_device_id *id)
566{
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200567 struct wacom_data *wdata;
568 int ret;
569
570 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
571 if (wdata == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -0800572 hid_err(hdev, "can't alloc wacom descriptor\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200573 return -ENOMEM;
574 }
575
576 hid_set_drvdata(hdev, wdata);
577
Bastien Nocera46a709b2010-01-20 12:00:53 +0000578 /* Parse the HID report now */
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200579 ret = hid_parse(hdev);
580 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800581 hid_err(hdev, "parse failed\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200582 goto err_free;
583 }
584
585 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
586 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800587 hid_err(hdev, "hw start failed\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200588 goto err_free;
589 }
590
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000591 ret = device_create_file(&hdev->dev, &dev_attr_speed);
592 if (ret)
Joe Perches4291ee32010-12-09 19:29:03 -0800593 hid_warn(hdev,
594 "can't create sysfs speed attribute err: %d\n", ret);
Jiri Kosina342f31e2010-02-03 15:52:31 +0100595
Przemo Firszt78761ff2011-11-05 11:28:22 +0000596 switch (hdev->product) {
597 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
598 /* Set Wacom mode 2 with high reporting speed */
599 wacom_poke(hdev, 1);
600 break;
601 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
Przemo Firszta72c5dd2012-02-17 17:47:10 +0000602 sprintf(hdev->name, "%s", "Wacom Intuos4 WL");
Przemo Firszt78761ff2011-11-05 11:28:22 +0000603 wdata->features = 0;
604 wacom_set_features(hdev);
605 break;
606 }
Bastien Nocera46a709b2010-01-20 12:00:53 +0000607
Przemo Firszt59d23342010-03-15 19:16:23 +0000608 wdata->battery.properties = wacom_battery_props;
609 wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
610 wdata->battery.get_property = wacom_battery_get_property;
611 wdata->battery.name = "wacom_battery";
612 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
613 wdata->battery.use_for_apm = 0;
Bastien Nocera46a709b2010-01-20 12:00:53 +0000614
Jeremy Fitzhardinge35b4c012011-12-09 09:35:00 -0800615
Przemo Firszt59d23342010-03-15 19:16:23 +0000616 ret = power_supply_register(&hdev->dev, &wdata->battery);
617 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800618 hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n",
619 ret);
David Herrmanndde58cf2011-09-05 18:45:28 +0200620 goto err_battery;
Przemo Firszt59d23342010-03-15 19:16:23 +0000621 }
622
Przemo Firsztd7cb3db2012-02-05 22:35:24 +0000623 power_supply_powers(&wdata->battery, &hdev->dev);
624
Przemo Firszt59d23342010-03-15 19:16:23 +0000625 wdata->ac.properties = wacom_ac_props;
626 wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
627 wdata->ac.get_property = wacom_ac_get_property;
628 wdata->ac.name = "wacom_ac";
629 wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
630 wdata->ac.use_for_apm = 0;
631
632 ret = power_supply_register(&hdev->dev, &wdata->ac);
633 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800634 hid_warn(hdev,
635 "can't create ac battery attribute, err: %d\n", ret);
David Herrmanndde58cf2011-09-05 18:45:28 +0200636 goto err_ac;
Przemo Firszt59d23342010-03-15 19:16:23 +0000637 }
Przemo Firsztd7cb3db2012-02-05 22:35:24 +0000638
639 power_supply_powers(&wdata->ac, &hdev->dev);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200640 return 0;
Daniel Mack987a6c02010-08-02 20:15:17 -0700641
David Herrmanndde58cf2011-09-05 18:45:28 +0200642err_ac:
643 power_supply_unregister(&wdata->battery);
644err_battery:
645 device_remove_file(&hdev->dev, &dev_attr_speed);
646 hid_hw_stop(hdev);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200647err_free:
648 kfree(wdata);
649 return ret;
650}
651
652static void wacom_remove(struct hid_device *hdev)
653{
Przemo Firszt59d23342010-03-15 19:16:23 +0000654 struct wacom_data *wdata = hid_get_drvdata(hdev);
David Herrmann90866172011-09-05 18:45:29 +0200655 device_remove_file(&hdev->dev, &dev_attr_speed);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200656 hid_hw_stop(hdev);
Przemo Firszt59d23342010-03-15 19:16:23 +0000657
Przemo Firszt59d23342010-03-15 19:16:23 +0000658 power_supply_unregister(&wdata->battery);
659 power_supply_unregister(&wdata->ac);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200660 kfree(hid_get_drvdata(hdev));
661}
662
663static const struct hid_device_id wacom_devices[] = {
664 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
Przemo Firszt78761ff2011-11-05 11:28:22 +0000665 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
Jiri Kosinad5e0a062010-07-20 17:48:48 +0200666
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200667 { }
668};
669MODULE_DEVICE_TABLE(hid, wacom_devices);
670
671static struct hid_driver wacom_driver = {
672 .name = "wacom",
673 .id_table = wacom_devices,
674 .probe = wacom_probe,
675 .remove = wacom_remove,
676 .raw_event = wacom_raw_event,
David Herrmann3797ef62011-10-08 23:20:17 +0200677 .input_mapped = wacom_input_mapped,
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200678};
679
Peter Huewea24f423b2009-07-02 19:08:38 +0200680static int __init wacom_init(void)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200681{
682 int ret;
683
684 ret = hid_register_driver(&wacom_driver);
685 if (ret)
Joe Perches4291ee32010-12-09 19:29:03 -0800686 pr_err("can't register wacom driver\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200687 return ret;
688}
689
Peter Huewea24f423b2009-07-02 19:08:38 +0200690static void __exit wacom_exit(void)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200691{
692 hid_unregister_driver(&wacom_driver);
693}
694
695module_init(wacom_init);
696module_exit(wacom_exit);
Przemo Firszt1fa2f722012-03-28 22:13:37 +0100697MODULE_DESCRIPTION("Driver for Wacom Graphire Bluetooth and Wacom Intuos4 WL");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200698MODULE_LICENSE("GPL");