Blame view

drivers/hid/hid-uclogic-core.c 11.3 KB
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  // SPDX-License-Identifier: GPL-2.0+
  /*
   *  HID driver for UC-Logic devices not fully compliant with HID standard
   *
   *  Copyright (c) 2010-2014 Nikolai Kondrashov
   *  Copyright (c) 2013 Martin Rusko
   */
  
  /*
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License as published by the Free
   * Software Foundation; either version 2 of the License, or (at your option)
   * any later version.
   */
  
  #include <linux/device.h>
  #include <linux/hid.h>
  #include <linux/module.h>
01309e29e   Nikolai Kondrashov   HID: uclogic: Sup...
19
  #include <linux/timer.h>
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
20
  #include "usbhid/usbhid.h"
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
21
  #include "hid-uclogic-params.h"
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
22
23
  
  #include "hid-ids.h"
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
24
25
  /* Driver data */
  struct uclogic_drvdata {
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
26
27
28
29
30
31
32
33
34
  	/* Interface parameters */
  	struct uclogic_params params;
  	/* Pointer to the replacement report descriptor. NULL if none. */
  	__u8 *desc_ptr;
  	/*
  	 * Size of the replacement report descriptor.
  	 * Only valid if desc_ptr is not NULL
  	 */
  	unsigned int desc_size;
01309e29e   Nikolai Kondrashov   HID: uclogic: Sup...
35
36
37
38
  	/* Pen input device */
  	struct input_dev *pen_input;
  	/* In-range timer */
  	struct timer_list inrange_timer;
8a47670c3   Nikolai Kondrashov   HID: uclogic: Sup...
39
40
  	/* Last rotary encoder state, or U8_MAX for none */
  	u8 re_state;
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
41
  };
