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