]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/media/video/cx231xx/cx231xx-input.c
a0e8bb88d67e5dbfa44a79b4c6065ab654e93194
[net-next-2.6.git] / drivers / media / video / cx231xx / cx231xx-input.c
1 /*
2   handle cx231xx IR remotes via linux kernel input layer.
3
4   Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
5                 Based on em28xx driver
6
7                 < This is a place holder for IR now.>
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/input.h>
29 #include <linux/usb.h>
30 #include <linux/slab.h>
31
32 #include "cx231xx.h"
33
34 static unsigned int ir_debug;
35 module_param(ir_debug, int, 0644);
36 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
37
38 #define MODULE_NAME "cx231xx"
39
40 #define i2cdprintk(fmt, arg...) \
41         if (ir_debug) { \
42                 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
43         }
44
45 #define dprintk(fmt, arg...) \
46         if (ir_debug) { \
47                 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
48         }
49
50 /**********************************************************
51  Polling structure used by cx231xx IR's
52  **********************************************************/
53
54 struct cx231xx_ir_poll_result {
55         unsigned int toggle_bit:1;
56         unsigned int read_count:7;
57         u8 rc_address;
58         u8 rc_data[4];
59 };
60
61 struct cx231xx_IR {
62         struct cx231xx *dev;
63         struct input_dev *input;
64         struct ir_input_state ir;
65         char name[32];
66         char phys[32];
67
68         /* poll external decoder */
69         int polling;
70         struct work_struct work;
71         struct timer_list timer;
72         unsigned int last_toggle:1;
73         unsigned int last_readcount;
74         unsigned int repeat_interval;
75
76         int (*get_key) (struct cx231xx_IR *, struct cx231xx_ir_poll_result *);
77 };
78
79 /**********************************************************
80  Polling code for cx231xx
81  **********************************************************/
82
83 static void cx231xx_ir_handle_key(struct cx231xx_IR *ir)
84 {
85         int result;
86         int do_sendkey = 0;
87         struct cx231xx_ir_poll_result poll_result;
88
89         /* read the registers containing the IR status */
90         result = ir->get_key(ir, &poll_result);
91         if (result < 0) {
92                 dprintk("ir->get_key() failed %d\n", result);
93                 return;
94         }
95
96         dprintk("ir->get_key result tb=%02x rc=%02x lr=%02x data=%02x\n",
97                 poll_result.toggle_bit, poll_result.read_count,
98                 ir->last_readcount, poll_result.rc_data[0]);
99
100         if (ir->dev->chip_id == CHIP_ID_EM2874) {
101                 /* The em2874 clears the readcount field every time the
102                    register is read.  The em2860/2880 datasheet says that it
103                    is supposed to clear the readcount, but it doesn't.  So with
104                    the em2874, we are looking for a non-zero read count as
105                    opposed to a readcount that is incrementing */
106                 ir->last_readcount = 0;
107         }
108
109         if (poll_result.read_count == 0) {
110                 /* The button has not been pressed since the last read */
111         } else if (ir->last_toggle != poll_result.toggle_bit) {
112                 /* A button has been pressed */
113                 dprintk("button has been pressed\n");
114                 ir->last_toggle = poll_result.toggle_bit;
115                 ir->repeat_interval = 0;
116                 do_sendkey = 1;
117         } else if (poll_result.toggle_bit == ir->last_toggle &&
118                    poll_result.read_count > 0 &&
119                    poll_result.read_count != ir->last_readcount) {
120                 /* The button is still being held down */
121                 dprintk("button being held down\n");
122
123                 /* Debouncer for first keypress */
124                 if (ir->repeat_interval++ > 9) {
125                         /* Start repeating after 1 second */
126                         do_sendkey = 1;
127                 }
128         }
129
130         if (do_sendkey) {
131                 dprintk("sending keypress\n");
132                 ir_input_keydown(ir->input, &ir->ir, poll_result.rc_data[0]);
133                 ir_input_nokey(ir->input, &ir->ir);
134         }
135
136         ir->last_readcount = poll_result.read_count;
137         return;
138 }
139
140 static void ir_timer(unsigned long data)
141 {
142         struct cx231xx_IR *ir = (struct cx231xx_IR *)data;
143
144         schedule_work(&ir->work);
145 }
146
147 static void cx231xx_ir_work(struct work_struct *work)
148 {
149         struct cx231xx_IR *ir = container_of(work, struct cx231xx_IR, work);
150
151         cx231xx_ir_handle_key(ir);
152         mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
153 }
154
155 void cx231xx_ir_start(struct cx231xx_IR *ir)
156 {
157         setup_timer(&ir->timer, ir_timer, (unsigned long)ir);
158         INIT_WORK(&ir->work, cx231xx_ir_work);
159         schedule_work(&ir->work);
160 }
161
162 static void cx231xx_ir_stop(struct cx231xx_IR *ir)
163 {
164         del_timer_sync(&ir->timer);
165         flush_scheduled_work();
166 }
167
168 int cx231xx_ir_init(struct cx231xx *dev)
169 {
170         struct cx231xx_IR *ir;
171         struct input_dev *input_dev;
172         u8 ir_config;
173         int err = -ENOMEM;
174
175         if (dev->board.ir_codes == NULL) {
176                 /* No remote control support */
177                 return 0;
178         }
179
180         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
181         input_dev = input_allocate_device();
182         if (!ir || !input_dev)
183                 goto err_out_free;
184
185         ir->input = input_dev;
186
187         /* Setup the proper handler based on the chip */
188         switch (dev->chip_id) {
189         default:
190                 printk("Unrecognized cx231xx chip id: IR not supported\n");
191                 goto err_out_free;
192         }
193
194         /* This is how often we ask the chip for IR information */
195         ir->polling = 100;      /* ms */
196
197         /* init input device */
198         snprintf(ir->name, sizeof(ir->name), "cx231xx IR (%s)", dev->name);
199
200         usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
201         strlcat(ir->phys, "/input0", sizeof(ir->phys));
202
203         err = ir_input_init(input_dev, &ir->ir, IR_TYPE_OTHER);
204         if (err < 0)
205                 goto err_out_free;
206
207         input_dev->name = ir->name;
208         input_dev->phys = ir->phys;
209         input_dev->id.bustype = BUS_USB;
210         input_dev->id.version = 1;
211         input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
212         input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
213
214         input_dev->dev.parent = &dev->udev->dev;
215         /* record handles to ourself */
216         ir->dev = dev;
217         dev->ir = ir;
218
219         cx231xx_ir_start(ir);
220
221         /* all done */
222         err = ir_input_register(ir->input, dev->board.ir_codes,
223                                 NULL, MODULE_NAME);
224         if (err)
225                 goto err_out_stop;
226
227         return 0;
228 err_out_stop:
229         cx231xx_ir_stop(ir);
230         dev->ir = NULL;
231 err_out_free:
232         kfree(ir);
233         return err;
234 }
235
236 int cx231xx_ir_fini(struct cx231xx *dev)
237 {
238         struct cx231xx_IR *ir = dev->ir;
239
240         /* skip detach on non attached boards */
241         if (!ir)
242                 return 0;
243
244         cx231xx_ir_stop(ir);
245         ir_input_unregister(ir->input);
246         kfree(ir);
247
248         /* done */
249         dev->ir = NULL;
250         return 0;
251 }