xref: /openbmc/linux/drivers/hid/hid-primax.c (revision f6a04605)
1f6a04605STerry Lambert /*
2f6a04605STerry Lambert  * HID driver for primax and similar keyboards with in-band modifiers
3f6a04605STerry Lambert  *
4f6a04605STerry Lambert  * Copyright 2011 Google Inc. All Rights Reserved
5f6a04605STerry Lambert  *
6f6a04605STerry Lambert  * Author:
7f6a04605STerry Lambert  *	Terry Lambert <tlambert@google.com>
8f6a04605STerry Lambert  *
9f6a04605STerry Lambert  * This software is licensed under the terms of the GNU General Public
10f6a04605STerry Lambert  * License version 2, as published by the Free Software Foundation, and
11f6a04605STerry Lambert  * may be copied, distributed, and modified under those terms.
12f6a04605STerry Lambert  *
13f6a04605STerry Lambert  * This program is distributed in the hope that it will be useful,
14f6a04605STerry Lambert  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15f6a04605STerry Lambert  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16f6a04605STerry Lambert  * GNU General Public License for more details.
17f6a04605STerry Lambert  */
18f6a04605STerry Lambert 
19f6a04605STerry Lambert #include <linux/device.h>
20f6a04605STerry Lambert #include <linux/hid.h>
21f6a04605STerry Lambert #include <linux/module.h>
22f6a04605STerry Lambert 
23f6a04605STerry Lambert #include "hid-ids.h"
24f6a04605STerry Lambert 
25f6a04605STerry Lambert static int px_raw_event(struct hid_device *hid, struct hid_report *report,
26f6a04605STerry Lambert 	 u8 *data, int size)
27f6a04605STerry Lambert {
28f6a04605STerry Lambert 	int idx = size;
29f6a04605STerry Lambert 
30f6a04605STerry Lambert 	switch (report->id) {
31f6a04605STerry Lambert 	case 0:		/* keyboard input */
32f6a04605STerry Lambert 		/*
33f6a04605STerry Lambert 		 * Convert in-band modifier key values into out of band
34f6a04605STerry Lambert 		 * modifier bits and pull the key strokes from the report.
35f6a04605STerry Lambert 		 * Thus a report data set which looked like:
36f6a04605STerry Lambert 		 *
37f6a04605STerry Lambert 		 * [00][00][E0][30][00][00][00][00]
38f6a04605STerry Lambert 		 * (no modifier bits + "Left Shift" key + "1" key)
39f6a04605STerry Lambert 		 *
40f6a04605STerry Lambert 		 * Would be converted to:
41f6a04605STerry Lambert 		 *
42f6a04605STerry Lambert 		 * [01][00][00][30][00][00][00][00]
43f6a04605STerry Lambert 		 * (Left Shift modifier bit + "1" key)
44f6a04605STerry Lambert 		 *
45f6a04605STerry Lambert 		 * As long as it's in the size range, the upper level
46f6a04605STerry Lambert 		 * drivers don't particularly care if there are in-band
47f6a04605STerry Lambert 		 * 0-valued keys, so they don't stop parsing.
48f6a04605STerry Lambert 		 */
49f6a04605STerry Lambert 		while (--idx > 1) {
50f6a04605STerry Lambert 			if (data[idx] < 0xE0 || data[idx] > 0xE7)
51f6a04605STerry Lambert 				continue;
52f6a04605STerry Lambert 			data[0] |= (1 << (data[idx] - 0xE0));
53f6a04605STerry Lambert 			data[idx] = 0;
54f6a04605STerry Lambert 		}
55f6a04605STerry Lambert 		hid_report_raw_event(hid, HID_INPUT_REPORT, data, size, 0);
56f6a04605STerry Lambert 		return 1;
57f6a04605STerry Lambert 
58f6a04605STerry Lambert 	default:	/* unknown report */
59f6a04605STerry Lambert 		/* Unknown report type; pass upstream */
60f6a04605STerry Lambert 		hid_info(hid, "unknown report type %d\n", report->id);
61f6a04605STerry Lambert 		break;
62f6a04605STerry Lambert 	}
63f6a04605STerry Lambert 
64f6a04605STerry Lambert 	return 0;
65f6a04605STerry Lambert }
66f6a04605STerry Lambert 
67f6a04605STerry Lambert static int px_probe(struct hid_device *hid, const struct hid_device_id *id)
68f6a04605STerry Lambert {
69f6a04605STerry Lambert 	int ret;
70f6a04605STerry Lambert 
71f6a04605STerry Lambert 	ret = hid_parse(hid);
72f6a04605STerry Lambert 	if (ret) {
73f6a04605STerry Lambert 		hid_err(hid, "parse failed\n");
74f6a04605STerry Lambert 		goto fail;
75f6a04605STerry Lambert 	}
76f6a04605STerry Lambert 
77f6a04605STerry Lambert 	ret = hid_hw_start(hid, HID_CONNECT_DEFAULT);
78f6a04605STerry Lambert 	if (ret)
79f6a04605STerry Lambert 		hid_err(hid, "hw start failed\n");
80f6a04605STerry Lambert 
81f6a04605STerry Lambert fail:
82f6a04605STerry Lambert 	return ret;
83f6a04605STerry Lambert }
84f6a04605STerry Lambert 
85f6a04605STerry Lambert static void px_remove(struct hid_device *hid)
86f6a04605STerry Lambert {
87f6a04605STerry Lambert 	hid_hw_stop(hid);
88f6a04605STerry Lambert }
89f6a04605STerry Lambert 
90f6a04605STerry Lambert static const struct hid_device_id px_devices[] = {
91f6a04605STerry Lambert 	{ HID_USB_DEVICE(USB_VENDOR_ID_PRIMAX, USB_DEVICE_ID_PRIMAX_KEYBOARD) },
92f6a04605STerry Lambert 	{ }
93f6a04605STerry Lambert };
94f6a04605STerry Lambert MODULE_DEVICE_TABLE(hid, px_devices);
95f6a04605STerry Lambert 
96f6a04605STerry Lambert static struct hid_driver px_driver = {
97f6a04605STerry Lambert 	.name = "primax",
98f6a04605STerry Lambert 	.id_table = px_devices,
99f6a04605STerry Lambert 	.raw_event = px_raw_event,
100f6a04605STerry Lambert 	.probe = px_probe,
101f6a04605STerry Lambert 	.remove = px_remove,
102f6a04605STerry Lambert };
103f6a04605STerry Lambert 
104f6a04605STerry Lambert static int __init px_init(void)
105f6a04605STerry Lambert {
106f6a04605STerry Lambert 	return hid_register_driver(&px_driver);
107f6a04605STerry Lambert }
108f6a04605STerry Lambert 
109f6a04605STerry Lambert static void __exit px_exit(void)
110f6a04605STerry Lambert {
111f6a04605STerry Lambert 	hid_unregister_driver(&px_driver);
112f6a04605STerry Lambert }
113f6a04605STerry Lambert 
114f6a04605STerry Lambert module_init(px_init);
115f6a04605STerry Lambert module_exit(px_exit);
116f6a04605STerry Lambert MODULE_AUTHOR("Terry Lambert <tlambert@google.com>");
117f6a04605STerry Lambert MODULE_LICENSE("GPL");
118