Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Remote Controller core raw events header
0004  *
0005  * Copyright (C) 2010 by Mauro Carvalho Chehab
0006  */
0007 
0008 #ifndef _RC_CORE_PRIV
0009 #define _RC_CORE_PRIV
0010 
0011 #define RC_DEV_MAX      256
0012 /* Define the max number of pulse/space transitions to buffer */
0013 #define MAX_IR_EVENT_SIZE   512
0014 
0015 #include <linux/slab.h>
0016 #include <uapi/linux/bpf.h>
0017 #include <media/rc-core.h>
0018 
0019 /**
0020  * rc_open - Opens a RC device
0021  *
0022  * @rdev: pointer to struct rc_dev.
0023  */
0024 int rc_open(struct rc_dev *rdev);
0025 
0026 /**
0027  * rc_close - Closes a RC device
0028  *
0029  * @rdev: pointer to struct rc_dev.
0030  */
0031 void rc_close(struct rc_dev *rdev);
0032 
0033 struct ir_raw_handler {
0034     struct list_head list;
0035 
0036     u64 protocols; /* which are handled by this handler */
0037     int (*decode)(struct rc_dev *dev, struct ir_raw_event event);
0038     int (*encode)(enum rc_proto protocol, u32 scancode,
0039               struct ir_raw_event *events, unsigned int max);
0040     u32 carrier;
0041     u32 min_timeout;
0042 
0043     /* These two should only be used by the mce kbd decoder */
0044     int (*raw_register)(struct rc_dev *dev);
0045     int (*raw_unregister)(struct rc_dev *dev);
0046 };
0047 
0048 struct ir_raw_event_ctrl {
0049     struct list_head        list;       /* to keep track of raw clients */
0050     struct task_struct      *thread;
0051     /* fifo for the pulse/space durations */
0052     DECLARE_KFIFO(kfifo, struct ir_raw_event, MAX_IR_EVENT_SIZE);
0053     ktime_t             last_event; /* when last event occurred */
0054     struct rc_dev           *dev;       /* pointer to the parent rc_dev */
0055     /* handle delayed ir_raw_event_store_edge processing */
0056     spinlock_t          edge_spinlock;
0057     struct timer_list       edge_handle;
0058 
0059     /* raw decoder state follows */
0060     struct ir_raw_event prev_ev;
0061     struct ir_raw_event this_ev;
0062 
0063 #ifdef CONFIG_BPF_LIRC_MODE2
0064     u32             bpf_sample;
0065     struct bpf_prog_array __rcu *progs;
0066 #endif
0067 #if IS_ENABLED(CONFIG_IR_NEC_DECODER)
0068     struct nec_dec {
0069         int state;
0070         unsigned count;
0071         u32 bits;
0072         bool is_nec_x;
0073         bool necx_repeat;
0074     } nec;
0075 #endif
0076 #if IS_ENABLED(CONFIG_IR_RC5_DECODER)
0077     struct rc5_dec {
0078         int state;
0079         u32 bits;
0080         unsigned count;
0081         bool is_rc5x;
0082     } rc5;
0083 #endif
0084 #if IS_ENABLED(CONFIG_IR_RC6_DECODER)
0085     struct rc6_dec {
0086         int state;
0087         u8 header;
0088         u32 body;
0089         bool toggle;
0090         unsigned count;
0091         unsigned wanted_bits;
0092     } rc6;
0093 #endif
0094 #if IS_ENABLED(CONFIG_IR_SONY_DECODER)
0095     struct sony_dec {
0096         int state;
0097         u32 bits;
0098         unsigned count;
0099     } sony;
0100 #endif
0101 #if IS_ENABLED(CONFIG_IR_JVC_DECODER)
0102     struct jvc_dec {
0103         int state;
0104         u16 bits;
0105         u16 old_bits;
0106         unsigned count;
0107         bool first;
0108         bool toggle;
0109     } jvc;
0110 #endif
0111 #if IS_ENABLED(CONFIG_IR_SANYO_DECODER)
0112     struct sanyo_dec {
0113         int state;
0114         unsigned count;
0115         u64 bits;
0116     } sanyo;
0117 #endif
0118 #if IS_ENABLED(CONFIG_IR_SHARP_DECODER)
0119     struct sharp_dec {
0120         int state;
0121         unsigned count;
0122         u32 bits;
0123         unsigned int pulse_len;
0124     } sharp;
0125 #endif
0126 #if IS_ENABLED(CONFIG_IR_MCE_KBD_DECODER)
0127     struct mce_kbd_dec {
0128         /* locks key up timer */
0129         spinlock_t keylock;
0130         struct timer_list rx_timeout;
0131         int state;
0132         u8 header;
0133         u32 body;
0134         unsigned count;
0135         unsigned wanted_bits;
0136     } mce_kbd;
0137 #endif
0138 #if IS_ENABLED(CONFIG_IR_XMP_DECODER)
0139     struct xmp_dec {
0140         int state;
0141         unsigned count;
0142         u32 durations[16];
0143     } xmp;
0144 #endif
0145 #if IS_ENABLED(CONFIG_IR_IMON_DECODER)
0146     struct imon_dec {
0147         int state;
0148         int count;
0149         int last_chk;
0150         unsigned int bits;
0151         bool stick_keyboard;
0152     } imon;
0153 #endif
0154 #if IS_ENABLED(CONFIG_IR_RCMM_DECODER)
0155     struct rcmm_dec {
0156         int state;
0157         unsigned int count;
0158         u32 bits;
0159     } rcmm;
0160 #endif
0161 };
0162 
0163 /* Mutex for locking raw IR processing and handler change */
0164 extern struct mutex ir_raw_handler_lock;
0165 
0166 /* macros for IR decoders */
0167 static inline bool geq_margin(unsigned d1, unsigned d2, unsigned margin)
0168 {
0169     return d1 > (d2 - margin);
0170 }
0171 
0172 static inline bool eq_margin(unsigned d1, unsigned d2, unsigned margin)
0173 {
0174     return ((d1 > (d2 - margin)) && (d1 < (d2 + margin)));
0175 }
0176 
0177 static inline bool is_transition(struct ir_raw_event *x, struct ir_raw_event *y)
0178 {
0179     return x->pulse != y->pulse;
0180 }
0181 
0182 static inline void decrease_duration(struct ir_raw_event *ev, unsigned duration)
0183 {
0184     if (duration > ev->duration)
0185         ev->duration = 0;
0186     else
0187         ev->duration -= duration;
0188 }
0189 
0190 /* Returns true if event is normal pulse/space event */
0191 static inline bool is_timing_event(struct ir_raw_event ev)
0192 {
0193     return !ev.carrier_report && !ev.overflow;
0194 }
0195 
0196 #define TO_STR(is_pulse)        ((is_pulse) ? "pulse" : "space")
0197 
0198 /* functions for IR encoders */
0199 bool rc_validate_scancode(enum rc_proto proto, u32 scancode);
0200 
0201 static inline void init_ir_raw_event_duration(struct ir_raw_event *ev,
0202                           unsigned int pulse,
0203                           u32 duration)
0204 {
0205     *ev = (struct ir_raw_event) {
0206         .duration = duration,
0207         .pulse = pulse
0208     };
0209 }
0210 
0211 /**
0212  * struct ir_raw_timings_manchester - Manchester coding timings
0213  * @leader_pulse:   duration of leader pulse (if any) 0 if continuing
0214  *          existing signal
0215  * @leader_space:   duration of leader space (if any)
0216  * @clock:      duration of each pulse/space in ns
0217  * @invert:     if set clock logic is inverted
0218  *          (0 = space + pulse, 1 = pulse + space)
0219  * @trailer_space:  duration of trailer space in ns
0220  */
0221 struct ir_raw_timings_manchester {
0222     unsigned int leader_pulse;
0223     unsigned int leader_space;
0224     unsigned int clock;
0225     unsigned int invert:1;
0226     unsigned int trailer_space;
0227 };
0228 
0229 int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max,
0230               const struct ir_raw_timings_manchester *timings,
0231               unsigned int n, u64 data);
0232 
0233 /**
0234  * ir_raw_gen_pulse_space() - generate pulse and space raw events.
0235  * @ev:         Pointer to pointer to next free raw event.
0236  *          Will be incremented for each raw event written.
0237  * @max:        Pointer to number of raw events available in buffer.
0238  *          Will be decremented for each raw event written.
0239  * @pulse_width:    Width of pulse in ns.
0240  * @space_width:    Width of space in ns.
0241  *
0242  * Returns: 0 on success.
0243  *      -ENOBUFS if there isn't enough buffer space to write both raw
0244  *      events. In this case @max events will have been written.
0245  */
0246 static inline int ir_raw_gen_pulse_space(struct ir_raw_event **ev,
0247                      unsigned int *max,
0248                      unsigned int pulse_width,
0249                      unsigned int space_width)
0250 {
0251     if (!*max)
0252         return -ENOBUFS;
0253     init_ir_raw_event_duration((*ev)++, 1, pulse_width);
0254     if (!--*max)
0255         return -ENOBUFS;
0256     init_ir_raw_event_duration((*ev)++, 0, space_width);
0257     --*max;
0258     return 0;
0259 }
0260 
0261 /**
0262  * struct ir_raw_timings_pd - pulse-distance modulation timings
0263  * @header_pulse:   duration of header pulse in ns (0 for none)
0264  * @header_space:   duration of header space in ns
0265  * @bit_pulse:      duration of bit pulse in ns
0266  * @bit_space:      duration of bit space (for logic 0 and 1) in ns
0267  * @trailer_pulse:  duration of trailer pulse in ns
0268  * @trailer_space:  duration of trailer space in ns
0269  * @msb_first:      1 if most significant bit is sent first
0270  */
0271 struct ir_raw_timings_pd {
0272     unsigned int header_pulse;
0273     unsigned int header_space;
0274     unsigned int bit_pulse;
0275     unsigned int bit_space[2];
0276     unsigned int trailer_pulse;
0277     unsigned int trailer_space;
0278     unsigned int msb_first:1;
0279 };
0280 
0281 int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max,
0282           const struct ir_raw_timings_pd *timings,
0283           unsigned int n, u64 data);
0284 
0285 /**
0286  * struct ir_raw_timings_pl - pulse-length modulation timings
0287  * @header_pulse:   duration of header pulse in ns (0 for none)
0288  * @bit_space:      duration of bit space in ns
0289  * @bit_pulse:      duration of bit pulse (for logic 0 and 1) in ns
0290  * @trailer_space:  duration of trailer space in ns
0291  * @msb_first:      1 if most significant bit is sent first
0292  */
0293 struct ir_raw_timings_pl {
0294     unsigned int header_pulse;
0295     unsigned int bit_space;
0296     unsigned int bit_pulse[2];
0297     unsigned int trailer_space;
0298     unsigned int msb_first:1;
0299 };
0300 
0301 int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max,
0302           const struct ir_raw_timings_pl *timings,
0303           unsigned int n, u64 data);
0304 
0305 /*
0306  * Routines from rc-raw.c to be used internally and by decoders
0307  */
0308 u64 ir_raw_get_allowed_protocols(void);
0309 int ir_raw_event_prepare(struct rc_dev *dev);
0310 int ir_raw_event_register(struct rc_dev *dev);
0311 void ir_raw_event_free(struct rc_dev *dev);
0312 void ir_raw_event_unregister(struct rc_dev *dev);
0313 int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler);
0314 void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler);
0315 void ir_raw_load_modules(u64 *protocols);
0316 void ir_raw_init(void);
0317 
0318 /*
0319  * lirc interface
0320  */
0321 #ifdef CONFIG_LIRC
0322 int lirc_dev_init(void);
0323 void lirc_dev_exit(void);
0324 void lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev);
0325 void lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc);
0326 int lirc_register(struct rc_dev *dev);
0327 void lirc_unregister(struct rc_dev *dev);
0328 struct rc_dev *rc_dev_get_from_fd(int fd);
0329 #else
0330 static inline int lirc_dev_init(void) { return 0; }
0331 static inline void lirc_dev_exit(void) {}
0332 static inline void lirc_raw_event(struct rc_dev *dev,
0333                   struct ir_raw_event ev) { }
0334 static inline void lirc_scancode_event(struct rc_dev *dev,
0335                        struct lirc_scancode *lsc) { }
0336 static inline int lirc_register(struct rc_dev *dev) { return 0; }
0337 static inline void lirc_unregister(struct rc_dev *dev) { }
0338 #endif
0339 
0340 /*
0341  * bpf interface
0342  */
0343 #ifdef CONFIG_BPF_LIRC_MODE2
0344 void lirc_bpf_free(struct rc_dev *dev);
0345 void lirc_bpf_run(struct rc_dev *dev, u32 sample);
0346 #else
0347 static inline void lirc_bpf_free(struct rc_dev *dev) { }
0348 static inline void lirc_bpf_run(struct rc_dev *dev, u32 sample) { }
0349 #endif
0350 
0351 #endif /* _RC_CORE_PRIV */