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