18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  HID driver for some gyration "special" devices
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (c) 1999 Andreas Gal
68c2ecf20Sopenharmony_ci *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
78c2ecf20Sopenharmony_ci *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
88c2ecf20Sopenharmony_ci *  Copyright (c) 2008 Jiri Slaby
98c2ecf20Sopenharmony_ci *  Copyright (c) 2006-2008 Jiri Kosina
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/device.h>
168c2ecf20Sopenharmony_ci#include <linux/input.h>
178c2ecf20Sopenharmony_ci#include <linux/hid.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include "hid-ids.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define gy_map_key_clear(c)	hid_map_usage_clear(hi, usage, bit, max, \
238c2ecf20Sopenharmony_ci					EV_KEY, (c))
248c2ecf20Sopenharmony_cistatic int gyration_input_mapping(struct hid_device *hdev, struct hid_input *hi,
258c2ecf20Sopenharmony_ci		struct hid_field *field, struct hid_usage *usage,
268c2ecf20Sopenharmony_ci		unsigned long **bit, int *max)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
298c2ecf20Sopenharmony_ci		return 0;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	set_bit(EV_REP, hi->input->evbit);
328c2ecf20Sopenharmony_ci	switch (usage->hid & HID_USAGE) {
338c2ecf20Sopenharmony_ci	/* Reported on Gyration MCE Remote */
348c2ecf20Sopenharmony_ci	case 0x00d: gy_map_key_clear(KEY_HOME);		break;
358c2ecf20Sopenharmony_ci	case 0x024: gy_map_key_clear(KEY_DVD);		break;
368c2ecf20Sopenharmony_ci	case 0x025: gy_map_key_clear(KEY_PVR);		break;
378c2ecf20Sopenharmony_ci	case 0x046: gy_map_key_clear(KEY_MEDIA);	break;
388c2ecf20Sopenharmony_ci	case 0x047: gy_map_key_clear(KEY_MP3);		break;
398c2ecf20Sopenharmony_ci	case 0x048: gy_map_key_clear(KEY_MEDIA);	break;
408c2ecf20Sopenharmony_ci	case 0x049: gy_map_key_clear(KEY_CAMERA);	break;
418c2ecf20Sopenharmony_ci	case 0x04a: gy_map_key_clear(KEY_VIDEO);	break;
428c2ecf20Sopenharmony_ci	case 0x05a: gy_map_key_clear(KEY_TEXT);		break;
438c2ecf20Sopenharmony_ci	case 0x05b: gy_map_key_clear(KEY_RED);		break;
448c2ecf20Sopenharmony_ci	case 0x05c: gy_map_key_clear(KEY_GREEN);	break;
458c2ecf20Sopenharmony_ci	case 0x05d: gy_map_key_clear(KEY_YELLOW);	break;
468c2ecf20Sopenharmony_ci	case 0x05e: gy_map_key_clear(KEY_BLUE);		break;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	default:
498c2ecf20Sopenharmony_ci		return 0;
508c2ecf20Sopenharmony_ci	}
518c2ecf20Sopenharmony_ci	return 1;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic int gyration_event(struct hid_device *hdev, struct hid_field *field,
558c2ecf20Sopenharmony_ci		struct hid_usage *usage, __s32 value)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput)
598c2ecf20Sopenharmony_ci		return 0;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK &&
628c2ecf20Sopenharmony_ci			(usage->hid & 0xff) == 0x82) {
638c2ecf20Sopenharmony_ci		struct input_dev *input = field->hidinput->input;
648c2ecf20Sopenharmony_ci		input_event(input, usage->type, usage->code, 1);
658c2ecf20Sopenharmony_ci		input_sync(input);
668c2ecf20Sopenharmony_ci		input_event(input, usage->type, usage->code, 0);
678c2ecf20Sopenharmony_ci		input_sync(input);
688c2ecf20Sopenharmony_ci		return 1;
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	return 0;
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic const struct hid_device_id gyration_devices[] = {
758c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
768c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
778c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_3) },
788c2ecf20Sopenharmony_ci	{ }
798c2ecf20Sopenharmony_ci};
808c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(hid, gyration_devices);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic struct hid_driver gyration_driver = {
838c2ecf20Sopenharmony_ci	.name = "gyration",
848c2ecf20Sopenharmony_ci	.id_table = gyration_devices,
858c2ecf20Sopenharmony_ci	.input_mapping = gyration_input_mapping,
868c2ecf20Sopenharmony_ci	.event = gyration_event,
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_cimodule_hid_driver(gyration_driver);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
91