blob: 5a58db2440fa674f494e11c5da89444816eb71f2 [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#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
29#include <linux/power_supply.h>
30#endif
Bastien Noceraca2dcd42009-05-11 17:18:12 +020031
32#include "hid-ids.h"
33
34struct wacom_data {
35 __u16 tool;
36 unsigned char butstate;
Przemo Firszt78761ff2011-11-05 11:28:22 +000037 __u8 features;
Przemo Firszt24709002012-02-24 13:52:32 +000038 __u32 id;
39 __u32 serial;
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +000040 unsigned char high_speed;
Przemo Firszt59d23342010-03-15 19:16:23 +000041#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
42 int battery_capacity;
43 struct power_supply battery;
44 struct power_supply ac;
45#endif
Bastien Noceraca2dcd42009-05-11 17:18:12 +020046};
47
Przemo Firszt59d23342010-03-15 19:16:23 +000048#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
49/*percent of battery capacity, 0 means AC online*/
50static unsigned short batcap[8] = { 1, 15, 25, 35, 50, 70, 100, 0 };
51
52static enum power_supply_property wacom_battery_props[] = {
53 POWER_SUPPLY_PROP_PRESENT,
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -080054 POWER_SUPPLY_PROP_CAPACITY,
55 POWER_SUPPLY_PROP_SCOPE,
Przemo Firszt59d23342010-03-15 19:16:23 +000056};
57
58static enum power_supply_property wacom_ac_props[] = {
59 POWER_SUPPLY_PROP_PRESENT,
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -080060 POWER_SUPPLY_PROP_ONLINE,
61 POWER_SUPPLY_PROP_SCOPE,
Przemo Firszt59d23342010-03-15 19:16:23 +000062};
63
64static int wacom_battery_get_property(struct power_supply *psy,
65 enum power_supply_property psp,
66 union power_supply_propval *val)
67{
68 struct wacom_data *wdata = container_of(psy,
69 struct wacom_data, battery);
70 int power_state = batcap[wdata->battery_capacity];
71 int ret = 0;
72
73 switch (psp) {
74 case POWER_SUPPLY_PROP_PRESENT:
75 val->intval = 1;
76 break;
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -080077 case POWER_SUPPLY_PROP_SCOPE:
78 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
79 break;
Przemo Firszt59d23342010-03-15 19:16:23 +000080 case POWER_SUPPLY_PROP_CAPACITY:
81 /* show 100% battery capacity when charging */
82 if (power_state == 0)
83 val->intval = 100;
84 else
85 val->intval = power_state;
86 break;
87 default:
88 ret = -EINVAL;
89 break;
90 }
91 return ret;
92}
93
94static int wacom_ac_get_property(struct power_supply *psy,
95 enum power_supply_property psp,
96 union power_supply_propval *val)
97{
98 struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
99 int power_state = batcap[wdata->battery_capacity];
100 int ret = 0;
101
102 switch (psp) {
103 case POWER_SUPPLY_PROP_PRESENT:
104 /* fall through */
105 case POWER_SUPPLY_PROP_ONLINE:
106 if (power_state == 0)
107 val->intval = 1;
108 else
109 val->intval = 0;
110 break;
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -0800111 case POWER_SUPPLY_PROP_SCOPE:
112 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
113 break;
Przemo Firszt59d23342010-03-15 19:16:23 +0000114 default:
115 ret = -EINVAL;
116 break;
117 }
118 return ret;
119}
120#endif
121
Przemo Firszt78761ff2011-11-05 11:28:22 +0000122static void wacom_set_features(struct hid_device *hdev)
123{
124 int ret;
125 __u8 rep_data[2];
126
127 /*set high speed, tablet mode*/
128 rep_data[0] = 0x03;
129 rep_data[1] = 0x20;
130 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
131 HID_FEATURE_REPORT);
132 return;
133}
134
Przemo Firszt06c7c312010-03-22 09:55:04 +0100135static void wacom_poke(struct hid_device *hdev, u8 speed)
136{
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000137 struct wacom_data *wdata = hid_get_drvdata(hdev);
Przemo Firszt06c7c312010-03-22 09:55:04 +0100138 int limit, ret;
139 char rep_data[2];
140
141 rep_data[0] = 0x03 ; rep_data[1] = 0x00;
142 limit = 3;
143 do {
144 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
145 HID_FEATURE_REPORT);
146 } while (ret < 0 && limit-- > 0);
147
148 if (ret >= 0) {
149 if (speed == 0)
150 rep_data[0] = 0x05;
151 else
152 rep_data[0] = 0x06;
153
154 rep_data[1] = 0x00;
155 limit = 3;
156 do {
157 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
158 HID_FEATURE_REPORT);
159 } while (ret < 0 && limit-- > 0);
160
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000161 if (ret >= 0) {
162 wdata->high_speed = speed;
Przemo Firszt06c7c312010-03-22 09:55:04 +0100163 return;
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000164 }
Przemo Firszt06c7c312010-03-22 09:55:04 +0100165 }
166
167 /*
168 * Note that if the raw queries fail, it's not a hard failure and it
169 * is safe to continue
170 */
Joe Perches4291ee32010-12-09 19:29:03 -0800171 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
172 rep_data[0], ret);
Przemo Firszt06c7c312010-03-22 09:55:04 +0100173 return;
174}
175
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000176static ssize_t wacom_show_speed(struct device *dev,
177 struct device_attribute
178 *attr, char *buf)
179{
180 struct wacom_data *wdata = dev_get_drvdata(dev);
181
182 return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
183}
184
185static ssize_t wacom_store_speed(struct device *dev,
186 struct device_attribute *attr,
187 const char *buf, size_t count)
188{
189 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
190 int new_speed;
191
192 if (sscanf(buf, "%1d", &new_speed ) != 1)
193 return -EINVAL;
194
195 if (new_speed == 0 || new_speed == 1) {
196 wacom_poke(hdev, new_speed);
197 return strnlen(buf, PAGE_SIZE);
198 } else
199 return -EINVAL;
200}
201
Jiri Kosinaedd21262010-11-18 16:28:43 +0100202static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000203 wacom_show_speed, wacom_store_speed);
204
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000205static int wacom_gr_parse_report(struct hid_device *hdev,
206 struct wacom_data *wdata,
207 struct input_dev *input, unsigned char *data)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200208{
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200209 int tool, x, y, rw;
210
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200211 tool = 0;
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200212 /* Get X & Y positions */
213 x = le16_to_cpu(*(__le16 *) &data[2]);
214 y = le16_to_cpu(*(__le16 *) &data[4]);
215
216 /* Get current tool identifier */
217 if (data[1] & 0x90) { /* If pen is in the in/active area */
218 switch ((data[1] >> 5) & 3) {
219 case 0: /* Pen */
220 tool = BTN_TOOL_PEN;
221 break;
222
223 case 1: /* Rubber */
224 tool = BTN_TOOL_RUBBER;
225 break;
226
227 case 2: /* Mouse with wheel */
228 case 3: /* Mouse without wheel */
229 tool = BTN_TOOL_MOUSE;
230 break;
231 }
232
233 /* Reset tool if out of active tablet area */
234 if (!(data[1] & 0x10))
235 tool = 0;
236 }
237
238 /* If tool changed, notify input subsystem */
239 if (wdata->tool != tool) {
240 if (wdata->tool) {
241 /* Completely reset old tool state */
242 if (wdata->tool == BTN_TOOL_MOUSE) {
243 input_report_key(input, BTN_LEFT, 0);
244 input_report_key(input, BTN_RIGHT, 0);
245 input_report_key(input, BTN_MIDDLE, 0);
246 input_report_abs(input, ABS_DISTANCE,
Daniel Mack987a6c02010-08-02 20:15:17 -0700247 input_abs_get_max(input, ABS_DISTANCE));
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200248 } else {
249 input_report_key(input, BTN_TOUCH, 0);
250 input_report_key(input, BTN_STYLUS, 0);
251 input_report_key(input, BTN_STYLUS2, 0);
252 input_report_abs(input, ABS_PRESSURE, 0);
253 }
254 input_report_key(input, wdata->tool, 0);
255 input_sync(input);
256 }
257 wdata->tool = tool;
258 if (tool)
259 input_report_key(input, tool, 1);
260 }
261
262 if (tool) {
263 input_report_abs(input, ABS_X, x);
264 input_report_abs(input, ABS_Y, y);
265
266 switch ((data[1] >> 5) & 3) {
267 case 2: /* Mouse with wheel */
268 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
269 rw = (data[6] & 0x01) ? -1 :
270 (data[6] & 0x02) ? 1 : 0;
271 input_report_rel(input, REL_WHEEL, rw);
272 /* fall through */
273
274 case 3: /* Mouse without wheel */
275 input_report_key(input, BTN_LEFT, data[1] & 0x01);
276 input_report_key(input, BTN_RIGHT, data[1] & 0x02);
277 /* Compute distance between mouse and tablet */
278 rw = 44 - (data[6] >> 2);
279 if (rw < 0)
280 rw = 0;
281 else if (rw > 31)
282 rw = 31;
283 input_report_abs(input, ABS_DISTANCE, rw);
284 break;
285
286 default:
287 input_report_abs(input, ABS_PRESSURE,
288 data[6] | (((__u16) (data[1] & 0x08)) << 5));
289 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
290 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
291 input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
292 break;
293 }
294
295 input_sync(input);
296 }
297
298 /* Report the state of the two buttons at the top of the tablet
299 * as two extra fingerpad keys (buttons 4 & 5). */
300 rw = data[7] & 0x03;
301 if (rw != wdata->butstate) {
302 wdata->butstate = rw;
303 input_report_key(input, BTN_0, rw & 0x02);
304 input_report_key(input, BTN_1, rw & 0x01);
Przemo Firszt0e253fd2010-01-09 15:20:03 +0100305 input_report_key(input, BTN_TOOL_FINGER, 0xf0);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200306 input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
307 input_sync(input);
308 }
309
Przemo Firszt59d23342010-03-15 19:16:23 +0000310#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
311 /* Store current battery capacity */
312 rw = (data[7] >> 2 & 0x07);
313 if (rw != wdata->battery_capacity)
314 wdata->battery_capacity = rw;
315#endif
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200316 return 1;
317}
Przemo Firszt78761ff2011-11-05 11:28:22 +0000318
319static void wacom_i4_parse_pen_report(struct wacom_data *wdata,
320 struct input_dev *input, unsigned char *data)
321{
322 __u16 x, y, pressure;
Przemo Firszte0829e92012-02-28 17:19:04 +0000323 __u8 distance;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000324
325 switch (data[1]) {
326 case 0x80: /* Out of proximity report */
Przemo Firszt78761ff2011-11-05 11:28:22 +0000327 input_report_key(input, BTN_TOUCH, 0);
328 input_report_abs(input, ABS_PRESSURE, 0);
329 input_report_key(input, wdata->tool, 0);
Przemo Firszt24709002012-02-24 13:52:32 +0000330 input_report_abs(input, ABS_MISC, 0);
331 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
Przemo Firszt32db7372012-02-19 20:20:29 +0000332 wdata->tool = 0;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000333 input_sync(input);
334 break;
335 case 0xC2: /* Tool report */
Przemo Firszt24709002012-02-24 13:52:32 +0000336 wdata->id = ((data[2] << 4) | (data[3] >> 4) |
Przemo Firszt78761ff2011-11-05 11:28:22 +0000337 ((data[7] & 0x0f) << 20) |
Przemo Firszt24709002012-02-24 13:52:32 +0000338 ((data[8] & 0xf0) << 12));
339 wdata->serial = ((data[3] & 0x0f) << 28) +
340 (data[4] << 20) + (data[5] << 12) +
341 (data[6] << 4) + (data[7] >> 4);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000342
Przemo Firszt24709002012-02-24 13:52:32 +0000343 switch (wdata->id) {
344 case 0x100802:
Przemo Firszt78761ff2011-11-05 11:28:22 +0000345 wdata->tool = BTN_TOOL_PEN;
346 break;
Przemo Firszt24709002012-02-24 13:52:32 +0000347 case 0x10080A:
Przemo Firszt78761ff2011-11-05 11:28:22 +0000348 wdata->tool = BTN_TOOL_RUBBER;
349 break;
350 }
351 break;
352 default: /* Position/pressure report */
353 x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1);
354 y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01);
355 pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5)
356 | (data[1] & 0x01);
Przemo Firszte0829e92012-02-28 17:19:04 +0000357 distance = (data[9] >> 2) & 0x3f;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000358
359 input_report_key(input, BTN_TOUCH, pressure > 1);
360
361 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
362 input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
363 input_report_key(input, wdata->tool, 1);
364 input_report_abs(input, ABS_X, x);
365 input_report_abs(input, ABS_Y, y);
366 input_report_abs(input, ABS_PRESSURE, pressure);
Przemo Firszte0829e92012-02-28 17:19:04 +0000367 input_report_abs(input, ABS_DISTANCE, distance);
Przemo Firszt24709002012-02-24 13:52:32 +0000368 input_report_abs(input, ABS_MISC, wdata->id);
369 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
370 input_report_key(input, wdata->tool, 1);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000371 input_sync(input);
372 break;
373 }
374
375 return;
376}
377
378static void wacom_i4_parse_report(struct hid_device *hdev,
379 struct wacom_data *wdata,
380 struct input_dev *input, unsigned char *data)
381{
382 switch (data[0]) {
383 case 0x00: /* Empty report */
384 break;
385 case 0x02: /* Pen report */
386 wacom_i4_parse_pen_report(wdata, input, data);
387 break;
388 case 0x03: /* Features Report */
389 wdata->features = data[2];
390 break;
391 case 0x0C: /* Button report */
392 break;
393 default:
394 hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]);
395 break;
396 }
397}
398
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000399static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
400 u8 *raw_data, int size)
401{
402 struct wacom_data *wdata = hid_get_drvdata(hdev);
403 struct hid_input *hidinput;
404 struct input_dev *input;
405 unsigned char *data = (unsigned char *) raw_data;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000406 int i;
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000407
408 if (!(hdev->claimed & HID_CLAIMED_INPUT))
409 return 0;
410
411 hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
412 input = hidinput->input;
413
414 /* Check if this is a tablet report */
415 if (data[0] != 0x03)
416 return 0;
417
Przemo Firszt78761ff2011-11-05 11:28:22 +0000418 switch (hdev->product) {
419 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
420 return wacom_gr_parse_report(hdev, wdata, input, data);
421 break;
422 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
423 i = 1;
424
425 switch (data[0]) {
426 case 0x04:
427 wacom_i4_parse_report(hdev, wdata, input, data + i);
428 i += 10;
429 /* fall through */
430 case 0x03:
431 wacom_i4_parse_report(hdev, wdata, input, data + i);
432 i += 10;
433 wacom_i4_parse_report(hdev, wdata, input, data + i);
434 break;
435 default:
436 hid_err(hdev, "Unknown report: %d,%d size:%d\n",
437 data[0], data[1], size);
438 return 0;
439 }
440 }
441 return 1;
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000442}
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200443
David Herrmann3797ef62011-10-08 23:20:17 +0200444static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
445 struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
446 int *max)
447{
448 struct input_dev *input = hi->input;
449
Jiri Kosinaf6f12422011-10-25 09:58:12 +0200450 __set_bit(INPUT_PROP_POINTER, input->propbit);
451
David Herrmann3797ef62011-10-08 23:20:17 +0200452 /* Basics */
453 input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
454
455 __set_bit(REL_WHEEL, input->relbit);
456
457 __set_bit(BTN_TOOL_PEN, input->keybit);
458 __set_bit(BTN_TOUCH, input->keybit);
459 __set_bit(BTN_STYLUS, input->keybit);
460 __set_bit(BTN_STYLUS2, input->keybit);
461 __set_bit(BTN_LEFT, input->keybit);
462 __set_bit(BTN_RIGHT, input->keybit);
463 __set_bit(BTN_MIDDLE, input->keybit);
464
465 /* Pad */
466 input->evbit[0] |= BIT(EV_MSC);
467
468 __set_bit(MSC_SERIAL, input->mscbit);
469
470 __set_bit(BTN_0, input->keybit);
471 __set_bit(BTN_1, input->keybit);
472 __set_bit(BTN_TOOL_FINGER, input->keybit);
473
474 /* Distance, rubber and mouse */
475 __set_bit(BTN_TOOL_RUBBER, input->keybit);
476 __set_bit(BTN_TOOL_MOUSE, input->keybit);
477
Przemo Firszt78761ff2011-11-05 11:28:22 +0000478 switch (hdev->product) {
479 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
480 input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
481 input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
482 input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
483 input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
484 break;
485 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
Przemo Firszt2c653e62012-02-24 13:47:48 +0000486 __set_bit(ABS_MISC, input->absbit);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000487 input_set_abs_params(input, ABS_X, 0, 40640, 4, 0);
488 input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0);
489 input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0);
Przemo Firszte0829e92012-02-28 17:19:04 +0000490 input_set_abs_params(input, ABS_DISTANCE, 0, 63, 0, 0);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000491 break;
492 }
David Herrmann3797ef62011-10-08 23:20:17 +0200493
494 return 0;
495}
496
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200497static int wacom_probe(struct hid_device *hdev,
498 const struct hid_device_id *id)
499{
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200500 struct wacom_data *wdata;
501 int ret;
502
503 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
504 if (wdata == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -0800505 hid_err(hdev, "can't alloc wacom descriptor\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200506 return -ENOMEM;
507 }
508
509 hid_set_drvdata(hdev, wdata);
510
Bastien Nocera46a709b2010-01-20 12:00:53 +0000511 /* Parse the HID report now */
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200512 ret = hid_parse(hdev);
513 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800514 hid_err(hdev, "parse failed\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200515 goto err_free;
516 }
517
518 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
519 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800520 hid_err(hdev, "hw start failed\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200521 goto err_free;
522 }
523
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000524 ret = device_create_file(&hdev->dev, &dev_attr_speed);
525 if (ret)
Joe Perches4291ee32010-12-09 19:29:03 -0800526 hid_warn(hdev,
527 "can't create sysfs speed attribute err: %d\n", ret);
Jiri Kosina342f31e2010-02-03 15:52:31 +0100528
Przemo Firszt78761ff2011-11-05 11:28:22 +0000529 switch (hdev->product) {
530 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
531 /* Set Wacom mode 2 with high reporting speed */
532 wacom_poke(hdev, 1);
533 break;
534 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
535 wdata->features = 0;
536 wacom_set_features(hdev);
537 break;
538 }
Bastien Nocera46a709b2010-01-20 12:00:53 +0000539
Przemo Firszt59d23342010-03-15 19:16:23 +0000540#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
541 wdata->battery.properties = wacom_battery_props;
542 wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
543 wdata->battery.get_property = wacom_battery_get_property;
544 wdata->battery.name = "wacom_battery";
545 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
546 wdata->battery.use_for_apm = 0;
Bastien Nocera46a709b2010-01-20 12:00:53 +0000547
Jeremy Fitzhardinge35b4c012011-12-09 09:35:00 -0800548
Przemo Firszt59d23342010-03-15 19:16:23 +0000549 ret = power_supply_register(&hdev->dev, &wdata->battery);
550 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800551 hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n",
552 ret);
David Herrmanndde58cf2011-09-05 18:45:28 +0200553 goto err_battery;
Przemo Firszt59d23342010-03-15 19:16:23 +0000554 }
555
Przemo Firsztd7cb3db2012-02-05 22:35:24 +0000556 power_supply_powers(&wdata->battery, &hdev->dev);
557
Przemo Firszt59d23342010-03-15 19:16:23 +0000558 wdata->ac.properties = wacom_ac_props;
559 wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
560 wdata->ac.get_property = wacom_ac_get_property;
561 wdata->ac.name = "wacom_ac";
562 wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
563 wdata->ac.use_for_apm = 0;
564
565 ret = power_supply_register(&hdev->dev, &wdata->ac);
566 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800567 hid_warn(hdev,
568 "can't create ac battery attribute, err: %d\n", ret);
David Herrmanndde58cf2011-09-05 18:45:28 +0200569 goto err_ac;
Przemo Firszt59d23342010-03-15 19:16:23 +0000570 }
Przemo Firsztd7cb3db2012-02-05 22:35:24 +0000571
572 power_supply_powers(&wdata->ac, &hdev->dev);
Przemo Firszt59d23342010-03-15 19:16:23 +0000573#endif
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200574 return 0;
Daniel Mack987a6c02010-08-02 20:15:17 -0700575
David Herrmanndde58cf2011-09-05 18:45:28 +0200576#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
577err_ac:
578 power_supply_unregister(&wdata->battery);
579err_battery:
580 device_remove_file(&hdev->dev, &dev_attr_speed);
581 hid_hw_stop(hdev);
582#endif
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200583err_free:
584 kfree(wdata);
585 return ret;
586}
587
588static void wacom_remove(struct hid_device *hdev)
589{
Przemo Firszt59d23342010-03-15 19:16:23 +0000590#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
591 struct wacom_data *wdata = hid_get_drvdata(hdev);
592#endif
David Herrmann90866172011-09-05 18:45:29 +0200593 device_remove_file(&hdev->dev, &dev_attr_speed);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200594 hid_hw_stop(hdev);
Przemo Firszt59d23342010-03-15 19:16:23 +0000595
596#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
597 power_supply_unregister(&wdata->battery);
598 power_supply_unregister(&wdata->ac);
599#endif
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200600 kfree(hid_get_drvdata(hdev));
601}
602
603static const struct hid_device_id wacom_devices[] = {
604 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
Przemo Firszt78761ff2011-11-05 11:28:22 +0000605 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
Jiri Kosinad5e0a062010-07-20 17:48:48 +0200606
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200607 { }
608};
609MODULE_DEVICE_TABLE(hid, wacom_devices);
610
611static struct hid_driver wacom_driver = {
612 .name = "wacom",
613 .id_table = wacom_devices,
614 .probe = wacom_probe,
615 .remove = wacom_remove,
616 .raw_event = wacom_raw_event,
David Herrmann3797ef62011-10-08 23:20:17 +0200617 .input_mapped = wacom_input_mapped,
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200618};
619
Peter Huewea24f423b2009-07-02 19:08:38 +0200620static int __init wacom_init(void)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200621{
622 int ret;
623
624 ret = hid_register_driver(&wacom_driver);
625 if (ret)
Joe Perches4291ee32010-12-09 19:29:03 -0800626 pr_err("can't register wacom driver\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200627 return ret;
628}
629
Peter Huewea24f423b2009-07-02 19:08:38 +0200630static void __exit wacom_exit(void)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200631{
632 hid_unregister_driver(&wacom_driver);
633}
634
635module_init(wacom_init);
636module_exit(wacom_exit);
637MODULE_LICENSE("GPL");
638