Merge branch 'master'
[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, 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
93         if (d->props.rc_key_map == NULL ||
94                 d->props.rc_query == NULL ||
95                 dvb_usb_disable_rc_polling)
96                 return 0;
97
98         usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
99         strlcpy(d->rc_phys, "/ir0", sizeof(d->rc_phys));
100
101         d->rc_input_dev = input_allocate_device();
102         if (!d->rc_input_dev)
103                 return -ENOMEM;
104
105         d->rc_input_dev->evbit[0] = BIT(EV_KEY);
106         d->rc_input_dev->keycodesize = sizeof(unsigned char);
107         d->rc_input_dev->keycodemax = KEY_MAX;
108         d->rc_input_dev->name = "IR-receiver inside an USB DVB receiver";
109         d->rc_input_dev->phys = d->rc_phys;
110
111         /* set the bits for the keys */
112         deb_rc("key map size: %d\n", d->props.rc_key_map_size);
113         for (i = 0; i < d->props.rc_key_map_size; i++) {
114                 deb_rc("setting bit for event %d item %d\n",d->props.rc_key_map[i].event, i);
115                 set_bit(d->props.rc_key_map[i].event, d->rc_input_dev->keybit);
116         }
117
118         /* Start the remote-control polling. */
119         if (d->props.rc_interval < 40)
120                 d->props.rc_interval = 100; /* default */
121
122         /* setting these two values to non-zero, we have to manage key repeats */
123         d->rc_input_dev->rep[REP_PERIOD] = d->props.rc_interval;
124         d->rc_input_dev->rep[REP_DELAY]  = d->props.rc_interval + 150;
125
126         input_register_device(d->rc_input_dev);
127
128         INIT_WORK(&d->rc_query_work, dvb_usb_read_remote_control, d);
129
130         info("schedule remote query interval to %d msecs.", d->props.rc_interval);
131         schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
132
133         d->state |= DVB_USB_STATE_REMOTE;
134
135         return 0;
136 }
137
138 int dvb_usb_remote_exit(struct dvb_usb_device *d)
139 {
140         if (d->state & DVB_USB_STATE_REMOTE) {
141                 cancel_delayed_work(&d->rc_query_work);
142                 flush_scheduled_work();
143                 input_unregister_device(d->rc_input_dev);
144         }
145         d->state &= ~DVB_USB_STATE_REMOTE;
146         return 0;
147 }
148
149 #define DVB_USB_RC_NEC_EMPTY           0x00
150 #define DVB_USB_RC_NEC_KEY_PRESSED     0x01
151 #define DVB_USB_RC_NEC_KEY_REPEATED    0x02
152 int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d,
153                 u8 keybuf[5], u32 *event, int *state)
154 {
155         int i;
156         struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
157         *event = 0;
158         *state = REMOTE_NO_KEY_PRESSED;
159         switch (keybuf[0]) {
160                 case DVB_USB_RC_NEC_EMPTY:
161                         break;
162                 case DVB_USB_RC_NEC_KEY_PRESSED:
163                         if ((u8) ~keybuf[1] != keybuf[2] ||
164                                 (u8) ~keybuf[3] != keybuf[4]) {
165                                 deb_err("remote control checksum failed.\n");
166                                 break;
167                         }
168                         /* See if we can match the raw key code. */
169                         for (i = 0; i < d->props.rc_key_map_size; i++)
170                                 if (keymap[i].custom == keybuf[1] &&
171                                         keymap[i].data == keybuf[3]) {
172                                         *event = keymap[i].event;
173                                         *state = REMOTE_KEY_PRESSED;
174                                         return 0;
175                                 }
176                         deb_err("key mapping failed - no appropriate key found in keymapping\n");
177                         break;
178                 case DVB_USB_RC_NEC_KEY_REPEATED:
179                         *state = REMOTE_KEY_REPEAT;
180                         break;
181                 default:
182                         deb_err("unkown type of remote status: %d\n",keybuf[0]);
183                         break;
184         }
185         return 0;
186 }
187 EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);