1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Loopback driver for rc-core,
5 * Copyright (c) 2010 David Härdeman <david@hardeman.nu>
7 * This driver receives TX data and passes it back as RX data,
8 * which is useful for (scripted) debugging of rc-core without
9 * having to use actual hardware.
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <media/rc-core.h>
18 #define DRIVER_NAME "rc-loopback"
19 #define RXMASK_NARROWBAND 0x1
20 #define RXMASK_WIDEBAND 0x2
34 static struct loopback_dev loopdev
;
36 static int loop_set_tx_mask(struct rc_dev
*dev
, u32 mask
)
38 struct loopback_dev
*lodev
= dev
->priv
;
40 if ((mask
& (RXMASK_NARROWBAND
| RXMASK_WIDEBAND
)) != mask
) {
41 dev_dbg(&dev
->dev
, "invalid tx mask: %u\n", mask
);
45 dev_dbg(&dev
->dev
, "setting tx mask: %u\n", mask
);
50 static int loop_set_tx_carrier(struct rc_dev
*dev
, u32 carrier
)
52 struct loopback_dev
*lodev
= dev
->priv
;
54 dev_dbg(&dev
->dev
, "setting tx carrier: %u\n", carrier
);
55 lodev
->txcarrier
= carrier
;
59 static int loop_set_tx_duty_cycle(struct rc_dev
*dev
, u32 duty_cycle
)
61 struct loopback_dev
*lodev
= dev
->priv
;
63 if (duty_cycle
< 1 || duty_cycle
> 99) {
64 dev_dbg(&dev
->dev
, "invalid duty cycle: %u\n", duty_cycle
);
68 dev_dbg(&dev
->dev
, "setting duty cycle: %u\n", duty_cycle
);
69 lodev
->txduty
= duty_cycle
;
73 static int loop_set_rx_carrier_range(struct rc_dev
*dev
, u32 min
, u32 max
)
75 struct loopback_dev
*lodev
= dev
->priv
;
77 if (min
< 1 || min
> max
) {
78 dev_dbg(&dev
->dev
, "invalid rx carrier range %u to %u\n", min
, max
);
82 dev_dbg(&dev
->dev
, "setting rx carrier range %u to %u\n", min
, max
);
83 lodev
->rxcarriermin
= min
;
84 lodev
->rxcarriermax
= max
;
88 static int loop_tx_ir(struct rc_dev
*dev
, unsigned *txbuf
, unsigned count
)
90 struct loopback_dev
*lodev
= dev
->priv
;
93 struct ir_raw_event rawir
= {};
95 if (lodev
->txcarrier
< lodev
->rxcarriermin
||
96 lodev
->txcarrier
> lodev
->rxcarriermax
) {
97 dev_dbg(&dev
->dev
, "ignoring tx, carrier out of range\n");
102 rxmask
= RXMASK_WIDEBAND
;
104 rxmask
= RXMASK_NARROWBAND
;
106 if (!(rxmask
& lodev
->txmask
)) {
107 dev_dbg(&dev
->dev
, "ignoring tx, rx mask mismatch\n");
111 for (i
= 0; i
< count
; i
++) {
112 rawir
.pulse
= i
% 2 ? false : true;
113 rawir
.duration
= txbuf
[i
];
115 /* simulate overflow if ridiculously long pulse was sent */
116 if (rawir
.pulse
&& rawir
.duration
> MS_TO_US(50))
117 ir_raw_event_overflow(dev
);
119 ir_raw_event_store_with_filter(dev
, &rawir
);
122 if (lodev
->carrierreport
) {
124 rawir
.carrier_report
= true;
125 rawir
.carrier
= lodev
->txcarrier
;
127 ir_raw_event_store(dev
, &rawir
);
130 /* Fake a silence long enough to cause us to go idle */
132 rawir
.duration
= dev
->timeout
;
133 ir_raw_event_store_with_filter(dev
, &rawir
);
135 ir_raw_event_handle(dev
);
141 static void loop_set_idle(struct rc_dev
*dev
, bool enable
)
143 struct loopback_dev
*lodev
= dev
->priv
;
145 if (lodev
->idle
!= enable
) {
146 dev_dbg(&dev
->dev
, "%sing idle mode\n", enable
? "enter" : "exit");
147 lodev
->idle
= enable
;
151 static int loop_set_wideband_receiver(struct rc_dev
*dev
, int enable
)
153 struct loopback_dev
*lodev
= dev
->priv
;
155 if (lodev
->wideband
!= enable
) {
156 dev_dbg(&dev
->dev
, "using %sband receiver\n", enable
? "wide" : "narrow");
157 lodev
->wideband
= !!enable
;
163 static int loop_set_carrier_report(struct rc_dev
*dev
, int enable
)
165 struct loopback_dev
*lodev
= dev
->priv
;
167 if (lodev
->carrierreport
!= enable
) {
168 dev_dbg(&dev
->dev
, "%sabling carrier reports\n", enable
? "en" : "dis");
169 lodev
->carrierreport
= !!enable
;
175 static int loop_set_wakeup_filter(struct rc_dev
*dev
,
176 struct rc_scancode_filter
*sc
)
178 static const unsigned int max
= 512;
179 struct ir_raw_event
*raw
;
183 /* fine to disable filter */
187 /* encode the specified filter and loop it back */
188 raw
= kmalloc_array(max
, sizeof(*raw
), GFP_KERNEL
);
192 ret
= ir_raw_encode_scancode(dev
->wakeup_protocol
, sc
->data
, raw
, max
);
193 /* still loop back the partial raw IR even if it's incomplete */
197 /* do the loopback */
198 for (i
= 0; i
< ret
; ++i
)
199 ir_raw_event_store(dev
, &raw
[i
]);
200 ir_raw_event_handle(dev
);
210 static int __init
loop_init(void)
215 rc
= rc_allocate_device(RC_DRIVER_IR_RAW
);
219 rc
->device_name
= "rc-core loopback device";
220 rc
->input_phys
= "rc-core/virtual";
221 rc
->input_id
.bustype
= BUS_VIRTUAL
;
222 rc
->input_id
.version
= 1;
223 rc
->driver_name
= DRIVER_NAME
;
224 rc
->map_name
= RC_MAP_EMPTY
;
226 rc
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
227 rc
->allowed_wakeup_protocols
= RC_PROTO_BIT_ALL_IR_ENCODER
;
228 rc
->encode_wakeup
= true;
229 rc
->timeout
= IR_DEFAULT_TIMEOUT
;
231 rc
->max_timeout
= IR_MAX_TIMEOUT
;
232 rc
->rx_resolution
= 1;
233 rc
->s_tx_mask
= loop_set_tx_mask
;
234 rc
->s_tx_carrier
= loop_set_tx_carrier
;
235 rc
->s_tx_duty_cycle
= loop_set_tx_duty_cycle
;
236 rc
->s_rx_carrier_range
= loop_set_rx_carrier_range
;
237 rc
->tx_ir
= loop_tx_ir
;
238 rc
->s_idle
= loop_set_idle
;
239 rc
->s_wideband_receiver
= loop_set_wideband_receiver
;
240 rc
->s_carrier_report
= loop_set_carrier_report
;
241 rc
->s_wakeup_filter
= loop_set_wakeup_filter
;
243 loopdev
.txmask
= RXMASK_NARROWBAND
;
244 loopdev
.txcarrier
= 36000;
246 loopdev
.rxcarriermin
= 1;
247 loopdev
.rxcarriermax
= ~0;
249 loopdev
.wideband
= false;
250 loopdev
.carrierreport
= false;
252 ret
= rc_register_device(rc
);
254 dev_err(&rc
->dev
, "rc_dev registration failed\n");
263 static void __exit
loop_exit(void)
265 rc_unregister_device(loopdev
.dev
);
268 module_init(loop_init
);
269 module_exit(loop_exit
);
271 MODULE_DESCRIPTION("Loopback device for rc-core debugging");
272 MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
273 MODULE_LICENSE("GPL");