GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / lirc / lirc_parallel.c
blob57a02e6f0880b1fd4a28ae3369d49580306ee396
1 /*
2 * lirc_parallel.c
4 * lirc_parallel - device driver for infra-red signal receiving and
5 * transmitting unit built by the author
7 * Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 /*** Includes ***/
27 #ifdef CONFIG_SMP
28 #error "--- Sorry, this driver is not SMP safe. ---"
29 #endif
31 #include <linux/module.h>
32 #include <linux/sched.h>
33 #include <linux/errno.h>
34 #include <linux/signal.h>
35 #include <linux/fs.h>
36 #include <linux/kernel.h>
37 #include <linux/ioport.h>
38 #include <linux/time.h>
39 #include <linux/mm.h>
40 #include <linux/delay.h>
42 #include <linux/io.h>
43 #include <linux/signal.h>
44 #include <linux/irq.h>
45 #include <linux/uaccess.h>
46 #include <asm/div64.h>
48 #include <linux/poll.h>
49 #include <linux/parport.h>
51 #include <media/lirc.h>
52 #include <media/lirc_dev.h>
54 #include "lirc_parallel.h"
56 #define LIRC_DRIVER_NAME "lirc_parallel"
58 #ifndef LIRC_IRQ
59 #define LIRC_IRQ 7
60 #endif
61 #ifndef LIRC_PORT
62 #define LIRC_PORT 0x378
63 #endif
64 #ifndef LIRC_TIMER
65 #define LIRC_TIMER 65536
66 #endif
68 /*** Global Variables ***/
70 static int debug;
71 static int check_pselecd;
73 unsigned int irq = LIRC_IRQ;
74 unsigned int io = LIRC_PORT;
75 #ifdef LIRC_TIMER
76 unsigned int timer;
77 unsigned int default_timer = LIRC_TIMER;
78 #endif
80 #define RBUF_SIZE (256) /* this must be a power of 2 larger than 1 */
82 static int rbuf[RBUF_SIZE];
84 DECLARE_WAIT_QUEUE_HEAD(lirc_wait);
86 unsigned int rptr;
87 unsigned int wptr;
88 unsigned int lost_irqs;
89 int is_open;
91 struct parport *pport;
92 struct pardevice *ppdevice;
93 int is_claimed;
95 unsigned int tx_mask = 1;
97 /*** Internal Functions ***/
99 static unsigned int in(int offset)
101 switch (offset) {
102 case LIRC_LP_BASE:
103 return parport_read_data(pport);
104 case LIRC_LP_STATUS:
105 return parport_read_status(pport);
106 case LIRC_LP_CONTROL:
107 return parport_read_control(pport);
109 return 0; /* make compiler happy */
112 static void out(int offset, int value)
114 switch (offset) {
115 case LIRC_LP_BASE:
116 parport_write_data(pport, value);
117 break;
118 case LIRC_LP_CONTROL:
119 parport_write_control(pport, value);
120 break;
121 case LIRC_LP_STATUS:
122 printk(KERN_INFO "%s: attempt to write to status register\n",
123 LIRC_DRIVER_NAME);
124 break;
128 static unsigned int lirc_get_timer(void)
130 return in(LIRC_PORT_TIMER) & LIRC_PORT_TIMER_BIT;
133 static unsigned int lirc_get_signal(void)
135 return in(LIRC_PORT_SIGNAL) & LIRC_PORT_SIGNAL_BIT;
138 static void lirc_on(void)
140 out(LIRC_PORT_DATA, tx_mask);
143 static void lirc_off(void)
145 out(LIRC_PORT_DATA, 0);
148 static unsigned int init_lirc_timer(void)
150 struct timeval tv, now;
151 unsigned int level, newlevel, timeelapsed, newtimer;
152 int count = 0;
154 do_gettimeofday(&tv);
155 tv.tv_sec++; /* wait max. 1 sec. */
156 level = lirc_get_timer();
157 do {
158 newlevel = lirc_get_timer();
159 if (level == 0 && newlevel != 0)
160 count++;
161 level = newlevel;
162 do_gettimeofday(&now);
163 } while (count < 1000 && (now.tv_sec < tv.tv_sec
164 || (now.tv_sec == tv.tv_sec
165 && now.tv_usec < tv.tv_usec)));
167 timeelapsed = ((now.tv_sec + 1 - tv.tv_sec)*1000000
168 + (now.tv_usec - tv.tv_usec));
169 if (count >= 1000 && timeelapsed > 0) {
170 if (default_timer == 0) {
171 /* autodetect timer */
172 newtimer = (1000000*count)/timeelapsed;
173 printk(KERN_INFO "%s: %u Hz timer detected\n",
174 LIRC_DRIVER_NAME, newtimer);
175 return newtimer;
176 } else {
177 newtimer = (1000000*count)/timeelapsed;
178 if (abs(newtimer - default_timer) > default_timer/10) {
179 /* bad timer */
180 printk(KERN_NOTICE "%s: bad timer: %u Hz\n",
181 LIRC_DRIVER_NAME, newtimer);
182 printk(KERN_NOTICE "%s: using default timer: "
183 "%u Hz\n",
184 LIRC_DRIVER_NAME, default_timer);
185 return default_timer;
186 } else {
187 printk(KERN_INFO "%s: %u Hz timer detected\n",
188 LIRC_DRIVER_NAME, newtimer);
189 return newtimer; /* use detected value */
192 } else {
193 printk(KERN_NOTICE "%s: no timer detected\n", LIRC_DRIVER_NAME);
194 return 0;
198 static int lirc_claim(void)
200 if (parport_claim(ppdevice) != 0) {
201 printk(KERN_WARNING "%s: could not claim port\n",
202 LIRC_DRIVER_NAME);
203 printk(KERN_WARNING "%s: waiting for port becoming available"
204 "\n", LIRC_DRIVER_NAME);
205 if (parport_claim_or_block(ppdevice) < 0) {
206 printk(KERN_NOTICE "%s: could not claim port, giving"
207 " up\n", LIRC_DRIVER_NAME);
208 return 0;
211 out(LIRC_LP_CONTROL, LP_PSELECP|LP_PINITP);
212 is_claimed = 1;
213 return 1;
216 /*** interrupt handler ***/
218 static void rbuf_write(int signal)
220 unsigned int nwptr;
222 nwptr = (wptr + 1) & (RBUF_SIZE - 1);
223 if (nwptr == rptr) {
224 /* no new signals will be accepted */
225 lost_irqs++;
226 printk(KERN_NOTICE "%s: buffer overrun\n", LIRC_DRIVER_NAME);
227 return;
229 rbuf[wptr] = signal;
230 wptr = nwptr;
233 static void irq_handler(void *blah)
235 struct timeval tv;
236 static struct timeval lasttv;
237 static int init;
238 long signal;
239 int data;
240 unsigned int level, newlevel;
241 unsigned int timeout;
243 if (!is_open)
244 return;
246 if (!is_claimed)
247 return;
249 if (check_pselecd && (in(1) & LP_PSELECD))
250 return;
252 #ifdef LIRC_TIMER
253 if (init) {
254 do_gettimeofday(&tv);
256 signal = tv.tv_sec - lasttv.tv_sec;
257 if (signal > 15)
258 /* really long time */
259 data = PULSE_MASK;
260 else
261 data = (int) (signal*1000000 +
262 tv.tv_usec - lasttv.tv_usec +
263 LIRC_SFH506_DELAY);
265 rbuf_write(data); /* space */
266 } else {
267 if (timer == 0) {
269 * wake up; we'll lose this signal, but it will be
270 * garbage if the device is turned on anyway
272 timer = init_lirc_timer();
273 /* enable_irq(irq); */
274 return;
276 init = 1;
279 timeout = timer/10; /* timeout after 1/10 sec. */
280 signal = 1;
281 level = lirc_get_timer();
282 do {
283 newlevel = lirc_get_timer();
284 if (level == 0 && newlevel != 0)
285 signal++;
286 level = newlevel;
288 /* giving up */
289 if (signal > timeout
290 || (check_pselecd && (in(1) & LP_PSELECD))) {
291 signal = 0;
292 printk(KERN_NOTICE "%s: timeout\n", LIRC_DRIVER_NAME);
293 break;
295 } while (lirc_get_signal());
297 if (signal != 0) {
298 /* ajust value to usecs */
299 unsigned long long helper;
301 helper = ((unsigned long long) signal)*1000000;
302 do_div(helper, timer);
303 signal = (long) helper;
305 if (signal > LIRC_SFH506_DELAY)
306 data = signal - LIRC_SFH506_DELAY;
307 else
308 data = 1;
309 rbuf_write(PULSE_BIT|data); /* pulse */
311 do_gettimeofday(&lasttv);
312 #else
313 /* add your code here */
314 #endif
316 wake_up_interruptible(&lirc_wait);
318 /* enable interrupt */
320 enable_irq(irq);
321 out(LIRC_PORT_IRQ, in(LIRC_PORT_IRQ)|LP_PINTEN);
325 /*** file operations ***/
327 static loff_t lirc_lseek(struct file *filep, loff_t offset, int orig)
329 return -ESPIPE;
332 static ssize_t lirc_read(struct file *filep, char *buf, size_t n, loff_t *ppos)
334 int result = 0;
335 int count = 0;
336 DECLARE_WAITQUEUE(wait, current);
338 if (n % sizeof(int))
339 return -EINVAL;
341 add_wait_queue(&lirc_wait, &wait);
342 set_current_state(TASK_INTERRUPTIBLE);
343 while (count < n) {
344 if (rptr != wptr) {
345 if (copy_to_user(buf+count, (char *) &rbuf[rptr],
346 sizeof(int))) {
347 result = -EFAULT;
348 break;
350 rptr = (rptr + 1) & (RBUF_SIZE - 1);
351 count += sizeof(int);
352 } else {
353 if (filep->f_flags & O_NONBLOCK) {
354 result = -EAGAIN;
355 break;
357 if (signal_pending(current)) {
358 result = -ERESTARTSYS;
359 break;
361 schedule();
362 set_current_state(TASK_INTERRUPTIBLE);
365 remove_wait_queue(&lirc_wait, &wait);
366 set_current_state(TASK_RUNNING);
367 return count ? count : result;
370 static ssize_t lirc_write(struct file *filep, const char *buf, size_t n,
371 loff_t *ppos)
373 int count;
374 unsigned int i;
375 unsigned int level, newlevel;
376 unsigned long flags;
377 int counttimer;
378 int *wbuf;
380 if (!is_claimed)
381 return -EBUSY;
383 count = n / sizeof(int);
385 if (n % sizeof(int) || count % 2 == 0)
386 return -EINVAL;
388 wbuf = memdup_user(buf, n);
389 if (IS_ERR(wbuf))
390 return PTR_ERR(wbuf);
392 #ifdef LIRC_TIMER
393 if (timer == 0) {
394 /* try again if device is ready */
395 timer = init_lirc_timer();
396 if (timer == 0)
397 return -EIO;
400 /* adjust values from usecs */
401 for (i = 0; i < count; i++) {
402 unsigned long long helper;
404 helper = ((unsigned long long) wbuf[i])*timer;
405 do_div(helper, 1000000);
406 wbuf[i] = (int) helper;
409 local_irq_save(flags);
410 i = 0;
411 while (i < count) {
412 level = lirc_get_timer();
413 counttimer = 0;
414 lirc_on();
415 do {
416 newlevel = lirc_get_timer();
417 if (level == 0 && newlevel != 0)
418 counttimer++;
419 level = newlevel;
420 if (check_pselecd && (in(1) & LP_PSELECD)) {
421 lirc_off();
422 local_irq_restore(flags);
423 return -EIO;
425 } while (counttimer < wbuf[i]);
426 i++;
428 lirc_off();
429 if (i == count)
430 break;
431 counttimer = 0;
432 do {
433 newlevel = lirc_get_timer();
434 if (level == 0 && newlevel != 0)
435 counttimer++;
436 level = newlevel;
437 if (check_pselecd && (in(1) & LP_PSELECD)) {
438 local_irq_restore(flags);
439 return -EIO;
441 } while (counttimer < wbuf[i]);
442 i++;
444 local_irq_restore(flags);
445 #else
446 /* place code that handles write without external timer here */
447 #endif
448 return n;
451 static unsigned int lirc_poll(struct file *file, poll_table *wait)
453 poll_wait(file, &lirc_wait, wait);
454 if (rptr != wptr)
455 return POLLIN | POLLRDNORM;
456 return 0;
459 static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
461 int result;
462 unsigned long features = LIRC_CAN_SET_TRANSMITTER_MASK |
463 LIRC_CAN_SEND_PULSE | LIRC_CAN_REC_MODE2;
464 unsigned long mode;
465 unsigned int ivalue;
467 switch (cmd) {
468 case LIRC_GET_FEATURES:
469 result = put_user(features, (unsigned long *) arg);
470 if (result)
471 return result;
472 break;
473 case LIRC_GET_SEND_MODE:
474 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
475 if (result)
476 return result;
477 break;
478 case LIRC_GET_REC_MODE:
479 result = put_user(LIRC_MODE_MODE2, (unsigned long *) arg);
480 if (result)
481 return result;
482 break;
483 case LIRC_SET_SEND_MODE:
484 result = get_user(mode, (unsigned long *) arg);
485 if (result)
486 return result;
487 if (mode != LIRC_MODE_PULSE)
488 return -EINVAL;
489 break;
490 case LIRC_SET_REC_MODE:
491 result = get_user(mode, (unsigned long *) arg);
492 if (result)
493 return result;
494 if (mode != LIRC_MODE_MODE2)
495 return -ENOSYS;
496 break;
497 case LIRC_SET_TRANSMITTER_MASK:
498 result = get_user(ivalue, (unsigned int *) arg);
499 if (result)
500 return result;
501 if ((ivalue & LIRC_PARALLEL_TRANSMITTER_MASK) != ivalue)
502 return LIRC_PARALLEL_MAX_TRANSMITTERS;
503 tx_mask = ivalue;
504 break;
505 default:
506 return -ENOIOCTLCMD;
508 return 0;
511 static int lirc_open(struct inode *node, struct file *filep)
513 if (is_open || !lirc_claim())
514 return -EBUSY;
516 parport_enable_irq(pport);
518 /* init read ptr */
519 rptr = 0;
520 wptr = 0;
521 lost_irqs = 0;
523 is_open = 1;
524 return 0;
527 static int lirc_close(struct inode *node, struct file *filep)
529 if (is_claimed) {
530 is_claimed = 0;
531 parport_release(ppdevice);
533 is_open = 0;
534 return 0;
537 static const struct file_operations lirc_fops = {
538 .owner = THIS_MODULE,
539 .llseek = lirc_lseek,
540 .read = lirc_read,
541 .write = lirc_write,
542 .poll = lirc_poll,
543 .unlocked_ioctl = lirc_ioctl,
544 .open = lirc_open,
545 .release = lirc_close
548 static int set_use_inc(void *data)
550 return 0;
553 static void set_use_dec(void *data)
557 static struct lirc_driver driver = {
558 .name = LIRC_DRIVER_NAME,
559 .minor = -1,
560 .code_length = 1,
561 .sample_rate = 0,
562 .data = NULL,
563 .add_to_buf = NULL,
564 .set_use_inc = set_use_inc,
565 .set_use_dec = set_use_dec,
566 .fops = &lirc_fops,
567 .dev = NULL,
568 .owner = THIS_MODULE,
571 static int pf(void *handle);
572 static void kf(void *handle);
574 static struct timer_list poll_timer;
575 static void poll_state(unsigned long ignored);
577 static void poll_state(unsigned long ignored)
579 printk(KERN_NOTICE "%s: time\n",
580 LIRC_DRIVER_NAME);
581 del_timer(&poll_timer);
582 if (is_claimed)
583 return;
584 kf(NULL);
585 if (!is_claimed) {
586 printk(KERN_NOTICE "%s: could not claim port, giving up\n",
587 LIRC_DRIVER_NAME);
588 init_timer(&poll_timer);
589 poll_timer.expires = jiffies + HZ;
590 poll_timer.data = (unsigned long)current;
591 poll_timer.function = poll_state;
592 add_timer(&poll_timer);
596 static int pf(void *handle)
598 parport_disable_irq(pport);
599 is_claimed = 0;
600 return 0;
603 static void kf(void *handle)
605 if (!is_open)
606 return;
607 if (!lirc_claim())
608 return;
609 parport_enable_irq(pport);
610 lirc_off();
611 /* this is a bit annoying when you actually print...*/
613 printk(KERN_INFO "%s: reclaimed port\n", LIRC_DRIVER_NAME);
617 /*** module initialization and cleanup ***/
619 static int __init lirc_parallel_init(void)
621 pport = parport_find_base(io);
622 if (pport == NULL) {
623 printk(KERN_NOTICE "%s: no port at %x found\n",
624 LIRC_DRIVER_NAME, io);
625 return -ENXIO;
627 ppdevice = parport_register_device(pport, LIRC_DRIVER_NAME,
628 pf, kf, irq_handler, 0, NULL);
629 parport_put_port(pport);
630 if (ppdevice == NULL) {
631 printk(KERN_NOTICE "%s: parport_register_device() failed\n",
632 LIRC_DRIVER_NAME);
633 return -ENXIO;
635 if (parport_claim(ppdevice) != 0)
636 goto skip_init;
637 is_claimed = 1;
638 out(LIRC_LP_CONTROL, LP_PSELECP|LP_PINITP);
640 #ifdef LIRC_TIMER
641 if (debug)
642 out(LIRC_PORT_DATA, tx_mask);
644 timer = init_lirc_timer();
646 if (debug)
647 out(LIRC_PORT_DATA, 0);
648 #endif
650 is_claimed = 0;
651 parport_release(ppdevice);
652 skip_init:
653 driver.minor = lirc_register_driver(&driver);
654 if (driver.minor < 0) {
655 printk(KERN_NOTICE "%s: register_chrdev() failed\n",
656 LIRC_DRIVER_NAME);
657 parport_unregister_device(ppdevice);
658 return -EIO;
660 printk(KERN_INFO "%s: installed using port 0x%04x irq %d\n",
661 LIRC_DRIVER_NAME, io, irq);
662 return 0;
665 static void __exit lirc_parallel_exit(void)
667 parport_unregister_device(ppdevice);
668 lirc_unregister_driver(driver.minor);
671 module_init(lirc_parallel_init);
672 module_exit(lirc_parallel_exit);
674 MODULE_DESCRIPTION("Infrared receiver driver for parallel ports.");
675 MODULE_AUTHOR("Christoph Bartelmus");
676 MODULE_LICENSE("GPL");
678 module_param(io, int, S_IRUGO);
679 MODULE_PARM_DESC(io, "I/O address base (0x3bc, 0x378 or 0x278)");
681 module_param(irq, int, S_IRUGO);
682 MODULE_PARM_DESC(irq, "Interrupt (7 or 5)");
684 module_param(tx_mask, int, S_IRUGO);
685 MODULE_PARM_DESC(tx_maxk, "Transmitter mask (default: 0x01)");
687 module_param(debug, bool, S_IRUGO | S_IWUSR);
688 MODULE_PARM_DESC(debug, "Enable debugging messages");
690 module_param(check_pselecd, bool, S_IRUGO | S_IWUSR);
691 MODULE_PARM_DESC(debug, "Check for printer (default: 0)");