Merge branch 'topic/sscape-fix' into for-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 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_MASK(EV_KEY);
110         input_dev->name = "IR-receiver inside an USB DVB receiver";
111         input_dev->phys = d->rc_phys;
112         usb_to_input_id(d->udev, &input_dev->id);
113         input_dev->dev.parent = &d->udev->dev;
114
115         /* set the bits for the keys */
116         deb_rc("key map size: %d\n", d->props.rc_key_map_size);
117         for (i = 0; i < d->props.rc_key_map_size; i++) {
118                 deb_rc("setting bit for event %d item %d\n",
119                         d->props.rc_key_map[i].event, i);
120                 set_bit(d->props.rc_key_map[i].event, input_dev->keybit);
121         }
122
123         /* Start the remote-control polling. */
124         if (d->props.rc_interval < 40)
125                 d->props.rc_interval = 100; /* default */
126
127         /* setting these two values to non-zero, we have to manage key repeats */
128         input_dev->rep[REP_PERIOD] = d->props.rc_interval;
129         input_dev->rep[REP_DELAY]  = d->props.rc_interval + 150;
130
131         err = input_register_device(input_dev);
132         if (err) {
133                 input_free_device(input_dev);
134                 return err;
135         }
136
137         d->rc_input_dev = input_dev;
138
139         INIT_DELAYED_WORK(&d->rc_query_work, dvb_usb_read_remote_control);
140
141         info("schedule remote query interval to %d msecs.", d->props.rc_interval);
142         schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
143
144         d->state |= DVB_USB_STATE_REMOTE;
145
146         return 0;
147 }
148
149 int dvb_usb_remote_exit(struct dvb_usb_device *d)
150 {
151         if (d->state & DVB_USB_STATE_REMOTE) {
152                 cancel_rearming_delayed_work(&d->rc_query_work);
153                 flush_scheduled_work();
154                 input_unregister_device(d->rc_input_dev);
155         }
156         d->state &= ~DVB_USB_STATE_REMOTE;
157         return 0;
158 }
159
160 #define DVB_USB_RC_NEC_EMPTY           0x00
161 #define DVB_USB_RC_NEC_KEY_PRESSED     0x01
162 #define DVB_USB_RC_NEC_KEY_REPEATED    0x02
163 int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d,
164                 u8 keybuf[5], u32 *event, int *state)
165 {
166         int i;
167         struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
168         *event = 0;
169         *state = REMOTE_NO_KEY_PRESSED;
170         switch (keybuf[0]) {
171                 case DVB_USB_RC_NEC_EMPTY:
172                         break;
173                 case DVB_USB_RC_NEC_KEY_PRESSED:
174                         if ((u8) ~keybuf[1] != keybuf[2] ||
175                                 (u8) ~keybuf[3] != keybuf[4]) {
176                                 deb_err("remote control checksum failed.\n");
177                                 break;
178                         }
179                         /* See if we can match the raw key code. */
180                         for (i = 0; i < d->props.rc_key_map_size; i++)
181                                 if (keymap[i].custom == keybuf[1] &&
182                                         keymap[i].data == keybuf[3]) {
183                                         *event = keymap[i].event;
184                                         *state = REMOTE_KEY_PRESSED;
185                                         return 0;
186                                 }
187                         deb_err("key mapping failed - no appropriate key found in keymapping\n");
188                         break;
189                 case DVB_USB_RC_NEC_KEY_REPEATED:
190                         *state = REMOTE_KEY_REPEAT;
191                         break;
192                 default:
193                         deb_err("unkown type of remote status: %d\n",keybuf[0]);
194                         break;
195         }
196         return 0;
197 }
198 EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);