Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 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) 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 Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 12 | * Copyright (c) 2011 Przemysław Firszt <przemo@firszt.eu> |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 13 | */ |
| 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 Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 23 | |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 24 | #include <linux/device.h> |
| 25 | #include <linux/hid.h> |
| 26 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 28 | #ifdef CONFIG_HID_WACOM_POWER_SUPPLY |
| 29 | #include <linux/power_supply.h> |
| 30 | #endif |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 31 | |
| 32 | #include "hid-ids.h" |
| 33 | |
Przemo Firszt | 6245bde | 2012-02-28 17:19:05 +0000 | [diff] [blame] | 34 | #define PAD_DEVICE_ID 0x0F |
| 35 | |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 36 | struct wacom_data { |
| 37 | __u16 tool; |
Przemo Firszt | 6245bde | 2012-02-28 17:19:05 +0000 | [diff] [blame] | 38 | __u16 butstate; |
Przemo Firszt | 7e503a3 | 2012-03-14 19:55:04 +0000 | [diff] [blame] | 39 | __u8 whlstate; |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 40 | __u8 features; |
Przemo Firszt | 2470900 | 2012-02-24 13:52:32 +0000 | [diff] [blame] | 41 | __u32 id; |
| 42 | __u32 serial; |
Przemo Firszt | 20a3ce7e | 2010-03-18 14:34:34 +0000 | [diff] [blame] | 43 | unsigned char high_speed; |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 44 | #ifdef CONFIG_HID_WACOM_POWER_SUPPLY |
| 45 | int battery_capacity; |
| 46 | struct power_supply battery; |
| 47 | struct power_supply ac; |
| 48 | #endif |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 51 | #ifdef CONFIG_HID_WACOM_POWER_SUPPLY |
| 52 | /*percent of battery capacity, 0 means AC online*/ |
| 53 | static unsigned short batcap[8] = { 1, 15, 25, 35, 50, 70, 100, 0 }; |
| 54 | |
| 55 | static enum power_supply_property wacom_battery_props[] = { |
| 56 | POWER_SUPPLY_PROP_PRESENT, |
Jeremy Fitzhardinge | 73db881 | 2011-12-07 11:29:46 -0800 | [diff] [blame] | 57 | POWER_SUPPLY_PROP_CAPACITY, |
| 58 | POWER_SUPPLY_PROP_SCOPE, |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static enum power_supply_property wacom_ac_props[] = { |
| 62 | POWER_SUPPLY_PROP_PRESENT, |
Jeremy Fitzhardinge | 73db881 | 2011-12-07 11:29:46 -0800 | [diff] [blame] | 63 | POWER_SUPPLY_PROP_ONLINE, |
| 64 | POWER_SUPPLY_PROP_SCOPE, |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static 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); |
| 73 | int power_state = batcap[wdata->battery_capacity]; |
| 74 | int ret = 0; |
| 75 | |
| 76 | switch (psp) { |
| 77 | case POWER_SUPPLY_PROP_PRESENT: |
| 78 | val->intval = 1; |
| 79 | break; |
Jeremy Fitzhardinge | 73db881 | 2011-12-07 11:29:46 -0800 | [diff] [blame] | 80 | case POWER_SUPPLY_PROP_SCOPE: |
| 81 | val->intval = POWER_SUPPLY_SCOPE_DEVICE; |
| 82 | break; |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 83 | case POWER_SUPPLY_PROP_CAPACITY: |
| 84 | /* show 100% battery capacity when charging */ |
| 85 | if (power_state == 0) |
| 86 | val->intval = 100; |
| 87 | else |
| 88 | val->intval = power_state; |
| 89 | break; |
| 90 | default: |
| 91 | ret = -EINVAL; |
| 92 | break; |
| 93 | } |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | static int wacom_ac_get_property(struct power_supply *psy, |
| 98 | enum power_supply_property psp, |
| 99 | union power_supply_propval *val) |
| 100 | { |
| 101 | struct wacom_data *wdata = container_of(psy, struct wacom_data, ac); |
| 102 | int power_state = batcap[wdata->battery_capacity]; |
| 103 | int ret = 0; |
| 104 | |
| 105 | switch (psp) { |
| 106 | case POWER_SUPPLY_PROP_PRESENT: |
| 107 | /* fall through */ |
| 108 | case POWER_SUPPLY_PROP_ONLINE: |
| 109 | if (power_state == 0) |
| 110 | val->intval = 1; |
| 111 | else |
| 112 | val->intval = 0; |
| 113 | break; |
Jeremy Fitzhardinge | 73db881 | 2011-12-07 11:29:46 -0800 | [diff] [blame] | 114 | case POWER_SUPPLY_PROP_SCOPE: |
| 115 | val->intval = POWER_SUPPLY_SCOPE_DEVICE; |
| 116 | break; |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 117 | default: |
| 118 | ret = -EINVAL; |
| 119 | break; |
| 120 | } |
| 121 | return ret; |
| 122 | } |
| 123 | #endif |
| 124 | |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 125 | static void wacom_set_features(struct hid_device *hdev) |
| 126 | { |
| 127 | int ret; |
| 128 | __u8 rep_data[2]; |
| 129 | |
| 130 | /*set high speed, tablet mode*/ |
| 131 | rep_data[0] = 0x03; |
| 132 | rep_data[1] = 0x20; |
| 133 | ret = hdev->hid_output_raw_report(hdev, rep_data, 2, |
| 134 | HID_FEATURE_REPORT); |
| 135 | return; |
| 136 | } |
| 137 | |
Przemo Firszt | 06c7c31 | 2010-03-22 09:55:04 +0100 | [diff] [blame] | 138 | static void wacom_poke(struct hid_device *hdev, u8 speed) |
| 139 | { |
Przemo Firszt | 20a3ce7e | 2010-03-18 14:34:34 +0000 | [diff] [blame] | 140 | struct wacom_data *wdata = hid_get_drvdata(hdev); |
Przemo Firszt | 06c7c31 | 2010-03-22 09:55:04 +0100 | [diff] [blame] | 141 | int limit, ret; |
| 142 | char rep_data[2]; |
| 143 | |
| 144 | rep_data[0] = 0x03 ; rep_data[1] = 0x00; |
| 145 | limit = 3; |
| 146 | do { |
| 147 | ret = hdev->hid_output_raw_report(hdev, rep_data, 2, |
| 148 | HID_FEATURE_REPORT); |
| 149 | } while (ret < 0 && limit-- > 0); |
| 150 | |
| 151 | if (ret >= 0) { |
| 152 | if (speed == 0) |
| 153 | rep_data[0] = 0x05; |
| 154 | else |
| 155 | rep_data[0] = 0x06; |
| 156 | |
| 157 | rep_data[1] = 0x00; |
| 158 | limit = 3; |
| 159 | do { |
| 160 | ret = hdev->hid_output_raw_report(hdev, rep_data, 2, |
| 161 | HID_FEATURE_REPORT); |
| 162 | } while (ret < 0 && limit-- > 0); |
| 163 | |
Przemo Firszt | 20a3ce7e | 2010-03-18 14:34:34 +0000 | [diff] [blame] | 164 | if (ret >= 0) { |
| 165 | wdata->high_speed = speed; |
Przemo Firszt | 06c7c31 | 2010-03-22 09:55:04 +0100 | [diff] [blame] | 166 | return; |
Przemo Firszt | 20a3ce7e | 2010-03-18 14:34:34 +0000 | [diff] [blame] | 167 | } |
Przemo Firszt | 06c7c31 | 2010-03-22 09:55:04 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Note that if the raw queries fail, it's not a hard failure and it |
| 172 | * is safe to continue |
| 173 | */ |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 174 | hid_warn(hdev, "failed to poke device, command %d, err %d\n", |
| 175 | rep_data[0], ret); |
Przemo Firszt | 06c7c31 | 2010-03-22 09:55:04 +0100 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | |
Przemo Firszt | 20a3ce7e | 2010-03-18 14:34:34 +0000 | [diff] [blame] | 179 | static ssize_t wacom_show_speed(struct device *dev, |
| 180 | struct device_attribute |
| 181 | *attr, char *buf) |
| 182 | { |
| 183 | struct wacom_data *wdata = dev_get_drvdata(dev); |
| 184 | |
| 185 | return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed); |
| 186 | } |
| 187 | |
| 188 | static ssize_t wacom_store_speed(struct device *dev, |
| 189 | struct device_attribute *attr, |
| 190 | const char *buf, size_t count) |
| 191 | { |
| 192 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
| 193 | int new_speed; |
| 194 | |
| 195 | if (sscanf(buf, "%1d", &new_speed ) != 1) |
| 196 | return -EINVAL; |
| 197 | |
| 198 | if (new_speed == 0 || new_speed == 1) { |
| 199 | wacom_poke(hdev, new_speed); |
| 200 | return strnlen(buf, PAGE_SIZE); |
| 201 | } else |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | |
Jiri Kosina | edd2126 | 2010-11-18 16:28:43 +0100 | [diff] [blame] | 205 | static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP, |
Przemo Firszt | 20a3ce7e | 2010-03-18 14:34:34 +0000 | [diff] [blame] | 206 | wacom_show_speed, wacom_store_speed); |
| 207 | |
Przemo Firszt | f6b7efc | 2011-11-05 11:28:21 +0000 | [diff] [blame] | 208 | static int wacom_gr_parse_report(struct hid_device *hdev, |
| 209 | struct wacom_data *wdata, |
| 210 | struct input_dev *input, unsigned char *data) |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 211 | { |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 212 | int tool, x, y, rw; |
| 213 | |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 214 | tool = 0; |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 215 | /* Get X & Y positions */ |
| 216 | x = le16_to_cpu(*(__le16 *) &data[2]); |
| 217 | y = le16_to_cpu(*(__le16 *) &data[4]); |
| 218 | |
| 219 | /* Get current tool identifier */ |
| 220 | if (data[1] & 0x90) { /* If pen is in the in/active area */ |
| 221 | switch ((data[1] >> 5) & 3) { |
| 222 | case 0: /* Pen */ |
| 223 | tool = BTN_TOOL_PEN; |
| 224 | break; |
| 225 | |
| 226 | case 1: /* Rubber */ |
| 227 | tool = BTN_TOOL_RUBBER; |
| 228 | break; |
| 229 | |
| 230 | case 2: /* Mouse with wheel */ |
| 231 | case 3: /* Mouse without wheel */ |
| 232 | tool = BTN_TOOL_MOUSE; |
| 233 | break; |
| 234 | } |
| 235 | |
| 236 | /* Reset tool if out of active tablet area */ |
| 237 | if (!(data[1] & 0x10)) |
| 238 | tool = 0; |
| 239 | } |
| 240 | |
| 241 | /* If tool changed, notify input subsystem */ |
| 242 | if (wdata->tool != tool) { |
| 243 | if (wdata->tool) { |
| 244 | /* Completely reset old tool state */ |
| 245 | if (wdata->tool == BTN_TOOL_MOUSE) { |
| 246 | input_report_key(input, BTN_LEFT, 0); |
| 247 | input_report_key(input, BTN_RIGHT, 0); |
| 248 | input_report_key(input, BTN_MIDDLE, 0); |
| 249 | input_report_abs(input, ABS_DISTANCE, |
Daniel Mack | 987a6c0 | 2010-08-02 20:15:17 -0700 | [diff] [blame] | 250 | input_abs_get_max(input, ABS_DISTANCE)); |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 251 | } else { |
| 252 | input_report_key(input, BTN_TOUCH, 0); |
| 253 | input_report_key(input, BTN_STYLUS, 0); |
| 254 | input_report_key(input, BTN_STYLUS2, 0); |
| 255 | input_report_abs(input, ABS_PRESSURE, 0); |
| 256 | } |
| 257 | input_report_key(input, wdata->tool, 0); |
| 258 | input_sync(input); |
| 259 | } |
| 260 | wdata->tool = tool; |
| 261 | if (tool) |
| 262 | input_report_key(input, tool, 1); |
| 263 | } |
| 264 | |
| 265 | if (tool) { |
| 266 | input_report_abs(input, ABS_X, x); |
| 267 | input_report_abs(input, ABS_Y, y); |
| 268 | |
| 269 | switch ((data[1] >> 5) & 3) { |
| 270 | case 2: /* Mouse with wheel */ |
| 271 | input_report_key(input, BTN_MIDDLE, data[1] & 0x04); |
| 272 | rw = (data[6] & 0x01) ? -1 : |
| 273 | (data[6] & 0x02) ? 1 : 0; |
| 274 | input_report_rel(input, REL_WHEEL, rw); |
| 275 | /* fall through */ |
| 276 | |
| 277 | case 3: /* Mouse without wheel */ |
| 278 | input_report_key(input, BTN_LEFT, data[1] & 0x01); |
| 279 | input_report_key(input, BTN_RIGHT, data[1] & 0x02); |
| 280 | /* Compute distance between mouse and tablet */ |
| 281 | rw = 44 - (data[6] >> 2); |
| 282 | if (rw < 0) |
| 283 | rw = 0; |
| 284 | else if (rw > 31) |
| 285 | rw = 31; |
| 286 | input_report_abs(input, ABS_DISTANCE, rw); |
| 287 | break; |
| 288 | |
| 289 | default: |
| 290 | input_report_abs(input, ABS_PRESSURE, |
| 291 | data[6] | (((__u16) (data[1] & 0x08)) << 5)); |
| 292 | input_report_key(input, BTN_TOUCH, data[1] & 0x01); |
| 293 | input_report_key(input, BTN_STYLUS, data[1] & 0x02); |
| 294 | input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04); |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | input_sync(input); |
| 299 | } |
| 300 | |
| 301 | /* Report the state of the two buttons at the top of the tablet |
| 302 | * as two extra fingerpad keys (buttons 4 & 5). */ |
| 303 | rw = data[7] & 0x03; |
| 304 | if (rw != wdata->butstate) { |
| 305 | wdata->butstate = rw; |
| 306 | input_report_key(input, BTN_0, rw & 0x02); |
| 307 | input_report_key(input, BTN_1, rw & 0x01); |
Przemo Firszt | 0e253fd | 2010-01-09 15:20:03 +0100 | [diff] [blame] | 308 | input_report_key(input, BTN_TOOL_FINGER, 0xf0); |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 309 | input_event(input, EV_MSC, MSC_SERIAL, 0xf0); |
| 310 | input_sync(input); |
| 311 | } |
| 312 | |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 313 | #ifdef CONFIG_HID_WACOM_POWER_SUPPLY |
| 314 | /* Store current battery capacity */ |
| 315 | rw = (data[7] >> 2 & 0x07); |
| 316 | if (rw != wdata->battery_capacity) |
| 317 | wdata->battery_capacity = rw; |
| 318 | #endif |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 319 | return 1; |
| 320 | } |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 321 | |
Przemo Firszt | 6245bde | 2012-02-28 17:19:05 +0000 | [diff] [blame] | 322 | static void wacom_i4_parse_button_report(struct wacom_data *wdata, |
| 323 | struct input_dev *input, unsigned char *data) |
| 324 | { |
| 325 | __u16 new_butstate; |
Przemo Firszt | 7e503a3 | 2012-03-14 19:55:04 +0000 | [diff] [blame] | 326 | __u8 new_whlstate; |
| 327 | __u8 sync = 0; |
| 328 | |
| 329 | new_whlstate = data[1]; |
| 330 | if (new_whlstate != wdata->whlstate) { |
| 331 | wdata->whlstate = new_whlstate; |
| 332 | if (new_whlstate & 0x80) { |
| 333 | input_report_key(input, BTN_TOUCH, 1); |
| 334 | input_report_abs(input, ABS_WHEEL, (new_whlstate & 0x7f)); |
| 335 | input_report_key(input, BTN_TOOL_FINGER, 1); |
| 336 | } else { |
| 337 | input_report_key(input, BTN_TOUCH, 0); |
| 338 | input_report_abs(input, ABS_WHEEL, 0); |
| 339 | input_report_key(input, BTN_TOOL_FINGER, 0); |
| 340 | } |
| 341 | sync = 1; |
| 342 | } |
Przemo Firszt | 6245bde | 2012-02-28 17:19:05 +0000 | [diff] [blame] | 343 | |
| 344 | new_butstate = (data[3] << 1) | (data[2] & 0x01); |
| 345 | if (new_butstate != wdata->butstate) { |
| 346 | wdata->butstate = new_butstate; |
| 347 | input_report_key(input, BTN_0, new_butstate & 0x001); |
| 348 | input_report_key(input, BTN_1, new_butstate & 0x002); |
| 349 | input_report_key(input, BTN_2, new_butstate & 0x004); |
| 350 | input_report_key(input, BTN_3, new_butstate & 0x008); |
| 351 | input_report_key(input, BTN_4, new_butstate & 0x010); |
| 352 | input_report_key(input, BTN_5, new_butstate & 0x020); |
| 353 | input_report_key(input, BTN_6, new_butstate & 0x040); |
| 354 | input_report_key(input, BTN_7, new_butstate & 0x080); |
| 355 | input_report_key(input, BTN_8, new_butstate & 0x100); |
| 356 | input_report_key(input, BTN_TOOL_FINGER, 1); |
Przemo Firszt | 7e503a3 | 2012-03-14 19:55:04 +0000 | [diff] [blame] | 357 | sync = 1; |
| 358 | } |
| 359 | |
| 360 | if (sync) { |
Przemo Firszt | 6245bde | 2012-02-28 17:19:05 +0000 | [diff] [blame] | 361 | input_report_abs(input, ABS_MISC, PAD_DEVICE_ID); |
| 362 | input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff); |
| 363 | input_sync(input); |
| 364 | } |
| 365 | } |
| 366 | |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 367 | static void wacom_i4_parse_pen_report(struct wacom_data *wdata, |
| 368 | struct input_dev *input, unsigned char *data) |
| 369 | { |
| 370 | __u16 x, y, pressure; |
Przemo Firszt | e0829e9 | 2012-02-28 17:19:04 +0000 | [diff] [blame] | 371 | __u8 distance; |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 372 | |
| 373 | switch (data[1]) { |
| 374 | case 0x80: /* Out of proximity report */ |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 375 | input_report_key(input, BTN_TOUCH, 0); |
| 376 | input_report_abs(input, ABS_PRESSURE, 0); |
Przemo Firszt | 693f45b | 2012-03-09 13:20:51 +0000 | [diff] [blame] | 377 | input_report_key(input, BTN_STYLUS, 0); |
| 378 | input_report_key(input, BTN_STYLUS2, 0); |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 379 | input_report_key(input, wdata->tool, 0); |
Przemo Firszt | 2470900 | 2012-02-24 13:52:32 +0000 | [diff] [blame] | 380 | input_report_abs(input, ABS_MISC, 0); |
| 381 | input_event(input, EV_MSC, MSC_SERIAL, wdata->serial); |
Przemo Firszt | 32db737 | 2012-02-19 20:20:29 +0000 | [diff] [blame] | 382 | wdata->tool = 0; |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 383 | input_sync(input); |
| 384 | break; |
| 385 | case 0xC2: /* Tool report */ |
Przemo Firszt | 2470900 | 2012-02-24 13:52:32 +0000 | [diff] [blame] | 386 | wdata->id = ((data[2] << 4) | (data[3] >> 4) | |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 387 | ((data[7] & 0x0f) << 20) | |
Przemo Firszt | 2470900 | 2012-02-24 13:52:32 +0000 | [diff] [blame] | 388 | ((data[8] & 0xf0) << 12)); |
| 389 | wdata->serial = ((data[3] & 0x0f) << 28) + |
| 390 | (data[4] << 20) + (data[5] << 12) + |
| 391 | (data[6] << 4) + (data[7] >> 4); |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 392 | |
Przemo Firszt | 2470900 | 2012-02-24 13:52:32 +0000 | [diff] [blame] | 393 | switch (wdata->id) { |
| 394 | case 0x100802: |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 395 | wdata->tool = BTN_TOOL_PEN; |
| 396 | break; |
Przemo Firszt | 2470900 | 2012-02-24 13:52:32 +0000 | [diff] [blame] | 397 | case 0x10080A: |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 398 | wdata->tool = BTN_TOOL_RUBBER; |
| 399 | break; |
| 400 | } |
| 401 | break; |
| 402 | default: /* Position/pressure report */ |
| 403 | x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1); |
| 404 | y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01); |
| 405 | pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5) |
| 406 | | (data[1] & 0x01); |
Przemo Firszt | e0829e9 | 2012-02-28 17:19:04 +0000 | [diff] [blame] | 407 | distance = (data[9] >> 2) & 0x3f; |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 408 | |
| 409 | input_report_key(input, BTN_TOUCH, pressure > 1); |
| 410 | |
| 411 | input_report_key(input, BTN_STYLUS, data[1] & 0x02); |
| 412 | input_report_key(input, BTN_STYLUS2, data[1] & 0x04); |
| 413 | input_report_key(input, wdata->tool, 1); |
| 414 | input_report_abs(input, ABS_X, x); |
| 415 | input_report_abs(input, ABS_Y, y); |
| 416 | input_report_abs(input, ABS_PRESSURE, pressure); |
Przemo Firszt | e0829e9 | 2012-02-28 17:19:04 +0000 | [diff] [blame] | 417 | input_report_abs(input, ABS_DISTANCE, distance); |
Przemo Firszt | 2470900 | 2012-02-24 13:52:32 +0000 | [diff] [blame] | 418 | input_report_abs(input, ABS_MISC, wdata->id); |
| 419 | input_event(input, EV_MSC, MSC_SERIAL, wdata->serial); |
| 420 | input_report_key(input, wdata->tool, 1); |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 421 | input_sync(input); |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | static void wacom_i4_parse_report(struct hid_device *hdev, |
| 429 | struct wacom_data *wdata, |
| 430 | struct input_dev *input, unsigned char *data) |
| 431 | { |
| 432 | switch (data[0]) { |
| 433 | case 0x00: /* Empty report */ |
| 434 | break; |
| 435 | case 0x02: /* Pen report */ |
| 436 | wacom_i4_parse_pen_report(wdata, input, data); |
| 437 | break; |
| 438 | case 0x03: /* Features Report */ |
| 439 | wdata->features = data[2]; |
| 440 | break; |
| 441 | case 0x0C: /* Button report */ |
Przemo Firszt | 6245bde | 2012-02-28 17:19:05 +0000 | [diff] [blame] | 442 | wacom_i4_parse_button_report(wdata, input, data); |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 443 | break; |
| 444 | default: |
| 445 | hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]); |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | |
Przemo Firszt | f6b7efc | 2011-11-05 11:28:21 +0000 | [diff] [blame] | 450 | static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report, |
| 451 | u8 *raw_data, int size) |
| 452 | { |
| 453 | struct wacom_data *wdata = hid_get_drvdata(hdev); |
| 454 | struct hid_input *hidinput; |
| 455 | struct input_dev *input; |
| 456 | unsigned char *data = (unsigned char *) raw_data; |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 457 | int i; |
Przemo Firszt | f6b7efc | 2011-11-05 11:28:21 +0000 | [diff] [blame] | 458 | |
| 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 Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 469 | 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); |
| 485 | break; |
| 486 | default: |
| 487 | hid_err(hdev, "Unknown report: %d,%d size:%d\n", |
| 488 | data[0], data[1], size); |
| 489 | return 0; |
| 490 | } |
| 491 | } |
| 492 | return 1; |
Przemo Firszt | f6b7efc | 2011-11-05 11:28:21 +0000 | [diff] [blame] | 493 | } |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 494 | |
David Herrmann | 3797ef6 | 2011-10-08 23:20:17 +0200 | [diff] [blame] | 495 | static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
| 496 | struct hid_field *field, struct hid_usage *usage, unsigned long **bit, |
| 497 | int *max) |
| 498 | { |
| 499 | struct input_dev *input = hi->input; |
| 500 | |
Jiri Kosina | f6f1242 | 2011-10-25 09:58:12 +0200 | [diff] [blame] | 501 | __set_bit(INPUT_PROP_POINTER, input->propbit); |
| 502 | |
David Herrmann | 3797ef6 | 2011-10-08 23:20:17 +0200 | [diff] [blame] | 503 | /* Basics */ |
| 504 | input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL); |
| 505 | |
| 506 | __set_bit(REL_WHEEL, input->relbit); |
| 507 | |
| 508 | __set_bit(BTN_TOOL_PEN, input->keybit); |
| 509 | __set_bit(BTN_TOUCH, input->keybit); |
| 510 | __set_bit(BTN_STYLUS, input->keybit); |
| 511 | __set_bit(BTN_STYLUS2, input->keybit); |
| 512 | __set_bit(BTN_LEFT, input->keybit); |
| 513 | __set_bit(BTN_RIGHT, input->keybit); |
| 514 | __set_bit(BTN_MIDDLE, input->keybit); |
| 515 | |
| 516 | /* Pad */ |
Przemo Firszt | 9a911da | 2012-03-14 19:55:03 +0000 | [diff] [blame] | 517 | input_set_capability(input, EV_MSC, MSC_SERIAL); |
David Herrmann | 3797ef6 | 2011-10-08 23:20:17 +0200 | [diff] [blame] | 518 | |
| 519 | __set_bit(BTN_0, input->keybit); |
| 520 | __set_bit(BTN_1, input->keybit); |
| 521 | __set_bit(BTN_TOOL_FINGER, input->keybit); |
| 522 | |
| 523 | /* Distance, rubber and mouse */ |
| 524 | __set_bit(BTN_TOOL_RUBBER, input->keybit); |
| 525 | __set_bit(BTN_TOOL_MOUSE, input->keybit); |
| 526 | |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 527 | switch (hdev->product) { |
| 528 | case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH: |
| 529 | input_set_abs_params(input, ABS_X, 0, 16704, 4, 0); |
| 530 | input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0); |
| 531 | input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0); |
| 532 | input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0); |
| 533 | break; |
| 534 | case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH: |
Przemo Firszt | 7e503a3 | 2012-03-14 19:55:04 +0000 | [diff] [blame] | 535 | __set_bit(ABS_WHEEL, input->absbit); |
Przemo Firszt | 2c653e6 | 2012-02-24 13:47:48 +0000 | [diff] [blame] | 536 | __set_bit(ABS_MISC, input->absbit); |
Przemo Firszt | 6245bde | 2012-02-28 17:19:05 +0000 | [diff] [blame] | 537 | __set_bit(BTN_2, input->keybit); |
| 538 | __set_bit(BTN_3, input->keybit); |
| 539 | __set_bit(BTN_4, input->keybit); |
| 540 | __set_bit(BTN_5, input->keybit); |
| 541 | __set_bit(BTN_6, input->keybit); |
| 542 | __set_bit(BTN_7, input->keybit); |
| 543 | __set_bit(BTN_8, input->keybit); |
Przemo Firszt | 7e503a3 | 2012-03-14 19:55:04 +0000 | [diff] [blame] | 544 | input_set_abs_params(input, ABS_WHEEL, 0, 71, 0, 0); |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 545 | input_set_abs_params(input, ABS_X, 0, 40640, 4, 0); |
| 546 | input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0); |
| 547 | input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0); |
Przemo Firszt | e0829e9 | 2012-02-28 17:19:04 +0000 | [diff] [blame] | 548 | input_set_abs_params(input, ABS_DISTANCE, 0, 63, 0, 0); |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 549 | break; |
| 550 | } |
David Herrmann | 3797ef6 | 2011-10-08 23:20:17 +0200 | [diff] [blame] | 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 555 | static int wacom_probe(struct hid_device *hdev, |
| 556 | const struct hid_device_id *id) |
| 557 | { |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 558 | struct wacom_data *wdata; |
| 559 | int ret; |
| 560 | |
| 561 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); |
| 562 | if (wdata == NULL) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 563 | hid_err(hdev, "can't alloc wacom descriptor\n"); |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 564 | return -ENOMEM; |
| 565 | } |
| 566 | |
| 567 | hid_set_drvdata(hdev, wdata); |
| 568 | |
Bastien Nocera | 46a709b | 2010-01-20 12:00:53 +0000 | [diff] [blame] | 569 | /* Parse the HID report now */ |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 570 | ret = hid_parse(hdev); |
| 571 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 572 | hid_err(hdev, "parse failed\n"); |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 573 | goto err_free; |
| 574 | } |
| 575 | |
| 576 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 577 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 578 | hid_err(hdev, "hw start failed\n"); |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 579 | goto err_free; |
| 580 | } |
| 581 | |
Przemo Firszt | 20a3ce7e | 2010-03-18 14:34:34 +0000 | [diff] [blame] | 582 | ret = device_create_file(&hdev->dev, &dev_attr_speed); |
| 583 | if (ret) |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 584 | hid_warn(hdev, |
| 585 | "can't create sysfs speed attribute err: %d\n", ret); |
Jiri Kosina | 342f31e | 2010-02-03 15:52:31 +0100 | [diff] [blame] | 586 | |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 587 | switch (hdev->product) { |
| 588 | case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH: |
| 589 | /* Set Wacom mode 2 with high reporting speed */ |
| 590 | wacom_poke(hdev, 1); |
| 591 | break; |
| 592 | case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH: |
Przemo Firszt | a72c5dd | 2012-02-17 17:47:10 +0000 | [diff] [blame] | 593 | sprintf(hdev->name, "%s", "Wacom Intuos4 WL"); |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 594 | wdata->features = 0; |
| 595 | wacom_set_features(hdev); |
| 596 | break; |
| 597 | } |
Bastien Nocera | 46a709b | 2010-01-20 12:00:53 +0000 | [diff] [blame] | 598 | |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 599 | #ifdef CONFIG_HID_WACOM_POWER_SUPPLY |
| 600 | wdata->battery.properties = wacom_battery_props; |
| 601 | wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props); |
| 602 | wdata->battery.get_property = wacom_battery_get_property; |
| 603 | wdata->battery.name = "wacom_battery"; |
| 604 | wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY; |
| 605 | wdata->battery.use_for_apm = 0; |
Bastien Nocera | 46a709b | 2010-01-20 12:00:53 +0000 | [diff] [blame] | 606 | |
Jeremy Fitzhardinge | 35b4c01 | 2011-12-09 09:35:00 -0800 | [diff] [blame] | 607 | |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 608 | ret = power_supply_register(&hdev->dev, &wdata->battery); |
| 609 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 610 | hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n", |
| 611 | ret); |
David Herrmann | dde58cf | 2011-09-05 18:45:28 +0200 | [diff] [blame] | 612 | goto err_battery; |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Przemo Firszt | d7cb3db | 2012-02-05 22:35:24 +0000 | [diff] [blame] | 615 | power_supply_powers(&wdata->battery, &hdev->dev); |
| 616 | |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 617 | wdata->ac.properties = wacom_ac_props; |
| 618 | wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props); |
| 619 | wdata->ac.get_property = wacom_ac_get_property; |
| 620 | wdata->ac.name = "wacom_ac"; |
| 621 | wdata->ac.type = POWER_SUPPLY_TYPE_MAINS; |
| 622 | wdata->ac.use_for_apm = 0; |
| 623 | |
| 624 | ret = power_supply_register(&hdev->dev, &wdata->ac); |
| 625 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 626 | hid_warn(hdev, |
| 627 | "can't create ac battery attribute, err: %d\n", ret); |
David Herrmann | dde58cf | 2011-09-05 18:45:28 +0200 | [diff] [blame] | 628 | goto err_ac; |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 629 | } |
Przemo Firszt | d7cb3db | 2012-02-05 22:35:24 +0000 | [diff] [blame] | 630 | |
| 631 | power_supply_powers(&wdata->ac, &hdev->dev); |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 632 | #endif |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 633 | return 0; |
Daniel Mack | 987a6c0 | 2010-08-02 20:15:17 -0700 | [diff] [blame] | 634 | |
David Herrmann | dde58cf | 2011-09-05 18:45:28 +0200 | [diff] [blame] | 635 | #ifdef CONFIG_HID_WACOM_POWER_SUPPLY |
| 636 | err_ac: |
| 637 | power_supply_unregister(&wdata->battery); |
| 638 | err_battery: |
| 639 | device_remove_file(&hdev->dev, &dev_attr_speed); |
| 640 | hid_hw_stop(hdev); |
| 641 | #endif |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 642 | err_free: |
| 643 | kfree(wdata); |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | static void wacom_remove(struct hid_device *hdev) |
| 648 | { |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 649 | #ifdef CONFIG_HID_WACOM_POWER_SUPPLY |
| 650 | struct wacom_data *wdata = hid_get_drvdata(hdev); |
| 651 | #endif |
David Herrmann | 9086617 | 2011-09-05 18:45:29 +0200 | [diff] [blame] | 652 | device_remove_file(&hdev->dev, &dev_attr_speed); |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 653 | hid_hw_stop(hdev); |
Przemo Firszt | 59d2334 | 2010-03-15 19:16:23 +0000 | [diff] [blame] | 654 | |
| 655 | #ifdef CONFIG_HID_WACOM_POWER_SUPPLY |
| 656 | power_supply_unregister(&wdata->battery); |
| 657 | power_supply_unregister(&wdata->ac); |
| 658 | #endif |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 659 | kfree(hid_get_drvdata(hdev)); |
| 660 | } |
| 661 | |
| 662 | static const struct hid_device_id wacom_devices[] = { |
| 663 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) }, |
Przemo Firszt | 78761ff | 2011-11-05 11:28:22 +0000 | [diff] [blame] | 664 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) }, |
Jiri Kosina | d5e0a06 | 2010-07-20 17:48:48 +0200 | [diff] [blame] | 665 | |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 666 | { } |
| 667 | }; |
| 668 | MODULE_DEVICE_TABLE(hid, wacom_devices); |
| 669 | |
| 670 | static struct hid_driver wacom_driver = { |
| 671 | .name = "wacom", |
| 672 | .id_table = wacom_devices, |
| 673 | .probe = wacom_probe, |
| 674 | .remove = wacom_remove, |
| 675 | .raw_event = wacom_raw_event, |
David Herrmann | 3797ef6 | 2011-10-08 23:20:17 +0200 | [diff] [blame] | 676 | .input_mapped = wacom_input_mapped, |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 677 | }; |
| 678 | |
Peter Huewe | a24f423 | 2009-07-02 19:08:38 +0200 | [diff] [blame] | 679 | static int __init wacom_init(void) |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 680 | { |
| 681 | int ret; |
| 682 | |
| 683 | ret = hid_register_driver(&wacom_driver); |
| 684 | if (ret) |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 685 | pr_err("can't register wacom driver\n"); |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 686 | return ret; |
| 687 | } |
| 688 | |
Peter Huewe | a24f423 | 2009-07-02 19:08:38 +0200 | [diff] [blame] | 689 | static void __exit wacom_exit(void) |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 690 | { |
| 691 | hid_unregister_driver(&wacom_driver); |
| 692 | } |
| 693 | |
| 694 | module_init(wacom_init); |
| 695 | module_exit(wacom_exit); |
| 696 | MODULE_LICENSE("GPL"); |
| 697 | |