Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Remote Controller core header |
| 3 | * |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 4 | * Copyright (C) 2009-2010 by Mauro Carvalho Chehab <mchehab@redhat.com> |
| 5 | * |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 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 | |
| 16 | #ifndef _IR_CORE |
| 17 | #define _IR_CORE |
| 18 | |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 19 | #include <linux/spinlock.h> |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 20 | #include <linux/kfifo.h> |
| 21 | #include <linux/time.h> |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 22 | #include <linux/timer.h> |
Mauro Carvalho Chehab | 02858ee | 2010-04-02 20:01:00 -0300 | [diff] [blame] | 23 | #include <media/rc-map.h> |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 24 | |
| 25 | extern int ir_core_debug; |
| 26 | #define IR_dprintk(level, fmt, arg...) if (ir_core_debug >= level) \ |
| 27 | printk(KERN_DEBUG "%s: " fmt , __func__, ## arg) |
| 28 | |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 29 | enum rc_driver_type { |
| 30 | RC_DRIVER_SCANCODE = 0, /* Driver or hardware generates a scancode */ |
| 31 | RC_DRIVER_IR_RAW, /* Needs a Infra-Red pulse/space decoder */ |
| 32 | }; |
| 33 | |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 34 | /** |
| 35 | * struct ir_dev_props - Allow caller drivers to set special properties |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 36 | * @driver_type: specifies if the driver or hardware have already a decoder, |
| 37 | * or if it needs to use the IR raw event decoders to produce a scancode |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 38 | * @allowed_protos: bitmask with the supported IR_TYPE_* protocols |
| 39 | * @scanmask: some hardware decoders are not capable of providing the full |
| 40 | * scancode to the application. As this is a hardware limit, we can't do |
| 41 | * anything with it. Yet, as the same keycode table can be used with other |
| 42 | * devices, a mask is provided to allow its usage. Drivers should generally |
| 43 | * leave this field in blank |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 44 | * @timeout: optional time after which device stops sending data |
| 45 | * @min_timeout: minimum timeout supported by device |
| 46 | * @max_timeout: maximum timeout supported by device |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 47 | * @rx_resolution : resolution (in ns) of input sampler |
| 48 | * @tx_resolution: resolution (in ns) of output sampler |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 49 | * @priv: driver-specific data, to be used on the callbacks |
| 50 | * @change_protocol: allow changing the protocol used on hardware decoders |
| 51 | * @open: callback to allow drivers to enable polling/irq when IR input device |
| 52 | * is opened. |
| 53 | * @close: callback to allow drivers to disable polling/irq when IR input device |
| 54 | * is opened. |
Jarod Wilson | 9b7c54d | 2010-06-16 17:55:25 -0300 | [diff] [blame] | 55 | * @s_tx_mask: set transmitter mask (for devices with multiple tx outputs) |
| 56 | * @s_tx_carrier: set transmit carrier frequency |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 57 | * @s_tx_duty_cycle: set transmit duty cycle (0% - 100%) |
| 58 | * @s_rx_carrier: inform driver about carrier it is expected to handle |
Jarod Wilson | 9b7c54d | 2010-06-16 17:55:25 -0300 | [diff] [blame] | 59 | * @tx_ir: transmit IR |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 60 | * @s_idle: optional: enable/disable hardware idle mode, upon which, |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 61 | device doesn't interrupt host until it sees IR pulses |
| 62 | * @s_learning_mode: enable wide band receiver used for learning |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 63 | * @s_carrier_report: enable carrier reports |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 64 | */ |
Mauro Carvalho Chehab | e93854d | 2009-12-14 00:16:55 -0300 | [diff] [blame] | 65 | struct ir_dev_props { |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 66 | enum rc_driver_type driver_type; |
| 67 | unsigned long allowed_protos; |
| 68 | u32 scanmask; |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 69 | |
| 70 | u32 timeout; |
| 71 | u32 min_timeout; |
| 72 | u32 max_timeout; |
| 73 | |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 74 | u32 rx_resolution; |
| 75 | u32 tx_resolution; |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 76 | |
Jarod Wilson | 9b7c54d | 2010-06-16 17:55:25 -0300 | [diff] [blame] | 77 | void *priv; |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 78 | int (*change_protocol)(void *priv, u64 ir_type); |
| 79 | int (*open)(void *priv); |
| 80 | void (*close)(void *priv); |
Jarod Wilson | 9b7c54d | 2010-06-16 17:55:25 -0300 | [diff] [blame] | 81 | int (*s_tx_mask)(void *priv, u32 mask); |
| 82 | int (*s_tx_carrier)(void *priv, u32 carrier); |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 83 | int (*s_tx_duty_cycle)(void *priv, u32 duty_cycle); |
| 84 | int (*s_rx_carrier_range)(void *priv, u32 min, u32 max); |
Jarod Wilson | 30eb1be | 2010-07-02 00:38:09 -0300 | [diff] [blame] | 85 | int (*tx_ir)(void *priv, int *txbuf, u32 n); |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 86 | void (*s_idle)(void *priv, bool enable); |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 87 | int (*s_learning_mode)(void *priv, int enable); |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 88 | int (*s_carrier_report) (void *priv, int enable); |
Mauro Carvalho Chehab | e93854d | 2009-12-14 00:16:55 -0300 | [diff] [blame] | 89 | }; |
| 90 | |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 91 | struct ir_input_dev { |
Mauro Carvalho Chehab | 945cdfa | 2010-03-11 12:41:56 -0300 | [diff] [blame] | 92 | struct device dev; /* device */ |
Mauro Carvalho Chehab | 727e625 | 2010-03-12 21:18:14 -0300 | [diff] [blame] | 93 | char *driver_name; /* Name of the driver module */ |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 94 | struct ir_scancode_table rc_tab; /* scan/key table */ |
| 95 | unsigned long devno; /* device number */ |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 96 | struct ir_dev_props *props; /* Device properties */ |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 97 | struct ir_raw_event_ctrl *raw; /* for raw pulse/space events */ |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 98 | struct input_dev *input_dev; /* the input device associated with this device */ |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 99 | bool idle; |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 100 | |
| 101 | /* key info - needed by IR keycode handlers */ |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 102 | spinlock_t keylock; /* protects the below members */ |
| 103 | bool keypressed; /* current state */ |
| 104 | unsigned long keyup_jiffies; /* when should the current keypress be released? */ |
| 105 | struct timer_list timer_keyup; /* timer for releasing a keypress */ |
| 106 | u32 last_keycode; /* keycode of last command */ |
| 107 | u32 last_scancode; /* scancode of last command */ |
| 108 | u8 last_toggle; /* toggle of last command */ |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 109 | }; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 110 | |
Mauro Carvalho Chehab | 3f113e3 | 2010-04-08 15:10:27 -0300 | [diff] [blame] | 111 | enum raw_event_type { |
| 112 | IR_SPACE = (1 << 0), |
| 113 | IR_PULSE = (1 << 1), |
| 114 | IR_START_EVENT = (1 << 2), |
| 115 | IR_STOP_EVENT = (1 << 3), |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 116 | }; |
| 117 | |
Mauro Carvalho Chehab | 53f8702 | 2009-12-14 02:16:36 -0300 | [diff] [blame] | 118 | #define to_ir_input_dev(_attr) container_of(_attr, struct ir_input_dev, attr) |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 119 | |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 120 | int __ir_input_register(struct input_dev *dev, |
Mauro Carvalho Chehab | e93854d | 2009-12-14 00:16:55 -0300 | [diff] [blame] | 121 | const struct ir_scancode_table *ir_codes, |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 122 | struct ir_dev_props *props, |
Mauro Carvalho Chehab | 727e625 | 2010-03-12 21:18:14 -0300 | [diff] [blame] | 123 | const char *driver_name); |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 124 | |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 125 | static inline int ir_input_register(struct input_dev *dev, |
| 126 | const char *map_name, |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 127 | struct ir_dev_props *props, |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 128 | const char *driver_name) { |
| 129 | struct ir_scancode_table *ir_codes; |
Mauro Carvalho Chehab | 02858ee | 2010-04-02 20:01:00 -0300 | [diff] [blame] | 130 | struct ir_input_dev *ir_dev; |
| 131 | int rc; |
| 132 | |
| 133 | if (!map_name) |
| 134 | return -EINVAL; |
Mauro Carvalho Chehab | 9ce50c1 | 2010-04-02 02:33:35 -0300 | [diff] [blame] | 135 | |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 136 | ir_codes = get_rc_map(map_name); |
Maxim Levitsky | b378f43 | 2010-07-31 11:59:21 -0300 | [diff] [blame] | 137 | if (!ir_codes) { |
| 138 | ir_codes = get_rc_map(RC_MAP_EMPTY); |
| 139 | |
| 140 | if (!ir_codes) |
| 141 | return -EINVAL; |
| 142 | } |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 143 | |
Mauro Carvalho Chehab | 02858ee | 2010-04-02 20:01:00 -0300 | [diff] [blame] | 144 | rc = __ir_input_register(dev, ir_codes, props, driver_name); |
| 145 | if (rc < 0) |
| 146 | return -EINVAL; |
| 147 | |
| 148 | ir_dev = input_get_drvdata(dev); |
| 149 | |
| 150 | if (!rc && ir_dev->props && ir_dev->props->change_protocol) |
| 151 | rc = ir_dev->props->change_protocol(ir_dev->props->priv, |
| 152 | ir_codes->ir_type); |
| 153 | |
| 154 | return rc; |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 155 | } |
| 156 | |
Mauro Carvalho Chehab | 02858ee | 2010-04-02 20:01:00 -0300 | [diff] [blame] | 157 | void ir_input_unregister(struct input_dev *input_dev); |
Mauro Carvalho Chehab | 9ce50c1 | 2010-04-02 02:33:35 -0300 | [diff] [blame] | 158 | |
Mauro Carvalho Chehab | 3f113e3 | 2010-04-08 15:10:27 -0300 | [diff] [blame] | 159 | void ir_repeat(struct input_dev *dev); |
| 160 | void ir_keydown(struct input_dev *dev, int scancode, u8 toggle); |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 161 | void ir_keydown_notimeout(struct input_dev *dev, int scancode, u8 toggle); |
| 162 | void ir_keyup(struct input_dev *dev); |
Jarod Wilson | 1c0e0ee | 2010-04-16 18:27:58 -0300 | [diff] [blame] | 163 | u32 ir_g_keycode_from_table(struct input_dev *input_dev, u32 scancode); |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 164 | |
Mauro Carvalho Chehab | 3f113e3 | 2010-04-08 15:10:27 -0300 | [diff] [blame] | 165 | /* From ir-raw-event.c */ |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 166 | struct ir_raw_event { |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 167 | union { |
| 168 | u32 duration; |
| 169 | |
| 170 | struct { |
| 171 | u32 carrier; |
| 172 | u8 duty_cycle; |
| 173 | }; |
| 174 | }; |
| 175 | |
| 176 | unsigned pulse:1; |
| 177 | unsigned reset:1; |
| 178 | unsigned timeout:1; |
| 179 | unsigned carrier_report:1; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 180 | }; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 181 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 182 | #define DEFINE_IR_RAW_EVENT(event) \ |
| 183 | struct ir_raw_event event = { \ |
| 184 | { .duration = 0 } , \ |
| 185 | .pulse = 0, \ |
| 186 | .reset = 0, \ |
| 187 | .timeout = 0, \ |
| 188 | .carrier_report = 0 } |
| 189 | |
| 190 | static inline void init_ir_raw_event(struct ir_raw_event *ev) |
| 191 | { |
| 192 | memset(ev, 0, sizeof(*ev)); |
| 193 | } |
| 194 | |
| 195 | #define IR_MAX_DURATION 0xFFFFFFFF /* a bit more than 4 seconds */ |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 196 | |
| 197 | void ir_raw_event_handle(struct input_dev *input_dev); |
| 198 | int ir_raw_event_store(struct input_dev *input_dev, struct ir_raw_event *ev); |
| 199 | int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type type); |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 200 | int ir_raw_event_store_with_filter(struct input_dev *input_dev, |
| 201 | struct ir_raw_event *ev); |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 202 | void ir_raw_event_set_idle(struct input_dev *input_dev, bool idle); |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 203 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 204 | static inline void ir_raw_event_reset(struct input_dev *input_dev) |
| 205 | { |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 206 | DEFINE_IR_RAW_EVENT(ev); |
| 207 | ev.reset = true; |
| 208 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 209 | ir_raw_event_store(input_dev, &ev); |
| 210 | ir_raw_event_handle(input_dev); |
| 211 | } |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 212 | |
David Härdeman | 3ffea49 | 2010-10-29 16:08:12 -0300 | [diff] [blame] | 213 | |
| 214 | /* extract mask bits out of data and pack them into the result */ |
| 215 | static inline u32 ir_extract_bits(u32 data, u32 mask) |
| 216 | { |
| 217 | u32 vbit = 1, value = 0; |
| 218 | |
| 219 | do { |
| 220 | if (mask & 1) { |
| 221 | if (data & 1) |
| 222 | value |= vbit; |
| 223 | vbit <<= 1; |
| 224 | } |
| 225 | data >>= 1; |
| 226 | } while (mask >>= 1); |
| 227 | |
| 228 | return value; |
| 229 | } |
| 230 | |
| 231 | |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 232 | #endif /* _IR_CORE */ |