Merge with rsync://fileserver/linux
[sfrench/cifs-2.6.git] / drivers / media / dvb / dvb-usb / dvb-usb-remote.c
1 /* dvb-usb-remote.c is part of the DVB USB library.
2  *
3  * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
4  * see dvb-usb-init.c for copyright information.
5  *
6  * This file contains functions for initializing the the input-device and for handling remote-control-queries.
7  */
8 #include "dvb-usb-common.h"
9
10 /* Remote-control poll function - called every dib->rc_query_interval ms to see
11  * whether the remote control has received anything.
12  *
13  * TODO: Fix the repeat rate of the input device.
14  */
15 static void dvb_usb_read_remote_control(void *data)
16 {
17         struct dvb_usb_device *d = data;
18         u32 event;
19         int state;
20
21         /* TODO: need a lock here.  We can simply skip checking for the remote control
22            if we're busy. */
23
24         if (d->props.rc_query(d,&event,&state)) {
25                 err("error while querying for an remote control event.");
26                 goto schedule;
27         }
28
29
30         switch (state) {
31                 case REMOTE_NO_KEY_PRESSED:
32                         break;
33                 case REMOTE_KEY_PRESSED:
34                         deb_rc("key pressed\n");
35                         d->last_event = event;
36                 case REMOTE_KEY_REPEAT:
37                         deb_rc("key repeated\n");
38                         input_event(&d->rc_input_dev, EV_KEY, event, 1);
39                         input_event(&d->rc_input_dev, EV_KEY, d->last_event, 0);
40                         input_sync(&d->rc_input_dev);
41                         break;
42                 default:
43                         break;
44         }
45
46 /* improved repeat handling ???
47         switch (state) {
48                 case REMOTE_NO_KEY_PRESSED:
49                         deb_rc("NO KEY PRESSED\n");
50                         if (d->last_state != REMOTE_NO_KEY_PRESSED) {
51                                 deb_rc("releasing event %d\n",d->last_event);
52                                 input_event(&d->rc_input_dev, EV_KEY, d->last_event, 0);
53                                 input_sync(&d->rc_input_dev);
54                         }
55                         d->last_state = REMOTE_NO_KEY_PRESSED;
56                         d->last_event = 0;
57                         break;
58                 case REMOTE_KEY_PRESSED:
59                         deb_rc("KEY PRESSED\n");
60                         deb_rc("pressing event %d\n",event);
61
62                         input_event(&d->rc_input_dev, EV_KEY, event, 1);
63                         input_sync(&d->rc_input_dev);
64
65                         d->last_event = event;
66                         d->last_state = REMOTE_KEY_PRESSED;
67                         break;
68                 case REMOTE_KEY_REPEAT:
69                         deb_rc("KEY_REPEAT\n");
70                         if (d->last_state != REMOTE_NO_KEY_PRESSED) {
71                                 deb_rc("repeating event %d\n",d->last_event);
72                                 input_event(&d->rc_input_dev, EV_KEY, d->last_event, 2);
73                                 input_sync(&d->rc_input_dev);
74                                 d->last_state = REMOTE_KEY_REPEAT;
75                         }
76                 default:
77                         break;
78         }
79 */
80
81 schedule:
82         schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
83 }
84
85 int dvb_usb_remote_init(struct dvb_usb_device *d)
86 {
87         int i;
88         if (d->props.rc_key_map == NULL)
89                 return 0;
90
91         /* Initialise the remote-control structures.*/
92         init_input_dev(&d->rc_input_dev);
93
94         d->rc_input_dev.evbit[0] = BIT(EV_KEY);
95         d->rc_input_dev.keycodesize = sizeof(unsigned char);
96         d->rc_input_dev.keycodemax = KEY_MAX;
97         d->rc_input_dev.name = "IR-receiver inside an USB DVB receiver";
98
99         /* set the bits for the keys */
100         deb_rc("key map size: %d\n",d->props.rc_key_map_size);
101         for (i = 0; i < d->props.rc_key_map_size; i++) {
102                 deb_rc("setting bit for event %d item %d\n",d->props.rc_key_map[i].event, i);
103                 set_bit(d->props.rc_key_map[i].event, d->rc_input_dev.keybit);
104         }
105
106         /* Start the remote-control polling. */
107         if (d->props.rc_interval < 40)
108                 d->props.rc_interval = 100; /* default */
109
110         /* setting these two values to non-zero, we have to manage key repeats */
111         d->rc_input_dev.rep[REP_PERIOD] = d->props.rc_interval;
112         d->rc_input_dev.rep[REP_DELAY]  = d->props.rc_interval + 150;
113
114         input_register_device(&d->rc_input_dev);
115
116         INIT_WORK(&d->rc_query_work, dvb_usb_read_remote_control, d);
117
118         info("schedule remote query interval to %d msecs.",d->props.rc_interval);
119         schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
120
121         d->state |= DVB_USB_STATE_REMOTE;
122
123         return 0;
124 }
125
126 int dvb_usb_remote_exit(struct dvb_usb_device *d)
127 {
128         if (d->state & DVB_USB_STATE_REMOTE) {
129                 cancel_delayed_work(&d->rc_query_work);
130                 flush_scheduled_work();
131                 input_unregister_device(&d->rc_input_dev);
132         }
133         d->state &= ~DVB_USB_STATE_REMOTE;
134         return 0;
135 }
136
137 #define DVB_USB_RC_NEC_EMPTY           0x00
138 #define DVB_USB_RC_NEC_KEY_PRESSED     0x01
139 #define DVB_USB_RC_NEC_KEY_REPEATED    0x02
140 int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d,
141                 u8 keybuf[5], u32 *event, int *state)
142 {
143         int i;
144         struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
145         *event = 0;
146         *state = REMOTE_NO_KEY_PRESSED;
147         switch (keybuf[0]) {
148                 case DVB_USB_RC_NEC_EMPTY:
149                         break;
150                 case DVB_USB_RC_NEC_KEY_PRESSED:
151                         if ((u8) ~keybuf[1] != keybuf[2] ||
152                                 (u8) ~keybuf[3] != keybuf[4]) {
153                                 deb_err("remote control checksum failed.\n");
154                                 break;
155                         }
156                         /* See if we can match the raw key code. */
157                         for (i = 0; i < sizeof(keymap)/sizeof(struct dvb_usb_rc_key); i++)
158                                 if (keymap[i].custom == keybuf[1] &&
159                                         keymap[i].data == keybuf[3]) {
160                                         *event = keymap[i].event;
161                                         *state = REMOTE_KEY_PRESSED;
162                                         break;
163                                 }
164                         deb_err("key mapping failed - no appropriate key found in keymapping\n");
165                         break;
166                 case DVB_USB_RC_NEC_KEY_REPEATED:
167                         *state = REMOTE_KEY_REPEAT;
168                         break;
169                 default:
170                         deb_err("unkown type of remote status: %d\n",keybuf[0]);
171                         break;
172         }
173         return 0;
174 }
175 EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);