| 1 | |
| 2 | |
| 3 | * Copyright (c) 2021 Hans de Goede <[email protected]>
|
| 4 | *
|
| 5 | * Driver for the LetSketch / VSON WP9620N drawing tablet.
|
| 6 | * This drawing tablet is also sold under other brand names such as Case U,
|
| 7 | * presumably this driver will work for all of them. But it has only been
|
| 8 | * tested with a LetSketch WP9620N model.
|
| 9 | *
|
| 10 | * These tablets also work without a special HID driver, but then only part
|
| 11 | * of the active area works and both the pad and stylus buttons are hardwired
|
| 12 | * to special key-combos. E.g. the 2 stylus buttons send right mouse clicks /
|
| 13 | * resp. "e" key presses.
|
| 14 | *
|
| 15 | * This device has 4 USB interfaces:
|
| 16 | *
|
| 17 | * Interface 0 EP 0x81 bootclass mouse, rdesc len 18, report id 0x08,
|
| 18 | * Application(ff00.0001)
|
| 19 | * This interface sends raw event input reports in a custom format, but only
|
| 20 | * after doing the special dance from letsketch_probe(). After enabling this
|
| 21 | * interface the other 3 interfaces are disabled.
|
| 22 | *
|
| 23 | * Interface 1 EP 0x82 bootclass mouse, rdesc len 83, report id 0x0a, Tablet
|
| 24 | * This interface sends absolute events for the pen, including pressure,
|
| 25 | * but only for some part of the active area due to special "aspect ratio"
|
| 26 | * correction and only half by default since it assumes it will be used
|
| 27 | * with a phone in portraid mode, while using the tablet in landscape mode.
|
| 28 | * Also stylus + pad button events are not reported here.
|
| 29 | *
|
| 30 | * Interface 2 EP 0x83 bootclass keybd, rdesc len 64, report id none, Std Kbd
|
| 31 | * This interfaces send various hard-coded key-combos for the pad buttons
|
| 32 | * and "e" keypresses for the 2nd stylus button
|
| 33 | *
|
| 34 | * Interface 3 EP 0x84 bootclass mouse, rdesc len 75, report id 0x01, Std Mouse
|
| 35 | * This reports right-click mouse-button events for the 1st stylus button
|
| 36 |
|
| 37 | #include <linux/device.h>
|
| 38 | #include <linux/input.h>
|
| 39 | #include <linux/hid.h>
|
| 40 | #include <linux/module.h>
|
| 41 | #include <linux/timer.h>
|
| 42 | #include <linux/usb.h>
|
| 43 |
|
| 44 | #include <linux/unaligned.h>
|
| 45 |
|
| 46 | #include "hid-ids.h"
|
| 47 |
|
| 48 | #define LETSKETCH_RAW_IF 0
|
| 49 |
|
| 50 | #define LETSKETCH_RAW_DATA_LEN 12
|
| 51 | #define LETSKETCH_RAW_REPORT_ID 8
|
| 52 |
|
| 53 | #define LETSKETCH_PAD_BUTTONS 5
|
| 54 |
|
| 55 | #define LETSKETCH_INFO_STR_IDX_BEGIN 0xc8
|
| 56 | #define LETSKETCH_INFO_STR_IDX_END 0xca
|
| 57 |
|
| 58 | #define LETSKETCH_GET_STRING_RETRIES 5
|
| 59 |
|
| 60 | struct letsketch_data {
|
| 61 | struct hid_device *hdev;
|
| 62 | struct input_dev *input_tablet;
|
| 63 | struct input_dev *input_tablet_pad;
|
| 64 | struct timer_list inrange_timer;
|
| 65 | };
|
| 66 |
|
| 67 | static int letsketch_open(struct input_dev *dev)
|
| 68 | {
|
| 69 | struct letsketch_data *data = input_get_drvdata(dev);
|
| 70 |
|
| 71 | return hid_hw_open(data->hdev);
|
| 72 | }
|
| 73 |
|
| 74 | static void letsketch_close(struct input_dev *dev)
|
| 75 | {
|
| 76 | struct letsketch_data *data = input_get_drvdata(dev);
|
| 77 |
|
| 78 | hid_hw_close(data->hdev);
|
| 79 | }
|
| 80 |
|
| 81 | static struct input_dev *letsketch_alloc_input_dev(struct letsketch_data *data)
|
| 82 | {
|
| 83 | struct input_dev *input;
|
| 84 |
|
| 85 | input = devm_input_allocate_device(&data->hdev->dev);
|
| 86 | if (!input)
|
| 87 | return NULL;
|
| 88 |
|
| 89 | input->id.bustype = data->hdev->bus;
|
| 90 | input->id.vendor = data->hdev->vendor;
|
| 91 | input->id.product = data->hdev->product;
|
| 92 | input->id.version = data->hdev->bus;
|
| 93 | input->phys = data->hdev->phys;
|
| 94 | input->uniq = data->hdev->uniq;
|
| 95 | input->open = letsketch_open;
|
| 96 | input->close = letsketch_close;
|
| 97 |
|
| 98 | input_set_drvdata(input, data);
|
| 99 |
|
| 100 | return input;
|
| 101 | }
|
| 102 |
|
| 103 | static int letsketch_setup_input_tablet(struct letsketch_data *data)
|
| 104 | {
|
| 105 | struct input_dev *input;
|
| 106 |
|
| 107 | input = letsketch_alloc_input_dev(data);
|
| 108 | if (!input)
|
| 109 | return -ENOMEM;
|
| 110 |
|
| 111 | input_set_abs_params(input, ABS_X, 0, 50800, 0, 0);
|
| 112 | input_set_abs_params(input, ABS_Y, 0, 31750, 0, 0);
|
| 113 | input_set_abs_params(input, ABS_PRESSURE, 0, 8192, 0, 0);
|
| 114 | input_abs_set_res(input, ABS_X, 240);
|
| 115 | input_abs_set_res(input, ABS_Y, 225);
|
| 116 | input_set_capability(input, EV_KEY, BTN_TOUCH);
|
| 117 | input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
|
| 118 | input_set_capability(input, EV_KEY, BTN_STYLUS);
|
| 119 | input_set_capability(input, EV_KEY, BTN_STYLUS2);
|
| 120 |
|
| 121 |
|
| 122 | input->name = "WP9620 Tablet";
|
| 123 |
|
| 124 | data->input_tablet = input;
|
| 125 |
|
| 126 | return input_register_device(data->input_tablet);
|
| 127 | }
|
| 128 |
|
| 129 | static int letsketch_setup_input_tablet_pad(struct letsketch_data *data)
|
| 130 | {
|
| 131 | struct input_dev *input;
|
| 132 | int i;
|
| 133 |
|
| 134 | input = letsketch_alloc_input_dev(data);
|
| 135 | if (!input)
|
| 136 | return -ENOMEM;
|
| 137 |
|
| 138 | for (i = 0; i < LETSKETCH_PAD_BUTTONS; i++)
|
| 139 | input_set_capability(input, EV_KEY, BTN_0 + i);
|
| 140 |
|
| 141 | |
| 142 | * These are never send on the pad input_dev, but must be set
|
| 143 | * on the Pad to make udev / libwacom happy.
|
| 144 |
|
| 145 | input_set_abs_params(input, ABS_X, 0, 1, 0, 0);
|
| 146 | input_set_abs_params(input, ABS_Y, 0, 1, 0, 0);
|
| 147 | input_set_capability(input, EV_KEY, BTN_STYLUS);
|
| 148 |
|
| 149 | input->name = "WP9620 Pad";
|
| 150 |
|
| 151 | data->input_tablet_pad = input;
|
| 152 |
|
| 153 | return input_register_device(data->input_tablet_pad);
|
| 154 | }
|
| 155 |
|
| 156 | static void letsketch_inrange_timeout(struct timer_list *t)
|
| 157 | {
|
| 158 | struct letsketch_data *data = timer_container_of(data, t,
|
| 159 | inrange_timer);
|
| 160 | struct input_dev *input = data->input_tablet;
|
| 161 |
|
| 162 | input_report_key(input, BTN_TOOL_PEN, 0);
|
| 163 | input_sync(input);
|
| 164 | }
|
| 165 |
|
| 166 | static int letsketch_raw_event(struct hid_device *hdev,
|
| 167 | struct hid_report *report,
|
| 168 | u8 *raw_data, int size)
|
| 169 | {
|
| 170 | struct letsketch_data *data = hid_get_drvdata(hdev);
|
| 171 | struct input_dev *input;
|
| 172 | int i;
|
| 173 |
|
| 174 | if (size != LETSKETCH_RAW_DATA_LEN || raw_data[0] != LETSKETCH_RAW_REPORT_ID)
|
| 175 | return 0;
|
| 176 |
|
| 177 | switch (raw_data[1] & 0xf0) {
|
| 178 | case 0x80:
|
| 179 | input = data->input_tablet;
|
| 180 | input_report_key(input, BTN_TOOL_PEN, 1);
|
| 181 | input_report_key(input, BTN_TOUCH, raw_data[1] & 0x01);
|
| 182 | input_report_key(input, BTN_STYLUS, raw_data[1] & 0x02);
|
| 183 | input_report_key(input, BTN_STYLUS2, raw_data[1] & 0x04);
|
| 184 | input_report_abs(input, ABS_X,
|
| 185 | get_unaligned_le16(raw_data + 2));
|
| 186 | input_report_abs(input, ABS_Y,
|
| 187 | get_unaligned_le16(raw_data + 4));
|
| 188 | input_report_abs(input, ABS_PRESSURE,
|
| 189 | get_unaligned_le16(raw_data + 6));
|
| 190 | |
| 191 | * There is no out of range event, so use a timer for this
|
| 192 | * when in range we get an event approx. every 8 ms.
|
| 193 |
|
| 194 | mod_timer(&data->inrange_timer, jiffies + msecs_to_jiffies(100));
|
| 195 | break;
|
| 196 | case 0xe0:
|
| 197 | input = data->input_tablet_pad;
|
| 198 | for (i = 0; i < LETSKETCH_PAD_BUTTONS; i++)
|
| 199 | input_report_key(input, BTN_0 + i, raw_data[4] == (i + 1));
|
| 200 | break;
|
| 201 | default:
|
| 202 | hid_warn(data->hdev, "Warning unknown data header: 0x%02x\n",
|
| 203 | raw_data[0]);
|
| 204 | return 0;
|
| 205 | }
|
| 206 |
|
| 207 | input_sync(input);
|
| 208 | return 0;
|
| 209 | }
|
| 210 |
|
| 211 | |
| 212 | * The tablets magic handshake to put it in raw mode relies on getting
|
| 213 | * string descriptors. But the firmware is buggy and does not like it if
|
| 214 | * we do this too fast. Even if we go slow sometimes the usb_string() call
|
| 215 | * fails. Ignore errors and retry it a couple of times if necessary.
|
| 216 |
|
| 217 | static int letsketch_get_string(struct usb_device *udev, int index, char *buf, int size)
|
| 218 | {
|
| 219 | int i, ret;
|
| 220 |
|
| 221 | for (i = 0; i < LETSKETCH_GET_STRING_RETRIES; i++) {
|
| 222 | usleep_range(5000, 7000);
|
| 223 | ret = usb_string(udev, index, buf, size);
|
| 224 | if (ret > 0)
|
| 225 | return 0;
|
| 226 | }
|
| 227 |
|
| 228 | dev_err(&udev->dev, "Max retries (%d) exceeded reading string descriptor %d\n",
|
| 229 | LETSKETCH_GET_STRING_RETRIES, index);
|
| 230 | return ret ? ret : -EIO;
|
| 231 | }
|
| 232 |
|
| 233 | static int letsketch_probe(struct hid_device *hdev, const struct hid_device_id *id)
|
| 234 | {
|
| 235 | struct device *dev = &hdev->dev;
|
| 236 | struct letsketch_data *data;
|
| 237 | struct usb_interface *intf;
|
| 238 | struct usb_device *udev;
|
| 239 | char buf[256];
|
| 240 | int i, ret;
|
| 241 |
|
| 242 | if (!hid_is_usb(hdev))
|
| 243 | return -ENODEV;
|
| 244 |
|
| 245 | intf = to_usb_interface(hdev->dev.parent);
|
| 246 | if (intf->altsetting->desc.bInterfaceNumber != LETSKETCH_RAW_IF)
|
| 247 | return -ENODEV;
|
| 248 |
|
| 249 | udev = interface_to_usbdev(intf);
|
| 250 |
|
| 251 | |
| 252 | * Instead of using a set-feature request, or even a custom USB ctrl
|
| 253 | * message the tablet needs this elaborate magic reading of USB
|
| 254 | * string descriptors to kick it into raw mode. This is what the
|
| 255 | * Windows drivers are seen doing in an USB trace under Windows.
|
| 256 |
|
| 257 | for (i = LETSKETCH_INFO_STR_IDX_BEGIN; i <= LETSKETCH_INFO_STR_IDX_END; i++) {
|
| 258 | ret = letsketch_get_string(udev, i, buf, sizeof(buf));
|
| 259 | if (ret)
|
| 260 | return ret;
|
| 261 |
|
| 262 | hid_info(hdev, "Device info: %s\n", buf);
|
| 263 | }
|
| 264 |
|
| 265 | for (i = 1; i <= 250; i++) {
|
| 266 | ret = letsketch_get_string(udev, i, buf, sizeof(buf));
|
| 267 | if (ret)
|
| 268 | return ret;
|
| 269 | }
|
| 270 |
|
| 271 | ret = letsketch_get_string(udev, 0x64, buf, sizeof(buf));
|
| 272 | if (ret)
|
| 273 | return ret;
|
| 274 |
|
| 275 | ret = letsketch_get_string(udev, LETSKETCH_INFO_STR_IDX_BEGIN, buf, sizeof(buf));
|
| 276 | if (ret)
|
| 277 | return ret;
|
| 278 |
|
| 279 | |
| 280 | * The tablet should be in raw mode now, end with a final delay before
|
| 281 | * doing further IO to the device.
|
| 282 |
|
| 283 | usleep_range(5000, 7000);
|
| 284 |
|
| 285 | ret = hid_parse(hdev);
|
| 286 | if (ret)
|
| 287 | return ret;
|
| 288 |
|
| 289 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
|
| 290 | if (!data)
|
| 291 | return -ENOMEM;
|
| 292 |
|
| 293 | data->hdev = hdev;
|
| 294 | timer_setup(&data->inrange_timer, letsketch_inrange_timeout, 0);
|
| 295 | hid_set_drvdata(hdev, data);
|
| 296 |
|
| 297 | ret = letsketch_setup_input_tablet(data);
|
| 298 | if (ret)
|
| 299 | goto err_shutdown_timer;
|
| 300 |
|
| 301 | ret = letsketch_setup_input_tablet_pad(data);
|
| 302 | if (ret)
|
| 303 | goto err_shutdown_timer;
|
| 304 |
|
| 305 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
|
| 306 | if (ret)
|
| 307 | goto err_shutdown_timer;
|
| 308 |
|
| 309 | return 0;
|
| 310 |
|
| 311 | err_shutdown_timer:
|
| 312 | |
| 313 | * Drain any pending callback and permanently disable the timer
|
| 314 | * before devm releases data: if hid_hw_start() enabled I/O on an
|
| 315 | * always-poll-quirk device and then failed, raw_event may have
|
| 316 | * armed the timer already.
|
| 317 |
|
| 318 | timer_shutdown_sync(&data->inrange_timer);
|
| 319 | return ret;
|
| 320 | }
|
| 321 |
|
| 322 | static void letsketch_remove(struct hid_device *hdev)
|
| 323 | {
|
| 324 | struct letsketch_data *data = hid_get_drvdata(hdev);
|
| 325 |
|
| 326 | |
| 327 | * hid_hw_stop() synchronously kills the URBs that deliver
|
| 328 | * raw_event(), so once it returns no path can re-arm
|
| 329 | * inrange_timer. timer_shutdown_sync() then drains any
|
| 330 | * in-flight callback and permanently disables further
|
| 331 | * mod_timer() calls before devm releases data.
|
| 332 |
|
| 333 | hid_hw_stop(hdev);
|
| 334 | timer_shutdown_sync(&data->inrange_timer);
|
| 335 | }
|
| 336 |
|
| 337 | static const struct hid_device_id letsketch_devices[] = {
|
| 338 | { HID_USB_DEVICE(USB_VENDOR_ID_LETSKETCH, USB_DEVICE_ID_WP9620N) },
|
| 339 | { }
|
| 340 | };
|
| 341 | MODULE_DEVICE_TABLE(hid, letsketch_devices);
|
| 342 |
|
| 343 | static struct hid_driver letsketch_driver = {
|
| 344 | .name = "letsketch",
|
| 345 | .id_table = letsketch_devices,
|
| 346 | .probe = letsketch_probe,
|
| 347 | .remove = letsketch_remove,
|
| 348 | .raw_event = letsketch_raw_event,
|
| 349 | };
|
| 350 | module_hid_driver(letsketch_driver);
|
| 351 |
|
| 352 | MODULE_AUTHOR("Hans de Goede <[email protected]>");
|
| 353 | MODULE_DESCRIPTION("Driver for the LetSketch / VSON WP9620N drawing tablet");
|
| 354 | MODULE_LICENSE("GPL");
|