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