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