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> |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 19 | #include <media/ir-core.h> |
| 20 | #include "ir-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. |
| 27 | * @input_dev: the struct input_dev descriptor of the device |
| 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 | */ |
| 32 | static int ir_lirc_decode(struct input_dev *input_dev, struct ir_raw_event ev) |
| 33 | { |
| 34 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
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 | |
| 37 | if (!(ir_dev->raw->enabled_protocols & IR_TYPE_LIRC)) |
| 38 | return 0; |
| 39 | |
| 40 | if (!ir_dev->raw->lirc.drv || !ir_dev->raw->lirc.drv->rbuf) |
| 41 | return -EINVAL; |
| 42 | |
Maxim Levitsky | 510fcb7 | 2010-07-31 11:59:15 -0300 | [diff] [blame^] | 43 | if (IS_RESET(ev)) |
| 44 | return 0; |
| 45 | |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 46 | IR_dprintk(2, "LIRC data transfer started (%uus %s)\n", |
| 47 | TO_US(ev.duration), TO_STR(ev.pulse)); |
| 48 | |
Maxim Levitsky | 510fcb7 | 2010-07-31 11:59:15 -0300 | [diff] [blame^] | 49 | |
| 50 | sample = ev.duration / 1000; |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 51 | if (ev.pulse) |
Maxim Levitsky | 510fcb7 | 2010-07-31 11:59:15 -0300 | [diff] [blame^] | 52 | sample |= PULSE_BIT; |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 53 | |
| 54 | lirc_buffer_write(ir_dev->raw->lirc.drv->rbuf, |
Maxim Levitsky | 510fcb7 | 2010-07-31 11:59:15 -0300 | [diff] [blame^] | 55 | (unsigned char *) &sample); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 56 | wake_up(&ir_dev->raw->lirc.drv->rbuf->wait_poll); |
| 57 | |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf, |
| 63 | size_t n, loff_t *ppos) |
| 64 | { |
| 65 | struct lirc_codec *lirc; |
| 66 | struct ir_input_dev *ir_dev; |
| 67 | int *txbuf; /* buffer with values to transmit */ |
| 68 | int ret = 0, count; |
| 69 | |
| 70 | lirc = lirc_get_pdata(file); |
| 71 | if (!lirc) |
| 72 | return -EFAULT; |
| 73 | |
| 74 | if (n % sizeof(int)) |
| 75 | return -EINVAL; |
| 76 | |
| 77 | count = n / sizeof(int); |
| 78 | if (count > LIRCBUF_SIZE || count % 2 == 0) |
| 79 | return -EINVAL; |
| 80 | |
Jarod Wilson | 6efb870 | 2010-07-16 18:29:54 -0300 | [diff] [blame] | 81 | txbuf = memdup_user(buf, n); |
| 82 | if (IS_ERR(txbuf)) |
| 83 | return PTR_ERR(txbuf); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 84 | |
| 85 | ir_dev = lirc->ir_dev; |
| 86 | if (!ir_dev) { |
| 87 | ret = -EFAULT; |
| 88 | goto out; |
| 89 | } |
| 90 | |
| 91 | if (ir_dev->props && ir_dev->props->tx_ir) |
| 92 | ret = ir_dev->props->tx_ir(ir_dev->props->priv, txbuf, (u32)n); |
| 93 | |
| 94 | out: |
| 95 | kfree(txbuf); |
| 96 | return ret; |
| 97 | } |
| 98 | |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 99 | static long ir_lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 100 | { |
| 101 | struct lirc_codec *lirc; |
| 102 | struct ir_input_dev *ir_dev; |
| 103 | int ret = 0; |
| 104 | void *drv_data; |
| 105 | unsigned long val; |
| 106 | |
| 107 | lirc = lirc_get_pdata(filep); |
| 108 | if (!lirc) |
| 109 | return -EFAULT; |
| 110 | |
| 111 | ir_dev = lirc->ir_dev; |
| 112 | if (!ir_dev || !ir_dev->props || !ir_dev->props->priv) |
| 113 | return -EFAULT; |
| 114 | |
| 115 | drv_data = ir_dev->props->priv; |
| 116 | |
| 117 | switch (cmd) { |
| 118 | case LIRC_SET_TRANSMITTER_MASK: |
| 119 | ret = get_user(val, (unsigned long *)arg); |
| 120 | if (ret) |
| 121 | return ret; |
| 122 | |
| 123 | if (ir_dev->props && ir_dev->props->s_tx_mask) |
| 124 | ret = ir_dev->props->s_tx_mask(drv_data, (u32)val); |
| 125 | else |
| 126 | return -EINVAL; |
| 127 | break; |
| 128 | |
| 129 | case LIRC_SET_SEND_CARRIER: |
| 130 | ret = get_user(val, (unsigned long *)arg); |
| 131 | if (ret) |
| 132 | return ret; |
| 133 | |
| 134 | if (ir_dev->props && ir_dev->props->s_tx_carrier) |
| 135 | ir_dev->props->s_tx_carrier(drv_data, (u32)val); |
| 136 | else |
| 137 | return -EINVAL; |
| 138 | break; |
| 139 | |
| 140 | case LIRC_GET_SEND_MODE: |
| 141 | val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK; |
| 142 | ret = put_user(val, (unsigned long *)arg); |
| 143 | break; |
| 144 | |
| 145 | case LIRC_SET_SEND_MODE: |
| 146 | ret = get_user(val, (unsigned long *)arg); |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | |
| 150 | if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK)) |
| 151 | return -EINVAL; |
| 152 | break; |
| 153 | |
| 154 | default: |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 155 | return lirc_dev_fop_ioctl(filep, cmd, arg); |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | static int ir_lirc_open(void *data) |
| 162 | { |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static void ir_lirc_close(void *data) |
| 167 | { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | static struct file_operations lirc_fops = { |
| 172 | .owner = THIS_MODULE, |
| 173 | .write = ir_lirc_transmit_ir, |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 174 | .unlocked_ioctl = ir_lirc_ioctl, |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 175 | .read = lirc_dev_fop_read, |
| 176 | .poll = lirc_dev_fop_poll, |
| 177 | .open = lirc_dev_fop_open, |
| 178 | .release = lirc_dev_fop_close, |
| 179 | }; |
| 180 | |
| 181 | static int ir_lirc_register(struct input_dev *input_dev) |
| 182 | { |
| 183 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 184 | struct lirc_driver *drv; |
| 185 | struct lirc_buffer *rbuf; |
| 186 | int rc = -ENOMEM; |
| 187 | unsigned long features; |
| 188 | |
| 189 | drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL); |
| 190 | if (!drv) |
| 191 | return rc; |
| 192 | |
| 193 | rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL); |
Dan Carpenter | 49b7a12 | 2010-07-23 07:08:26 -0300 | [diff] [blame] | 194 | if (!rbuf) |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 195 | goto rbuf_alloc_failed; |
| 196 | |
| 197 | rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE); |
| 198 | if (rc) |
| 199 | goto rbuf_init_failed; |
| 200 | |
| 201 | features = LIRC_CAN_REC_MODE2; |
| 202 | if (ir_dev->props->tx_ir) { |
| 203 | features |= LIRC_CAN_SEND_PULSE; |
| 204 | if (ir_dev->props->s_tx_mask) |
| 205 | features |= LIRC_CAN_SET_TRANSMITTER_MASK; |
| 206 | if (ir_dev->props->s_tx_carrier) |
| 207 | features |= LIRC_CAN_SET_SEND_CARRIER; |
| 208 | } |
| 209 | |
| 210 | snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)", |
| 211 | ir_dev->driver_name); |
| 212 | drv->minor = -1; |
| 213 | drv->features = features; |
| 214 | drv->data = &ir_dev->raw->lirc; |
| 215 | drv->rbuf = rbuf; |
| 216 | drv->set_use_inc = &ir_lirc_open; |
| 217 | drv->set_use_dec = &ir_lirc_close; |
| 218 | drv->code_length = sizeof(struct ir_raw_event) * 8; |
| 219 | drv->fops = &lirc_fops; |
| 220 | drv->dev = &ir_dev->dev; |
| 221 | drv->owner = THIS_MODULE; |
| 222 | |
| 223 | drv->minor = lirc_register_driver(drv); |
| 224 | if (drv->minor < 0) { |
| 225 | rc = -ENODEV; |
| 226 | goto lirc_register_failed; |
| 227 | } |
| 228 | |
| 229 | ir_dev->raw->lirc.drv = drv; |
| 230 | ir_dev->raw->lirc.ir_dev = ir_dev; |
Jarod Wilson | ca41469 | 2010-07-03 01:07:53 -0300 | [diff] [blame] | 231 | return 0; |
| 232 | |
| 233 | lirc_register_failed: |
| 234 | rbuf_init_failed: |
| 235 | kfree(rbuf); |
| 236 | rbuf_alloc_failed: |
| 237 | kfree(drv); |
| 238 | |
| 239 | return rc; |
| 240 | } |
| 241 | |
| 242 | static int ir_lirc_unregister(struct input_dev *input_dev) |
| 243 | { |
| 244 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 245 | struct lirc_codec *lirc = &ir_dev->raw->lirc; |
| 246 | |
| 247 | lirc_unregister_driver(lirc->drv->minor); |
| 248 | lirc_buffer_free(lirc->drv->rbuf); |
| 249 | kfree(lirc->drv); |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static struct ir_raw_handler lirc_handler = { |
| 255 | .protocols = IR_TYPE_LIRC, |
| 256 | .decode = ir_lirc_decode, |
| 257 | .raw_register = ir_lirc_register, |
| 258 | .raw_unregister = ir_lirc_unregister, |
| 259 | }; |
| 260 | |
| 261 | static int __init ir_lirc_codec_init(void) |
| 262 | { |
| 263 | ir_raw_handler_register(&lirc_handler); |
| 264 | |
| 265 | printk(KERN_INFO "IR LIRC bridge handler initialized\n"); |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static void __exit ir_lirc_codec_exit(void) |
| 270 | { |
| 271 | ir_raw_handler_unregister(&lirc_handler); |
| 272 | } |
| 273 | |
| 274 | module_init(ir_lirc_codec_init); |
| 275 | module_exit(ir_lirc_codec_exit); |
| 276 | |
| 277 | MODULE_LICENSE("GPL"); |
| 278 | MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>"); |
| 279 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); |
| 280 | MODULE_DESCRIPTION("LIRC IR handler bridge"); |