1 // SPDX-License-Identifier: GPL-2.0-only
3 * HID driver for THQ PS3 uDraw tablet
5 * Copyright (C) 2016 Red Hat Inc. All Rights Reserved
8 #include <linux/device.h>
10 #include <linux/module.h>
13 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
14 MODULE_DESCRIPTION("PS3 uDraw tablet driver");
15 MODULE_LICENSE("GPL");
18 * Protocol information from:
19 * https://brandonw.net/udraw/
20 * and the source code of:
21 * https://vvvv.org/contribution/udraw-hid
25 * The device is setup with multiple input devices:
26 * - the touch area which works as a touchpad
27 * - the tablet area which works as a touchpad/drawing tablet
28 * - a joypad with a d-pad, and 7 buttons
29 * - an accelerometer device
46 * Accelerometer min/max values
47 * in order, X, Y and Z
53 [AXIS_X] = { 490, 534 },
54 [AXIS_Y] = { 490, 534 },
55 [AXIS_Z] = { 492, 536 }
58 #define DEVICE_NAME "THQ uDraw Game Tablet for PS3"
59 /* resolution in pixels */
65 #define PRESSURE_OFFSET 113
66 #define MAX_PRESSURE (255 - PRESSURE_OFFSET)
69 struct input_dev *joy_input_dev;
70 struct input_dev *touch_input_dev;
71 struct input_dev *pen_input_dev;
72 struct input_dev *accel_input_dev;
73 struct hid_device *hdev;
76 * The device's two-finger support is pretty unreliable, as
77 * the device could report a single touch when the two fingers
78 * are too close together, and the distance between fingers, even
79 * though reported is not in the same unit as the touches.
81 * We'll make do without it, and try to report the first touch
82 * as reliably as possible.
84 int last_one_finger_x;
85 int last_one_finger_y;
86 int last_two_finger_x;
87 int last_two_finger_y;
90 static int clamp_accel(int axis, int offset)
93 accel_limits[offset].min,
94 accel_limits[offset].max);
95 axis = (axis - accel_limits[offset].min) /
96 ((accel_limits[offset].max -
97 accel_limits[offset].min) * 0xFF);
101 static int udraw_raw_event(struct hid_device *hdev, struct hid_report *report,
104 struct udraw *udraw = hid_get_drvdata(hdev);
111 if (data[11] == 0x00)
113 else if (data[11] == 0x40)
115 else if (data[11] == 0x80)
116 touch = TOUCH_FINGER;
118 touch = TOUCH_TWOFINGER;
121 input_report_key(udraw->joy_input_dev, BTN_WEST, data[0] & 1);
122 input_report_key(udraw->joy_input_dev, BTN_SOUTH, !!(data[0] & 2));
123 input_report_key(udraw->joy_input_dev, BTN_EAST, !!(data[0] & 4));
124 input_report_key(udraw->joy_input_dev, BTN_NORTH, !!(data[0] & 8));
126 input_report_key(udraw->joy_input_dev, BTN_SELECT, !!(data[1] & 1));
127 input_report_key(udraw->joy_input_dev, BTN_START, !!(data[1] & 2));
128 input_report_key(udraw->joy_input_dev, BTN_MODE, !!(data[1] & 16));
164 input_report_abs(udraw->joy_input_dev, ABS_X, x);
165 input_report_abs(udraw->joy_input_dev, ABS_Y, y);
167 input_sync(udraw->joy_input_dev);
169 /* For pen and touchpad */
171 if (touch != TOUCH_NONE) {
172 if (data[15] != 0x0F)
173 x = data[15] * 256 + data[17];
174 if (data[16] != 0x0F)
175 y = data[16] * 256 + data[18];
178 if (touch == TOUCH_FINGER) {
179 /* Save the last one-finger touch */
180 udraw->last_one_finger_x = x;
181 udraw->last_one_finger_y = y;
182 udraw->last_two_finger_x = -1;
183 udraw->last_two_finger_y = -1;
184 } else if (touch == TOUCH_TWOFINGER) {
186 * We have a problem because x/y is the one for the
187 * second finger but we want the first finger given
188 * to user-space otherwise it'll look as if it jumped.
190 * See the udraw struct definition for why this was
191 * implemented this way.
193 if (udraw->last_two_finger_x == -1) {
194 /* Save the position of the 2nd finger */
195 udraw->last_two_finger_x = x;
196 udraw->last_two_finger_y = y;
198 x = udraw->last_one_finger_x;
199 y = udraw->last_one_finger_y;
202 * Offset the 2-finger coords using the
203 * saved data from the first finger
205 x = x - (udraw->last_two_finger_x
206 - udraw->last_one_finger_x);
207 y = y - (udraw->last_two_finger_y
208 - udraw->last_one_finger_y);
213 if (touch == TOUCH_FINGER || touch == TOUCH_TWOFINGER) {
214 input_report_key(udraw->touch_input_dev, BTN_TOUCH, 1);
215 input_report_key(udraw->touch_input_dev, BTN_TOOL_FINGER,
216 touch == TOUCH_FINGER);
217 input_report_key(udraw->touch_input_dev, BTN_TOOL_DOUBLETAP,
218 touch == TOUCH_TWOFINGER);
220 input_report_abs(udraw->touch_input_dev, ABS_X, x);
221 input_report_abs(udraw->touch_input_dev, ABS_Y, y);
223 input_report_key(udraw->touch_input_dev, BTN_TOUCH, 0);
224 input_report_key(udraw->touch_input_dev, BTN_TOOL_FINGER, 0);
225 input_report_key(udraw->touch_input_dev, BTN_TOOL_DOUBLETAP, 0);
227 input_sync(udraw->touch_input_dev);
230 if (touch == TOUCH_PEN) {
233 level = clamp(data[13] - PRESSURE_OFFSET,
236 input_report_key(udraw->pen_input_dev, BTN_TOUCH, (level != 0));
237 input_report_key(udraw->pen_input_dev, BTN_TOOL_PEN, 1);
238 input_report_abs(udraw->pen_input_dev, ABS_PRESSURE, level);
239 input_report_abs(udraw->pen_input_dev, ABS_X, x);
240 input_report_abs(udraw->pen_input_dev, ABS_Y, y);
242 input_report_key(udraw->pen_input_dev, BTN_TOUCH, 0);
243 input_report_key(udraw->pen_input_dev, BTN_TOOL_PEN, 0);
244 input_report_abs(udraw->pen_input_dev, ABS_PRESSURE, 0);
246 input_sync(udraw->pen_input_dev);
249 x = (data[19] + (data[20] << 8));
250 x = clamp_accel(x, AXIS_X);
251 y = (data[21] + (data[22] << 8));
252 y = clamp_accel(y, AXIS_Y);
253 z = (data[23] + (data[24] << 8));
254 z = clamp_accel(z, AXIS_Z);
255 input_report_abs(udraw->accel_input_dev, ABS_X, x);
256 input_report_abs(udraw->accel_input_dev, ABS_Y, y);
257 input_report_abs(udraw->accel_input_dev, ABS_Z, z);
258 input_sync(udraw->accel_input_dev);
260 /* let hidraw and hiddev handle the report */
264 static int udraw_open(struct input_dev *dev)
266 struct udraw *udraw = input_get_drvdata(dev);
268 return hid_hw_open(udraw->hdev);
271 static void udraw_close(struct input_dev *dev)
273 struct udraw *udraw = input_get_drvdata(dev);
275 hid_hw_close(udraw->hdev);
278 static struct input_dev *allocate_and_setup(struct hid_device *hdev,
281 struct input_dev *input_dev;
283 input_dev = devm_input_allocate_device(&hdev->dev);
287 input_dev->name = name;
288 input_dev->phys = hdev->phys;
289 input_dev->dev.parent = &hdev->dev;
290 input_dev->open = udraw_open;
291 input_dev->close = udraw_close;
292 input_dev->uniq = hdev->uniq;
293 input_dev->id.bustype = hdev->bus;
294 input_dev->id.vendor = hdev->vendor;
295 input_dev->id.product = hdev->product;
296 input_dev->id.version = hdev->version;
297 input_set_drvdata(input_dev, hid_get_drvdata(hdev));
302 static bool udraw_setup_touch(struct udraw *udraw,
303 struct hid_device *hdev)
305 struct input_dev *input_dev;
307 input_dev = allocate_and_setup(hdev, DEVICE_NAME " Touchpad");
311 input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
313 input_set_abs_params(input_dev, ABS_X, 0, RES_X, 1, 0);
314 input_abs_set_res(input_dev, ABS_X, RES_X / WIDTH);
315 input_set_abs_params(input_dev, ABS_Y, 0, RES_Y, 1, 0);
316 input_abs_set_res(input_dev, ABS_Y, RES_Y / HEIGHT);
318 set_bit(BTN_TOUCH, input_dev->keybit);
319 set_bit(BTN_TOOL_FINGER, input_dev->keybit);
320 set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
322 set_bit(INPUT_PROP_POINTER, input_dev->propbit);
324 udraw->touch_input_dev = input_dev;
329 static bool udraw_setup_pen(struct udraw *udraw,
330 struct hid_device *hdev)
332 struct input_dev *input_dev;
334 input_dev = allocate_and_setup(hdev, DEVICE_NAME " Pen");
338 input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
340 input_set_abs_params(input_dev, ABS_X, 0, RES_X, 1, 0);
341 input_abs_set_res(input_dev, ABS_X, RES_X / WIDTH);
342 input_set_abs_params(input_dev, ABS_Y, 0, RES_Y, 1, 0);
343 input_abs_set_res(input_dev, ABS_Y, RES_Y / HEIGHT);
344 input_set_abs_params(input_dev, ABS_PRESSURE,
345 0, MAX_PRESSURE, 0, 0);
347 set_bit(BTN_TOUCH, input_dev->keybit);
348 set_bit(BTN_TOOL_PEN, input_dev->keybit);
350 set_bit(INPUT_PROP_POINTER, input_dev->propbit);
352 udraw->pen_input_dev = input_dev;
357 static bool udraw_setup_accel(struct udraw *udraw,
358 struct hid_device *hdev)
360 struct input_dev *input_dev;
362 input_dev = allocate_and_setup(hdev, DEVICE_NAME " Accelerometer");
366 input_dev->evbit[0] = BIT(EV_ABS);
368 /* 1G accel is reported as ~256, so clamp to 2G */
369 input_set_abs_params(input_dev, ABS_X, -512, 512, 0, 0);
370 input_set_abs_params(input_dev, ABS_Y, -512, 512, 0, 0);
371 input_set_abs_params(input_dev, ABS_Z, -512, 512, 0, 0);
373 set_bit(INPUT_PROP_ACCELEROMETER, input_dev->propbit);
375 udraw->accel_input_dev = input_dev;
380 static bool udraw_setup_joypad(struct udraw *udraw,
381 struct hid_device *hdev)
383 struct input_dev *input_dev;
385 input_dev = allocate_and_setup(hdev, DEVICE_NAME " Joypad");
389 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
391 set_bit(BTN_SOUTH, input_dev->keybit);
392 set_bit(BTN_NORTH, input_dev->keybit);
393 set_bit(BTN_EAST, input_dev->keybit);
394 set_bit(BTN_WEST, input_dev->keybit);
395 set_bit(BTN_SELECT, input_dev->keybit);
396 set_bit(BTN_START, input_dev->keybit);
397 set_bit(BTN_MODE, input_dev->keybit);
399 input_set_abs_params(input_dev, ABS_X, -127, 127, 0, 0);
400 input_set_abs_params(input_dev, ABS_Y, -127, 127, 0, 0);
402 udraw->joy_input_dev = input_dev;
407 static int udraw_probe(struct hid_device *hdev, const struct hid_device_id *id)
412 udraw = devm_kzalloc(&hdev->dev, sizeof(struct udraw), GFP_KERNEL);
417 udraw->last_two_finger_x = -1;
418 udraw->last_two_finger_y = -1;
420 hid_set_drvdata(hdev, udraw);
422 ret = hid_parse(hdev);
424 hid_err(hdev, "parse failed\n");
428 if (!udraw_setup_joypad(udraw, hdev) ||
429 !udraw_setup_touch(udraw, hdev) ||
430 !udraw_setup_pen(udraw, hdev) ||
431 !udraw_setup_accel(udraw, hdev)) {
432 hid_err(hdev, "could not allocate interfaces\n");
436 ret = input_register_device(udraw->joy_input_dev) ||
437 input_register_device(udraw->touch_input_dev) ||
438 input_register_device(udraw->pen_input_dev) ||
439 input_register_device(udraw->accel_input_dev);
441 hid_err(hdev, "failed to register interfaces\n");
445 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW | HID_CONNECT_DRIVER);
447 hid_err(hdev, "hw start failed\n");
454 static const struct hid_device_id udraw_devices[] = {
455 { HID_USB_DEVICE(USB_VENDOR_ID_THQ, USB_DEVICE_ID_THQ_PS3_UDRAW) },
458 MODULE_DEVICE_TABLE(hid, udraw_devices);
460 static struct hid_driver udraw_driver = {
462 .id_table = udraw_devices,
463 .raw_event = udraw_raw_event,
464 .probe = udraw_probe,
466 module_hid_driver(udraw_driver);