]>
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, |
4a62a5ab JW |
164 | .open = lirc_dev_fop_open, |
165 | .release = lirc_dev_fop_close, | |
6038f373 | 166 | .llseek = noop_llseek, |
4a62a5ab JW |
167 | }; |
168 | ||
169 | static int lirc_cdev_add(struct irctl *ir) | |
170 | { | |
171 | int retval; | |
172 | struct lirc_driver *d = &ir->d; | |
173 | ||
174 | if (d->fops) { | |
175 | cdev_init(&ir->cdev, d->fops); | |
176 | ir->cdev.owner = d->owner; | |
177 | } else { | |
178 | cdev_init(&ir->cdev, &fops); | |
179 | ir->cdev.owner = THIS_MODULE; | |
180 | } | |
181 | kobject_set_name(&ir->cdev.kobj, "lirc%d", d->minor); | |
182 | ||
183 | retval = cdev_add(&ir->cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1); | |
184 | if (retval) | |
185 | kobject_put(&ir->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 (MAX_IRCTL_DEVICES <= d->minor) { | |
207 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | |
208 | "\"minor\" must be between 0 and %d (%d)!\n", | |
209 | MAX_IRCTL_DEVICES-1, d->minor); | |
210 | err = -EBADRQC; | |
211 | goto out; | |
212 | } | |
213 | ||
214 | if (1 > d->code_length || (BUFLEN * 8) < d->code_length) { | |
215 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | |
216 | "code length in bits for minor (%d) " | |
217 | "must be less than %d!\n", | |
218 | d->minor, BUFLEN * 8); | |
219 | err = -EBADRQC; | |
220 | goto out; | |
221 | } | |
222 | ||
223 | dev_dbg(d->dev, "lirc_dev: lirc_register_driver: sample_rate: %d\n", | |
224 | d->sample_rate); | |
225 | if (d->sample_rate) { | |
226 | if (2 > d->sample_rate || HZ < d->sample_rate) { | |
227 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | |
228 | "sample_rate must be between 2 and %d!\n", HZ); | |
229 | err = -EBADRQC; | |
230 | goto out; | |
231 | } | |
232 | if (!d->add_to_buf) { | |
233 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | |
234 | "add_to_buf cannot be NULL when " | |
235 | "sample_rate is set\n"); | |
236 | err = -EBADRQC; | |
237 | goto out; | |
238 | } | |
239 | } else if (!(d->fops && d->fops->read) && !d->rbuf) { | |
240 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | |
241 | "fops->read and rbuf cannot all be NULL!\n"); | |
242 | err = -EBADRQC; | |
243 | goto out; | |
244 | } else if (!d->rbuf) { | |
245 | if (!(d->fops && d->fops->read && d->fops->poll && | |
044e5878 | 246 | d->fops->unlocked_ioctl)) { |
4a62a5ab | 247 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
044e5878 | 248 | "neither read, poll nor unlocked_ioctl can be NULL!\n"); |
4a62a5ab JW |
249 | err = -EBADRQC; |
250 | goto out; | |
251 | } | |
252 | } | |
253 | ||
254 | mutex_lock(&lirc_dev_lock); | |
255 | ||
256 | minor = d->minor; | |
257 | ||
258 | if (minor < 0) { | |
259 | /* find first free slot for driver */ | |
260 | for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++) | |
261 | if (!irctls[minor]) | |
262 | break; | |
263 | if (MAX_IRCTL_DEVICES == minor) { | |
264 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | |
265 | "no free slots for drivers!\n"); | |
266 | err = -ENOMEM; | |
267 | goto out_lock; | |
268 | } | |
269 | } else if (irctls[minor]) { | |
270 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | |
271 | "minor (%d) just registered!\n", minor); | |
272 | err = -EBUSY; | |
273 | goto out_lock; | |
274 | } | |
275 | ||
276 | ir = kzalloc(sizeof(struct irctl), GFP_KERNEL); | |
277 | if (!ir) { | |
278 | err = -ENOMEM; | |
279 | goto out_lock; | |
280 | } | |
281 | init_irctl(ir); | |
282 | irctls[minor] = ir; | |
283 | d->minor = minor; | |
284 | ||
285 | if (d->sample_rate) { | |
286 | ir->jiffies_to_wait = HZ / d->sample_rate; | |
287 | } else { | |
288 | /* it means - wait for external event in task queue */ | |
289 | ir->jiffies_to_wait = 0; | |
290 | } | |
291 | ||
292 | /* some safety check 8-) */ | |
293 | d->name[sizeof(d->name)-1] = '\0'; | |
294 | ||
295 | bytes_in_key = BITS_TO_LONGS(d->code_length) + | |
296 | (d->code_length % 8 ? 1 : 0); | |
297 | buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key; | |
298 | chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key; | |
299 | ||
300 | if (d->rbuf) { | |
301 | ir->buf = d->rbuf; | |
302 | } else { | |
303 | ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL); | |
304 | if (!ir->buf) { | |
305 | err = -ENOMEM; | |
306 | goto out_lock; | |
307 | } | |
308 | err = lirc_buffer_init(ir->buf, chunk_size, buffer_size); | |
309 | if (err) { | |
310 | kfree(ir->buf); | |
311 | goto out_lock; | |
312 | } | |
313 | } | |
314 | ir->chunk_size = ir->buf->chunk_size; | |
315 | ||
316 | if (d->features == 0) | |
317 | d->features = LIRC_CAN_REC_LIRCCODE; | |
318 | ||
319 | ir->d = *d; | |
320 | ir->d.minor = minor; | |
321 | ||
322 | device_create(lirc_class, ir->d.dev, | |
323 | MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL, | |
324 | "lirc%u", ir->d.minor); | |
325 | ||
326 | if (d->sample_rate) { | |
327 | /* try to fire up polling thread */ | |
328 | ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev"); | |
329 | if (IS_ERR(ir->task)) { | |
330 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " | |
331 | "cannot run poll thread for minor = %d\n", | |
332 | d->minor); | |
333 | err = -ECHILD; | |
334 | goto out_sysfs; | |
335 | } | |
336 | } | |
337 | ||
338 | err = lirc_cdev_add(ir); | |
339 | if (err) | |
340 | goto out_sysfs; | |
341 | ||
342 | ir->attached = 1; | |
343 | mutex_unlock(&lirc_dev_lock); | |
344 | ||
345 | dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n", | |
346 | ir->d.name, ir->d.minor); | |
347 | return minor; | |
348 | ||
349 | out_sysfs: | |
350 | device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor)); | |
351 | out_lock: | |
352 | mutex_unlock(&lirc_dev_lock); | |
353 | out: | |
354 | return err; | |
355 | } | |
356 | EXPORT_SYMBOL(lirc_register_driver); | |
357 | ||
358 | int lirc_unregister_driver(int minor) | |
359 | { | |
360 | struct irctl *ir; | |
361 | ||
362 | if (minor < 0 || minor >= MAX_IRCTL_DEVICES) { | |
363 | printk(KERN_ERR "lirc_dev: lirc_unregister_driver: " | |
364 | "\"minor (%d)\" must be between 0 and %d!\n", | |
365 | minor, MAX_IRCTL_DEVICES-1); | |
366 | return -EBADRQC; | |
367 | } | |
368 | ||
369 | ir = irctls[minor]; | |
370 | ||
371 | mutex_lock(&lirc_dev_lock); | |
372 | ||
373 | if (ir->d.minor != minor) { | |
374 | printk(KERN_ERR "lirc_dev: lirc_unregister_driver: " | |
375 | "minor (%d) device not registered!", minor); | |
376 | mutex_unlock(&lirc_dev_lock); | |
377 | return -ENOENT; | |
378 | } | |
379 | ||
380 | /* end up polling thread */ | |
381 | if (ir->task) | |
382 | kthread_stop(ir->task); | |
383 | ||
384 | dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n", | |
385 | ir->d.name, ir->d.minor); | |
386 | ||
387 | ir->attached = 0; | |
388 | if (ir->open) { | |
389 | dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n", | |
390 | ir->d.name, ir->d.minor); | |
391 | wake_up_interruptible(&ir->buf->wait_poll); | |
392 | mutex_lock(&ir->irctl_lock); | |
393 | ir->d.set_use_dec(ir->d.data); | |
394 | module_put(ir->d.owner); | |
395 | mutex_unlock(&ir->irctl_lock); | |
396 | cdev_del(&ir->cdev); | |
397 | } else { | |
398 | cleanup(ir); | |
399 | cdev_del(&ir->cdev); | |
400 | kfree(ir); | |
401 | irctls[minor] = NULL; | |
402 | } | |
403 | ||
404 | mutex_unlock(&lirc_dev_lock); | |
405 | ||
406 | return 0; | |
407 | } | |
408 | EXPORT_SYMBOL(lirc_unregister_driver); | |
409 | ||
410 | int lirc_dev_fop_open(struct inode *inode, struct file *file) | |
411 | { | |
412 | struct irctl *ir; | |
413 | int retval = 0; | |
414 | ||
415 | if (iminor(inode) >= MAX_IRCTL_DEVICES) { | |
416 | printk(KERN_WARNING "lirc_dev [%d]: open result = -ENODEV\n", | |
417 | iminor(inode)); | |
418 | return -ENODEV; | |
419 | } | |
420 | ||
421 | if (mutex_lock_interruptible(&lirc_dev_lock)) | |
422 | return -ERESTARTSYS; | |
423 | ||
424 | ir = irctls[iminor(inode)]; | |
425 | if (!ir) { | |
426 | retval = -ENODEV; | |
427 | goto error; | |
428 | } | |
044e5878 | 429 | file->private_data = ir; |
4a62a5ab JW |
430 | |
431 | dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor); | |
432 | ||
433 | if (ir->d.minor == NOPLUG) { | |
434 | retval = -ENODEV; | |
435 | goto error; | |
436 | } | |
437 | ||
438 | if (ir->open) { | |
439 | retval = -EBUSY; | |
440 | goto error; | |
441 | } | |
442 | ||
443 | if (try_module_get(ir->d.owner)) { | |
444 | ++ir->open; | |
445 | retval = ir->d.set_use_inc(ir->d.data); | |
446 | ||
447 | if (retval) { | |
448 | module_put(ir->d.owner); | |
449 | --ir->open; | |
450 | } else { | |
451 | lirc_buffer_clear(ir->buf); | |
452 | } | |
453 | if (ir->task) | |
454 | wake_up_process(ir->task); | |
455 | } | |
456 | ||
457 | error: | |
458 | if (ir) | |
459 | dev_dbg(ir->d.dev, LOGHEAD "open result = %d\n", | |
460 | ir->d.name, ir->d.minor, retval); | |
461 | ||
462 | mutex_unlock(&lirc_dev_lock); | |
463 | ||
d9d2e9d5 AB |
464 | nonseekable_open(inode, file); |
465 | ||
4a62a5ab JW |
466 | return retval; |
467 | } | |
468 | EXPORT_SYMBOL(lirc_dev_fop_open); | |
469 | ||
470 | int lirc_dev_fop_close(struct inode *inode, struct file *file) | |
471 | { | |
472 | struct irctl *ir = irctls[iminor(inode)]; | |
473 | ||
474 | dev_dbg(ir->d.dev, LOGHEAD "close called\n", ir->d.name, ir->d.minor); | |
475 | ||
476 | WARN_ON(mutex_lock_killable(&lirc_dev_lock)); | |
477 | ||
478 | --ir->open; | |
479 | if (ir->attached) { | |
480 | ir->d.set_use_dec(ir->d.data); | |
481 | module_put(ir->d.owner); | |
482 | } else { | |
483 | cleanup(ir); | |
484 | irctls[ir->d.minor] = NULL; | |
485 | kfree(ir); | |
486 | } | |
487 | ||
488 | mutex_unlock(&lirc_dev_lock); | |
489 | ||
490 | return 0; | |
491 | } | |
492 | EXPORT_SYMBOL(lirc_dev_fop_close); | |
493 | ||
494 | unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait) | |
495 | { | |
496 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; | |
497 | unsigned int ret; | |
498 | ||
499 | dev_dbg(ir->d.dev, LOGHEAD "poll called\n", ir->d.name, ir->d.minor); | |
500 | ||
501 | if (!ir->attached) { | |
502 | mutex_unlock(&ir->irctl_lock); | |
503 | return POLLERR; | |
504 | } | |
505 | ||
506 | poll_wait(file, &ir->buf->wait_poll, wait); | |
507 | ||
508 | if (ir->buf) | |
509 | if (lirc_buffer_empty(ir->buf)) | |
510 | ret = 0; | |
511 | else | |
512 | ret = POLLIN | POLLRDNORM; | |
513 | else | |
514 | ret = POLLERR; | |
515 | ||
516 | dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n", | |
517 | ir->d.name, ir->d.minor, ret); | |
518 | ||
519 | return ret; | |
520 | } | |
521 | EXPORT_SYMBOL(lirc_dev_fop_poll); | |
522 | ||
044e5878 | 523 | long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
4a62a5ab JW |
524 | { |
525 | unsigned long mode; | |
526 | int result = 0; | |
044e5878 | 527 | struct irctl *ir = file->private_data; |
4a62a5ab JW |
528 | |
529 | dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n", | |
530 | ir->d.name, ir->d.minor, cmd); | |
531 | ||
532 | if (ir->d.minor == NOPLUG || !ir->attached) { | |
533 | dev_dbg(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n", | |
534 | ir->d.name, ir->d.minor); | |
535 | return -ENODEV; | |
536 | } | |
537 | ||
538 | mutex_lock(&ir->irctl_lock); | |
539 | ||
540 | switch (cmd) { | |
541 | case LIRC_GET_FEATURES: | |
542 | result = put_user(ir->d.features, (unsigned long *)arg); | |
543 | break; | |
544 | case LIRC_GET_REC_MODE: | |
545 | if (!(ir->d.features & LIRC_CAN_REC_MASK)) { | |
546 | result = -ENOSYS; | |
547 | break; | |
548 | } | |
549 | ||
550 | result = put_user(LIRC_REC2MODE | |
551 | (ir->d.features & LIRC_CAN_REC_MASK), | |
552 | (unsigned long *)arg); | |
553 | break; | |
554 | case LIRC_SET_REC_MODE: | |
555 | if (!(ir->d.features & LIRC_CAN_REC_MASK)) { | |
556 | result = -ENOSYS; | |
557 | break; | |
558 | } | |
559 | ||
560 | result = get_user(mode, (unsigned long *)arg); | |
561 | if (!result && !(LIRC_MODE2REC(mode) & ir->d.features)) | |
562 | result = -EINVAL; | |
563 | /* | |
564 | * FIXME: We should actually set the mode somehow but | |
565 | * for now, lirc_serial doesn't support mode changing either | |
566 | */ | |
567 | break; | |
568 | case LIRC_GET_LENGTH: | |
569 | result = put_user(ir->d.code_length, (unsigned long *)arg); | |
570 | break; | |
571 | case LIRC_GET_MIN_TIMEOUT: | |
572 | if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || | |
573 | ir->d.min_timeout == 0) { | |
574 | result = -ENOSYS; | |
575 | break; | |
576 | } | |
577 | ||
578 | result = put_user(ir->d.min_timeout, (unsigned long *)arg); | |
579 | break; | |
580 | case LIRC_GET_MAX_TIMEOUT: | |
581 | if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || | |
582 | ir->d.max_timeout == 0) { | |
583 | result = -ENOSYS; | |
584 | break; | |
585 | } | |
586 | ||
587 | result = put_user(ir->d.max_timeout, (unsigned long *)arg); | |
588 | break; | |
589 | default: | |
590 | result = -EINVAL; | |
591 | } | |
592 | ||
593 | dev_dbg(ir->d.dev, LOGHEAD "ioctl result = %d\n", | |
594 | ir->d.name, ir->d.minor, result); | |
595 | ||
596 | mutex_unlock(&ir->irctl_lock); | |
597 | ||
598 | return result; | |
599 | } | |
600 | EXPORT_SYMBOL(lirc_dev_fop_ioctl); | |
601 | ||
602 | ssize_t lirc_dev_fop_read(struct file *file, | |
603 | char *buffer, | |
604 | size_t length, | |
605 | loff_t *ppos) | |
606 | { | |
607 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; | |
608 | unsigned char buf[ir->chunk_size]; | |
609 | int ret = 0, written = 0; | |
610 | DECLARE_WAITQUEUE(wait, current); | |
611 | ||
612 | dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor); | |
613 | ||
614 | if (mutex_lock_interruptible(&ir->irctl_lock)) | |
615 | return -ERESTARTSYS; | |
616 | if (!ir->attached) { | |
617 | mutex_unlock(&ir->irctl_lock); | |
618 | return -ENODEV; | |
619 | } | |
620 | ||
621 | if (length % ir->chunk_size) { | |
622 | dev_dbg(ir->d.dev, LOGHEAD "read result = -EINVAL\n", | |
623 | ir->d.name, ir->d.minor); | |
624 | mutex_unlock(&ir->irctl_lock); | |
625 | return -EINVAL; | |
626 | } | |
627 | ||
628 | /* | |
629 | * we add ourselves to the task queue before buffer check | |
630 | * to avoid losing scan code (in case when queue is awaken somewhere | |
631 | * between while condition checking and scheduling) | |
632 | */ | |
633 | add_wait_queue(&ir->buf->wait_poll, &wait); | |
634 | set_current_state(TASK_INTERRUPTIBLE); | |
635 | ||
636 | /* | |
637 | * while we didn't provide 'length' bytes, device is opened in blocking | |
638 | * mode and 'copy_to_user' is happy, wait for data. | |
639 | */ | |
640 | while (written < length && ret == 0) { | |
641 | if (lirc_buffer_empty(ir->buf)) { | |
642 | /* According to the read(2) man page, 'written' can be | |
643 | * returned as less than 'length', instead of blocking | |
644 | * again, returning -EWOULDBLOCK, or returning | |
645 | * -ERESTARTSYS */ | |
646 | if (written) | |
647 | break; | |
648 | if (file->f_flags & O_NONBLOCK) { | |
649 | ret = -EWOULDBLOCK; | |
650 | break; | |
651 | } | |
652 | if (signal_pending(current)) { | |
653 | ret = -ERESTARTSYS; | |
654 | break; | |
655 | } | |
656 | ||
657 | mutex_unlock(&ir->irctl_lock); | |
658 | schedule(); | |
659 | set_current_state(TASK_INTERRUPTIBLE); | |
660 | ||
661 | if (mutex_lock_interruptible(&ir->irctl_lock)) { | |
662 | ret = -ERESTARTSYS; | |
69c271f3 JW |
663 | remove_wait_queue(&ir->buf->wait_poll, &wait); |
664 | set_current_state(TASK_RUNNING); | |
665 | goto out_unlocked; | |
4a62a5ab JW |
666 | } |
667 | ||
668 | if (!ir->attached) { | |
669 | ret = -ENODEV; | |
670 | break; | |
671 | } | |
672 | } else { | |
673 | lirc_buffer_read(ir->buf, buf); | |
674 | ret = copy_to_user((void *)buffer+written, buf, | |
675 | ir->buf->chunk_size); | |
676 | written += ir->buf->chunk_size; | |
677 | } | |
678 | } | |
679 | ||
680 | remove_wait_queue(&ir->buf->wait_poll, &wait); | |
681 | set_current_state(TASK_RUNNING); | |
682 | mutex_unlock(&ir->irctl_lock); | |
683 | ||
69c271f3 | 684 | out_unlocked: |
4a62a5ab JW |
685 | dev_dbg(ir->d.dev, LOGHEAD "read result = %s (%d)\n", |
686 | ir->d.name, ir->d.minor, ret ? "-EFAULT" : "OK", ret); | |
687 | ||
688 | return ret ? ret : written; | |
689 | } | |
690 | EXPORT_SYMBOL(lirc_dev_fop_read); | |
691 | ||
692 | void *lirc_get_pdata(struct file *file) | |
693 | { | |
694 | void *data = NULL; | |
695 | ||
696 | if (file && file->f_dentry && file->f_dentry->d_inode && | |
697 | file->f_dentry->d_inode->i_rdev) { | |
698 | struct irctl *ir; | |
699 | ir = irctls[iminor(file->f_dentry->d_inode)]; | |
700 | data = ir->d.data; | |
701 | } | |
702 | ||
703 | return data; | |
704 | } | |
705 | EXPORT_SYMBOL(lirc_get_pdata); | |
706 | ||
707 | ||
708 | ssize_t lirc_dev_fop_write(struct file *file, const char *buffer, | |
709 | size_t length, loff_t *ppos) | |
710 | { | |
711 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; | |
712 | ||
713 | dev_dbg(ir->d.dev, LOGHEAD "write called\n", ir->d.name, ir->d.minor); | |
714 | ||
715 | if (!ir->attached) | |
716 | return -ENODEV; | |
717 | ||
718 | return -EINVAL; | |
719 | } | |
720 | EXPORT_SYMBOL(lirc_dev_fop_write); | |
721 | ||
722 | ||
723 | static int __init lirc_dev_init(void) | |
724 | { | |
725 | int retval; | |
726 | ||
727 | lirc_class = class_create(THIS_MODULE, "lirc"); | |
728 | if (IS_ERR(lirc_class)) { | |
729 | retval = PTR_ERR(lirc_class); | |
730 | printk(KERN_ERR "lirc_dev: class_create failed\n"); | |
731 | goto error; | |
732 | } | |
733 | ||
734 | retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES, | |
735 | IRCTL_DEV_NAME); | |
736 | if (retval) { | |
737 | class_destroy(lirc_class); | |
738 | printk(KERN_ERR "lirc_dev: alloc_chrdev_region failed\n"); | |
739 | goto error; | |
740 | } | |
741 | ||
742 | ||
743 | printk(KERN_INFO "lirc_dev: IR Remote Control driver registered, " | |
744 | "major %d \n", MAJOR(lirc_base_dev)); | |
745 | ||
746 | error: | |
747 | return retval; | |
748 | } | |
749 | ||
750 | ||
751 | ||
752 | static void __exit lirc_dev_exit(void) | |
753 | { | |
754 | class_destroy(lirc_class); | |
755 | unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES); | |
756 | printk(KERN_INFO "lirc_dev: module unloaded\n"); | |
757 | } | |
758 | ||
759 | module_init(lirc_dev_init); | |
760 | module_exit(lirc_dev_exit); | |
761 | ||
762 | MODULE_DESCRIPTION("LIRC base driver module"); | |
763 | MODULE_AUTHOR("Artur Lipowski"); | |
764 | MODULE_LICENSE("GPL"); | |
765 | ||
766 | module_param(debug, bool, S_IRUGO | S_IWUSR); | |
767 | MODULE_PARM_DESC(debug, "Enable debugging messages"); |