18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  HID driver for some monterey "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) 2006-2007 Jiri Kosina
98c2ecf20Sopenharmony_ci *  Copyright (c) 2008 Jiri Slaby
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/device.h>
168c2ecf20Sopenharmony_ci#include <linux/hid.h>
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include "hid-ids.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic __u8 *mr_report_fixup(struct hid_device *hdev, __u8 *rdesc,
228c2ecf20Sopenharmony_ci		unsigned int *rsize)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	if (*rsize >= 31 && rdesc[29] == 0x05 && rdesc[30] == 0x09) {
258c2ecf20Sopenharmony_ci		hid_info(hdev, "fixing up button/consumer in HID report descriptor\n");
268c2ecf20Sopenharmony_ci		rdesc[30] = 0x0c;
278c2ecf20Sopenharmony_ci	}
288c2ecf20Sopenharmony_ci	return rdesc;
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define mr_map_key_clear(c)	hid_map_usage_clear(hi, usage, bit, max, \
328c2ecf20Sopenharmony_ci					EV_KEY, (c))
338c2ecf20Sopenharmony_cistatic int mr_input_mapping(struct hid_device *hdev, struct hid_input *hi,
348c2ecf20Sopenharmony_ci		struct hid_field *field, struct hid_usage *usage,
358c2ecf20Sopenharmony_ci		unsigned long **bit, int *max)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
388c2ecf20Sopenharmony_ci		return 0;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	switch (usage->hid & HID_USAGE) {
418c2ecf20Sopenharmony_ci	case 0x156: mr_map_key_clear(KEY_WORDPROCESSOR);	break;
428c2ecf20Sopenharmony_ci	case 0x157: mr_map_key_clear(KEY_SPREADSHEET);		break;
438c2ecf20Sopenharmony_ci	case 0x158: mr_map_key_clear(KEY_PRESENTATION);		break;
448c2ecf20Sopenharmony_ci	case 0x15c: mr_map_key_clear(KEY_STOP);			break;
458c2ecf20Sopenharmony_ci	default:
468c2ecf20Sopenharmony_ci		return 0;
478c2ecf20Sopenharmony_ci	}
488c2ecf20Sopenharmony_ci	return 1;
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic const struct hid_device_id mr_devices[] = {
528c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
538c2ecf20Sopenharmony_ci	{ }
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(hid, mr_devices);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic struct hid_driver mr_driver = {
588c2ecf20Sopenharmony_ci	.name = "monterey",
598c2ecf20Sopenharmony_ci	.id_table = mr_devices,
608c2ecf20Sopenharmony_ci	.report_fixup = mr_report_fixup,
618c2ecf20Sopenharmony_ci	.input_mapping = mr_input_mapping,
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_cimodule_hid_driver(mr_driver);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
66