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
27 #include <linux/module.h>
28 #include <linux/sched.h>
29 #include <linux/errno.h>
30 #include <linux/signal.h>
32 #include <linux/kernel.h>
33 #include <linux/ioport.h>
34 #include <linux/time.h>
36 #include <linux/delay.h>
39 #include <linux/irq.h>
40 #include <linux/uaccess.h>
41 #include <asm/div64.h>
43 #include <linux/poll.h>
44 #include <linux/parport.h>
46 #include <media/lirc.h>
47 #include <media/lirc_dev.h>
49 #include "lirc_parallel.h"
51 #define LIRC_DRIVER_NAME "lirc_parallel"
57 #define LIRC_PORT 0x378
60 #define LIRC_TIMER 65536
63 /*** Global Variables ***/
66 static int check_pselecd;
68 unsigned int irq = LIRC_IRQ;
69 unsigned int io = LIRC_PORT;
72 unsigned int default_timer = LIRC_TIMER;
75 #define RBUF_SIZE (256) /* this must be a power of 2 larger than 1 */
77 static int rbuf[RBUF_SIZE];
79 DECLARE_WAIT_QUEUE_HEAD(lirc_wait);
83 unsigned int lost_irqs;
86 struct parport *pport;
87 struct pardevice *ppdevice;
90 unsigned int tx_mask = 1;
92 /*** Internal Functions ***/
94 static unsigned int in(int offset)
98 return parport_read_data(pport);
100 return parport_read_status(pport);
101 case LIRC_LP_CONTROL:
102 return parport_read_control(pport);
104 return 0; /* make compiler happy */
107 static void out(int offset, int value)
111 parport_write_data(pport, value);
113 case LIRC_LP_CONTROL:
114 parport_write_control(pport, value);
117 printk(KERN_INFO "%s: attempt to write to status register\n",
123 static unsigned int lirc_get_timer(void)
125 return in(LIRC_PORT_TIMER) & LIRC_PORT_TIMER_BIT;
128 static unsigned int lirc_get_signal(void)
130 return in(LIRC_PORT_SIGNAL) & LIRC_PORT_SIGNAL_BIT;
133 static void lirc_on(void)
135 out(LIRC_PORT_DATA, tx_mask);
138 static void lirc_off(void)
140 out(LIRC_PORT_DATA, 0);
143 static unsigned int init_lirc_timer(void)
145 struct timeval tv, now;
146 unsigned int level, newlevel, timeelapsed, newtimer;
149 do_gettimeofday(&tv);
150 tv.tv_sec++; /* wait max. 1 sec. */
151 level = lirc_get_timer();
153 newlevel = lirc_get_timer();
154 if (level == 0 && newlevel != 0)
157 do_gettimeofday(&now);
158 } while (count < 1000 && (now.tv_sec < tv.tv_sec
159 || (now.tv_sec == tv.tv_sec
160 && now.tv_usec < tv.tv_usec)));
162 timeelapsed = ((now.tv_sec + 1 - tv.tv_sec)*1000000
163 + (now.tv_usec - tv.tv_usec));
164 if (count >= 1000 && timeelapsed > 0) {
165 if (default_timer == 0) {
166 /* autodetect timer */
167 newtimer = (1000000*count)/timeelapsed;
168 printk(KERN_INFO "%s: %u Hz timer detected\n",
169 LIRC_DRIVER_NAME, newtimer);
172 newtimer = (1000000*count)/timeelapsed;
173 if (abs(newtimer - default_timer) > default_timer/10) {
175 printk(KERN_NOTICE "%s: bad timer: %u Hz\n",
176 LIRC_DRIVER_NAME, newtimer);
177 printk(KERN_NOTICE "%s: using default timer: "
179 LIRC_DRIVER_NAME, default_timer);
180 return default_timer;
182 printk(KERN_INFO "%s: %u Hz timer detected\n",
183 LIRC_DRIVER_NAME, newtimer);
184 return newtimer; /* use detected value */
188 printk(KERN_NOTICE "%s: no timer detected\n", LIRC_DRIVER_NAME);
193 static int lirc_claim(void)
195 if (parport_claim(ppdevice) != 0) {
196 printk(KERN_WARNING "%s: could not claim port\n",
198 printk(KERN_WARNING "%s: waiting for port becoming available"
199 "\n", LIRC_DRIVER_NAME);
200 if (parport_claim_or_block(ppdevice) < 0) {
201 printk(KERN_NOTICE "%s: could not claim port, giving"
202 " up\n", LIRC_DRIVER_NAME);
206 out(LIRC_LP_CONTROL, LP_PSELECP|LP_PINITP);
211 /*** interrupt handler ***/
213 static void rbuf_write(int signal)
217 nwptr = (wptr + 1) & (RBUF_SIZE - 1);
219 /* no new signals will be accepted */
221 printk(KERN_NOTICE "%s: buffer overrun\n", LIRC_DRIVER_NAME);
228 static void irq_handler(void *blah)
231 static struct timeval lasttv;
235 unsigned int level, newlevel;
236 unsigned int timeout;
245 /* disable interrupt */
247 out(LIRC_PORT_IRQ, in(LIRC_PORT_IRQ) & (~LP_PINTEN));
249 if (check_pselecd && (in(1) & LP_PSELECD))
254 do_gettimeofday(&tv);
256 signal = tv.tv_sec - lasttv.tv_sec;
258 /* really long time */
261 data = (int) (signal*1000000 +
262 tv.tv_usec - lasttv.tv_usec +
265 rbuf_write(data); /* space */
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); */
279 timeout = timer/10; /* timeout after 1/10 sec. */
281 level = lirc_get_timer();
283 newlevel = lirc_get_timer();
284 if (level == 0 && newlevel != 0)
290 || (check_pselecd && (in(1) & LP_PSELECD))) {
292 printk(KERN_NOTICE "%s: timeout\n", LIRC_DRIVER_NAME);
295 } while (lirc_get_signal());
298 /* ajust value to usecs */
301 helper = ((__u64) signal)*1000000;
302 do_div(helper, timer);
303 signal = (long) helper;
305 if (signal > LIRC_SFH506_DELAY)
306 data = signal - LIRC_SFH506_DELAY;
309 rbuf_write(PULSE_BIT|data); /* pulse */
311 do_gettimeofday(&lasttv);
313 /* add your code here */
316 wake_up_interruptible(&lirc_wait);
318 /* enable interrupt */
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)
332 static ssize_t lirc_read(struct file *filep, char *buf, size_t n, loff_t *ppos)
336 DECLARE_WAITQUEUE(wait, current);
341 add_wait_queue(&lirc_wait, &wait);
342 set_current_state(TASK_INTERRUPTIBLE);
345 if (copy_to_user(buf+count, (char *) &rbuf[rptr],
350 rptr = (rptr + 1) & (RBUF_SIZE - 1);
351 count += sizeof(int);
353 if (filep->f_flags & O_NONBLOCK) {
357 if (signal_pending(current)) {
358 result = -ERESTARTSYS;
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,
375 unsigned int level, newlevel;
383 count = n / sizeof(int);
385 if (n % sizeof(int) || count % 2 == 0)
388 wbuf = memdup_user(buf, n);
390 return PTR_ERR(wbuf);
394 /* try again if device is ready */
395 timer = init_lirc_timer();
400 /* adjust values from usecs */
401 for (i = 0; i < count; i++) {
404 helper = ((__u64) wbuf[i])*timer;
405 do_div(helper, 1000000);
406 wbuf[i] = (int) helper;
409 local_irq_save(flags);
412 level = lirc_get_timer();
416 newlevel = lirc_get_timer();
417 if (level == 0 && newlevel != 0)
420 if (check_pselecd && (in(1) & LP_PSELECD)) {
422 local_irq_restore(flags);
425 } while (counttimer < wbuf[i]);
433 newlevel = lirc_get_timer();
434 if (level == 0 && newlevel != 0)
437 if (check_pselecd && (in(1) & LP_PSELECD)) {
438 local_irq_restore(flags);
441 } while (counttimer < wbuf[i]);
444 local_irq_restore(flags);
446 /* place code that handles write without external timer here */
451 static unsigned int lirc_poll(struct file *file, poll_table *wait)
453 poll_wait(file, &lirc_wait, wait);
455 return POLLIN | POLLRDNORM;
459 static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
462 __u32 features = LIRC_CAN_SET_TRANSMITTER_MASK |
463 LIRC_CAN_SEND_PULSE | LIRC_CAN_REC_MODE2;
468 case LIRC_GET_FEATURES:
469 result = put_user(features, (__u32 *) arg);
473 case LIRC_GET_SEND_MODE:
474 result = put_user(LIRC_MODE_PULSE, (__u32 *) arg);
478 case LIRC_GET_REC_MODE:
479 result = put_user(LIRC_MODE_MODE2, (__u32 *) arg);
483 case LIRC_SET_SEND_MODE:
484 result = get_user(mode, (__u32 *) arg);
487 if (mode != LIRC_MODE_PULSE)
490 case LIRC_SET_REC_MODE:
491 result = get_user(mode, (__u32 *) arg);
494 if (mode != LIRC_MODE_MODE2)
497 case LIRC_SET_TRANSMITTER_MASK:
498 result = get_user(value, (__u32 *) arg);
501 if ((value & LIRC_PARALLEL_TRANSMITTER_MASK) != value)
502 return LIRC_PARALLEL_MAX_TRANSMITTERS;
511 static int lirc_open(struct inode *node, struct file *filep)
513 if (is_open || !lirc_claim())
516 parport_enable_irq(pport);
527 static int lirc_close(struct inode *node, struct file *filep)
531 parport_release(ppdevice);
537 static const struct file_operations lirc_fops = {
538 .owner = THIS_MODULE,
539 .llseek = lirc_lseek,
543 .unlocked_ioctl = lirc_ioctl,
545 .compat_ioctl = lirc_ioctl,
548 .release = lirc_close
551 static int set_use_inc(void *data)
556 static void set_use_dec(void *data)
560 static struct lirc_driver driver = {
561 .name = LIRC_DRIVER_NAME,
567 .set_use_inc = set_use_inc,
568 .set_use_dec = set_use_dec,
571 .owner = THIS_MODULE,
574 static int pf(void *handle);
575 static void kf(void *handle);
577 static int pf(void *handle)
579 parport_disable_irq(pport);
584 static void kf(void *handle)
590 parport_enable_irq(pport);
592 /* this is a bit annoying when you actually print...*/
594 printk(KERN_INFO "%s: reclaimed port\n", LIRC_DRIVER_NAME);
598 /*** module initialization and cleanup ***/
600 static int __init lirc_parallel_init(void)
602 pport = parport_find_base(io);
604 printk(KERN_NOTICE "%s: no port at %x found\n",
605 LIRC_DRIVER_NAME, io);
608 ppdevice = parport_register_device(pport, LIRC_DRIVER_NAME,
609 pf, kf, irq_handler, 0, NULL);
610 parport_put_port(pport);
611 if (ppdevice == NULL) {
612 printk(KERN_NOTICE "%s: parport_register_device() failed\n",
616 if (parport_claim(ppdevice) != 0)
619 out(LIRC_LP_CONTROL, LP_PSELECP|LP_PINITP);
623 out(LIRC_PORT_DATA, tx_mask);
625 timer = init_lirc_timer();
627 #if 0 /* continue even if device is offline */
630 parport_release(pport);
631 parport_unregister_device(ppdevice);
637 out(LIRC_PORT_DATA, 0);
641 parport_release(ppdevice);
643 driver.minor = lirc_register_driver(&driver);
644 if (driver.minor < 0) {
645 printk(KERN_NOTICE "%s: register_chrdev() failed\n",
647 parport_unregister_device(ppdevice);
650 printk(KERN_INFO "%s: installed using port 0x%04x irq %d\n",
651 LIRC_DRIVER_NAME, io, irq);
655 static void __exit lirc_parallel_exit(void)
657 parport_unregister_device(ppdevice);
658 lirc_unregister_driver(driver.minor);
661 module_init(lirc_parallel_init);
662 module_exit(lirc_parallel_exit);
664 MODULE_DESCRIPTION("Infrared receiver driver for parallel ports.");
665 MODULE_AUTHOR("Christoph Bartelmus");
666 MODULE_LICENSE("GPL");
668 module_param(io, int, S_IRUGO);
669 MODULE_PARM_DESC(io, "I/O address base (0x3bc, 0x378 or 0x278)");
671 module_param(irq, int, S_IRUGO);
672 MODULE_PARM_DESC(irq, "Interrupt (7 or 5)");
674 module_param(tx_mask, int, S_IRUGO);
675 MODULE_PARM_DESC(tx_maxk, "Transmitter mask (default: 0x01)");
677 module_param(debug, bool, S_IRUGO | S_IWUSR);
678 MODULE_PARM_DESC(debug, "Enable debugging messages");
680 module_param(check_pselecd, bool, S_IRUGO | S_IWUSR);
681 MODULE_PARM_DESC(debug, "Check for printer (default: 0)");