xref: /openbmc/linux/drivers/media/rc/lirc_dev.c (revision 8ee90c5c)
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/sched/signal.h>
22 #include <linux/ioctl.h>
23 #include <linux/poll.h>
24 #include <linux/mutex.h>
25 #include <linux/device.h>
26 #include <linux/cdev.h>
27 
28 #include <media/rc-core.h>
29 #include <media/lirc.h>
30 #include <media/lirc_dev.h>
31 
32 #define NOPLUG		-1
33 #define LOGHEAD		"lirc_dev (%s[%d]): "
34 
35 static dev_t lirc_base_dev;
36 
37 struct irctl {
38 	struct lirc_driver d;
39 	int attached;
40 	int open;
41 
42 	struct mutex irctl_lock;
43 	struct lirc_buffer *buf;
44 	bool buf_internal;
45 	unsigned int chunk_size;
46 
47 	struct device dev;
48 	struct cdev cdev;
49 };
50 
51 static DEFINE_MUTEX(lirc_dev_lock);
52 
53 static struct irctl *irctls[MAX_IRCTL_DEVICES];
54 
55 /* Only used for sysfs but defined to void otherwise */
56 static struct class *lirc_class;
57 
58 static void lirc_release(struct device *ld)
59 {
60 	struct irctl *ir = container_of(ld, struct irctl, dev);
61 
62 	put_device(ir->dev.parent);
63 
64 	if (ir->buf_internal) {
65 		lirc_buffer_free(ir->buf);
66 		kfree(ir->buf);
67 	}
68 
69 	mutex_lock(&lirc_dev_lock);
70 	irctls[ir->d.minor] = NULL;
71 	mutex_unlock(&lirc_dev_lock);
72 	kfree(ir);
73 }
74 
75 static int lirc_allocate_buffer(struct irctl *ir)
76 {
77 	int err = 0;
78 	int bytes_in_key;
79 	unsigned int chunk_size;
80 	unsigned int buffer_size;
81 	struct lirc_driver *d = &ir->d;
82 
83 	bytes_in_key = BITS_TO_LONGS(d->code_length) +
84 						(d->code_length % 8 ? 1 : 0);
85 	buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
86 	chunk_size  = d->chunk_size  ? d->chunk_size  : bytes_in_key;
87 
88 	if (d->rbuf) {
89 		ir->buf = d->rbuf;
90 		ir->buf_internal = false;
91 	} else {
92 		ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
93 		if (!ir->buf) {
94 			err = -ENOMEM;
95 			goto out;
96 		}
97 
98 		err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
99 		if (err) {
100 			kfree(ir->buf);
101 			ir->buf = NULL;
102 			goto out;
103 		}
104 
105 		ir->buf_internal = true;
106 		d->rbuf = ir->buf;
107 	}
108 	ir->chunk_size = ir->buf->chunk_size;
109 
110 out:
111 	return err;
112 }
113 
114 int lirc_register_driver(struct lirc_driver *d)
115 {
116 	struct irctl *ir;
117 	int minor;
118 	int err;
119 
120 	if (!d) {
121 		pr_err("driver pointer must be not NULL!\n");
122 		return -EBADRQC;
123 	}
124 
125 	if (!d->dev) {
126 		pr_err("dev pointer not filled in!\n");
127 		return -EINVAL;
128 	}
129 
130 	if (!d->fops) {
131 		pr_err("fops pointer not filled in!\n");
132 		return -EINVAL;
133 	}
134 
135 	if (d->minor >= MAX_IRCTL_DEVICES) {
136 		dev_err(d->dev, "minor must be between 0 and %d!\n",
137 						MAX_IRCTL_DEVICES - 1);
138 		return -EBADRQC;
139 	}
140 
141 	if (d->code_length < 1 || d->code_length > (BUFLEN * 8)) {
142 		dev_err(d->dev, "code length must be less than %d bits\n",
143 								BUFLEN * 8);
144 		return -EBADRQC;
145 	}
146 
147 	if (!d->rbuf && !(d->fops && d->fops->read &&
148 			  d->fops->poll && d->fops->unlocked_ioctl)) {
149 		dev_err(d->dev, "undefined read, poll, ioctl\n");
150 		return -EBADRQC;
151 	}
152 
153 	mutex_lock(&lirc_dev_lock);
154 
155 	minor = d->minor;
156 
157 	if (minor < 0) {
158 		/* find first free slot for driver */
159 		for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
160 			if (!irctls[minor])
161 				break;
162 		if (minor == MAX_IRCTL_DEVICES) {
163 			dev_err(d->dev, "no free slots for drivers!\n");
164 			err = -ENOMEM;
165 			goto out_lock;
166 		}
167 	} else if (irctls[minor]) {
168 		dev_err(d->dev, "minor (%d) just registered!\n", minor);
169 		err = -EBUSY;
170 		goto out_lock;
171 	}
172 
173 	ir = kzalloc(sizeof(struct irctl), GFP_KERNEL);
174 	if (!ir) {
175 		err = -ENOMEM;
176 		goto out_lock;
177 	}
178 
179 	mutex_init(&ir->irctl_lock);
180 	irctls[minor] = ir;
181 	d->minor = minor;
182 
183 	/* some safety check 8-) */
184 	d->name[sizeof(d->name)-1] = '\0';
185 
186 	if (d->features == 0)
187 		d->features = LIRC_CAN_REC_LIRCCODE;
188 
189 	ir->d = *d;
190 
191 	if (LIRC_CAN_REC(d->features)) {
192 		err = lirc_allocate_buffer(irctls[minor]);
193 		if (err) {
194 			kfree(ir);
195 			goto out_lock;
196 		}
197 		d->rbuf = ir->buf;
198 	}
199 
200 	device_initialize(&ir->dev);
201 	ir->dev.devt = MKDEV(MAJOR(lirc_base_dev), ir->d.minor);
202 	ir->dev.class = lirc_class;
203 	ir->dev.parent = d->dev;
204 	ir->dev.release = lirc_release;
205 	dev_set_name(&ir->dev, "lirc%d", ir->d.minor);
206 
207 	cdev_init(&ir->cdev, d->fops);
208 	ir->cdev.owner = ir->d.owner;
209 	ir->cdev.kobj.parent = &ir->dev.kobj;
210 
211 	err = cdev_add(&ir->cdev, ir->dev.devt, 1);
212 	if (err)
213 		goto out_free_dev;
214 
215 	ir->attached = 1;
216 
217 	err = device_add(&ir->dev);
218 	if (err)
219 		goto out_cdev;
220 
221 	mutex_unlock(&lirc_dev_lock);
222 
223 	get_device(ir->dev.parent);
224 
225 	dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n",
226 		 ir->d.name, ir->d.minor);
227 
228 	return minor;
229 
230 out_cdev:
231 	cdev_del(&ir->cdev);
232 out_free_dev:
233 	put_device(&ir->dev);
234 out_lock:
235 	mutex_unlock(&lirc_dev_lock);
236 
237 	return err;
238 }
239 EXPORT_SYMBOL(lirc_register_driver);
240 
241 int lirc_unregister_driver(int minor)
242 {
243 	struct irctl *ir;
244 
245 	if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
246 		pr_err("minor (%d) must be between 0 and %d!\n",
247 					minor, MAX_IRCTL_DEVICES - 1);
248 		return -EBADRQC;
249 	}
250 
251 	ir = irctls[minor];
252 	if (!ir) {
253 		pr_err("failed to get irctl\n");
254 		return -ENOENT;
255 	}
256 
257 	mutex_lock(&lirc_dev_lock);
258 
259 	if (ir->d.minor != minor) {
260 		dev_err(ir->d.dev, "lirc_dev: minor %d device not registered\n",
261 									minor);
262 		mutex_unlock(&lirc_dev_lock);
263 		return -ENOENT;
264 	}
265 
266 	dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n",
267 		ir->d.name, ir->d.minor);
268 
269 	ir->attached = 0;
270 	if (ir->open) {
271 		dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n",
272 			ir->d.name, ir->d.minor);
273 		wake_up_interruptible(&ir->buf->wait_poll);
274 	}
275 
276 	mutex_unlock(&lirc_dev_lock);
277 
278 	device_del(&ir->dev);
279 	cdev_del(&ir->cdev);
280 	put_device(&ir->dev);
281 
282 	return 0;
283 }
284 EXPORT_SYMBOL(lirc_unregister_driver);
285 
286 int lirc_dev_fop_open(struct inode *inode, struct file *file)
287 {
288 	struct irctl *ir;
289 	int retval = 0;
290 
291 	if (iminor(inode) >= MAX_IRCTL_DEVICES) {
292 		pr_err("open result for %d is -ENODEV\n", iminor(inode));
293 		return -ENODEV;
294 	}
295 
296 	if (mutex_lock_interruptible(&lirc_dev_lock))
297 		return -ERESTARTSYS;
298 
299 	ir = irctls[iminor(inode)];
300 	mutex_unlock(&lirc_dev_lock);
301 
302 	if (!ir) {
303 		retval = -ENODEV;
304 		goto error;
305 	}
306 
307 	dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
308 
309 	if (ir->d.minor == NOPLUG) {
310 		retval = -ENODEV;
311 		goto error;
312 	}
313 
314 	if (ir->open) {
315 		retval = -EBUSY;
316 		goto error;
317 	}
318 
319 	if (ir->d.rdev) {
320 		retval = rc_open(ir->d.rdev);
321 		if (retval)
322 			goto error;
323 	}
324 
325 	if (ir->buf)
326 		lirc_buffer_clear(ir->buf);
327 
328 	ir->open++;
329 
330 error:
331 	nonseekable_open(inode, file);
332 
333 	return retval;
334 }
335 EXPORT_SYMBOL(lirc_dev_fop_open);
336 
337 int lirc_dev_fop_close(struct inode *inode, struct file *file)
338 {
339 	struct irctl *ir = irctls[iminor(inode)];
340 	int ret;
341 
342 	if (!ir) {
343 		pr_err("called with invalid irctl\n");
344 		return -EINVAL;
345 	}
346 
347 	ret = mutex_lock_killable(&lirc_dev_lock);
348 	WARN_ON(ret);
349 
350 	rc_close(ir->d.rdev);
351 
352 	ir->open--;
353 	if (!ret)
354 		mutex_unlock(&lirc_dev_lock);
355 
356 	return 0;
357 }
358 EXPORT_SYMBOL(lirc_dev_fop_close);
359 
360 unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
361 {
362 	struct irctl *ir = irctls[iminor(file_inode(file))];
363 	unsigned int ret;
364 
365 	if (!ir) {
366 		pr_err("called with invalid irctl\n");
367 		return POLLERR;
368 	}
369 
370 	if (!ir->attached)
371 		return POLLHUP | POLLERR;
372 
373 	if (ir->buf) {
374 		poll_wait(file, &ir->buf->wait_poll, wait);
375 
376 		if (lirc_buffer_empty(ir->buf))
377 			ret = 0;
378 		else
379 			ret = POLLIN | POLLRDNORM;
380 	} else
381 		ret = POLLERR;
382 
383 	dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
384 		ir->d.name, ir->d.minor, ret);
385 
386 	return ret;
387 }
388 EXPORT_SYMBOL(lirc_dev_fop_poll);
389 
390 long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
391 {
392 	__u32 mode;
393 	int result = 0;
394 	struct irctl *ir = irctls[iminor(file_inode(file))];
395 
396 	if (!ir) {
397 		pr_err("no irctl found!\n");
398 		return -ENODEV;
399 	}
400 
401 	dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
402 		ir->d.name, ir->d.minor, cmd);
403 
404 	if (ir->d.minor == NOPLUG || !ir->attached) {
405 		dev_err(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
406 			ir->d.name, ir->d.minor);
407 		return -ENODEV;
408 	}
409 
410 	mutex_lock(&ir->irctl_lock);
411 
412 	switch (cmd) {
413 	case LIRC_GET_FEATURES:
414 		result = put_user(ir->d.features, (__u32 __user *)arg);
415 		break;
416 	case LIRC_GET_REC_MODE:
417 		if (!LIRC_CAN_REC(ir->d.features)) {
418 			result = -ENOTTY;
419 			break;
420 		}
421 
422 		result = put_user(LIRC_REC2MODE
423 				  (ir->d.features & LIRC_CAN_REC_MASK),
424 				  (__u32 __user *)arg);
425 		break;
426 	case LIRC_SET_REC_MODE:
427 		if (!LIRC_CAN_REC(ir->d.features)) {
428 			result = -ENOTTY;
429 			break;
430 		}
431 
432 		result = get_user(mode, (__u32 __user *)arg);
433 		if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
434 			result = -EINVAL;
435 		/*
436 		 * FIXME: We should actually set the mode somehow but
437 		 * for now, lirc_serial doesn't support mode changing either
438 		 */
439 		break;
440 	case LIRC_GET_LENGTH:
441 		result = put_user(ir->d.code_length, (__u32 __user *)arg);
442 		break;
443 	case LIRC_GET_MIN_TIMEOUT:
444 		if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
445 		    ir->d.min_timeout == 0) {
446 			result = -ENOTTY;
447 			break;
448 		}
449 
450 		result = put_user(ir->d.min_timeout, (__u32 __user *)arg);
451 		break;
452 	case LIRC_GET_MAX_TIMEOUT:
453 		if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
454 		    ir->d.max_timeout == 0) {
455 			result = -ENOTTY;
456 			break;
457 		}
458 
459 		result = put_user(ir->d.max_timeout, (__u32 __user *)arg);
460 		break;
461 	default:
462 		result = -ENOTTY;
463 	}
464 
465 	mutex_unlock(&ir->irctl_lock);
466 
467 	return result;
468 }
469 EXPORT_SYMBOL(lirc_dev_fop_ioctl);
470 
471 ssize_t lirc_dev_fop_read(struct file *file,
472 			  char __user *buffer,
473 			  size_t length,
474 			  loff_t *ppos)
475 {
476 	struct irctl *ir = irctls[iminor(file_inode(file))];
477 	unsigned char *buf;
478 	int ret = 0, written = 0;
479 	DECLARE_WAITQUEUE(wait, current);
480 
481 	if (!ir) {
482 		pr_err("called with invalid irctl\n");
483 		return -ENODEV;
484 	}
485 
486 	if (!LIRC_CAN_REC(ir->d.features))
487 		return -EINVAL;
488 
489 	dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
490 
491 	buf = kzalloc(ir->chunk_size, GFP_KERNEL);
492 	if (!buf)
493 		return -ENOMEM;
494 
495 	if (mutex_lock_interruptible(&ir->irctl_lock)) {
496 		ret = -ERESTARTSYS;
497 		goto out_unlocked;
498 	}
499 	if (!ir->attached) {
500 		ret = -ENODEV;
501 		goto out_locked;
502 	}
503 
504 	if (length % ir->chunk_size) {
505 		ret = -EINVAL;
506 		goto out_locked;
507 	}
508 
509 	/*
510 	 * we add ourselves to the task queue before buffer check
511 	 * to avoid losing scan code (in case when queue is awaken somewhere
512 	 * between while condition checking and scheduling)
513 	 */
514 	add_wait_queue(&ir->buf->wait_poll, &wait);
515 
516 	/*
517 	 * while we didn't provide 'length' bytes, device is opened in blocking
518 	 * mode and 'copy_to_user' is happy, wait for data.
519 	 */
520 	while (written < length && ret == 0) {
521 		if (lirc_buffer_empty(ir->buf)) {
522 			/* According to the read(2) man page, 'written' can be
523 			 * returned as less than 'length', instead of blocking
524 			 * again, returning -EWOULDBLOCK, or returning
525 			 * -ERESTARTSYS
526 			 */
527 			if (written)
528 				break;
529 			if (file->f_flags & O_NONBLOCK) {
530 				ret = -EWOULDBLOCK;
531 				break;
532 			}
533 			if (signal_pending(current)) {
534 				ret = -ERESTARTSYS;
535 				break;
536 			}
537 
538 			mutex_unlock(&ir->irctl_lock);
539 			set_current_state(TASK_INTERRUPTIBLE);
540 			schedule();
541 			set_current_state(TASK_RUNNING);
542 
543 			if (mutex_lock_interruptible(&ir->irctl_lock)) {
544 				ret = -ERESTARTSYS;
545 				remove_wait_queue(&ir->buf->wait_poll, &wait);
546 				goto out_unlocked;
547 			}
548 
549 			if (!ir->attached) {
550 				ret = -ENODEV;
551 				goto out_locked;
552 			}
553 		} else {
554 			lirc_buffer_read(ir->buf, buf);
555 			ret = copy_to_user((void __user *)buffer+written, buf,
556 					   ir->buf->chunk_size);
557 			if (!ret)
558 				written += ir->buf->chunk_size;
559 			else
560 				ret = -EFAULT;
561 		}
562 	}
563 
564 	remove_wait_queue(&ir->buf->wait_poll, &wait);
565 
566 out_locked:
567 	mutex_unlock(&ir->irctl_lock);
568 
569 out_unlocked:
570 	kfree(buf);
571 
572 	return ret ? ret : written;
573 }
574 EXPORT_SYMBOL(lirc_dev_fop_read);
575 
576 void *lirc_get_pdata(struct file *file)
577 {
578 	return irctls[iminor(file_inode(file))]->d.data;
579 }
580 EXPORT_SYMBOL(lirc_get_pdata);
581 
582 
583 static int __init lirc_dev_init(void)
584 {
585 	int retval;
586 
587 	lirc_class = class_create(THIS_MODULE, "lirc");
588 	if (IS_ERR(lirc_class)) {
589 		pr_err("class_create failed\n");
590 		return PTR_ERR(lirc_class);
591 	}
592 
593 	retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
594 				     "BaseRemoteCtl");
595 	if (retval) {
596 		class_destroy(lirc_class);
597 		pr_err("alloc_chrdev_region failed\n");
598 		return retval;
599 	}
600 
601 	pr_info("IR Remote Control driver registered, major %d\n",
602 						MAJOR(lirc_base_dev));
603 
604 	return 0;
605 }
606 
607 static void __exit lirc_dev_exit(void)
608 {
609 	class_destroy(lirc_class);
610 	unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
611 	pr_info("module unloaded\n");
612 }
613 
614 module_init(lirc_dev_init);
615 module_exit(lirc_dev_exit);
616 
617 MODULE_DESCRIPTION("LIRC base driver module");
618 MODULE_AUTHOR("Artur Lipowski");
619 MODULE_LICENSE("GPL");
620