01309e29e   Nikolai Kondrashov   HID: uclogic: Sup...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  /**
   * uclogic_inrange_timeout - handle pen in-range state timeout.
   * Emulate input events normally generated when pen goes out of range for
   * tablets which don't report that.
   *
   * @t:	The timer the timeout handler is attached to, stored in a struct
   *	uclogic_drvdata.
   */
  static void uclogic_inrange_timeout(struct timer_list *t)
  {
  	struct uclogic_drvdata *drvdata = from_timer(drvdata, t,
  							inrange_timer);
  	struct input_dev *input = drvdata->pen_input;
  
  	if (input == NULL)
  		return;
  	input_report_abs(input, ABS_PRESSURE, 0);
  	/* If BTN_TOUCH state is changing */
  	if (test_bit(BTN_TOUCH, input->key)) {
  		input_event(input, EV_MSC, MSC_SCAN,
  				/* Digitizer Tip Switch usage */
  				0xd0042);
  		input_report_key(input, BTN_TOUCH, 0);
  	}
  	input_report_key(input, BTN_TOOL_PEN, 0);
  	input_sync(input);
  }
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
69
70
71
  static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  					unsigned int *rsize)
  {
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
72
  	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
73
74
75
  	if (drvdata->desc_ptr != NULL) {
  		rdesc = drvdata->desc_ptr;
  		*rsize = drvdata->desc_size;
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
76
  	}
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
77
78
  	return rdesc;
  }
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
79
80
81
82
83
84
  static int uclogic_input_mapping(struct hid_device *hdev,
  				 struct hid_input *hi,
  				 struct hid_field *field,
  				 struct hid_usage *usage,
  				 unsigned long **bit,
  				 int *max)
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
85
86
  {
  	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
87
  	struct uclogic_params *params = &drvdata->params;
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
88
89
  
  	/* discard the unused pen interface */
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
90
  	if (params->pen_unused && (field->application == HID_DG_PEN))
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
91
92
93
94
95
96
97
98
99
  		return -1;
  
  	/* let hid-core decide what to do */
  	return 0;
  }
  
  static int uclogic_input_configured(struct hid_device *hdev,
  		struct hid_input *hi)
  {
01309e29e   Nikolai Kondrashov   HID: uclogic: Sup...
100
101
  	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
  	struct uclogic_params *params = &drvdata->params;
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
102
103
104
105
106
107
108
109
  	char *name;
  	const char *suffix = NULL;
  	struct hid_field *field;
  	size_t len;
  
  	/* no report associated (HID_QUIRK_MULTI_INPUT not set) */
  	if (!hi->report)
  		return 0;
01309e29e   Nikolai Kondrashov   HID: uclogic: Sup...
110
111
112
113
114
115
116
117
  	/*
  	 * If this is the input corresponding to the pen report
  	 * in need of tweaking.
  	 */
  	if (hi->report->id == params->pen.id) {
  		/* Remember the input device so we can simulate events */
  		drvdata->pen_input = hi->input;
  	}
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  	field = hi->report->field[0];
  
  	switch (field->application) {
  	case HID_GD_KEYBOARD:
  		suffix = "Keyboard";
  		break;
  	case HID_GD_MOUSE:
  		suffix = "Mouse";
  		break;
  	case HID_GD_KEYPAD:
  		suffix = "Pad";
  		break;
  	case HID_DG_PEN:
  		suffix = "Pen";
  		break;
  	case HID_CP_CONSUMER_CONTROL:
  		suffix = "Consumer Control";
  		break;
  	case HID_GD_SYSTEM_CONTROL:
  		suffix = "System Control";
  		break;
  	}
  
  	if (suffix) {
  		len = strlen(hdev->name) + 2 + strlen(suffix);
  		name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL);
  		if (name) {
  			snprintf(name, len, "%s %s", hdev->name, suffix);
  			hi->input->name = name;
  		}
  	}
  
  	return 0;
  }
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
152
153
154
155
  static int uclogic_probe(struct hid_device *hdev,
  		const struct hid_device_id *id)
  {
  	int rc;
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
156
157
  	struct uclogic_drvdata *drvdata = NULL;
  	bool params_initialized = false;
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
158
159
160
161
162
163
164
165
166
  
  	/*
  	 * libinput requires the pad interface to be on a different node
  	 * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
  	 */
  	hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  
  	/* Allocate and assign driver data */
  	drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
167
168
169
170
  	if (drvdata == NULL) {
  		rc = -ENOMEM;
  		goto failure;
  	}
01309e29e   Nikolai Kondrashov   HID: uclogic: Sup...
171
  	timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
8a47670c3   Nikolai Kondrashov   HID: uclogic: Sup...
172
  	drvdata->re_state = U8_MAX;
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
173
  	hid_set_drvdata(hdev, drvdata);
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  	/* Initialize the device and retrieve interface parameters */
  	rc = uclogic_params_init(&drvdata->params, hdev);
  	if (rc != 0) {
  		hid_err(hdev, "failed probing parameters: %d
  ", rc);
  		goto failure;
  	}
  	params_initialized = true;
  	hid_dbg(hdev, "parameters:
  " UCLOGIC_PARAMS_FMT_STR,
  		UCLOGIC_PARAMS_FMT_ARGS(&drvdata->params));
  	if (drvdata->params.invalid) {
  		hid_info(hdev, "interface is invalid, ignoring
  ");
  		rc = -ENODEV;
  		goto failure;
  	}
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
191

9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
192
193
194
195
196
197
198
199
200
201
  	/* Generate replacement report descriptor */
  	rc = uclogic_params_get_desc(&drvdata->params,
  				     &drvdata->desc_ptr,
  				     &drvdata->desc_size);
  	if (rc) {
  		hid_err(hdev,
  			"failed generating replacement report descriptor: %d
  ",
  			rc);
  		goto failure;
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
202
203
204
205
206
207
  	}
  
  	rc = hid_parse(hdev);
  	if (rc) {
  		hid_err(hdev, "parse failed
  ");
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
208
  		goto failure;
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
209
210
211
212
213
214
  	}
  
  	rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  	if (rc) {
  		hid_err(hdev, "hw start failed
  ");
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
215
  		goto failure;
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
216
217
218
  	}
  
  	return 0;
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
219
220
221
222
223
  failure:
  	/* Assume "remove" might not be called if "probe" failed */
  	if (params_initialized)
  		uclogic_params_cleanup(&drvdata->params);
  	return rc;
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
224
  }
251b42756   Nikolai Kondrashov   HID: uclogic: Re-...
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  #ifdef CONFIG_PM
  static int uclogic_resume(struct hid_device *hdev)
  {
  	int rc;
  	struct uclogic_params params;
  
  	/* Re-initialize the device, but discard parameters */
  	rc = uclogic_params_init(&params, hdev);
  	if (rc != 0)
  		hid_err(hdev, "failed to re-initialize the device
  ");
  	else
  		uclogic_params_cleanup(&params);
  
  	return rc;
  }
  #endif
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
242
243
244
  static int uclogic_raw_event(struct hid_device *hdev,
  				struct hid_report *report,
  				u8 *data, int size)
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
245
246
  {
  	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
247
  	struct uclogic_params *params = &drvdata->params;
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
248

9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
249
250
251
252
  	/* Tweak pen reports, if necessary */
  	if (!params->pen_unused &&
  	    (report->type == HID_INPUT_REPORT) &&
  	    (report->id == params->pen.id) &&
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
253
  	    (size >= 2)) {
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
254
255
256
257
258
259
260
261
262
263
  		/* If it's the "virtual" frame controls report */
  		if (params->frame.id != 0 &&
  		    data[1] & params->pen_frame_flag) {
  			/* Change to virtual frame controls report ID */
  			data[0] = params->frame.id;
  			return 0;
  		}
  		/* If in-range reports are inverted */
  		if (params->pen.inrange ==
  			UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
264
265
  			/* Invert the in-range bit */
  			data[1] ^= 0x40;
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
266
  		}
59f2e0fca   Nikolai Kondrashov   HID: uclogic: Sup...
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  		/*
  		 * If report contains fragmented high-resolution pen
  		 * coordinates
  		 */
  		if (size >= 10 && params->pen.fragmented_hires) {
  			u8 pressure_low_byte;
  			u8 pressure_high_byte;
  
  			/* Lift pressure bytes */
  			pressure_low_byte = data[6];
  			pressure_high_byte = data[7];
  			/*
  			 * Move Y coord to make space for high-order X
  			 * coord byte
  			 */
  			data[6] = data[5];
  			data[5] = data[4];
  			/* Move high-order X coord byte */
  			data[4] = data[8];
  			/* Move high-order Y coord byte */
  			data[7] = data[9];
  			/* Place pressure bytes */
  			data[8] = pressure_low_byte;
  			data[9] = pressure_high_byte;
  		}
01309e29e   Nikolai Kondrashov   HID: uclogic: Sup...
292
293
294
295
296
297
298
299
  		/* If we need to emulate in-range detection */
  		if (params->pen.inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
  			/* Set in-range bit */
  			data[1] |= 0x40;
  			/* (Re-)start in-range timeout */
  			mod_timer(&drvdata->inrange_timer,
  					jiffies + msecs_to_jiffies(100));
  		}
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
300
  	}
fde44ac55   Nikolai Kondrashov   HID: uclogic: Sup...
301
302
303
304
305
306
307
308
  	/* Tweak frame control reports, if necessary */
  	if ((report->type == HID_INPUT_REPORT) &&
  	    (report->id == params->frame.id)) {
  		/* If need to, and can, set pad device ID for Wacom drivers */
  		if (params->frame.dev_id_byte > 0 &&
  		    params->frame.dev_id_byte < size) {
  			data[params->frame.dev_id_byte] = 0xf;
  		}
8a47670c3   Nikolai Kondrashov   HID: uclogic: Sup...
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  		/* If need to, and can, read rotary encoder state change */
  		if (params->frame.re_lsb > 0 &&
  		    params->frame.re_lsb / 8 < size) {
  			unsigned int byte = params->frame.re_lsb / 8;
  			unsigned int bit = params->frame.re_lsb % 8;
  
  			u8 change;
  			u8 prev_state = drvdata->re_state;
  			/* Read Gray-coded state */
  			u8 state = (data[byte] >> bit) & 0x3;
  			/* Encode state change into 2-bit signed integer */
  			if ((prev_state == 1 && state == 0) ||
  			    (prev_state == 2 && state == 3)) {
  				change = 1;
  			} else if ((prev_state == 2 && state == 0) ||
  				   (prev_state == 1 && state == 3)) {
  				change = 3;
  			} else {
  				change = 0;
  			}
  			/* Write change */
  			data[byte] = (data[byte] & ~((u8)3 << bit)) |
  					(change << bit);
  			/* Remember state */
  			drvdata->re_state = state;
  		}
fde44ac55   Nikolai Kondrashov   HID: uclogic: Sup...
335
  	}
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
336
337
  	return 0;
  }
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
338
339
340
  static void uclogic_remove(struct hid_device *hdev)
  {
  	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
01309e29e   Nikolai Kondrashov   HID: uclogic: Sup...
341
  	del_timer_sync(&drvdata->inrange_timer);
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
342
343
344
345
  	hid_hw_stop(hdev);
  	kfree(drvdata->desc_ptr);
  	uclogic_params_cleanup(&drvdata->params);
  }
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
  static const struct hid_device_id uclogic_devices[] = {
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  				USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  				USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  				USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  				USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  				USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  				USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  				USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
361
362
  	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
  				USB_DEVICE_ID_HUION_TABLET) },
315ffcc9a   Kyle Godbey   HID: uclogic: Add...
363
364
  	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
  				USB_DEVICE_ID_HUION_HS64) },
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
365
366
367
368
369
370
371
372
373
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  				USB_DEVICE_ID_HUION_TABLET) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  				USB_DEVICE_ID_YIYNOVA_TABLET) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0c15efe9e   Nikolai Kondrashov   HID: uclogic: Add...
374
375
  				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
