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