2 * Driver for the Conexant CX23885/7/8 PCIe bridge
4 * Infrared remote control input device
8 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
10 * However, the cx23885_input_{init,fini} functions contained herein are
11 * derived from Linux kernel files linux/media/video/.../...-input.c marked as:
13 * Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
14 * Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
15 * Markus Rechberger <mrechberger@gmail.com>
16 * Mauro Carvalho Chehab <mchehab@kernel.org>
17 * Sascha Sommer <saschasommer@freenet.de>
18 * Copyright (C) 2004, 2005 Chris Pascoe
19 * Copyright (C) 2003, 2004 Gerd Knorr
20 * Copyright (C) 2003 Pavel Machek
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * as published by the Free Software Foundation; either version 2
25 * of the License, or (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
34 #include "cx23885-input.h"
36 #include <linux/slab.h>
37 #include <media/rc-core.h>
38 #include <media/v4l2-subdev.h>
40 #define MODULE_NAME "cx23885"
42 static void cx23885_input_process_measurements(struct cx23885_dev
*dev
,
45 struct cx23885_kernel_ir
*kernel_ir
= dev
->kernel_ir
;
50 struct ir_raw_event ir_core_event
[64];
54 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_read
, (u8
*) ir_core_event
,
55 sizeof(ir_core_event
), &num
);
57 count
= num
/ sizeof(struct ir_raw_event
);
59 for (i
= 0; i
< count
; i
++) {
60 ir_raw_event_store(kernel_ir
->rc
,
67 ir_raw_event_reset(kernel_ir
->rc
);
69 ir_raw_event_handle(kernel_ir
->rc
);
72 void cx23885_input_rx_work_handler(struct cx23885_dev
*dev
, u32 events
)
74 struct v4l2_subdev_ir_parameters params
;
75 int overrun
, data_available
;
77 if (dev
->sd_ir
== NULL
|| events
== 0)
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
:
99 * The only boards we handle right now. However other boards
100 * using the CX2388x integrated IR controller should be similar
107 overrun
= events
& (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN
|
108 V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN
);
110 data_available
= events
& (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED
|
111 V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ
);
114 /* If there was a FIFO overrun, stop the device */
115 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_g_parameters
, ¶ms
);
116 params
.enable
= false;
117 /* Mitigate race with cx23885_input_ir_stop() */
118 params
.shutdown
= atomic_read(&dev
->ir_input_stopping
);
119 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_s_parameters
, ¶ms
);
123 cx23885_input_process_measurements(dev
, overrun
);
126 /* If there was a FIFO overrun, clear & restart the device */
127 params
.enable
= true;
128 /* Mitigate race with cx23885_input_ir_stop() */
129 params
.shutdown
= atomic_read(&dev
->ir_input_stopping
);
130 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_s_parameters
, ¶ms
);
134 static int cx23885_input_ir_start(struct cx23885_dev
*dev
)
136 struct v4l2_subdev_ir_parameters params
;
138 if (dev
->sd_ir
== NULL
)
141 atomic_set(&dev
->ir_input_stopping
, 0);
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
:
159 * The IR controller on this board only returns pulse widths.
160 * Any other mode setting will fail to set up the device.
162 params
.mode
= V4L2_SUBDEV_IR_MODE_PULSE_WIDTH
;
163 params
.enable
= true;
164 params
.interrupt_enable
= true;
165 params
.shutdown
= false;
167 /* Setup for baseband compatible with both RC-5 and RC-6A */
168 params
.modulation
= false;
169 /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/
170 /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/
171 params
.max_pulse_width
= 3333333; /* ns */
172 /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
173 /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
174 params
.noise_filter_min_width
= 333333; /* ns */
176 * This board has inverted receive sense:
177 * mark is received as low logic level;
178 * falling edges are detected as rising edges; etc.
180 params
.invert_level
= true;
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
:
187 * The IR controller on this board only returns pulse widths.
188 * Any other mode setting will fail to set up the device.
190 params
.mode
= V4L2_SUBDEV_IR_MODE_PULSE_WIDTH
;
191 params
.enable
= true;
192 params
.interrupt_enable
= true;
193 params
.shutdown
= false;
195 /* Setup for a standard NEC protocol */
196 params
.carrier_freq
= 37917; /* Hz, 455 kHz/12 for NEC */
197 params
.carrier_range_lower
= 33000; /* Hz */
198 params
.carrier_range_upper
= 43000; /* Hz */
199 params
.duty_cycle
= 33; /* percent, 33 percent for NEC */
202 * NEC max pulse width: (64/3)/(455 kHz/12) * 16 nec_units
203 * (64/3)/(455 kHz/12) * 16 nec_units * 1.375 = 12378022 ns
205 params
.max_pulse_width
= 12378022; /* ns */
208 * NEC noise filter min width: (64/3)/(455 kHz/12) * 1 nec_unit
209 * (64/3)/(455 kHz/12) * 1 nec_units * 0.625 = 351648 ns
211 params
.noise_filter_min_width
= 351648; /* ns */
213 params
.modulation
= false;
214 params
.invert_level
= true;
217 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_s_parameters
, ¶ms
);
221 static int cx23885_input_ir_open(struct rc_dev
*rc
)
223 struct cx23885_kernel_ir
*kernel_ir
= rc
->priv
;
225 if (kernel_ir
->cx
== NULL
)
228 return cx23885_input_ir_start(kernel_ir
->cx
);
231 static void cx23885_input_ir_stop(struct cx23885_dev
*dev
)
233 struct v4l2_subdev_ir_parameters params
;
235 if (dev
->sd_ir
== NULL
)
239 * Stop the sd_ir subdevice from generating notifications and
241 * It is shutdown this way in order to mitigate a race with
242 * cx23885_input_rx_work_handler() in the overrun case, which could
243 * re-enable the subdevice.
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
);
254 flush_work(&dev
->cx25840_work
);
255 flush_work(&dev
->ir_rx_work
);
256 flush_work(&dev
->ir_tx_work
);
259 static void cx23885_input_ir_close(struct rc_dev
*rc
)
261 struct cx23885_kernel_ir
*kernel_ir
= rc
->priv
;
263 if (kernel_ir
->cx
!= NULL
)
264 cx23885_input_ir_stop(kernel_ir
->cx
);
267 int cx23885_input_init(struct cx23885_dev
*dev
)
269 struct cx23885_kernel_ir
*kernel_ir
;
277 * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't
278 * encapsulated in a v4l2_subdev, then I'm not going to deal with it.
280 if (dev
->sd_ir
== NULL
)
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 /* Integrated CX2388[58] IR controller */
290 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
291 /* The grey Hauppauge RC-5 remote */
292 rc_map
= RC_MAP_HAUPPAUGE
;
294 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL
:
295 /* Integrated CX23885 IR controller */
296 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
297 /* The grey Terratec remote with orange buttons */
298 rc_map
= RC_MAP_NEC_TERRATEC_CINERGY_XS
;
300 case CX23885_BOARD_TEVII_S470
:
301 /* Integrated CX23885 IR controller */
302 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
303 /* A guess at the remote */
304 rc_map
= RC_MAP_TEVII_NEC
;
306 case CX23885_BOARD_MYGICA_X8507
:
307 /* Integrated CX23885 IR controller */
308 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
309 /* A guess at the remote */
310 rc_map
= RC_MAP_TOTAL_MEDIA_IN_HAND_02
;
312 case CX23885_BOARD_TBS_6980
:
313 case CX23885_BOARD_TBS_6981
:
314 /* Integrated CX23885 IR controller */
315 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
316 /* A guess at the remote */
317 rc_map
= RC_MAP_TBS_NEC
;
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 /* Integrated CX23885 IR controller */
326 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
327 rc_map
= RC_MAP_DVBSKY
;
329 case CX23885_BOARD_TT_CT2_4500_CI
:
330 /* Integrated CX23885 IR controller */
331 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
332 rc_map
= RC_MAP_TT_1500
;
338 /* cx23885 board instance kernel IR state */
339 kernel_ir
= kzalloc(sizeof(struct cx23885_kernel_ir
), GFP_KERNEL
);
340 if (kernel_ir
== NULL
)
344 kernel_ir
->name
= kasprintf(GFP_KERNEL
, "cx23885 IR (%s)",
345 cx23885_boards
[dev
->board
].name
);
346 if (!kernel_ir
->name
) {
351 kernel_ir
->phys
= kasprintf(GFP_KERNEL
, "pci-%s/ir0",
353 if (!kernel_ir
->phys
) {
355 goto err_out_free_name
;
359 rc
= rc_allocate_device(RC_DRIVER_IR_RAW
);
362 goto err_out_free_phys
;
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
;
374 rc
->input_id
.vendor
= dev
->pci
->vendor
;
375 rc
->input_id
.product
= dev
->pci
->device
;
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
;
386 dev
->kernel_ir
= kernel_ir
;
387 ret
= rc_register_device(rc
);
394 cx23885_input_ir_stop(dev
);
395 dev
->kernel_ir
= NULL
;
398 kfree(kernel_ir
->phys
);
400 kfree(kernel_ir
->name
);
406 void cx23885_input_fini(struct cx23885_dev
*dev
)
408 /* Always stop the IR hardware from generating interrupts */
409 cx23885_input_ir_stop(dev
);
411 if (dev
->kernel_ir
== NULL
)
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
;