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