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