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