Line 0
Link Here
|
|
|
1 |
/**************************************************************************** |
2 |
** hw_zotac.c ************************************************************* |
3 |
**************************************************************************** |
4 |
* |
5 |
* Lirc driver for Zotac remote |
6 |
* |
7 |
* Copyright (C) 2010 Rainer Hochecker |
8 |
* |
9 |
* Distribute under GPL version 2 or later. |
10 |
* |
11 |
*/ |
12 |
|
13 |
#ifdef HAVE_CONFIG_H |
14 |
# include <config.h> |
15 |
#endif |
16 |
|
17 |
#include <stdio.h> |
18 |
#include <pthread.h> |
19 |
#include <sys/fcntl.h> |
20 |
#include <signal.h> |
21 |
#include <linux/hiddev.h> |
22 |
#include <sys/ioctl.h> |
23 |
#include "hardware.h" |
24 |
#include "ir_remote.h" |
25 |
#include "lircd.h" |
26 |
|
27 |
enum { |
28 |
RPT_NO = 0, |
29 |
RPT_YES = 1, |
30 |
}; |
31 |
|
32 |
static int zotac_init(); |
33 |
static int zotac_deinit(); |
34 |
static char *zotac_rec(struct ir_remote *remotes); |
35 |
static int zotac_decode(struct ir_remote *remote, ir_code * prep, ir_code * codep, ir_code * postp, int *repeat_flagp, |
36 |
lirc_t * min_remaining_gapp, lirc_t * max_remaining_gapp); |
37 |
static void *zotac_repeat(); |
38 |
static int zotac_getcode(); |
39 |
|
40 |
/** Max number of repetitions */ |
41 |
const unsigned max_repeat_count = 500; |
42 |
/** Code that triggers key release */ |
43 |
const unsigned release_code = 0x00000000; |
44 |
/** Code that triggers device remove */ |
45 |
const unsigned remove_code =0x00FFFFFF; |
46 |
/** Time to wait before first repetition */ |
47 |
const unsigned repeat_time1_us = 500000; |
48 |
/** Time to wait between two repetitions */ |
49 |
const unsigned repeat_time2_us = 100000; |
50 |
/** Pipe between main thread and repetition thread */ |
51 |
static int fd_pipe[2] = { -1, -1 }; |
52 |
|
53 |
/** Thread that simulates repetitions */ |
54 |
static pthread_t repeat_thread; |
55 |
/** File descriptor for the real device */ |
56 |
static int fd_hidraw; |
57 |
|
58 |
const int main_code_length = 32; |
59 |
static signed int main_code = 0; |
60 |
static struct timeval start, end, last; |
61 |
static int repeat_state = RPT_NO; |
62 |
static int error_state = 0; |
63 |
static int probe_code = 0; |
64 |
|
65 |
#ifdef HAVE_LINUX_HIDDEV_FLAG_UREF |
66 |
/* Zotac USB iR Receiver */ |
67 |
struct hardware hw_zotac = { |
68 |
"/dev/usb/hiddev0", /* "device" */ |
69 |
-1, /* fd (device) */ |
70 |
LIRC_CAN_REC_LIRCCODE, /* features */ |
71 |
0, /* send_mode */ |
72 |
LIRC_MODE_LIRCCODE, /* rec_mode */ |
73 |
32, /* code_length */ |
74 |
zotac_init, /* init_func */ |
75 |
zotac_deinit, /* deinit_func */ |
76 |
NULL, /* send_func */ |
77 |
zotac_rec, /* rec_func */ |
78 |
zotac_decode, /* decode_func */ |
79 |
NULL, /* ioctl_func */ |
80 |
NULL, /* readdata */ |
81 |
"zotac" /* name */ |
82 |
}; |
83 |
#endif |
84 |
|
85 |
static int zotac_decode(struct ir_remote *remote, ir_code * prep, ir_code * codep, ir_code * postp, int *repeat_flagp, |
86 |
lirc_t * min_remaining_gapp, lirc_t * max_remaining_gapp) |
87 |
{ |
88 |
LOGPRINTF(1, "zotac_decode"); |
89 |
|
90 |
if (!map_code(remote, prep, codep, postp, 0, 0, main_code_length, main_code, 0, 0)) { |
91 |
return 0; |
92 |
} |
93 |
|
94 |
map_gap(remote, &start, &last, 0, repeat_flagp, min_remaining_gapp, max_remaining_gapp); |
95 |
/* override repeat */ |
96 |
*repeat_flagp = repeat_state; |
97 |
|
98 |
return 1; |
99 |
} |
100 |
|
101 |
static int zotac_getcode() { |
102 |
|
103 |
ssize_t rd; |
104 |
struct hiddev_usage_ref uref; |
105 |
struct hiddev_report_info rinfo; |
106 |
struct hiddev_field_info finfo; |
107 |
int shift = 0; |
108 |
|
109 |
rd = read(fd_hidraw, &uref, sizeof(uref)); |
110 |
if (rd < 0) { |
111 |
logprintf(LOG_ERR, "error reading '%s'", hw.device); |
112 |
logperror(LOG_ERR, NULL); |
113 |
zotac_deinit(); |
114 |
error_state = 1; |
115 |
return -1; |
116 |
} |
117 |
|
118 |
if (uref.field_index == HID_FIELD_INDEX_NONE) { |
119 |
/* |
120 |
* we get this when the new report has been send from |
121 |
* device at this point we have the uref structure |
122 |
* prefilled with correct report type and id |
123 |
* |
124 |
*/ |
125 |
|
126 |
switch (uref.report_id) { |
127 |
case 1: /* USB standard keyboard usage page */ |
128 |
{ |
129 |
/* This page reports cursor keys */ |
130 |
LOGPRINTF(3, "Keyboard (standard)\n"); |
131 |
|
132 |
/* check for special codes */ |
133 |
uref.field_index = 0; |
134 |
uref.usage_index = 1; |
135 |
/* fetch the usage code for given indexes */ |
136 |
ioctl(fd_hidraw, HIDIOCGUCODE, &uref, sizeof(uref)); |
137 |
/* fetch the value from report */ |
138 |
ioctl(fd_hidraw, HIDIOCGUSAGE, &uref, sizeof(uref)); |
139 |
|
140 |
if (uref.value) |
141 |
shift = 1; |
142 |
|
143 |
/* populate required field number */ |
144 |
uref.field_index = 1; |
145 |
uref.usage_index = 0; |
146 |
/* fetch the usage code for given indexes */ |
147 |
ioctl(fd_hidraw, HIDIOCGUCODE, &uref, sizeof(uref)); |
148 |
/* fetch the value from report */ |
149 |
ioctl(fd_hidraw, HIDIOCGUSAGE, &uref, sizeof(uref)); |
150 |
/* now we have the key */ |
151 |
|
152 |
LOGPRINTF(3, "usage: %x value: %x shift: %d\n",uref.usage_code, uref.value, shift); |
153 |
|
154 |
/* now we have the key */ |
155 |
if (uref.value) { |
156 |
probe_code = (uref.usage_code | uref.value); |
157 |
if (shift) |
158 |
probe_code |= 0x10000000; |
159 |
LOGPRINTF(3, "Main code 1: %x\n", probe_code); |
160 |
return 1; |
161 |
} |
162 |
else { |
163 |
LOGPRINTF(3, "rel button\n"); |
164 |
probe_code = release_code; |
165 |
return 2; |
166 |
} |
167 |
} |
168 |
break; |
169 |
|
170 |
case 2: |
171 |
case 3: /* USB generic desktop usage page */ |
172 |
case 4: |
173 |
{ |
174 |
/* This page reports power key |
175 |
* (via SystemControl SLEEP) |
176 |
*/ |
177 |
LOGPRINTF(3, "Generic desktop (standard)\n"); |
178 |
|
179 |
|
180 |
/* traverse report descriptor */ |
181 |
rinfo.report_type = HID_REPORT_TYPE_INPUT; |
182 |
rinfo.report_id = HID_REPORT_ID_FIRST; |
183 |
rd = ioctl(fd_hidraw, HIDIOCGREPORTINFO, &rinfo); |
184 |
|
185 |
unsigned int i,j; |
186 |
while (rd >= 0) { |
187 |
for (i = 0; i < rinfo.num_fields; i++) { |
188 |
finfo.report_type = rinfo.report_type; |
189 |
finfo.report_id = rinfo.report_id; |
190 |
finfo.field_index = i; |
191 |
ioctl(fd_hidraw, HIDIOCGFIELDINFO, &finfo); |
192 |
for (j = 0; j < finfo.maxusage; j++) { |
193 |
uref.field_index = i; |
194 |
uref.usage_index = j; |
195 |
ioctl(fd_hidraw, HIDIOCGUCODE, &uref); |
196 |
ioctl(fd_hidraw, HIDIOCGUSAGE, &uref); |
197 |
|
198 |
if (uref.value != 0) { |
199 |
LOGPRINTF(3, "field: %d, idx: %d, usage: %x value: %x\n",i, j, uref.usage_code, uref.value); |
200 |
probe_code = uref.usage_code; |
201 |
return 1; |
202 |
} |
203 |
} |
204 |
} |
205 |
rinfo.report_id |= HID_REPORT_ID_NEXT; |
206 |
rd = ioctl(fd_hidraw, HIDIOCGREPORTINFO, &rinfo); |
207 |
} |
208 |
return 2; |
209 |
} |
210 |
break; |
211 |
default: |
212 |
/* Unknown/unsupported report id. |
213 |
* Should not happen because remaining reports |
214 |
* from report descriptor seem to be unused by remote. |
215 |
*/ |
216 |
logprintf(LOG_ERR, "Unexpected report id %d", uref.report_id); |
217 |
break; |
218 |
} |
219 |
} |
220 |
else { |
221 |
/* This page reports power key |
222 |
* (via SystemControl SLEEP) |
223 |
*/ |
224 |
LOGPRINTF(3, "Same Event ...\n"); |
225 |
|
226 |
/* traverse report descriptor */ |
227 |
rinfo.report_type = HID_REPORT_TYPE_INPUT; |
228 |
rinfo.report_id = HID_REPORT_ID_FIRST; |
229 |
rd = ioctl(fd_hidraw, HIDIOCGREPORTINFO, &rinfo); |
230 |
|
231 |
unsigned int i,j; |
232 |
while (rd >= 0) { |
233 |
for (i = 0; i < rinfo.num_fields; i++) { |
234 |
finfo.report_type = rinfo.report_type; |
235 |
finfo.report_id = rinfo.report_id; |
236 |
finfo.field_index = i; |
237 |
ioctl(fd_hidraw, HIDIOCGFIELDINFO, &finfo); |
238 |
for (j = 0; j < finfo.maxusage; j++) { |
239 |
uref.field_index = i; |
240 |
uref.usage_index = j; |
241 |
ioctl(fd_hidraw, HIDIOCGUCODE, &uref); |
242 |
ioctl(fd_hidraw, HIDIOCGUSAGE, &uref); |
243 |
|
244 |
if (uref.value != 0) { |
245 |
LOGPRINTF(3, "usage: %x value: %x\n",uref.usage_code, uref.value); |
246 |
//probe_code = uref.usage_code; |
247 |
return 0; |
248 |
} |
249 |
} |
250 |
} |
251 |
rinfo.report_id |= HID_REPORT_ID_NEXT; |
252 |
rd = ioctl(fd_hidraw, HIDIOCGREPORTINFO, &rinfo); |
253 |
} |
254 |
return 2; |
255 |
} |
256 |
return 0; |
257 |
} |
258 |
|
259 |
static int zotac_init() |
260 |
{ |
261 |
logprintf(LOG_INFO, "zotac initializing '%s'", hw.device); |
262 |
if ((fd_hidraw = open(hw.device, O_RDONLY)) < 0) { |
263 |
logprintf(LOG_ERR, "unable to open '%s'", hw.device); |
264 |
return 0; |
265 |
} |
266 |
int flags = HIDDEV_FLAG_UREF | HIDDEV_FLAG_REPORT; |
267 |
if (ioctl(fd_hidraw, HIDIOCSFLAG, &flags)) { |
268 |
return 0; |
269 |
} |
270 |
hw.fd = fd_hidraw; |
271 |
|
272 |
/* Create pipe so that events sent by the repeat thread will |
273 |
trigger main thread */ |
274 |
if (pipe(fd_pipe) != 0) { |
275 |
logperror(LOG_ERR, "couldn't open pipe"); |
276 |
close(fd_hidraw); |
277 |
return 0; |
278 |
} |
279 |
hw.fd = fd_pipe[0]; |
280 |
/* Create thread to simulate repetitions */ |
281 |
if (pthread_create(&repeat_thread, NULL, zotac_repeat, NULL)) { |
282 |
logprintf(LOG_ERR, "Could not create \"repeat thread\""); |
283 |
return 0; |
284 |
} |
285 |
return 1; |
286 |
} |
287 |
|
288 |
static int zotac_deinit() |
289 |
{ |
290 |
pthread_cancel(repeat_thread); |
291 |
if (fd_hidraw != -1) { |
292 |
// Close device if it is open |
293 |
logprintf(LOG_INFO, "closing '%s'", hw.device); |
294 |
close(fd_hidraw); |
295 |
fd_hidraw = -1; |
296 |
} |
297 |
// Close pipe input |
298 |
if (fd_pipe[1] >= 0) { |
299 |
close(fd_pipe[1]); |
300 |
fd_pipe[1] = -1; |
301 |
} |
302 |
// Close pipe output |
303 |
if (fd_pipe[0] >= 0) { |
304 |
close(fd_pipe[0]); |
305 |
fd_pipe[0] = -1; |
306 |
} |
307 |
hw.fd = -1; |
308 |
return 1; |
309 |
} |
310 |
|
311 |
/** |
312 |
* Runtime that reads device, forwards codes to main thread |
313 |
* and simulates repetitions. |
314 |
*/ |
315 |
static void *zotac_repeat() |
316 |
{ |
317 |
int repeat_count = 0; |
318 |
unsigned current_code; |
319 |
int ret; |
320 |
int sel; |
321 |
fd_set files; |
322 |
struct timeval delay; |
323 |
int pressed = 0; |
324 |
int fd = fd_pipe[1]; |
325 |
|
326 |
while (1) { |
327 |
// Initialize set to monitor device's events |
328 |
FD_ZERO(&files); |
329 |
FD_SET(fd_hidraw, &files); |
330 |
if (pressed) { |
331 |
sel = select(FD_SETSIZE, &files, NULL, NULL, &delay); |
332 |
} else { |
333 |
sel = select(FD_SETSIZE, &files, NULL, NULL, NULL); |
334 |
} |
335 |
|
336 |
switch (sel) { |
337 |
case 1: |
338 |
// Data ready in device's file |
339 |
ret = zotac_getcode(); |
340 |
|
341 |
if (ret < 0) { |
342 |
// Error |
343 |
logprintf(LOG_ERR, "(%s) Could not read %s", __FUNCTION__, hw.device); |
344 |
goto exit_loop; |
345 |
} |
346 |
if (ret == 1) { |
347 |
// Key code : forward it to main thread |
348 |
pressed = 1; |
349 |
repeat_count = 0; |
350 |
delay.tv_sec = 0; |
351 |
delay.tv_usec = repeat_time1_us; |
352 |
current_code = probe_code; |
353 |
} else if (ret == 2) { |
354 |
// Release code : stop repetitions |
355 |
pressed = 0; |
356 |
current_code = release_code; |
357 |
} else if (ret == 0) { |
358 |
continue; |
359 |
} |
360 |
break; |
361 |
case 0: |
362 |
repeat_count++; |
363 |
if (repeat_count >= max_repeat_count) { |
364 |
// Too many repetitions, something must have gone wrong |
365 |
logprintf(LOG_ERR,"(%s) too many repetitions", __FUNCTION__); |
366 |
goto exit_loop; |
367 |
} |
368 |
// Timeout : send current_code again to main |
369 |
// thread to simulate repetition |
370 |
delay.tv_sec = 0; |
371 |
delay.tv_usec = repeat_time2_us; |
372 |
break; |
373 |
default: |
374 |
// Error |
375 |
logprintf(LOG_ERR, "(%s) select() failed", __FUNCTION__); |
376 |
goto exit_loop; |
377 |
} |
378 |
// Send code to main thread through pipe |
379 |
write(fd, ¤t_code, sizeof(current_code)); |
380 |
} |
381 |
exit_loop: |
382 |
|
383 |
// Wake up main thread with special key code |
384 |
current_code = remove_code; |
385 |
write(fd, ¤t_code, sizeof(current_code)); |
386 |
return NULL; |
387 |
} |
388 |
|
389 |
/* |
390 |
* Aureal Technology ATWF@83 cheap remote |
391 |
* specific code. |
392 |
*/ |
393 |
|
394 |
static char *zotac_rec(struct ir_remote *remotes) |
395 |
{ |
396 |
unsigned ev; |
397 |
int rd; |
398 |
last = end; |
399 |
gettimeofday(&start, NULL); |
400 |
rd = read(hw.fd, &ev, sizeof(ev)); |
401 |
|
402 |
if (rd == -1) { |
403 |
// Error |
404 |
logprintf(LOG_ERR, "(%s) could not read pipe", __FUNCTION__); |
405 |
zotac_deinit(); |
406 |
return 0; |
407 |
} |
408 |
|
409 |
if (ev == release_code) { |
410 |
// Release code |
411 |
main_code = 0; |
412 |
return 0; |
413 |
} else if (ev == remove_code) { |
414 |
// Device has been removed |
415 |
zotac_deinit(); |
416 |
return 0; |
417 |
} |
418 |
|
419 |
LOGPRINTF(1, "zotac : %x", ev); |
420 |
// Record the code and check for repetition |
421 |
if (main_code == ev) { |
422 |
repeat_state = RPT_YES; |
423 |
} else { |
424 |
main_code = ev; |
425 |
repeat_state = RPT_NO; |
426 |
} |
427 |
gettimeofday(&end, NULL); |
428 |
return decode_all(remotes); |
429 |
} |