rc-core-priv.h 9.0 KB

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