ir-core.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Remote Controller core header
  3. *
  4. * Copyright (C) 2009-2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
  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 _IR_CORE
  16. #define _IR_CORE
  17. #include <linux/spinlock.h>
  18. #include <linux/kfifo.h>
  19. #include <linux/time.h>
  20. #include <linux/timer.h>
  21. #include <media/rc-map.h>
  22. extern int ir_core_debug;
  23. #define IR_dprintk(level, fmt, arg...) if (ir_core_debug >= level) \
  24. printk(KERN_DEBUG "%s: " fmt , __func__, ## arg)
  25. enum rc_driver_type {
  26. RC_DRIVER_SCANCODE = 0, /* Driver or hardware generates a scancode */
  27. RC_DRIVER_IR_RAW, /* Needs a Infra-Red pulse/space decoder */
  28. };
  29. /**
  30. * struct ir_dev_props - Allow caller drivers to set special properties
  31. * @driver_type: specifies if the driver or hardware have already a decoder,
  32. * or if it needs to use the IR raw event decoders to produce a scancode
  33. * @allowed_protos: bitmask with the supported IR_TYPE_* protocols
  34. * @scanmask: some hardware decoders are not capable of providing the full
  35. * scancode to the application. As this is a hardware limit, we can't do
  36. * anything with it. Yet, as the same keycode table can be used with other
  37. * devices, a mask is provided to allow its usage. Drivers should generally
  38. * leave this field in blank
  39. * @priv: driver-specific data, to be used on the callbacks
  40. * @change_protocol: allow changing the protocol used on hardware decoders
  41. * @open: callback to allow drivers to enable polling/irq when IR input device
  42. * is opened.
  43. * @close: callback to allow drivers to disable polling/irq when IR input device
  44. * is opened.
  45. * @s_tx_mask: set transmitter mask (for devices with multiple tx outputs)
  46. * @s_tx_carrier: set transmit carrier frequency
  47. * @tx_ir: transmit IR
  48. */
  49. struct ir_dev_props {
  50. enum rc_driver_type driver_type;
  51. unsigned long allowed_protos;
  52. u32 scanmask;
  53. void *priv;
  54. int (*change_protocol)(void *priv, u64 ir_type);
  55. int (*open)(void *priv);
  56. void (*close)(void *priv);
  57. int (*s_tx_mask)(void *priv, u32 mask);
  58. int (*s_tx_carrier)(void *priv, u32 carrier);
  59. int (*tx_ir)(void *priv, int *txbuf, u32 n);
  60. };
  61. struct ir_input_dev {
  62. struct device dev; /* device */
  63. char *driver_name; /* Name of the driver module */
  64. struct ir_scancode_table rc_tab; /* scan/key table */
  65. unsigned long devno; /* device number */
  66. const struct ir_dev_props *props; /* Device properties */
  67. struct ir_raw_event_ctrl *raw; /* for raw pulse/space events */
  68. struct input_dev *input_dev; /* the input device associated with this device */
  69. /* key info - needed by IR keycode handlers */
  70. spinlock_t keylock; /* protects the below members */
  71. bool keypressed; /* current state */
  72. unsigned long keyup_jiffies; /* when should the current keypress be released? */
  73. struct timer_list timer_keyup; /* timer for releasing a keypress */
  74. u32 last_keycode; /* keycode of last command */
  75. u32 last_scancode; /* scancode of last command */
  76. u8 last_toggle; /* toggle of last command */
  77. };
  78. enum raw_event_type {
  79. IR_SPACE = (1 << 0),
  80. IR_PULSE = (1 << 1),
  81. IR_START_EVENT = (1 << 2),
  82. IR_STOP_EVENT = (1 << 3),
  83. };
  84. #define to_ir_input_dev(_attr) container_of(_attr, struct ir_input_dev, attr)
  85. /* From ir-keytable.c */
  86. int __ir_input_register(struct input_dev *dev,
  87. const struct ir_scancode_table *ir_codes,
  88. const struct ir_dev_props *props,
  89. const char *driver_name);
  90. static inline int ir_input_register(struct input_dev *dev,
  91. const char *map_name,
  92. const struct ir_dev_props *props,
  93. const char *driver_name) {
  94. struct ir_scancode_table *ir_codes;
  95. struct ir_input_dev *ir_dev;
  96. int rc;
  97. if (!map_name)
  98. return -EINVAL;
  99. ir_codes = get_rc_map(map_name);
  100. if (!ir_codes)
  101. return -EINVAL;
  102. rc = __ir_input_register(dev, ir_codes, props, driver_name);
  103. if (rc < 0)
  104. return -EINVAL;
  105. ir_dev = input_get_drvdata(dev);
  106. if (!rc && ir_dev->props && ir_dev->props->change_protocol)
  107. rc = ir_dev->props->change_protocol(ir_dev->props->priv,
  108. ir_codes->ir_type);
  109. return rc;
  110. }
  111. void ir_input_unregister(struct input_dev *input_dev);
  112. void ir_repeat(struct input_dev *dev);
  113. void ir_keydown(struct input_dev *dev, int scancode, u8 toggle);
  114. u32 ir_g_keycode_from_table(struct input_dev *input_dev, u32 scancode);
  115. /* From ir-raw-event.c */
  116. struct ir_raw_event {
  117. unsigned pulse:1;
  118. unsigned duration:31;
  119. };
  120. #define IR_MAX_DURATION 0x7FFFFFFF /* a bit more than 2 seconds */
  121. void ir_raw_event_handle(struct input_dev *input_dev);
  122. int ir_raw_event_store(struct input_dev *input_dev, struct ir_raw_event *ev);
  123. int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type type);
  124. static inline void ir_raw_event_reset(struct input_dev *input_dev)
  125. {
  126. struct ir_raw_event ev = { .pulse = false, .duration = 0 };
  127. ir_raw_event_store(input_dev, &ev);
  128. ir_raw_event_handle(input_dev);
  129. }
  130. #endif /* _IR_CORE */