376
377
378
379
  				USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
  				USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
e902ed934   Nikolai Kondrashov   HID: uclogic: Add...
380
381
  				USB_DEVICE_ID_UGEE_TABLET_G5) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
382
  				USB_DEVICE_ID_UGEE_TABLET_EX07S) },
c3e5a67c4   Nikolai Kondrashov   HID: uclogic: Add...
383
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
88bb346dd   Wang Xuerui   HID: uclogic: Add...
384
385
  				USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
c3e5a67c4   Nikolai Kondrashov   HID: uclogic: Add...
386
  				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
492a9e9a3   Nikolai Kondrashov   HID: uclogic: Add...
387
388
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
08367be17   Nikolai Kondrashov   HID: uclogic: Add...
389
390
  	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
391
392
393
394
395
396
397
398
  	{ }
  };
  MODULE_DEVICE_TABLE(hid, uclogic_devices);
  
  static struct hid_driver uclogic_driver = {
  	.name = "uclogic",
  	.id_table = uclogic_devices,
  	.probe = uclogic_probe,
9614219e9   Nikolai Kondrashov   HID: uclogic: Ext...
399
  	.remove = uclogic_remove,
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
400
401
402
403
  	.report_fixup = uclogic_report_fixup,
  	.raw_event = uclogic_raw_event,
  	.input_mapping = uclogic_input_mapping,
  	.input_configured = uclogic_input_configured,
251b42756   Nikolai Kondrashov   HID: uclogic: Re-...
404
405
406
407
  #ifdef CONFIG_PM
  	.resume	          = uclogic_resume,
  	.reset_resume     = uclogic_resume,
  #endif
ff0c13d6d   Nikolai Kondrashov   HID: uclogic: Ext...
408
409
410
411
412
413
  };
  module_hid_driver(uclogic_driver);
  
  MODULE_AUTHOR("Martin Rusko");
  MODULE_AUTHOR("Nikolai Kondrashov");
  MODULE_LICENSE("GPL");