xref: /linux/drivers/hid/hid-haptic.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  HID Haptic support for Linux
4  *
5  *  Copyright (c) 2021 Angela Czubak <acz@semihalf.com>
6  */
7 
8 #include <linux/input/mt.h>
9 #include <linux/module.h>
10 
11 #include "hid-haptic.h"
12 
13 void hid_haptic_feature_mapping(struct hid_device *hdev,
14 				struct hid_haptic_device *haptic,
15 				struct hid_field *field, struct hid_usage *usage)
16 {
17 	u16 usage_hid;
18 
19 	if (usage->hid == HID_HP_AUTOTRIGGER) {
20 		if (usage->usage_index >= field->report_count) {
21 			dev_err(&hdev->dev,
22 				"HID_HP_AUTOTRIGGER out of range\n");
23 			return;
24 		}
25 
26 		hid_device_io_start(hdev);
27 		hid_hw_request(hdev, field->report, HID_REQ_GET_REPORT);
28 		hid_hw_wait(hdev);
29 		hid_device_io_stop(hdev);
30 		haptic->default_auto_trigger =
31 			field->value[usage->usage_index];
32 		haptic->auto_trigger_report = field->report;
33 	} else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_ORDINAL) {
34 		usage_hid = usage->hid & HID_USAGE;
35 		switch (field->logical) {
36 		case HID_HP_WAVEFORMLIST:
37 			if (usage_hid > haptic->max_waveform_id)
38 				haptic->max_waveform_id = usage_hid;
39 			break;
40 		case HID_HP_DURATIONLIST:
41 			if (usage_hid > haptic->max_duration_id)
42 				haptic->max_duration_id = usage_hid;
43 			break;
44 		default:
45 			break;
46 		}
47 	}
48 }
49 EXPORT_SYMBOL_GPL(hid_haptic_feature_mapping);
50 
51 bool hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
52 				    struct hid_input *hi, struct hid_field *field)
53 {
54 	if (field->unit == HID_UNIT_GRAM || field->unit == HID_UNIT_NEWTON) {
55 		haptic->force_logical_minimum = field->logical_minimum;
56 		haptic->force_physical_minimum = field->physical_minimum;
57 		haptic->force_resolution = input_abs_get_res(hi->input,
58 							     ABS_MT_PRESSURE);
59 		return true;
60 	}
61 	return false;
62 }
63 EXPORT_SYMBOL_GPL(hid_haptic_check_pressure_unit);
64 
65 int hid_haptic_input_mapping(struct hid_device *hdev,
66 			     struct hid_haptic_device *haptic,
67 			     struct hid_input *hi,
68 			     struct hid_field *field, struct hid_usage *usage,
69 			     unsigned long **bit, int *max)
70 {
71 	if (usage->hid == HID_HP_MANUALTRIGGER) {
72 		haptic->manual_trigger_report = field->report;
73 		/* we don't really want to map these fields */
74 		return -1;
75 	}
76 
77 	return 0;
78 }
79 EXPORT_SYMBOL_GPL(hid_haptic_input_mapping);
80 
81 int hid_haptic_input_configured(struct hid_device *hdev,
82 				struct hid_haptic_device *haptic,
83 				struct hid_input *hi)
84 {
85 	int error;
86 
87 	if (hi->application != HID_DG_TOUCHPAD)
88 		return -1;
89 
90 	if (!haptic->auto_trigger_report || !haptic->manual_trigger_report)
91 		return 0;
92 
93 	__set_bit(INPUT_PROP_PRESSUREPAD, hi->input->propbit);
94 
95 	error = hid_haptic_init(hdev, haptic, hi->input);
96 	if (error) {
97 		dev_warn(&hdev->dev, "Cannot allocate haptic for %s\n",
98 			 hdev->name);
99 		return 0;
100 	}
101 
102 	return 1;
103 }
104 EXPORT_SYMBOL_GPL(hid_haptic_input_configured);
105 
106 static void parse_auto_trigger_field(struct hid_haptic_device *haptic,
107 				     struct hid_field *field)
108 {
109 	int count = field->report_count;
110 	int n;
111 	u16 usage_hid;
112 
113 	for (n = 0; n < count; n++) {
114 		switch (field->usage[n].hid & HID_USAGE_PAGE) {
115 		case HID_UP_ORDINAL:
116 			usage_hid = field->usage[n].hid & HID_USAGE;
117 			switch (field->logical) {
118 			case HID_HP_WAVEFORMLIST:
119 				haptic->hid_usage_map[usage_hid] = field->value[n];
120 				if (field->value[n] ==
121 				    (HID_HP_WAVEFORMPRESS & HID_USAGE)) {
122 					haptic->press_ordinal = usage_hid;
123 				} else if (field->value[n] ==
124 					   (HID_HP_WAVEFORMRELEASE & HID_USAGE)) {
125 					haptic->release_ordinal = usage_hid;
126 				}
127 				break;
128 			case HID_HP_DURATIONLIST:
129 				haptic->duration_map[usage_hid] =
130 					field->value[n];
131 				break;
132 			default:
133 				break;
134 			}
135 			break;
136 		case HID_UP_HAPTIC:
137 			switch (field->usage[n].hid) {
138 			case HID_HP_WAVEFORMVENDORID:
139 				haptic->vendor_id = field->value[n];
140 				break;
141 			case HID_HP_WAVEFORMVENDORPAGE:
142 				haptic->vendor_page = field->value[n];
143 				break;
144 			default:
145 				break;
146 			}
147 			break;
148 		default:
149 			/* Should not really happen */
150 			break;
151 		}
152 	}
153 }
154 
155 static void fill_effect_buf(struct hid_haptic_device *haptic,
156 			    struct ff_haptic_effect *effect,
157 			    struct hid_haptic_effect *haptic_effect,
158 			    int waveform_ordinal)
159 {
160 	struct hid_report *rep = haptic->manual_trigger_report;
161 	struct hid_usage *usage;
162 	struct hid_field *field;
163 	s32 value;
164 	int i, j;
165 	u8 *buf = haptic_effect->report_buf;
166 
167 	mutex_lock(&haptic->manual_trigger_mutex);
168 	for (i = 0; i < rep->maxfield; i++) {
169 		field = rep->field[i];
170 		/* Ignore if report count is out of bounds. */
171 		if (field->report_count < 1)
172 			continue;
173 
174 		for (j = 0; j < field->maxusage; j++) {
175 			usage = &field->usage[j];
176 
177 			switch (usage->hid) {
178 			case HID_HP_INTENSITY:
179 				if (effect->intensity > 100) {
180 					value = field->logical_maximum;
181 				} else {
182 					value = field->logical_minimum +
183 						effect->intensity *
184 						(field->logical_maximum -
185 						 field->logical_minimum) / 100;
186 				}
187 				break;
188 			case HID_HP_REPEATCOUNT:
189 				value = effect->repeat_count;
190 				break;
191 			case HID_HP_RETRIGGERPERIOD:
192 				value = effect->retrigger_period;
193 				break;
194 			case HID_HP_MANUALTRIGGER:
195 				value = waveform_ordinal;
196 				break;
197 			default:
198 				continue;
199 			}
200 
201 			field->value[j] = value;
202 		}
203 	}
204 
205 	hid_output_report(rep, buf);
206 	mutex_unlock(&haptic->manual_trigger_mutex);
207 }
208 
209 static void switch_mode(struct hid_device *hdev, struct hid_haptic_device *haptic,
210 			int mode)
211 {
212 	struct hid_report *rep = haptic->auto_trigger_report;
213 	struct hid_field *field;
214 	s32 value;
215 	int i, j;
216 
217 	if (mode == HID_HAPTIC_MODE_HOST)
218 		value = HID_HAPTIC_ORDINAL_WAVEFORMSTOP;
219 	else
220 		value = haptic->default_auto_trigger;
221 
222 	mutex_lock(&haptic->auto_trigger_mutex);
223 	for (i = 0; i < rep->maxfield; i++) {
224 		field = rep->field[i];
225 		/* Ignore if report count is out of bounds. */
226 		if (field->report_count < 1)
227 			continue;
228 
229 		for (j = 0; j < field->maxusage; j++) {
230 			if (field->usage[j].hid == HID_HP_AUTOTRIGGER)
231 				field->value[j] = value;
232 		}
233 	}
234 
235 	/* send the report */
236 	hid_hw_request(hdev, rep, HID_REQ_SET_REPORT);
237 	mutex_unlock(&haptic->auto_trigger_mutex);
238 	haptic->mode = mode;
239 }
240 
241 static int hid_haptic_upload_effect(struct input_dev *dev, struct ff_effect *effect,
242 				    struct ff_effect *old)
243 {
244 	struct hid_device *hdev = input_get_drvdata(dev);
245 	struct ff_device *ff = dev->ff;
246 	struct hid_haptic_device *haptic = ff->private;
247 	int i, ordinal = 0;
248 	bool switch_modes = false;
249 
250 	/* If vendor range, check vendor id and page */
251 	if (effect->u.haptic.hid_usage >= (HID_HP_VENDORWAVEFORMMIN & HID_USAGE) &&
252 	    effect->u.haptic.hid_usage <= (HID_HP_VENDORWAVEFORMMAX & HID_USAGE) &&
253 	    (effect->u.haptic.vendor_id != haptic->vendor_id ||
254 	     effect->u.haptic.vendor_waveform_page != haptic->vendor_page))
255 		return -EINVAL;
256 
257 	/* Check hid_usage */
258 	for (i = 1; i <= haptic->max_waveform_id; i++) {
259 		if (haptic->hid_usage_map[i] == effect->u.haptic.hid_usage) {
260 			ordinal = i;
261 			break;
262 		}
263 	}
264 	if (ordinal < 1)
265 		return -EINVAL;
266 
267 	/* Fill the buffer for the effect id */
268 	fill_effect_buf(haptic, &effect->u.haptic, &haptic->effect[effect->id],
269 			ordinal);
270 
271 	if (effect->u.haptic.hid_usage == (HID_HP_WAVEFORMPRESS & HID_USAGE) ||
272 			effect->u.haptic.hid_usage == (HID_HP_WAVEFORMRELEASE & HID_USAGE))
273 		switch_modes = true;
274 
275 	/* If device is in autonomous mode, and the uploaded effect signals userspace
276 	 * wants control of the device, change modes
277 	 */
278 	if (switch_modes && haptic->mode == HID_HAPTIC_MODE_DEVICE)
279 		switch_mode(hdev, haptic, HID_HAPTIC_MODE_HOST);
280 
281 	return 0;
282 }
283 
284 static int play_effect(struct hid_device *hdev, struct hid_haptic_device *haptic,
285 		       struct hid_haptic_effect *effect)
286 {
287 	int ret;
288 
289 	ret = hid_hw_output_report(hdev, effect->report_buf,
290 				   haptic->manual_trigger_report_len);
291 	if (ret < 0) {
292 		ret = hid_hw_raw_request(hdev,
293 					 haptic->manual_trigger_report->id,
294 					 effect->report_buf,
295 					 haptic->manual_trigger_report_len,
296 					 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
297 	}
298 
299 	return ret;
300 }
301 
302 static void haptic_work_handler(struct work_struct *work)
303 {
304 
305 	struct hid_haptic_effect *effect = container_of(work,
306 							struct hid_haptic_effect,
307 							work);
308 	struct input_dev *dev = effect->input_dev;
309 	struct hid_device *hdev = input_get_drvdata(dev);
310 	struct hid_haptic_device *haptic = dev->ff->private;
311 
312 	mutex_lock(&haptic->manual_trigger_mutex);
313 	if (effect != &haptic->stop_effect)
314 		play_effect(hdev, haptic, &haptic->stop_effect);
315 
316 	play_effect(hdev, haptic, effect);
317 	mutex_unlock(&haptic->manual_trigger_mutex);
318 
319 }
320 
321 static int hid_haptic_playback(struct input_dev *dev, int effect_id, int value)
322 {
323 	struct hid_haptic_device *haptic = dev->ff->private;
324 
325 	if (value)
326 		queue_work(haptic->wq, &haptic->effect[effect_id].work);
327 	else
328 		queue_work(haptic->wq, &haptic->stop_effect.work);
329 
330 	return 0;
331 }
332 
333 static void effect_set_default(struct ff_effect *effect)
334 {
335 	effect->type = FF_HAPTIC;
336 	effect->id = -1;
337 	effect->u.haptic.hid_usage = HID_HP_WAVEFORMNONE & HID_USAGE;
338 	effect->u.haptic.intensity = 100;
339 	effect->u.haptic.retrigger_period = 0;
340 	effect->u.haptic.repeat_count = 0;
341 }
342 
343 static int hid_haptic_erase(struct input_dev *dev, int effect_id)
344 {
345 	struct hid_haptic_device *haptic = dev->ff->private;
346 	struct hid_device *hdev = input_get_drvdata(dev);
347 	struct ff_effect effect;
348 	int ordinal;
349 
350 	effect_set_default(&effect);
351 
352 	if (effect.u.haptic.hid_usage == (HID_HP_WAVEFORMRELEASE & HID_USAGE)) {
353 		ordinal = haptic->release_ordinal;
354 		if (!ordinal) {
355 			ordinal = HID_HAPTIC_ORDINAL_WAVEFORMNONE;
356 			if (haptic->mode == HID_HAPTIC_MODE_HOST)
357 				switch_mode(hdev, haptic, HID_HAPTIC_MODE_DEVICE);
358 		} else
359 			effect.u.haptic.hid_usage = HID_HP_WAVEFORMRELEASE & HID_USAGE;
360 
361 		fill_effect_buf(haptic, &effect.u.haptic, &haptic->effect[effect_id],
362 				ordinal);
363 	} else if (effect.u.haptic.hid_usage == (HID_HP_WAVEFORMPRESS & HID_USAGE)) {
364 		ordinal = haptic->press_ordinal;
365 		if (!ordinal) {
366 			ordinal = HID_HAPTIC_ORDINAL_WAVEFORMNONE;
367 			if (haptic->mode == HID_HAPTIC_MODE_HOST)
368 				switch_mode(hdev, haptic, HID_HAPTIC_MODE_DEVICE);
369 		}
370 		else
371 			effect.u.haptic.hid_usage = HID_HP_WAVEFORMPRESS & HID_USAGE;
372 
373 		fill_effect_buf(haptic, &effect.u.haptic, &haptic->effect[effect_id],
374 				ordinal);
375 	}
376 
377 	return 0;
378 }
379 
380 static void hid_haptic_destroy(struct ff_device *ff)
381 {
382 	struct hid_haptic_device *haptic = ff->private;
383 	struct hid_device *hdev = haptic->hdev;
384 	int r;
385 
386 	if (hdev)
387 		put_device(&hdev->dev);
388 
389 	kfree(haptic->stop_effect.report_buf);
390 	haptic->stop_effect.report_buf = NULL;
391 
392 	if (haptic->effect) {
393 		for (r = 0; r < ff->max_effects; r++)
394 			kfree(haptic->effect[r].report_buf);
395 		kfree(haptic->effect);
396 	}
397 	haptic->effect = NULL;
398 
399 	destroy_workqueue(haptic->wq);
400 	haptic->wq = NULL;
401 
402 	kfree(haptic->duration_map);
403 	haptic->duration_map = NULL;
404 
405 	kfree(haptic->hid_usage_map);
406 	haptic->hid_usage_map = NULL;
407 
408 	module_put(THIS_MODULE);
409 }
410 
411 int hid_haptic_init(struct hid_device *hdev,
412 		    struct hid_haptic_device *haptic,
413 		    struct input_dev *dev)
414 {
415 	struct ff_device *ff;
416 	int ret = 0, r;
417 	struct ff_haptic_effect stop_effect = {
418 		.hid_usage = HID_HP_WAVEFORMSTOP & HID_USAGE,
419 	};
420 	const char *prefix = "hid-haptic";
421 	char *name;
422 	int (*flush)(struct input_dev *dev, struct file *file);
423 	int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
424 
425 	haptic->hdev = hdev;
426 	haptic->max_waveform_id = max(2u, haptic->max_waveform_id);
427 	haptic->max_duration_id = max(2u, haptic->max_duration_id);
428 
429 	haptic->hid_usage_map = kcalloc(haptic->max_waveform_id + 1,
430 					sizeof(u16), GFP_KERNEL);
431 	if (!haptic->hid_usage_map) {
432 		ret = -ENOMEM;
433 		goto exit;
434 	}
435 	haptic->duration_map = kcalloc(haptic->max_duration_id + 1,
436 				       sizeof(u32), GFP_KERNEL);
437 	if (!haptic->duration_map) {
438 		ret = -ENOMEM;
439 		goto usage_map;
440 	}
441 
442 	if (haptic->max_waveform_id != haptic->max_duration_id)
443 		dev_warn(&hdev->dev,
444 			 "Haptic duration and waveform lists have different max id (%u and %u).\n",
445 			 haptic->max_duration_id, haptic->max_waveform_id);
446 
447 	haptic->hid_usage_map[HID_HAPTIC_ORDINAL_WAVEFORMNONE] =
448 		HID_HP_WAVEFORMNONE & HID_USAGE;
449 	haptic->hid_usage_map[HID_HAPTIC_ORDINAL_WAVEFORMSTOP] =
450 		HID_HP_WAVEFORMSTOP & HID_USAGE;
451 
452 	mutex_init(&haptic->auto_trigger_mutex);
453 	for (r = 0; r < haptic->auto_trigger_report->maxfield; r++)
454 		parse_auto_trigger_field(haptic, haptic->auto_trigger_report->field[r]);
455 
456 	haptic->input_dev = dev;
457 	haptic->manual_trigger_report_len =
458 		hid_report_len(haptic->manual_trigger_report);
459 	mutex_init(&haptic->manual_trigger_mutex);
460 	name = kmalloc(strlen(prefix) + strlen(hdev->name) + 2, GFP_KERNEL);
461 	if (name) {
462 		sprintf(name, "%s %s", prefix, hdev->name);
463 		haptic->wq = create_singlethread_workqueue(name);
464 		kfree(name);
465 	}
466 	if (!haptic->wq) {
467 		ret = -ENOMEM;
468 		goto duration_map;
469 	}
470 	haptic->effect = kzalloc_objs(struct hid_haptic_effect, FF_MAX_EFFECTS);
471 	if (!haptic->effect) {
472 		ret = -ENOMEM;
473 		goto output_queue;
474 	}
475 	for (r = 0; r < FF_MAX_EFFECTS; r++) {
476 		haptic->effect[r].report_buf =
477 			hid_alloc_report_buf(haptic->manual_trigger_report,
478 					     GFP_KERNEL);
479 		if (!haptic->effect[r].report_buf) {
480 			dev_err(&hdev->dev,
481 				"Failed to allocate a buffer for an effect.\n");
482 			ret = -ENOMEM;
483 			goto buffer_free;
484 		}
485 		haptic->effect[r].input_dev = dev;
486 		INIT_WORK(&haptic->effect[r].work, haptic_work_handler);
487 	}
488 	haptic->stop_effect.report_buf =
489 		hid_alloc_report_buf(haptic->manual_trigger_report,
490 				     GFP_KERNEL);
491 	if (!haptic->stop_effect.report_buf) {
492 		dev_err(&hdev->dev,
493 			"Failed to allocate a buffer for stop effect.\n");
494 		ret = -ENOMEM;
495 		goto buffer_free;
496 	}
497 	haptic->stop_effect.input_dev = dev;
498 	INIT_WORK(&haptic->stop_effect.work, haptic_work_handler);
499 	fill_effect_buf(haptic, &stop_effect, &haptic->stop_effect,
500 			HID_HAPTIC_ORDINAL_WAVEFORMSTOP);
501 
502 	input_set_capability(dev, EV_FF, FF_HAPTIC);
503 
504 	flush = dev->flush;
505 	event = dev->event;
506 	ret = input_ff_create(dev, FF_MAX_EFFECTS);
507 	if (ret) {
508 		dev_err(&hdev->dev, "Failed to create ff device.\n");
509 		goto stop_buffer_free;
510 	}
511 
512 	ff = dev->ff;
513 	ff->private = haptic;
514 	ff->upload = hid_haptic_upload_effect;
515 	ff->playback = hid_haptic_playback;
516 	ff->erase = hid_haptic_erase;
517 	ff->destroy = hid_haptic_destroy;
518 	if (!try_module_get(THIS_MODULE)) {
519 		dev_err(&hdev->dev, "Failed to increase module count.\n");
520 		goto input_free;
521 	}
522 	if (!get_device(&hdev->dev)) {
523 		dev_err(&hdev->dev, "Failed to get hdev device.\n");
524 		module_put(THIS_MODULE);
525 		goto input_free;
526 	}
527 	return 0;
528 
529 input_free:
530 	input_ff_destroy(dev);
531 	/* Restore dev flush and event */
532 	dev->flush = flush;
533 	dev->event = event;
534 	return ret;
535 stop_buffer_free:
536 	kfree(haptic->stop_effect.report_buf);
537 	haptic->stop_effect.report_buf = NULL;
538 buffer_free:
539 	while (--r >= 0)
540 		kfree(haptic->effect[r].report_buf);
541 	kfree(haptic->effect);
542 	haptic->effect = NULL;
543 output_queue:
544 	destroy_workqueue(haptic->wq);
545 	haptic->wq = NULL;
546 duration_map:
547 	kfree(haptic->duration_map);
548 	haptic->duration_map = NULL;
549 usage_map:
550 	kfree(haptic->hid_usage_map);
551 	haptic->hid_usage_map = NULL;
552 exit:
553 	return ret;
554 }
555 EXPORT_SYMBOL_GPL(hid_haptic_init);
556 
557 void hid_haptic_pressure_reset(struct hid_haptic_device *haptic)
558 {
559 	haptic->pressure_sum = 0;
560 }
561 EXPORT_SYMBOL_GPL(hid_haptic_pressure_reset);
562 
563 void hid_haptic_pressure_increase(struct hid_haptic_device *haptic,
564 				 __s32 pressure)
565 {
566 	haptic->pressure_sum += pressure;
567 }
568 EXPORT_SYMBOL_GPL(hid_haptic_pressure_increase);
569