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