xref: /openbmc/linux/drivers/media/rc/lirc_dev.c (revision 0ad53fe3)
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 		    scan.rc_proto > RC_PROTO_MAX) {
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 			txbuf[i] = raw[i].duration;
300 
301 		if (dev->s_tx_carrier) {
302 			int carrier = ir_raw_encode_carrier(scan.rc_proto);
303 
304 			if (carrier > 0)
305 				dev->s_tx_carrier(dev, carrier);
306 		}
307 	} else {
308 		if (n < sizeof(unsigned int) || n % sizeof(unsigned int)) {
309 			ret = -EINVAL;
310 			goto out_unlock;
311 		}
312 
313 		count = n / sizeof(unsigned int);
314 		if (count > LIRCBUF_SIZE || count % 2 == 0) {
315 			ret = -EINVAL;
316 			goto out_unlock;
317 		}
318 
319 		txbuf = memdup_user(buf, n);
320 		if (IS_ERR(txbuf)) {
321 			ret = PTR_ERR(txbuf);
322 			goto out_unlock;
323 		}
324 	}
325 
326 	for (i = 0; i < count; i++) {
327 		if (txbuf[i] > IR_MAX_DURATION - duration || !txbuf[i]) {
328 			ret = -EINVAL;
329 			goto out_kfree;
330 		}
331 
332 		duration += txbuf[i];
333 	}
334 
335 	start = ktime_get();
336 
337 	ret = dev->tx_ir(dev, txbuf, count);
338 	if (ret < 0)
339 		goto out_kfree;
340 
341 	kfree(txbuf);
342 	kfree(raw);
343 	mutex_unlock(&dev->lock);
344 
345 	/*
346 	 * The lircd gap calculation expects the write function to
347 	 * wait for the actual IR signal to be transmitted before
348 	 * returning.
349 	 */
350 	towait = ktime_us_delta(ktime_add_us(start, duration),
351 				ktime_get());
352 	if (towait > 0) {
353 		set_current_state(TASK_INTERRUPTIBLE);
354 		schedule_timeout(usecs_to_jiffies(towait));
355 	}
356 
357 	return n;
358 out_kfree:
359 	kfree(txbuf);
360 out_kfree_raw:
361 	kfree(raw);
362 out_unlock:
363 	mutex_unlock(&dev->lock);
364 	return ret;
365 }
366 
367 static long lirc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
368 {
369 	struct lirc_fh *fh = file->private_data;
370 	struct rc_dev *dev = fh->rc;
371 	u32 __user *argp = (u32 __user *)(arg);
372 	u32 val = 0;
373 	int ret;
374 
375 	if (_IOC_DIR(cmd) & _IOC_WRITE) {
376 		ret = get_user(val, argp);
377 		if (ret)
378 			return ret;
379 	}
380 
381 	ret = mutex_lock_interruptible(&dev->lock);
382 	if (ret)
383 		return ret;
384 
385 	if (!dev->registered) {
386 		ret = -ENODEV;
387 		goto out;
388 	}
389 
390 	switch (cmd) {
391 	case LIRC_GET_FEATURES:
392 		if (dev->driver_type == RC_DRIVER_SCANCODE)
393 			val |= LIRC_CAN_REC_SCANCODE;
394 
395 		if (dev->driver_type == RC_DRIVER_IR_RAW) {
396 			val |= LIRC_CAN_REC_MODE2;
397 			if (dev->rx_resolution)
398 				val |= LIRC_CAN_GET_REC_RESOLUTION;
399 		}
400 
401 		if (dev->tx_ir) {
402 			val |= LIRC_CAN_SEND_PULSE;
403 			if (dev->s_tx_mask)
404 				val |= LIRC_CAN_SET_TRANSMITTER_MASK;
405 			if (dev->s_tx_carrier)
406 				val |= LIRC_CAN_SET_SEND_CARRIER;
407 			if (dev->s_tx_duty_cycle)
408 				val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
409 		}
410 
411 		if (dev->s_rx_carrier_range)
412 			val |= LIRC_CAN_SET_REC_CARRIER |
413 				LIRC_CAN_SET_REC_CARRIER_RANGE;
414 
415 		if (dev->s_wideband_receiver)
416 			val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
417 
418 		if (dev->s_carrier_report)
419 			val |= LIRC_CAN_MEASURE_CARRIER;
420 
421 		if (dev->max_timeout)
422 			val |= LIRC_CAN_SET_REC_TIMEOUT;
423 
424 		break;
425 
426 	/* mode support */
427 	case LIRC_GET_REC_MODE:
428 		if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
429 			ret = -ENOTTY;
430 		else
431 			val = fh->rec_mode;
432 		break;
433 
434 	case LIRC_SET_REC_MODE:
435 		switch (dev->driver_type) {
436 		case RC_DRIVER_IR_RAW_TX:
437 			ret = -ENOTTY;
438 			break;
439 		case RC_DRIVER_SCANCODE:
440 			if (val != LIRC_MODE_SCANCODE)
441 				ret = -EINVAL;
442 			break;
443 		case RC_DRIVER_IR_RAW:
444 			if (!(val == LIRC_MODE_MODE2 ||
445 			      val == LIRC_MODE_SCANCODE))
446 				ret = -EINVAL;
447 			break;
448 		}
449 
450 		if (!ret)
451 			fh->rec_mode = val;
452 		break;
453 
454 	case LIRC_GET_SEND_MODE:
455 		if (!dev->tx_ir)
456 			ret = -ENOTTY;
457 		else
458 			val = fh->send_mode;
459 		break;
460 
461 	case LIRC_SET_SEND_MODE:
462 		if (!dev->tx_ir)
463 			ret = -ENOTTY;
464 		else if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
465 			ret = -EINVAL;
466 		else
467 			fh->send_mode = val;
468 		break;
469 
470 	/* TX settings */
471 	case LIRC_SET_TRANSMITTER_MASK:
472 		if (!dev->s_tx_mask)
473 			ret = -ENOTTY;
474 		else
475 			ret = dev->s_tx_mask(dev, val);
476 		break;
477 
478 	case LIRC_SET_SEND_CARRIER:
479 		if (!dev->s_tx_carrier)
480 			ret = -ENOTTY;
481 		else
482 			ret = dev->s_tx_carrier(dev, val);
483 		break;
484 
485 	case LIRC_SET_SEND_DUTY_CYCLE:
486 		if (!dev->s_tx_duty_cycle)
487 			ret = -ENOTTY;
488 		else if (val <= 0 || val >= 100)
489 			ret = -EINVAL;
490 		else
491 			ret = dev->s_tx_duty_cycle(dev, val);
492 		break;
493 
494 	/* RX settings */
495 	case LIRC_SET_REC_CARRIER:
496 		if (!dev->s_rx_carrier_range)
497 			ret = -ENOTTY;
498 		else if (val <= 0)
499 			ret = -EINVAL;
500 		else
501 			ret = dev->s_rx_carrier_range(dev, fh->carrier_low,
502 						      val);
503 		break;
504 
505 	case LIRC_SET_REC_CARRIER_RANGE:
506 		if (!dev->s_rx_carrier_range)
507 			ret = -ENOTTY;
508 		else if (val <= 0)
509 			ret = -EINVAL;
510 		else
511 			fh->carrier_low = val;
512 		break;
513 
514 	case LIRC_GET_REC_RESOLUTION:
515 		if (!dev->rx_resolution)
516 			ret = -ENOTTY;
517 		else
518 			val = dev->rx_resolution;
519 		break;
520 
521 	case LIRC_SET_WIDEBAND_RECEIVER:
522 		if (!dev->s_wideband_receiver)
523 			ret = -ENOTTY;
524 		else
525 			ret = dev->s_wideband_receiver(dev, !!val);
526 		break;
527 
528 	case LIRC_SET_MEASURE_CARRIER_MODE:
529 		if (!dev->s_carrier_report)
530 			ret = -ENOTTY;
531 		else
532 			ret = dev->s_carrier_report(dev, !!val);
533 		break;
534 
535 	/* Generic timeout support */
536 	case LIRC_GET_MIN_TIMEOUT:
537 		if (!dev->max_timeout)
538 			ret = -ENOTTY;
539 		else
540 			val = dev->min_timeout;
541 		break;
542 
543 	case LIRC_GET_MAX_TIMEOUT:
544 		if (!dev->max_timeout)
545 			ret = -ENOTTY;
546 		else
547 			val = dev->max_timeout;
548 		break;
549 
550 	case LIRC_SET_REC_TIMEOUT:
551 		if (!dev->max_timeout) {
552 			ret = -ENOTTY;
553 		} else {
554 			if (val < dev->min_timeout || val > dev->max_timeout)
555 				ret = -EINVAL;
556 			else if (dev->s_timeout)
557 				ret = dev->s_timeout(dev, val);
558 			else
559 				dev->timeout = val;
560 		}
561 		break;
562 
563 	case LIRC_GET_REC_TIMEOUT:
564 		if (!dev->timeout)
565 			ret = -ENOTTY;
566 		else
567 			val = dev->timeout;
568 		break;
569 
570 	case LIRC_SET_REC_TIMEOUT_REPORTS:
571 		if (dev->driver_type != RC_DRIVER_IR_RAW)
572 			ret = -ENOTTY;
573 		else
574 			fh->send_timeout_reports = !!val;
575 		break;
576 
577 	default:
578 		ret = -ENOTTY;
579 	}
580 
581 	if (!ret && _IOC_DIR(cmd) & _IOC_READ)
582 		ret = put_user(val, argp);
583 
584 out:
585 	mutex_unlock(&dev->lock);
586 	return ret;
587 }
588 
589 static __poll_t lirc_poll(struct file *file, struct poll_table_struct *wait)
590 {
591 	struct lirc_fh *fh = file->private_data;
592 	struct rc_dev *rcdev = fh->rc;
593 	__poll_t events = 0;
594 
595 	poll_wait(file, &fh->wait_poll, wait);
596 
597 	if (!rcdev->registered) {
598 		events = EPOLLHUP | EPOLLERR;
599 	} else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
600 		if (fh->rec_mode == LIRC_MODE_SCANCODE &&
601 		    !kfifo_is_empty(&fh->scancodes))
602 			events = EPOLLIN | EPOLLRDNORM;
603 
604 		if (fh->rec_mode == LIRC_MODE_MODE2 &&
605 		    !kfifo_is_empty(&fh->rawir))
606 			events = EPOLLIN | EPOLLRDNORM;
607 	}
608 
609 	return events;
610 }
611 
612 static ssize_t lirc_read_mode2(struct file *file, char __user *buffer,
613 			       size_t length)
614 {
615 	struct lirc_fh *fh = file->private_data;
616 	struct rc_dev *rcdev = fh->rc;
617 	unsigned int copied;
618 	int ret;
619 
620 	if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
621 		return -EINVAL;
622 
623 	do {
624 		if (kfifo_is_empty(&fh->rawir)) {
625 			if (file->f_flags & O_NONBLOCK)
626 				return -EAGAIN;
627 
628 			ret = wait_event_interruptible(fh->wait_poll,
629 					!kfifo_is_empty(&fh->rawir) ||
630 					!rcdev->registered);
631 			if (ret)
632 				return ret;
633 		}
634 
635 		if (!rcdev->registered)
636 			return -ENODEV;
637 
638 		ret = mutex_lock_interruptible(&rcdev->lock);
639 		if (ret)
640 			return ret;
641 		ret = kfifo_to_user(&fh->rawir, buffer, length, &copied);
642 		mutex_unlock(&rcdev->lock);
643 		if (ret)
644 			return ret;
645 	} while (copied == 0);
646 
647 	return copied;
648 }
649 
650 static ssize_t lirc_read_scancode(struct file *file, char __user *buffer,
651 				  size_t length)
652 {
653 	struct lirc_fh *fh = file->private_data;
654 	struct rc_dev *rcdev = fh->rc;
655 	unsigned int copied;
656 	int ret;
657 
658 	if (length < sizeof(struct lirc_scancode) ||
659 	    length % sizeof(struct lirc_scancode))
660 		return -EINVAL;
661 
662 	do {
663 		if (kfifo_is_empty(&fh->scancodes)) {
664 			if (file->f_flags & O_NONBLOCK)
665 				return -EAGAIN;
666 
667 			ret = wait_event_interruptible(fh->wait_poll,
668 					!kfifo_is_empty(&fh->scancodes) ||
669 					!rcdev->registered);
670 			if (ret)
671 				return ret;
672 		}
673 
674 		if (!rcdev->registered)
675 			return -ENODEV;
676 
677 		ret = mutex_lock_interruptible(&rcdev->lock);
678 		if (ret)
679 			return ret;
680 		ret = kfifo_to_user(&fh->scancodes, buffer, length, &copied);
681 		mutex_unlock(&rcdev->lock);
682 		if (ret)
683 			return ret;
684 	} while (copied == 0);
685 
686 	return copied;
687 }
688 
689 static ssize_t lirc_read(struct file *file, char __user *buffer, size_t length,
690 			 loff_t *ppos)
691 {
692 	struct lirc_fh *fh = file->private_data;
693 	struct rc_dev *rcdev = fh->rc;
694 
695 	if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
696 		return -EINVAL;
697 
698 	if (!rcdev->registered)
699 		return -ENODEV;
700 
701 	if (fh->rec_mode == LIRC_MODE_MODE2)
702 		return lirc_read_mode2(file, buffer, length);
703 	else /* LIRC_MODE_SCANCODE */
704 		return lirc_read_scancode(file, buffer, length);
705 }
706 
707 static const struct file_operations lirc_fops = {
708 	.owner		= THIS_MODULE,
709 	.write		= lirc_transmit,
710 	.unlocked_ioctl	= lirc_ioctl,
711 	.compat_ioctl	= compat_ptr_ioctl,
712 	.read		= lirc_read,
713 	.poll		= lirc_poll,
714 	.open		= lirc_open,
715 	.release	= lirc_close,
716 	.llseek		= no_llseek,
717 };
718 
719 static void lirc_release_device(struct device *ld)
720 {
721 	struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);
722 
723 	put_device(&rcdev->dev);
724 }
725 
726 int lirc_register(struct rc_dev *dev)
727 {
728 	const char *rx_type, *tx_type;
729 	int err, minor;
730 
731 	minor = ida_simple_get(&lirc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
732 	if (minor < 0)
733 		return minor;
734 
735 	device_initialize(&dev->lirc_dev);
736 	dev->lirc_dev.class = lirc_class;
737 	dev->lirc_dev.parent = &dev->dev;
738 	dev->lirc_dev.release = lirc_release_device;
739 	dev->lirc_dev.devt = MKDEV(MAJOR(lirc_base_dev), minor);
740 	dev_set_name(&dev->lirc_dev, "lirc%d", minor);
741 
742 	INIT_LIST_HEAD(&dev->lirc_fh);
743 	spin_lock_init(&dev->lirc_fh_lock);
744 
745 	cdev_init(&dev->lirc_cdev, &lirc_fops);
746 
747 	err = cdev_device_add(&dev->lirc_cdev, &dev->lirc_dev);
748 	if (err)
749 		goto out_ida;
750 
751 	get_device(&dev->dev);
752 
753 	switch (dev->driver_type) {
754 	case RC_DRIVER_SCANCODE:
755 		rx_type = "scancode";
756 		break;
757 	case RC_DRIVER_IR_RAW:
758 		rx_type = "raw IR";
759 		break;
760 	default:
761 		rx_type = "no";
762 		break;
763 	}
764 
765 	if (dev->tx_ir)
766 		tx_type = "raw IR";
767 	else
768 		tx_type = "no";
769 
770 	dev_info(&dev->dev, "lirc_dev: driver %s registered at minor = %d, %s receiver, %s transmitter",
771 		 dev->driver_name, minor, rx_type, tx_type);
772 
773 	return 0;
774 
775 out_ida:
776 	ida_simple_remove(&lirc_ida, minor);
777 	return err;
778 }
779 
780 void lirc_unregister(struct rc_dev *dev)
781 {
782 	unsigned long flags;
783 	struct lirc_fh *fh;
784 
785 	dev_dbg(&dev->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
786 		dev->driver_name, MINOR(dev->lirc_dev.devt));
787 
788 	spin_lock_irqsave(&dev->lirc_fh_lock, flags);
789 	list_for_each_entry(fh, &dev->lirc_fh, list)
790 		wake_up_poll(&fh->wait_poll, EPOLLHUP | EPOLLERR);
791 	spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
792 
793 	cdev_device_del(&dev->lirc_cdev, &dev->lirc_dev);
794 	ida_simple_remove(&lirc_ida, MINOR(dev->lirc_dev.devt));
795 }
796 
797 int __init lirc_dev_init(void)
798 {
799 	int retval;
800 
801 	lirc_class = class_create(THIS_MODULE, "lirc");
802 	if (IS_ERR(lirc_class)) {
803 		pr_err("class_create failed\n");
804 		return PTR_ERR(lirc_class);
805 	}
806 
807 	retval = alloc_chrdev_region(&lirc_base_dev, 0, RC_DEV_MAX, "lirc");
808 	if (retval) {
809 		class_destroy(lirc_class);
810 		pr_err("alloc_chrdev_region failed\n");
811 		return retval;
812 	}
813 
814 	pr_debug("IR Remote Control driver registered, major %d\n",
815 		 MAJOR(lirc_base_dev));
816 
817 	return 0;
818 }
819 
820 void __exit lirc_dev_exit(void)
821 {
822 	class_destroy(lirc_class);
823 	unregister_chrdev_region(lirc_base_dev, RC_DEV_MAX);
824 }
825 
826 struct rc_dev *rc_dev_get_from_fd(int fd)
827 {
828 	struct fd f = fdget(fd);
829 	struct lirc_fh *fh;
830 	struct rc_dev *dev;
831 
832 	if (!f.file)
833 		return ERR_PTR(-EBADF);
834 
835 	if (f.file->f_op != &lirc_fops) {
836 		fdput(f);
837 		return ERR_PTR(-EINVAL);
838 	}
839 
840 	fh = f.file->private_data;
841 	dev = fh->rc;
842 
843 	get_device(&dev->dev);
844 	fdput(f);
845 
846 	return dev;
847 }
848 
849 MODULE_ALIAS("lirc_dev");
850