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