1 /* $Id: lirc_serial.c,v 5.90 2008/05/28 06:19:25 lirc Exp $ */
3 /****************************************************************************
4 ** lirc_serial.c ***********************************************************
5 ****************************************************************************
7 * lirc_serial - Device driver that records pulse- and pause-lengths
8 * (space-lengths) between DDCD event on a serial port.
10 * Copyright (C) 1996,97 Ralph Metzler <rjkm@thp.uni-koeln.de>
11 * Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>
12 * Copyright (C) 1998 Ben Pfaff <blp@gnu.org>
13 * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
14 * Copyright (C) 2007 Andrei Tanas <andrei@tanas.ca> (suspend/resume support)
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 /* Steve's changes to improve transmission fidelity:
32 - for systems with the rdtsc instruction and the clock counter, a
33 send_pule that times the pulses directly using the counter.
34 This means that the LIRC_SERIAL_TRANSMITTER_LATENCY fudge is
35 not needed. Measurement shows very stable waveform, even where
36 PCI activity slows the access to the UART, which trips up other
38 - For other system, non-integer-microsecond pulse/space lengths,
39 done using fixed point binary. So, much more accurate carrier
41 - fine tuned transmitter latency, taking advantage of fractional
42 microseconds in previous change
43 - Fixed bug in the way transmitter latency was accounted for by
44 tuning the pulse lengths down - the send_pulse routine ignored
45 this overhead as it timed the overall pulse length - so the
46 pulse frequency was right but overall pulse length was too
47 long. Fixed by accounting for latency on each pulse/space
50 Steve Davies <steve@daviesfam.org> July 2001
57 #include <linux/version.h>
58 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 18)
59 #error "**********************************************************"
60 #error " Sorry, this driver needs kernel version 2.2.18 or higher "
61 #error "**********************************************************"
64 #include <linux/autoconf.h>
66 #if defined(CONFIG_SERIAL) || defined(CONFIG_SERIAL_8250)
67 #warning "******************************************"
68 #warning " Your serial port driver is compiled into "
69 #warning " the kernel. You will have to release the "
70 #warning " port you want to use for LIRC with: "
71 #warning " setserial /dev/ttySx uart none "
72 #warning "******************************************"
75 #include <linux/module.h>
76 #include <linux/errno.h>
77 #include <linux/signal.h>
78 #include <linux/sched.h>
80 #include <linux/interrupt.h>
81 #include <linux/ioport.h>
82 #include <linux/kernel.h>
83 #include <linux/major.h>
84 #include <linux/serial_reg.h>
85 #include <linux/time.h>
86 #include <linux/string.h>
87 #include <linux/types.h>
88 #include <linux/wait.h>
90 #include <linux/delay.h>
91 #include <linux/poll.h>
92 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
93 #include <linux/platform_device.h>
96 #include <asm/system.h>
97 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
98 #include <asm/uaccess.h>
100 #include <linux/uaccess.h>
102 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
105 #include <linux/io.h>
107 #include <linux/irq.h>
108 #include <linux/fcntl.h>
110 #if defined(LIRC_SERIAL_NSLU2)
111 #include <asm/hardware.h>
112 /* From Intel IXP42X Developer's Manual (#252480-005): */
113 /* ftp://download.intel.com/design/network/manuals/25248005.pdf */
114 #define UART_IE_IXP42X_UUE 0x40 /* IXP42X UART Unit enable */
115 #define UART_IE_IXP42X_RTOIE 0x10 /* IXP42X Receiver Data Timeout int.enable */
116 #ifndef NSLU2_LED_GRN_GPIO
117 /* added in 2.6.22 */
118 #define NSLU2_LED_GRN_GPIO NSLU2_LED_GRN
122 #include "drivers/lirc.h"
123 #include "drivers/kcompat.h"
124 #include "drivers/lirc_dev/lirc_dev.h"
126 #if defined(LIRC_SERIAL_SOFTCARRIER) && !defined(LIRC_SERIAL_TRANSMITTER)
127 #warning "Software carrier only affects transmitting"
133 #warning "Note: using rdtsc instruction"
136 #ifdef LIRC_SERIAL_ANIMAX
137 #ifdef LIRC_SERIAL_TRANSMITTER
138 #warning "******************************************"
139 #warning " This receiver does not have a "
140 #warning " transmitter diode "
141 #warning "******************************************"
145 #define LIRC_DRIVER_NAME "lirc_serial"
149 int signal_pin_change
;
152 long (*send_pulse
)(unsigned long length
);
153 void (*send_space
)(long length
);
157 #define LIRC_HOMEBREW 0
159 #define LIRC_IRDEO_REMOTE 2
160 #define LIRC_ANIMAX 3
164 #ifdef LIRC_SERIAL_IRDEO
165 static int type
= LIRC_IRDEO
;
166 #elif defined(LIRC_SERIAL_IRDEO_REMOTE)
167 static int type
= LIRC_IRDEO_REMOTE
;
168 #elif defined(LIRC_SERIAL_ANIMAX)
169 static int type
= LIRC_ANIMAX
;
170 #elif defined(LIRC_SERIAL_IGOR)
171 static int type
= LIRC_IGOR
;
172 #elif defined(LIRC_SERIAL_NSLU2)
173 static int type
= LIRC_NSLU2
;
175 static int type
= LIRC_HOMEBREW
;
178 /* Set defaults for NSLU2 */
179 #if defined(LIRC_SERIAL_NSLU2)
181 #define LIRC_IRQ IRQ_IXP4XX_UART2
184 #define LIRC_PORT (IXP4XX_UART2_BASE_VIRT + REG_OFFSET)
187 #define LIRC_IOMMAP IXP4XX_UART2_BASE_PHYS
190 #define LIRC_IOSHIFT 2
192 #ifndef LIRC_ALLOW_MMAPPED_IO
193 #define LIRC_ALLOW_MMAPPED_IO
197 #if defined(LIRC_ALLOW_MMAPPED_IO)
199 #define LIRC_IOMMAP 0
202 #define LIRC_IOSHIFT 0
204 static int iommap
= LIRC_IOMMAP
;
205 static int ioshift
= LIRC_IOSHIFT
;
208 #ifdef LIRC_SERIAL_SOFTCARRIER
209 static int softcarrier
= 1;
211 static int softcarrier
;
214 static int share_irq
;
217 #define dprintk(fmt, args...) \
220 printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
224 /* forward declarations */
225 static long send_pulse_irdeo(unsigned long length
);
226 static long send_pulse_homebrew(unsigned long length
);
227 static void send_space_irdeo(long length
);
228 static void send_space_homebrew(long length
);
230 static struct lirc_serial hardware
[] = {
231 /* home-brew receiver/transmitter */
235 UART_MCR_RTS
|UART_MCR_OUT2
|UART_MCR_DTR
,
236 UART_MCR_RTS
|UART_MCR_OUT2
,
240 #ifdef LIRC_SERIAL_TRANSMITTER
241 LIRC_CAN_SET_SEND_DUTY_CYCLE
|
242 LIRC_CAN_SET_SEND_CARRIER
|
253 UART_MCR_RTS
|UART_MCR_DTR
|UART_MCR_OUT2
,
256 (LIRC_CAN_SET_SEND_DUTY_CYCLE
|
265 UART_MCR_RTS
|UART_MCR_DTR
|UART_MCR_OUT2
,
266 UART_MCR_RTS
|UART_MCR_DTR
|UART_MCR_OUT2
,
269 (LIRC_CAN_SET_SEND_DUTY_CYCLE
|
279 UART_MCR_RTS
|UART_MCR_DTR
|UART_MCR_OUT2
,
285 /* home-brew receiver/transmitter (Igor Cesko's variation) */
289 UART_MCR_RTS
|UART_MCR_OUT2
|UART_MCR_DTR
,
290 UART_MCR_RTS
|UART_MCR_OUT2
,
294 #ifdef LIRC_SERIAL_TRANSMITTER
295 LIRC_CAN_SET_SEND_DUTY_CYCLE
|
296 LIRC_CAN_SET_SEND_CARRIER
|
302 #if defined(LIRC_SERIAL_NSLU2)
303 /* Modified Linksys Network Storage Link USB 2.0 (NSLU2):
304 We receive on CTS of the 2nd serial port (R142,LHS), we
305 transmit with a IR diode between GPIO[1] (green status LED),
306 and ground (Matthias Goebl <matthias.goebl@goebl.net>).
307 See also http://www.nslu2-linux.org for this device */
311 UART_MCR_RTS
|UART_MCR_OUT2
|UART_MCR_DTR
,
312 UART_MCR_RTS
|UART_MCR_OUT2
,
316 #ifdef LIRC_SERIAL_TRANSMITTER
317 LIRC_CAN_SET_SEND_DUTY_CYCLE
|
318 LIRC_CAN_SET_SEND_CARRIER
|
327 #define RS_ISR_PASS_LIMIT 256
329 /* A long pulse code from a remote might take upto 300 bytes. The
330 daemon should read the bytes as soon as they are generated, so take
331 the number of keys you think you can push before the daemon runs
332 and multiply by 300. The driver will warn you if you overrun this
333 buffer. If you have a slow computer or non-busmastering IDE disks,
334 maybe you will need to increase this. */
336 /* This MUST be a power of two! It has to be larger than 1 as well. */
341 static int sense
= -1; /* -1 = auto, 0 = active high, 1 = active low */
342 static int txsense
; /* 0 = active high, 1 = active low */
348 #define LIRC_PORT 0x3f8
351 static int io
= LIRC_PORT
;
352 static int irq
= LIRC_IRQ
;
354 static struct timeval lasttv
= {0, 0};
356 static struct lirc_buffer rbuf
;
358 static lirc_t wbuf
[WBUF_LEN
];
360 static unsigned int freq
= 38000;
361 static unsigned int duty_cycle
= 50;
363 /* Initialized in init_timing_params() */
364 static unsigned long period
;
365 static unsigned long pulse_width
;
366 static unsigned long space_width
;
368 #if defined(__i386__)
371 Linux I/O port programming mini-HOWTO
372 Author: Riku Saikkonen <Riku.Saikkonen@hut.fi>
376 Actually, a port I/O instruction on most ports in the 0-0x3ff range
377 takes almost exactly 1 microsecond, so if you're, for example, using
378 the parallel port directly, just do additional inb()s from that port
382 /* transmitter latency 1.5625us 0x1.90 - this figure arrived at from
383 * comment above plus trimming to match actual measured frequency.
384 * This will be sensitive to cpu speed, though hopefully most of the 1.5us
385 * is spent in the uart access. Still - for reference test machine was a
386 * 1.13GHz Athlon system - Steve
389 /* changed from 400 to 450 as this works better on slower machines;
390 faster machines will use the rdtsc code anyway */
392 #define LIRC_SERIAL_TRANSMITTER_LATENCY 450
396 /* does anybody have information on other platforms ? */
398 #define LIRC_SERIAL_TRANSMITTER_LATENCY 256
400 #endif /* __i386__ */
402 static inline unsigned int sinp(int offset
)
404 #if defined(LIRC_ALLOW_MMAPPED_IO)
406 /* the register is memory-mapped */
408 return readb(io
+ offset
);
411 return inb(io
+ offset
);
414 static inline void soutp(int offset
, int value
)
416 #if defined(LIRC_ALLOW_MMAPPED_IO)
418 /* the register is memory-mapped */
420 writeb(value
, io
+ offset
);
423 outb(value
, io
+ offset
);
426 static inline void on(void)
428 #if defined(LIRC_SERIAL_NSLU2)
429 /* On NSLU2, we put the transmit diode between the output of the green
430 status LED and ground */
431 if (type
== LIRC_NSLU2
) {
432 gpio_line_set(NSLU2_LED_GRN_GPIO
, IXP4XX_GPIO_LOW
);
437 soutp(UART_MCR
, hardware
[type
].off
);
439 soutp(UART_MCR
, hardware
[type
].on
);
442 static inline void off(void)
444 #if defined(LIRC_SERIAL_NSLU2)
445 if (type
== LIRC_NSLU2
) {
446 gpio_line_set(NSLU2_LED_GRN_GPIO
, IXP4XX_GPIO_HIGH
);
451 soutp(UART_MCR
, hardware
[type
].on
);
453 soutp(UART_MCR
, hardware
[type
].off
);
456 #ifndef MAX_UDELAY_MS
457 #define MAX_UDELAY_US 5000
459 #define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
462 static inline void safe_udelay(unsigned long usecs
)
464 while (usecs
> MAX_UDELAY_US
) {
465 udelay(MAX_UDELAY_US
);
466 usecs
-= MAX_UDELAY_US
;
472 /* This is an overflow/precision juggle, complicated in that we can't
473 do long long divide in the kernel */
475 /* When we use the rdtsc instruction to measure clocks, we keep the
476 * pulse and space widths as clock cycles. As this is CPU speed
477 * dependent, the widths must be calculated in init_port and ioctl
481 /* So send_pulse can quickly convert microseconds to clocks */
482 static unsigned long conv_us_to_clocks
;
484 static inline int init_timing_params(unsigned int new_duty_cycle
,
485 unsigned int new_freq
)
487 unsigned long long loops_per_sec
, work
;
489 duty_cycle
= new_duty_cycle
;
492 loops_per_sec
= current_cpu_data
.loops_per_jiffy
;
495 /* How many clocks in a microsecond?, avoiding long long divide */
496 work
= loops_per_sec
;
497 work
*= 4295; /* 4295 = 2^32 / 1e6 */
498 conv_us_to_clocks
= (work
>>32);
500 /* Carrier period in clocks, approach good up to 32GHz clock,
501 gets carrier frequency within 8Hz */
502 period
= loops_per_sec
>>3;
505 /* Derive pulse and space from the period */
507 pulse_width
= period
*duty_cycle
/100;
508 space_width
= period
- pulse_width
;
509 dprintk("in init_timing_params, freq=%d, duty_cycle=%d, "
510 "clk/jiffy=%ld, pulse=%ld, space=%ld, "
511 "conv_us_to_clocks=%ld\n",
512 freq
, duty_cycle
, current_cpu_data
.loops_per_jiffy
,
513 pulse_width
, space_width
, conv_us_to_clocks
);
516 #else /* ! USE_RDTSC */
517 static inline int init_timing_params(unsigned int new_duty_cycle
,
518 unsigned int new_freq
)
520 /* period, pulse/space width are kept with 8 binary places -
521 * IE multiplied by 256. */
522 if (256*1000000L/new_freq
*new_duty_cycle
/100 <=
523 LIRC_SERIAL_TRANSMITTER_LATENCY
)
525 if (256*1000000L/new_freq
*(100-new_duty_cycle
)/100 <=
526 LIRC_SERIAL_TRANSMITTER_LATENCY
)
528 duty_cycle
= new_duty_cycle
;
530 period
= 256*1000000L/freq
;
531 pulse_width
= period
*duty_cycle
/100;
532 space_width
= period
-pulse_width
;
533 dprintk("in init_timing_params, freq=%d pulse=%ld, "
534 "space=%ld\n", freq
, pulse_width
, space_width
);
537 #endif /* USE_RDTSC */
540 /* return value: space length delta */
542 static long send_pulse_irdeo(unsigned long length
)
546 unsigned char output
;
547 unsigned char chunk
, shifted
;
549 /* how many bits have to be sent ? */
550 rawbits
= length
*1152/10000;
555 for (i
= 0, output
= 0x7f; rawbits
> 0; rawbits
-= 3) {
556 shifted
= chunk
<<(i
*3);
558 output
&= (~shifted
);
561 soutp(UART_TX
, output
);
562 while (!(sinp(UART_LSR
) & UART_LSR_THRE
));
568 soutp(UART_TX
, output
);
569 while (!(sinp(UART_LSR
) & UART_LSR_TEMT
));
573 return ((-rawbits
)*10000/1152);
575 return ((3-i
)*3*10000/1152+(-rawbits
)*10000/1152);
579 /* Version that uses Pentium rdtsc instruction to measure clocks */
581 /* This version does sub-microsecond timing using rdtsc instruction,
582 * and does away with the fudged LIRC_SERIAL_TRANSMITTER_LATENCY
583 * Implicitly i586 architecture... - Steve
586 static inline long send_pulse_homebrew_softcarrier(unsigned long length
)
589 unsigned long target
, start
, now
;
591 /* Get going quick as we can */
593 /* Convert length from microseconds to clocks */
594 length
*= conv_us_to_clocks
;
595 /* And loop till time is up - flipping at right intervals */
597 target
= pulse_width
;
599 while ((now
-start
) < length
) {
600 /* Delay till flip time */
603 while ((now
-start
) < target
);
608 target
+= space_width
;
611 target
+= pulse_width
;
616 return (((now
-start
)-length
)/conv_us_to_clocks
);
618 #else /* ! USE_RDTSC */
619 /* Version using udelay() */
621 /* here we use fixed point arithmetic, with 8
622 fractional bits. that gets us within 0.1% or so of the right average
623 frequency, albeit with some jitter in pulse length - Steve */
625 /* To match 8 fractional bits used for pulse/space length */
627 static inline long send_pulse_homebrew_softcarrier(unsigned long length
)
630 unsigned long actual
, target
, d
;
633 actual
= 0; target
= 0; flag
= 0;
634 while (actual
< length
) {
637 target
+= space_width
;
640 target
+= pulse_width
;
642 d
= (target
-actual
-LIRC_SERIAL_TRANSMITTER_LATENCY
+128)>>8;
643 /* Note - we've checked in ioctl that the pulse/space
644 widths are big enough so that d is > 0 */
646 actual
+= (d
<<8)+LIRC_SERIAL_TRANSMITTER_LATENCY
;
649 return ((actual
-length
)>>8);
651 #endif /* USE_RDTSC */
653 static long send_pulse_homebrew(unsigned long length
)
659 return send_pulse_homebrew_softcarrier(length
);
667 static void send_space_irdeo(long length
)
675 static void send_space_homebrew(long length
)
683 static inline void rbwrite(lirc_t l
)
685 if (lirc_buffer_full(&rbuf
)) {
686 /* no new signals will be accepted */
687 dprintk("Buffer overrun\n");
690 _lirc_buffer_write_1(&rbuf
, (void *)&l
);
693 static inline void frbwrite(lirc_t l
)
695 /* simple noise filter */
696 static lirc_t pulse
= 0L, space
= 0L;
697 static unsigned int ptr
;
699 if (ptr
> 0 && (l
&PULSE_BIT
)) {
700 pulse
+= l
&PULSE_MASK
;
703 rbwrite(pulse
|PULSE_BIT
);
709 if (!(l
&PULSE_BIT
)) {
719 if (space
> PULSE_MASK
)
722 if (space
> PULSE_MASK
)
728 rbwrite(pulse
|PULSE_BIT
);
736 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
737 static irqreturn_t
irq_handler(int i
, void *blah
)
739 static irqreturn_t
irq_handler(int i
, void *blah
, struct pt_regs
*regs
)
743 int status
, counter
, dcd
;
746 static int last_dcd
= -1;
748 if ((sinp(UART_IIR
) & UART_IIR_NO_INT
)) {
749 /* not our interrupt */
750 return IRQ_RETVAL(IRQ_NONE
);
756 status
= sinp(UART_MSR
);
757 if (counter
> RS_ISR_PASS_LIMIT
) {
758 printk(KERN_WARNING LIRC_DRIVER_NAME
": AIEEEE: "
762 if ((status
&hardware
[type
].signal_pin_change
) && sense
!= -1) {
763 /* get current time */
764 do_gettimeofday(&tv
);
766 /* New mode, written by Trent Piepho
767 <xyzzy@u.washington.edu>. */
769 /* The old format was not very portable.
770 We now use the type lirc_t to pass pulses
771 and spaces to user space.
773 If PULSE_BIT is set a pulse has been
774 received, otherwise a space has been
775 received. The driver needs to know if your
776 receiver is active high or active low, or
777 the space/pulse sense could be
778 inverted. The bits denoted by PULSE_MASK are
779 the length in microseconds. Lengths greater
780 than or equal to 16 seconds are clamped to
781 PULSE_MASK. All other bits are unused.
782 This is a much simpler interface for user
783 programs, as well as eliminating "out of
784 phase" errors with space/pulse
787 /* calculate time since last interrupt in
789 dcd
= (status
& hardware
[type
].signal_pin
) ? 1:0;
791 if (dcd
== last_dcd
) {
792 printk(KERN_WARNING LIRC_DRIVER_NAME
793 ": ignoring spike: %d %d %lx %lx %lx %lx\n",
795 tv
.tv_sec
, lasttv
.tv_sec
,
796 tv
.tv_usec
, lasttv
.tv_usec
);
800 deltv
= tv
.tv_sec
-lasttv
.tv_sec
;
801 if (tv
.tv_sec
< lasttv
.tv_sec
||
802 (tv
.tv_sec
== lasttv
.tv_sec
&&
803 tv
.tv_usec
< lasttv
.tv_usec
)) {
804 printk(KERN_WARNING LIRC_DRIVER_NAME
805 ": AIEEEE: your clock just jumped "
807 printk(KERN_WARNING LIRC_DRIVER_NAME
808 ": %d %d %lx %lx %lx %lx\n",
810 tv
.tv_sec
, lasttv
.tv_sec
,
811 tv
.tv_usec
, lasttv
.tv_usec
);
813 } else if (deltv
> 15) {
814 data
= PULSE_MASK
; /* really long time */
817 printk(KERN_WARNING LIRC_DRIVER_NAME
819 "%d %d %lx %lx %lx %lx\n",
821 tv
.tv_sec
, lasttv
.tv_sec
,
822 tv
.tv_usec
, lasttv
.tv_usec
);
823 /* detecting pulse while this
828 data
= (lirc_t
) (deltv
*1000000 +
831 frbwrite(dcd
^sense
? data
: (data
|PULSE_BIT
));
834 wake_up_interruptible(&rbuf
.wait_poll
);
836 } while (!(sinp(UART_IIR
) & UART_IIR_NO_INT
)); /* still pending ? */
837 return IRQ_RETVAL(IRQ_HANDLED
);
840 static void hardware_init_port(void)
843 local_irq_save(flags
);
846 soutp(UART_LCR
, sinp(UART_LCR
) & (~UART_LCR_DLAB
));
848 /* First of all, disable all interrupts */
849 soutp(UART_IER
, sinp(UART_IER
) &
850 (~(UART_IER_MSI
|UART_IER_RLSI
|UART_IER_THRI
|UART_IER_RDI
)));
852 /* Clear registers. */
858 #if defined(LIRC_SERIAL_NSLU2)
859 if (type
== LIRC_NSLU2
) {
860 /* Setup NSLU2 UART */
863 soutp(UART_IER
, sinp(UART_IER
) | UART_IE_IXP42X_UUE
);
864 /* Disable Receiver data Time out interrupt */
865 soutp(UART_IER
, sinp(UART_IER
) & ~UART_IE_IXP42X_RTOIE
);
866 /* set out2 = interupt unmask; off() doesn't set MCR
868 soutp(UART_MCR
, UART_MCR_RTS
|UART_MCR_OUT2
);
872 /* Set line for power source */
875 /* Clear registers again to be sure. */
883 case LIRC_IRDEO_REMOTE
:
884 /* setup port to 7N1 @ 115200 Baud */
885 /* 7N1+start = 9 bits at 115200 ~ 3 bits at 38kHz */
888 soutp(UART_LCR
, sinp(UART_LCR
) | UART_LCR_DLAB
);
889 /* Set divisor to 1 => 115200 Baud */
892 /* Set DLAB 0 + 7N1 */
893 soutp(UART_LCR
, UART_LCR_WLEN7
);
894 /* THR interrupt already disabled at this point */
900 local_irq_restore(flags
);
903 static int init_port(void)
907 /* Reserve io region. */
908 #if defined(LIRC_ALLOW_MMAPPED_IO)
909 /* Future MMAP-Developers: Attention!
910 For memory mapped I/O you *might* need to use ioremap() first,
911 for the NSLU2 it's done in boot code. */
913 && (request_mem_region(iommap
, 8<<ioshift
,
914 LIRC_DRIVER_NAME
) == NULL
))
916 && (request_region(io
, 8, LIRC_DRIVER_NAME
) == NULL
))) {
918 if (request_region(io
, 8, LIRC_DRIVER_NAME
) == NULL
) {
920 printk(KERN_ERR LIRC_DRIVER_NAME
921 ": port %04x already in use\n", io
);
922 printk(KERN_WARNING LIRC_DRIVER_NAME
923 ": use 'setserial /dev/ttySX uart none'\n");
924 printk(KERN_WARNING LIRC_DRIVER_NAME
925 ": or compile the serial port driver as module and\n");
926 printk(KERN_WARNING LIRC_DRIVER_NAME
927 ": make sure this module is loaded first\n");
931 hardware_init_port();
933 /* Initialize pulse/space widths */
934 init_timing_params(duty_cycle
, freq
);
936 /* If pin is high, then this must be an active low receiver. */
938 /* wait 1/2 sec for the power supply */
940 set_current_state(TASK_INTERRUPTIBLE
);
941 schedule_timeout(HZ
/2);
943 /* probe 9 times every 0.04s, collect "votes" for
947 for (i
= 0; i
< 9; i
++) {
948 if (sinp(UART_MSR
) & hardware
[type
].signal_pin
)
952 schedule_timeout(HZ
/25);
954 sense
= (nlow
>= nhigh
? 1 : 0);
955 printk(KERN_INFO LIRC_DRIVER_NAME
": auto-detected active "
956 "%s receiver\n", sense
? "low":"high");
958 printk(KERN_INFO LIRC_DRIVER_NAME
": Manually using active "
959 "%s receiver\n", sense
? "low":"high");
964 static int set_use_inc(void *data
)
969 /* Init read buffer. */
970 if (lirc_buffer_init(&rbuf
, sizeof(lirc_t
), RBUF_LEN
) < 0)
973 /* initialize timestamp */
974 do_gettimeofday(&lasttv
);
976 result
= request_irq(irq
, irq_handler
,
977 IRQF_DISABLED
| (share_irq
? IRQF_SHARED
:0),
978 LIRC_DRIVER_NAME
, (void *)&hardware
);
982 printk(KERN_ERR LIRC_DRIVER_NAME
": IRQ %d busy\n", irq
);
983 lirc_buffer_free(&rbuf
);
986 printk(KERN_ERR LIRC_DRIVER_NAME
987 ": Bad irq number or handler\n");
988 lirc_buffer_free(&rbuf
);
991 dprintk("Interrupt %d, port %04x obtained\n", irq
, io
);
995 local_irq_save(flags
);
998 soutp(UART_LCR
, sinp(UART_LCR
) & (~UART_LCR_DLAB
));
1000 soutp(UART_IER
, sinp(UART_IER
)|UART_IER_MSI
);
1002 local_irq_restore(flags
);
1008 static void set_use_dec(void *data
)
1009 { unsigned long flags
;
1011 local_irq_save(flags
);
1014 soutp(UART_LCR
, sinp(UART_LCR
) & (~UART_LCR_DLAB
));
1016 /* First of all, disable all interrupts */
1017 soutp(UART_IER
, sinp(UART_IER
) &
1018 (~(UART_IER_MSI
|UART_IER_RLSI
|UART_IER_THRI
|UART_IER_RDI
)));
1019 local_irq_restore(flags
);
1021 free_irq(irq
, (void *)&hardware
);
1023 dprintk("freed IRQ %d\n", irq
);
1024 lirc_buffer_free(&rbuf
);
1029 static ssize_t
lirc_write(struct file
*file
, const char *buf
,
1030 size_t n
, loff_t
*ppos
)
1033 unsigned long flags
;
1036 if (!(hardware
[type
].features
&LIRC_CAN_SEND_PULSE
))
1039 if (n
% sizeof(lirc_t
))
1041 count
= n
/ sizeof(lirc_t
);
1042 if (count
> WBUF_LEN
|| count
% 2 == 0)
1044 if (copy_from_user(wbuf
, buf
, n
))
1046 local_irq_save(flags
);
1047 if (type
== LIRC_IRDEO
) {
1051 for (i
= 0; i
< count
; i
++) {
1053 hardware
[type
].send_space(wbuf
[i
]-delta
);
1055 delta
= hardware
[type
].send_pulse(wbuf
[i
]);
1058 local_irq_restore(flags
);
1062 static int lirc_ioctl(struct inode
*node
, struct file
*filep
, unsigned int cmd
,
1066 unsigned long value
;
1067 unsigned int ivalue
;
1070 case LIRC_GET_SEND_MODE
:
1071 if (!(hardware
[type
].features
&LIRC_CAN_SEND_MASK
))
1072 return -ENOIOCTLCMD
;
1074 result
= put_user(LIRC_SEND2MODE
1075 (hardware
[type
].features
&LIRC_CAN_SEND_MASK
),
1076 (unsigned long *) arg
);
1081 case LIRC_SET_SEND_MODE
:
1082 if (!(hardware
[type
].features
&LIRC_CAN_SEND_MASK
))
1083 return -ENOIOCTLCMD
;
1085 result
= get_user(value
, (unsigned long *) arg
);
1088 /* only LIRC_MODE_PULSE supported */
1089 if (value
!= LIRC_MODE_PULSE
)
1093 case LIRC_GET_LENGTH
:
1097 case LIRC_SET_SEND_DUTY_CYCLE
:
1098 dprintk("SET_SEND_DUTY_CYCLE\n");
1099 if (!(hardware
[type
].features
&LIRC_CAN_SET_SEND_DUTY_CYCLE
))
1100 return -ENOIOCTLCMD
;
1102 result
= get_user(ivalue
, (unsigned int *) arg
);
1105 if (ivalue
<= 0 || ivalue
> 100)
1107 return init_timing_params(ivalue
, freq
);
1110 case LIRC_SET_SEND_CARRIER
:
1111 dprintk("SET_SEND_CARRIER\n");
1112 if (!(hardware
[type
].features
&LIRC_CAN_SET_SEND_CARRIER
))
1113 return -ENOIOCTLCMD
;
1115 result
= get_user(ivalue
, (unsigned int *) arg
);
1118 if (ivalue
> 500000 || ivalue
< 20000)
1120 return init_timing_params(duty_cycle
, ivalue
);
1124 return -ENOIOCTLCMD
;
1129 static struct file_operations lirc_fops
= {
1130 .write
= lirc_write
,
1133 static struct lirc_plugin plugin
= {
1134 .name
= LIRC_DRIVER_NAME
,
1142 .set_use_inc
= set_use_inc
,
1143 .set_use_dec
= set_use_dec
,
1144 .ioctl
= lirc_ioctl
,
1147 .owner
= THIS_MODULE
,
1152 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
1153 static struct platform_device
*lirc_serial_dev
;
1155 static int __devinit
lirc_serial_probe(struct platform_device
*dev
)
1160 static int __devexit
lirc_serial_remove(struct platform_device
*dev
)
1165 static int lirc_serial_suspend(struct platform_device
*dev
,
1169 soutp(UART_LCR
, sinp(UART_LCR
) & (~UART_LCR_DLAB
));
1171 /* Disable all interrupts */
1172 soutp(UART_IER
, sinp(UART_IER
) &
1173 (~(UART_IER_MSI
|UART_IER_RLSI
|UART_IER_THRI
|UART_IER_RDI
)));
1175 /* Clear registers. */
1184 static int lirc_serial_resume(struct platform_device
*dev
)
1186 unsigned long flags
;
1188 hardware_init_port();
1190 local_irq_save(flags
);
1191 /* Enable Interrupt */
1192 do_gettimeofday(&lasttv
);
1193 soutp(UART_IER
, sinp(UART_IER
)|UART_IER_MSI
);
1196 lirc_buffer_clear(&rbuf
);
1198 local_irq_restore(flags
);
1203 static struct platform_driver lirc_serial_driver
= {
1204 .probe
= lirc_serial_probe
,
1205 .remove
= __devexit_p(lirc_serial_remove
),
1206 .suspend
= lirc_serial_suspend
,
1207 .resume
= lirc_serial_resume
,
1209 .name
= "lirc_serial",
1210 .owner
= THIS_MODULE
,
1214 static int __init
lirc_serial_init(void)
1218 result
= platform_driver_register(&lirc_serial_driver
);
1220 printk("lirc register returned %d\n", result
);
1224 lirc_serial_dev
= platform_device_alloc("lirc_serial", 0);
1225 if (!lirc_serial_dev
) {
1227 goto exit_driver_unregister
;
1230 result
= platform_device_add(lirc_serial_dev
);
1232 goto exit_device_put
;
1237 platform_device_put(lirc_serial_dev
);
1238 exit_driver_unregister
:
1239 platform_driver_unregister(&lirc_serial_driver
);
1243 static void __exit
lirc_serial_exit(void)
1245 platform_device_unregister(lirc_serial_dev
);
1246 platform_driver_unregister(&lirc_serial_driver
);
1250 int __init
init_module(void)
1254 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
1255 result
= lirc_serial_init();
1262 case LIRC_IRDEO_REMOTE
:
1265 #if defined(LIRC_SERIAL_NSLU2)
1271 goto exit_serial_exit
;
1278 hardware
[type
].features
&=
1279 ~(LIRC_CAN_SET_SEND_DUTY_CYCLE
|
1280 LIRC_CAN_SET_SEND_CARRIER
);
1284 result
= init_port();
1286 goto exit_serial_exit
;
1287 plugin
.features
= hardware
[type
].features
;
1288 plugin
.minor
= lirc_register_plugin(&plugin
);
1289 if (plugin
.minor
< 0) {
1290 printk(KERN_ERR LIRC_DRIVER_NAME
1291 ": register_chrdev failed!\n");
1297 release_region(io
, 8);
1299 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
1305 void __exit
cleanup_module(void)
1307 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
1310 #if defined(LIRC_ALLOW_MMAPPED_IO)
1312 release_mem_region(iommap
, 8<<ioshift
);
1314 release_region(io
, 8);
1316 release_region(io
, 8);
1318 lirc_unregister_plugin(plugin
.minor
);
1319 dprintk("cleaned up module\n");
1322 MODULE_DESCRIPTION("Infra-red receiver driver for serial ports.");
1323 MODULE_AUTHOR("Ralph Metzler, Trent Piepho, Ben Pfaff, "
1324 "Christoph Bartelmus, Andrei Tanas");
1325 MODULE_LICENSE("GPL");
1327 module_param(type
, int, 0444);
1328 #if defined(LIRC_SERIAL_NSLU2)
1329 MODULE_PARM_DESC(type
, "Hardware type (0 = home-brew, 1 = IRdeo,"
1330 " 2 = IRdeo Remote, 3 = AnimaX, 4 = IgorPlug,"
1331 " 5 = NSLU2 RX:CTS2/TX:GreenLED)");
1333 MODULE_PARM_DESC(type
, "Hardware type (0 = home-brew, 1 = IRdeo,"
1334 " 2 = IRdeo Remote, 3 = AnimaX, 4 = IgorPlug)");
1337 module_param(io
, int, 0444);
1338 MODULE_PARM_DESC(io
, "I/O address base (0x3f8 or 0x2f8)");
1340 #if defined(LIRC_ALLOW_MMAPPED_IO)
1341 /* some architectures (e.g. intel xscale) have memory mapped registers */
1342 module_param(iommap
, bool, 0444);
1343 MODULE_PARM_DESC(iommap
, "physical base for memory mapped I/O"
1344 " (0 = no memory mapped io)");
1346 /* some architectures (e.g. intel xscale) align the 8bit serial registers
1347 on 32bit word boundaries.
1348 See linux-kernel/drivers/serial/8250.c serial_in()/out() */
1349 module_param(ioshift
, int, 0444);
1350 MODULE_PARM_DESC(ioshift
, "shift I/O register offset (0 = no shift)");
1353 module_param(irq
, int, 0444);
1354 MODULE_PARM_DESC(irq
, "Interrupt (4 or 3)");
1356 module_param(share_irq
, bool, 0444);
1357 MODULE_PARM_DESC(share_irq
, "Share interrupts (0 = off, 1 = on)");
1359 module_param(sense
, bool, 0444);
1360 MODULE_PARM_DESC(sense
, "Override autodetection of IR receiver circuit"
1361 " (0 = active high, 1 = active low )");
1363 #ifdef LIRC_SERIAL_TRANSMITTER
1364 module_param(txsense
, bool, 0444);
1365 MODULE_PARM_DESC(txsense
, "Sense of transmitter circuit"
1366 " (0 = active high, 1 = active low )");
1369 module_param(softcarrier
, bool, 0444);
1370 MODULE_PARM_DESC(softcarrier
, "Software carrier (0 = off, 1 = on)");
1372 module_param(debug
, bool, 0644);
1373 MODULE_PARM_DESC(debug
, "Enable debugging messages");