| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Copyright (c) 2021 Hans de Goede <hdegoede@redhat.com> |
| 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(hdev: 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(hdev: 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(dev: 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(dev: input, ABS_X, min: 0, max: 50800, fuzz: 0, flat: 0); |
| 112 | input_set_abs_params(dev: input, ABS_Y, min: 0, max: 31750, fuzz: 0, flat: 0); |
| 113 | input_set_abs_params(dev: input, ABS_PRESSURE, min: 0, max: 8192, fuzz: 0, flat: 0); |
| 114 | input_abs_set_res(dev: input, ABS_X, val: 240); |
| 115 | input_abs_set_res(dev: input, ABS_Y, val: 225); |
| 116 | input_set_capability(dev: input, EV_KEY, BTN_TOUCH); |
| 117 | input_set_capability(dev: input, EV_KEY, BTN_TOOL_PEN); |
| 118 | input_set_capability(dev: input, EV_KEY, BTN_STYLUS); |
| 119 | input_set_capability(dev: input, EV_KEY, BTN_STYLUS2); |
| 120 | |
| 121 | /* All known brands selling this tablet use WP9620[N] as model name */ |
| 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(dev: 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(dev: input, ABS_X, min: 0, max: 1, fuzz: 0, flat: 0); |
| 146 | input_set_abs_params(dev: input, ABS_Y, min: 0, max: 1, fuzz: 0, flat: 0); |
| 147 | input_set_capability(dev: 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(dev: input, BTN_TOOL_PEN, value: 0); |
| 163 | input_sync(dev: 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: /* Pen data */ |
| 179 | input = data->input_tablet; |
| 180 | input_report_key(dev: input, BTN_TOOL_PEN, value: 1); |
| 181 | input_report_key(dev: input, BTN_TOUCH, value: raw_data[1] & 0x01); |
| 182 | input_report_key(dev: input, BTN_STYLUS, value: raw_data[1] & 0x02); |
| 183 | input_report_key(dev: input, BTN_STYLUS2, value: raw_data[1] & 0x04); |
| 184 | input_report_abs(dev: input, ABS_X, |
| 185 | value: get_unaligned_le16(p: raw_data + 2)); |
| 186 | input_report_abs(dev: input, ABS_Y, |
| 187 | value: get_unaligned_le16(p: raw_data + 4)); |
| 188 | input_report_abs(dev: input, ABS_PRESSURE, |
| 189 | value: get_unaligned_le16(p: 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(timer: &data->inrange_timer, expires: jiffies + msecs_to_jiffies(m: 100)); |
| 195 | break; |
| 196 | case 0xe0: /* Pad data */ |
| 197 | input = data->input_tablet_pad; |
| 198 | for (i = 0; i < LETSKETCH_PAD_BUTTONS; i++) |
| 199 | input_report_key(dev: input, BTN_0 + i, value: 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(dev: 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(min: 5000, max: 7000); |
| 223 | ret = usb_string(dev: 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; /* Ignore the other interfaces */ |
| 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, index: i, buf, size: 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, index: i, buf, size: sizeof(buf)); |
| 267 | if (ret) |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | ret = letsketch_get_string(udev, index: 0x64, buf, size: sizeof(buf)); |
| 272 | if (ret) |
| 273 | return ret; |
| 274 | |
| 275 | ret = letsketch_get_string(udev, LETSKETCH_INFO_STR_IDX_BEGIN, buf, size: 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(min: 5000, max: 7000); |
| 284 | |
| 285 | ret = hid_parse(hdev); |
| 286 | if (ret) |
| 287 | return ret; |
| 288 | |
| 289 | data = devm_kzalloc(dev, size: 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 | return ret; |
| 300 | |
| 301 | ret = letsketch_setup_input_tablet_pad(data); |
| 302 | if (ret) |
| 303 | return ret; |
| 304 | |
| 305 | return hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 306 | } |
| 307 | |
| 308 | static const struct hid_device_id letsketch_devices[] = { |
| 309 | { HID_USB_DEVICE(USB_VENDOR_ID_LETSKETCH, USB_DEVICE_ID_WP9620N) }, |
| 310 | { } |
| 311 | }; |
| 312 | MODULE_DEVICE_TABLE(hid, letsketch_devices); |
| 313 | |
| 314 | static struct hid_driver letsketch_driver = { |
| 315 | .name = "letsketch" , |
| 316 | .id_table = letsketch_devices, |
| 317 | .probe = letsketch_probe, |
| 318 | .raw_event = letsketch_raw_event, |
| 319 | }; |
| 320 | module_hid_driver(letsketch_driver); |
| 321 | |
| 322 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>" ); |
| 323 | MODULE_DESCRIPTION("Driver for the LetSketch / VSON WP9620N drawing tablet" ); |
| 324 | MODULE_LICENSE("GPL" ); |
| 325 | |