Jarod Wilson | 457e2ff | 2011-01-18 02:33:08 -0300 | [diff] [blame] | 1 | /* ir-lirc-codec.c - rc-core to classic lirc interface bridge |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 2 | * |
| 3 | * Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/wait.h> |
Paul Gortmaker | 7a707b8 | 2011-07-03 14:03:12 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 18 | #include <media/lirc.h> |
Jarod Wilson | 5690085 | 2010-07-16 14:25:33 -0300 | [diff] [blame] | 19 | #include <media/lirc_dev.h> |
Mauro Carvalho Chehab | 6bda964 | 2010-11-17 13:28:38 -0300 | [diff] [blame] | 20 | #include <media/rc-core.h> |
Mauro Carvalho Chehab | f62de67 | 2010-11-09 23:09:57 -0300 | [diff] [blame] | 21 | #include "rc-core-priv.h" |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 22 | |
| 23 | #define LIRCBUF_SIZE 256 |
| 24 | |
| 25 | /** |
| 26 | * ir_lirc_decode() - Send raw IR data to lirc_dev to be relayed to the |
| 27 | * lircd userspace daemon for decoding. |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 28 | * @input_dev: the struct rc_dev descriptor of the device |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 29 | * @duration: the struct ir_raw_event descriptor of the pulse/space |
| 30 | * |
| 31 | * This function returns -EINVAL if the lirc interfaces aren't wired up. |
| 32 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 33 | static int ir_lirc_decode(struct rc_dev *dev, struct ir_raw_event ev) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 34 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 35 | struct lirc_codec *lirc = &dev->raw->lirc; |
Maxim Levitsky | 510fcb7 | 2010-07-31 11:59:15 -0300 | [diff] [blame] | 36 | int sample; |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 37 | |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 38 | if (!(dev->raw->enabled_protocols & RC_BIT_LIRC)) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 39 | return 0; |
| 40 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 41 | if (!dev->raw->lirc.drv || !dev->raw->lirc.drv->rbuf) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 42 | return -EINVAL; |
| 43 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 44 | /* Packet start */ |
| 45 | if (ev.reset) |
Maxim Levitsky | 510fcb7 | 2010-07-31 11:59:15 -0300 | [diff] [blame] | 46 | return 0; |
| 47 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 48 | /* Carrier reports */ |
| 49 | if (ev.carrier_report) { |
| 50 | sample = LIRC_FREQUENCY(ev.carrier); |
Jarod Wilson | 457e2ff | 2011-01-18 02:33:08 -0300 | [diff] [blame] | 51 | IR_dprintk(2, "carrier report (freq: %d)\n", sample); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 52 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 53 | /* Packet end */ |
| 54 | } else if (ev.timeout) { |
| 55 | |
| 56 | if (lirc->gap) |
| 57 | return 0; |
| 58 | |
| 59 | lirc->gap_start = ktime_get(); |
| 60 | lirc->gap = true; |
| 61 | lirc->gap_duration = ev.duration; |
| 62 | |
| 63 | if (!lirc->send_timeout_reports) |
| 64 | return 0; |
| 65 | |
| 66 | sample = LIRC_TIMEOUT(ev.duration / 1000); |
Jarod Wilson | 457e2ff | 2011-01-18 02:33:08 -0300 | [diff] [blame] | 67 | IR_dprintk(2, "timeout report (duration: %d)\n", sample); |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 68 | |
| 69 | /* Normal sample */ |
| 70 | } else { |
| 71 | |
| 72 | if (lirc->gap) { |
| 73 | int gap_sample; |
| 74 | |
| 75 | lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(), |
| 76 | lirc->gap_start)); |
| 77 | |
| 78 | /* Convert to ms and cap by LIRC_VALUE_MASK */ |
| 79 | do_div(lirc->gap_duration, 1000); |
| 80 | lirc->gap_duration = min(lirc->gap_duration, |
| 81 | (u64)LIRC_VALUE_MASK); |
| 82 | |
| 83 | gap_sample = LIRC_SPACE(lirc->gap_duration); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 84 | lirc_buffer_write(dev->raw->lirc.drv->rbuf, |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 85 | (unsigned char *) &gap_sample); |
| 86 | lirc->gap = false; |
| 87 | } |
| 88 | |
| 89 | sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) : |
| 90 | LIRC_SPACE(ev.duration / 1000); |
Jarod Wilson | 457e2ff | 2011-01-18 02:33:08 -0300 | [diff] [blame] | 91 | IR_dprintk(2, "delivering %uus %s to lirc_dev\n", |
| 92 | TO_US(ev.duration), TO_STR(ev.pulse)); |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 93 | } |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 94 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 95 | lirc_buffer_write(dev->raw->lirc.drv->rbuf, |
Maxim Levitsky | 510fcb7 | 2010-07-31 11:59:15 -0300 | [diff] [blame] | 96 | (unsigned char *) &sample); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 97 | wake_up(&dev->raw->lirc.drv->rbuf->wait_poll); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 98 | |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Dan Carpenter | 06bd801 | 2011-10-06 02:41:06 -0300 | [diff] [blame] | 102 | static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf, |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 103 | size_t n, loff_t *ppos) |
| 104 | { |
| 105 | struct lirc_codec *lirc; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 106 | struct rc_dev *dev; |
David Härdeman | 5588dc2 | 2011-04-28 12:13:58 -0300 | [diff] [blame] | 107 | unsigned int *txbuf; /* buffer with values to transmit */ |
Sean Young | d07df223 | 2012-08-13 08:59:49 -0300 | [diff] [blame] | 108 | ssize_t ret = -EINVAL; |
Vasiliy Kulikov | 4c8b869 | 2010-11-26 14:06:35 -0300 | [diff] [blame] | 109 | size_t count; |
David Härdeman | f8e00d5 | 2011-04-28 11:14:03 -0300 | [diff] [blame] | 110 | ktime_t start; |
| 111 | s64 towait; |
| 112 | unsigned int duration = 0; /* signal duration in us */ |
| 113 | int i; |
| 114 | |
| 115 | start = ktime_get(); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 116 | |
| 117 | lirc = lirc_get_pdata(file); |
| 118 | if (!lirc) |
| 119 | return -EFAULT; |
| 120 | |
David Härdeman | 5588dc2 | 2011-04-28 12:13:58 -0300 | [diff] [blame] | 121 | if (n < sizeof(unsigned) || n % sizeof(unsigned)) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 122 | return -EINVAL; |
| 123 | |
David Härdeman | 5588dc2 | 2011-04-28 12:13:58 -0300 | [diff] [blame] | 124 | count = n / sizeof(unsigned); |
| 125 | if (count > LIRCBUF_SIZE || count % 2 == 0) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 126 | return -EINVAL; |
| 127 | |
Jarod Wilson | 6efb870 | 2010-07-16 18:29:54 -0300 | [diff] [blame] | 128 | txbuf = memdup_user(buf, n); |
| 129 | if (IS_ERR(txbuf)) |
| 130 | return PTR_ERR(txbuf); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 131 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 132 | dev = lirc->dev; |
| 133 | if (!dev) { |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 134 | ret = -EFAULT; |
| 135 | goto out; |
| 136 | } |
| 137 | |
David Härdeman | f8e00d5 | 2011-04-28 11:14:03 -0300 | [diff] [blame] | 138 | if (!dev->tx_ir) { |
| 139 | ret = -ENOSYS; |
| 140 | goto out; |
| 141 | } |
David Härdeman | 5588dc2 | 2011-04-28 12:13:58 -0300 | [diff] [blame] | 142 | |
Sean Young | 586c61f | 2012-08-23 17:18:25 -0300 | [diff] [blame] | 143 | ret = dev->tx_ir(dev, txbuf, count); |
David Härdeman | f8e00d5 | 2011-04-28 11:14:03 -0300 | [diff] [blame] | 144 | if (ret < 0) |
| 145 | goto out; |
| 146 | |
| 147 | for (i = 0; i < ret; i++) |
| 148 | duration += txbuf[i]; |
| 149 | |
| 150 | ret *= sizeof(unsigned int); |
| 151 | |
| 152 | /* |
| 153 | * The lircd gap calculation expects the write function to |
| 154 | * wait for the actual IR signal to be transmitted before |
| 155 | * returning. |
| 156 | */ |
| 157 | towait = ktime_us_delta(ktime_add_us(start, duration), ktime_get()); |
| 158 | if (towait > 0) { |
| 159 | set_current_state(TASK_INTERRUPTIBLE); |
| 160 | schedule_timeout(usecs_to_jiffies(towait)); |
| 161 | } |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 162 | |
| 163 | out: |
| 164 | kfree(txbuf); |
| 165 | return ret; |
| 166 | } |
| 167 | |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 168 | static long ir_lirc_ioctl(struct file *filep, unsigned int cmd, |
Dan Carpenter | 06bd801 | 2011-10-06 02:41:06 -0300 | [diff] [blame] | 169 | unsigned long arg) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 170 | { |
| 171 | struct lirc_codec *lirc; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 172 | struct rc_dev *dev; |
Dan Carpenter | 06bd801 | 2011-10-06 02:41:06 -0300 | [diff] [blame] | 173 | u32 __user *argp = (u32 __user *)(arg); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 174 | int ret = 0; |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 175 | __u32 val = 0, tmp; |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 176 | |
| 177 | lirc = lirc_get_pdata(filep); |
| 178 | if (!lirc) |
| 179 | return -EFAULT; |
| 180 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 181 | dev = lirc->dev; |
| 182 | if (!dev) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 183 | return -EFAULT; |
| 184 | |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 185 | if (_IOC_DIR(cmd) & _IOC_WRITE) { |
Dan Carpenter | 06bd801 | 2011-10-06 02:41:06 -0300 | [diff] [blame] | 186 | ret = get_user(val, argp); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 187 | if (ret) |
| 188 | return ret; |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 189 | } |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 190 | |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 191 | switch (cmd) { |
| 192 | |
| 193 | /* legacy support */ |
| 194 | case LIRC_GET_SEND_MODE: |
| 195 | val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK; |
| 196 | break; |
| 197 | |
| 198 | case LIRC_SET_SEND_MODE: |
| 199 | if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK)) |
| 200 | return -EINVAL; |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 201 | return 0; |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 202 | |
| 203 | /* TX settings */ |
| 204 | case LIRC_SET_TRANSMITTER_MASK: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 205 | if (!dev->s_tx_mask) |
Dan Carpenter | 5f49908 | 2012-09-11 07:11:53 -0300 | [diff] [blame] | 206 | return -ENOSYS; |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 207 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 208 | return dev->s_tx_mask(dev, val); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 209 | |
| 210 | case LIRC_SET_SEND_CARRIER: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 211 | if (!dev->s_tx_carrier) |
Dan Carpenter | 5f49908 | 2012-09-11 07:11:53 -0300 | [diff] [blame] | 212 | return -ENOSYS; |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 213 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 214 | return dev->s_tx_carrier(dev, val); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 215 | |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 216 | case LIRC_SET_SEND_DUTY_CYCLE: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 217 | if (!dev->s_tx_duty_cycle) |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 218 | return -ENOSYS; |
| 219 | |
| 220 | if (val <= 0 || val >= 100) |
| 221 | return -EINVAL; |
| 222 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 223 | return dev->s_tx_duty_cycle(dev, val); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 224 | |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 225 | /* RX settings */ |
| 226 | case LIRC_SET_REC_CARRIER: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 227 | if (!dev->s_rx_carrier_range) |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 228 | return -ENOSYS; |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 229 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 230 | if (val <= 0) |
| 231 | return -EINVAL; |
| 232 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 233 | return dev->s_rx_carrier_range(dev, |
| 234 | dev->raw->lirc.carrier_low, |
| 235 | val); |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 236 | |
| 237 | case LIRC_SET_REC_CARRIER_RANGE: |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 238 | if (val <= 0) |
| 239 | return -EINVAL; |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 240 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 241 | dev->raw->lirc.carrier_low = val; |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 242 | return 0; |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 243 | |
| 244 | case LIRC_GET_REC_RESOLUTION: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 245 | val = dev->rx_resolution; |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 246 | break; |
| 247 | |
| 248 | case LIRC_SET_WIDEBAND_RECEIVER: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 249 | if (!dev->s_learning_mode) |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 250 | return -ENOSYS; |
| 251 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 252 | return dev->s_learning_mode(dev, !!val); |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 253 | |
| 254 | case LIRC_SET_MEASURE_CARRIER_MODE: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 255 | if (!dev->s_carrier_report) |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 256 | return -ENOSYS; |
| 257 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 258 | return dev->s_carrier_report(dev, !!val); |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 259 | |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 260 | /* Generic timeout support */ |
| 261 | case LIRC_GET_MIN_TIMEOUT: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 262 | if (!dev->max_timeout) |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 263 | return -ENOSYS; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 264 | val = dev->min_timeout / 1000; |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 265 | break; |
| 266 | |
| 267 | case LIRC_GET_MAX_TIMEOUT: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 268 | if (!dev->max_timeout) |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 269 | return -ENOSYS; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 270 | val = dev->max_timeout / 1000; |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 271 | break; |
| 272 | |
| 273 | case LIRC_SET_REC_TIMEOUT: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 274 | if (!dev->max_timeout) |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 275 | return -ENOSYS; |
| 276 | |
| 277 | tmp = val * 1000; |
| 278 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 279 | if (tmp < dev->min_timeout || |
| 280 | tmp > dev->max_timeout) |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 281 | return -EINVAL; |
| 282 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 283 | dev->timeout = tmp; |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 284 | break; |
| 285 | |
| 286 | case LIRC_SET_REC_TIMEOUT_REPORTS: |
| 287 | lirc->send_timeout_reports = !!val; |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 288 | break; |
| 289 | |
| 290 | default: |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 291 | return lirc_dev_fop_ioctl(filep, cmd, arg); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 292 | } |
| 293 | |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 294 | if (_IOC_DIR(cmd) & _IOC_READ) |
Dan Carpenter | 06bd801 | 2011-10-06 02:41:06 -0300 | [diff] [blame] | 295 | ret = put_user(val, argp); |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 296 | |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | static int ir_lirc_open(void *data) |
| 301 | { |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static void ir_lirc_close(void *data) |
| 306 | { |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | static struct file_operations lirc_fops = { |
| 311 | .owner = THIS_MODULE, |
| 312 | .write = ir_lirc_transmit_ir, |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 313 | .unlocked_ioctl = ir_lirc_ioctl, |
Jarod Wilson | 8be292c | 2010-10-09 15:07:06 -0300 | [diff] [blame] | 314 | #ifdef CONFIG_COMPAT |
| 315 | .compat_ioctl = ir_lirc_ioctl, |
| 316 | #endif |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 317 | .read = lirc_dev_fop_read, |
| 318 | .poll = lirc_dev_fop_poll, |
| 319 | .open = lirc_dev_fop_open, |
| 320 | .release = lirc_dev_fop_close, |
Arnd Bergmann | d9d2e9d | 2010-08-15 18:51:56 +0200 | [diff] [blame] | 321 | .llseek = no_llseek, |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 322 | }; |
| 323 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 324 | static int ir_lirc_register(struct rc_dev *dev) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 325 | { |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 326 | struct lirc_driver *drv; |
| 327 | struct lirc_buffer *rbuf; |
| 328 | int rc = -ENOMEM; |
| 329 | unsigned long features; |
| 330 | |
| 331 | drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL); |
| 332 | if (!drv) |
| 333 | return rc; |
| 334 | |
| 335 | rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL); |
Dan Carpenter | 49b7a12 | 2010-07-23 07:08:26 -0300 | [diff] [blame] | 336 | if (!rbuf) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 337 | goto rbuf_alloc_failed; |
| 338 | |
| 339 | rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE); |
| 340 | if (rc) |
| 341 | goto rbuf_init_failed; |
| 342 | |
| 343 | features = LIRC_CAN_REC_MODE2; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 344 | if (dev->tx_ir) { |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 345 | features |= LIRC_CAN_SEND_PULSE; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 346 | if (dev->s_tx_mask) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 347 | features |= LIRC_CAN_SET_TRANSMITTER_MASK; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 348 | if (dev->s_tx_carrier) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 349 | features |= LIRC_CAN_SET_SEND_CARRIER; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 350 | if (dev->s_tx_duty_cycle) |
Maxim Levitsky | 67332ba | 2010-09-06 18:26:08 -0300 | [diff] [blame] | 351 | features |= LIRC_CAN_SET_SEND_DUTY_CYCLE; |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 352 | } |
| 353 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 354 | if (dev->s_rx_carrier_range) |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 355 | features |= LIRC_CAN_SET_REC_CARRIER | |
| 356 | LIRC_CAN_SET_REC_CARRIER_RANGE; |
| 357 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 358 | if (dev->s_learning_mode) |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 359 | features |= LIRC_CAN_USE_WIDEBAND_RECEIVER; |
| 360 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 361 | if (dev->s_carrier_report) |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 362 | features |= LIRC_CAN_MEASURE_CARRIER; |
| 363 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 364 | if (dev->max_timeout) |
Maxim Levitsky | e589333 | 2010-07-31 11:59:23 -0300 | [diff] [blame] | 365 | features |= LIRC_CAN_SET_REC_TIMEOUT; |
| 366 | |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 367 | snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)", |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 368 | dev->driver_name); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 369 | drv->minor = -1; |
| 370 | drv->features = features; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 371 | drv->data = &dev->raw->lirc; |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 372 | drv->rbuf = rbuf; |
| 373 | drv->set_use_inc = &ir_lirc_open; |
| 374 | drv->set_use_dec = &ir_lirc_close; |
| 375 | drv->code_length = sizeof(struct ir_raw_event) * 8; |
| 376 | drv->fops = &lirc_fops; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 377 | drv->dev = &dev->dev; |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 378 | drv->owner = THIS_MODULE; |
| 379 | |
| 380 | drv->minor = lirc_register_driver(drv); |
| 381 | if (drv->minor < 0) { |
| 382 | rc = -ENODEV; |
| 383 | goto lirc_register_failed; |
| 384 | } |
| 385 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 386 | dev->raw->lirc.drv = drv; |
| 387 | dev->raw->lirc.dev = dev; |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 388 | return 0; |
| 389 | |
| 390 | lirc_register_failed: |
| 391 | rbuf_init_failed: |
| 392 | kfree(rbuf); |
| 393 | rbuf_alloc_failed: |
| 394 | kfree(drv); |
| 395 | |
| 396 | return rc; |
| 397 | } |
| 398 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 399 | static int ir_lirc_unregister(struct rc_dev *dev) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 400 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 401 | struct lirc_codec *lirc = &dev->raw->lirc; |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 402 | |
| 403 | lirc_unregister_driver(lirc->drv->minor); |
| 404 | lirc_buffer_free(lirc->drv->rbuf); |
| 405 | kfree(lirc->drv); |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static struct ir_raw_handler lirc_handler = { |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 411 | .protocols = RC_BIT_LIRC, |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 412 | .decode = ir_lirc_decode, |
| 413 | .raw_register = ir_lirc_register, |
| 414 | .raw_unregister = ir_lirc_unregister, |
| 415 | }; |
| 416 | |
| 417 | static int __init ir_lirc_codec_init(void) |
| 418 | { |
| 419 | ir_raw_handler_register(&lirc_handler); |
| 420 | |
| 421 | printk(KERN_INFO "IR LIRC bridge handler initialized\n"); |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static void __exit ir_lirc_codec_exit(void) |
| 426 | { |
| 427 | ir_raw_handler_unregister(&lirc_handler); |
| 428 | } |
| 429 | |
| 430 | module_init(ir_lirc_codec_init); |
| 431 | module_exit(ir_lirc_codec_exit); |
| 432 | |
| 433 | MODULE_LICENSE("GPL"); |
| 434 | MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>"); |
| 435 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); |
| 436 | MODULE_DESCRIPTION("LIRC IR handler bridge"); |