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>
45 #include <linux/platform_device.h>
47 #include <media/lirc.h>
48 #include <media/lirc_dev.h>
50 #include "lirc_parallel.h"
52 #define LIRC_DRIVER_NAME "lirc_parallel"
58 #define LIRC_PORT 0x378
61 #define LIRC_TIMER 65536
64 /*** Global Variables ***/
67 static int check_pselecd;
69 unsigned int irq = LIRC_IRQ;
70 unsigned int io = LIRC_PORT;
73 unsigned int default_timer = LIRC_TIMER;
76 #define RBUF_SIZE (256) /* this must be a power of 2 larger than 1 */
78 static int rbuf[RBUF_SIZE];
80 DECLARE_WAIT_QUEUE_HEAD(lirc_wait);
84 unsigned int lost_irqs;
87 struct parport *pport;
88 struct pardevice *ppdevice;
91 unsigned int tx_mask = 1;
93 /*** Internal Functions ***/
95 static unsigned int in(int offset)
99 return parport_read_data(pport);
101 return parport_read_status(pport);
102 case LIRC_LP_CONTROL:
103 return parport_read_control(pport);
105 return 0; /* make compiler happy */
108 static void out(int offset, int value)
112 parport_write_data(pport, value);
114 case LIRC_LP_CONTROL:
115 parport_write_control(pport, value);
118 printk(KERN_INFO "%s: attempt to write to status register\n",
124 static unsigned int lirc_get_timer(void)
126 return in(LIRC_PORT_TIMER) & LIRC_PORT_TIMER_BIT;
129 static unsigned int lirc_get_signal(void)
131 return in(LIRC_PORT_SIGNAL) & LIRC_PORT_SIGNAL_BIT;
134 static void lirc_on(void)
136 out(LIRC_PORT_DATA, tx_mask);
139 static void lirc_off(void)
141 out(LIRC_PORT_DATA, 0);
144 static unsigned int init_lirc_timer(void)
146 struct timeval tv, now;
147 unsigned int level, newlevel, timeelapsed, newtimer;
150 do_gettimeofday(&tv);
151 tv.tv_sec++; /* wait max. 1 sec. */
152 level = lirc_get_timer();
154 newlevel = lirc_get_timer();
155 if (level == 0 && newlevel != 0)
158 do_gettimeofday(&now);
159 } while (count < 1000 && (now.tv_sec < tv.tv_sec
160 || (now.tv_sec == tv.tv_sec
161 && now.tv_usec < tv.tv_usec)));
163 timeelapsed = ((now.tv_sec + 1 - tv.tv_sec)*1000000
164 + (now.tv_usec - tv.tv_usec));
165 if (count >= 1000 && timeelapsed > 0) {
166 if (default_timer == 0) {
167 /* autodetect timer */
168 newtimer = (1000000*count)/timeelapsed;
169 printk(KERN_INFO "%s: %u Hz timer detected\n",
170 LIRC_DRIVER_NAME, newtimer);
173 newtimer = (1000000*count)/timeelapsed;
174 if (abs(newtimer - default_timer) > default_timer/10) {
176 printk(KERN_NOTICE "%s: bad timer: %u Hz\n",
177 LIRC_DRIVER_NAME, newtimer);
178 printk(KERN_NOTICE "%s: using default timer: "
180 LIRC_DRIVER_NAME, default_timer);
181 return default_timer;
183 printk(KERN_INFO "%s: %u Hz timer detected\n",
184 LIRC_DRIVER_NAME, newtimer);
185 return newtimer; /* use detected value */
189 printk(KERN_NOTICE "%s: no timer detected\n", LIRC_DRIVER_NAME);
194 static int lirc_claim(void)
196 if (parport_claim(ppdevice) != 0) {
197 printk(KERN_WARNING "%s: could not claim port\n",
199 printk(KERN_WARNING "%s: waiting for port becoming available"
200 "\n", LIRC_DRIVER_NAME);
201 if (parport_claim_or_block(ppdevice) < 0) {
202 printk(KERN_NOTICE "%s: could not claim port, giving"
203 " up\n", LIRC_DRIVER_NAME);
207 out(LIRC_LP_CONTROL, LP_PSELECP|LP_PINITP);
212 /*** interrupt handler ***/
214 static void rbuf_write(int signal)
218 nwptr = (wptr + 1) & (RBUF_SIZE - 1);
220 /* no new signals will be accepted */
222 printk(KERN_NOTICE "%s: buffer overrun\n", LIRC_DRIVER_NAME);
229 static void irq_handler(void *blah)
232 static struct timeval lasttv;
236 unsigned int level, newlevel;
237 unsigned int timeout;
246 /* disable interrupt */
248 out(LIRC_PORT_IRQ, in(LIRC_PORT_IRQ) & (~LP_PINTEN));
250 if (check_pselecd && (in(1) & LP_PSELECD))
255 do_gettimeofday(&tv);
257 signal = tv.tv_sec - lasttv.tv_sec;
259 /* really long time */
262 data = (int) (signal*1000000 +
263 tv.tv_usec - lasttv.tv_usec +
266 rbuf_write(data); /* space */
270 * wake up; we'll lose this signal, but it will be
271 * garbage if the device is turned on anyway
273 timer = init_lirc_timer();
274 /* enable_irq(irq); */
280 timeout = timer/10; /* timeout after 1/10 sec. */
282 level = lirc_get_timer();
284 newlevel = lirc_get_timer();
285 if (level == 0 && newlevel != 0)
291 || (check_pselecd && (in(1) & LP_PSELECD))) {
293 printk(KERN_NOTICE "%s: timeout\n", LIRC_DRIVER_NAME);
296 } while (lirc_get_signal());
299 /* adjust value to usecs */
302 helper = ((__u64) signal)*1000000;
303 do_div(helper, timer);
304 signal = (long) helper;
306 if (signal > LIRC_SFH506_DELAY)
307 data = signal - LIRC_SFH506_DELAY;
310 rbuf_write(PULSE_BIT|data); /* pulse */
312 do_gettimeofday(&lasttv);
314 /* add your code here */
317 wake_up_interruptible(&lirc_wait);
319 /* enable interrupt */
322 out(LIRC_PORT_IRQ, in(LIRC_PORT_IRQ)|LP_PINTEN);
326 /*** file operations ***/
328 static loff_t lirc_lseek(struct file *filep, loff_t offset, int orig)
333 static ssize_t lirc_read(struct file *filep, char *buf, size_t n, loff_t *ppos)
337 DECLARE_WAITQUEUE(wait, current);
342 add_wait_queue(&lirc_wait, &wait);
343 set_current_state(TASK_INTERRUPTIBLE);
346 if (copy_to_user(buf+count, (char *) &rbuf[rptr],
351 rptr = (rptr + 1) & (RBUF_SIZE - 1);
352 count += sizeof(int);
354 if (filep->f_flags & O_NONBLOCK) {
358 if (signal_pending(current)) {
359 result = -ERESTARTSYS;
363 set_current_state(TASK_INTERRUPTIBLE);
366 remove_wait_queue(&lirc_wait, &wait);
367 set_current_state(TASK_RUNNING);
368 return count ? count : result;
371 static ssize_t lirc_write(struct file *filep, const char *buf, size_t n,
376 unsigned int level, newlevel;
385 count = n / sizeof(int);
387 if (n % sizeof(int) || count % 2 == 0)
390 wbuf = memdup_user(buf, n);
392 return PTR_ERR(wbuf);
396 /* try again if device is ready */
397 timer = init_lirc_timer();
404 /* adjust values from usecs */
405 for (i = 0; i < count; i++) {
408 helper = ((__u64) 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);
430 } while (counttimer < wbuf[i]);
438 newlevel = lirc_get_timer();
439 if (level == 0 && newlevel != 0)
442 if (check_pselecd && (in(1) & LP_PSELECD)) {
443 local_irq_restore(flags);
447 } while (counttimer < wbuf[i]);
450 local_irq_restore(flags);
452 /* place code that handles write without external timer here */
461 static unsigned int lirc_poll(struct file *file, poll_table *wait)
463 poll_wait(file, &lirc_wait, wait);
465 return POLLIN | POLLRDNORM;
469 static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
472 __u32 features = LIRC_CAN_SET_TRANSMITTER_MASK |
473 LIRC_CAN_SEND_PULSE | LIRC_CAN_REC_MODE2;
478 case LIRC_GET_FEATURES:
479 result = put_user(features, (__u32 *) arg);
483 case LIRC_GET_SEND_MODE:
484 result = put_user(LIRC_MODE_PULSE, (__u32 *) arg);
488 case LIRC_GET_REC_MODE:
489 result = put_user(LIRC_MODE_MODE2, (__u32 *) arg);
493 case LIRC_SET_SEND_MODE:
494 result = get_user(mode, (__u32 *) arg);
497 if (mode != LIRC_MODE_PULSE)
500 case LIRC_SET_REC_MODE:
501 result = get_user(mode, (__u32 *) arg);
504 if (mode != LIRC_MODE_MODE2)
507 case LIRC_SET_TRANSMITTER_MASK:
508 result = get_user(value, (__u32 *) arg);
511 if ((value & LIRC_PARALLEL_TRANSMITTER_MASK) != value)
512 return LIRC_PARALLEL_MAX_TRANSMITTERS;
521 static int lirc_open(struct inode *node, struct file *filep)
523 if (is_open || !lirc_claim())
526 parport_enable_irq(pport);
537 static int lirc_close(struct inode *node, struct file *filep)
541 parport_release(ppdevice);
547 static const struct file_operations lirc_fops = {
548 .owner = THIS_MODULE,
549 .llseek = lirc_lseek,
553 .unlocked_ioctl = lirc_ioctl,
555 .compat_ioctl = lirc_ioctl,
558 .release = lirc_close
561 static int set_use_inc(void *data)
566 static void set_use_dec(void *data)
570 static struct lirc_driver driver = {
571 .name = LIRC_DRIVER_NAME,
577 .set_use_inc = set_use_inc,
578 .set_use_dec = set_use_dec,
581 .owner = THIS_MODULE,
584 static struct platform_device *lirc_parallel_dev;
586 static int __devinit lirc_parallel_probe(struct platform_device *dev)
591 static int __devexit lirc_parallel_remove(struct platform_device *dev)
596 static int lirc_parallel_suspend(struct platform_device *dev,
602 static int lirc_parallel_resume(struct platform_device *dev)
607 static struct platform_driver lirc_parallel_driver = {
608 .probe = lirc_parallel_probe,
609 .remove = __devexit_p(lirc_parallel_remove),
610 .suspend = lirc_parallel_suspend,
611 .resume = lirc_parallel_resume,
613 .name = LIRC_DRIVER_NAME,
614 .owner = THIS_MODULE,
618 static int pf(void *handle)
620 parport_disable_irq(pport);
625 static void kf(void *handle)
631 parport_enable_irq(pport);
633 /* this is a bit annoying when you actually print...*/
635 printk(KERN_INFO "%s: reclaimed port\n", LIRC_DRIVER_NAME);
639 /*** module initialization and cleanup ***/
641 static int __init lirc_parallel_init(void)
645 result = platform_driver_register(&lirc_parallel_driver);
647 printk(KERN_NOTICE "platform_driver_register"
648 " returned %d\n", result);
652 lirc_parallel_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
653 if (!lirc_parallel_dev) {
655 goto exit_driver_unregister;
658 result = platform_device_add(lirc_parallel_dev);
660 goto exit_device_put;
662 pport = parport_find_base(io);
664 printk(KERN_NOTICE "%s: no port at %x found\n",
665 LIRC_DRIVER_NAME, io);
667 goto exit_device_put;
669 ppdevice = parport_register_device(pport, LIRC_DRIVER_NAME,
670 pf, kf, irq_handler, 0, NULL);
671 parport_put_port(pport);
672 if (ppdevice == NULL) {
673 printk(KERN_NOTICE "%s: parport_register_device() failed\n",
676 goto exit_device_put;
678 if (parport_claim(ppdevice) != 0)
681 out(LIRC_LP_CONTROL, LP_PSELECP|LP_PINITP);
685 out(LIRC_PORT_DATA, tx_mask);
687 timer = init_lirc_timer();
689 #if 0 /* continue even if device is offline */
692 parport_release(pport);
693 parport_unregister_device(ppdevice);
695 goto exit_device_put;
700 out(LIRC_PORT_DATA, 0);
704 parport_release(ppdevice);
706 driver.dev = &lirc_parallel_dev->dev;
707 driver.minor = lirc_register_driver(&driver);
708 if (driver.minor < 0) {
709 printk(KERN_NOTICE "%s: register_chrdev() failed\n",
711 parport_unregister_device(ppdevice);
713 goto exit_device_put;
715 printk(KERN_INFO "%s: installed using port 0x%04x irq %d\n",
716 LIRC_DRIVER_NAME, io, irq);
720 platform_device_put(lirc_parallel_dev);
721 exit_driver_unregister:
722 platform_driver_unregister(&lirc_parallel_driver);
726 static void __exit lirc_parallel_exit(void)
728 parport_unregister_device(ppdevice);
729 lirc_unregister_driver(driver.minor);
731 platform_device_unregister(lirc_parallel_dev);
732 platform_driver_unregister(&lirc_parallel_driver);
735 module_init(lirc_parallel_init);
736 module_exit(lirc_parallel_exit);
738 MODULE_DESCRIPTION("Infrared receiver driver for parallel ports.");
739 MODULE_AUTHOR("Christoph Bartelmus");
740 MODULE_LICENSE("GPL");
742 module_param(io, int, S_IRUGO);
743 MODULE_PARM_DESC(io, "I/O address base (0x3bc, 0x378 or 0x278)");
745 module_param(irq, int, S_IRUGO);
746 MODULE_PARM_DESC(irq, "Interrupt (7 or 5)");
748 module_param(tx_mask, int, S_IRUGO);
749 MODULE_PARM_DESC(tx_maxk, "Transmitter mask (default: 0x01)");
751 module_param(debug, bool, S_IRUGO | S_IWUSR);
752 MODULE_PARM_DESC(debug, "Enable debugging messages");
754 module_param(check_pselecd, bool, S_IRUGO | S_IWUSR);
755 MODULE_PARM_DESC(debug, "Check for printer (default: 0)");