rc-core-priv.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Remote Controller core raw events header
  3. //
  4. // Copyright (C) 2010 by Mauro Carvalho Chehab
  5. #ifndef _RC_CORE_PRIV
  6. #define _RC_CORE_PRIV
  7. /* Define the max number of pulse/space transitions to buffer */
  8. #define MAX_IR_EVENT_SIZE 512
  9. #include <linux/slab.h>
  10. #include <media/rc-core.h>
  11. /**
  12. * rc_open - Opens a RC device
  13. *
  14. * @rdev: pointer to struct rc_dev.
  15. */
  16. int rc_open(struct rc_dev *rdev);
  17. /**
  18. * rc_close - Closes a RC device
  19. *
  20. * @rdev: pointer to struct rc_dev.
  21. */
  22. void rc_close(struct rc_dev *rdev);
  23. struct ir_raw_handler {
  24. struct list_head list;
  25. u64 protocols; /* which are handled by this handler */
  26. int (*decode)(struct rc_dev *dev, struct ir_raw_event event);
  27. int (*encode)(enum rc_proto protocol, u32 scancode,
  28. struct ir_raw_event *events, unsigned int max);
  29. u32 carrier;
  30. /* These two should only be used by the mce kbd decoder */
  31. int (*raw_register)(struct rc_dev *dev);
  32. int (*raw_unregister)(struct rc_dev *dev);
  33. };
  34. struct ir_raw_event_ctrl {
  35. struct list_head list; /* to keep track of raw clients */
  36. struct task_struct *thread;
  37. /* fifo for the pulse/space durations */
  38. DECLARE_KFIFO(kfifo, struct ir_raw_event, MAX_IR_EVENT_SIZE);
  39. ktime_t last_event; /* when last event occurred */
  40. struct rc_dev *dev; /* pointer to the parent rc_dev */
  41. /* edge driver */
  42. struct timer_list edge_handle;
  43. /* raw decoder state follows */
  44. struct ir_raw_event prev_ev;
  45. struct ir_raw_event this_ev;
  46. struct nec_dec {
  47. int state;
  48. unsigned count;
  49. u32 bits;
  50. bool is_nec_x;
  51. bool necx_repeat;
  52. } nec;
  53. struct rc5_dec {
  54. int state;
  55. u32 bits;
  56. unsigned count;
  57. bool is_rc5x;
  58. } rc5;
  59. struct rc6_dec {
  60. int state;
  61. u8 header;
  62. u32 body;
  63. bool toggle;
  64. unsigned count;
  65. unsigned wanted_bits;
  66. } rc6;
  67. struct sony_dec {
  68. int state;
  69. u32 bits;
  70. unsigned count;
  71. } sony;
  72. struct jvc_dec {
  73. int state;
  74. u16 bits;
  75. u16 old_bits;
  76. unsigned count;
  77. bool first;
  78. bool toggle;
  79. } jvc;
  80. struct sanyo_dec {
  81. int state;
  82. unsigned count;
  83. u64 bits;
  84. } sanyo;
  85. struct sharp_dec {
  86. int state;
  87. unsigned count;
  88. u32 bits;
  89. unsigned int pulse_len;
  90. } sharp;
  91. struct mce_kbd_dec {
  92. struct input_dev *idev;
  93. struct timer_list rx_timeout;
  94. char name[64];
  95. char phys[64];
  96. int state;
  97. u8 header;
  98. u32 body;
  99. unsigned count;
  100. unsigned wanted_bits;
  101. } mce_kbd;
  102. struct xmp_dec {
  103. int state;
  104. unsigned count;
  105. u32 durations[16];
  106. } xmp;
  107. };
  108. /* macros for IR decoders */
  109. static inline bool geq_margin(unsigned d1, unsigned d2, unsigned margin)
  110. {
  111. return d1 > (d2 - margin);
  112. }
  113. static inline bool eq_margin(unsigned d1, unsigned d2, unsigned margin)
  114. {
  115. return ((d1 > (d2 - margin)) && (d1 < (d2 + margin)));
  116. }
  117. static inline bool is_transition(struct ir_raw_event *x, struct ir_raw_event *y)
  118. {
  119. return x->pulse != y->pulse;
  120. }
  121. static inline void decrease_duration(struct ir_raw_event *ev, unsigned duration)
  122. {
  123. if (duration > ev->duration)
  124. ev->duration = 0;
  125. else
  126. ev->duration -= duration;
  127. }
  128. /* Returns true if event is normal pulse/space event */
  129. static inline bool is_timing_event(struct ir_raw_event ev)
  130. {
  131. return !ev.carrier_report && !ev.reset;
  132. }
  133. #define TO_US(duration) DIV_ROUND_CLOSEST((duration), 1000)
  134. #define TO_STR(is_pulse) ((is_pulse) ? "pulse" : "space")
  135. /* functions for IR encoders */
  136. static inline void init_ir_raw_event_duration(struct ir_raw_event *ev,
  137. unsigned int pulse,
  138. u32 duration)
  139. {
  140. init_ir_raw_event(ev);
  141. ev->duration = duration;
  142. ev->pulse = pulse;
  143. }
  144. /**
  145. * struct ir_raw_timings_manchester - Manchester coding timings
  146. * @leader: duration of leader pulse (if any) 0 if continuing
  147. * existing signal (see @pulse_space_start)
  148. * @pulse_space_start: 1 for starting with pulse (0 for starting with space)
  149. * @clock: duration of each pulse/space in ns
  150. * @invert: if set clock logic is inverted
  151. * (0 = space + pulse, 1 = pulse + space)
  152. * @trailer_space: duration of trailer space in ns
  153. */
  154. struct ir_raw_timings_manchester {
  155. unsigned int leader;
  156. unsigned int pulse_space_start:1;
  157. unsigned int clock;
  158. unsigned int invert:1;
  159. unsigned int trailer_space;
  160. };
  161. int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max,
  162. const struct ir_raw_timings_manchester *timings,
  163. unsigned int n, u64 data);
  164. /**
  165. * ir_raw_gen_pulse_space() - generate pulse and space raw events.
  166. * @ev: Pointer to pointer to next free raw event.
  167. * Will be incremented for each raw event written.
  168. * @max: Pointer to number of raw events available in buffer.
  169. * Will be decremented for each raw event written.
  170. * @pulse_width: Width of pulse in ns.
  171. * @space_width: Width of space in ns.
  172. *
  173. * Returns: 0 on success.
  174. * -ENOBUFS if there isn't enough buffer space to write both raw
  175. * events. In this case @max events will have been written.
  176. */
  177. static inline int ir_raw_gen_pulse_space(struct ir_raw_event **ev,
  178. unsigned int *max,
  179. unsigned int pulse_width,
  180. unsigned int space_width)
  181. {
  182. if (!*max)
  183. return -ENOBUFS;
  184. init_ir_raw_event_duration((*ev)++, 1, pulse_width);
  185. if (!--*max)
  186. return -ENOBUFS;
  187. init_ir_raw_event_duration((*ev)++, 0, space_width);
  188. --*max;
  189. return 0;
  190. }
  191. /**
  192. * struct ir_raw_timings_pd - pulse-distance modulation timings
  193. * @header_pulse: duration of header pulse in ns (0 for none)
  194. * @header_space: duration of header space in ns
  195. * @bit_pulse: duration of bit pulse in ns
  196. * @bit_space: duration of bit space (for logic 0 and 1) in ns
  197. * @trailer_pulse: duration of trailer pulse in ns
  198. * @trailer_space: duration of trailer space in ns
  199. * @msb_first: 1 if most significant bit is sent first
  200. */
  201. struct ir_raw_timings_pd {
  202. unsigned int header_pulse;
  203. unsigned int header_space;
  204. unsigned int bit_pulse;
  205. unsigned int bit_space[2];
  206. unsigned int trailer_pulse;
  207. unsigned int trailer_space;
  208. unsigned int msb_first:1;
  209. };
  210. int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max,
  211. const struct ir_raw_timings_pd *timings,
  212. unsigned int n, u64 data);
  213. /**
  214. * struct ir_raw_timings_pl - pulse-length modulation timings
  215. * @header_pulse: duration of header pulse in ns (0 for none)
  216. * @bit_space: duration of bit space in ns
  217. * @bit_pulse: duration of bit pulse (for logic 0 and 1) in ns
  218. * @trailer_space: duration of trailer space in ns
  219. * @msb_first: 1 if most significant bit is sent first
  220. */
  221. struct ir_raw_timings_pl {
  222. unsigned int header_pulse;
  223. unsigned int bit_space;
  224. unsigned int bit_pulse[2];
  225. unsigned int trailer_space;
  226. unsigned int msb_first:1;
  227. };
  228. int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max,
  229. const struct ir_raw_timings_pl *timings,
  230. unsigned int n, u64 data);
  231. /*
  232. * Routines from rc-raw.c to be used internally and by decoders
  233. */
  234. u64 ir_raw_get_allowed_protocols(void);
  235. int ir_raw_event_prepare(struct rc_dev *dev);
  236. int ir_raw_event_register(struct rc_dev *dev);
  237. void ir_raw_event_free(struct rc_dev *dev);
  238. void ir_raw_event_unregister(struct rc_dev *dev);
  239. int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler);
  240. void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler);
  241. void ir_raw_load_modules(u64 *protocols);
  242. void ir_raw_init(void);
  243. /*
  244. * lirc interface
  245. */
  246. #ifdef CONFIG_LIRC
  247. int lirc_dev_init(void);
  248. void lirc_dev_exit(void);
  249. void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev);
  250. int ir_lirc_register(struct rc_dev *dev);
  251. void ir_lirc_unregister(struct rc_dev *dev);
  252. #else
  253. static inline int lirc_dev_init(void) { return 0; }
  254. static inline void lirc_dev_exit(void) {}
  255. static inline void ir_lirc_raw_event(struct rc_dev *dev,
  256. struct ir_raw_event ev) { }
  257. static inline int ir_lirc_register(struct rc_dev *dev) { return 0; }
  258. static inline void ir_lirc_unregister(struct rc_dev *dev) { }
  259. #endif
  260. /*
  261. * Decoder initialization code
  262. *
  263. * Those load logic are called during ir-core init, and automatically
  264. * loads the compiled decoders for their usage with IR raw events
  265. */
  266. #endif /* _RC_CORE_PRIV */