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