Merge ../linus
[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-6 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 #include <linux/usb/input.h>
10
11 /* Remote-control poll function - called every dib->rc_query_interval ms to see
12  * whether the remote control has received anything.
13  *
14  * TODO: Fix the repeat rate of the input device.
15  */
16 static void dvb_usb_read_remote_control(struct work_struct *work)
17 {
18         struct dvb_usb_device *d =
19                 container_of(work, struct dvb_usb_device, rc_query_work.work);
20         u32 event;
21         int state;
22
23         /* TODO: need a lock here.  We can simply skip checking for the remote control
24            if we're busy. */
25
26         /* when the parameter has been set to 1 via sysfs while the driver was running */
27         if (dvb_usb_disable_rc_polling)
28                 return;
29
30         if (d->props.rc_query(d,&event,&state)) {
31                 err("error while querying for an remote control event.");
32                 goto schedule;
33         }
34
35
36         switch (state) {
37                 case REMOTE_NO_KEY_PRESSED:
38                         break;
39                 case REMOTE_KEY_PRESSED:
40                         deb_rc("key pressed\n");
41                         d->last_event = event;
42                 case REMOTE_KEY_REPEAT:
43                         deb_rc("key repeated\n");
44                         input_event(d->rc_input_dev, EV_KEY, event, 1);
45                         input_event(d->rc_input_dev, EV_KEY, d->last_event, 0);
46                         input_sync(d->rc_input_dev);
47                         break;
48                 default:
49                         break;
50         }
51
52 /* improved repeat handling ???
53         switch (state) {
54                 case REMOTE_NO_KEY_PRESSED:
55                         deb_rc("NO KEY PRESSED\n");
56                         if (d->last_state != REMOTE_NO_KEY_PRESSED) {
57                                 deb_rc("releasing event %d\n",d->last_event);
58                                 input_event(d->rc_input_dev, EV_KEY, d->last_event, 0);
59                                 input_sync(d->rc_input_dev);
60                         }
61                         d->last_state = REMOTE_NO_KEY_PRESSED;
62                         d->last_event = 0;
63                         break;
64                 case REMOTE_KEY_PRESSED:
65                         deb_rc("KEY PRESSED\n");
66                         deb_rc("pressing event %d\n",event);
67
68                         input_event(d->rc_input_dev, EV_KEY, event, 1);
69                         input_sync(d->rc_input_dev);
70
71                         d->last_event = event;
72                         d->last_state = REMOTE_KEY_PRESSED;
73                         break;
74                 case REMOTE_KEY_REPEAT:
75                         deb_rc("KEY_REPEAT\n");
76                         if (d->last_state != REMOTE_NO_KEY_PRESSED) {
77                                 deb_rc("repeating event %d\n",d->last_event);
78                                 input_event(d->rc_input_dev, EV_KEY, d->last_event, 2);
79                                 input_sync(d->rc_input_dev);
80                                 d->last_state = REMOTE_KEY_REPEAT;
81                         }
82                 default:
83                         break;
84         }
85 */
86
87 schedule:
88         schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
89 }
90
91 int dvb_usb_remote_init(struct dvb_usb_device *d)
92 {
93         struct input_dev *input_dev;
94         int i;
95         int err;
96
97         if (d->props.rc_key_map == NULL ||
98                 d->props.rc_query == NULL ||
99                 dvb_usb_disable_rc_polling)
100                 return 0;
101
102         usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
103         strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
104
105         input_dev = input_allocate_device();
106         if (!input_dev)
107                 return -ENOMEM;
108
109         input_dev->evbit[0] = BIT(EV_KEY);
110         input_dev->keycodesize = sizeof(unsigned char);
111         input_dev->keycodemax = KEY_MAX;
112         input_dev->name = "IR-receiver inside an USB DVB receiver";
113         input_dev->phys = d->rc_phys;
114         usb_to_input_id(d->udev, &input_dev->id);
115         input_dev->cdev.dev = &d->udev->dev;
116
117         /* set the bits for the keys */
118         deb_rc("key map size: %d\n", d->props.rc_key_map_size);
119         for (i = 0; i < d->props.rc_key_map_size; i++) {
120                 deb_rc("setting bit for event %d item %d\n",
121                         d->props.rc_key_map[i].event, i);
122                 set_bit(d->props.rc_key_map[i].event, input_dev->keybit);
123         }
124
125         /* Start the remote-control polling. */
126         if (d->props.rc_interval < 40)
127                 d->props.rc_interval = 100; /* default */
128
129         /* setting these two values to non-zero, we have to manage key repeats */
130         input_dev->rep[REP_PERIOD] = d->props.rc_interval;
131         input_dev->rep[REP_DELAY]  = d->props.rc_interval + 150;
132
133         err = input_register_device(input_dev);
134         if (err) {
135                 input_free_device(input_dev);
136                 return err;
137         }
138
139         d->rc_input_dev = input_dev;
140
141         INIT_DELAYED_WORK(&d->rc_query_work, dvb_usb_read_remote_control);
142
143         info("schedule remote query interval to %d msecs.", d->props.rc_interval);
144         schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
145
146         d->state |= DVB_USB_STATE_REMOTE;
147
148         return 0;
149 }
150
151 int dvb_usb_remote_exit(struct dvb_usb_device *d)
152 {
153         if (d->state & DVB_USB_STATE_REMOTE) {
154                 cancel_delayed_work(&d->rc_query_work);
155                 flush_scheduled_work();
156                 input_unregister_device(d->rc_input_dev);
157         }
158         d->state &= ~DVB_USB_STATE_REMOTE;
159         return 0;
160 }
161
162 #define DVB_USB_RC_NEC_EMPTY           0x00
163 #define DVB_USB_RC_NEC_KEY_PRESSED     0x01
164 #define DVB_USB_RC_NEC_KEY_REPEATED    0x02
165 int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d,
166                 u8 keybuf[5], u32 *event, int *state)
167 {
168         int i;
169         struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
170         *event = 0;
171         *state = REMOTE_NO_KEY_PRESSED;
172         switch (keybuf[0]) {
173                 case DVB_USB_RC_NEC_EMPTY:
174                         break;
175                 case DVB_USB_RC_NEC_KEY_PRESSED:
176                         if ((u8) ~keybuf[1] != keybuf[2] ||
177                                 (u8) ~keybuf[3] != keybuf[4]) {
178                                 deb_err("remote control checksum failed.\n");
179                                 break;
180                         }
181                         /* See if we can match the raw key code. */
182                         for (i = 0; i < d->props.rc_key_map_size; i++)
183                                 if (keymap[i].custom == keybuf[1] &&
184                                         keymap[i].data == keybuf[3]) {
185                                         *event = keymap[i].event;
186                                         *state = REMOTE_KEY_PRESSED;
187                                         return 0;
188                                 }
189                         deb_err("key mapping failed - no appropriate key found in keymapping\n");
190                         break;
191                 case DVB_USB_RC_NEC_KEY_REPEATED:
192                         *state = REMOTE_KEY_REPEAT;
193                         break;
194                 default:
195                         deb_err("unkown type of remote status: %d\n",keybuf[0]);
196                         break;
197         }
198         return 0;
199 }
200 EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);