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