Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 1 | /* |
| 2 | * LIRC base driver |
| 3 | * |
| 4 | * by Artur Lipowski <alipowski@interia.pl> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/ioctl.h> |
| 27 | #include <linux/fs.h> |
| 28 | #include <linux/poll.h> |
| 29 | #include <linux/completion.h> |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/mutex.h> |
| 32 | #include <linux/wait.h> |
| 33 | #include <linux/unistd.h> |
| 34 | #include <linux/kthread.h> |
| 35 | #include <linux/bitops.h> |
| 36 | #include <linux/device.h> |
| 37 | #include <linux/cdev.h> |
| 38 | |
| 39 | #include <media/lirc.h> |
Jarod Wilson | 5690085 | 2010-07-16 14:25:33 -0300 | [diff] [blame] | 40 | #include <media/lirc_dev.h> |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 41 | |
| 42 | static int debug; |
| 43 | |
| 44 | #define IRCTL_DEV_NAME "BaseRemoteCtl" |
| 45 | #define NOPLUG -1 |
| 46 | #define LOGHEAD "lirc_dev (%s[%d]): " |
| 47 | |
| 48 | static dev_t lirc_base_dev; |
| 49 | |
| 50 | struct irctl { |
| 51 | struct lirc_driver d; |
| 52 | int attached; |
| 53 | int open; |
| 54 | |
| 55 | struct mutex irctl_lock; |
| 56 | struct lirc_buffer *buf; |
| 57 | unsigned int chunk_size; |
| 58 | |
| 59 | struct task_struct *task; |
| 60 | long jiffies_to_wait; |
| 61 | |
| 62 | struct cdev cdev; |
| 63 | }; |
| 64 | |
| 65 | static DEFINE_MUTEX(lirc_dev_lock); |
| 66 | |
| 67 | static struct irctl *irctls[MAX_IRCTL_DEVICES]; |
| 68 | |
| 69 | /* Only used for sysfs but defined to void otherwise */ |
| 70 | static struct class *lirc_class; |
| 71 | |
| 72 | /* helper function |
| 73 | * initializes the irctl structure |
| 74 | */ |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 75 | static void lirc_irctl_init(struct irctl *ir) |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 76 | { |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 77 | mutex_init(&ir->irctl_lock); |
| 78 | ir->d.minor = NOPLUG; |
| 79 | } |
| 80 | |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 81 | static void lirc_irctl_cleanup(struct irctl *ir) |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 82 | { |
| 83 | dev_dbg(ir->d.dev, LOGHEAD "cleaning up\n", ir->d.name, ir->d.minor); |
| 84 | |
| 85 | device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor)); |
| 86 | |
| 87 | if (ir->buf != ir->d.rbuf) { |
| 88 | lirc_buffer_free(ir->buf); |
| 89 | kfree(ir->buf); |
| 90 | } |
| 91 | ir->buf = NULL; |
| 92 | } |
| 93 | |
| 94 | /* helper function |
| 95 | * reads key codes from driver and puts them into buffer |
| 96 | * returns 0 on success |
| 97 | */ |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 98 | static int lirc_add_to_buf(struct irctl *ir) |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 99 | { |
| 100 | if (ir->d.add_to_buf) { |
| 101 | int res = -ENODATA; |
| 102 | int got_data = 0; |
| 103 | |
| 104 | /* |
| 105 | * service the device as long as it is returning |
| 106 | * data and we have space |
| 107 | */ |
| 108 | get_data: |
| 109 | res = ir->d.add_to_buf(ir->d.data, ir->buf); |
| 110 | if (res == 0) { |
| 111 | got_data++; |
| 112 | goto get_data; |
| 113 | } |
| 114 | |
| 115 | if (res == -ENODEV) |
| 116 | kthread_stop(ir->task); |
| 117 | |
| 118 | return got_data ? 0 : res; |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /* main function of the polling thread |
| 125 | */ |
| 126 | static int lirc_thread(void *irctl) |
| 127 | { |
| 128 | struct irctl *ir = irctl; |
| 129 | |
| 130 | dev_dbg(ir->d.dev, LOGHEAD "poll thread started\n", |
| 131 | ir->d.name, ir->d.minor); |
| 132 | |
| 133 | do { |
| 134 | if (ir->open) { |
| 135 | if (ir->jiffies_to_wait) { |
| 136 | set_current_state(TASK_INTERRUPTIBLE); |
| 137 | schedule_timeout(ir->jiffies_to_wait); |
| 138 | } |
| 139 | if (kthread_should_stop()) |
| 140 | break; |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 141 | if (!lirc_add_to_buf(ir)) |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 142 | wake_up_interruptible(&ir->buf->wait_poll); |
| 143 | } else { |
| 144 | set_current_state(TASK_INTERRUPTIBLE); |
| 145 | schedule(); |
| 146 | } |
| 147 | } while (!kthread_should_stop()); |
| 148 | |
| 149 | dev_dbg(ir->d.dev, LOGHEAD "poll thread ended\n", |
| 150 | ir->d.name, ir->d.minor); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 156 | static struct file_operations lirc_dev_fops = { |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 157 | .owner = THIS_MODULE, |
| 158 | .read = lirc_dev_fop_read, |
| 159 | .write = lirc_dev_fop_write, |
| 160 | .poll = lirc_dev_fop_poll, |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 161 | .unlocked_ioctl = lirc_dev_fop_ioctl, |
Jarod Wilson | 8be292c | 2010-10-09 15:07:06 -0300 | [diff] [blame] | 162 | #ifdef CONFIG_COMPAT |
| 163 | .compat_ioctl = lirc_dev_fop_ioctl, |
| 164 | #endif |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 165 | .open = lirc_dev_fop_open, |
| 166 | .release = lirc_dev_fop_close, |
| 167 | }; |
| 168 | |
| 169 | static int lirc_cdev_add(struct irctl *ir) |
| 170 | { |
| 171 | int retval; |
| 172 | struct lirc_driver *d = &ir->d; |
| 173 | |
| 174 | if (d->fops) { |
| 175 | cdev_init(&ir->cdev, d->fops); |
| 176 | ir->cdev.owner = d->owner; |
| 177 | } else { |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 178 | cdev_init(&ir->cdev, &lirc_dev_fops); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 179 | ir->cdev.owner = THIS_MODULE; |
| 180 | } |
| 181 | kobject_set_name(&ir->cdev.kobj, "lirc%d", d->minor); |
| 182 | |
| 183 | retval = cdev_add(&ir->cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1); |
| 184 | if (retval) |
| 185 | kobject_put(&ir->cdev.kobj); |
| 186 | |
| 187 | return retval; |
| 188 | } |
| 189 | |
| 190 | int lirc_register_driver(struct lirc_driver *d) |
| 191 | { |
| 192 | struct irctl *ir; |
| 193 | int minor; |
| 194 | int bytes_in_key; |
| 195 | unsigned int chunk_size; |
| 196 | unsigned int buffer_size; |
| 197 | int err; |
| 198 | |
| 199 | if (!d) { |
| 200 | printk(KERN_ERR "lirc_dev: lirc_register_driver: " |
| 201 | "driver pointer must be not NULL!\n"); |
| 202 | err = -EBADRQC; |
| 203 | goto out; |
| 204 | } |
| 205 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 206 | if (!d->dev) { |
| 207 | printk(KERN_ERR "%s: dev pointer not filled in!\n", __func__); |
| 208 | err = -EINVAL; |
| 209 | goto out; |
| 210 | } |
| 211 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 212 | if (MAX_IRCTL_DEVICES <= d->minor) { |
| 213 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 214 | "\"minor\" must be between 0 and %d (%d)!\n", |
| 215 | MAX_IRCTL_DEVICES-1, d->minor); |
| 216 | err = -EBADRQC; |
| 217 | goto out; |
| 218 | } |
| 219 | |
| 220 | if (1 > d->code_length || (BUFLEN * 8) < d->code_length) { |
| 221 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 222 | "code length in bits for minor (%d) " |
| 223 | "must be less than %d!\n", |
| 224 | d->minor, BUFLEN * 8); |
| 225 | err = -EBADRQC; |
| 226 | goto out; |
| 227 | } |
| 228 | |
| 229 | dev_dbg(d->dev, "lirc_dev: lirc_register_driver: sample_rate: %d\n", |
| 230 | d->sample_rate); |
| 231 | if (d->sample_rate) { |
| 232 | if (2 > d->sample_rate || HZ < d->sample_rate) { |
| 233 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 234 | "sample_rate must be between 2 and %d!\n", HZ); |
| 235 | err = -EBADRQC; |
| 236 | goto out; |
| 237 | } |
| 238 | if (!d->add_to_buf) { |
| 239 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 240 | "add_to_buf cannot be NULL when " |
| 241 | "sample_rate is set\n"); |
| 242 | err = -EBADRQC; |
| 243 | goto out; |
| 244 | } |
| 245 | } else if (!(d->fops && d->fops->read) && !d->rbuf) { |
| 246 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 247 | "fops->read and rbuf cannot all be NULL!\n"); |
| 248 | err = -EBADRQC; |
| 249 | goto out; |
| 250 | } else if (!d->rbuf) { |
| 251 | if (!(d->fops && d->fops->read && d->fops->poll && |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 252 | d->fops->unlocked_ioctl)) { |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 253 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 254 | "neither read, poll nor unlocked_ioctl can be NULL!\n"); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 255 | err = -EBADRQC; |
| 256 | goto out; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | mutex_lock(&lirc_dev_lock); |
| 261 | |
| 262 | minor = d->minor; |
| 263 | |
| 264 | if (minor < 0) { |
| 265 | /* find first free slot for driver */ |
| 266 | for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++) |
| 267 | if (!irctls[minor]) |
| 268 | break; |
| 269 | if (MAX_IRCTL_DEVICES == minor) { |
| 270 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 271 | "no free slots for drivers!\n"); |
| 272 | err = -ENOMEM; |
| 273 | goto out_lock; |
| 274 | } |
| 275 | } else if (irctls[minor]) { |
| 276 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 277 | "minor (%d) just registered!\n", minor); |
| 278 | err = -EBUSY; |
| 279 | goto out_lock; |
| 280 | } |
| 281 | |
| 282 | ir = kzalloc(sizeof(struct irctl), GFP_KERNEL); |
| 283 | if (!ir) { |
| 284 | err = -ENOMEM; |
| 285 | goto out_lock; |
| 286 | } |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 287 | lirc_irctl_init(ir); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 288 | irctls[minor] = ir; |
| 289 | d->minor = minor; |
| 290 | |
| 291 | if (d->sample_rate) { |
| 292 | ir->jiffies_to_wait = HZ / d->sample_rate; |
| 293 | } else { |
| 294 | /* it means - wait for external event in task queue */ |
| 295 | ir->jiffies_to_wait = 0; |
| 296 | } |
| 297 | |
| 298 | /* some safety check 8-) */ |
| 299 | d->name[sizeof(d->name)-1] = '\0'; |
| 300 | |
| 301 | bytes_in_key = BITS_TO_LONGS(d->code_length) + |
| 302 | (d->code_length % 8 ? 1 : 0); |
| 303 | buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key; |
| 304 | chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key; |
| 305 | |
| 306 | if (d->rbuf) { |
| 307 | ir->buf = d->rbuf; |
| 308 | } else { |
| 309 | ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL); |
| 310 | if (!ir->buf) { |
| 311 | err = -ENOMEM; |
| 312 | goto out_lock; |
| 313 | } |
| 314 | err = lirc_buffer_init(ir->buf, chunk_size, buffer_size); |
| 315 | if (err) { |
| 316 | kfree(ir->buf); |
| 317 | goto out_lock; |
| 318 | } |
| 319 | } |
| 320 | ir->chunk_size = ir->buf->chunk_size; |
| 321 | |
| 322 | if (d->features == 0) |
| 323 | d->features = LIRC_CAN_REC_LIRCCODE; |
| 324 | |
| 325 | ir->d = *d; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 326 | |
| 327 | device_create(lirc_class, ir->d.dev, |
| 328 | MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL, |
| 329 | "lirc%u", ir->d.minor); |
| 330 | |
| 331 | if (d->sample_rate) { |
| 332 | /* try to fire up polling thread */ |
| 333 | ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev"); |
| 334 | if (IS_ERR(ir->task)) { |
| 335 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 336 | "cannot run poll thread for minor = %d\n", |
| 337 | d->minor); |
| 338 | err = -ECHILD; |
| 339 | goto out_sysfs; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | err = lirc_cdev_add(ir); |
| 344 | if (err) |
| 345 | goto out_sysfs; |
| 346 | |
| 347 | ir->attached = 1; |
| 348 | mutex_unlock(&lirc_dev_lock); |
| 349 | |
| 350 | dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n", |
| 351 | ir->d.name, ir->d.minor); |
| 352 | return minor; |
| 353 | |
| 354 | out_sysfs: |
| 355 | device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor)); |
| 356 | out_lock: |
| 357 | mutex_unlock(&lirc_dev_lock); |
| 358 | out: |
| 359 | return err; |
| 360 | } |
| 361 | EXPORT_SYMBOL(lirc_register_driver); |
| 362 | |
| 363 | int lirc_unregister_driver(int minor) |
| 364 | { |
| 365 | struct irctl *ir; |
| 366 | |
| 367 | if (minor < 0 || minor >= MAX_IRCTL_DEVICES) { |
Jarod Wilson | 4fc2154 | 2010-10-09 15:17:03 -0300 | [diff] [blame] | 368 | printk(KERN_ERR "lirc_dev: %s: minor (%d) must be between " |
| 369 | "0 and %d!\n", __func__, minor, MAX_IRCTL_DEVICES-1); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 370 | return -EBADRQC; |
| 371 | } |
| 372 | |
| 373 | ir = irctls[minor]; |
Jarod Wilson | df1868e | 2010-09-17 18:12:31 -0300 | [diff] [blame] | 374 | if (!ir) { |
Jarod Wilson | 4fc2154 | 2010-10-09 15:17:03 -0300 | [diff] [blame] | 375 | printk(KERN_ERR "lirc_dev: %s: failed to get irctl struct " |
| 376 | "for minor %d!\n", __func__, minor); |
Jarod Wilson | df1868e | 2010-09-17 18:12:31 -0300 | [diff] [blame] | 377 | return -ENOENT; |
| 378 | } |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 379 | |
| 380 | mutex_lock(&lirc_dev_lock); |
| 381 | |
| 382 | if (ir->d.minor != minor) { |
Jarod Wilson | 4fc2154 | 2010-10-09 15:17:03 -0300 | [diff] [blame] | 383 | printk(KERN_ERR "lirc_dev: %s: minor (%d) device not " |
| 384 | "registered!\n", __func__, minor); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 385 | mutex_unlock(&lirc_dev_lock); |
| 386 | return -ENOENT; |
| 387 | } |
| 388 | |
| 389 | /* end up polling thread */ |
| 390 | if (ir->task) |
| 391 | kthread_stop(ir->task); |
| 392 | |
| 393 | dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n", |
| 394 | ir->d.name, ir->d.minor); |
| 395 | |
| 396 | ir->attached = 0; |
| 397 | if (ir->open) { |
| 398 | dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n", |
| 399 | ir->d.name, ir->d.minor); |
| 400 | wake_up_interruptible(&ir->buf->wait_poll); |
| 401 | mutex_lock(&ir->irctl_lock); |
| 402 | ir->d.set_use_dec(ir->d.data); |
Jarod Wilson | 90dc4cf | 2010-10-16 21:32:44 -0300 | [diff] [blame] | 403 | module_put(ir->cdev.owner); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 404 | mutex_unlock(&ir->irctl_lock); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 405 | } else { |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 406 | lirc_irctl_cleanup(ir); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 407 | cdev_del(&ir->cdev); |
| 408 | kfree(ir); |
| 409 | irctls[minor] = NULL; |
| 410 | } |
| 411 | |
| 412 | mutex_unlock(&lirc_dev_lock); |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | EXPORT_SYMBOL(lirc_unregister_driver); |
| 417 | |
| 418 | int lirc_dev_fop_open(struct inode *inode, struct file *file) |
| 419 | { |
| 420 | struct irctl *ir; |
| 421 | int retval = 0; |
| 422 | |
| 423 | if (iminor(inode) >= MAX_IRCTL_DEVICES) { |
| 424 | printk(KERN_WARNING "lirc_dev [%d]: open result = -ENODEV\n", |
| 425 | iminor(inode)); |
| 426 | return -ENODEV; |
| 427 | } |
| 428 | |
| 429 | if (mutex_lock_interruptible(&lirc_dev_lock)) |
| 430 | return -ERESTARTSYS; |
| 431 | |
| 432 | ir = irctls[iminor(inode)]; |
| 433 | if (!ir) { |
| 434 | retval = -ENODEV; |
| 435 | goto error; |
| 436 | } |
| 437 | |
| 438 | dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor); |
| 439 | |
| 440 | if (ir->d.minor == NOPLUG) { |
| 441 | retval = -ENODEV; |
| 442 | goto error; |
| 443 | } |
| 444 | |
| 445 | if (ir->open) { |
| 446 | retval = -EBUSY; |
| 447 | goto error; |
| 448 | } |
| 449 | |
Jarod Wilson | 90dc4cf | 2010-10-16 21:32:44 -0300 | [diff] [blame] | 450 | if (try_module_get(ir->cdev.owner)) { |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 451 | ++ir->open; |
| 452 | retval = ir->d.set_use_inc(ir->d.data); |
| 453 | |
| 454 | if (retval) { |
Jarod Wilson | 90dc4cf | 2010-10-16 21:32:44 -0300 | [diff] [blame] | 455 | module_put(ir->cdev.owner); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 456 | --ir->open; |
| 457 | } else { |
| 458 | lirc_buffer_clear(ir->buf); |
| 459 | } |
| 460 | if (ir->task) |
| 461 | wake_up_process(ir->task); |
| 462 | } |
| 463 | |
| 464 | error: |
| 465 | if (ir) |
| 466 | dev_dbg(ir->d.dev, LOGHEAD "open result = %d\n", |
| 467 | ir->d.name, ir->d.minor, retval); |
| 468 | |
| 469 | mutex_unlock(&lirc_dev_lock); |
| 470 | |
| 471 | return retval; |
| 472 | } |
| 473 | EXPORT_SYMBOL(lirc_dev_fop_open); |
| 474 | |
| 475 | int lirc_dev_fop_close(struct inode *inode, struct file *file) |
| 476 | { |
| 477 | struct irctl *ir = irctls[iminor(inode)]; |
| 478 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 479 | if (!ir) { |
| 480 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); |
| 481 | return -EINVAL; |
| 482 | } |
| 483 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 484 | dev_dbg(ir->d.dev, LOGHEAD "close called\n", ir->d.name, ir->d.minor); |
| 485 | |
| 486 | WARN_ON(mutex_lock_killable(&lirc_dev_lock)); |
| 487 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 488 | ir->open--; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 489 | if (ir->attached) { |
| 490 | ir->d.set_use_dec(ir->d.data); |
Jarod Wilson | 90dc4cf | 2010-10-16 21:32:44 -0300 | [diff] [blame] | 491 | module_put(ir->cdev.owner); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 492 | } else { |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 493 | lirc_irctl_cleanup(ir); |
Jarod Wilson | 09c8dd8 | 2010-10-18 17:30:20 -0300 | [diff] [blame^] | 494 | cdev_del(&ir->cdev); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 495 | irctls[ir->d.minor] = NULL; |
| 496 | kfree(ir); |
| 497 | } |
| 498 | |
| 499 | mutex_unlock(&lirc_dev_lock); |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | EXPORT_SYMBOL(lirc_dev_fop_close); |
| 504 | |
| 505 | unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait) |
| 506 | { |
| 507 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; |
| 508 | unsigned int ret; |
| 509 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 510 | if (!ir) { |
| 511 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); |
| 512 | return POLLERR; |
| 513 | } |
| 514 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 515 | dev_dbg(ir->d.dev, LOGHEAD "poll called\n", ir->d.name, ir->d.minor); |
| 516 | |
| 517 | if (!ir->attached) { |
| 518 | mutex_unlock(&ir->irctl_lock); |
| 519 | return POLLERR; |
| 520 | } |
| 521 | |
| 522 | poll_wait(file, &ir->buf->wait_poll, wait); |
| 523 | |
| 524 | if (ir->buf) |
| 525 | if (lirc_buffer_empty(ir->buf)) |
| 526 | ret = 0; |
| 527 | else |
| 528 | ret = POLLIN | POLLRDNORM; |
| 529 | else |
| 530 | ret = POLLERR; |
| 531 | |
| 532 | dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n", |
| 533 | ir->d.name, ir->d.minor, ret); |
| 534 | |
| 535 | return ret; |
| 536 | } |
| 537 | EXPORT_SYMBOL(lirc_dev_fop_poll); |
| 538 | |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 539 | long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 540 | { |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 541 | __u32 mode; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 542 | int result = 0; |
Jarod Wilson | d889a13 | 2010-10-16 21:36:43 -0300 | [diff] [blame] | 543 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 544 | |
Jarod Wilson | 8be292c | 2010-10-09 15:07:06 -0300 | [diff] [blame] | 545 | if (!ir) { |
| 546 | printk(KERN_ERR "lirc_dev: %s: no irctl found!\n", __func__); |
| 547 | return -ENODEV; |
| 548 | } |
| 549 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 550 | dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n", |
| 551 | ir->d.name, ir->d.minor, cmd); |
| 552 | |
| 553 | if (ir->d.minor == NOPLUG || !ir->attached) { |
| 554 | dev_dbg(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n", |
| 555 | ir->d.name, ir->d.minor); |
| 556 | return -ENODEV; |
| 557 | } |
| 558 | |
| 559 | mutex_lock(&ir->irctl_lock); |
| 560 | |
| 561 | switch (cmd) { |
| 562 | case LIRC_GET_FEATURES: |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 563 | result = put_user(ir->d.features, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 564 | break; |
| 565 | case LIRC_GET_REC_MODE: |
| 566 | if (!(ir->d.features & LIRC_CAN_REC_MASK)) { |
| 567 | result = -ENOSYS; |
| 568 | break; |
| 569 | } |
| 570 | |
| 571 | result = put_user(LIRC_REC2MODE |
| 572 | (ir->d.features & LIRC_CAN_REC_MASK), |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 573 | (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 574 | break; |
| 575 | case LIRC_SET_REC_MODE: |
| 576 | if (!(ir->d.features & LIRC_CAN_REC_MASK)) { |
| 577 | result = -ENOSYS; |
| 578 | break; |
| 579 | } |
| 580 | |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 581 | result = get_user(mode, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 582 | if (!result && !(LIRC_MODE2REC(mode) & ir->d.features)) |
| 583 | result = -EINVAL; |
| 584 | /* |
| 585 | * FIXME: We should actually set the mode somehow but |
| 586 | * for now, lirc_serial doesn't support mode changing either |
| 587 | */ |
| 588 | break; |
| 589 | case LIRC_GET_LENGTH: |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 590 | result = put_user(ir->d.code_length, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 591 | break; |
| 592 | case LIRC_GET_MIN_TIMEOUT: |
| 593 | if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || |
| 594 | ir->d.min_timeout == 0) { |
| 595 | result = -ENOSYS; |
| 596 | break; |
| 597 | } |
| 598 | |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 599 | result = put_user(ir->d.min_timeout, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 600 | break; |
| 601 | case LIRC_GET_MAX_TIMEOUT: |
| 602 | if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || |
| 603 | ir->d.max_timeout == 0) { |
| 604 | result = -ENOSYS; |
| 605 | break; |
| 606 | } |
| 607 | |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 608 | result = put_user(ir->d.max_timeout, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 609 | break; |
| 610 | default: |
| 611 | result = -EINVAL; |
| 612 | } |
| 613 | |
| 614 | dev_dbg(ir->d.dev, LOGHEAD "ioctl result = %d\n", |
| 615 | ir->d.name, ir->d.minor, result); |
| 616 | |
| 617 | mutex_unlock(&ir->irctl_lock); |
| 618 | |
| 619 | return result; |
| 620 | } |
| 621 | EXPORT_SYMBOL(lirc_dev_fop_ioctl); |
| 622 | |
| 623 | ssize_t lirc_dev_fop_read(struct file *file, |
| 624 | char *buffer, |
| 625 | size_t length, |
| 626 | loff_t *ppos) |
| 627 | { |
| 628 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 629 | unsigned char *buf; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 630 | int ret = 0, written = 0; |
| 631 | DECLARE_WAITQUEUE(wait, current); |
| 632 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 633 | if (!ir) { |
| 634 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); |
| 635 | return -ENODEV; |
| 636 | } |
| 637 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 638 | dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor); |
| 639 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 640 | buf = kzalloc(ir->chunk_size, GFP_KERNEL); |
| 641 | if (!buf) |
| 642 | return -ENOMEM; |
| 643 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 644 | if (mutex_lock_interruptible(&ir->irctl_lock)) |
| 645 | return -ERESTARTSYS; |
| 646 | if (!ir->attached) { |
| 647 | mutex_unlock(&ir->irctl_lock); |
| 648 | return -ENODEV; |
| 649 | } |
| 650 | |
| 651 | if (length % ir->chunk_size) { |
| 652 | dev_dbg(ir->d.dev, LOGHEAD "read result = -EINVAL\n", |
| 653 | ir->d.name, ir->d.minor); |
| 654 | mutex_unlock(&ir->irctl_lock); |
| 655 | return -EINVAL; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * we add ourselves to the task queue before buffer check |
| 660 | * to avoid losing scan code (in case when queue is awaken somewhere |
| 661 | * between while condition checking and scheduling) |
| 662 | */ |
| 663 | add_wait_queue(&ir->buf->wait_poll, &wait); |
| 664 | set_current_state(TASK_INTERRUPTIBLE); |
| 665 | |
| 666 | /* |
| 667 | * while we didn't provide 'length' bytes, device is opened in blocking |
| 668 | * mode and 'copy_to_user' is happy, wait for data. |
| 669 | */ |
| 670 | while (written < length && ret == 0) { |
| 671 | if (lirc_buffer_empty(ir->buf)) { |
| 672 | /* According to the read(2) man page, 'written' can be |
| 673 | * returned as less than 'length', instead of blocking |
| 674 | * again, returning -EWOULDBLOCK, or returning |
| 675 | * -ERESTARTSYS */ |
| 676 | if (written) |
| 677 | break; |
| 678 | if (file->f_flags & O_NONBLOCK) { |
| 679 | ret = -EWOULDBLOCK; |
| 680 | break; |
| 681 | } |
| 682 | if (signal_pending(current)) { |
| 683 | ret = -ERESTARTSYS; |
| 684 | break; |
| 685 | } |
| 686 | |
| 687 | mutex_unlock(&ir->irctl_lock); |
| 688 | schedule(); |
| 689 | set_current_state(TASK_INTERRUPTIBLE); |
| 690 | |
| 691 | if (mutex_lock_interruptible(&ir->irctl_lock)) { |
| 692 | ret = -ERESTARTSYS; |
Jarod Wilson | 69c271f | 2010-07-07 11:29:44 -0300 | [diff] [blame] | 693 | remove_wait_queue(&ir->buf->wait_poll, &wait); |
| 694 | set_current_state(TASK_RUNNING); |
| 695 | goto out_unlocked; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | if (!ir->attached) { |
| 699 | ret = -ENODEV; |
| 700 | break; |
| 701 | } |
| 702 | } else { |
| 703 | lirc_buffer_read(ir->buf, buf); |
| 704 | ret = copy_to_user((void *)buffer+written, buf, |
| 705 | ir->buf->chunk_size); |
| 706 | written += ir->buf->chunk_size; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | remove_wait_queue(&ir->buf->wait_poll, &wait); |
| 711 | set_current_state(TASK_RUNNING); |
| 712 | mutex_unlock(&ir->irctl_lock); |
| 713 | |
Jarod Wilson | 69c271f | 2010-07-07 11:29:44 -0300 | [diff] [blame] | 714 | out_unlocked: |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 715 | kfree(buf); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 716 | dev_dbg(ir->d.dev, LOGHEAD "read result = %s (%d)\n", |
| 717 | ir->d.name, ir->d.minor, ret ? "-EFAULT" : "OK", ret); |
| 718 | |
| 719 | return ret ? ret : written; |
| 720 | } |
| 721 | EXPORT_SYMBOL(lirc_dev_fop_read); |
| 722 | |
| 723 | void *lirc_get_pdata(struct file *file) |
| 724 | { |
| 725 | void *data = NULL; |
| 726 | |
| 727 | if (file && file->f_dentry && file->f_dentry->d_inode && |
| 728 | file->f_dentry->d_inode->i_rdev) { |
| 729 | struct irctl *ir; |
| 730 | ir = irctls[iminor(file->f_dentry->d_inode)]; |
| 731 | data = ir->d.data; |
| 732 | } |
| 733 | |
| 734 | return data; |
| 735 | } |
| 736 | EXPORT_SYMBOL(lirc_get_pdata); |
| 737 | |
| 738 | |
| 739 | ssize_t lirc_dev_fop_write(struct file *file, const char *buffer, |
| 740 | size_t length, loff_t *ppos) |
| 741 | { |
| 742 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; |
| 743 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 744 | if (!ir) { |
| 745 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); |
| 746 | return -ENODEV; |
| 747 | } |
| 748 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 749 | dev_dbg(ir->d.dev, LOGHEAD "write called\n", ir->d.name, ir->d.minor); |
| 750 | |
| 751 | if (!ir->attached) |
| 752 | return -ENODEV; |
| 753 | |
| 754 | return -EINVAL; |
| 755 | } |
| 756 | EXPORT_SYMBOL(lirc_dev_fop_write); |
| 757 | |
| 758 | |
| 759 | static int __init lirc_dev_init(void) |
| 760 | { |
| 761 | int retval; |
| 762 | |
| 763 | lirc_class = class_create(THIS_MODULE, "lirc"); |
| 764 | if (IS_ERR(lirc_class)) { |
| 765 | retval = PTR_ERR(lirc_class); |
| 766 | printk(KERN_ERR "lirc_dev: class_create failed\n"); |
| 767 | goto error; |
| 768 | } |
| 769 | |
| 770 | retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES, |
| 771 | IRCTL_DEV_NAME); |
| 772 | if (retval) { |
| 773 | class_destroy(lirc_class); |
| 774 | printk(KERN_ERR "lirc_dev: alloc_chrdev_region failed\n"); |
| 775 | goto error; |
| 776 | } |
| 777 | |
| 778 | |
| 779 | printk(KERN_INFO "lirc_dev: IR Remote Control driver registered, " |
| 780 | "major %d \n", MAJOR(lirc_base_dev)); |
| 781 | |
| 782 | error: |
| 783 | return retval; |
| 784 | } |
| 785 | |
| 786 | |
| 787 | |
| 788 | static void __exit lirc_dev_exit(void) |
| 789 | { |
| 790 | class_destroy(lirc_class); |
| 791 | unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES); |
| 792 | printk(KERN_INFO "lirc_dev: module unloaded\n"); |
| 793 | } |
| 794 | |
| 795 | module_init(lirc_dev_init); |
| 796 | module_exit(lirc_dev_exit); |
| 797 | |
| 798 | MODULE_DESCRIPTION("LIRC base driver module"); |
| 799 | MODULE_AUTHOR("Artur Lipowski"); |
| 800 | MODULE_LICENSE("GPL"); |
| 801 | |
| 802 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 803 | MODULE_PARM_DESC(debug, "Enable debugging messages"); |