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