rc-core-priv.h 8.1 KB

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