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