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
28 #error "--- Sorry, this driver is not SMP safe. ---"
31 #include <linux/module.h>
32 #include <linux/sched.h>
33 #include <linux/errno.h>
34 #include <linux/signal.h>
36 #include <linux/kernel.h>
37 #include <linux/ioport.h>
38 #include <linux/time.h>
40 #include <linux/delay.h>
43 #include <linux/irq.h>
44 #include <linux/uaccess.h>
45 #include <asm/div64.h>
47 #include <linux/poll.h>
48 #include <linux/parport.h>
50 #include <media/lirc.h>
51 #include <media/lirc_dev.h>
53 #include "lirc_parallel.h"
55 #define LIRC_DRIVER_NAME "lirc_parallel"
61 #define LIRC_PORT 0x378
64 #define LIRC_TIMER 65536
67 /*** Global Variables ***/
70 static int check_pselecd;
72 unsigned int irq = LIRC_IRQ;
73 unsigned int io = LIRC_PORT;
76 unsigned int default_timer = LIRC_TIMER;
79 #define RBUF_SIZE (256) /* this must be a power of 2 larger than 1 */
81 static int rbuf[RBUF_SIZE];
83 DECLARE_WAIT_QUEUE_HEAD(lirc_wait);
87 unsigned int lost_irqs;
90 struct parport *pport;
91 struct pardevice *ppdevice;
94 unsigned int tx_mask = 1;
96 /*** Internal Functions ***/
98 static unsigned int in(int offset)
102 return parport_read_data(pport);
104 return parport_read_status(pport);
105 case LIRC_LP_CONTROL:
106 return parport_read_control(pport);
108 return 0; /* make compiler happy */
111 static void out(int offset, int value)
115 parport_write_data(pport, value);
117 case LIRC_LP_CONTROL:
118 parport_write_control(pport, value);
121 printk(KERN_INFO "%s: attempt to write to status register\n",
127 static unsigned int lirc_get_timer(void)
129 return in(LIRC_PORT_TIMER) & LIRC_PORT_TIMER_BIT;
132 static unsigned int lirc_get_signal(void)
134 return in(LIRC_PORT_SIGNAL) & LIRC_PORT_SIGNAL_BIT;
137 static void lirc_on(void)
139 out(LIRC_PORT_DATA, tx_mask);
142 static void lirc_off(void)
144 out(LIRC_PORT_DATA, 0);
147 static unsigned int init_lirc_timer(void)
149 struct timeval tv, now;
150 unsigned int level, newlevel, timeelapsed, newtimer;
153 do_gettimeofday(&tv);
154 tv.tv_sec++; /* wait max. 1 sec. */
155 level = lirc_get_timer();
157 newlevel = lirc_get_timer();
158 if (level == 0 && newlevel != 0)
161 do_gettimeofday(&now);
162 } while (count < 1000 && (now.tv_sec < tv.tv_sec
163 || (now.tv_sec == tv.tv_sec
164 && now.tv_usec < tv.tv_usec)));
166 timeelapsed = ((now.tv_sec + 1 - tv.tv_sec)*1000000
167 + (now.tv_usec - tv.tv_usec));
168 if (count >= 1000 && timeelapsed > 0) {
169 if (default_timer == 0) {
170 /* autodetect timer */
171 newtimer = (1000000*count)/timeelapsed;
172 printk(KERN_INFO "%s: %u Hz timer detected\n",
173 LIRC_DRIVER_NAME, newtimer);
176 newtimer = (1000000*count)/timeelapsed;
177 if (abs(newtimer - default_timer) > default_timer/10) {
179 printk(KERN_NOTICE "%s: bad timer: %u Hz\n",
180 LIRC_DRIVER_NAME, newtimer);
181 printk(KERN_NOTICE "%s: using default timer: "
183 LIRC_DRIVER_NAME, default_timer);
184 return default_timer;
186 printk(KERN_INFO "%s: %u Hz timer detected\n",
187 LIRC_DRIVER_NAME, newtimer);
188 return newtimer; /* use detected value */
192 printk(KERN_NOTICE "%s: no timer detected\n", LIRC_DRIVER_NAME);
197 static int lirc_claim(void)
199 if (parport_claim(ppdevice) != 0) {
200 printk(KERN_WARNING "%s: could not claim port\n",
202 printk(KERN_WARNING "%s: waiting for port becoming available"
203 "\n", LIRC_DRIVER_NAME);
204 if (parport_claim_or_block(ppdevice) < 0) {
205 printk(KERN_NOTICE "%s: could not claim port, giving"
206 " up\n", LIRC_DRIVER_NAME);
210 out(LIRC_LP_CONTROL, LP_PSELECP|LP_PINITP);
215 /*** interrupt handler ***/
217 static void rbuf_write(int signal)
221 nwptr = (wptr + 1) & (RBUF_SIZE - 1);
223 /* no new signals will be accepted */
225 printk(KERN_NOTICE "%s: buffer overrun\n", LIRC_DRIVER_NAME);
232 static void irq_handler(void *blah)
235 static struct timeval lasttv;
239 unsigned int level, newlevel;
240 unsigned int timeout;
249 /* disable interrupt */
251 out(LIRC_PORT_IRQ, in(LIRC_PORT_IRQ) & (~LP_PINTEN));
253 if (check_pselecd && (in(1) & LP_PSELECD))
258 do_gettimeofday(&tv);
260 signal = tv.tv_sec - lasttv.tv_sec;
262 /* really long time */
265 data = (int) (signal*1000000 +
266 tv.tv_usec - lasttv.tv_usec +
269 rbuf_write(data); /* space */
273 * wake up; we'll lose this signal, but it will be
274 * garbage if the device is turned on anyway
276 timer = init_lirc_timer();
277 /* enable_irq(irq); */
283 timeout = timer/10; /* timeout after 1/10 sec. */
285 level = lirc_get_timer();
287 newlevel = lirc_get_timer();
288 if (level == 0 && newlevel != 0)
294 || (check_pselecd && (in(1) & LP_PSELECD))) {
296 printk(KERN_NOTICE "%s: timeout\n", LIRC_DRIVER_NAME);
299 } while (lirc_get_signal());
302 /* ajust value to usecs */
303 unsigned long long helper;
305 helper = ((unsigned long long) signal)*1000000;
306 do_div(helper, timer);
307 signal = (long) helper;
309 if (signal > LIRC_SFH506_DELAY)
310 data = signal - LIRC_SFH506_DELAY;
313 rbuf_write(PULSE_BIT|data); /* pulse */
315 do_gettimeofday(&lasttv);
317 /* add your code here */
320 wake_up_interruptible(&lirc_wait);
322 /* enable interrupt */
325 out(LIRC_PORT_IRQ, in(LIRC_PORT_IRQ)|LP_PINTEN);
329 /*** file operations ***/
331 static loff_t lirc_lseek(struct file *filep, loff_t offset, int orig)
336 static ssize_t lirc_read(struct file *filep, char *buf, size_t n, loff_t *ppos)
340 DECLARE_WAITQUEUE(wait, current);
345 add_wait_queue(&lirc_wait, &wait);
346 set_current_state(TASK_INTERRUPTIBLE);
349 if (copy_to_user(buf+count, (char *) &rbuf[rptr],
354 rptr = (rptr + 1) & (RBUF_SIZE - 1);
355 count += sizeof(int);
357 if (filep->f_flags & O_NONBLOCK) {
361 if (signal_pending(current)) {
362 result = -ERESTARTSYS;
366 set_current_state(TASK_INTERRUPTIBLE);
369 remove_wait_queue(&lirc_wait, &wait);
370 set_current_state(TASK_RUNNING);
371 return count ? count : result;
374 static ssize_t lirc_write(struct file *filep, const char *buf, size_t n,
379 unsigned int level, newlevel;
387 count = n / sizeof(int);
389 if (n % sizeof(int) || count % 2 == 0)
392 wbuf = memdup_user(buf, n);
394 return PTR_ERR(wbuf);
398 /* try again if device is ready */
399 timer = init_lirc_timer();
404 /* adjust values from usecs */
405 for (i = 0; i < count; i++) {
406 unsigned long long helper;
408 helper = ((unsigned long long) wbuf[i])*timer;
409 do_div(helper, 1000000);
410 wbuf[i] = (int) helper;
413 local_irq_save(flags);
416 level = lirc_get_timer();
420 newlevel = lirc_get_timer();
421 if (level == 0 && newlevel != 0)
424 if (check_pselecd && (in(1) & LP_PSELECD)) {
426 local_irq_restore(flags);
429 } while (counttimer < wbuf[i]);
437 newlevel = lirc_get_timer();
438 if (level == 0 && newlevel != 0)
441 if (check_pselecd && (in(1) & LP_PSELECD)) {
442 local_irq_restore(flags);
445 } while (counttimer < wbuf[i]);
448 local_irq_restore(flags);
450 /* place code that handles write without external timer here */
455 static unsigned int lirc_poll(struct file *file, poll_table *wait)
457 poll_wait(file, &lirc_wait, wait);
459 return POLLIN | POLLRDNORM;
463 static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
466 unsigned long features = LIRC_CAN_SET_TRANSMITTER_MASK |
467 LIRC_CAN_SEND_PULSE | LIRC_CAN_REC_MODE2;
472 case LIRC_GET_FEATURES:
473 result = put_user(features, (unsigned long *) arg);
477 case LIRC_GET_SEND_MODE:
478 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
482 case LIRC_GET_REC_MODE:
483 result = put_user(LIRC_MODE_MODE2, (unsigned long *) arg);
487 case LIRC_SET_SEND_MODE:
488 result = get_user(mode, (unsigned long *) arg);
491 if (mode != LIRC_MODE_PULSE)
494 case LIRC_SET_REC_MODE:
495 result = get_user(mode, (unsigned long *) arg);
498 if (mode != LIRC_MODE_MODE2)
501 case LIRC_SET_TRANSMITTER_MASK:
502 result = get_user(ivalue, (unsigned int *) arg);
505 if ((ivalue & LIRC_PARALLEL_TRANSMITTER_MASK) != ivalue)
506 return LIRC_PARALLEL_MAX_TRANSMITTERS;
515 static int lirc_open(struct inode *node, struct file *filep)
517 if (is_open || !lirc_claim())
520 parport_enable_irq(pport);
531 static int lirc_close(struct inode *node, struct file *filep)
535 parport_release(ppdevice);
541 static const struct file_operations lirc_fops = {
542 .owner = THIS_MODULE,
543 .llseek = lirc_lseek,
547 .unlocked_ioctl = lirc_ioctl,
549 .release = lirc_close
552 static int set_use_inc(void *data)
557 static void set_use_dec(void *data)
561 static struct lirc_driver driver = {
562 .name = LIRC_DRIVER_NAME,
568 .set_use_inc = set_use_inc,
569 .set_use_dec = set_use_dec,
572 .owner = THIS_MODULE,
575 static int pf(void *handle);
576 static void kf(void *handle);
578 static struct timer_list poll_timer;
579 static void poll_state(unsigned long ignored);
581 static void poll_state(unsigned long ignored)
583 printk(KERN_NOTICE "%s: time\n",
585 del_timer(&poll_timer);
590 printk(KERN_NOTICE "%s: could not claim port, giving up\n",
592 init_timer(&poll_timer);
593 poll_timer.expires = jiffies + HZ;
594 poll_timer.data = (unsigned long)current;
595 poll_timer.function = poll_state;
596 add_timer(&poll_timer);
600 static int pf(void *handle)
602 parport_disable_irq(pport);
607 static void kf(void *handle)
613 parport_enable_irq(pport);
615 /* this is a bit annoying when you actually print...*/
617 printk(KERN_INFO "%s: reclaimed port\n", LIRC_DRIVER_NAME);
621 /*** module initialization and cleanup ***/
623 static int __init lirc_parallel_init(void)
625 pport = parport_find_base(io);
627 printk(KERN_NOTICE "%s: no port at %x found\n",
628 LIRC_DRIVER_NAME, io);
631 ppdevice = parport_register_device(pport, LIRC_DRIVER_NAME,
632 pf, kf, irq_handler, 0, NULL);
633 parport_put_port(pport);
634 if (ppdevice == NULL) {
635 printk(KERN_NOTICE "%s: parport_register_device() failed\n",
639 if (parport_claim(ppdevice) != 0)
642 out(LIRC_LP_CONTROL, LP_PSELECP|LP_PINITP);
646 out(LIRC_PORT_DATA, tx_mask);
648 timer = init_lirc_timer();
650 #if 0 /* continue even if device is offline */
653 parport_release(pport);
654 parport_unregister_device(ppdevice);
660 out(LIRC_PORT_DATA, 0);
664 parport_release(ppdevice);
666 driver.minor = lirc_register_driver(&driver);
667 if (driver.minor < 0) {
668 printk(KERN_NOTICE "%s: register_chrdev() failed\n",
670 parport_unregister_device(ppdevice);
673 printk(KERN_INFO "%s: installed using port 0x%04x irq %d\n",
674 LIRC_DRIVER_NAME, io, irq);
678 static void __exit lirc_parallel_exit(void)
680 parport_unregister_device(ppdevice);
681 lirc_unregister_driver(driver.minor);
684 module_init(lirc_parallel_init);
685 module_exit(lirc_parallel_exit);
687 MODULE_DESCRIPTION("Infrared receiver driver for parallel ports.");
688 MODULE_AUTHOR("Christoph Bartelmus");
689 MODULE_LICENSE("GPL");
691 module_param(io, int, S_IRUGO);
692 MODULE_PARM_DESC(io, "I/O address base (0x3bc, 0x378 or 0x278)");
694 module_param(irq, int, S_IRUGO);
695 MODULE_PARM_DESC(irq, "Interrupt (7 or 5)");
697 module_param(tx_mask, int, S_IRUGO);
698 MODULE_PARM_DESC(tx_maxk, "Transmitter mask (default: 0x01)");
700 module_param(debug, bool, S_IRUGO | S_IWUSR);
701 MODULE_PARM_DESC(debug, "Enable debugging messages");
703 module_param(check_pselecd, bool, S_IRUGO | S_IWUSR);
704 MODULE_PARM_DESC(debug, "Check for printer (default: 0)");