1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Conexant CX23885/7/8 PCIe bridge
5 * Infrared remote control input device
9 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
11 * However, the cx23885_input_{init,fini} functions contained herein are
12 * derived from Linux kernel files linux/media/video/.../...-input.c marked as:
14 * Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
15 * Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
16 * Markus Rechberger <mrechberger@gmail.com>
17 * Mauro Carvalho Chehab <mchehab@kernel.org>
18 * Sascha Sommer <saschasommer@freenet.de>
19 * Copyright (C) 2004, 2005 Chris Pascoe
20 * Copyright (C) 2003, 2004 Gerd Knorr
21 * Copyright (C) 2003 Pavel Machek
25 #include "cx23885-input.h"
27 #include <linux/slab.h>
28 #include <media/rc-core.h>
29 #include <media/v4l2-subdev.h>
31 #define MODULE_NAME "cx23885"
33 static void cx23885_input_process_measurements(struct cx23885_dev
*dev
,
36 struct cx23885_kernel_ir
*kernel_ir
= dev
->kernel_ir
;
41 struct ir_raw_event ir_core_event
[64];
45 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_read
, (u8
*) ir_core_event
,
46 sizeof(ir_core_event
), &num
);
48 count
= num
/ sizeof(struct ir_raw_event
);
50 for (i
= 0; i
< count
; i
++) {
51 ir_raw_event_store(kernel_ir
->rc
,
58 ir_raw_event_reset(kernel_ir
->rc
);
60 ir_raw_event_handle(kernel_ir
->rc
);
63 void cx23885_input_rx_work_handler(struct cx23885_dev
*dev
, u32 events
)
65 struct v4l2_subdev_ir_parameters params
;
66 int overrun
, data_available
;
68 if (dev
->sd_ir
== NULL
|| events
== 0)
72 case CX23885_BOARD_HAUPPAUGE_HVR1270
:
73 case CX23885_BOARD_HAUPPAUGE_HVR1850
:
74 case CX23885_BOARD_HAUPPAUGE_HVR1290
:
75 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL
:
76 case CX23885_BOARD_TEVII_S470
:
77 case CX23885_BOARD_HAUPPAUGE_HVR1250
:
78 case CX23885_BOARD_MYGICA_X8507
:
79 case CX23885_BOARD_TBS_6980
:
80 case CX23885_BOARD_TBS_6981
:
81 case CX23885_BOARD_DVBSKY_T9580
:
82 case CX23885_BOARD_DVBSKY_T980C
:
83 case CX23885_BOARD_DVBSKY_S950C
:
84 case CX23885_BOARD_TT_CT2_4500_CI
:
85 case CX23885_BOARD_DVBSKY_S950
:
86 case CX23885_BOARD_DVBSKY_S952
:
87 case CX23885_BOARD_DVBSKY_T982
:
88 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4
:
90 * The only boards we handle right now. However other boards
91 * using the CX2388x integrated IR controller should be similar
98 overrun
= events
& (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN
|
99 V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN
);
101 data_available
= events
& (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED
|
102 V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ
);
105 /* If there was a FIFO overrun, stop the device */
106 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_g_parameters
, ¶ms
);
107 params
.enable
= false;
108 /* Mitigate race with cx23885_input_ir_stop() */
109 params
.shutdown
= atomic_read(&dev
->ir_input_stopping
);
110 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_s_parameters
, ¶ms
);
114 cx23885_input_process_measurements(dev
, overrun
);
117 /* If there was a FIFO overrun, clear & restart the device */
118 params
.enable
= true;
119 /* Mitigate race with cx23885_input_ir_stop() */
120 params
.shutdown
= atomic_read(&dev
->ir_input_stopping
);
121 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_s_parameters
, ¶ms
);
125 static int cx23885_input_ir_start(struct cx23885_dev
*dev
)
127 struct v4l2_subdev_ir_parameters params
;
129 if (dev
->sd_ir
== NULL
)
132 atomic_set(&dev
->ir_input_stopping
, 0);
134 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_g_parameters
, ¶ms
);
135 switch (dev
->board
) {
136 case CX23885_BOARD_HAUPPAUGE_HVR1270
:
137 case CX23885_BOARD_HAUPPAUGE_HVR1850
:
138 case CX23885_BOARD_HAUPPAUGE_HVR1290
:
139 case CX23885_BOARD_HAUPPAUGE_HVR1250
:
140 case CX23885_BOARD_MYGICA_X8507
:
141 case CX23885_BOARD_DVBSKY_T9580
:
142 case CX23885_BOARD_DVBSKY_T980C
:
143 case CX23885_BOARD_DVBSKY_S950C
:
144 case CX23885_BOARD_TT_CT2_4500_CI
:
145 case CX23885_BOARD_DVBSKY_S950
:
146 case CX23885_BOARD_DVBSKY_S952
:
147 case CX23885_BOARD_DVBSKY_T982
:
148 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4
:
150 * The IR controller on this board only returns pulse widths.
151 * Any other mode setting will fail to set up the device.
153 params
.mode
= V4L2_SUBDEV_IR_MODE_PULSE_WIDTH
;
154 params
.enable
= true;
155 params
.interrupt_enable
= true;
156 params
.shutdown
= false;
158 /* Setup for baseband compatible with both RC-5 and RC-6A */
159 params
.modulation
= false;
160 /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/
161 /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/
162 params
.max_pulse_width
= 3333333; /* ns */
163 /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
164 /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
165 params
.noise_filter_min_width
= 333333; /* ns */
167 * This board has inverted receive sense:
168 * mark is received as low logic level;
169 * falling edges are detected as rising edges; etc.
171 params
.invert_level
= true;
173 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL
:
174 case CX23885_BOARD_TEVII_S470
:
175 case CX23885_BOARD_TBS_6980
:
176 case CX23885_BOARD_TBS_6981
:
178 * The IR controller on this board only returns pulse widths.
179 * Any other mode setting will fail to set up the device.
181 params
.mode
= V4L2_SUBDEV_IR_MODE_PULSE_WIDTH
;
182 params
.enable
= true;
183 params
.interrupt_enable
= true;
184 params
.shutdown
= false;
186 /* Setup for a standard NEC protocol */
187 params
.carrier_freq
= 37917; /* Hz, 455 kHz/12 for NEC */
188 params
.carrier_range_lower
= 33000; /* Hz */
189 params
.carrier_range_upper
= 43000; /* Hz */
190 params
.duty_cycle
= 33; /* percent, 33 percent for NEC */
193 * NEC max pulse width: (64/3)/(455 kHz/12) * 16 nec_units
194 * (64/3)/(455 kHz/12) * 16 nec_units * 1.375 = 12378022 ns
196 params
.max_pulse_width
= 12378022; /* ns */
199 * NEC noise filter min width: (64/3)/(455 kHz/12) * 1 nec_unit
200 * (64/3)/(455 kHz/12) * 1 nec_units * 0.625 = 351648 ns
202 params
.noise_filter_min_width
= 351648; /* ns */
204 params
.modulation
= false;
205 params
.invert_level
= true;
208 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_s_parameters
, ¶ms
);
212 static int cx23885_input_ir_open(struct rc_dev
*rc
)
214 struct cx23885_kernel_ir
*kernel_ir
= rc
->priv
;
216 if (kernel_ir
->cx
== NULL
)
219 return cx23885_input_ir_start(kernel_ir
->cx
);
222 static void cx23885_input_ir_stop(struct cx23885_dev
*dev
)
224 struct v4l2_subdev_ir_parameters params
;
226 if (dev
->sd_ir
== NULL
)
230 * Stop the sd_ir subdevice from generating notifications and
232 * It is shutdown this way in order to mitigate a race with
233 * cx23885_input_rx_work_handler() in the overrun case, which could
234 * re-enable the subdevice.
236 atomic_set(&dev
->ir_input_stopping
, 1);
237 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_g_parameters
, ¶ms
);
238 while (params
.shutdown
== false) {
239 params
.enable
= false;
240 params
.interrupt_enable
= false;
241 params
.shutdown
= true;
242 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_s_parameters
, ¶ms
);
243 v4l2_subdev_call(dev
->sd_ir
, ir
, rx_g_parameters
, ¶ms
);
245 flush_work(&dev
->cx25840_work
);
246 flush_work(&dev
->ir_rx_work
);
247 flush_work(&dev
->ir_tx_work
);
250 static void cx23885_input_ir_close(struct rc_dev
*rc
)
252 struct cx23885_kernel_ir
*kernel_ir
= rc
->priv
;
254 if (kernel_ir
->cx
!= NULL
)
255 cx23885_input_ir_stop(kernel_ir
->cx
);
258 int cx23885_input_init(struct cx23885_dev
*dev
)
260 struct cx23885_kernel_ir
*kernel_ir
;
268 * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't
269 * encapsulated in a v4l2_subdev, then I'm not going to deal with it.
271 if (dev
->sd_ir
== NULL
)
274 switch (dev
->board
) {
275 case CX23885_BOARD_HAUPPAUGE_HVR1270
:
276 case CX23885_BOARD_HAUPPAUGE_HVR1850
:
277 case CX23885_BOARD_HAUPPAUGE_HVR1290
:
278 case CX23885_BOARD_HAUPPAUGE_HVR1250
:
279 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4
:
280 /* Integrated CX2388[58] IR controller */
281 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
282 /* The grey Hauppauge RC-5 remote */
283 rc_map
= RC_MAP_HAUPPAUGE
;
285 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL
:
286 /* Integrated CX23885 IR controller */
287 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
288 /* The grey Terratec remote with orange buttons */
289 rc_map
= RC_MAP_NEC_TERRATEC_CINERGY_XS
;
291 case CX23885_BOARD_TEVII_S470
:
292 /* Integrated CX23885 IR controller */
293 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
294 /* A guess at the remote */
295 rc_map
= RC_MAP_TEVII_NEC
;
297 case CX23885_BOARD_MYGICA_X8507
:
298 /* Integrated CX23885 IR controller */
299 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
300 /* A guess at the remote */
301 rc_map
= RC_MAP_TOTAL_MEDIA_IN_HAND_02
;
303 case CX23885_BOARD_TBS_6980
:
304 case CX23885_BOARD_TBS_6981
:
305 /* Integrated CX23885 IR controller */
306 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
307 /* A guess at the remote */
308 rc_map
= RC_MAP_TBS_NEC
;
310 case CX23885_BOARD_DVBSKY_T9580
:
311 case CX23885_BOARD_DVBSKY_T980C
:
312 case CX23885_BOARD_DVBSKY_S950C
:
313 case CX23885_BOARD_DVBSKY_S950
:
314 case CX23885_BOARD_DVBSKY_S952
:
315 case CX23885_BOARD_DVBSKY_T982
:
316 /* Integrated CX23885 IR controller */
317 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
318 rc_map
= RC_MAP_DVBSKY
;
320 case CX23885_BOARD_TT_CT2_4500_CI
:
321 /* Integrated CX23885 IR controller */
322 allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
323 rc_map
= RC_MAP_TT_1500
;
329 /* cx23885 board instance kernel IR state */
330 kernel_ir
= kzalloc(sizeof(struct cx23885_kernel_ir
), GFP_KERNEL
);
331 if (kernel_ir
== NULL
)
335 kernel_ir
->name
= kasprintf(GFP_KERNEL
, "cx23885 IR (%s)",
336 cx23885_boards
[dev
->board
].name
);
337 if (!kernel_ir
->name
) {
342 kernel_ir
->phys
= kasprintf(GFP_KERNEL
, "pci-%s/ir0",
344 if (!kernel_ir
->phys
) {
346 goto err_out_free_name
;
350 rc
= rc_allocate_device(RC_DRIVER_IR_RAW
);
353 goto err_out_free_phys
;
357 rc
->device_name
= kernel_ir
->name
;
358 rc
->input_phys
= kernel_ir
->phys
;
359 rc
->input_id
.bustype
= BUS_PCI
;
360 rc
->input_id
.version
= 1;
361 if (dev
->pci
->subsystem_vendor
) {
362 rc
->input_id
.vendor
= dev
->pci
->subsystem_vendor
;
363 rc
->input_id
.product
= dev
->pci
->subsystem_device
;
365 rc
->input_id
.vendor
= dev
->pci
->vendor
;
366 rc
->input_id
.product
= dev
->pci
->device
;
368 rc
->dev
.parent
= &dev
->pci
->dev
;
369 rc
->allowed_protocols
= allowed_protos
;
370 rc
->priv
= kernel_ir
;
371 rc
->open
= cx23885_input_ir_open
;
372 rc
->close
= cx23885_input_ir_close
;
373 rc
->map_name
= rc_map
;
374 rc
->driver_name
= MODULE_NAME
;
377 dev
->kernel_ir
= kernel_ir
;
378 ret
= rc_register_device(rc
);
385 cx23885_input_ir_stop(dev
);
386 dev
->kernel_ir
= NULL
;
389 kfree(kernel_ir
->phys
);
391 kfree(kernel_ir
->name
);
397 void cx23885_input_fini(struct cx23885_dev
*dev
)
399 /* Always stop the IR hardware from generating interrupts */
400 cx23885_input_ir_stop(dev
);
402 if (dev
->kernel_ir
== NULL
)
404 rc_unregister_device(dev
->kernel_ir
->rc
);
405 kfree(dev
->kernel_ir
->phys
);
406 kfree(dev
->kernel_ir
->name
);
407 kfree(dev
->kernel_ir
);
408 dev
->kernel_ir
= NULL
;