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