1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
34static unsigned int ir_debug;
35module_param(ir_debug, int, 0644);
36MODULE_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
52
53
54struct 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
61struct cx231xx_IR {
62 struct cx231xx *dev;
63 struct input_dev *input;
64 char name[32];
65 char phys[32];
66
67
68 int polling;
69 struct work_struct work;
70 struct timer_list timer;
71 unsigned int last_readcount;
72
73 int (*get_key) (struct cx231xx_IR *, struct cx231xx_ir_poll_result *);
74};
75
76
77
78
79
80static void cx231xx_ir_handle_key(struct cx231xx_IR *ir)
81{
82 int result;
83 struct cx231xx_ir_poll_result poll_result;
84
85
86 result = ir->get_key(ir, &poll_result);
87 if (result < 0) {
88 dprintk("ir->get_key() failed %d\n", result);
89 return;
90 }
91
92 dprintk("ir->get_key result tb=%02x rc=%02x lr=%02x data=%02x\n",
93 poll_result.toggle_bit, poll_result.read_count,
94 ir->last_readcount, poll_result.rc_data[0]);
95
96 if (poll_result.read_count > 0 &&
97 poll_result.read_count != ir->last_readcount)
98 ir_keydown(ir->input,
99 poll_result.rc_data[0],
100 poll_result.toggle_bit);
101
102 if (ir->dev->chip_id == CHIP_ID_EM2874)
103
104
105
106
107
108 ir->last_readcount = 0;
109 else
110 ir->last_readcount = poll_result.read_count;
111
112 }
113}
114
115static void ir_timer(unsigned long data)
116{
117 struct cx231xx_IR *ir = (struct cx231xx_IR *)data;
118
119 schedule_work(&ir->work);
120}
121
122static void cx231xx_ir_work(struct work_struct *work)
123{
124 struct cx231xx_IR *ir = container_of(work, struct cx231xx_IR, work);
125
126 cx231xx_ir_handle_key(ir);
127 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
128}
129
130void cx231xx_ir_start(struct cx231xx_IR *ir)
131{
132 setup_timer(&ir->timer, ir_timer, (unsigned long)ir);
133 INIT_WORK(&ir->work, cx231xx_ir_work);
134 schedule_work(&ir->work);
135}
136
137static void cx231xx_ir_stop(struct cx231xx_IR *ir)
138{
139 del_timer_sync(&ir->timer);
140 flush_scheduled_work();
141}
142
143int cx231xx_ir_init(struct cx231xx *dev)
144{
145 struct cx231xx_IR *ir;
146 struct input_dev *input_dev;
147 u8 ir_config;
148 int err = -ENOMEM;
149
150 if (dev->board.ir_codes == NULL) {
151
152 return 0;
153 }
154
155 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
156 input_dev = input_allocate_device();
157 if (!ir || !input_dev)
158 goto err_out_free;
159
160 ir->input = input_dev;
161
162
163 switch (dev->chip_id) {
164 default:
165 printk("Unrecognized cx231xx chip id: IR not supported\n");
166 goto err_out_free;
167 }
168
169
170 ir->polling = 100;
171
172
173 snprintf(ir->name, sizeof(ir->name), "cx231xx IR (%s)", dev->name);
174
175 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
176 strlcat(ir->phys, "/input0", sizeof(ir->phys));
177
178 input_dev->name = ir->name;
179 input_dev->phys = ir->phys;
180 input_dev->id.bustype = BUS_USB;
181 input_dev->id.version = 1;
182 input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
183 input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
184
185 input_dev->dev.parent = &dev->udev->dev;
186
187 ir->dev = dev;
188 dev->ir = ir;
189
190 cx231xx_ir_start(ir);
191
192
193 err = __ir_input_register(ir->input, dev->board.ir_codes,
194 NULL, MODULE_NAME);
195 if (err)
196 goto err_out_stop;
197
198 return 0;
199err_out_stop:
200 cx231xx_ir_stop(ir);
201 dev->ir = NULL;
202err_out_free:
203 kfree(ir);
204 return err;
205}
206
207int cx231xx_ir_fini(struct cx231xx *dev)
208{
209 struct cx231xx_IR *ir = dev->ir;
210
211
212 if (!ir)
213 return 0;
214
215 cx231xx_ir_stop(ir);
216 ir_input_unregister(ir->input);
217 kfree(ir);
218
219
220 dev->ir = NULL;
221 return 0;
222}
223