c5771db3bfce8d6269113f3c1eb5f4cab2e1ffc6
[linux-flexiantxendom0-natty.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
31 #include "cx231xx.h"
32
33 static unsigned int ir_debug;
34 module_param(ir_debug, int, 0644);
35 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
36
37 #define i2cdprintk(fmt, arg...) \
38         if (ir_debug) { \
39                 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
40         }
41
42 #define dprintk(fmt, arg...) \
43         if (ir_debug) { \
44                 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
45         }
46
47 /**********************************************************
48  Polling structure used by cx231xx IR's
49  **********************************************************/
50
51 struct cx231xx_ir_poll_result {
52         unsigned int toggle_bit:1;
53         unsigned int read_count:7;
54         u8 rc_address;
55         u8 rc_data[4];
56 };
57
58 struct cx231xx_IR {
59         struct cx231xx *dev;
60         struct input_dev *input;
61         struct ir_input_state ir;
62         char name[32];
63         char phys[32];
64
65         /* poll external decoder */
66         int polling;
67         struct work_struct work;
68         struct timer_list timer;
69         unsigned int last_toggle:1;
70         unsigned int last_readcount;
71         unsigned int repeat_interval;
72
73         int (*get_key) (struct cx231xx_IR *, struct cx231xx_ir_poll_result *);
74 };
75
76 /**********************************************************
77  Polling code for cx231xx
78  **********************************************************/
79
80 static void cx231xx_ir_handle_key(struct cx231xx_IR *ir)
81 {
82         int result;
83         int do_sendkey = 0;
84         struct cx231xx_ir_poll_result poll_result;
85
86         /* read the registers containing the IR status */
87         result = ir->get_key(ir, &poll_result);
88         if (result < 0) {
89                 dprintk("ir->get_key() failed %d\n", result);
90                 return;
91         }
92
93         dprintk("ir->get_key result tb=%02x rc=%02x lr=%02x data=%02x\n",
94                 poll_result.toggle_bit, poll_result.read_count,
95                 ir->last_readcount, poll_result.rc_data[0]);
96
97         if (ir->dev->chip_id == CHIP_ID_EM2874) {
98                 /* The em2874 clears the readcount field every time the
99                    register is read.  The em2860/2880 datasheet says that it
100                    is supposed to clear the readcount, but it doesn't.  So with
101                    the em2874, we are looking for a non-zero read count as
102                    opposed to a readcount that is incrementing */
103                 ir->last_readcount = 0;
104         }
105
106         if (poll_result.read_count == 0) {
107                 /* The button has not been pressed since the last read */
108         } else if (ir->last_toggle != poll_result.toggle_bit) {
109                 /* A button has been pressed */
110                 dprintk("button has been pressed\n");
111                 ir->last_toggle = poll_result.toggle_bit;
112                 ir->repeat_interval = 0;
113                 do_sendkey = 1;
114         } else if (poll_result.toggle_bit == ir->last_toggle &&
115                    poll_result.read_count > 0 &&
116                    poll_result.read_count != ir->last_readcount) {
117                 /* The button is still being held down */
118                 dprintk("button being held down\n");
119
120                 /* Debouncer for first keypress */
121                 if (ir->repeat_interval++ > 9) {
122                         /* Start repeating after 1 second */
123                         do_sendkey = 1;
124                 }
125         }
126
127         if (do_sendkey) {
128                 dprintk("sending keypress\n");
129                 ir_input_keydown(ir->input, &ir->ir, poll_result.rc_data[0]);
130                 ir_input_nokey(ir->input, &ir->ir);
131         }
132
133         ir->last_readcount = poll_result.read_count;
134         return;
135 }
136
137 static void ir_timer(unsigned long data)
138 {
139         struct cx231xx_IR *ir = (struct cx231xx_IR *)data;
140
141         schedule_work(&ir->work);
142 }
143
144 static void cx231xx_ir_work(struct work_struct *work)
145 {
146         struct cx231xx_IR *ir = container_of(work, struct cx231xx_IR, work);
147
148         cx231xx_ir_handle_key(ir);
149         mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
150 }
151
152 void cx231xx_ir_start(struct cx231xx_IR *ir)
153 {
154         setup_timer(&ir->timer, ir_timer, (unsigned long)ir);
155         INIT_WORK(&ir->work, cx231xx_ir_work);
156         schedule_work(&ir->work);
157 }
158
159 static void cx231xx_ir_stop(struct cx231xx_IR *ir)
160 {
161         del_timer_sync(&ir->timer);
162         flush_scheduled_work();
163 }
164
165 int cx231xx_ir_init(struct cx231xx *dev)
166 {
167         struct cx231xx_IR *ir;
168         struct input_dev *input_dev;
169         u8 ir_config;
170         int err = -ENOMEM;
171
172         if (dev->board.ir_codes == NULL) {
173                 /* No remote control support */
174                 return 0;
175         }
176
177         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
178         input_dev = input_allocate_device();
179         if (!ir || !input_dev)
180                 goto err_out_free;
181
182         ir->input = input_dev;
183
184         /* Setup the proper handler based on the chip */
185         switch (dev->chip_id) {
186         default:
187                 printk("Unrecognized cx231xx chip id: IR not supported\n");
188                 goto err_out_free;
189         }
190
191         /* This is how often we ask the chip for IR information */
192         ir->polling = 100;      /* ms */
193
194         /* init input device */
195         snprintf(ir->name, sizeof(ir->name), "cx231xx IR (%s)", dev->name);
196
197         usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
198         strlcat(ir->phys, "/input0", sizeof(ir->phys));
199
200         err = ir_input_init(input_dev, &ir->ir, IR_TYPE_OTHER);
201         if (err < 0)
202                 goto err_out_free;
203
204         input_dev->name = ir->name;
205         input_dev->phys = ir->phys;
206         input_dev->id.bustype = BUS_USB;
207         input_dev->id.version = 1;
208         input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
209         input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
210
211         input_dev->dev.parent = &dev->udev->dev;
212         /* record handles to ourself */
213         ir->dev = dev;
214         dev->ir = ir;
215
216         cx231xx_ir_start(ir);
217
218         /* all done */
219         err = ir_input_register(ir->input, dev->board.ir_codes, NULL);
220         if (err)
221                 goto err_out_stop;
222
223         return 0;
224 err_out_stop:
225         cx231xx_ir_stop(ir);
226         dev->ir = NULL;
227 err_out_free:
228         kfree(ir);
229         return err;
230 }
231
232 int cx231xx_ir_fini(struct cx231xx *dev)
233 {
234         struct cx231xx_IR *ir = dev->ir;
235
236         /* skip detach on non attached boards */
237         if (!ir)
238                 return 0;
239
240         cx231xx_ir_stop(ir);
241         ir_input_unregister(ir->input);
242         kfree(ir);
243
244         /* done */
245         dev->ir = NULL;
246         return 0;
247 }