blob: aac1f92731491083db68e0c212dd1292aed67b74 [file] [log] [blame]
Jiri Kosinad0742ab2010-05-17 20:57:02 +02001/*
2* HID driver for zydacron remote control
3*
4* Copyright (c) 2010 Don Prince <dhprince.devel@yahoo.co.uk>
5*/
6
7/*
8* This program is free software; you can redistribute it and/or modify it
9* under the terms of the GNU General Public License as published by the Free
10* Software Foundation; either version 2 of the License, or (at your option)
11* any later version.
12*/
13
14#include <linux/device.h>
15#include <linux/hid.h>
16#include <linux/module.h>
17
18#include "hid-ids.h"
19
20struct zc_device {
21 struct input_dev *input_ep81;
22 unsigned short last_key[4];
23};
24
25
26/*
27* Zydacron remote control has an invalid HID report descriptor,
28* that needs fixing before we can parse it.
29*/
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040030static __u8 *zc_report_fixup(struct hid_device *hdev, __u8 *rdesc,
31 unsigned int *rsize)
Jiri Kosinad0742ab2010-05-17 20:57:02 +020032{
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040033 if (*rsize >= 253 &&
Jiri Kosinad0742ab2010-05-17 20:57:02 +020034 rdesc[0x96] == 0xbc && rdesc[0x97] == 0xff &&
35 rdesc[0xca] == 0xbc && rdesc[0xcb] == 0xff &&
36 rdesc[0xe1] == 0xbc && rdesc[0xe2] == 0xff) {
37 dev_info(&hdev->dev,
38 "fixing up zydacron remote control report "
39 "descriptor\n");
40 rdesc[0x96] = rdesc[0xca] = rdesc[0xe1] = 0x0c;
41 rdesc[0x97] = rdesc[0xcb] = rdesc[0xe2] = 0x00;
42 }
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040043 return rdesc;
Jiri Kosinad0742ab2010-05-17 20:57:02 +020044}
45
46#define zc_map_key_clear(c) \
47 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
48
49static int zc_input_mapping(struct hid_device *hdev, struct hid_input *hi,
50 struct hid_field *field, struct hid_usage *usage,
51 unsigned long **bit, int *max)
52{
53 int i;
54 struct zc_device *zc = hid_get_drvdata(hdev);
55 zc->input_ep81 = hi->input;
56
57 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
58 return 0;
59
60 dbg_hid("zynacron input mapping event [0x%x]\n",
61 usage->hid & HID_USAGE);
62
63 switch (usage->hid & HID_USAGE) {
64 /* report 2 */
65 case 0x10:
66 zc_map_key_clear(KEY_MODE);
67 break;
68 case 0x30:
69 zc_map_key_clear(KEY_SCREEN);
70 break;
71 case 0x70:
72 zc_map_key_clear(KEY_INFO);
73 break;
74 /* report 3 */
75 case 0x04:
76 zc_map_key_clear(KEY_RADIO);
77 break;
78 /* report 4 */
79 case 0x0d:
80 zc_map_key_clear(KEY_PVR);
81 break;
82 case 0x25:
83 zc_map_key_clear(KEY_TV);
84 break;
85 case 0x47:
86 zc_map_key_clear(KEY_AUDIO);
87 break;
88 case 0x49:
89 zc_map_key_clear(KEY_AUX);
90 break;
91 case 0x4a:
92 zc_map_key_clear(KEY_VIDEO);
93 break;
94 case 0x48:
95 zc_map_key_clear(KEY_DVD);
96 break;
97 case 0x24:
98 zc_map_key_clear(KEY_MENU);
99 break;
100 case 0x32:
101 zc_map_key_clear(KEY_TEXT);
102 break;
103 default:
104 return 0;
105 }
106
107 for (i = 0; i < 4; i++)
108 zc->last_key[i] = 0;
109
110 return 1;
111}
112
113static int zc_raw_event(struct hid_device *hdev, struct hid_report *report,
114 u8 *data, int size)
115{
116 struct zc_device *zc = hid_get_drvdata(hdev);
117 int ret = 0;
118 unsigned key;
119 unsigned short index;
120
121 if (report->id == data[0]) {
122
123 /* break keys */
124 for (index = 0; index < 4; index++) {
125 key = zc->last_key[index];
126 if (key) {
127 input_event(zc->input_ep81, EV_KEY, key, 0);
128 zc->last_key[index] = 0;
129 }
130 }
131
132 key = 0;
133 switch (report->id) {
134 case 0x02:
135 case 0x03:
136 switch (data[1]) {
137 case 0x10:
138 key = KEY_MODE;
139 index = 0;
140 break;
141 case 0x30:
142 key = KEY_SCREEN;
143 index = 1;
144 break;
145 case 0x70:
146 key = KEY_INFO;
147 index = 2;
148 break;
149 case 0x04:
150 key = KEY_RADIO;
151 index = 3;
152 break;
153 }
154
155 if (key) {
156 input_event(zc->input_ep81, EV_KEY, key, 1);
157 zc->last_key[index] = key;
158 }
159
160 ret = 1;
161 break;
162 }
163 }
164
165 return ret;
166}
167
168static int zc_probe(struct hid_device *hdev, const struct hid_device_id *id)
169{
170 int ret;
171 struct zc_device *zc;
172
173 zc = kzalloc(sizeof(*zc), GFP_KERNEL);
174 if (zc == NULL) {
175 dev_err(&hdev->dev, "zydacron: can't alloc descriptor\n");
176 return -ENOMEM;
177 }
178
179 hid_set_drvdata(hdev, zc);
180
181 ret = hid_parse(hdev);
182 if (ret) {
183 dev_err(&hdev->dev, "zydacron: parse failed\n");
184 goto err_free;
185 }
186
187 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
188 if (ret) {
189 dev_err(&hdev->dev, "zydacron: hw start failed\n");
190 goto err_free;
191 }
192
193 return 0;
194err_free:
195 kfree(zc);
196
197 return ret;
198}
199
200static void zc_remove(struct hid_device *hdev)
201{
202 struct zc_device *zc = hid_get_drvdata(hdev);
203
204 hid_hw_stop(hdev);
205
206 if (NULL != zc)
207 kfree(zc);
208}
209
210static const struct hid_device_id zc_devices[] = {
211 { HID_USB_DEVICE(USB_VENDOR_ID_ZYDACRON, USB_DEVICE_ID_ZYDACRON_REMOTE_CONTROL) },
212 { }
213};
214MODULE_DEVICE_TABLE(hid, zc_devices);
215
216static struct hid_driver zc_driver = {
217 .name = "zydacron",
218 .id_table = zc_devices,
219 .report_fixup = zc_report_fixup,
220 .input_mapping = zc_input_mapping,
221 .raw_event = zc_raw_event,
222 .probe = zc_probe,
223 .remove = zc_remove,
224};
225
226static int __init zc_init(void)
227{
228 return hid_register_driver(&zc_driver);
229}
230
231static void __exit zc_exit(void)
232{
233 hid_unregister_driver(&zc_driver);
234}
235
236module_init(zc_init);
237module_exit(zc_exit);
238MODULE_LICENSE("GPL");