rc-core.h 12 KB

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