rc-core.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Remote Controller core header
  3. *
  4. * Copyright (C) 2009-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
  16. #define _RC_CORE
  17. #include <linux/spinlock.h>
  18. #include <linux/kfifo.h>
  19. #include <linux/time.h>
  20. #include <linux/timer.h>
  21. #include <media/lirc_dev.h>
  22. #include <media/rc-map.h>
  23. extern int rc_core_debug;
  24. #define IR_dprintk(level, fmt, ...) \
  25. do { \
  26. if (rc_core_debug >= level) \
  27. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
  28. } while (0)
  29. /**
  30. * enum rc_driver_type - type of the RC output
  31. *
  32. * @RC_DRIVER_SCANCODE: Driver or hardware generates a scancode
  33. * @RC_DRIVER_IR_RAW: Driver or hardware generates pulse/space sequences.
  34. * It needs a Infra-Red pulse/space decoder
  35. * @RC_DRIVER_IR_RAW_TX: Device transmitter only,
  36. * driver requires pulse/space data sequence.
  37. */
  38. enum rc_driver_type {
  39. RC_DRIVER_SCANCODE = 0,
  40. RC_DRIVER_IR_RAW,
  41. RC_DRIVER_IR_RAW_TX,
  42. };
  43. /**
  44. * struct rc_scancode_filter - Filter scan codes.
  45. * @data: Scancode data to match.
  46. * @mask: Mask of bits of scancode to compare.
  47. */
  48. struct rc_scancode_filter {
  49. u32 data;
  50. u32 mask;
  51. };
  52. /**
  53. * enum rc_filter_type - Filter type constants.
  54. * @RC_FILTER_NORMAL: Filter for normal operation.
  55. * @RC_FILTER_WAKEUP: Filter for waking from suspend.
  56. * @RC_FILTER_MAX: Number of filter types.
  57. */
  58. enum rc_filter_type {
  59. RC_FILTER_NORMAL = 0,
  60. RC_FILTER_WAKEUP,
  61. RC_FILTER_MAX
  62. };
  63. /**
  64. * struct rc_dev - represents a remote control device
  65. * @dev: driver model's view of this device
  66. * @managed_alloc: devm_rc_allocate_device was used to create rc_dev
  67. * @sysfs_groups: sysfs attribute groups
  68. * @device_name: name of the rc child device
  69. * @input_phys: physical path to the input child device
  70. * @input_id: id of the input child device (struct input_id)
  71. * @driver_name: name of the hardware driver which registered this device
  72. * @map_name: name of the default keymap
  73. * @rc_map: current scan/key table
  74. * @lock: used to ensure we've filled in all protocol details before
  75. * anyone can call show_protocols or store_protocols
  76. * @minor: unique minor remote control device number
  77. * @raw: additional data for raw pulse/space devices
  78. * @input_dev: the input child device used to communicate events to userspace
  79. * @driver_type: specifies if protocol decoding is done in hardware or software
  80. * @idle: used to keep track of RX state
  81. * @encode_wakeup: wakeup filtering uses IR encode API, therefore the allowed
  82. * wakeup protocols is the set of all raw encoders
  83. * @allowed_protocols: bitmask with the supported RC_PROTO_BIT_* protocols
  84. * @enabled_protocols: bitmask with the enabled RC_PROTO_BIT_* protocols
  85. * @allowed_wakeup_protocols: bitmask with the supported RC_PROTO_BIT_* wakeup
  86. * protocols
  87. * @wakeup_protocol: the enabled RC_PROTO_* wakeup protocol or
  88. * RC_PROTO_UNKNOWN if disabled.
  89. * @scancode_filter: scancode filter
  90. * @scancode_wakeup_filter: scancode wakeup filters
  91. * @scancode_mask: some hardware decoders are not capable of providing the full
  92. * scancode to the application. As this is a hardware limit, we can't do
  93. * anything with it. Yet, as the same keycode table can be used with other
  94. * devices, a mask is provided to allow its usage. Drivers should generally
  95. * leave this field in blank
  96. * @users: number of current users of the device
  97. * @priv: driver-specific data
  98. * @keylock: protects the remaining members of the struct
  99. * @keypressed: whether a key is currently pressed
  100. * @keyup_jiffies: time (in jiffies) when the current keypress should be released
  101. * @timer_keyup: timer for releasing a keypress
  102. * @last_keycode: keycode of last keypress
  103. * @last_protocol: protocol of last keypress
  104. * @last_scancode: scancode of last keypress
  105. * @last_toggle: toggle value of last command
  106. * @timeout: optional time after which device stops sending data
  107. * @min_timeout: minimum timeout supported by device
  108. * @max_timeout: maximum timeout supported by device
  109. * @rx_resolution : resolution (in ns) of input sampler
  110. * @tx_resolution: resolution (in ns) of output sampler
  111. * @lirc_dev: lirc char device
  112. * @carrier_low: when setting the carrier range, first the low end must be
  113. * set with an ioctl and then the high end with another ioctl
  114. * @gap_start: time when gap starts
  115. * @gap_duration: duration of initial gap
  116. * @gap: true if we're in a gap
  117. * @send_timeout_reports: report timeouts in lirc raw IR.
  118. * @send_mode: lirc mode for sending, either LIRC_MODE_SCANCODE or
  119. * LIRC_MODE_PULSE
  120. * @change_protocol: allow changing the protocol used on hardware decoders
  121. * @open: callback to allow drivers to enable polling/irq when IR input device
  122. * is opened.
  123. * @close: callback to allow drivers to disable polling/irq when IR input device
  124. * is opened.
  125. * @s_tx_mask: set transmitter mask (for devices with multiple tx outputs)
  126. * @s_tx_carrier: set transmit carrier frequency
  127. * @s_tx_duty_cycle: set transmit duty cycle (0% - 100%)
  128. * @s_rx_carrier_range: inform driver about carrier it is expected to handle
  129. * @tx_ir: transmit IR
  130. * @s_idle: enable/disable hardware idle mode, upon which,
  131. * device doesn't interrupt host until it sees IR pulses
  132. * @s_learning_mode: enable wide band receiver used for learning
  133. * @s_carrier_report: enable carrier reports
  134. * @s_filter: set the scancode filter
  135. * @s_wakeup_filter: set the wakeup scancode filter. If the mask is zero
  136. * then wakeup should be disabled. wakeup_protocol will be set to
  137. * a valid protocol if mask is nonzero.
  138. * @s_timeout: set hardware timeout in ns
  139. */
  140. struct rc_dev {
  141. struct device dev;
  142. bool managed_alloc;
  143. const struct attribute_group *sysfs_groups[5];
  144. const char *device_name;
  145. const char *input_phys;
  146. struct input_id input_id;
  147. const char *driver_name;
  148. const char *map_name;
  149. struct rc_map rc_map;
  150. struct mutex lock;
  151. unsigned int minor;
  152. struct ir_raw_event_ctrl *raw;
  153. struct input_dev *input_dev;
  154. enum rc_driver_type driver_type;
  155. bool idle;
  156. bool encode_wakeup;
  157. u64 allowed_protocols;
  158. u64 enabled_protocols;
  159. u64 allowed_wakeup_protocols;
  160. enum rc_proto wakeup_protocol;
  161. struct rc_scancode_filter scancode_filter;
  162. struct rc_scancode_filter scancode_wakeup_filter;
  163. u32 scancode_mask;
  164. u32 users;
  165. void *priv;
  166. spinlock_t keylock;
  167. bool keypressed;
  168. unsigned long keyup_jiffies;
  169. struct timer_list timer_keyup;
  170. u32 last_keycode;
  171. enum rc_proto last_protocol;
  172. u32 last_scancode;
  173. u8 last_toggle;
  174. u32 timeout;
  175. u32 min_timeout;
  176. u32 max_timeout;
  177. u32 rx_resolution;
  178. u32 tx_resolution;
  179. #ifdef CONFIG_LIRC
  180. struct lirc_dev *lirc_dev;
  181. int carrier_low;
  182. ktime_t gap_start;
  183. u64 gap_duration;
  184. bool gap;
  185. bool send_timeout_reports;
  186. u8 send_mode;
  187. #endif
  188. int (*change_protocol)(struct rc_dev *dev, u64 *rc_proto);
  189. int (*open)(struct rc_dev *dev);
  190. void (*close)(struct rc_dev *dev);
  191. int (*s_tx_mask)(struct rc_dev *dev, u32 mask);
  192. int (*s_tx_carrier)(struct rc_dev *dev, u32 carrier);
  193. int (*s_tx_duty_cycle)(struct rc_dev *dev, u32 duty_cycle);
  194. int (*s_rx_carrier_range)(struct rc_dev *dev, u32 min, u32 max);
  195. int (*tx_ir)(struct rc_dev *dev, unsigned *txbuf, unsigned n);
  196. void (*s_idle)(struct rc_dev *dev, bool enable);
  197. int (*s_learning_mode)(struct rc_dev *dev, int enable);
  198. int (*s_carrier_report) (struct rc_dev *dev, int enable);
  199. int (*s_filter)(struct rc_dev *dev,
  200. struct rc_scancode_filter *filter);
  201. int (*s_wakeup_filter)(struct rc_dev *dev,
  202. struct rc_scancode_filter *filter);
  203. int (*s_timeout)(struct rc_dev *dev,
  204. unsigned int timeout);
  205. };
  206. #define to_rc_dev(d) container_of(d, struct rc_dev, dev)
  207. /*
  208. * From rc-main.c
  209. * Those functions can be used on any type of Remote Controller. They
  210. * basically creates an input_dev and properly reports the device as a
  211. * Remote Controller, at sys/class/rc.
  212. */
  213. /**
  214. * rc_allocate_device - Allocates a RC device
  215. *
  216. * @rc_driver_type: specifies the type of the RC output to be allocated
  217. * returns a pointer to struct rc_dev.
  218. */
  219. struct rc_dev *rc_allocate_device(enum rc_driver_type);
  220. /**
  221. * devm_rc_allocate_device - Managed RC device allocation
  222. *
  223. * @dev: pointer to struct device
  224. * @rc_driver_type: specifies the type of the RC output to be allocated
  225. * returns a pointer to struct rc_dev.
  226. */
  227. struct rc_dev *devm_rc_allocate_device(struct device *dev, enum rc_driver_type);
  228. /**
  229. * rc_free_device - Frees a RC device
  230. *
  231. * @dev: pointer to struct rc_dev.
  232. */
  233. void rc_free_device(struct rc_dev *dev);
  234. /**
  235. * rc_register_device - Registers a RC device
  236. *
  237. * @dev: pointer to struct rc_dev.
  238. */
  239. int rc_register_device(struct rc_dev *dev);
  240. /**
  241. * devm_rc_register_device - Manageded registering of a RC device
  242. *
  243. * @parent: pointer to struct device.
  244. * @dev: pointer to struct rc_dev.
  245. */
  246. int devm_rc_register_device(struct device *parent, struct rc_dev *dev);
  247. /**
  248. * rc_unregister_device - Unregisters a RC device
  249. *
  250. * @dev: pointer to struct rc_dev.
  251. */
  252. void rc_unregister_device(struct rc_dev *dev);
  253. void rc_repeat(struct rc_dev *dev);
  254. void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
  255. u8 toggle);
  256. void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
  257. u32 scancode, u8 toggle);
  258. void rc_keyup(struct rc_dev *dev);
  259. u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode);
  260. /*
  261. * From rc-raw.c
  262. * The Raw interface is specific to InfraRed. It may be a good idea to
  263. * split it later into a separate header.
  264. */
  265. struct ir_raw_event {
  266. union {
  267. u32 duration;
  268. u32 carrier;
  269. };
  270. u8 duty_cycle;
  271. unsigned pulse:1;
  272. unsigned reset:1;
  273. unsigned timeout:1;
  274. unsigned carrier_report:1;
  275. };
  276. #define DEFINE_IR_RAW_EVENT(event) struct ir_raw_event event = {}
  277. static inline void init_ir_raw_event(struct ir_raw_event *ev)
  278. {
  279. memset(ev, 0, sizeof(*ev));
  280. }
  281. #define IR_DEFAULT_TIMEOUT MS_TO_NS(125)
  282. #define IR_MAX_DURATION 500000000 /* 500 ms */
  283. #define US_TO_NS(usec) ((usec) * 1000)
  284. #define MS_TO_US(msec) ((msec) * 1000)
  285. #define MS_TO_NS(msec) ((msec) * 1000 * 1000)
  286. void ir_raw_event_handle(struct rc_dev *dev);
  287. int ir_raw_event_store(struct rc_dev *dev, struct ir_raw_event *ev);
  288. int ir_raw_event_store_edge(struct rc_dev *dev, bool pulse);
  289. int ir_raw_event_store_with_filter(struct rc_dev *dev,
  290. struct ir_raw_event *ev);
  291. void ir_raw_event_set_idle(struct rc_dev *dev, bool idle);
  292. int ir_raw_encode_scancode(enum rc_proto protocol, u32 scancode,
  293. struct ir_raw_event *events, unsigned int max);
  294. int ir_raw_encode_carrier(enum rc_proto protocol);
  295. static inline void ir_raw_event_reset(struct rc_dev *dev)
  296. {
  297. struct ir_raw_event ev = { .reset = true };
  298. ir_raw_event_store(dev, &ev);
  299. ir_raw_event_handle(dev);
  300. }
  301. /* extract mask bits out of data and pack them into the result */
  302. static inline u32 ir_extract_bits(u32 data, u32 mask)
  303. {
  304. u32 vbit = 1, value = 0;
  305. do {
  306. if (mask & 1) {
  307. if (data & 1)
  308. value |= vbit;
  309. vbit <<= 1;
  310. }
  311. data >>= 1;
  312. } while (mask >>= 1);
  313. return value;
  314. }
  315. /* Get NEC scancode and protocol type from address and command bytes */
  316. static inline u32 ir_nec_bytes_to_scancode(u8 address, u8 not_address,
  317. u8 command, u8 not_command,
  318. enum rc_proto *protocol)
  319. {
  320. u32 scancode;
  321. if ((command ^ not_command) != 0xff) {
  322. /* NEC transport, but modified protocol, used by at
  323. * least Apple and TiVo remotes
  324. */
  325. scancode = not_address << 24 |
  326. address << 16 |
  327. not_command << 8 |
  328. command;
  329. *protocol = RC_PROTO_NEC32;
  330. } else if ((address ^ not_address) != 0xff) {
  331. /* Extended NEC */
  332. scancode = address << 16 |
  333. not_address << 8 |
  334. command;
  335. *protocol = RC_PROTO_NECX;
  336. } else {
  337. /* Normal NEC */
  338. scancode = address << 8 | command;
  339. *protocol = RC_PROTO_NEC;
  340. }
  341. return scancode;
  342. }
  343. #endif /* _RC_CORE */