rc-core-priv.h 7.4 KB

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