Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * LIRC base driver
4 *
5 * by Artur Lipowski <alipowski@interia.pl>
6 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/module.h>
11#include <linux/mutex.h>
12#include <linux/device.h>
13#include <linux/file.h>
14#include <linux/idr.h>
15#include <linux/poll.h>
16#include <linux/sched.h>
17#include <linux/wait.h>
18
19#include "rc-core-priv.h"
20#include <uapi/linux/lirc.h>
21
22#define LIRCBUF_SIZE 1024
23
24static dev_t lirc_base_dev;
25
26/* Used to keep track of allocated lirc devices */
27static DEFINE_IDA(lirc_ida);
28
29/* Only used for sysfs but defined to void otherwise */
30static struct class *lirc_class;
31
32/**
33 * lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
34 *
35 * @dev: the struct rc_dev descriptor of the device
36 * @ev: the struct ir_raw_event descriptor of the pulse/space
37 */
38void lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
39{
40 unsigned long flags;
41 struct lirc_fh *fh;
42 int sample;
43
44 /* Receiver overflow, data missing */
45 if (ev.overflow) {
46 /*
47 * Send lirc overflow message. This message is unknown to
48 * lircd, but it will interpret this as a long space as
49 * long as the value is set to high value. This resets its
50 * decoder state.
51 */
52 sample = LIRC_OVERFLOW(LIRC_VALUE_MASK);
53 dev_dbg(&dev->dev, "delivering overflow to lirc_dev\n");
54
55 /* Carrier reports */
56 } else if (ev.carrier_report) {
57 sample = LIRC_FREQUENCY(ev.carrier);
58 dev_dbg(&dev->dev, "carrier report (freq: %d)\n", sample);
59
60 /* Packet end */
61 } else if (ev.timeout) {
62 dev->gap_start = ktime_get();
63
64 sample = LIRC_TIMEOUT(ev.duration);
65 dev_dbg(&dev->dev, "timeout report (duration: %d)\n", sample);
66
67 /* Normal sample */
68 } else {
69 if (dev->gap_start) {
70 u64 duration = ktime_us_delta(ktime_get(),
71 dev->gap_start);
72
73 /* Cap by LIRC_VALUE_MASK */
74 duration = min_t(u64, duration, LIRC_VALUE_MASK);
75
76 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
77 list_for_each_entry(fh, &dev->lirc_fh, list)
78 kfifo_put(&fh->rawir, LIRC_SPACE(duration));
79 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
80 dev->gap_start = 0;
81 }
82
83 sample = ev.pulse ? LIRC_PULSE(ev.duration) :
84 LIRC_SPACE(ev.duration);
85 dev_dbg(&dev->dev, "delivering %uus %s to lirc_dev\n",
86 ev.duration, TO_STR(ev.pulse));
87 }
88
89 /*
90 * bpf does not care about the gap generated above; that exists
91 * for backwards compatibility
92 */
93 lirc_bpf_run(dev, sample);
94
95 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
96 list_for_each_entry(fh, &dev->lirc_fh, list) {
97 if (kfifo_put(&fh->rawir, sample))
98 wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
99 }
100 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
101}
102
103/**
104 * lirc_scancode_event() - Send scancode data to lirc to be relayed to
105 * userspace. This can be called in atomic context.
106 * @dev: the struct rc_dev descriptor of the device
107 * @lsc: the struct lirc_scancode describing the decoded scancode
108 */
109void lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc)
110{
111 unsigned long flags;
112 struct lirc_fh *fh;
113
114 lsc->timestamp = ktime_get_ns();
115
116 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
117 list_for_each_entry(fh, &dev->lirc_fh, list) {
118 if (kfifo_put(&fh->scancodes, *lsc))
119 wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
120 }
121 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
122}
123EXPORT_SYMBOL_GPL(lirc_scancode_event);
124
125static int lirc_open(struct inode *inode, struct file *file)
126{
127 struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev,
128 lirc_cdev);
129 struct lirc_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
130 unsigned long flags;
131 int retval;
132
133 if (!fh)
134 return -ENOMEM;
135
136 get_device(&dev->dev);
137
138 if (!dev->registered) {
139 retval = -ENODEV;
140 goto out_fh;
141 }
142
143 if (dev->driver_type == RC_DRIVER_IR_RAW) {
144 if (kfifo_alloc(&fh->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL)) {
145 retval = -ENOMEM;
146 goto out_fh;
147 }
148 }
149
150 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
151 if (kfifo_alloc(&fh->scancodes, 32, GFP_KERNEL)) {
152 retval = -ENOMEM;
153 goto out_rawir;
154 }
155 }
156
157 fh->send_mode = LIRC_MODE_PULSE;
158 fh->rc = dev;
159
160 if (dev->driver_type == RC_DRIVER_SCANCODE)
161 fh->rec_mode = LIRC_MODE_SCANCODE;
162 else
163 fh->rec_mode = LIRC_MODE_MODE2;
164
165 retval = rc_open(dev);
166 if (retval)
167 goto out_kfifo;
168
169 init_waitqueue_head(&fh->wait_poll);
170
171 file->private_data = fh;
172 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
173 list_add(&fh->list, &dev->lirc_fh);
174 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
175
176 stream_open(inode, file);
177
178 return 0;
179out_kfifo:
180 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
181 kfifo_free(&fh->scancodes);
182out_rawir:
183 if (dev->driver_type == RC_DRIVER_IR_RAW)
184 kfifo_free(&fh->rawir);
185out_fh:
186 kfree(fh);
187 put_device(&dev->dev);
188
189 return retval;
190}
191
192static int lirc_close(struct inode *inode, struct file *file)
193{
194 struct lirc_fh *fh = file->private_data;
195 struct rc_dev *dev = fh->rc;
196 unsigned long flags;
197
198 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
199 list_del(&fh->list);
200 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
201
202 if (dev->driver_type == RC_DRIVER_IR_RAW)
203 kfifo_free(&fh->rawir);
204 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
205 kfifo_free(&fh->scancodes);
206 kfree(fh);
207
208 rc_close(dev);
209 put_device(&dev->dev);
210
211 return 0;
212}
213
214static ssize_t lirc_transmit(struct file *file, const char __user *buf,
215 size_t n, loff_t *ppos)
216{
217 struct lirc_fh *fh = file->private_data;
218 struct rc_dev *dev = fh->rc;
219 unsigned int *txbuf;
220 struct ir_raw_event *raw = NULL;
221 ssize_t ret;
222 size_t count;
223 ktime_t start;
224 s64 towait;
225 unsigned int duration = 0; /* signal duration in us */
226 int i;
227
228 ret = mutex_lock_interruptible(&dev->lock);
229 if (ret)
230 return ret;
231
232 if (!dev->registered) {
233 ret = -ENODEV;
234 goto out_unlock;
235 }
236
237 if (!dev->tx_ir) {
238 ret = -EINVAL;
239 goto out_unlock;
240 }
241
242 if (fh->send_mode == LIRC_MODE_SCANCODE) {
243 struct lirc_scancode scan;
244
245 if (n != sizeof(scan)) {
246 ret = -EINVAL;
247 goto out_unlock;
248 }
249
250 if (copy_from_user(&scan, buf, sizeof(scan))) {
251 ret = -EFAULT;
252 goto out_unlock;
253 }
254
255 if (scan.flags || scan.keycode || scan.timestamp ||
256 scan.rc_proto > RC_PROTO_MAX) {
257 ret = -EINVAL;
258 goto out_unlock;
259 }
260
261 /* We only have encoders for 32-bit protocols. */
262 if (scan.scancode > U32_MAX ||
263 !rc_validate_scancode(scan.rc_proto, scan.scancode)) {
264 ret = -EINVAL;
265 goto out_unlock;
266 }
267
268 raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
269 if (!raw) {
270 ret = -ENOMEM;
271 goto out_unlock;
272 }
273
274 ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
275 raw, LIRCBUF_SIZE);
276 if (ret < 0)
277 goto out_kfree_raw;
278
279 count = ret;
280
281 txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
282 if (!txbuf) {
283 ret = -ENOMEM;
284 goto out_kfree_raw;
285 }
286
287 for (i = 0; i < count; i++)
288 txbuf[i] = raw[i].duration;
289
290 if (dev->s_tx_carrier) {
291 int carrier = ir_raw_encode_carrier(scan.rc_proto);
292
293 if (carrier > 0)
294 dev->s_tx_carrier(dev, carrier);
295 }
296 } else {
297 if (n < sizeof(unsigned int) || n % sizeof(unsigned int)) {
298 ret = -EINVAL;
299 goto out_unlock;
300 }
301
302 count = n / sizeof(unsigned int);
303 if (count > LIRCBUF_SIZE || count % 2 == 0) {
304 ret = -EINVAL;
305 goto out_unlock;
306 }
307
308 txbuf = memdup_user(buf, n);
309 if (IS_ERR(txbuf)) {
310 ret = PTR_ERR(txbuf);
311 goto out_unlock;
312 }
313 }
314
315 for (i = 0; i < count; i++) {
316 if (txbuf[i] > IR_MAX_DURATION - duration || !txbuf[i]) {
317 ret = -EINVAL;
318 goto out_kfree;
319 }
320
321 duration += txbuf[i];
322 }
323
324 start = ktime_get();
325
326 ret = dev->tx_ir(dev, txbuf, count);
327 if (ret < 0)
328 goto out_kfree;
329
330 kfree(txbuf);
331 kfree(raw);
332 mutex_unlock(&dev->lock);
333
334 /*
335 * The lircd gap calculation expects the write function to
336 * wait for the actual IR signal to be transmitted before
337 * returning.
338 */
339 towait = ktime_us_delta(ktime_add_us(start, duration),
340 ktime_get());
341 if (towait > 0) {
342 set_current_state(TASK_INTERRUPTIBLE);
343 schedule_timeout(usecs_to_jiffies(towait));
344 }
345
346 return n;
347out_kfree:
348 kfree(txbuf);
349out_kfree_raw:
350 kfree(raw);
351out_unlock:
352 mutex_unlock(&dev->lock);
353 return ret;
354}
355
356static long lirc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
357{
358 struct lirc_fh *fh = file->private_data;
359 struct rc_dev *dev = fh->rc;
360 u32 __user *argp = (u32 __user *)(arg);
361 u32 val = 0;
362 int ret;
363
364 if (_IOC_DIR(cmd) & _IOC_WRITE) {
365 ret = get_user(val, argp);
366 if (ret)
367 return ret;
368 }
369
370 ret = mutex_lock_interruptible(&dev->lock);
371 if (ret)
372 return ret;
373
374 if (!dev->registered) {
375 ret = -ENODEV;
376 goto out;
377 }
378
379 switch (cmd) {
380 case LIRC_GET_FEATURES:
381 if (dev->driver_type == RC_DRIVER_SCANCODE)
382 val |= LIRC_CAN_REC_SCANCODE;
383
384 if (dev->driver_type == RC_DRIVER_IR_RAW) {
385 val |= LIRC_CAN_REC_MODE2;
386 if (dev->rx_resolution)
387 val |= LIRC_CAN_GET_REC_RESOLUTION;
388 }
389
390 if (dev->tx_ir) {
391 val |= LIRC_CAN_SEND_PULSE;
392 if (dev->s_tx_mask)
393 val |= LIRC_CAN_SET_TRANSMITTER_MASK;
394 if (dev->s_tx_carrier)
395 val |= LIRC_CAN_SET_SEND_CARRIER;
396 if (dev->s_tx_duty_cycle)
397 val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
398 }
399
400 if (dev->s_rx_carrier_range)
401 val |= LIRC_CAN_SET_REC_CARRIER |
402 LIRC_CAN_SET_REC_CARRIER_RANGE;
403
404 if (dev->s_wideband_receiver)
405 val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
406
407 if (dev->s_carrier_report)
408 val |= LIRC_CAN_MEASURE_CARRIER;
409
410 if (dev->max_timeout)
411 val |= LIRC_CAN_SET_REC_TIMEOUT;
412
413 break;
414
415 /* mode support */
416 case LIRC_GET_REC_MODE:
417 if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
418 ret = -ENOTTY;
419 else
420 val = fh->rec_mode;
421 break;
422
423 case LIRC_SET_REC_MODE:
424 switch (dev->driver_type) {
425 case RC_DRIVER_IR_RAW_TX:
426 ret = -ENOTTY;
427 break;
428 case RC_DRIVER_SCANCODE:
429 if (val != LIRC_MODE_SCANCODE)
430 ret = -EINVAL;
431 break;
432 case RC_DRIVER_IR_RAW:
433 if (!(val == LIRC_MODE_MODE2 ||
434 val == LIRC_MODE_SCANCODE))
435 ret = -EINVAL;
436 break;
437 }
438
439 if (!ret)
440 fh->rec_mode = val;
441 break;
442
443 case LIRC_GET_SEND_MODE:
444 if (!dev->tx_ir)
445 ret = -ENOTTY;
446 else
447 val = fh->send_mode;
448 break;
449
450 case LIRC_SET_SEND_MODE:
451 if (!dev->tx_ir)
452 ret = -ENOTTY;
453 else if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
454 ret = -EINVAL;
455 else
456 fh->send_mode = val;
457 break;
458
459 /* TX settings */
460 case LIRC_SET_TRANSMITTER_MASK:
461 if (!dev->s_tx_mask)
462 ret = -ENOTTY;
463 else
464 ret = dev->s_tx_mask(dev, val);
465 break;
466
467 case LIRC_SET_SEND_CARRIER:
468 if (!dev->s_tx_carrier)
469 ret = -ENOTTY;
470 else
471 ret = dev->s_tx_carrier(dev, val);
472 break;
473
474 case LIRC_SET_SEND_DUTY_CYCLE:
475 if (!dev->s_tx_duty_cycle)
476 ret = -ENOTTY;
477 else if (val <= 0 || val >= 100)
478 ret = -EINVAL;
479 else
480 ret = dev->s_tx_duty_cycle(dev, val);
481 break;
482
483 /* RX settings */
484 case LIRC_SET_REC_CARRIER:
485 if (!dev->s_rx_carrier_range)
486 ret = -ENOTTY;
487 else if (val <= 0)
488 ret = -EINVAL;
489 else
490 ret = dev->s_rx_carrier_range(dev, fh->carrier_low,
491 val);
492 break;
493
494 case LIRC_SET_REC_CARRIER_RANGE:
495 if (!dev->s_rx_carrier_range)
496 ret = -ENOTTY;
497 else if (val <= 0)
498 ret = -EINVAL;
499 else
500 fh->carrier_low = val;
501 break;
502
503 case LIRC_GET_REC_RESOLUTION:
504 if (!dev->rx_resolution)
505 ret = -ENOTTY;
506 else
507 val = dev->rx_resolution;
508 break;
509
510 case LIRC_SET_WIDEBAND_RECEIVER:
511 if (!dev->s_wideband_receiver)
512 ret = -ENOTTY;
513 else
514 ret = dev->s_wideband_receiver(dev, !!val);
515 break;
516
517 case LIRC_SET_MEASURE_CARRIER_MODE:
518 if (!dev->s_carrier_report)
519 ret = -ENOTTY;
520 else
521 ret = dev->s_carrier_report(dev, !!val);
522 break;
523
524 /* Generic timeout support */
525 case LIRC_GET_MIN_TIMEOUT:
526 if (!dev->max_timeout)
527 ret = -ENOTTY;
528 else
529 val = dev->min_timeout;
530 break;
531
532 case LIRC_GET_MAX_TIMEOUT:
533 if (!dev->max_timeout)
534 ret = -ENOTTY;
535 else
536 val = dev->max_timeout;
537 break;
538
539 case LIRC_SET_REC_TIMEOUT:
540 if (!dev->max_timeout) {
541 ret = -ENOTTY;
542 } else {
543 if (val < dev->min_timeout || val > dev->max_timeout)
544 ret = -EINVAL;
545 else if (dev->s_timeout)
546 ret = dev->s_timeout(dev, val);
547 else
548 dev->timeout = val;
549 }
550 break;
551
552 case LIRC_GET_REC_TIMEOUT:
553 if (!dev->timeout)
554 ret = -ENOTTY;
555 else
556 val = dev->timeout;
557 break;
558
559 case LIRC_SET_REC_TIMEOUT_REPORTS:
560 if (dev->driver_type != RC_DRIVER_IR_RAW)
561 ret = -ENOTTY;
562 break;
563
564 default:
565 ret = -ENOTTY;
566 }
567
568 if (!ret && _IOC_DIR(cmd) & _IOC_READ)
569 ret = put_user(val, argp);
570
571out:
572 mutex_unlock(&dev->lock);
573 return ret;
574}
575
576static __poll_t lirc_poll(struct file *file, struct poll_table_struct *wait)
577{
578 struct lirc_fh *fh = file->private_data;
579 struct rc_dev *rcdev = fh->rc;
580 __poll_t events = 0;
581
582 poll_wait(file, &fh->wait_poll, wait);
583
584 if (!rcdev->registered) {
585 events = EPOLLHUP | EPOLLERR;
586 } else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
587 if (fh->rec_mode == LIRC_MODE_SCANCODE &&
588 !kfifo_is_empty(&fh->scancodes))
589 events = EPOLLIN | EPOLLRDNORM;
590
591 if (fh->rec_mode == LIRC_MODE_MODE2 &&
592 !kfifo_is_empty(&fh->rawir))
593 events = EPOLLIN | EPOLLRDNORM;
594 }
595
596 return events;
597}
598
599static ssize_t lirc_read_mode2(struct file *file, char __user *buffer,
600 size_t length)
601{
602 struct lirc_fh *fh = file->private_data;
603 struct rc_dev *rcdev = fh->rc;
604 unsigned int copied;
605 int ret;
606
607 if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
608 return -EINVAL;
609
610 do {
611 if (kfifo_is_empty(&fh->rawir)) {
612 if (file->f_flags & O_NONBLOCK)
613 return -EAGAIN;
614
615 ret = wait_event_interruptible(fh->wait_poll,
616 !kfifo_is_empty(&fh->rawir) ||
617 !rcdev->registered);
618 if (ret)
619 return ret;
620 }
621
622 if (!rcdev->registered)
623 return -ENODEV;
624
625 ret = mutex_lock_interruptible(&rcdev->lock);
626 if (ret)
627 return ret;
628 ret = kfifo_to_user(&fh->rawir, buffer, length, &copied);
629 mutex_unlock(&rcdev->lock);
630 if (ret)
631 return ret;
632 } while (copied == 0);
633
634 return copied;
635}
636
637static ssize_t lirc_read_scancode(struct file *file, char __user *buffer,
638 size_t length)
639{
640 struct lirc_fh *fh = file->private_data;
641 struct rc_dev *rcdev = fh->rc;
642 unsigned int copied;
643 int ret;
644
645 if (length < sizeof(struct lirc_scancode) ||
646 length % sizeof(struct lirc_scancode))
647 return -EINVAL;
648
649 do {
650 if (kfifo_is_empty(&fh->scancodes)) {
651 if (file->f_flags & O_NONBLOCK)
652 return -EAGAIN;
653
654 ret = wait_event_interruptible(fh->wait_poll,
655 !kfifo_is_empty(&fh->scancodes) ||
656 !rcdev->registered);
657 if (ret)
658 return ret;
659 }
660
661 if (!rcdev->registered)
662 return -ENODEV;
663
664 ret = mutex_lock_interruptible(&rcdev->lock);
665 if (ret)
666 return ret;
667 ret = kfifo_to_user(&fh->scancodes, buffer, length, &copied);
668 mutex_unlock(&rcdev->lock);
669 if (ret)
670 return ret;
671 } while (copied == 0);
672
673 return copied;
674}
675
676static ssize_t lirc_read(struct file *file, char __user *buffer, size_t length,
677 loff_t *ppos)
678{
679 struct lirc_fh *fh = file->private_data;
680 struct rc_dev *rcdev = fh->rc;
681
682 if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
683 return -EINVAL;
684
685 if (!rcdev->registered)
686 return -ENODEV;
687
688 if (fh->rec_mode == LIRC_MODE_MODE2)
689 return lirc_read_mode2(file, buffer, length);
690 else /* LIRC_MODE_SCANCODE */
691 return lirc_read_scancode(file, buffer, length);
692}
693
694static const struct file_operations lirc_fops = {
695 .owner = THIS_MODULE,
696 .write = lirc_transmit,
697 .unlocked_ioctl = lirc_ioctl,
698 .compat_ioctl = compat_ptr_ioctl,
699 .read = lirc_read,
700 .poll = lirc_poll,
701 .open = lirc_open,
702 .release = lirc_close,
703 .llseek = no_llseek,
704};
705
706static void lirc_release_device(struct device *ld)
707{
708 struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);
709
710 put_device(&rcdev->dev);
711}
712
713int lirc_register(struct rc_dev *dev)
714{
715 const char *rx_type, *tx_type;
716 int err, minor;
717
718 minor = ida_alloc_max(&lirc_ida, RC_DEV_MAX - 1, GFP_KERNEL);
719 if (minor < 0)
720 return minor;
721
722 device_initialize(&dev->lirc_dev);
723 dev->lirc_dev.class = lirc_class;
724 dev->lirc_dev.parent = &dev->dev;
725 dev->lirc_dev.release = lirc_release_device;
726 dev->lirc_dev.devt = MKDEV(MAJOR(lirc_base_dev), minor);
727 dev_set_name(&dev->lirc_dev, "lirc%d", minor);
728
729 INIT_LIST_HEAD(&dev->lirc_fh);
730 spin_lock_init(&dev->lirc_fh_lock);
731
732 cdev_init(&dev->lirc_cdev, &lirc_fops);
733
734 err = cdev_device_add(&dev->lirc_cdev, &dev->lirc_dev);
735 if (err)
736 goto out_ida;
737
738 get_device(&dev->dev);
739
740 switch (dev->driver_type) {
741 case RC_DRIVER_SCANCODE:
742 rx_type = "scancode";
743 break;
744 case RC_DRIVER_IR_RAW:
745 rx_type = "raw IR";
746 break;
747 default:
748 rx_type = "no";
749 break;
750 }
751
752 if (dev->tx_ir)
753 tx_type = "raw IR";
754 else
755 tx_type = "no";
756
757 dev_info(&dev->dev, "lirc_dev: driver %s registered at minor = %d, %s receiver, %s transmitter",
758 dev->driver_name, minor, rx_type, tx_type);
759
760 return 0;
761
762out_ida:
763 ida_free(&lirc_ida, minor);
764 return err;
765}
766
767void lirc_unregister(struct rc_dev *dev)
768{
769 unsigned long flags;
770 struct lirc_fh *fh;
771
772 dev_dbg(&dev->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
773 dev->driver_name, MINOR(dev->lirc_dev.devt));
774
775 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
776 list_for_each_entry(fh, &dev->lirc_fh, list)
777 wake_up_poll(&fh->wait_poll, EPOLLHUP | EPOLLERR);
778 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
779
780 cdev_device_del(&dev->lirc_cdev, &dev->lirc_dev);
781 ida_free(&lirc_ida, MINOR(dev->lirc_dev.devt));
782}
783
784int __init lirc_dev_init(void)
785{
786 int retval;
787
788 lirc_class = class_create(THIS_MODULE, "lirc");
789 if (IS_ERR(lirc_class)) {
790 pr_err("class_create failed\n");
791 return PTR_ERR(lirc_class);
792 }
793
794 retval = alloc_chrdev_region(&lirc_base_dev, 0, RC_DEV_MAX, "lirc");
795 if (retval) {
796 class_destroy(lirc_class);
797 pr_err("alloc_chrdev_region failed\n");
798 return retval;
799 }
800
801 pr_debug("IR Remote Control driver registered, major %d\n",
802 MAJOR(lirc_base_dev));
803
804 return 0;
805}
806
807void __exit lirc_dev_exit(void)
808{
809 class_destroy(lirc_class);
810 unregister_chrdev_region(lirc_base_dev, RC_DEV_MAX);
811}
812
813struct rc_dev *rc_dev_get_from_fd(int fd)
814{
815 struct fd f = fdget(fd);
816 struct lirc_fh *fh;
817 struct rc_dev *dev;
818
819 if (!f.file)
820 return ERR_PTR(-EBADF);
821
822 if (f.file->f_op != &lirc_fops) {
823 fdput(f);
824 return ERR_PTR(-EINVAL);
825 }
826
827 fh = f.file->private_data;
828 dev = fh->rc;
829
830 get_device(&dev->dev);
831 fdput(f);
832
833 return dev;
834}
835
836MODULE_ALIAS("lirc_dev");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * LIRC base driver
4 *
5 * by Artur Lipowski <alipowski@interia.pl>
6 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/module.h>
11#include <linux/mutex.h>
12#include <linux/device.h>
13#include <linux/file.h>
14#include <linux/idr.h>
15#include <linux/poll.h>
16#include <linux/sched.h>
17#include <linux/wait.h>
18
19#include "rc-core-priv.h"
20#include <uapi/linux/lirc.h>
21
22#define LIRCBUF_SIZE 1024
23
24static dev_t lirc_base_dev;
25
26/* Used to keep track of allocated lirc devices */
27static DEFINE_IDA(lirc_ida);
28
29/* Only used for sysfs but defined to void otherwise */
30static struct class *lirc_class;
31
32/**
33 * lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
34 *
35 * @dev: the struct rc_dev descriptor of the device
36 * @ev: the struct ir_raw_event descriptor of the pulse/space
37 */
38void lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
39{
40 unsigned long flags;
41 struct lirc_fh *fh;
42 int sample;
43
44 /* Receiver overflow, data missing */
45 if (ev.overflow) {
46 /*
47 * Send lirc overflow message. This message is unknown to
48 * lircd, but it will interpret this as a long space as
49 * long as the value is set to high value. This resets its
50 * decoder state.
51 */
52 sample = LIRC_OVERFLOW(LIRC_VALUE_MASK);
53 dev_dbg(&dev->dev, "delivering overflow to lirc_dev\n");
54
55 /* Carrier reports */
56 } else if (ev.carrier_report) {
57 sample = LIRC_FREQUENCY(ev.carrier);
58 dev_dbg(&dev->dev, "carrier report (freq: %d)\n", sample);
59
60 /* Packet end */
61 } else if (ev.timeout) {
62 dev->gap_start = ktime_get();
63
64 sample = LIRC_TIMEOUT(ev.duration);
65 dev_dbg(&dev->dev, "timeout report (duration: %d)\n", sample);
66
67 /* Normal sample */
68 } else {
69 if (dev->gap_start) {
70 u64 duration = ktime_us_delta(ktime_get(),
71 dev->gap_start);
72
73 /* Cap by LIRC_VALUE_MASK */
74 duration = min_t(u64, duration, LIRC_VALUE_MASK);
75
76 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
77 list_for_each_entry(fh, &dev->lirc_fh, list)
78 kfifo_put(&fh->rawir, LIRC_SPACE(duration));
79 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
80 dev->gap_start = 0;
81 }
82
83 sample = ev.pulse ? LIRC_PULSE(ev.duration) :
84 LIRC_SPACE(ev.duration);
85 dev_dbg(&dev->dev, "delivering %uus %s to lirc_dev\n",
86 ev.duration, TO_STR(ev.pulse));
87 }
88
89 /*
90 * bpf does not care about the gap generated above; that exists
91 * for backwards compatibility
92 */
93 lirc_bpf_run(dev, sample);
94
95 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
96 list_for_each_entry(fh, &dev->lirc_fh, list) {
97 if (kfifo_put(&fh->rawir, sample))
98 wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
99 }
100 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
101}
102
103/**
104 * lirc_scancode_event() - Send scancode data to lirc to be relayed to
105 * userspace. This can be called in atomic context.
106 * @dev: the struct rc_dev descriptor of the device
107 * @lsc: the struct lirc_scancode describing the decoded scancode
108 */
109void lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc)
110{
111 unsigned long flags;
112 struct lirc_fh *fh;
113
114 lsc->timestamp = ktime_get_ns();
115
116 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
117 list_for_each_entry(fh, &dev->lirc_fh, list) {
118 if (kfifo_put(&fh->scancodes, *lsc))
119 wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
120 }
121 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
122}
123EXPORT_SYMBOL_GPL(lirc_scancode_event);
124
125static int lirc_open(struct inode *inode, struct file *file)
126{
127 struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev,
128 lirc_cdev);
129 struct lirc_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
130 unsigned long flags;
131 int retval;
132
133 if (!fh)
134 return -ENOMEM;
135
136 get_device(&dev->dev);
137
138 if (!dev->registered) {
139 retval = -ENODEV;
140 goto out_fh;
141 }
142
143 if (dev->driver_type == RC_DRIVER_IR_RAW) {
144 if (kfifo_alloc(&fh->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL)) {
145 retval = -ENOMEM;
146 goto out_fh;
147 }
148 }
149
150 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
151 if (kfifo_alloc(&fh->scancodes, 32, GFP_KERNEL)) {
152 retval = -ENOMEM;
153 goto out_rawir;
154 }
155 }
156
157 fh->send_mode = LIRC_MODE_PULSE;
158 fh->rc = dev;
159
160 if (dev->driver_type == RC_DRIVER_SCANCODE)
161 fh->rec_mode = LIRC_MODE_SCANCODE;
162 else
163 fh->rec_mode = LIRC_MODE_MODE2;
164
165 retval = rc_open(dev);
166 if (retval)
167 goto out_kfifo;
168
169 init_waitqueue_head(&fh->wait_poll);
170
171 file->private_data = fh;
172 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
173 list_add(&fh->list, &dev->lirc_fh);
174 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
175
176 stream_open(inode, file);
177
178 return 0;
179out_kfifo:
180 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
181 kfifo_free(&fh->scancodes);
182out_rawir:
183 if (dev->driver_type == RC_DRIVER_IR_RAW)
184 kfifo_free(&fh->rawir);
185out_fh:
186 kfree(fh);
187 put_device(&dev->dev);
188
189 return retval;
190}
191
192static int lirc_close(struct inode *inode, struct file *file)
193{
194 struct lirc_fh *fh = file->private_data;
195 struct rc_dev *dev = fh->rc;
196 unsigned long flags;
197
198 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
199 list_del(&fh->list);
200 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
201
202 if (dev->driver_type == RC_DRIVER_IR_RAW)
203 kfifo_free(&fh->rawir);
204 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
205 kfifo_free(&fh->scancodes);
206 kfree(fh);
207
208 rc_close(dev);
209 put_device(&dev->dev);
210
211 return 0;
212}
213
214static ssize_t lirc_transmit(struct file *file, const char __user *buf,
215 size_t n, loff_t *ppos)
216{
217 struct lirc_fh *fh = file->private_data;
218 struct rc_dev *dev = fh->rc;
219 unsigned int *txbuf;
220 struct ir_raw_event *raw = NULL;
221 ssize_t ret;
222 size_t count;
223 ktime_t start;
224 s64 towait;
225 unsigned int duration = 0; /* signal duration in us */
226 int i;
227
228 ret = mutex_lock_interruptible(&dev->lock);
229 if (ret)
230 return ret;
231
232 if (!dev->registered) {
233 ret = -ENODEV;
234 goto out_unlock;
235 }
236
237 if (!dev->tx_ir) {
238 ret = -EINVAL;
239 goto out_unlock;
240 }
241
242 if (fh->send_mode == LIRC_MODE_SCANCODE) {
243 struct lirc_scancode scan;
244
245 if (n != sizeof(scan)) {
246 ret = -EINVAL;
247 goto out_unlock;
248 }
249
250 if (copy_from_user(&scan, buf, sizeof(scan))) {
251 ret = -EFAULT;
252 goto out_unlock;
253 }
254
255 if (scan.flags || scan.keycode || scan.timestamp ||
256 scan.rc_proto > RC_PROTO_MAX) {
257 ret = -EINVAL;
258 goto out_unlock;
259 }
260
261 /* We only have encoders for 32-bit protocols. */
262 if (scan.scancode > U32_MAX ||
263 !rc_validate_scancode(scan.rc_proto, scan.scancode)) {
264 ret = -EINVAL;
265 goto out_unlock;
266 }
267
268 raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
269 if (!raw) {
270 ret = -ENOMEM;
271 goto out_unlock;
272 }
273
274 ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
275 raw, LIRCBUF_SIZE);
276 if (ret < 0)
277 goto out_kfree_raw;
278
279 /* drop trailing space */
280 if (!(ret % 2))
281 count = ret - 1;
282 else
283 count = ret;
284
285 txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
286 if (!txbuf) {
287 ret = -ENOMEM;
288 goto out_kfree_raw;
289 }
290
291 for (i = 0; i < count; i++)
292 txbuf[i] = raw[i].duration;
293
294 if (dev->s_tx_carrier) {
295 int carrier = ir_raw_encode_carrier(scan.rc_proto);
296
297 if (carrier > 0)
298 dev->s_tx_carrier(dev, carrier);
299 }
300 } else {
301 if (n < sizeof(unsigned int) || n % sizeof(unsigned int)) {
302 ret = -EINVAL;
303 goto out_unlock;
304 }
305
306 count = n / sizeof(unsigned int);
307 if (count > LIRCBUF_SIZE || count % 2 == 0) {
308 ret = -EINVAL;
309 goto out_unlock;
310 }
311
312 txbuf = memdup_user(buf, n);
313 if (IS_ERR(txbuf)) {
314 ret = PTR_ERR(txbuf);
315 goto out_unlock;
316 }
317 }
318
319 for (i = 0; i < count; i++) {
320 if (txbuf[i] > IR_MAX_DURATION - duration || !txbuf[i]) {
321 ret = -EINVAL;
322 goto out_kfree;
323 }
324
325 duration += txbuf[i];
326 }
327
328 start = ktime_get();
329
330 ret = dev->tx_ir(dev, txbuf, count);
331 if (ret < 0)
332 goto out_kfree;
333
334 kfree(txbuf);
335 kfree(raw);
336 mutex_unlock(&dev->lock);
337
338 /*
339 * The lircd gap calculation expects the write function to
340 * wait for the actual IR signal to be transmitted before
341 * returning.
342 */
343 towait = ktime_us_delta(ktime_add_us(start, duration),
344 ktime_get());
345 if (towait > 0) {
346 set_current_state(TASK_INTERRUPTIBLE);
347 schedule_timeout(usecs_to_jiffies(towait));
348 }
349
350 return n;
351out_kfree:
352 kfree(txbuf);
353out_kfree_raw:
354 kfree(raw);
355out_unlock:
356 mutex_unlock(&dev->lock);
357 return ret;
358}
359
360static long lirc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
361{
362 struct lirc_fh *fh = file->private_data;
363 struct rc_dev *dev = fh->rc;
364 u32 __user *argp = (u32 __user *)(arg);
365 u32 val = 0;
366 int ret;
367
368 if (_IOC_DIR(cmd) & _IOC_WRITE) {
369 ret = get_user(val, argp);
370 if (ret)
371 return ret;
372 }
373
374 ret = mutex_lock_interruptible(&dev->lock);
375 if (ret)
376 return ret;
377
378 if (!dev->registered) {
379 ret = -ENODEV;
380 goto out;
381 }
382
383 switch (cmd) {
384 case LIRC_GET_FEATURES:
385 if (dev->driver_type == RC_DRIVER_SCANCODE)
386 val |= LIRC_CAN_REC_SCANCODE;
387
388 if (dev->driver_type == RC_DRIVER_IR_RAW) {
389 val |= LIRC_CAN_REC_MODE2;
390 if (dev->rx_resolution)
391 val |= LIRC_CAN_GET_REC_RESOLUTION;
392 }
393
394 if (dev->tx_ir) {
395 val |= LIRC_CAN_SEND_PULSE;
396 if (dev->s_tx_mask)
397 val |= LIRC_CAN_SET_TRANSMITTER_MASK;
398 if (dev->s_tx_carrier)
399 val |= LIRC_CAN_SET_SEND_CARRIER;
400 if (dev->s_tx_duty_cycle)
401 val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
402 }
403
404 if (dev->s_rx_carrier_range)
405 val |= LIRC_CAN_SET_REC_CARRIER |
406 LIRC_CAN_SET_REC_CARRIER_RANGE;
407
408 if (dev->s_wideband_receiver)
409 val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
410
411 if (dev->s_carrier_report)
412 val |= LIRC_CAN_MEASURE_CARRIER;
413
414 if (dev->max_timeout)
415 val |= LIRC_CAN_SET_REC_TIMEOUT;
416
417 break;
418
419 /* mode support */
420 case LIRC_GET_REC_MODE:
421 if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
422 ret = -ENOTTY;
423 else
424 val = fh->rec_mode;
425 break;
426
427 case LIRC_SET_REC_MODE:
428 switch (dev->driver_type) {
429 case RC_DRIVER_IR_RAW_TX:
430 ret = -ENOTTY;
431 break;
432 case RC_DRIVER_SCANCODE:
433 if (val != LIRC_MODE_SCANCODE)
434 ret = -EINVAL;
435 break;
436 case RC_DRIVER_IR_RAW:
437 if (!(val == LIRC_MODE_MODE2 ||
438 val == LIRC_MODE_SCANCODE))
439 ret = -EINVAL;
440 break;
441 }
442
443 if (!ret)
444 fh->rec_mode = val;
445 break;
446
447 case LIRC_GET_SEND_MODE:
448 if (!dev->tx_ir)
449 ret = -ENOTTY;
450 else
451 val = fh->send_mode;
452 break;
453
454 case LIRC_SET_SEND_MODE:
455 if (!dev->tx_ir)
456 ret = -ENOTTY;
457 else if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
458 ret = -EINVAL;
459 else
460 fh->send_mode = val;
461 break;
462
463 /* TX settings */
464 case LIRC_SET_TRANSMITTER_MASK:
465 if (!dev->s_tx_mask)
466 ret = -ENOTTY;
467 else
468 ret = dev->s_tx_mask(dev, val);
469 break;
470
471 case LIRC_SET_SEND_CARRIER:
472 if (!dev->s_tx_carrier)
473 ret = -ENOTTY;
474 else
475 ret = dev->s_tx_carrier(dev, val);
476 break;
477
478 case LIRC_SET_SEND_DUTY_CYCLE:
479 if (!dev->s_tx_duty_cycle)
480 ret = -ENOTTY;
481 else if (val <= 0 || val >= 100)
482 ret = -EINVAL;
483 else
484 ret = dev->s_tx_duty_cycle(dev, val);
485 break;
486
487 /* RX settings */
488 case LIRC_SET_REC_CARRIER:
489 if (!dev->s_rx_carrier_range)
490 ret = -ENOTTY;
491 else if (val <= 0)
492 ret = -EINVAL;
493 else
494 ret = dev->s_rx_carrier_range(dev, fh->carrier_low,
495 val);
496 break;
497
498 case LIRC_SET_REC_CARRIER_RANGE:
499 if (!dev->s_rx_carrier_range)
500 ret = -ENOTTY;
501 else if (val <= 0)
502 ret = -EINVAL;
503 else
504 fh->carrier_low = val;
505 break;
506
507 case LIRC_GET_REC_RESOLUTION:
508 if (!dev->rx_resolution)
509 ret = -ENOTTY;
510 else
511 val = dev->rx_resolution;
512 break;
513
514 case LIRC_SET_WIDEBAND_RECEIVER:
515 if (!dev->s_wideband_receiver)
516 ret = -ENOTTY;
517 else
518 ret = dev->s_wideband_receiver(dev, !!val);
519 break;
520
521 case LIRC_SET_MEASURE_CARRIER_MODE:
522 if (!dev->s_carrier_report)
523 ret = -ENOTTY;
524 else
525 ret = dev->s_carrier_report(dev, !!val);
526 break;
527
528 /* Generic timeout support */
529 case LIRC_GET_MIN_TIMEOUT:
530 if (!dev->max_timeout)
531 ret = -ENOTTY;
532 else
533 val = dev->min_timeout;
534 break;
535
536 case LIRC_GET_MAX_TIMEOUT:
537 if (!dev->max_timeout)
538 ret = -ENOTTY;
539 else
540 val = dev->max_timeout;
541 break;
542
543 case LIRC_SET_REC_TIMEOUT:
544 if (!dev->max_timeout) {
545 ret = -ENOTTY;
546 } else {
547 if (val < dev->min_timeout || val > dev->max_timeout)
548 ret = -EINVAL;
549 else if (dev->s_timeout)
550 ret = dev->s_timeout(dev, val);
551 else
552 dev->timeout = val;
553 }
554 break;
555
556 case LIRC_GET_REC_TIMEOUT:
557 if (!dev->timeout)
558 ret = -ENOTTY;
559 else
560 val = dev->timeout;
561 break;
562
563 case LIRC_SET_REC_TIMEOUT_REPORTS:
564 if (dev->driver_type != RC_DRIVER_IR_RAW)
565 ret = -ENOTTY;
566 break;
567
568 default:
569 ret = -ENOTTY;
570 }
571
572 if (!ret && _IOC_DIR(cmd) & _IOC_READ)
573 ret = put_user(val, argp);
574
575out:
576 mutex_unlock(&dev->lock);
577 return ret;
578}
579
580static __poll_t lirc_poll(struct file *file, struct poll_table_struct *wait)
581{
582 struct lirc_fh *fh = file->private_data;
583 struct rc_dev *rcdev = fh->rc;
584 __poll_t events = 0;
585
586 poll_wait(file, &fh->wait_poll, wait);
587
588 if (!rcdev->registered) {
589 events = EPOLLHUP | EPOLLERR;
590 } else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
591 if (fh->rec_mode == LIRC_MODE_SCANCODE &&
592 !kfifo_is_empty(&fh->scancodes))
593 events = EPOLLIN | EPOLLRDNORM;
594
595 if (fh->rec_mode == LIRC_MODE_MODE2 &&
596 !kfifo_is_empty(&fh->rawir))
597 events = EPOLLIN | EPOLLRDNORM;
598 }
599
600 return events;
601}
602
603static ssize_t lirc_read_mode2(struct file *file, char __user *buffer,
604 size_t length)
605{
606 struct lirc_fh *fh = file->private_data;
607 struct rc_dev *rcdev = fh->rc;
608 unsigned int copied;
609 int ret;
610
611 if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
612 return -EINVAL;
613
614 do {
615 if (kfifo_is_empty(&fh->rawir)) {
616 if (file->f_flags & O_NONBLOCK)
617 return -EAGAIN;
618
619 ret = wait_event_interruptible(fh->wait_poll,
620 !kfifo_is_empty(&fh->rawir) ||
621 !rcdev->registered);
622 if (ret)
623 return ret;
624 }
625
626 if (!rcdev->registered)
627 return -ENODEV;
628
629 ret = mutex_lock_interruptible(&rcdev->lock);
630 if (ret)
631 return ret;
632 ret = kfifo_to_user(&fh->rawir, buffer, length, &copied);
633 mutex_unlock(&rcdev->lock);
634 if (ret)
635 return ret;
636 } while (copied == 0);
637
638 return copied;
639}
640
641static ssize_t lirc_read_scancode(struct file *file, char __user *buffer,
642 size_t length)
643{
644 struct lirc_fh *fh = file->private_data;
645 struct rc_dev *rcdev = fh->rc;
646 unsigned int copied;
647 int ret;
648
649 if (length < sizeof(struct lirc_scancode) ||
650 length % sizeof(struct lirc_scancode))
651 return -EINVAL;
652
653 do {
654 if (kfifo_is_empty(&fh->scancodes)) {
655 if (file->f_flags & O_NONBLOCK)
656 return -EAGAIN;
657
658 ret = wait_event_interruptible(fh->wait_poll,
659 !kfifo_is_empty(&fh->scancodes) ||
660 !rcdev->registered);
661 if (ret)
662 return ret;
663 }
664
665 if (!rcdev->registered)
666 return -ENODEV;
667
668 ret = mutex_lock_interruptible(&rcdev->lock);
669 if (ret)
670 return ret;
671 ret = kfifo_to_user(&fh->scancodes, buffer, length, &copied);
672 mutex_unlock(&rcdev->lock);
673 if (ret)
674 return ret;
675 } while (copied == 0);
676
677 return copied;
678}
679
680static ssize_t lirc_read(struct file *file, char __user *buffer, size_t length,
681 loff_t *ppos)
682{
683 struct lirc_fh *fh = file->private_data;
684 struct rc_dev *rcdev = fh->rc;
685
686 if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
687 return -EINVAL;
688
689 if (!rcdev->registered)
690 return -ENODEV;
691
692 if (fh->rec_mode == LIRC_MODE_MODE2)
693 return lirc_read_mode2(file, buffer, length);
694 else /* LIRC_MODE_SCANCODE */
695 return lirc_read_scancode(file, buffer, length);
696}
697
698static const struct file_operations lirc_fops = {
699 .owner = THIS_MODULE,
700 .write = lirc_transmit,
701 .unlocked_ioctl = lirc_ioctl,
702 .compat_ioctl = compat_ptr_ioctl,
703 .read = lirc_read,
704 .poll = lirc_poll,
705 .open = lirc_open,
706 .release = lirc_close,
707 .llseek = no_llseek,
708};
709
710static void lirc_release_device(struct device *ld)
711{
712 struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);
713
714 put_device(&rcdev->dev);
715}
716
717int lirc_register(struct rc_dev *dev)
718{
719 const char *rx_type, *tx_type;
720 int err, minor;
721
722 minor = ida_alloc_max(&lirc_ida, RC_DEV_MAX - 1, GFP_KERNEL);
723 if (minor < 0)
724 return minor;
725
726 device_initialize(&dev->lirc_dev);
727 dev->lirc_dev.class = lirc_class;
728 dev->lirc_dev.parent = &dev->dev;
729 dev->lirc_dev.release = lirc_release_device;
730 dev->lirc_dev.devt = MKDEV(MAJOR(lirc_base_dev), minor);
731 dev_set_name(&dev->lirc_dev, "lirc%d", minor);
732
733 INIT_LIST_HEAD(&dev->lirc_fh);
734 spin_lock_init(&dev->lirc_fh_lock);
735
736 cdev_init(&dev->lirc_cdev, &lirc_fops);
737
738 err = cdev_device_add(&dev->lirc_cdev, &dev->lirc_dev);
739 if (err)
740 goto out_ida;
741
742 get_device(&dev->dev);
743
744 switch (dev->driver_type) {
745 case RC_DRIVER_SCANCODE:
746 rx_type = "scancode";
747 break;
748 case RC_DRIVER_IR_RAW:
749 rx_type = "raw IR";
750 break;
751 default:
752 rx_type = "no";
753 break;
754 }
755
756 if (dev->tx_ir)
757 tx_type = "raw IR";
758 else
759 tx_type = "no";
760
761 dev_info(&dev->dev, "lirc_dev: driver %s registered at minor = %d, %s receiver, %s transmitter",
762 dev->driver_name, minor, rx_type, tx_type);
763
764 return 0;
765
766out_ida:
767 ida_free(&lirc_ida, minor);
768 return err;
769}
770
771void lirc_unregister(struct rc_dev *dev)
772{
773 unsigned long flags;
774 struct lirc_fh *fh;
775
776 dev_dbg(&dev->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
777 dev->driver_name, MINOR(dev->lirc_dev.devt));
778
779 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
780 list_for_each_entry(fh, &dev->lirc_fh, list)
781 wake_up_poll(&fh->wait_poll, EPOLLHUP | EPOLLERR);
782 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
783
784 cdev_device_del(&dev->lirc_cdev, &dev->lirc_dev);
785 ida_free(&lirc_ida, MINOR(dev->lirc_dev.devt));
786}
787
788int __init lirc_dev_init(void)
789{
790 int retval;
791
792 lirc_class = class_create("lirc");
793 if (IS_ERR(lirc_class)) {
794 pr_err("class_create failed\n");
795 return PTR_ERR(lirc_class);
796 }
797
798 retval = alloc_chrdev_region(&lirc_base_dev, 0, RC_DEV_MAX, "lirc");
799 if (retval) {
800 class_destroy(lirc_class);
801 pr_err("alloc_chrdev_region failed\n");
802 return retval;
803 }
804
805 pr_debug("IR Remote Control driver registered, major %d\n",
806 MAJOR(lirc_base_dev));
807
808 return 0;
809}
810
811void __exit lirc_dev_exit(void)
812{
813 class_destroy(lirc_class);
814 unregister_chrdev_region(lirc_base_dev, RC_DEV_MAX);
815}
816
817struct rc_dev *rc_dev_get_from_fd(int fd, bool write)
818{
819 struct fd f = fdget(fd);
820 struct lirc_fh *fh;
821 struct rc_dev *dev;
822
823 if (!f.file)
824 return ERR_PTR(-EBADF);
825
826 if (f.file->f_op != &lirc_fops) {
827 fdput(f);
828 return ERR_PTR(-EINVAL);
829 }
830
831 if (write && !(f.file->f_mode & FMODE_WRITE))
832 return ERR_PTR(-EPERM);
833
834 fh = f.file->private_data;
835 dev = fh->rc;
836
837 get_device(&dev->dev);
838 fdput(f);
839
840 return dev;
841}
842
843MODULE_ALIAS("lirc_dev");