1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#include <linux/slab.h>
39#include <media/rc-core.h>
40#include <media/v4l2-subdev.h>
41
42#include "cx23885.h"
43#include "cx23885-input.h"
44
45#define MODULE_NAME "cx23885"
46
47static void cx23885_input_process_measurements(struct cx23885_dev *dev,
48 bool overrun)
49{
50 struct cx23885_kernel_ir *kernel_ir = dev->kernel_ir;
51
52 ssize_t num;
53 int count, i;
54 bool handle = false;
55 struct ir_raw_event ir_core_event[64];
56
57 do {
58 num = 0;
59 v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) ir_core_event,
60 sizeof(ir_core_event), &num);
61
62 count = num / sizeof(struct ir_raw_event);
63
64 for (i = 0; i < count; i++) {
65 ir_raw_event_store(kernel_ir->rc,
66 &ir_core_event[i]);
67 handle = true;
68 }
69 } while (num != 0);
70
71 if (overrun)
72 ir_raw_event_reset(kernel_ir->rc);
73 else if (handle)
74 ir_raw_event_handle(kernel_ir->rc);
75}
76
77void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events)
78{
79 struct v4l2_subdev_ir_parameters params;
80 int overrun, data_available;
81
82 if (dev->sd_ir == NULL || events == 0)
83 return;
84
85 switch (dev->board) {
86 case CX23885_BOARD_HAUPPAUGE_HVR1270:
87 case CX23885_BOARD_HAUPPAUGE_HVR1850:
88 case CX23885_BOARD_HAUPPAUGE_HVR1290:
89 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
90 case CX23885_BOARD_TEVII_S470:
91 case CX23885_BOARD_HAUPPAUGE_HVR1250:
92 case CX23885_BOARD_MYGICA_X8507:
93
94
95
96
97 break;
98 default:
99 return;
100 }
101
102 overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN |
103 V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN);
104
105 data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED |
106 V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ);
107
108 if (overrun) {
109
110 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms);
111 params.enable = false;
112
113 params.shutdown = atomic_read(&dev->ir_input_stopping);
114 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms);
115 }
116
117 if (data_available)
118 cx23885_input_process_measurements(dev, overrun);
119
120 if (overrun) {
121
122 params.enable = true;
123
124 params.shutdown = atomic_read(&dev->ir_input_stopping);
125 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms);
126 }
127}
128
129static int cx23885_input_ir_start(struct cx23885_dev *dev)
130{
131 struct v4l2_subdev_ir_parameters params;
132
133 if (dev->sd_ir == NULL)
134 return -ENODEV;
135
136 atomic_set(&dev->ir_input_stopping, 0);
137
138 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms);
139 switch (dev->board) {
140 case CX23885_BOARD_HAUPPAUGE_HVR1270:
141 case CX23885_BOARD_HAUPPAUGE_HVR1850:
142 case CX23885_BOARD_HAUPPAUGE_HVR1290:
143 case CX23885_BOARD_HAUPPAUGE_HVR1250:
144 case CX23885_BOARD_MYGICA_X8507:
145
146
147
148
149 params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
150 params.enable = true;
151 params.interrupt_enable = true;
152 params.shutdown = false;
153
154
155 params.modulation = false;
156
157
158 params.max_pulse_width = 3333333;
159
160
161 params.noise_filter_min_width = 333333;
162
163
164
165
166
167 params.invert_level = true;
168 break;
169 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
170 case CX23885_BOARD_TEVII_S470:
171
172
173
174
175 params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
176 params.enable = true;
177 params.interrupt_enable = true;
178 params.shutdown = false;
179
180
181 params.carrier_freq = 37917;
182 params.carrier_range_lower = 33000;
183 params.carrier_range_upper = 43000;
184 params.duty_cycle = 33;
185
186
187
188
189
190 params.max_pulse_width = 12378022;
191
192
193
194
195
196 params.noise_filter_min_width = 351648;
197
198 params.modulation = false;
199 params.invert_level = true;
200 break;
201 }
202 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms);
203 return 0;
204}
205
206static int cx23885_input_ir_open(struct rc_dev *rc)
207{
208 struct cx23885_kernel_ir *kernel_ir = rc->priv;
209
210 if (kernel_ir->cx == NULL)
211 return -ENODEV;
212
213 return cx23885_input_ir_start(kernel_ir->cx);
214}
215
216static void cx23885_input_ir_stop(struct cx23885_dev *dev)
217{
218 struct v4l2_subdev_ir_parameters params;
219
220 if (dev->sd_ir == NULL)
221 return;
222
223
224
225
226
227
228
229
230 atomic_set(&dev->ir_input_stopping, 1);
231 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms);
232 while (params.shutdown == false) {
233 params.enable = false;
234 params.interrupt_enable = false;
235 params.shutdown = true;
236 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms);
237 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms);
238 }
239 flush_work(&dev->cx25840_work);
240 flush_work(&dev->ir_rx_work);
241 flush_work(&dev->ir_tx_work);
242}
243
244static void cx23885_input_ir_close(struct rc_dev *rc)
245{
246 struct cx23885_kernel_ir *kernel_ir = rc->priv;
247
248 if (kernel_ir->cx != NULL)
249 cx23885_input_ir_stop(kernel_ir->cx);
250}
251
252int cx23885_input_init(struct cx23885_dev *dev)
253{
254 struct cx23885_kernel_ir *kernel_ir;
255 struct rc_dev *rc;
256 char *rc_map;
257 enum rc_driver_type driver_type;
258 unsigned long allowed_protos;
259
260 int ret;
261
262
263
264
265
266 if (dev->sd_ir == NULL)
267 return -ENODEV;
268
269 switch (dev->board) {
270 case CX23885_BOARD_HAUPPAUGE_HVR1270:
271 case CX23885_BOARD_HAUPPAUGE_HVR1850:
272 case CX23885_BOARD_HAUPPAUGE_HVR1290:
273 case CX23885_BOARD_HAUPPAUGE_HVR1250:
274
275 driver_type = RC_DRIVER_IR_RAW;
276 allowed_protos = RC_BIT_ALL;
277
278 rc_map = RC_MAP_HAUPPAUGE;
279 break;
280 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
281
282 driver_type = RC_DRIVER_IR_RAW;
283 allowed_protos = RC_BIT_NEC;
284
285 rc_map = RC_MAP_NEC_TERRATEC_CINERGY_XS;
286 break;
287 case CX23885_BOARD_TEVII_S470:
288
289 driver_type = RC_DRIVER_IR_RAW;
290 allowed_protos = RC_BIT_ALL;
291
292 rc_map = RC_MAP_TEVII_NEC;
293 break;
294 case CX23885_BOARD_MYGICA_X8507:
295
296 driver_type = RC_DRIVER_IR_RAW;
297 allowed_protos = RC_BIT_ALL;
298
299 rc_map = RC_MAP_TOTAL_MEDIA_IN_HAND_02;
300 break;
301 default:
302 return -ENODEV;
303 }
304
305
306 kernel_ir = kzalloc(sizeof(struct cx23885_kernel_ir), GFP_KERNEL);
307 if (kernel_ir == NULL)
308 return -ENOMEM;
309
310 kernel_ir->cx = dev;
311 kernel_ir->name = kasprintf(GFP_KERNEL, "cx23885 IR (%s)",
312 cx23885_boards[dev->board].name);
313 kernel_ir->phys = kasprintf(GFP_KERNEL, "pci-%s/ir0",
314 pci_name(dev->pci));
315
316
317 rc = rc_allocate_device();
318 if (!rc) {
319 ret = -ENOMEM;
320 goto err_out_free;
321 }
322
323 kernel_ir->rc = rc;
324 rc->input_name = kernel_ir->name;
325 rc->input_phys = kernel_ir->phys;
326 rc->input_id.bustype = BUS_PCI;
327 rc->input_id.version = 1;
328 if (dev->pci->subsystem_vendor) {
329 rc->input_id.vendor = dev->pci->subsystem_vendor;
330 rc->input_id.product = dev->pci->subsystem_device;
331 } else {
332 rc->input_id.vendor = dev->pci->vendor;
333 rc->input_id.product = dev->pci->device;
334 }
335 rc->dev.parent = &dev->pci->dev;
336 rc->driver_type = driver_type;
337 rc->allowed_protos = allowed_protos;
338 rc->priv = kernel_ir;
339 rc->open = cx23885_input_ir_open;
340 rc->close = cx23885_input_ir_close;
341 rc->map_name = rc_map;
342 rc->driver_name = MODULE_NAME;
343
344
345 dev->kernel_ir = kernel_ir;
346 ret = rc_register_device(rc);
347 if (ret)
348 goto err_out_stop;
349
350 return 0;
351
352err_out_stop:
353 cx23885_input_ir_stop(dev);
354 dev->kernel_ir = NULL;
355 rc_free_device(rc);
356err_out_free:
357 kfree(kernel_ir->phys);
358 kfree(kernel_ir->name);
359 kfree(kernel_ir);
360 return ret;
361}
362
363void cx23885_input_fini(struct cx23885_dev *dev)
364{
365
366 cx23885_input_ir_stop(dev);
367
368 if (dev->kernel_ir == NULL)
369 return;
370 rc_unregister_device(dev->kernel_ir->rc);
371 kfree(dev->kernel_ir->phys);
372 kfree(dev->kernel_ir->name);
373 kfree(dev->kernel_ir);
374 dev->kernel_ir = NULL;
375}
376