rc-core-priv.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * SPDX-License-Identifier: GPL-2.0
  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. struct nec_dec {
  57. int state;
  58. unsigned count;
  59. u32 bits;
  60. bool is_nec_x;
  61. bool necx_repeat;
  62. } nec;
  63. struct rc5_dec {
  64. int state;
  65. u32 bits;
  66. unsigned count;
  67. bool is_rc5x;
  68. } rc5;
  69. struct rc6_dec {
  70. int state;
  71. u8 header;
  72. u32 body;
  73. bool toggle;
  74. unsigned count;
  75. unsigned wanted_bits;
  76. } rc6;
  77. struct sony_dec {
  78. int state;
  79. u32 bits;
  80. unsigned count;
  81. } sony;
  82. struct jvc_dec {
  83. int state;
  84. u16 bits;
  85. u16 old_bits;
  86. unsigned count;
  87. bool first;
  88. bool toggle;
  89. } jvc;
  90. struct sanyo_dec {
  91. int state;
  92. unsigned count;
  93. u64 bits;
  94. } sanyo;
  95. struct sharp_dec {
  96. int state;
  97. unsigned count;
  98. u32 bits;
  99. unsigned int pulse_len;
  100. } sharp;
  101. struct mce_kbd_dec {
  102. struct input_dev *idev;
  103. /* locks key up timer */
  104. spinlock_t keylock;
  105. struct timer_list rx_timeout;
  106. char name[64];
  107. char phys[64];
  108. int state;
  109. u8 header;
  110. u32 body;
  111. unsigned count;
  112. unsigned wanted_bits;
  113. } mce_kbd;
  114. struct xmp_dec {
  115. int state;
  116. unsigned count;
  117. u32 durations[16];
  118. } xmp;
  119. struct imon_dec {
  120. int state;
  121. int count;
  122. int last_chk;
  123. unsigned int bits;
  124. bool stick_keyboard;
  125. struct input_dev *idev;
  126. char name[64];
  127. } imon;
  128. };
  129. /* Mutex for locking raw IR processing and handler change */
  130. extern struct mutex ir_raw_handler_lock;
  131. /* macros for IR decoders */
  132. static inline bool geq_margin(unsigned d1, unsigned d2, unsigned margin)
  133. {
  134. return d1 > (d2 - margin);
  135. }
  136. static inline bool eq_margin(unsigned d1, unsigned d2, unsigned margin)
  137. {
  138. return ((d1 > (d2 - margin)) && (d1 < (d2 + margin)));
  139. }
  140. static inline bool is_transition(struct ir_raw_event *x, struct ir_raw_event *y)
  141. {
  142. return x->pulse != y->pulse;
  143. }
  144. static inline void decrease_duration(struct ir_raw_event *ev, unsigned duration)
  145. {
  146. if (duration > ev->duration)
  147. ev->duration = 0;
  148. else
  149. ev->duration -= duration;
  150. }
  151. /* Returns true if event is normal pulse/space event */
  152. static inline bool is_timing_event(struct ir_raw_event ev)
  153. {
  154. return !ev.carrier_report && !ev.reset;
  155. }
  156. #define TO_US(duration) DIV_ROUND_CLOSEST((duration), 1000)
  157. #define TO_STR(is_pulse) ((is_pulse) ? "pulse" : "space")
  158. /* functions for IR encoders */
  159. bool rc_validate_scancode(enum rc_proto proto, u32 scancode);
  160. static inline void init_ir_raw_event_duration(struct ir_raw_event *ev,
  161. unsigned int pulse,
  162. u32 duration)
  163. {
  164. init_ir_raw_event(ev);
  165. ev->duration = duration;
  166. ev->pulse = pulse;
  167. }
  168. /**
  169. * struct ir_raw_timings_manchester - Manchester coding timings
  170. * @leader_pulse: duration of leader pulse (if any) 0 if continuing
  171. * existing signal
  172. * @leader_space: duration of leader space (if any)
  173. * @clock: duration of each pulse/space in ns
  174. * @invert: if set clock logic is inverted
  175. * (0 = space + pulse, 1 = pulse + space)
  176. * @trailer_space: duration of trailer space in ns
  177. */
  178. struct ir_raw_timings_manchester {
  179. unsigned int leader_pulse;
  180. unsigned int leader_space;
  181. unsigned int clock;
  182. unsigned int invert:1;
  183. unsigned int trailer_space;
  184. };
  185. int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max,
  186. const struct ir_raw_timings_manchester *timings,
  187. unsigned int n, u64 data);
  188. /**
  189. * ir_raw_gen_pulse_space() - generate pulse and space raw events.
  190. * @ev: Pointer to pointer to next free raw event.
  191. * Will be incremented for each raw event written.
  192. * @max: Pointer to number of raw events available in buffer.
  193. * Will be decremented for each raw event written.
  194. * @pulse_width: Width of pulse in ns.
  195. * @space_width: Width of space in ns.
  196. *
  197. * Returns: 0 on success.
  198. * -ENOBUFS if there isn't enough buffer space to write both raw
  199. * events. In this case @max events will have been written.
  200. */
  201. static inline int ir_raw_gen_pulse_space(struct ir_raw_event **ev,
  202. unsigned int *max,
  203. unsigned int pulse_width,
  204. unsigned int space_width)
  205. {
  206. if (!*max)
  207. return -ENOBUFS;
  208. init_ir_raw_event_duration((*ev)++, 1, pulse_width);
  209. if (!--*max)
  210. return -ENOBUFS;
  211. init_ir_raw_event_duration((*ev)++, 0, space_width);
  212. --*max;
  213. return 0;
  214. }
  215. /**
  216. * struct ir_raw_timings_pd - pulse-distance modulation timings
  217. * @header_pulse: duration of header pulse in ns (0 for none)
  218. * @header_space: duration of header space in ns
  219. * @bit_pulse: duration of bit pulse in ns
  220. * @bit_space: duration of bit space (for logic 0 and 1) in ns
  221. * @trailer_pulse: duration of trailer pulse in ns
  222. * @trailer_space: duration of trailer space in ns
  223. * @msb_first: 1 if most significant bit is sent first
  224. */
  225. struct ir_raw_timings_pd {
  226. unsigned int header_pulse;
  227. unsigned int header_space;
  228. unsigned int bit_pulse;
  229. unsigned int bit_space[2];
  230. unsigned int trailer_pulse;
  231. unsigned int trailer_space;
  232. unsigned int msb_first:1;
  233. };
  234. int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max,
  235. const struct ir_raw_timings_pd *timings,
  236. unsigned int n, u64 data);
  237. /**
  238. * struct ir_raw_timings_pl - pulse-length modulation timings
  239. * @header_pulse: duration of header pulse in ns (0 for none)
  240. * @bit_space: duration of bit space in ns
  241. * @bit_pulse: duration of bit pulse (for logic 0 and 1) in ns
  242. * @trailer_space: duration of trailer space in ns
  243. * @msb_first: 1 if most significant bit is sent first
  244. */
  245. struct ir_raw_timings_pl {
  246. unsigned int header_pulse;
  247. unsigned int bit_space;
  248. unsigned int bit_pulse[2];
  249. unsigned int trailer_space;
  250. unsigned int msb_first:1;
  251. };
  252. int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max,
  253. const struct ir_raw_timings_pl *timings,
  254. unsigned int n, u64 data);
  255. /*
  256. * Routines from rc-raw.c to be used internally and by decoders
  257. */
  258. u64 ir_raw_get_allowed_protocols(void);
  259. int ir_raw_event_prepare(struct rc_dev *dev);
  260. int ir_raw_event_register(struct rc_dev *dev);
  261. void ir_raw_event_free(struct rc_dev *dev);
  262. void ir_raw_event_unregister(struct rc_dev *dev);
  263. int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler);
  264. void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler);
  265. void ir_raw_load_modules(u64 *protocols);
  266. void ir_raw_init(void);
  267. /*
  268. * lirc interface
  269. */
  270. #ifdef CONFIG_LIRC
  271. int lirc_dev_init(void);
  272. void lirc_dev_exit(void);
  273. void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev);
  274. void ir_lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc);
  275. int ir_lirc_register(struct rc_dev *dev);
  276. void ir_lirc_unregister(struct rc_dev *dev);
  277. struct rc_dev *rc_dev_get_from_fd(int fd);
  278. #else
  279. static inline int lirc_dev_init(void) { return 0; }
  280. static inline void lirc_dev_exit(void) {}
  281. static inline void ir_lirc_raw_event(struct rc_dev *dev,
  282. struct ir_raw_event ev) { }
  283. static inline void ir_lirc_scancode_event(struct rc_dev *dev,
  284. struct lirc_scancode *lsc) { }
  285. static inline int ir_lirc_register(struct rc_dev *dev) { return 0; }
  286. static inline void ir_lirc_unregister(struct rc_dev *dev) { }
  287. #endif
  288. /*
  289. * bpf interface
  290. */
  291. #ifdef CONFIG_BPF_LIRC_MODE2
  292. void lirc_bpf_free(struct rc_dev *dev);
  293. void lirc_bpf_run(struct rc_dev *dev, u32 sample);
  294. #else
  295. static inline void lirc_bpf_free(struct rc_dev *dev) { }
  296. static inline void lirc_bpf_run(struct rc_dev *dev, u32 sample) { }
  297. #endif
  298. #endif /* _RC_CORE_PRIV */