diff options
Diffstat (limited to 'drivers/media/rc/lirc_dev.c')
-rw-r--r-- | drivers/media/rc/lirc_dev.c | 814 |
1 files changed, 814 insertions, 0 deletions
diff --git a/drivers/media/rc/lirc_dev.c b/drivers/media/rc/lirc_dev.c new file mode 100644 index 00000000000..756656e17bd --- /dev/null +++ b/drivers/media/rc/lirc_dev.c | |||
@@ -0,0 +1,814 @@ | |||
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 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
19 | * | ||
20 | */ | ||
21 | |||
22 | #include <linux/module.h> | ||
23 | #include <linux/kernel.h> | ||
24 | #include <linux/sched.h> | ||
25 | #include <linux/errno.h> | ||
26 | #include <linux/ioctl.h> | ||
27 | #include <linux/fs.h> | ||
28 | #include <linux/poll.h> | ||
29 | #include <linux/completion.h> | ||
30 | #include <linux/mutex.h> | ||
31 | #include <linux/wait.h> | ||
32 | #include <linux/unistd.h> | ||
33 | #include <linux/kthread.h> | ||
34 | #include <linux/bitops.h> | ||
35 | #include <linux/device.h> | ||
36 | #include <linux/cdev.h> | ||
37 | |||
38 | #include <media/lirc.h> | ||
39 | #include <media/lirc_dev.h> | ||
40 | |||
41 | static int debug; | ||
42 | |||
43 | #define IRCTL_DEV_NAME "BaseRemoteCtl" | ||
44 | #define NOPLUG -1 | ||
45 | #define LOGHEAD "lirc_dev (%s[%d]): " | ||
46 | |||
47 | static dev_t lirc_base_dev; | ||
48 | |||
49 | struct irctl { | ||
50 | struct lirc_driver d; | ||
51 | int attached; | ||
52 | int open; | ||
53 | |||
54 | struct mutex irctl_lock; | ||
55 | struct lirc_buffer *buf; | ||
56 | unsigned int chunk_size; | ||
57 | |||
58 | struct task_struct *task; | ||
59 | long jiffies_to_wait; | ||
60 | }; | ||
61 | |||
62 | static DEFINE_MUTEX(lirc_dev_lock); | ||
63 | |||
64 | static struct irctl *irctls[MAX_IRCTL_DEVICES]; | ||
65 | static struct cdev cdevs[MAX_IRCTL_DEVICES]; | ||
66 | |||
67 | /* Only used for sysfs but defined to void otherwise */ | ||
68 | static struct class *lirc_class; | ||
69 | |||
70 | /* helper function | ||
71 | * initializes the irctl structure | ||
72 | */ | ||
73 | static void lirc_irctl_init(struct irctl *ir) | ||
74 | { | ||
75 | mutex_init(&ir->irctl_lock); | ||
76 | ir->d.minor = NOPLUG; | ||
77 | } | ||
78 | |||
79 | static void lirc_irctl_cleanup(struct irctl *ir) | ||
80 | { | ||
81 | dev_dbg(ir->d.dev, LOGHEAD "cleaning up\n", ir->d.name, ir->d.minor); | ||
82 | |||
83 | device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor)); | ||
84 | |||
85 | if (ir->buf != ir->d.rbuf) { | ||
86 | lirc_buffer_free(ir->buf); | ||
87 | kfree(ir->buf); | ||
88 | } | ||
89 | ir->buf = NULL; | ||
90 | } | ||
91 | |||
92 | /* helper function | ||
93 | * reads key codes from driver and puts them into buffer | ||
94 | * returns 0 on success | ||
95 | */ | ||
96 | static int lirc_add_to_buf(struct irctl *ir) | ||
97 | { | ||
98 | if (ir->d.add_to_buf) { | ||
99 | int res = -ENODATA; | ||
100 | int got_data = 0; | ||
101 | |||
102 | /* | ||
103 | * service the device as long as it is returning | ||
104 | * data and we have space | ||
105 | */ | ||
106 | get_data: | ||
107 | res = ir->d.add_to_buf(ir->d.data, ir->buf); | ||
108 | if (res == 0) { | ||
109 | got_data++; | ||
110 | goto get_data; | ||
111 | } | ||
112 | |||
113 | if (res == -ENODEV) | ||
114 | kthread_stop(ir->task); | ||
115 | |||
116 | return got_data ? 0 : res; | ||
117 | } | ||
118 | |||
119 | return 0; | ||
120 | } | ||
121 | |||
122 | /* main function of the polling thread | ||
123 | */ | ||
124 | static int lirc_thread(void *irctl) | ||
125 | { | ||
126 | struct irctl *ir = irctl; | ||
127 | |||
128 | dev_dbg(ir->d.dev, LOGHEAD "poll thread started\n", | ||
129 | ir->d.name, ir->d.minor); | ||
130 | |||
131 | do { | ||
132 | if (ir->open) { | ||
133 | if (ir->jiffies_to_wait) { | ||
134 | set_current_state(TASK_INTERRUPTIBLE); | ||
135 | schedule_timeout(ir->jiffies_to_wait); | ||
136 | } | ||
137 | if (kthread_should_stop()) | ||
138 | break; | ||
139 | if (!lirc_add_to_buf(ir)) | ||
140 | wake_up_interruptible(&ir->buf->wait_poll); | ||
141 | } else { | ||
142 | set_current_state(TASK_INTERRUPTIBLE); | ||
143 | schedule(); | ||
144 | } | ||
145 | } while (!kthread_should_stop()); | ||
146 | |||
147 | dev_dbg(ir->d.dev, LOGHEAD "poll thread ended\n", | ||
148 | ir->d.name, ir->d.minor); | ||
149 | |||
150 | return 0; | ||
151 | } | ||
152 | |||
153 | |||
154 | static struct file_operations lirc_dev_fops = { | ||
155 | .owner = THIS_MODULE, | ||
156 | .read = lirc_dev_fop_read, | ||
157 | .write = lirc_dev_fop_write, | ||
158 | .poll = lirc_dev_fop_poll, | ||
159 | .unlocked_ioctl = lirc_dev_fop_ioctl, | ||
160 | #ifdef CONFIG_COMPAT | ||
161 | .compat_ioctl = lirc_dev_fop_ioctl, | ||
162 | #endif | ||
163 | .open = lirc_dev_fop_open, | ||
164 | .release = lirc_dev_fop_close, | ||
165 | .llseek = noop_llseek, | ||
166 | }; | ||
167 | |||
168 | static int lirc_cdev_add(struct irctl *ir) | ||
169 | { | ||
170 | int retval; | ||
171 | struct lirc_driver *d = &ir->d; | ||
172 | struct cdev *cdev = &cdevs[d->minor]; | ||
173 | |||
174 | if (d->fops) { | ||
175 | cdev_init(cdev, d->fops); | ||
176 | cdev->owner = d->owner; | ||
177 | } else { | ||
178 | cdev_init(cdev, &lirc_dev_fops); | ||
179 | cdev->owner = THIS_MODULE; | ||
180 | } | ||
181 | kobject_set_name(&cdev->kobj, "lirc%d", d->minor); | ||
182 | |||
183 | retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1); | ||
184 | if (retval) | ||
185 | kobject_put(&cdev->kobj); | ||
186 | |||
187 | return retval; | ||
188 | } | ||
189 | |||
190 | int lirc_register_driver(struct lirc_driver *d) | ||
191 | { | ||
192 | struct irctl *ir; | ||
193 | int minor; | ||
194 | int bytes_in_key; | ||
195 | unsigned int chunk_size; | ||
196 | unsigned int buffer_size; | ||
197 | int err; | ||
198 | |||
199 | if (!d) { | ||
200 | printk(KERN_ERR "lirc_dev: lirc_register_driver: " | ||
201 | "driver pointer must be not NULL!\n"); | ||
202 | err = -EBADRQC; | ||
203 | goto out; | ||
204 | } | ||
205 | |||
206 | if (!d->dev) { | ||
207 | printk(KERN_ERR "%s: dev pointer not filled in!\n", __func__); | ||
208 | err = -EINVAL; | ||
209 | goto out; | ||
210 | } | ||
211 | |||
212 | if (MAX_IRCTL_DEVICES <= d->minor) { | ||
213 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | ||
214 | "\"minor\" must be between 0 and %d (%d)!\n", | ||
215 | MAX_IRCTL_DEVICES-1, d->minor); | ||
216 | err = -EBADRQC; | ||
217 | goto out; | ||
218 | } | ||
219 | |||
220 | if (1 > d->code_length || (BUFLEN * 8) < d->code_length) { | ||
221 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | ||
222 | "code length in bits for minor (%d) " | ||
223 | "must be less than %d!\n", | ||
224 | d->minor, BUFLEN * 8); | ||
225 | err = -EBADRQC; | ||
226 | goto out; | ||
227 | } | ||
228 | |||
229 | dev_dbg(d->dev, "lirc_dev: lirc_register_driver: sample_rate: %d\n", | ||
230 | d->sample_rate); | ||
231 | if (d->sample_rate) { | ||
232 | if (2 > d->sample_rate || HZ < d->sample_rate) { | ||
233 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | ||
234 | "sample_rate must be between 2 and %d!\n", HZ); | ||
235 | err = -EBADRQC; | ||
236 | goto out; | ||
237 | } | ||
238 | if (!d->add_to_buf) { | ||
239 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | ||
240 | "add_to_buf cannot be NULL when " | ||
241 | "sample_rate is set\n"); | ||
242 | err = -EBADRQC; | ||
243 | goto out; | ||
244 | } | ||
245 | } else if (!(d->fops && d->fops->read) && !d->rbuf) { | ||
246 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | ||
247 | "fops->read and rbuf cannot all be NULL!\n"); | ||
248 | err = -EBADRQC; | ||
249 | goto out; | ||
250 | } else if (!d->rbuf) { | ||
251 | if (!(d->fops && d->fops->read && d->fops->poll && | ||
252 | d->fops->unlocked_ioctl)) { | ||
253 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | ||
254 | "neither read, poll nor unlocked_ioctl can be NULL!\n"); | ||
255 | err = -EBADRQC; | ||
256 | goto out; | ||
257 | } | ||
258 | } | ||
259 | |||
260 | mutex_lock(&lirc_dev_lock); | ||
261 | |||
262 | minor = d->minor; | ||
263 | |||
264 | if (minor < 0) { | ||
265 | /* find first free slot for driver */ | ||
266 | for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++) | ||
267 | if (!irctls[minor]) | ||
268 | break; | ||
269 | if (MAX_IRCTL_DEVICES == minor) { | ||
270 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | ||
271 | "no free slots for drivers!\n"); | ||
272 | err = -ENOMEM; | ||
273 | goto out_lock; | ||
274 | } | ||
275 | } else if (irctls[minor]) { | ||
276 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | ||
277 | "minor (%d) just registered!\n", minor); | ||
278 | err = -EBUSY; | ||
279 | goto out_lock; | ||
280 | } | ||
281 | |||
282 | ir = kzalloc(sizeof(struct irctl), GFP_KERNEL); | ||
283 | if (!ir) { | ||
284 | err = -ENOMEM; | ||
285 | goto out_lock; | ||
286 | } | ||
287 | lirc_irctl_init(ir); | ||
288 | irctls[minor] = ir; | ||
289 | d->minor = minor; | ||
290 | |||
291 | if (d->sample_rate) { | ||
292 | ir->jiffies_to_wait = HZ / d->sample_rate; | ||
293 | } else { | ||
294 | /* it means - wait for external event in task queue */ | ||
295 | ir->jiffies_to_wait = 0; | ||
296 | } | ||
297 | |||
298 | /* some safety check 8-) */ | ||
299 | d->name[sizeof(d->name)-1] = '\0'; | ||
300 | |||
301 | bytes_in_key = BITS_TO_LONGS(d->code_length) + | ||
302 | (d->code_length % 8 ? 1 : 0); | ||
303 | buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key; | ||
304 | chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key; | ||
305 | |||
306 | if (d->rbuf) { | ||
307 | ir->buf = d->rbuf; | ||
308 | } else { | ||
309 | ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL); | ||
310 | if (!ir->buf) { | ||
311 | err = -ENOMEM; | ||
312 | goto out_lock; | ||
313 | } | ||
314 | err = lirc_buffer_init(ir->buf, chunk_size, buffer_size); | ||
315 | if (err) { | ||
316 | kfree(ir->buf); | ||
317 | goto out_lock; | ||
318 | } | ||
319 | } | ||
320 | ir->chunk_size = ir->buf->chunk_size; | ||
321 | |||
322 | if (d->features == 0) | ||
323 | d->features = LIRC_CAN_REC_LIRCCODE; | ||
324 | |||
325 | ir->d = *d; | ||
326 | |||
327 | device_create(lirc_class, ir->d.dev, | ||
328 | MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL, | ||
329 | "lirc%u", ir->d.minor); | ||
330 | |||
331 | if (d->sample_rate) { | ||
332 | /* try to fire up polling thread */ | ||
333 | ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev"); | ||
334 | if (IS_ERR(ir->task)) { | ||
335 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | ||
336 | "cannot run poll thread for minor = %d\n", | ||
337 | d->minor); | ||
338 | err = -ECHILD; | ||
339 | goto out_sysfs; | ||
340 | } | ||
341 | } | ||
342 | |||
343 | err = lirc_cdev_add(ir); | ||
344 | if (err) | ||
345 | goto out_sysfs; | ||
346 | |||
347 | ir->attached = 1; | ||
348 | mutex_unlock(&lirc_dev_lock); | ||
349 | |||
350 | dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n", | ||
351 | ir->d.name, ir->d.minor); | ||
352 | return minor; | ||
353 | |||
354 | out_sysfs: | ||
355 | device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor)); | ||
356 | out_lock: | ||
357 | mutex_unlock(&lirc_dev_lock); | ||
358 | out: | ||
359 | return err; | ||
360 | } | ||
361 | EXPORT_SYMBOL(lirc_register_driver); | ||
362 | |||
363 | int lirc_unregister_driver(int minor) | ||
364 | { | ||
365 | struct irctl *ir; | ||
366 | struct cdev *cdev; | ||
367 | |||
368 | if (minor < 0 || minor >= MAX_IRCTL_DEVICES) { | ||
369 | printk(KERN_ERR "lirc_dev: %s: minor (%d) must be between " | ||
370 | "0 and %d!\n", __func__, minor, MAX_IRCTL_DEVICES-1); | ||
371 | return -EBADRQC; | ||
372 | } | ||
373 | |||
374 | ir = irctls[minor]; | ||
375 | if (!ir) { | ||
376 | printk(KERN_ERR "lirc_dev: %s: failed to get irctl struct " | ||
377 | "for minor %d!\n", __func__, minor); | ||
378 | return -ENOENT; | ||
379 | } | ||
380 | |||
381 | cdev = &cdevs[minor]; | ||
382 | |||
383 | mutex_lock(&lirc_dev_lock); | ||
384 | |||
385 | if (ir->d.minor != minor) { | ||
386 | printk(KERN_ERR "lirc_dev: %s: minor (%d) device not " | ||
387 | "registered!\n", __func__, minor); | ||
388 | mutex_unlock(&lirc_dev_lock); | ||
389 | return -ENOENT; | ||
390 | } | ||
391 | |||
392 | /* end up polling thread */ | ||
393 | if (ir->task) | ||
394 | kthread_stop(ir->task); | ||
395 | |||
396 | dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n", | ||
397 | ir->d.name, ir->d.minor); | ||
398 | |||
399 | ir->attached = 0; | ||
400 | if (ir->open) { | ||
401 | dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n", | ||
402 | ir->d.name, ir->d.minor); | ||
403 | wake_up_interruptible(&ir->buf->wait_poll); | ||
404 | mutex_lock(&ir->irctl_lock); | ||
405 | ir->d.set_use_dec(ir->d.data); | ||
406 | module_put(cdev->owner); | ||
407 | mutex_unlock(&ir->irctl_lock); | ||
408 | } else { | ||
409 | lirc_irctl_cleanup(ir); | ||
410 | cdev_del(cdev); | ||
411 | kfree(ir); | ||
412 | irctls[minor] = NULL; | ||
413 | } | ||
414 | |||
415 | mutex_unlock(&lirc_dev_lock); | ||
416 | |||
417 | return 0; | ||
418 | } | ||
419 | EXPORT_SYMBOL(lirc_unregister_driver); | ||
420 | |||
421 | int lirc_dev_fop_open(struct inode *inode, struct file *file) | ||
422 | { | ||
423 | struct irctl *ir; | ||
424 | struct cdev *cdev; | ||
425 | int retval = 0; | ||
426 | |||
427 | if (iminor(inode) >= MAX_IRCTL_DEVICES) { | ||
428 | printk(KERN_WARNING "lirc_dev [%d]: open result = -ENODEV\n", | ||
429 | iminor(inode)); | ||
430 | return -ENODEV; | ||
431 | } | ||
432 | |||
433 | if (mutex_lock_interruptible(&lirc_dev_lock)) | ||
434 | return -ERESTARTSYS; | ||
435 | |||
436 | ir = irctls[iminor(inode)]; | ||
437 | if (!ir) { | ||
438 | retval = -ENODEV; | ||
439 | goto error; | ||
440 | } | ||
441 | |||
442 | dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor); | ||
443 | |||
444 | if (ir->d.minor == NOPLUG) { | ||
445 | retval = -ENODEV; | ||
446 | goto error; | ||
447 | } | ||
448 | |||
449 | if (ir->open) { | ||
450 | retval = -EBUSY; | ||
451 | goto error; | ||
452 | } | ||
453 | |||
454 | cdev = &cdevs[iminor(inode)]; | ||
455 | if (try_module_get(cdev->owner)) { | ||
456 | ir->open++; | ||
457 | retval = ir->d.set_use_inc(ir->d.data); | ||
458 | |||
459 | if (retval) { | ||
460 | module_put(cdev->owner); | ||
461 | ir->open--; | ||
462 | } else { | ||
463 | lirc_buffer_clear(ir->buf); | ||
464 | } | ||
465 | if (ir->task) | ||
466 | wake_up_process(ir->task); | ||
467 | } | ||
468 | |||
469 | error: | ||
470 | if (ir) | ||
471 | dev_dbg(ir->d.dev, LOGHEAD "open result = %d\n", | ||
472 | ir->d.name, ir->d.minor, retval); | ||
473 | |||
474 | mutex_unlock(&lirc_dev_lock); | ||
475 | |||
476 | nonseekable_open(inode, file); | ||
477 | |||
478 | return retval; | ||
479 | } | ||
480 | EXPORT_SYMBOL(lirc_dev_fop_open); | ||
481 | |||
482 | int lirc_dev_fop_close(struct inode *inode, struct file *file) | ||
483 | { | ||
484 | struct irctl *ir = irctls[iminor(inode)]; | ||
485 | struct cdev *cdev = &cdevs[iminor(inode)]; | ||
486 | |||
487 | if (!ir) { | ||
488 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); | ||
489 | return -EINVAL; | ||
490 | } | ||
491 | |||
492 | dev_dbg(ir->d.dev, LOGHEAD "close called\n", ir->d.name, ir->d.minor); | ||
493 | |||
494 | WARN_ON(mutex_lock_killable(&lirc_dev_lock)); | ||
495 | |||
496 | ir->open--; | ||
497 | if (ir->attached) { | ||
498 | ir->d.set_use_dec(ir->d.data); | ||
499 | module_put(cdev->owner); | ||
500 | } else { | ||
501 | lirc_irctl_cleanup(ir); | ||
502 | cdev_del(cdev); | ||
503 | irctls[ir->d.minor] = NULL; | ||
504 | kfree(ir); | ||
505 | } | ||
506 | |||
507 | mutex_unlock(&lirc_dev_lock); | ||
508 | |||
509 | return 0; | ||
510 | } | ||
511 | EXPORT_SYMBOL(lirc_dev_fop_close); | ||
512 | |||
513 | unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait) | ||
514 | { | ||
515 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; | ||
516 | unsigned int ret; | ||
517 | |||
518 | if (!ir) { | ||
519 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); | ||
520 | return POLLERR; | ||
521 | } | ||
522 | |||
523 | dev_dbg(ir->d.dev, LOGHEAD "poll called\n", ir->d.name, ir->d.minor); | ||
524 | |||
525 | if (!ir->attached) | ||
526 | return POLLERR; | ||
527 | |||
528 | poll_wait(file, &ir->buf->wait_poll, wait); | ||
529 | |||
530 | if (ir->buf) | ||
531 | if (lirc_buffer_empty(ir->buf)) | ||
532 | ret = 0; | ||
533 | else | ||
534 | ret = POLLIN | POLLRDNORM; | ||
535 | else | ||
536 | ret = POLLERR; | ||
537 | |||
538 | dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n", | ||
539 | ir->d.name, ir->d.minor, ret); | ||
540 | |||
541 | return ret; | ||
542 | } | ||
543 | EXPORT_SYMBOL(lirc_dev_fop_poll); | ||
544 | |||
545 | long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | ||
546 | { | ||
547 | __u32 mode; | ||
548 | int result = 0; | ||
549 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; | ||
550 | |||
551 | if (!ir) { | ||
552 | printk(KERN_ERR "lirc_dev: %s: no irctl found!\n", __func__); | ||
553 | return -ENODEV; | ||
554 | } | ||
555 | |||
556 | dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n", | ||
557 | ir->d.name, ir->d.minor, cmd); | ||
558 | |||
559 | if (ir->d.minor == NOPLUG || !ir->attached) { | ||
560 | dev_dbg(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n", | ||
561 | ir->d.name, ir->d.minor); | ||
562 | return -ENODEV; | ||
563 | } | ||
564 | |||
565 | mutex_lock(&ir->irctl_lock); | ||
566 | |||
567 | switch (cmd) { | ||
568 | case LIRC_GET_FEATURES: | ||
569 | result = put_user(ir->d.features, (__u32 *)arg); | ||
570 | break; | ||
571 | case LIRC_GET_REC_MODE: | ||
572 | if (!(ir->d.features & LIRC_CAN_REC_MASK)) { | ||
573 | result = -ENOSYS; | ||
574 | break; | ||
575 | } | ||
576 | |||
577 | result = put_user(LIRC_REC2MODE | ||
578 | (ir->d.features & LIRC_CAN_REC_MASK), | ||
579 | (__u32 *)arg); | ||
580 | break; | ||
581 | case LIRC_SET_REC_MODE: | ||
582 | if (!(ir->d.features & LIRC_CAN_REC_MASK)) { | ||
583 | result = -ENOSYS; | ||
584 | break; | ||
585 | } | ||
586 | |||
587 | result = get_user(mode, (__u32 *)arg); | ||
588 | if (!result && !(LIRC_MODE2REC(mode) & ir->d.features)) | ||
589 | result = -EINVAL; | ||
590 | /* | ||
591 | * FIXME: We should actually set the mode somehow but | ||
592 | * for now, lirc_serial doesn't support mode changing either | ||
593 | */ | ||
594 | break; | ||
595 | case LIRC_GET_LENGTH: | ||
596 | result = put_user(ir->d.code_length, (__u32 *)arg); | ||
597 | break; | ||
598 | case LIRC_GET_MIN_TIMEOUT: | ||
599 | if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || | ||
600 | ir->d.min_timeout == 0) { | ||
601 | result = -ENOSYS; | ||
602 | break; | ||
603 | } | ||
604 | |||
605 | result = put_user(ir->d.min_timeout, (__u32 *)arg); | ||
606 | break; | ||
607 | case LIRC_GET_MAX_TIMEOUT: | ||
608 | if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || | ||
609 | ir->d.max_timeout == 0) { | ||
610 | result = -ENOSYS; | ||
611 | break; | ||
612 | } | ||
613 | |||
614 | result = put_user(ir->d.max_timeout, (__u32 *)arg); | ||
615 | break; | ||
616 | default: | ||
617 | result = -EINVAL; | ||
618 | } | ||
619 | |||
620 | dev_dbg(ir->d.dev, LOGHEAD "ioctl result = %d\n", | ||
621 | ir->d.name, ir->d.minor, result); | ||
622 | |||
623 | mutex_unlock(&ir->irctl_lock); | ||
624 | |||
625 | return result; | ||
626 | } | ||
627 | EXPORT_SYMBOL(lirc_dev_fop_ioctl); | ||
628 | |||
629 | ssize_t lirc_dev_fop_read(struct file *file, | ||
630 | char *buffer, | ||
631 | size_t length, | ||
632 | loff_t *ppos) | ||
633 | { | ||
634 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; | ||
635 | unsigned char *buf; | ||
636 | int ret = 0, written = 0; | ||
637 | DECLARE_WAITQUEUE(wait, current); | ||
638 | |||
639 | if (!ir) { | ||
640 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); | ||
641 | return -ENODEV; | ||
642 | } | ||
643 | |||
644 | dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor); | ||
645 | |||
646 | buf = kzalloc(ir->chunk_size, GFP_KERNEL); | ||
647 | if (!buf) | ||
648 | return -ENOMEM; | ||
649 | |||
650 | if (mutex_lock_interruptible(&ir->irctl_lock)) { | ||
651 | ret = -ERESTARTSYS; | ||
652 | goto out_unlocked; | ||
653 | } | ||
654 | if (!ir->attached) { | ||
655 | ret = -ENODEV; | ||
656 | goto out_locked; | ||
657 | } | ||
658 | |||
659 | if (length % ir->chunk_size) { | ||
660 | ret = -EINVAL; | ||
661 | goto out_locked; | ||
662 | } | ||
663 | |||
664 | /* | ||
665 | * we add ourselves to the task queue before buffer check | ||
666 | * to avoid losing scan code (in case when queue is awaken somewhere | ||
667 | * between while condition checking and scheduling) | ||
668 | */ | ||
669 | add_wait_queue(&ir->buf->wait_poll, &wait); | ||
670 | set_current_state(TASK_INTERRUPTIBLE); | ||
671 | |||
672 | /* | ||
673 | * while we didn't provide 'length' bytes, device is opened in blocking | ||
674 | * mode and 'copy_to_user' is happy, wait for data. | ||
675 | */ | ||
676 | while (written < length && ret == 0) { | ||
677 | if (lirc_buffer_empty(ir->buf)) { | ||
678 | /* According to the read(2) man page, 'written' can be | ||
679 | * returned as less than 'length', instead of blocking | ||
680 | * again, returning -EWOULDBLOCK, or returning | ||
681 | * -ERESTARTSYS */ | ||
682 | if (written) | ||
683 | break; | ||
684 | if (file->f_flags & O_NONBLOCK) { | ||
685 | ret = -EWOULDBLOCK; | ||
686 | break; | ||
687 | } | ||
688 | if (signal_pending(current)) { | ||
689 | ret = -ERESTARTSYS; | ||
690 | break; | ||
691 | } | ||
692 | |||
693 | mutex_unlock(&ir->irctl_lock); | ||
694 | schedule(); | ||
695 | set_current_state(TASK_INTERRUPTIBLE); | ||
696 | |||
697 | if (mutex_lock_interruptible(&ir->irctl_lock)) { | ||
698 | ret = -ERESTARTSYS; | ||
699 | remove_wait_queue(&ir->buf->wait_poll, &wait); | ||
700 | set_current_state(TASK_RUNNING); | ||
701 | goto out_unlocked; | ||
702 | } | ||
703 | |||
704 | if (!ir->attached) { | ||
705 | ret = -ENODEV; | ||
706 | break; | ||
707 | } | ||
708 | } else { | ||
709 | lirc_buffer_read(ir->buf, buf); | ||
710 | ret = copy_to_user((void *)buffer+written, buf, | ||
711 | ir->buf->chunk_size); | ||
712 | if (!ret) | ||
713 | written += ir->buf->chunk_size; | ||
714 | else | ||
715 | ret = -EFAULT; | ||
716 | } | ||
717 | } | ||
718 | |||
719 | remove_wait_queue(&ir->buf->wait_poll, &wait); | ||
720 | set_current_state(TASK_RUNNING); | ||
721 | |||
722 | out_locked: | ||
723 | mutex_unlock(&ir->irctl_lock); | ||
724 | |||
725 | out_unlocked: | ||
726 | kfree(buf); | ||
727 | dev_dbg(ir->d.dev, LOGHEAD "read result = %s (%d)\n", | ||
728 | ir->d.name, ir->d.minor, ret ? "<fail>" : "<ok>", ret); | ||
729 | |||
730 | return ret ? ret : written; | ||
731 | } | ||
732 | EXPORT_SYMBOL(lirc_dev_fop_read); | ||
733 | |||
734 | void *lirc_get_pdata(struct file *file) | ||
735 | { | ||
736 | void *data = NULL; | ||
737 | |||
738 | if (file && file->f_dentry && file->f_dentry->d_inode && | ||
739 | file->f_dentry->d_inode->i_rdev) { | ||
740 | struct irctl *ir; | ||
741 | ir = irctls[iminor(file->f_dentry->d_inode)]; | ||
742 | data = ir->d.data; | ||
743 | } | ||
744 | |||
745 | return data; | ||
746 | } | ||
747 | EXPORT_SYMBOL(lirc_get_pdata); | ||
748 | |||
749 | |||
750 | ssize_t lirc_dev_fop_write(struct file *file, const char *buffer, | ||
751 | size_t length, loff_t *ppos) | ||
752 | { | ||
753 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; | ||
754 | |||
755 | if (!ir) { | ||
756 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); | ||
757 | return -ENODEV; | ||
758 | } | ||
759 | |||
760 | dev_dbg(ir->d.dev, LOGHEAD "write called\n", ir->d.name, ir->d.minor); | ||
761 | |||
762 | if (!ir->attached) | ||
763 | return -ENODEV; | ||
764 | |||
765 | return -EINVAL; | ||
766 | } | ||
767 | EXPORT_SYMBOL(lirc_dev_fop_write); | ||
768 | |||
769 | |||
770 | static int __init lirc_dev_init(void) | ||
771 | { | ||
772 | int retval; | ||
773 | |||
774 | lirc_class = class_create(THIS_MODULE, "lirc"); | ||
775 | if (IS_ERR(lirc_class)) { | ||
776 | retval = PTR_ERR(lirc_class); | ||
777 | printk(KERN_ERR "lirc_dev: class_create failed\n"); | ||
778 | goto error; | ||
779 | } | ||
780 | |||
781 | retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES, | ||
782 | IRCTL_DEV_NAME); | ||
783 | if (retval) { | ||
784 | class_destroy(lirc_class); | ||
785 | printk(KERN_ERR "lirc_dev: alloc_chrdev_region failed\n"); | ||
786 | goto error; | ||
787 | } | ||
788 | |||
789 | |||
790 | printk(KERN_INFO "lirc_dev: IR Remote Control driver registered, " | ||
791 | "major %d \n", MAJOR(lirc_base_dev)); | ||
792 | |||
793 | error: | ||
794 | return retval; | ||
795 | } | ||
796 | |||
797 | |||
798 | |||
799 | static void __exit lirc_dev_exit(void) | ||
800 | { | ||
801 | class_destroy(lirc_class); | ||
802 | unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES); | ||
803 | printk(KERN_INFO "lirc_dev: module unloaded\n"); | ||
804 | } | ||
805 | |||
806 | module_init(lirc_dev_init); | ||
807 | module_exit(lirc_dev_exit); | ||
808 | |||
809 | MODULE_DESCRIPTION("LIRC base driver module"); | ||
810 | MODULE_AUTHOR("Artur Lipowski"); | ||
811 | MODULE_LICENSE("GPL"); | ||
812 | |||
813 | module_param(debug, bool, S_IRUGO | S_IWUSR); | ||
814 | MODULE_PARM_DESC(debug, "Enable debugging messages"); | ||