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> |
| 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 | #include <linux/device.h> |
| 22 | #include <linux/hid.h> |
| 23 | #include <linux/module.h> |
| 24 | |
| 25 | #include "hid-ids.h" |
| 26 | |
| 27 | struct wacom_data { |
| 28 | __u16 tool; |
| 29 | unsigned char butstate; |
| 30 | }; |
| 31 | |
| 32 | static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report, |
| 33 | u8 *raw_data, int size) |
| 34 | { |
| 35 | struct wacom_data *wdata = hid_get_drvdata(hdev); |
| 36 | struct hid_input *hidinput; |
| 37 | struct input_dev *input; |
| 38 | unsigned char *data = (unsigned char *) raw_data; |
| 39 | int tool, x, y, rw; |
| 40 | |
| 41 | if (!(hdev->claimed & HID_CLAIMED_INPUT)) |
| 42 | return 0; |
| 43 | |
| 44 | tool = 0; |
| 45 | hidinput = list_entry(hdev->inputs.next, struct hid_input, list); |
| 46 | input = hidinput->input; |
| 47 | |
| 48 | /* Check if this is a tablet report */ |
| 49 | if (data[0] != 0x03) |
| 50 | return 0; |
| 51 | |
| 52 | /* Get X & Y positions */ |
| 53 | x = le16_to_cpu(*(__le16 *) &data[2]); |
| 54 | y = le16_to_cpu(*(__le16 *) &data[4]); |
| 55 | |
| 56 | /* Get current tool identifier */ |
| 57 | if (data[1] & 0x90) { /* If pen is in the in/active area */ |
| 58 | switch ((data[1] >> 5) & 3) { |
| 59 | case 0: /* Pen */ |
| 60 | tool = BTN_TOOL_PEN; |
| 61 | break; |
| 62 | |
| 63 | case 1: /* Rubber */ |
| 64 | tool = BTN_TOOL_RUBBER; |
| 65 | break; |
| 66 | |
| 67 | case 2: /* Mouse with wheel */ |
| 68 | case 3: /* Mouse without wheel */ |
| 69 | tool = BTN_TOOL_MOUSE; |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | /* Reset tool if out of active tablet area */ |
| 74 | if (!(data[1] & 0x10)) |
| 75 | tool = 0; |
| 76 | } |
| 77 | |
| 78 | /* If tool changed, notify input subsystem */ |
| 79 | if (wdata->tool != tool) { |
| 80 | if (wdata->tool) { |
| 81 | /* Completely reset old tool state */ |
| 82 | if (wdata->tool == BTN_TOOL_MOUSE) { |
| 83 | input_report_key(input, BTN_LEFT, 0); |
| 84 | input_report_key(input, BTN_RIGHT, 0); |
| 85 | input_report_key(input, BTN_MIDDLE, 0); |
| 86 | input_report_abs(input, ABS_DISTANCE, |
| 87 | input->absmax[ABS_DISTANCE]); |
| 88 | } else { |
| 89 | input_report_key(input, BTN_TOUCH, 0); |
| 90 | input_report_key(input, BTN_STYLUS, 0); |
| 91 | input_report_key(input, BTN_STYLUS2, 0); |
| 92 | input_report_abs(input, ABS_PRESSURE, 0); |
| 93 | } |
| 94 | input_report_key(input, wdata->tool, 0); |
| 95 | input_sync(input); |
| 96 | } |
| 97 | wdata->tool = tool; |
| 98 | if (tool) |
| 99 | input_report_key(input, tool, 1); |
| 100 | } |
| 101 | |
| 102 | if (tool) { |
| 103 | input_report_abs(input, ABS_X, x); |
| 104 | input_report_abs(input, ABS_Y, y); |
| 105 | |
| 106 | switch ((data[1] >> 5) & 3) { |
| 107 | case 2: /* Mouse with wheel */ |
| 108 | input_report_key(input, BTN_MIDDLE, data[1] & 0x04); |
| 109 | rw = (data[6] & 0x01) ? -1 : |
| 110 | (data[6] & 0x02) ? 1 : 0; |
| 111 | input_report_rel(input, REL_WHEEL, rw); |
| 112 | /* fall through */ |
| 113 | |
| 114 | case 3: /* Mouse without wheel */ |
| 115 | input_report_key(input, BTN_LEFT, data[1] & 0x01); |
| 116 | input_report_key(input, BTN_RIGHT, data[1] & 0x02); |
| 117 | /* Compute distance between mouse and tablet */ |
| 118 | rw = 44 - (data[6] >> 2); |
| 119 | if (rw < 0) |
| 120 | rw = 0; |
| 121 | else if (rw > 31) |
| 122 | rw = 31; |
| 123 | input_report_abs(input, ABS_DISTANCE, rw); |
| 124 | break; |
| 125 | |
| 126 | default: |
| 127 | input_report_abs(input, ABS_PRESSURE, |
| 128 | data[6] | (((__u16) (data[1] & 0x08)) << 5)); |
| 129 | input_report_key(input, BTN_TOUCH, data[1] & 0x01); |
| 130 | input_report_key(input, BTN_STYLUS, data[1] & 0x02); |
| 131 | input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04); |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | input_sync(input); |
| 136 | } |
| 137 | |
| 138 | /* Report the state of the two buttons at the top of the tablet |
| 139 | * as two extra fingerpad keys (buttons 4 & 5). */ |
| 140 | rw = data[7] & 0x03; |
| 141 | if (rw != wdata->butstate) { |
| 142 | wdata->butstate = rw; |
| 143 | input_report_key(input, BTN_0, rw & 0x02); |
| 144 | input_report_key(input, BTN_1, rw & 0x01); |
Przemo Firszt | 0e253fd | 2010-01-09 15:20:03 +0100 | [diff] [blame] | 145 | input_report_key(input, BTN_TOOL_FINGER, 0xf0); |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 146 | input_event(input, EV_MSC, MSC_SERIAL, 0xf0); |
| 147 | input_sync(input); |
| 148 | } |
| 149 | |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | static int wacom_probe(struct hid_device *hdev, |
| 154 | const struct hid_device_id *id) |
| 155 | { |
| 156 | struct hid_input *hidinput; |
| 157 | struct input_dev *input; |
| 158 | struct wacom_data *wdata; |
Bastien Nocera | 46a709b | 2010-01-20 12:00:53 +0000 | [diff] [blame^] | 159 | char rep_data[2]; |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 160 | int ret; |
Bastien Nocera | 46a709b | 2010-01-20 12:00:53 +0000 | [diff] [blame^] | 161 | int limit; |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 162 | |
| 163 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); |
| 164 | if (wdata == NULL) { |
| 165 | dev_err(&hdev->dev, "can't alloc wacom descriptor\n"); |
| 166 | return -ENOMEM; |
| 167 | } |
| 168 | |
| 169 | hid_set_drvdata(hdev, wdata); |
| 170 | |
Bastien Nocera | 46a709b | 2010-01-20 12:00:53 +0000 | [diff] [blame^] | 171 | /* Parse the HID report now */ |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 172 | ret = hid_parse(hdev); |
| 173 | if (ret) { |
| 174 | dev_err(&hdev->dev, "parse failed\n"); |
| 175 | goto err_free; |
| 176 | } |
| 177 | |
| 178 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 179 | if (ret) { |
| 180 | dev_err(&hdev->dev, "hw start failed\n"); |
| 181 | goto err_free; |
| 182 | } |
| 183 | |
Bastien Nocera | 46a709b | 2010-01-20 12:00:53 +0000 | [diff] [blame^] | 184 | /* Set Wacom mode2 */ |
| 185 | rep_data[0] = 0x03; rep_data[1] = 0x00; |
| 186 | limit = 3; |
| 187 | do { |
| 188 | ret = hdev->hid_output_raw_report(hdev, rep_data, 2, |
| 189 | HID_FEATURE_REPORT); |
| 190 | } while (ret < 0 && limit-- > 0); |
| 191 | if (ret < 0) { |
| 192 | dev_err(&hdev->dev, "failed to poke device #1, %d\n", ret); |
| 193 | goto err_free; |
| 194 | } |
| 195 | |
| 196 | /* 0x06 - high reporting speed, 0x05 - low speed */ |
| 197 | rep_data[0] = 0x06; rep_data[1] = 0x00; |
| 198 | limit = 3; |
| 199 | do { |
| 200 | ret = hdev->hid_output_raw_report(hdev, rep_data, 2, |
| 201 | HID_FEATURE_REPORT); |
| 202 | } while (ret < 0 && limit-- > 0); |
| 203 | if (ret < 0) { |
| 204 | dev_err(&hdev->dev, "failed to poke device #2, %d\n", ret); |
| 205 | goto err_free; |
| 206 | } |
| 207 | |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 208 | hidinput = list_entry(hdev->inputs.next, struct hid_input, list); |
| 209 | input = hidinput->input; |
| 210 | |
| 211 | /* Basics */ |
| 212 | input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL); |
| 213 | input->absbit[0] |= BIT(ABS_X) | BIT(ABS_Y) | |
| 214 | BIT(ABS_PRESSURE) | BIT(ABS_DISTANCE); |
| 215 | input->relbit[0] |= BIT(REL_WHEEL); |
| 216 | set_bit(BTN_TOOL_PEN, input->keybit); |
| 217 | set_bit(BTN_TOUCH, input->keybit); |
| 218 | set_bit(BTN_STYLUS, input->keybit); |
| 219 | set_bit(BTN_STYLUS2, input->keybit); |
| 220 | set_bit(BTN_LEFT, input->keybit); |
| 221 | set_bit(BTN_RIGHT, input->keybit); |
| 222 | set_bit(BTN_MIDDLE, input->keybit); |
| 223 | |
| 224 | /* Pad */ |
| 225 | input->evbit[0] |= BIT(EV_MSC); |
| 226 | input->mscbit[0] |= BIT(MSC_SERIAL); |
Przemo Firszt | d01799b | 2010-01-04 12:32:00 +0100 | [diff] [blame] | 227 | set_bit(BTN_0, input->keybit); |
| 228 | set_bit(BTN_1, input->keybit); |
| 229 | set_bit(BTN_TOOL_FINGER, input->keybit); |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 230 | |
| 231 | /* Distance, rubber and mouse */ |
| 232 | input->absbit[0] |= BIT(ABS_DISTANCE); |
| 233 | set_bit(BTN_TOOL_RUBBER, input->keybit); |
| 234 | set_bit(BTN_TOOL_MOUSE, input->keybit); |
| 235 | |
| 236 | input->absmax[ABS_PRESSURE] = 511; |
| 237 | input->absmax[ABS_DISTANCE] = 32; |
| 238 | |
| 239 | input->absmax[ABS_X] = 16704; |
| 240 | input->absmax[ABS_Y] = 12064; |
| 241 | input->absfuzz[ABS_X] = 4; |
| 242 | input->absfuzz[ABS_Y] = 4; |
| 243 | |
| 244 | return 0; |
| 245 | err_free: |
| 246 | kfree(wdata); |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | static void wacom_remove(struct hid_device *hdev) |
| 251 | { |
| 252 | hid_hw_stop(hdev); |
| 253 | kfree(hid_get_drvdata(hdev)); |
| 254 | } |
| 255 | |
| 256 | static const struct hid_device_id wacom_devices[] = { |
| 257 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) }, |
| 258 | |
| 259 | { } |
| 260 | }; |
| 261 | MODULE_DEVICE_TABLE(hid, wacom_devices); |
| 262 | |
| 263 | static struct hid_driver wacom_driver = { |
| 264 | .name = "wacom", |
| 265 | .id_table = wacom_devices, |
| 266 | .probe = wacom_probe, |
| 267 | .remove = wacom_remove, |
| 268 | .raw_event = wacom_raw_event, |
| 269 | }; |
| 270 | |
Peter Huewe | a24f423 | 2009-07-02 19:08:38 +0200 | [diff] [blame] | 271 | static int __init wacom_init(void) |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 272 | { |
| 273 | int ret; |
| 274 | |
| 275 | ret = hid_register_driver(&wacom_driver); |
| 276 | if (ret) |
| 277 | printk(KERN_ERR "can't register wacom driver\n"); |
| 278 | printk(KERN_ERR "wacom driver registered\n"); |
| 279 | return ret; |
| 280 | } |
| 281 | |
Peter Huewe | a24f423 | 2009-07-02 19:08:38 +0200 | [diff] [blame] | 282 | static void __exit wacom_exit(void) |
Bastien Nocera | ca2dcd4 | 2009-05-11 17:18:12 +0200 | [diff] [blame] | 283 | { |
| 284 | hid_unregister_driver(&wacom_driver); |
| 285 | } |
| 286 | |
| 287 | module_init(wacom_init); |
| 288 | module_exit(wacom_exit); |
| 289 | MODULE_LICENSE("GPL"); |
| 290 | |