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