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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #include <linux/module.h>
30 #include <linux/sched.h>
31 #include <linux/errno.h>
32 #include <linux/signal.h>
34 #include <linux/kernel.h>
35 #include <linux/ioport.h>
36 #include <linux/ktime.h>
38 #include <linux/delay.h>
41 #include <linux/irq.h>
42 #include <linux/uaccess.h>
43 #include <asm/div64.h>
45 #include <linux/poll.h>
46 #include <linux/parport.h>
47 #include <linux/platform_device.h>
49 #include <media/lirc.h>
50 #include <media/lirc_dev.h>
52 #include "lirc_parallel.h"
54 #define LIRC_DRIVER_NAME "lirc_parallel"
60 #define LIRC_PORT 0x378
63 #define LIRC_TIMER 65536
66 /*** Global Variables ***/
69 static bool check_pselecd
;
71 static unsigned int irq
= LIRC_IRQ
;
72 static unsigned int io
= LIRC_PORT
;
74 static unsigned int timer
;
75 static unsigned int default_timer
= LIRC_TIMER
;
78 #define RBUF_SIZE (256) /* this must be a power of 2 larger than 1 */
80 static int rbuf
[RBUF_SIZE
];
82 static DECLARE_WAIT_QUEUE_HEAD(lirc_wait
);
84 static unsigned int rptr
;
85 static unsigned int wptr
;
86 static unsigned int lost_irqs
;
89 static struct parport
*pport
;
90 static struct pardevice
*ppdevice
;
91 static int is_claimed
;
93 static unsigned int tx_mask
= 1;
95 /*** Internal Functions ***/
97 static unsigned int in(int offset
)
101 return parport_read_data(pport
);
103 return parport_read_status(pport
);
104 case LIRC_LP_CONTROL
:
105 return parport_read_control(pport
);
107 return 0; /* make compiler happy */
110 static void out(int offset
, int value
)
114 parport_write_data(pport
, value
);
116 case LIRC_LP_CONTROL
:
117 parport_write_control(pport
, value
);
120 pr_info("attempt to write to status register\n");
125 static unsigned int lirc_get_timer(void)
127 return in(LIRC_PORT_TIMER
) & LIRC_PORT_TIMER_BIT
;
130 static unsigned int lirc_get_signal(void)
132 return in(LIRC_PORT_SIGNAL
) & LIRC_PORT_SIGNAL_BIT
;
135 static void lirc_on(void)
137 out(LIRC_PORT_DATA
, tx_mask
);
140 static void lirc_off(void)
142 out(LIRC_PORT_DATA
, 0);
145 static unsigned int init_lirc_timer(void)
147 ktime_t kt
, now
, timeout
;
148 unsigned int level
, newlevel
, timeelapsed
, newtimer
;
152 /* wait max. 1 sec. */
153 timeout
= ktime_add_ns(kt
, NSEC_PER_SEC
);
154 level
= lirc_get_timer();
156 newlevel
= lirc_get_timer();
157 if (level
== 0 && newlevel
!= 0)
161 } while (count
< 1000 && (ktime_before(now
, timeout
)));
162 timeelapsed
= ktime_us_delta(now
, kt
);
163 if (count
>= 1000 && timeelapsed
> 0) {
164 if (default_timer
== 0) {
165 /* autodetect timer */
166 newtimer
= (1000000 * count
) / timeelapsed
;
167 pr_info("%u Hz timer detected\n", newtimer
);
170 newtimer
= (1000000 * count
) / timeelapsed
;
171 if (abs(newtimer
- default_timer
) > default_timer
/ 10) {
173 pr_notice("bad timer: %u Hz\n", newtimer
);
174 pr_notice("using default timer: %u Hz\n",
176 return default_timer
;
178 pr_info("%u Hz timer detected\n", newtimer
);
179 return newtimer
; /* use detected value */
182 pr_notice("no timer detected\n");
186 static int lirc_claim(void)
188 if (parport_claim(ppdevice
) != 0) {
189 pr_warn("could not claim port\n");
190 pr_warn("waiting for port becoming available\n");
191 if (parport_claim_or_block(ppdevice
) < 0) {
192 pr_notice("could not claim port, giving up\n");
196 out(LIRC_LP_CONTROL
, LP_PSELECP
| LP_PINITP
);
201 /*** interrupt handler ***/
203 static void rbuf_write(int signal
)
207 nwptr
= (wptr
+ 1) & (RBUF_SIZE
- 1);
209 /* no new signals will be accepted */
211 pr_notice("buffer overrun\n");
218 static void lirc_lirc_irq_handler(void *blah
)
221 static ktime_t lastkt
;
225 unsigned int level
, newlevel
;
226 unsigned int timeout
;
235 /* disable interrupt */
237 out(LIRC_PORT_IRQ
, in(LIRC_PORT_IRQ
) & (~LP_PINTEN
));
239 if (check_pselecd
&& (in(1) & LP_PSELECD
))
246 delkt
= ktime_sub(kt
, lastkt
);
247 if (ktime_compare(delkt
, ktime_set(15, 0)) > 0)
248 /* really long time */
251 data
= (int)(ktime_to_us(delkt
) + LIRC_SFH506_DELAY
);
253 rbuf_write(data
); /* space */
257 * wake up; we'll lose this signal, but it will be
258 * garbage if the device is turned on anyway
260 timer
= init_lirc_timer();
261 /* enable_irq(irq); */
267 timeout
= timer
/ 10; /* timeout after 1/10 sec. */
269 level
= lirc_get_timer();
271 newlevel
= lirc_get_timer();
272 if (level
== 0 && newlevel
!= 0)
278 || (check_pselecd
&& (in(1) & LP_PSELECD
))) {
280 pr_notice("timeout\n");
283 } while (lirc_get_signal());
286 /* adjust value to usecs */
289 helper
= ((__u64
)signal
) * 1000000;
290 do_div(helper
, timer
);
291 signal
= (long)helper
;
293 if (signal
> LIRC_SFH506_DELAY
)
294 data
= signal
- LIRC_SFH506_DELAY
;
297 rbuf_write(PULSE_BIT
| data
); /* pulse */
299 lastkt
= ktime_get();
301 /* add your code here */
304 wake_up_interruptible(&lirc_wait
);
306 /* enable interrupt */
309 * out(LIRC_PORT_IRQ, in(LIRC_PORT_IRQ)|LP_PINTEN);
313 /*** file operations ***/
315 static loff_t
lirc_lseek(struct file
*filep
, loff_t offset
, int orig
)
320 static ssize_t
lirc_read(struct file
*filep
, char __user
*buf
, size_t n
,
325 DECLARE_WAITQUEUE(wait
, current
);
330 add_wait_queue(&lirc_wait
, &wait
);
331 set_current_state(TASK_INTERRUPTIBLE
);
334 if (copy_to_user(buf
+ count
, &rbuf
[rptr
],
339 rptr
= (rptr
+ 1) & (RBUF_SIZE
- 1);
340 count
+= sizeof(int);
342 if (filep
->f_flags
& O_NONBLOCK
) {
346 if (signal_pending(current
)) {
347 result
= -ERESTARTSYS
;
351 set_current_state(TASK_INTERRUPTIBLE
);
354 remove_wait_queue(&lirc_wait
, &wait
);
355 set_current_state(TASK_RUNNING
);
356 return count
? count
: result
;
359 static ssize_t
lirc_write(struct file
*filep
, const char __user
*buf
, size_t n
,
364 unsigned int level
, newlevel
;
373 count
= n
/ sizeof(int);
375 if (n
% sizeof(int) || count
% 2 == 0)
378 wbuf
= memdup_user(buf
, n
);
380 return PTR_ERR(wbuf
);
384 /* try again if device is ready */
385 timer
= init_lirc_timer();
392 /* adjust values from usecs */
393 for (i
= 0; i
< count
; i
++) {
396 helper
= ((__u64
)wbuf
[i
]) * timer
;
397 do_div(helper
, 1000000);
398 wbuf
[i
] = (int)helper
;
401 local_irq_save(flags
);
404 level
= lirc_get_timer();
408 newlevel
= lirc_get_timer();
409 if (level
== 0 && newlevel
!= 0)
412 if (check_pselecd
&& (in(1) & LP_PSELECD
)) {
414 local_irq_restore(flags
);
418 } while (counttimer
< wbuf
[i
]);
426 newlevel
= lirc_get_timer();
427 if (level
== 0 && newlevel
!= 0)
430 if (check_pselecd
&& (in(1) & LP_PSELECD
)) {
431 local_irq_restore(flags
);
435 } while (counttimer
< wbuf
[i
]);
438 local_irq_restore(flags
);
440 /* place code that handles write without external timer here */
449 static unsigned int lirc_poll(struct file
*file
, poll_table
*wait
)
451 poll_wait(file
, &lirc_wait
, wait
);
453 return POLLIN
| POLLRDNORM
;
457 static long lirc_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
460 u32 __user
*uptr
= (u32 __user
*)arg
;
461 u32 features
= LIRC_CAN_SET_TRANSMITTER_MASK
|
462 LIRC_CAN_SEND_PULSE
| LIRC_CAN_REC_MODE2
;
467 case LIRC_GET_FEATURES
:
468 result
= put_user(features
, uptr
);
472 case LIRC_GET_SEND_MODE
:
473 result
= put_user(LIRC_MODE_PULSE
, uptr
);
477 case LIRC_GET_REC_MODE
:
478 result
= put_user(LIRC_MODE_MODE2
, uptr
);
482 case LIRC_SET_SEND_MODE
:
483 result
= get_user(mode
, uptr
);
486 if (mode
!= LIRC_MODE_PULSE
)
489 case LIRC_SET_REC_MODE
:
490 result
= get_user(mode
, uptr
);
493 if (mode
!= LIRC_MODE_MODE2
)
496 case LIRC_SET_TRANSMITTER_MASK
:
497 result
= get_user(value
, uptr
);
500 if ((value
& LIRC_PARALLEL_TRANSMITTER_MASK
) != value
)
501 return LIRC_PARALLEL_MAX_TRANSMITTERS
;
510 static int lirc_open(struct inode
*node
, struct file
*filep
)
512 if (is_open
|| !lirc_claim())
515 parport_enable_irq(pport
);
526 static int lirc_close(struct inode
*node
, struct file
*filep
)
530 parport_release(ppdevice
);
536 static const struct file_operations lirc_fops
= {
537 .owner
= THIS_MODULE
,
538 .llseek
= lirc_lseek
,
542 .unlocked_ioctl
= lirc_ioctl
,
544 .compat_ioctl
= lirc_ioctl
,
547 .release
= lirc_close
550 static int set_use_inc(void *data
)
555 static void set_use_dec(void *data
)
559 static struct lirc_driver driver
= {
560 .name
= LIRC_DRIVER_NAME
,
566 .set_use_inc
= set_use_inc
,
567 .set_use_dec
= set_use_dec
,
570 .owner
= THIS_MODULE
,
573 static struct platform_device
*lirc_parallel_dev
;
575 static int lirc_parallel_probe(struct platform_device
*dev
)
580 static int lirc_parallel_remove(struct platform_device
*dev
)
585 static int lirc_parallel_suspend(struct platform_device
*dev
,
591 static int lirc_parallel_resume(struct platform_device
*dev
)
596 static struct platform_driver lirc_parallel_driver
= {
597 .probe
= lirc_parallel_probe
,
598 .remove
= lirc_parallel_remove
,
599 .suspend
= lirc_parallel_suspend
,
600 .resume
= lirc_parallel_resume
,
602 .name
= LIRC_DRIVER_NAME
,
606 static int pf(void *handle
)
608 parport_disable_irq(pport
);
613 static void kf(void *handle
)
619 parport_enable_irq(pport
);
621 /* this is a bit annoying when you actually print...*/
623 * printk(KERN_INFO "%s: reclaimed port\n", LIRC_DRIVER_NAME);
627 /*** module initialization and cleanup ***/
629 static int __init
lirc_parallel_init(void)
633 result
= platform_driver_register(&lirc_parallel_driver
);
635 pr_notice("platform_driver_register returned %d\n", result
);
639 lirc_parallel_dev
= platform_device_alloc(LIRC_DRIVER_NAME
, 0);
640 if (!lirc_parallel_dev
) {
642 goto exit_driver_unregister
;
645 result
= platform_device_add(lirc_parallel_dev
);
647 goto exit_device_put
;
649 pport
= parport_find_base(io
);
651 pr_notice("no port at %x found\n", io
);
653 goto exit_device_del
;
655 ppdevice
= parport_register_device(pport
, LIRC_DRIVER_NAME
,
656 pf
, kf
, lirc_lirc_irq_handler
, 0,
658 parport_put_port(pport
);
660 pr_notice("parport_register_device() failed\n");
662 goto exit_device_del
;
664 if (parport_claim(ppdevice
) != 0)
667 out(LIRC_LP_CONTROL
, LP_PSELECP
| LP_PINITP
);
671 out(LIRC_PORT_DATA
, tx_mask
);
673 timer
= init_lirc_timer();
675 #if 0 /* continue even if device is offline */
678 parport_release(pport
);
679 parport_unregister_device(ppdevice
);
681 goto exit_device_del
;
686 out(LIRC_PORT_DATA
, 0);
690 parport_release(ppdevice
);
692 driver
.dev
= &lirc_parallel_dev
->dev
;
693 driver
.minor
= lirc_register_driver(&driver
);
694 if (driver
.minor
< 0) {
695 pr_notice("register_chrdev() failed\n");
696 parport_unregister_device(ppdevice
);
698 goto exit_device_del
;
700 pr_info("installed using port 0x%04x irq %d\n", io
, irq
);
704 platform_device_del(lirc_parallel_dev
);
706 platform_device_put(lirc_parallel_dev
);
707 exit_driver_unregister
:
708 platform_driver_unregister(&lirc_parallel_driver
);
712 static void __exit
lirc_parallel_exit(void)
714 parport_unregister_device(ppdevice
);
715 lirc_unregister_driver(driver
.minor
);
717 platform_device_unregister(lirc_parallel_dev
);
718 platform_driver_unregister(&lirc_parallel_driver
);
721 module_init(lirc_parallel_init
);
722 module_exit(lirc_parallel_exit
);
724 MODULE_DESCRIPTION("Infrared receiver driver for parallel ports.");
725 MODULE_AUTHOR("Christoph Bartelmus");
726 MODULE_LICENSE("GPL");
728 module_param(io
, int, S_IRUGO
);
729 MODULE_PARM_DESC(io
, "I/O address base (0x3bc, 0x378 or 0x278)");
731 module_param(irq
, int, S_IRUGO
);
732 MODULE_PARM_DESC(irq
, "Interrupt (7 or 5)");
734 module_param(tx_mask
, int, S_IRUGO
);
735 MODULE_PARM_DESC(tx_mask
, "Transmitter mask (default: 0x01)");
737 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
738 MODULE_PARM_DESC(debug
, "Enable debugging messages");
740 module_param(check_pselecd
, bool, S_IRUGO
| S_IWUSR
);
741 MODULE_PARM_DESC(check_pselecd
, "Check for printer (default: 0)");