Merge master.kernel.org:/pub/scm/linux/kernel/git/tglx/mtd-2.6
[linux-2.6] / 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         /* when the parameter has been set to 1 via sysfs while the driver was running */
25         if (dvb_usb_disable_rc_polling)
26                 return;
27
28         if (d->props.rc_query(d,&event,&state)) {
29                 err("error while querying for an remote control event.");
30                 goto schedule;
31         }
32
33
34         switch (state) {
35                 case REMOTE_NO_KEY_PRESSED:
36                         break;
37                 case REMOTE_KEY_PRESSED:
38                         deb_rc("key pressed\n");
39                         d->last_event = event;
40                 case REMOTE_KEY_REPEAT:
41                         deb_rc("key repeated\n");
42                         input_event(&d->rc_input_dev, EV_KEY, d->last_event, 1);
43                         input_event(&d->rc_input_dev, EV_KEY, d->last_event, 0);
44                         input_sync(&d->rc_input_dev);
45                         break;
46                 default:
47                         break;
48         }
49
50 /* improved repeat handling ???
51         switch (state) {
52                 case REMOTE_NO_KEY_PRESSED:
53                         deb_rc("NO KEY PRESSED\n");
54                         if (d->last_state != REMOTE_NO_KEY_PRESSED) {
55                                 deb_rc("releasing event %d\n",d->last_event);
56                                 input_event(&d->rc_input_dev, EV_KEY, d->last_event, 0);
57                                 input_sync(&d->rc_input_dev);
58                         }
59                         d->last_state = REMOTE_NO_KEY_PRESSED;
60                         d->last_event = 0;
61                         break;
62                 case REMOTE_KEY_PRESSED:
63                         deb_rc("KEY PRESSED\n");
64                         deb_rc("pressing event %d\n",event);
65
66                         input_event(&d->rc_input_dev, EV_KEY, event, 1);
67                         input_sync(&d->rc_input_dev);
68
69                         d->last_event = event;
70                         d->last_state = REMOTE_KEY_PRESSED;
71                         break;
72                 case REMOTE_KEY_REPEAT:
73                         deb_rc("KEY_REPEAT\n");
74                         if (d->last_state != REMOTE_NO_KEY_PRESSED) {
75                                 deb_rc("repeating event %d\n",d->last_event);
76                                 input_event(&d->rc_input_dev, EV_KEY, d->last_event, 2);
77                                 input_sync(&d->rc_input_dev);
78                                 d->last_state = REMOTE_KEY_REPEAT;
79                         }
80                 default:
81                         break;
82         }
83 */
84
85 schedule:
86         schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
87 }
88
89 int dvb_usb_remote_init(struct dvb_usb_device *d)
90 {
91         int i;
92         if (d->props.rc_key_map == NULL ||
93                 d->props.rc_query == NULL ||
94                 dvb_usb_disable_rc_polling)
95                 return 0;
96
97         /* Initialise the remote-control structures.*/
98         init_input_dev(&d->rc_input_dev);
99
100         d->rc_input_dev.evbit[0] = BIT(EV_KEY);
101         d->rc_input_dev.keycodesize = sizeof(unsigned char);
102         d->rc_input_dev.keycodemax = KEY_MAX;
103         d->rc_input_dev.name = "IR-receiver inside an USB DVB receiver";
104
105         /* set the bits for the keys */
106         deb_rc("key map size: %d\n",d->props.rc_key_map_size);
107         for (i = 0; i < d->props.rc_key_map_size; i++) {
108                 deb_rc("setting bit for event %d item %d\n",d->props.rc_key_map[i].event, i);
109                 set_bit(d->props.rc_key_map[i].event, d->rc_input_dev.keybit);
110         }
111
112         /* Start the remote-control polling. */
113         if (d->props.rc_interval < 40)
114                 d->props.rc_interval = 100; /* default */
115
116         /* setting these two values to non-zero, we have to manage key repeats */
117         d->rc_input_dev.rep[REP_PERIOD] = d->props.rc_interval;
118         d->rc_input_dev.rep[REP_DELAY]  = d->props.rc_interval + 150;
119
120         input_register_device(&d->rc_input_dev);
121
122         INIT_WORK(&d->rc_query_work, dvb_usb_read_remote_control, d);
123
124         info("schedule remote query interval to %d msecs.",d->props.rc_interval);
125         schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
126
127         d->state |= DVB_USB_STATE_REMOTE;
128
129         return 0;
130 }
131
132 int dvb_usb_remote_exit(struct dvb_usb_device *d)
133 {
134         if (d->state & DVB_USB_STATE_REMOTE) {
135                 cancel_delayed_work(&d->rc_query_work);
136                 flush_scheduled_work();
137                 input_unregister_device(&d->rc_input_dev);
138         }
139         d->state &= ~DVB_USB_STATE_REMOTE;
140         return 0;
141 }
142
143 #define DVB_USB_RC_NEC_EMPTY           0x00
144 #define DVB_USB_RC_NEC_KEY_PRESSED     0x01
145 #define DVB_USB_RC_NEC_KEY_REPEATED    0x02
146 int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d,
147                 u8 keybuf[5], u32 *event, int *state)
148 {
149         int i;
150         struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
151         *event = 0;
152         *state = REMOTE_NO_KEY_PRESSED;
153         switch (keybuf[0]) {
154                 case DVB_USB_RC_NEC_EMPTY:
155                         break;
156                 case DVB_USB_RC_NEC_KEY_PRESSED:
157                         if ((u8) ~keybuf[1] != keybuf[2] ||
158                                 (u8) ~keybuf[3] != keybuf[4]) {
159                                 deb_err("remote control checksum failed.\n");
160                                 break;
161                         }
162                         /* See if we can match the raw key code. */
163                         for (i = 0; i < d->props.rc_key_map_size; i++)
164                                 if (keymap[i].custom == keybuf[1] &&
165                                         keymap[i].data == keybuf[3]) {
166                                         *event = keymap[i].event;
167                                         *state = REMOTE_KEY_PRESSED;
168                                         return 0;
169                                 }
170                         deb_err("key mapping failed - no appropriate key found in keymapping\n");
171                         break;
172                 case DVB_USB_RC_NEC_KEY_REPEATED:
173                         *state = REMOTE_KEY_REPEAT;
174                         break;
175                 default:
176                         deb_err("unkown type of remote status: %d\n",keybuf[0]);
177                         break;
178         }
179         return 0;
180 }
181 EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);