1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Remote Controller core raw events header 4 * 5 * Copyright (C) 2010 by Mauro Carvalho Chehab 6 */ 7 8 #ifndef _RC_CORE_PRIV 9 #define _RC_CORE_PRIV 10 11 #define RC_DEV_MAX 256 12 /* Define the max number of pulse/space transitions to buffer */ 13 #define MAX_IR_EVENT_SIZE 512 14 15 #include <linux/slab.h> 16 #include <uapi/linux/bpf.h> 17 #include <media/rc-core.h> 18 19 /** 20 * rc_open - Opens a RC device 21 * 22 * @rdev: pointer to struct rc_dev. 23 */ 24 int rc_open(struct rc_dev *rdev); 25 26 /** 27 * rc_close - Closes a RC device 28 * 29 * @rdev: pointer to struct rc_dev. 30 */ 31 void rc_close(struct rc_dev *rdev); 32 33 struct ir_raw_handler { 34 struct list_head list; 35 36 u64 protocols; /* which are handled by this handler */ 37 int (*decode)(struct rc_dev *dev, struct ir_raw_event event); 38 int (*encode)(enum rc_proto protocol, u32 scancode, 39 struct ir_raw_event *events, unsigned int max); 40 u32 carrier; 41 u32 min_timeout; 42 43 /* These two should only be used by the mce kbd decoder */ 44 int (*raw_register)(struct rc_dev *dev); 45 int (*raw_unregister)(struct rc_dev *dev); 46 }; 47 48 struct ir_raw_event_ctrl { 49 struct list_head list; /* to keep track of raw clients */ 50 struct task_struct *thread; 51 /* fifo for the pulse/space durations */ 52 DECLARE_KFIFO(kfifo, struct ir_raw_event, MAX_IR_EVENT_SIZE); 53 ktime_t last_event; /* when last event occurred */ 54 struct rc_dev *dev; /* pointer to the parent rc_dev */ 55 /* handle delayed ir_raw_event_store_edge processing */ 56 spinlock_t edge_spinlock; 57 struct timer_list edge_handle; 58 59 /* raw decoder state follows */ 60 struct ir_raw_event prev_ev; 61 struct ir_raw_event this_ev; 62 63 #ifdef CONFIG_BPF_LIRC_MODE2 64 u32 bpf_sample; 65 struct bpf_prog_array __rcu *progs; 66 #endif 67 #if IS_ENABLED(CONFIG_IR_NEC_DECODER) 68 struct nec_dec { 69 int state; 70 unsigned count; 71 u32 bits; 72 bool is_nec_x; 73 bool necx_repeat; 74 } nec; 75 #endif 76 #if IS_ENABLED(CONFIG_IR_RC5_DECODER) 77 struct rc5_dec { 78 int state; 79 u32 bits; 80 unsigned count; 81 bool is_rc5x; 82 } rc5; 83 #endif 84 #if IS_ENABLED(CONFIG_IR_RC6_DECODER) 85 struct rc6_dec { 86 int state; 87 u8 header; 88 u32 body; 89 bool toggle; 90 unsigned count; 91 unsigned wanted_bits; 92 } rc6; 93 #endif 94 #if IS_ENABLED(CONFIG_IR_SONY_DECODER) 95 struct sony_dec { 96 int state; 97 u32 bits; 98 unsigned count; 99 } sony; 100 #endif 101 #if IS_ENABLED(CONFIG_IR_JVC_DECODER) 102 struct jvc_dec { 103 int state; 104 u16 bits; 105 u16 old_bits; 106 unsigned count; 107 bool first; 108 bool toggle; 109 } jvc; 110 #endif 111 #if IS_ENABLED(CONFIG_IR_SANYO_DECODER) 112 struct sanyo_dec { 113 int state; 114 unsigned count; 115 u64 bits; 116 } sanyo; 117 #endif 118 #if IS_ENABLED(CONFIG_IR_SHARP_DECODER) 119 struct sharp_dec { 120 int state; 121 unsigned count; 122 u32 bits; 123 unsigned int pulse_len; 124 } sharp; 125 #endif 126 #if IS_ENABLED(CONFIG_IR_MCE_KBD_DECODER) 127 struct mce_kbd_dec { 128 /* locks key up timer */ 129 spinlock_t keylock; 130 struct timer_list rx_timeout; 131 int state; 132 u8 header; 133 u32 body; 134 unsigned count; 135 unsigned wanted_bits; 136 } mce_kbd; 137 #endif 138 #if IS_ENABLED(CONFIG_IR_XMP_DECODER) 139 struct xmp_dec { 140 int state; 141 unsigned count; 142 u32 durations[16]; 143 } xmp; 144 #endif 145 #if IS_ENABLED(CONFIG_IR_IMON_DECODER) 146 struct imon_dec { 147 int state; 148 int count; 149 int last_chk; 150 unsigned int bits; 151 bool stick_keyboard; 152 } imon; 153 #endif 154 #if IS_ENABLED(CONFIG_IR_RCMM_DECODER) 155 struct rcmm_dec { 156 int state; 157 unsigned int count; 158 u32 bits; 159 } rcmm; 160 #endif 161 }; 162 163 /* Mutex for locking raw IR processing and handler change */ 164 extern struct mutex ir_raw_handler_lock; 165 166 /* macros for IR decoders */ 167 static inline bool geq_margin(unsigned d1, unsigned d2, unsigned margin) 168 { 169 return d1 > (d2 - margin); 170 } 171 172 static inline bool eq_margin(unsigned d1, unsigned d2, unsigned margin) 173 { 174 return ((d1 > (d2 - margin)) && (d1 < (d2 + margin))); 175 } 176 177 static inline bool is_transition(struct ir_raw_event *x, struct ir_raw_event *y) 178 { 179 return x->pulse != y->pulse; 180 } 181 182 static inline void decrease_duration(struct ir_raw_event *ev, unsigned duration) 183 { 184 if (duration > ev->duration) 185 ev->duration = 0; 186 else 187 ev->duration -= duration; 188 } 189 190 /* Returns true if event is normal pulse/space event */ 191 static inline bool is_timing_event(struct ir_raw_event ev) 192 { 193 return !ev.carrier_report && !ev.reset; 194 } 195 196 #define TO_US(duration) DIV_ROUND_CLOSEST((duration), 1000) 197 #define TO_STR(is_pulse) ((is_pulse) ? "pulse" : "space") 198 199 /* functions for IR encoders */ 200 bool rc_validate_scancode(enum rc_proto proto, u32 scancode); 201 202 static inline void init_ir_raw_event_duration(struct ir_raw_event *ev, 203 unsigned int pulse, 204 u32 duration) 205 { 206 *ev = (struct ir_raw_event) { 207 .duration = duration, 208 .pulse = pulse 209 }; 210 } 211 212 /** 213 * struct ir_raw_timings_manchester - Manchester coding timings 214 * @leader_pulse: duration of leader pulse (if any) 0 if continuing 215 * existing signal 216 * @leader_space: duration of leader space (if any) 217 * @clock: duration of each pulse/space in ns 218 * @invert: if set clock logic is inverted 219 * (0 = space + pulse, 1 = pulse + space) 220 * @trailer_space: duration of trailer space in ns 221 */ 222 struct ir_raw_timings_manchester { 223 unsigned int leader_pulse; 224 unsigned int leader_space; 225 unsigned int clock; 226 unsigned int invert:1; 227 unsigned int trailer_space; 228 }; 229 230 int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max, 231 const struct ir_raw_timings_manchester *timings, 232 unsigned int n, u64 data); 233 234 /** 235 * ir_raw_gen_pulse_space() - generate pulse and space raw events. 236 * @ev: Pointer to pointer to next free raw event. 237 * Will be incremented for each raw event written. 238 * @max: Pointer to number of raw events available in buffer. 239 * Will be decremented for each raw event written. 240 * @pulse_width: Width of pulse in ns. 241 * @space_width: Width of space in ns. 242 * 243 * Returns: 0 on success. 244 * -ENOBUFS if there isn't enough buffer space to write both raw 245 * events. In this case @max events will have been written. 246 */ 247 static inline int ir_raw_gen_pulse_space(struct ir_raw_event **ev, 248 unsigned int *max, 249 unsigned int pulse_width, 250 unsigned int space_width) 251 { 252 if (!*max) 253 return -ENOBUFS; 254 init_ir_raw_event_duration((*ev)++, 1, pulse_width); 255 if (!--*max) 256 return -ENOBUFS; 257 init_ir_raw_event_duration((*ev)++, 0, space_width); 258 --*max; 259 return 0; 260 } 261 262 /** 263 * struct ir_raw_timings_pd - pulse-distance modulation timings 264 * @header_pulse: duration of header pulse in ns (0 for none) 265 * @header_space: duration of header space in ns 266 * @bit_pulse: duration of bit pulse in ns 267 * @bit_space: duration of bit space (for logic 0 and 1) in ns 268 * @trailer_pulse: duration of trailer pulse in ns 269 * @trailer_space: duration of trailer space in ns 270 * @msb_first: 1 if most significant bit is sent first 271 */ 272 struct ir_raw_timings_pd { 273 unsigned int header_pulse; 274 unsigned int header_space; 275 unsigned int bit_pulse; 276 unsigned int bit_space[2]; 277 unsigned int trailer_pulse; 278 unsigned int trailer_space; 279 unsigned int msb_first:1; 280 }; 281 282 int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max, 283 const struct ir_raw_timings_pd *timings, 284 unsigned int n, u64 data); 285 286 /** 287 * struct ir_raw_timings_pl - pulse-length modulation timings 288 * @header_pulse: duration of header pulse in ns (0 for none) 289 * @bit_space: duration of bit space in ns 290 * @bit_pulse: duration of bit pulse (for logic 0 and 1) in ns 291 * @trailer_space: duration of trailer space in ns 292 * @msb_first: 1 if most significant bit is sent first 293 */ 294 struct ir_raw_timings_pl { 295 unsigned int header_pulse; 296 unsigned int bit_space; 297 unsigned int bit_pulse[2]; 298 unsigned int trailer_space; 299 unsigned int msb_first:1; 300 }; 301 302 int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max, 303 const struct ir_raw_timings_pl *timings, 304 unsigned int n, u64 data); 305 306 /* 307 * Routines from rc-raw.c to be used internally and by decoders 308 */ 309 u64 ir_raw_get_allowed_protocols(void); 310 int ir_raw_event_prepare(struct rc_dev *dev); 311 int ir_raw_event_register(struct rc_dev *dev); 312 void ir_raw_event_free(struct rc_dev *dev); 313 void ir_raw_event_unregister(struct rc_dev *dev); 314 int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler); 315 void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler); 316 void ir_raw_load_modules(u64 *protocols); 317 void ir_raw_init(void); 318 319 /* 320 * lirc interface 321 */ 322 #ifdef CONFIG_LIRC 323 int lirc_dev_init(void); 324 void lirc_dev_exit(void); 325 void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev); 326 void ir_lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc); 327 int ir_lirc_register(struct rc_dev *dev); 328 void ir_lirc_unregister(struct rc_dev *dev); 329 struct rc_dev *rc_dev_get_from_fd(int fd); 330 #else 331 static inline int lirc_dev_init(void) { return 0; } 332 static inline void lirc_dev_exit(void) {} 333 static inline void ir_lirc_raw_event(struct rc_dev *dev, 334 struct ir_raw_event ev) { } 335 static inline void ir_lirc_scancode_event(struct rc_dev *dev, 336 struct lirc_scancode *lsc) { } 337 static inline int ir_lirc_register(struct rc_dev *dev) { return 0; } 338 static inline void ir_lirc_unregister(struct rc_dev *dev) { } 339 #endif 340 341 /* 342 * bpf interface 343 */ 344 #ifdef CONFIG_BPF_LIRC_MODE2 345 void lirc_bpf_free(struct rc_dev *dev); 346 void lirc_bpf_run(struct rc_dev *dev, u32 sample); 347 #else 348 static inline void lirc_bpf_free(struct rc_dev *dev) { } 349 static inline void lirc_bpf_run(struct rc_dev *dev, u32 sample) { } 350 #endif 351 352 #endif /* _RC_CORE_PRIV */ 353