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, | |
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]; | |
df1868e4 JW |
369 | if (!ir) { |
370 | printk(KERN_ERR "lirc_dev: lirc_unregister_driver: " | |
371 | "failed to get irctl struct for minor %d!", minor); | |
372 | return -ENOENT; | |
373 | } | |
4a62a5ab JW |
374 | |
375 | mutex_lock(&lirc_dev_lock); | |
376 | ||
377 | if (ir->d.minor != minor) { | |
378 | printk(KERN_ERR "lirc_dev: lirc_unregister_driver: " | |
379 | "minor (%d) device not registered!", minor); | |
380 | mutex_unlock(&lirc_dev_lock); | |
381 | return -ENOENT; | |
382 | } | |
383 | ||
384 | /* end up polling thread */ | |
385 | if (ir->task) | |
386 | kthread_stop(ir->task); | |
387 | ||
388 | dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n", | |
389 | ir->d.name, ir->d.minor); | |
390 | ||
391 | ir->attached = 0; | |
392 | if (ir->open) { | |
393 | dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n", | |
394 | ir->d.name, ir->d.minor); | |
395 | wake_up_interruptible(&ir->buf->wait_poll); | |
396 | mutex_lock(&ir->irctl_lock); | |
397 | ir->d.set_use_dec(ir->d.data); | |
398 | module_put(ir->d.owner); | |
399 | mutex_unlock(&ir->irctl_lock); | |
400 | cdev_del(&ir->cdev); | |
401 | } else { | |
402 | cleanup(ir); | |
403 | cdev_del(&ir->cdev); | |
404 | kfree(ir); | |
405 | irctls[minor] = NULL; | |
406 | } | |
407 | ||
408 | mutex_unlock(&lirc_dev_lock); | |
409 | ||
410 | return 0; | |
411 | } | |
412 | EXPORT_SYMBOL(lirc_unregister_driver); | |
413 | ||
414 | int lirc_dev_fop_open(struct inode *inode, struct file *file) | |
415 | { | |
416 | struct irctl *ir; | |
417 | int retval = 0; | |
418 | ||
419 | if (iminor(inode) >= MAX_IRCTL_DEVICES) { | |
420 | printk(KERN_WARNING "lirc_dev [%d]: open result = -ENODEV\n", | |
421 | iminor(inode)); | |
422 | return -ENODEV; | |
423 | } | |
424 | ||
425 | if (mutex_lock_interruptible(&lirc_dev_lock)) | |
426 | return -ERESTARTSYS; | |
427 | ||
428 | ir = irctls[iminor(inode)]; | |
429 | if (!ir) { | |
430 | retval = -ENODEV; | |
431 | goto error; | |
432 | } | |
044e5878 | 433 | file->private_data = ir; |
4a62a5ab JW |
434 | |
435 | dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor); | |
436 | ||
437 | if (ir->d.minor == NOPLUG) { | |
438 | retval = -ENODEV; | |
439 | goto error; | |
440 | } | |
441 | ||
442 | if (ir->open) { | |
443 | retval = -EBUSY; | |
444 | goto error; | |
445 | } | |
446 | ||
447 | if (try_module_get(ir->d.owner)) { | |
448 | ++ir->open; | |
449 | retval = ir->d.set_use_inc(ir->d.data); | |
450 | ||
451 | if (retval) { | |
452 | module_put(ir->d.owner); | |
453 | --ir->open; | |
454 | } else { | |
455 | lirc_buffer_clear(ir->buf); | |
456 | } | |
457 | if (ir->task) | |
458 | wake_up_process(ir->task); | |
459 | } | |
460 | ||
461 | error: | |
462 | if (ir) | |
463 | dev_dbg(ir->d.dev, LOGHEAD "open result = %d\n", | |
464 | ir->d.name, ir->d.minor, retval); | |
465 | ||
466 | mutex_unlock(&lirc_dev_lock); | |
467 | ||
468 | return retval; | |
469 | } | |
470 | EXPORT_SYMBOL(lirc_dev_fop_open); | |
471 | ||
472 | int lirc_dev_fop_close(struct inode *inode, struct file *file) | |
473 | { | |
474 | struct irctl *ir = irctls[iminor(inode)]; | |
475 | ||
476 | dev_dbg(ir->d.dev, LOGHEAD "close called\n", ir->d.name, ir->d.minor); | |
477 | ||
478 | WARN_ON(mutex_lock_killable(&lirc_dev_lock)); | |
479 | ||
480 | --ir->open; | |
481 | if (ir->attached) { | |
482 | ir->d.set_use_dec(ir->d.data); | |
483 | module_put(ir->d.owner); | |
484 | } else { | |
485 | cleanup(ir); | |
486 | irctls[ir->d.minor] = NULL; | |
487 | kfree(ir); | |
488 | } | |
489 | ||
490 | mutex_unlock(&lirc_dev_lock); | |
491 | ||
492 | return 0; | |
493 | } | |
494 | EXPORT_SYMBOL(lirc_dev_fop_close); | |
495 | ||
496 | unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait) | |
497 | { | |
498 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; | |
499 | unsigned int ret; | |
500 | ||
501 | dev_dbg(ir->d.dev, LOGHEAD "poll called\n", ir->d.name, ir->d.minor); | |
502 | ||
503 | if (!ir->attached) { | |
504 | mutex_unlock(&ir->irctl_lock); | |
505 | return POLLERR; | |
506 | } | |
507 | ||
508 | poll_wait(file, &ir->buf->wait_poll, wait); | |
509 | ||
510 | if (ir->buf) | |
511 | if (lirc_buffer_empty(ir->buf)) | |
512 | ret = 0; | |
513 | else | |
514 | ret = POLLIN | POLLRDNORM; | |
515 | else | |
516 | ret = POLLERR; | |
517 | ||
518 | dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n", | |
519 | ir->d.name, ir->d.minor, ret); | |
520 | ||
521 | return ret; | |
522 | } | |
523 | EXPORT_SYMBOL(lirc_dev_fop_poll); | |
524 | ||
044e5878 | 525 | long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
4a62a5ab JW |
526 | { |
527 | unsigned long mode; | |
528 | int result = 0; | |
044e5878 | 529 | struct irctl *ir = file->private_data; |
4a62a5ab JW |
530 | |
531 | dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n", | |
532 | ir->d.name, ir->d.minor, cmd); | |
533 | ||
534 | if (ir->d.minor == NOPLUG || !ir->attached) { | |
535 | dev_dbg(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n", | |
536 | ir->d.name, ir->d.minor); | |
537 | return -ENODEV; | |
538 | } | |
539 | ||
540 | mutex_lock(&ir->irctl_lock); | |
541 | ||
542 | switch (cmd) { | |
543 | case LIRC_GET_FEATURES: | |
544 | result = put_user(ir->d.features, (unsigned long *)arg); | |
545 | break; | |
546 | case LIRC_GET_REC_MODE: | |
547 | if (!(ir->d.features & LIRC_CAN_REC_MASK)) { | |
548 | result = -ENOSYS; | |
549 | break; | |
550 | } | |
551 | ||
552 | result = put_user(LIRC_REC2MODE | |
553 | (ir->d.features & LIRC_CAN_REC_MASK), | |
554 | (unsigned long *)arg); | |
555 | break; | |
556 | case LIRC_SET_REC_MODE: | |
557 | if (!(ir->d.features & LIRC_CAN_REC_MASK)) { | |
558 | result = -ENOSYS; | |
559 | break; | |
560 | } | |
561 | ||
562 | result = get_user(mode, (unsigned long *)arg); | |
563 | if (!result && !(LIRC_MODE2REC(mode) & ir->d.features)) | |
564 | result = -EINVAL; | |
565 | /* | |
566 | * FIXME: We should actually set the mode somehow but | |
567 | * for now, lirc_serial doesn't support mode changing either | |
568 | */ | |
569 | break; | |
570 | case LIRC_GET_LENGTH: | |
571 | result = put_user(ir->d.code_length, (unsigned long *)arg); | |
572 | break; | |
573 | case LIRC_GET_MIN_TIMEOUT: | |
574 | if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || | |
575 | ir->d.min_timeout == 0) { | |
576 | result = -ENOSYS; | |
577 | break; | |
578 | } | |
579 | ||
580 | result = put_user(ir->d.min_timeout, (unsigned long *)arg); | |
581 | break; | |
582 | case LIRC_GET_MAX_TIMEOUT: | |
583 | if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || | |
584 | ir->d.max_timeout == 0) { | |
585 | result = -ENOSYS; | |
586 | break; | |
587 | } | |
588 | ||
589 | result = put_user(ir->d.max_timeout, (unsigned long *)arg); | |
590 | break; | |
591 | default: | |
592 | result = -EINVAL; | |
593 | } | |
594 | ||
595 | dev_dbg(ir->d.dev, LOGHEAD "ioctl result = %d\n", | |
596 | ir->d.name, ir->d.minor, result); | |
597 | ||
598 | mutex_unlock(&ir->irctl_lock); | |
599 | ||
600 | return result; | |
601 | } | |
602 | EXPORT_SYMBOL(lirc_dev_fop_ioctl); | |
603 | ||
604 | ssize_t lirc_dev_fop_read(struct file *file, | |
605 | char *buffer, | |
606 | size_t length, | |
607 | loff_t *ppos) | |
608 | { | |
609 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; | |
610 | unsigned char buf[ir->chunk_size]; | |
611 | int ret = 0, written = 0; | |
612 | DECLARE_WAITQUEUE(wait, current); | |
613 | ||
614 | dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor); | |
615 | ||
616 | if (mutex_lock_interruptible(&ir->irctl_lock)) | |
617 | return -ERESTARTSYS; | |
618 | if (!ir->attached) { | |
619 | mutex_unlock(&ir->irctl_lock); | |
620 | return -ENODEV; | |
621 | } | |
622 | ||
623 | if (length % ir->chunk_size) { | |
624 | dev_dbg(ir->d.dev, LOGHEAD "read result = -EINVAL\n", | |
625 | ir->d.name, ir->d.minor); | |
626 | mutex_unlock(&ir->irctl_lock); | |
627 | return -EINVAL; | |
628 | } | |
629 | ||
630 | /* | |
631 | * we add ourselves to the task queue before buffer check | |
632 | * to avoid losing scan code (in case when queue is awaken somewhere | |
633 | * between while condition checking and scheduling) | |
634 | */ | |
635 | add_wait_queue(&ir->buf->wait_poll, &wait); | |
636 | set_current_state(TASK_INTERRUPTIBLE); | |
637 | ||
638 | /* | |
639 | * while we didn't provide 'length' bytes, device is opened in blocking | |
640 | * mode and 'copy_to_user' is happy, wait for data. | |
641 | */ | |
642 | while (written < length && ret == 0) { | |
643 | if (lirc_buffer_empty(ir->buf)) { | |
644 | /* According to the read(2) man page, 'written' can be | |
645 | * returned as less than 'length', instead of blocking | |
646 | * again, returning -EWOULDBLOCK, or returning | |
647 | * -ERESTARTSYS */ | |
648 | if (written) | |
649 | break; | |
650 | if (file->f_flags & O_NONBLOCK) { | |
651 | ret = -EWOULDBLOCK; | |
652 | break; | |
653 | } | |
654 | if (signal_pending(current)) { | |
655 | ret = -ERESTARTSYS; | |
656 | break; | |
657 | } | |
658 | ||
659 | mutex_unlock(&ir->irctl_lock); | |
660 | schedule(); | |
661 | set_current_state(TASK_INTERRUPTIBLE); | |
662 | ||
663 | if (mutex_lock_interruptible(&ir->irctl_lock)) { | |
664 | ret = -ERESTARTSYS; | |
69c271f3 JW |
665 | remove_wait_queue(&ir->buf->wait_poll, &wait); |
666 | set_current_state(TASK_RUNNING); | |
667 | goto out_unlocked; | |
4a62a5ab JW |
668 | } |
669 | ||
670 | if (!ir->attached) { | |
671 | ret = -ENODEV; | |
672 | break; | |
673 | } | |
674 | } else { | |
675 | lirc_buffer_read(ir->buf, buf); | |
676 | ret = copy_to_user((void *)buffer+written, buf, | |
677 | ir->buf->chunk_size); | |
678 | written += ir->buf->chunk_size; | |
679 | } | |
680 | } | |
681 | ||
682 | remove_wait_queue(&ir->buf->wait_poll, &wait); | |
683 | set_current_state(TASK_RUNNING); | |
684 | mutex_unlock(&ir->irctl_lock); | |
685 | ||
69c271f3 | 686 | out_unlocked: |
4a62a5ab JW |
687 | dev_dbg(ir->d.dev, LOGHEAD "read result = %s (%d)\n", |
688 | ir->d.name, ir->d.minor, ret ? "-EFAULT" : "OK", ret); | |
689 | ||
690 | return ret ? ret : written; | |
691 | } | |
692 | EXPORT_SYMBOL(lirc_dev_fop_read); | |
693 | ||
694 | void *lirc_get_pdata(struct file *file) | |
695 | { | |
696 | void *data = NULL; | |
697 | ||
698 | if (file && file->f_dentry && file->f_dentry->d_inode && | |
699 | file->f_dentry->d_inode->i_rdev) { | |
700 | struct irctl *ir; | |
701 | ir = irctls[iminor(file->f_dentry->d_inode)]; | |
702 | data = ir->d.data; | |
703 | } | |
704 | ||
705 | return data; | |
706 | } | |
707 | EXPORT_SYMBOL(lirc_get_pdata); | |
708 | ||
709 | ||
710 | ssize_t lirc_dev_fop_write(struct file *file, const char *buffer, | |
711 | size_t length, loff_t *ppos) | |
712 | { | |
713 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; | |
714 | ||
715 | dev_dbg(ir->d.dev, LOGHEAD "write called\n", ir->d.name, ir->d.minor); | |
716 | ||
717 | if (!ir->attached) | |
718 | return -ENODEV; | |
719 | ||
720 | return -EINVAL; | |
721 | } | |
722 | EXPORT_SYMBOL(lirc_dev_fop_write); | |
723 | ||
724 | ||
725 | static int __init lirc_dev_init(void) | |
726 | { | |
727 | int retval; | |
728 | ||
729 | lirc_class = class_create(THIS_MODULE, "lirc"); | |
730 | if (IS_ERR(lirc_class)) { | |
731 | retval = PTR_ERR(lirc_class); | |
732 | printk(KERN_ERR "lirc_dev: class_create failed\n"); | |
733 | goto error; | |
734 | } | |
735 | ||
736 | retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES, | |
737 | IRCTL_DEV_NAME); | |
738 | if (retval) { | |
739 | class_destroy(lirc_class); | |
740 | printk(KERN_ERR "lirc_dev: alloc_chrdev_region failed\n"); | |
741 | goto error; | |
742 | } | |
743 | ||
744 | ||
745 | printk(KERN_INFO "lirc_dev: IR Remote Control driver registered, " | |
746 | "major %d \n", MAJOR(lirc_base_dev)); | |
747 | ||
748 | error: | |
749 | return retval; | |
750 | } | |
751 | ||
752 | ||
753 | ||
754 | static void __exit lirc_dev_exit(void) | |
755 | { | |
756 | class_destroy(lirc_class); | |
757 | unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES); | |
758 | printk(KERN_INFO "lirc_dev: module unloaded\n"); | |
759 | } | |
760 | ||
761 | module_init(lirc_dev_init); | |
762 | module_exit(lirc_dev_exit); | |
763 | ||
764 | MODULE_DESCRIPTION("LIRC base driver module"); | |
765 | MODULE_AUTHOR("Artur Lipowski"); | |
766 | MODULE_LICENSE("GPL"); | |
767 | ||
768 | module_param(debug, bool, S_IRUGO | S_IWUSR); | |
769 | MODULE_PARM_DESC(debug, "Enable debugging messages"); |