]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/hid/hid-wacom.c
HID: introduce helper to access hid_output_raw_report()
[karo-tx-linux.git] / drivers / hid / hid-wacom.c
1 /*
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) 2008 Jiri Slaby <jirislaby@gmail.com>
9  *  Copyright (c) 2006 Andrew Zabolotny <zap@homelink.ru>
10  *  Copyright (c) 2009 Bastien Nocera <hadess@hadess.net>
11  *  Copyright (c) 2011 Przemysław Firszt <przemo@firszt.eu>
12  */
13
14 /*
15  * This program is free software; you can redistribute it and/or modify it
16  * under the terms of the GNU General Public License as published by the Free
17  * Software Foundation; either version 2 of the License, or (at your option)
18  * any later version.
19  */
20
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include <linux/device.h>
24 #include <linux/hid.h>
25 #include <linux/module.h>
26 #include <linux/leds.h>
27 #include <linux/slab.h>
28 #include <linux/power_supply.h>
29
30 #include "hid-ids.h"
31
32 #define PAD_DEVICE_ID   0x0F
33
34 #define WAC_CMD_LED_CONTROL     0x20
35 #define WAC_CMD_ICON_START_STOP     0x21
36 #define WAC_CMD_ICON_TRANSFER       0x26
37
38 struct wacom_data {
39         __u16 tool;
40         __u16 butstate;
41         __u8 whlstate;
42         __u8 features;
43         __u32 id;
44         __u32 serial;
45         unsigned char high_speed;
46         __u8 battery_capacity;
47         __u8 power_raw;
48         __u8 ps_connected;
49         __u8 bat_charging;
50         struct power_supply battery;
51         struct power_supply ac;
52         __u8 led_selector;
53         struct led_classdev *leds[4];
54 };
55
56 /*percent of battery capacity for Graphire
57   8th value means AC online and show 100% capacity */
58 static unsigned short batcap_gr[8] = { 1, 15, 25, 35, 50, 70, 100, 100 };
59 /*percent of battery capacity for Intuos4 WL, AC has a separate bit*/
60 static unsigned short batcap_i4[8] = { 1, 15, 30, 45, 60, 70, 85, 100 };
61
62 static enum power_supply_property wacom_battery_props[] = {
63         POWER_SUPPLY_PROP_PRESENT,
64         POWER_SUPPLY_PROP_CAPACITY,
65         POWER_SUPPLY_PROP_SCOPE,
66         POWER_SUPPLY_PROP_STATUS,
67 };
68
69 static enum power_supply_property wacom_ac_props[] = {
70         POWER_SUPPLY_PROP_PRESENT,
71         POWER_SUPPLY_PROP_ONLINE,
72         POWER_SUPPLY_PROP_SCOPE,
73 };
74
75 static void wacom_scramble(__u8 *image)
76 {
77         __u16 mask;
78         __u16 s1;
79         __u16 s2;
80         __u16 r1 ;
81         __u16 r2 ;
82         __u16 r;
83         __u8 buf[256];
84         int i, w, x, y, z;
85
86         for (x = 0; x < 32; x++) {
87                 for (y = 0; y < 8; y++)
88                         buf[(8 * x) + (7 - y)] = image[(8 * x) + y];
89         }
90
91         /* Change 76543210 into GECA6420 as required by Intuos4 WL
92          *        HGFEDCBA      HFDB7531
93          */
94         for (x = 0; x < 4; x++) {
95                 for (y = 0; y < 4; y++) {
96                         for (z = 0; z < 8; z++) {
97                                 mask = 0x0001;
98                                 r1 = 0;
99                                 r2 = 0;
100                                 i = (x << 6) + (y << 4) + z;
101                                 s1 = buf[i];
102                                 s2 = buf[i+8];
103                                 for (w = 0; w < 8; w++) {
104                                         r1 |= (s1 & mask);
105                                         r2 |= (s2 & mask);
106                                         s1 <<= 1;
107                                         s2 <<= 1;
108                                         mask <<= 2;
109                                 }
110                                 r = r1 | (r2 << 1);
111                                 i = (x << 6) + (y << 4) + (z << 1);
112                                 image[i] = 0xFF & r;
113                                 image[i+1] = (0xFF00 & r) >> 8;
114                         }
115                 }
116         }
117 }
118
119 static void wacom_set_image(struct hid_device *hdev, const char *image,
120                                                 __u8 icon_no)
121 {
122         __u8 rep_data[68];
123         __u8 p[256];
124         int ret, i, j;
125
126         for (i = 0; i < 256; i++)
127                 p[i] = image[i];
128
129         rep_data[0] = WAC_CMD_ICON_START_STOP;
130         rep_data[1] = 0;
131         ret = hid_output_raw_report(hdev, rep_data, 2, HID_FEATURE_REPORT);
132         if (ret < 0)
133                 goto err;
134
135         rep_data[0] = WAC_CMD_ICON_TRANSFER;
136         rep_data[1] = icon_no & 0x07;
137
138         wacom_scramble(p);
139
140         for (i = 0; i < 4; i++) {
141                 for (j = 0; j < 64; j++)
142                         rep_data[j + 3] = p[(i << 6) + j];
143
144                 rep_data[2] = i;
145                 ret = hid_output_raw_report(hdev, rep_data, 67,
146                                         HID_FEATURE_REPORT);
147         }
148
149         rep_data[0] = WAC_CMD_ICON_START_STOP;
150         rep_data[1] = 0;
151
152         ret = hid_output_raw_report(hdev, rep_data, 2, HID_FEATURE_REPORT);
153
154 err:
155         return;
156 }
157
158 static void wacom_leds_set_brightness(struct led_classdev *led_dev,
159                                                 enum led_brightness value)
160 {
161         struct device *dev = led_dev->dev->parent;
162         struct hid_device *hdev;
163         struct wacom_data *wdata;
164         unsigned char *buf;
165         __u8 led = 0;
166         int i;
167
168         hdev = container_of(dev, struct hid_device, dev);
169         wdata = hid_get_drvdata(hdev);
170         for (i = 0; i < 4; ++i) {
171                 if (wdata->leds[i] == led_dev)
172                         wdata->led_selector = i;
173         }
174
175         led = wdata->led_selector | 0x04;
176         buf = kzalloc(9, GFP_KERNEL);
177         if (buf) {
178                 buf[0] = WAC_CMD_LED_CONTROL;
179                 buf[1] = led;
180                 buf[2] = value >> 2;
181                 buf[3] = value;
182                 /* use fixed brightness for OLEDs */
183                 buf[4] = 0x08;
184                 hid_output_raw_report(hdev, buf, 9, HID_FEATURE_REPORT);
185                 kfree(buf);
186         }
187
188         return;
189 }
190
191 static enum led_brightness wacom_leds_get_brightness(struct led_classdev *led_dev)
192 {
193         struct wacom_data *wdata;
194         struct device *dev = led_dev->dev->parent;
195         int value = 0;
196         int i;
197
198         wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
199
200         for (i = 0; i < 4; ++i) {
201                 if (wdata->leds[i] == led_dev) {
202                         value = wdata->leds[i]->brightness;
203                         break;
204                 }
205         }
206
207         return value;
208 }
209
210
211 static int wacom_initialize_leds(struct hid_device *hdev)
212 {
213         struct wacom_data *wdata = hid_get_drvdata(hdev);
214         struct led_classdev *led;
215         struct device *dev = &hdev->dev;
216         size_t namesz = strlen(dev_name(dev)) + 12;
217         char *name;
218         int i, ret;
219
220         wdata->led_selector = 0;
221
222         for (i = 0; i < 4; i++) {
223                 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
224                 if (!led) {
225                         hid_warn(hdev,
226                                  "can't allocate memory for LED selector\n");
227                         ret = -ENOMEM;
228                         goto err;
229                 }
230
231                 name = (void *)&led[1];
232                 snprintf(name, namesz, "%s:selector:%d", dev_name(dev), i);
233                 led->name = name;
234                 led->brightness = 0;
235                 led->max_brightness = 127;
236                 led->brightness_get = wacom_leds_get_brightness;
237                 led->brightness_set = wacom_leds_set_brightness;
238
239                 wdata->leds[i] = led;
240
241                 ret = led_classdev_register(dev, wdata->leds[i]);
242
243                 if (ret) {
244                         wdata->leds[i] = NULL;
245                         kfree(led);
246                         hid_warn(hdev, "can't register LED\n");
247                         goto err;
248                 }
249         }
250
251 err:
252         return ret;
253 }
254
255 static void wacom_destroy_leds(struct hid_device *hdev)
256 {
257         struct wacom_data *wdata = hid_get_drvdata(hdev);
258         struct led_classdev *led;
259         int i;
260
261         for (i = 0; i < 4; ++i) {
262                 if (wdata->leds[i]) {
263                         led = wdata->leds[i];
264                         wdata->leds[i] = NULL;
265                         led_classdev_unregister(led);
266                         kfree(led);
267                 }
268         }
269
270 }
271
272 static int wacom_battery_get_property(struct power_supply *psy,
273                                 enum power_supply_property psp,
274                                 union power_supply_propval *val)
275 {
276         struct wacom_data *wdata = container_of(psy,
277                                         struct wacom_data, battery);
278         int ret = 0;
279
280         switch (psp) {
281         case POWER_SUPPLY_PROP_PRESENT:
282                 val->intval = 1;
283                 break;
284         case POWER_SUPPLY_PROP_SCOPE:
285                 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
286                 break;
287         case POWER_SUPPLY_PROP_CAPACITY:
288                 val->intval = wdata->battery_capacity;
289                 break;
290         case POWER_SUPPLY_PROP_STATUS:
291                 if (wdata->bat_charging)
292                         val->intval = POWER_SUPPLY_STATUS_CHARGING;
293                 else
294                         if (wdata->battery_capacity == 100 && wdata->ps_connected)
295                                 val->intval = POWER_SUPPLY_STATUS_FULL;
296                         else
297                                 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
298                 break;
299         default:
300                 ret = -EINVAL;
301                 break;
302         }
303         return ret;
304 }
305
306 static int wacom_ac_get_property(struct power_supply *psy,
307                                 enum power_supply_property psp,
308                                 union power_supply_propval *val)
309 {
310         struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
311         int ret = 0;
312
313         switch (psp) {
314         case POWER_SUPPLY_PROP_PRESENT:
315                 /* fall through */
316         case POWER_SUPPLY_PROP_ONLINE:
317                 val->intval = wdata->ps_connected;
318                 break;
319         case POWER_SUPPLY_PROP_SCOPE:
320                 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
321                 break;
322         default:
323                 ret = -EINVAL;
324                 break;
325         }
326         return ret;
327 }
328
329 static void wacom_set_features(struct hid_device *hdev, u8 speed)
330 {
331         struct wacom_data *wdata = hid_get_drvdata(hdev);
332         int limit, ret;
333         __u8 rep_data[2];
334
335         switch (hdev->product) {
336         case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
337                 rep_data[0] = 0x03 ; rep_data[1] = 0x00;
338                 limit = 3;
339                 do {
340                         ret = hid_output_raw_report(hdev, rep_data, 2,
341                                         HID_FEATURE_REPORT);
342                 } while (ret < 0 && limit-- > 0);
343
344                 if (ret >= 0) {
345                         if (speed == 0)
346                                 rep_data[0] = 0x05;
347                         else
348                                 rep_data[0] = 0x06;
349
350                         rep_data[1] = 0x00;
351                         limit = 3;
352                         do {
353                                 ret = hid_output_raw_report(hdev,
354                                         rep_data, 2, HID_FEATURE_REPORT);
355                         } while (ret < 0 && limit-- > 0);
356
357                         if (ret >= 0) {
358                                 wdata->high_speed = speed;
359                                 return;
360                         }
361                 }
362
363                 /*
364                  * Note that if the raw queries fail, it's not a hard failure
365                  * and it is safe to continue
366                  */
367                 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
368                          rep_data[0], ret);
369                 break;
370         case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
371                 if (speed == 1)
372                         wdata->features &= ~0x20;
373                 else
374                         wdata->features |= 0x20;
375
376                 rep_data[0] = 0x03;
377                 rep_data[1] = wdata->features;
378
379                 ret = hid_output_raw_report(hdev, rep_data, 2,
380                                         HID_FEATURE_REPORT);
381                 if (ret >= 0)
382                         wdata->high_speed = speed;
383                 break;
384         }
385
386         return;
387 }
388
389 static ssize_t wacom_show_speed(struct device *dev,
390                                 struct device_attribute
391                                 *attr, char *buf)
392 {
393         struct wacom_data *wdata = dev_get_drvdata(dev);
394
395         return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
396 }
397
398 static ssize_t wacom_store_speed(struct device *dev,
399                                 struct device_attribute *attr,
400                                 const char *buf, size_t count)
401 {
402         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
403         int new_speed;
404
405         if (sscanf(buf, "%1d", &new_speed ) != 1)
406                 return -EINVAL;
407
408         if (new_speed == 0 || new_speed == 1) {
409                 wacom_set_features(hdev, new_speed);
410                 return strnlen(buf, PAGE_SIZE);
411         } else
412                 return -EINVAL;
413 }
414
415 static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
416                 wacom_show_speed, wacom_store_speed);
417
418 #define WACOM_STORE(OLED_ID)                                            \
419 static ssize_t wacom_oled##OLED_ID##_store(struct device *dev,          \
420                                 struct device_attribute *attr,          \
421                                 const char *buf, size_t count)          \
422 {                                                                       \
423         struct hid_device *hdev = container_of(dev, struct hid_device,  \
424                                 dev);                                   \
425                                                                         \
426         if (count != 256)                                               \
427                 return -EINVAL;                                         \
428                                                                         \
429         wacom_set_image(hdev, buf, OLED_ID);                            \
430                                                                         \
431         return count;                                                   \
432 }                                                                       \
433                                                                         \
434 static DEVICE_ATTR(oled##OLED_ID##_img, S_IWUSR | S_IWGRP, NULL,        \
435                                 wacom_oled##OLED_ID##_store)
436
437 WACOM_STORE(0);
438 WACOM_STORE(1);
439 WACOM_STORE(2);
440 WACOM_STORE(3);
441 WACOM_STORE(4);
442 WACOM_STORE(5);
443 WACOM_STORE(6);
444 WACOM_STORE(7);
445
446 static int wacom_gr_parse_report(struct hid_device *hdev,
447                         struct wacom_data *wdata,
448                         struct input_dev *input, unsigned char *data)
449 {
450         int tool, x, y, rw;
451
452         tool = 0;
453         /* Get X & Y positions */
454         x = le16_to_cpu(*(__le16 *) &data[2]);
455         y = le16_to_cpu(*(__le16 *) &data[4]);
456
457         /* Get current tool identifier */
458         if (data[1] & 0x90) { /* If pen is in the in/active area */
459                 switch ((data[1] >> 5) & 3) {
460                 case 0: /* Pen */
461                         tool = BTN_TOOL_PEN;
462                         break;
463
464                 case 1: /* Rubber */
465                         tool = BTN_TOOL_RUBBER;
466                         break;
467
468                 case 2: /* Mouse with wheel */
469                 case 3: /* Mouse without wheel */
470                         tool = BTN_TOOL_MOUSE;
471                         break;
472                 }
473
474                 /* Reset tool if out of active tablet area */
475                 if (!(data[1] & 0x10))
476                         tool = 0;
477         }
478
479         /* If tool changed, notify input subsystem */
480         if (wdata->tool != tool) {
481                 if (wdata->tool) {
482                         /* Completely reset old tool state */
483                         if (wdata->tool == BTN_TOOL_MOUSE) {
484                                 input_report_key(input, BTN_LEFT, 0);
485                                 input_report_key(input, BTN_RIGHT, 0);
486                                 input_report_key(input, BTN_MIDDLE, 0);
487                                 input_report_abs(input, ABS_DISTANCE,
488                                         input_abs_get_max(input, ABS_DISTANCE));
489                         } else {
490                                 input_report_key(input, BTN_TOUCH, 0);
491                                 input_report_key(input, BTN_STYLUS, 0);
492                                 input_report_key(input, BTN_STYLUS2, 0);
493                                 input_report_abs(input, ABS_PRESSURE, 0);
494                         }
495                         input_report_key(input, wdata->tool, 0);
496                         input_sync(input);
497                 }
498                 wdata->tool = tool;
499                 if (tool)
500                         input_report_key(input, tool, 1);
501         }
502
503         if (tool) {
504                 input_report_abs(input, ABS_X, x);
505                 input_report_abs(input, ABS_Y, y);
506
507                 switch ((data[1] >> 5) & 3) {
508                 case 2: /* Mouse with wheel */
509                         input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
510                         rw = (data[6] & 0x01) ? -1 :
511                                 (data[6] & 0x02) ? 1 : 0;
512                         input_report_rel(input, REL_WHEEL, rw);
513                         /* fall through */
514
515                 case 3: /* Mouse without wheel */
516                         input_report_key(input, BTN_LEFT, data[1] & 0x01);
517                         input_report_key(input, BTN_RIGHT, data[1] & 0x02);
518                         /* Compute distance between mouse and tablet */
519                         rw = 44 - (data[6] >> 2);
520                         if (rw < 0)
521                                 rw = 0;
522                         else if (rw > 31)
523                                 rw = 31;
524                         input_report_abs(input, ABS_DISTANCE, rw);
525                         break;
526
527                 default:
528                         input_report_abs(input, ABS_PRESSURE,
529                                         data[6] | (((__u16) (data[1] & 0x08)) << 5));
530                         input_report_key(input, BTN_TOUCH, data[1] & 0x01);
531                         input_report_key(input, BTN_STYLUS, data[1] & 0x02);
532                         input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
533                         break;
534                 }
535
536                 input_sync(input);
537         }
538
539         /* Report the state of the two buttons at the top of the tablet
540          * as two extra fingerpad keys (buttons 4 & 5). */
541         rw = data[7] & 0x03;
542         if (rw != wdata->butstate) {
543                 wdata->butstate = rw;
544                 input_report_key(input, BTN_0, rw & 0x02);
545                 input_report_key(input, BTN_1, rw & 0x01);
546                 input_report_key(input, BTN_TOOL_FINGER, 0xf0);
547                 input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
548                 input_sync(input);
549         }
550
551         /* Store current battery capacity and power supply state*/
552         rw = (data[7] >> 2 & 0x07);
553         if (rw != wdata->power_raw) {
554                 wdata->power_raw = rw;
555                 wdata->battery_capacity = batcap_gr[rw];
556                 if (rw == 7)
557                         wdata->ps_connected = 1;
558                 else
559                         wdata->ps_connected = 0;
560         }
561         return 1;
562 }
563
564 static void wacom_i4_parse_button_report(struct wacom_data *wdata,
565                         struct input_dev *input, unsigned char *data)
566 {
567         __u16 new_butstate;
568         __u8 new_whlstate;
569         __u8 sync = 0;
570
571         new_whlstate = data[1];
572         if (new_whlstate != wdata->whlstate) {
573                 wdata->whlstate = new_whlstate;
574                 if (new_whlstate & 0x80) {
575                         input_report_key(input, BTN_TOUCH, 1);
576                         input_report_abs(input, ABS_WHEEL, (new_whlstate & 0x7f));
577                         input_report_key(input, BTN_TOOL_FINGER, 1);
578                 } else {
579                         input_report_key(input, BTN_TOUCH, 0);
580                         input_report_abs(input, ABS_WHEEL, 0);
581                         input_report_key(input, BTN_TOOL_FINGER, 0);
582                 }
583                 sync = 1;
584         }
585
586         new_butstate = (data[3] << 1) | (data[2] & 0x01);
587         if (new_butstate != wdata->butstate) {
588                 wdata->butstate = new_butstate;
589                 input_report_key(input, BTN_0, new_butstate & 0x001);
590                 input_report_key(input, BTN_1, new_butstate & 0x002);
591                 input_report_key(input, BTN_2, new_butstate & 0x004);
592                 input_report_key(input, BTN_3, new_butstate & 0x008);
593                 input_report_key(input, BTN_4, new_butstate & 0x010);
594                 input_report_key(input, BTN_5, new_butstate & 0x020);
595                 input_report_key(input, BTN_6, new_butstate & 0x040);
596                 input_report_key(input, BTN_7, new_butstate & 0x080);
597                 input_report_key(input, BTN_8, new_butstate & 0x100);
598                 input_report_key(input, BTN_TOOL_FINGER, 1);
599                 sync = 1;
600         }
601
602         if (sync) {
603                 input_report_abs(input, ABS_MISC, PAD_DEVICE_ID);
604                 input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff);
605                 input_sync(input);
606         }
607 }
608
609 static void wacom_i4_parse_pen_report(struct wacom_data *wdata,
610                         struct input_dev *input, unsigned char *data)
611 {
612         __u16 x, y, pressure;
613         __u8 distance;
614         __u8 tilt_x, tilt_y;
615
616         switch (data[1]) {
617         case 0x80: /* Out of proximity report */
618                 input_report_key(input, BTN_TOUCH, 0);
619                 input_report_abs(input, ABS_PRESSURE, 0);
620                 input_report_key(input, BTN_STYLUS, 0);
621                 input_report_key(input, BTN_STYLUS2, 0);
622                 input_report_key(input, wdata->tool, 0);
623                 input_report_abs(input, ABS_MISC, 0);
624                 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
625                 wdata->tool = 0;
626                 input_sync(input);
627                 break;
628         case 0xC2: /* Tool report */
629                 wdata->id = ((data[2] << 4) | (data[3] >> 4) |
630                         ((data[7] & 0x0f) << 20) |
631                         ((data[8] & 0xf0) << 12));
632                 wdata->serial = ((data[3] & 0x0f) << 28) +
633                                 (data[4] << 20) + (data[5] << 12) +
634                                 (data[6] << 4) + (data[7] >> 4);
635
636                 switch (wdata->id) {
637                 case 0x100802:
638                         wdata->tool = BTN_TOOL_PEN;
639                         break;
640                 case 0x10080A:
641                         wdata->tool = BTN_TOOL_RUBBER;
642                         break;
643                 }
644                 break;
645         default: /* Position/pressure report */
646                 x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1);
647                 y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01);
648                 pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5)
649                         | (data[1] & 0x01);
650                 distance = (data[9] >> 2) & 0x3f;
651                 tilt_x = ((data[7] << 1) & 0x7e) | (data[8] >> 7);
652                 tilt_y = data[8] & 0x7f;
653
654                 input_report_key(input, BTN_TOUCH, pressure > 1);
655
656                 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
657                 input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
658                 input_report_key(input, wdata->tool, 1);
659                 input_report_abs(input, ABS_X, x);
660                 input_report_abs(input, ABS_Y, y);
661                 input_report_abs(input, ABS_PRESSURE, pressure);
662                 input_report_abs(input, ABS_DISTANCE, distance);
663                 input_report_abs(input, ABS_TILT_X, tilt_x);
664                 input_report_abs(input, ABS_TILT_Y, tilt_y);
665                 input_report_abs(input, ABS_MISC, wdata->id);
666                 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
667                 input_report_key(input, wdata->tool, 1);
668                 input_sync(input);
669                 break;
670         }
671
672         return;
673 }
674
675 static void wacom_i4_parse_report(struct hid_device *hdev,
676                         struct wacom_data *wdata,
677                         struct input_dev *input, unsigned char *data)
678 {
679         switch (data[0]) {
680         case 0x00: /* Empty report */
681                 break;
682         case 0x02: /* Pen report */
683                 wacom_i4_parse_pen_report(wdata, input, data);
684                 break;
685         case 0x03: /* Features Report */
686                 wdata->features = data[2];
687                 break;
688         case 0x0C: /* Button report */
689                 wacom_i4_parse_button_report(wdata, input, data);
690                 break;
691         default:
692                 hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]);
693                 break;
694         }
695 }
696
697 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
698                 u8 *raw_data, int size)
699 {
700         struct wacom_data *wdata = hid_get_drvdata(hdev);
701         struct hid_input *hidinput;
702         struct input_dev *input;
703         unsigned char *data = (unsigned char *) raw_data;
704         int i;
705         __u8 power_raw;
706
707         if (!(hdev->claimed & HID_CLAIMED_INPUT))
708                 return 0;
709
710         hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
711         input = hidinput->input;
712
713         switch (hdev->product) {
714         case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
715                 if (data[0] == 0x03) {
716                         return wacom_gr_parse_report(hdev, wdata, input, data);
717                 } else {
718                         hid_err(hdev, "Unknown report: %d,%d size:%d\n",
719                                         data[0], data[1], size);
720                         return 0;
721                 }
722                 break;
723         case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
724                 i = 1;
725
726                 switch (data[0]) {
727                 case 0x04:
728                         wacom_i4_parse_report(hdev, wdata, input, data + i);
729                         i += 10;
730                         /* fall through */
731                 case 0x03:
732                         wacom_i4_parse_report(hdev, wdata, input, data + i);
733                         i += 10;
734                         wacom_i4_parse_report(hdev, wdata, input, data + i);
735                         power_raw = data[i+10];
736                         if (power_raw != wdata->power_raw) {
737                                 wdata->power_raw = power_raw;
738                                 wdata->battery_capacity = batcap_i4[power_raw & 0x07];
739                                 wdata->bat_charging = (power_raw & 0x08) ? 1 : 0;
740                                 wdata->ps_connected = (power_raw & 0x10) ? 1 : 0;
741                         }
742
743                         break;
744                 default:
745                         hid_err(hdev, "Unknown report: %d,%d size:%d\n",
746                                         data[0], data[1], size);
747                         return 0;
748                 }
749         }
750         return 1;
751 }
752
753 static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
754         struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
755                                                                 int *max)
756 {
757         struct input_dev *input = hi->input;
758
759         __set_bit(INPUT_PROP_POINTER, input->propbit);
760
761         /* Basics */
762         input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
763
764         __set_bit(REL_WHEEL, input->relbit);
765
766         __set_bit(BTN_TOOL_PEN, input->keybit);
767         __set_bit(BTN_TOUCH, input->keybit);
768         __set_bit(BTN_STYLUS, input->keybit);
769         __set_bit(BTN_STYLUS2, input->keybit);
770         __set_bit(BTN_LEFT, input->keybit);
771         __set_bit(BTN_RIGHT, input->keybit);
772         __set_bit(BTN_MIDDLE, input->keybit);
773
774         /* Pad */
775         input_set_capability(input, EV_MSC, MSC_SERIAL);
776
777         __set_bit(BTN_0, input->keybit);
778         __set_bit(BTN_1, input->keybit);
779         __set_bit(BTN_TOOL_FINGER, input->keybit);
780
781         /* Distance, rubber and mouse */
782         __set_bit(BTN_TOOL_RUBBER, input->keybit);
783         __set_bit(BTN_TOOL_MOUSE, input->keybit);
784
785         switch (hdev->product) {
786         case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
787                 input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
788                 input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
789                 input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
790                 input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
791                 break;
792         case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
793                 __set_bit(ABS_WHEEL, input->absbit);
794                 __set_bit(ABS_MISC, input->absbit);
795                 __set_bit(BTN_2, input->keybit);
796                 __set_bit(BTN_3, input->keybit);
797                 __set_bit(BTN_4, input->keybit);
798                 __set_bit(BTN_5, input->keybit);
799                 __set_bit(BTN_6, input->keybit);
800                 __set_bit(BTN_7, input->keybit);
801                 __set_bit(BTN_8, input->keybit);
802                 input_set_abs_params(input, ABS_WHEEL, 0, 71, 0, 0);
803                 input_set_abs_params(input, ABS_X, 0, 40640, 4, 0);
804                 input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0);
805                 input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0);
806                 input_set_abs_params(input, ABS_DISTANCE, 0, 63, 0, 0);
807                 input_set_abs_params(input, ABS_TILT_X, 0, 127, 0, 0);
808                 input_set_abs_params(input, ABS_TILT_Y, 0, 127, 0, 0);
809                 break;
810         }
811
812         return 0;
813 }
814
815 static int wacom_probe(struct hid_device *hdev,
816                 const struct hid_device_id *id)
817 {
818         struct wacom_data *wdata;
819         int ret;
820
821         wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
822         if (wdata == NULL) {
823                 hid_err(hdev, "can't alloc wacom descriptor\n");
824                 return -ENOMEM;
825         }
826
827         hid_set_drvdata(hdev, wdata);
828
829         /* Parse the HID report now */
830         ret = hid_parse(hdev);
831         if (ret) {
832                 hid_err(hdev, "parse failed\n");
833                 goto err_free;
834         }
835
836         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
837         if (ret) {
838                 hid_err(hdev, "hw start failed\n");
839                 goto err_free;
840         }
841
842         ret = device_create_file(&hdev->dev, &dev_attr_speed);
843         if (ret)
844                 hid_warn(hdev,
845                          "can't create sysfs speed attribute err: %d\n", ret);
846
847 #define OLED_INIT(OLED_ID)                                              \
848         do {                                                            \
849                 ret = device_create_file(&hdev->dev,                    \
850                                 &dev_attr_oled##OLED_ID##_img);         \
851                 if (ret)                                                \
852                         hid_warn(hdev,                                  \
853                          "can't create sysfs oled attribute, err: %d\n", ret);\
854         } while (0)
855
856 OLED_INIT(0);
857 OLED_INIT(1);
858 OLED_INIT(2);
859 OLED_INIT(3);
860 OLED_INIT(4);
861 OLED_INIT(5);
862 OLED_INIT(6);
863 OLED_INIT(7);
864
865         wdata->features = 0;
866         wacom_set_features(hdev, 1);
867
868         if (hdev->product == USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) {
869                 sprintf(hdev->name, "%s", "Wacom Intuos4 WL");
870                 ret = wacom_initialize_leds(hdev);
871                 if (ret)
872                         hid_warn(hdev,
873                                  "can't create led attribute, err: %d\n", ret);
874         }
875
876         wdata->battery.properties = wacom_battery_props;
877         wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
878         wdata->battery.get_property = wacom_battery_get_property;
879         wdata->battery.name = "wacom_battery";
880         wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
881         wdata->battery.use_for_apm = 0;
882
883
884         ret = power_supply_register(&hdev->dev, &wdata->battery);
885         if (ret) {
886                 hid_err(hdev, "can't create sysfs battery attribute, err: %d\n",
887                         ret);
888                 goto err_battery;
889         }
890
891         power_supply_powers(&wdata->battery, &hdev->dev);
892
893         wdata->ac.properties = wacom_ac_props;
894         wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
895         wdata->ac.get_property = wacom_ac_get_property;
896         wdata->ac.name = "wacom_ac";
897         wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
898         wdata->ac.use_for_apm = 0;
899
900         ret = power_supply_register(&hdev->dev, &wdata->ac);
901         if (ret) {
902                 hid_err(hdev,
903                         "can't create ac battery attribute, err: %d\n", ret);
904                 goto err_ac;
905         }
906
907         power_supply_powers(&wdata->ac, &hdev->dev);
908         return 0;
909
910 err_ac:
911         power_supply_unregister(&wdata->battery);
912 err_battery:
913         wacom_destroy_leds(hdev);
914         device_remove_file(&hdev->dev, &dev_attr_oled0_img);
915         device_remove_file(&hdev->dev, &dev_attr_oled1_img);
916         device_remove_file(&hdev->dev, &dev_attr_oled2_img);
917         device_remove_file(&hdev->dev, &dev_attr_oled3_img);
918         device_remove_file(&hdev->dev, &dev_attr_oled4_img);
919         device_remove_file(&hdev->dev, &dev_attr_oled5_img);
920         device_remove_file(&hdev->dev, &dev_attr_oled6_img);
921         device_remove_file(&hdev->dev, &dev_attr_oled7_img);
922         device_remove_file(&hdev->dev, &dev_attr_speed);
923         hid_hw_stop(hdev);
924 err_free:
925         kfree(wdata);
926         return ret;
927 }
928
929 static void wacom_remove(struct hid_device *hdev)
930 {
931         struct wacom_data *wdata = hid_get_drvdata(hdev);
932
933         wacom_destroy_leds(hdev);
934         device_remove_file(&hdev->dev, &dev_attr_oled0_img);
935         device_remove_file(&hdev->dev, &dev_attr_oled1_img);
936         device_remove_file(&hdev->dev, &dev_attr_oled2_img);
937         device_remove_file(&hdev->dev, &dev_attr_oled3_img);
938         device_remove_file(&hdev->dev, &dev_attr_oled4_img);
939         device_remove_file(&hdev->dev, &dev_attr_oled5_img);
940         device_remove_file(&hdev->dev, &dev_attr_oled6_img);
941         device_remove_file(&hdev->dev, &dev_attr_oled7_img);
942         device_remove_file(&hdev->dev, &dev_attr_speed);
943         hid_hw_stop(hdev);
944
945         power_supply_unregister(&wdata->battery);
946         power_supply_unregister(&wdata->ac);
947         kfree(hid_get_drvdata(hdev));
948 }
949
950 static const struct hid_device_id wacom_devices[] = {
951         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
952         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
953
954         { }
955 };
956 MODULE_DEVICE_TABLE(hid, wacom_devices);
957
958 static struct hid_driver wacom_driver = {
959         .name = "wacom",
960         .id_table = wacom_devices,
961         .probe = wacom_probe,
962         .remove = wacom_remove,
963         .raw_event = wacom_raw_event,
964         .input_mapped = wacom_input_mapped,
965 };
966 module_hid_driver(wacom_driver);
967
968 MODULE_DESCRIPTION("Driver for Wacom Graphire Bluetooth and Wacom Intuos4 WL");
969 MODULE_LICENSE("GPL");