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