Added lirc.
[irreco.git] / lirc-0.8.4a / drivers / lirc_serial / lirc_serial.c
blob2860d0af3a6fcbab1e9a80ac1b01879f721dd974
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
37 versions.
38 - For other system, non-integer-microsecond pulse/space lengths,
39 done using fixed point binary. So, much more accurate carrier
40 frequency.
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
48 iteration.
50 Steve Davies <steve@daviesfam.org> July 2001
53 #ifdef HAVE_CONFIG_H
54 # include <config.h>
55 #endif
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 "**********************************************************"
62 #endif
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 "******************************************"
73 #endif
75 #include <linux/module.h>
76 #include <linux/errno.h>
77 #include <linux/signal.h>
78 #include <linux/sched.h>
79 #include <linux/fs.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>
89 #include <linux/mm.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>
94 #endif
96 #include <asm/system.h>
97 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
98 #include <asm/uaccess.h>
99 #else
100 #include <linux/uaccess.h>
101 #endif
102 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
103 #include <asm/io.h>
104 #else
105 #include <linux/io.h>
106 #endif
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
119 #endif
120 #endif
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"
128 #endif
130 #if defined(rdtscl)
132 #define USE_RDTSC
133 #warning "Note: using rdtsc instruction"
134 #endif
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 "******************************************"
142 #endif
143 #endif
145 #define LIRC_DRIVER_NAME "lirc_serial"
147 struct lirc_serial {
148 int signal_pin;
149 int signal_pin_change;
150 int on;
151 int off;
152 long (*send_pulse)(unsigned long length);
153 void (*send_space)(long length);
154 int features;
157 #define LIRC_HOMEBREW 0
158 #define LIRC_IRDEO 1
159 #define LIRC_IRDEO_REMOTE 2
160 #define LIRC_ANIMAX 3
161 #define LIRC_IGOR 4
162 #define LIRC_NSLU2 5
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;
174 #else
175 static int type = LIRC_HOMEBREW;
176 #endif
178 /* Set defaults for NSLU2 */
179 #if defined(LIRC_SERIAL_NSLU2)
180 #ifndef LIRC_IRQ
181 #define LIRC_IRQ IRQ_IXP4XX_UART2
182 #endif
183 #ifndef LIRC_PORT
184 #define LIRC_PORT (IXP4XX_UART2_BASE_VIRT + REG_OFFSET)
185 #endif
186 #ifndef LIRC_IOMMAP
187 #define LIRC_IOMMAP IXP4XX_UART2_BASE_PHYS
188 #endif
189 #ifndef LIRC_IOSHIFT
190 #define LIRC_IOSHIFT 2
191 #endif
192 #ifndef LIRC_ALLOW_MMAPPED_IO
193 #define LIRC_ALLOW_MMAPPED_IO
194 #endif
195 #endif
197 #if defined(LIRC_ALLOW_MMAPPED_IO)
198 #ifndef LIRC_IOMMAP
199 #define LIRC_IOMMAP 0
200 #endif
201 #ifndef LIRC_IOSHIFT
202 #define LIRC_IOSHIFT 0
203 #endif
204 static int iommap = LIRC_IOMMAP;
205 static int ioshift = LIRC_IOSHIFT;
206 #endif
208 #ifdef LIRC_SERIAL_SOFTCARRIER
209 static int softcarrier = 1;
210 #else
211 static int softcarrier;
212 #endif
214 static int share_irq;
215 static int debug;
217 #define dprintk(fmt, args...) \
218 do { \
219 if (debug) \
220 printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
221 fmt, ## args); \
222 } while (0)
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 */
233 UART_MSR_DCD,
234 UART_MSR_DDCD,
235 UART_MCR_RTS|UART_MCR_OUT2|UART_MCR_DTR,
236 UART_MCR_RTS|UART_MCR_OUT2,
237 send_pulse_homebrew,
238 send_space_homebrew,
240 #ifdef LIRC_SERIAL_TRANSMITTER
241 LIRC_CAN_SET_SEND_DUTY_CYCLE|
242 LIRC_CAN_SET_SEND_CARRIER|
243 LIRC_CAN_SEND_PULSE|
244 #endif
245 LIRC_CAN_REC_MODE2)
248 /* IRdeo classic */
250 UART_MSR_DSR,
251 UART_MSR_DDSR,
252 UART_MCR_OUT2,
253 UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
254 send_pulse_irdeo,
255 send_space_irdeo,
256 (LIRC_CAN_SET_SEND_DUTY_CYCLE|
257 LIRC_CAN_SEND_PULSE|
258 LIRC_CAN_REC_MODE2)
261 /* IRdeo remote */
263 UART_MSR_DSR,
264 UART_MSR_DDSR,
265 UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
266 UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
267 send_pulse_irdeo,
268 send_space_irdeo,
269 (LIRC_CAN_SET_SEND_DUTY_CYCLE|
270 LIRC_CAN_SEND_PULSE|
271 LIRC_CAN_REC_MODE2)
274 /* AnimaX */
276 UART_MSR_DCD,
277 UART_MSR_DDCD,
279 UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
280 NULL,
281 NULL,
282 LIRC_CAN_REC_MODE2
285 /* home-brew receiver/transmitter (Igor Cesko's variation) */
287 UART_MSR_DSR,
288 UART_MSR_DDSR,
289 UART_MCR_RTS|UART_MCR_OUT2|UART_MCR_DTR,
290 UART_MCR_RTS|UART_MCR_OUT2,
291 send_pulse_homebrew,
292 send_space_homebrew,
294 #ifdef LIRC_SERIAL_TRANSMITTER
295 LIRC_CAN_SET_SEND_DUTY_CYCLE|
296 LIRC_CAN_SET_SEND_CARRIER|
297 LIRC_CAN_SEND_PULSE|
298 #endif
299 LIRC_CAN_REC_MODE2)
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 */
309 UART_MSR_CTS,
310 UART_MSR_DCTS,
311 UART_MCR_RTS|UART_MCR_OUT2|UART_MCR_DTR,
312 UART_MCR_RTS|UART_MCR_OUT2,
313 send_pulse_homebrew,
314 send_space_homebrew,
316 #ifdef LIRC_SERIAL_TRANSMITTER
317 LIRC_CAN_SET_SEND_DUTY_CYCLE|
318 LIRC_CAN_SET_SEND_CARRIER|
319 LIRC_CAN_SEND_PULSE|
320 #endif
321 LIRC_CAN_REC_MODE2)
323 #endif
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. */
338 #define RBUF_LEN 256
339 #define WBUF_LEN 256
341 static int sense = -1; /* -1 = auto, 0 = active high, 1 = active low */
342 static int txsense; /* 0 = active high, 1 = active low */
344 #ifndef LIRC_IRQ
345 #define LIRC_IRQ 4
346 #endif
347 #ifndef LIRC_PORT
348 #define LIRC_PORT 0x3f8
349 #endif
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__)
370 From:
371 Linux I/O port programming mini-HOWTO
372 Author: Riku Saikkonen <Riku.Saikkonen@hut.fi>
373 v, 28 December 1997
375 [...]
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
379 to delay.
380 [...]
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
394 #else
396 /* does anybody have information on other platforms ? */
397 /* 256 = 1<<8 */
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)
405 if (iommap != 0) {
406 /* the register is memory-mapped */
407 offset <<= ioshift;
408 return readb(io + offset);
410 #endif
411 return inb(io + offset);
414 static inline void soutp(int offset, int value)
416 #if defined(LIRC_ALLOW_MMAPPED_IO)
417 if (iommap != 0) {
418 /* the register is memory-mapped */
419 offset <<= ioshift;
420 writeb(value, io + offset);
422 #endif
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);
433 return;
435 #endif
436 if (txsense)
437 soutp(UART_MCR, hardware[type].off);
438 else
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);
447 return;
449 #endif
450 if (txsense)
451 soutp(UART_MCR, hardware[type].on);
452 else
453 soutp(UART_MCR, hardware[type].off);
456 #ifndef MAX_UDELAY_MS
457 #define MAX_UDELAY_US 5000
458 #else
459 #define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
460 #endif
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;
468 udelay(usecs);
471 #ifdef USE_RDTSC
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
478 * time
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;
490 freq = new_freq;
492 loops_per_sec = current_cpu_data.loops_per_jiffy;
493 loops_per_sec *= HZ;
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;
503 period /= (freq>>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);
514 return 0;
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)
524 return -EINVAL;
525 if (256*1000000L/new_freq*(100-new_duty_cycle)/100 <=
526 LIRC_SERIAL_TRANSMITTER_LATENCY)
527 return -EINVAL;
528 duty_cycle = new_duty_cycle;
529 freq = new_freq;
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);
535 return 0;
537 #endif /* USE_RDTSC */
540 /* return value: space length delta */
542 static long send_pulse_irdeo(unsigned long length)
544 long rawbits;
545 int i;
546 unsigned char output;
547 unsigned char chunk, shifted;
549 /* how many bits have to be sent ? */
550 rawbits = length*1152/10000;
551 if (duty_cycle > 50)
552 chunk = 3;
553 else
554 chunk = 1;
555 for (i = 0, output = 0x7f; rawbits > 0; rawbits -= 3) {
556 shifted = chunk<<(i*3);
557 shifted >>= 1;
558 output &= (~shifted);
559 i++;
560 if (i == 3) {
561 soutp(UART_TX, output);
562 while (!(sinp(UART_LSR) & UART_LSR_THRE));
563 output = 0x7f;
564 i = 0;
567 if (i != 0) {
568 soutp(UART_TX, output);
569 while (!(sinp(UART_LSR) & UART_LSR_TEMT));
572 if (i == 0)
573 return ((-rawbits)*10000/1152);
574 else
575 return ((3-i)*3*10000/1152+(-rawbits)*10000/1152);
578 #ifdef USE_RDTSC
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)
588 int flag;
589 unsigned long target, start, now;
591 /* Get going quick as we can */
592 rdtscl(start); on();
593 /* Convert length from microseconds to clocks */
594 length *= conv_us_to_clocks;
595 /* And loop till time is up - flipping at right intervals */
596 now = start;
597 target = pulse_width;
598 flag = 1;
599 while ((now-start) < length) {
600 /* Delay till flip time */
602 rdtscl(now);
603 while ((now-start) < target);
605 /* flip */
606 if (flag) {
607 rdtscl(now); off();
608 target += space_width;
609 } else {
610 rdtscl(now); on();
611 target += pulse_width;
613 flag = !flag;
615 rdtscl(now);
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)
629 int flag;
630 unsigned long actual, target, d;
631 length <<= 8;
633 actual = 0; target = 0; flag = 0;
634 while (actual < length) {
635 if (flag) {
636 off();
637 target += space_width;
638 } else {
639 on();
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 */
645 udelay(d);
646 actual += (d<<8)+LIRC_SERIAL_TRANSMITTER_LATENCY;
647 flag = !flag;
649 return ((actual-length)>>8);
651 #endif /* USE_RDTSC */
653 static long send_pulse_homebrew(unsigned long length)
655 if (length <= 0)
656 return 0;
658 if (softcarrier)
659 return send_pulse_homebrew_softcarrier(length);
660 else {
661 on();
662 safe_udelay(length);
663 return 0;
667 static void send_space_irdeo(long length)
669 if (length <= 0)
670 return;
672 safe_udelay(length);
675 static void send_space_homebrew(long length)
677 off();
678 if (length <= 0)
679 return;
680 safe_udelay(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");
688 return;
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;
701 if (pulse > 250) {
702 rbwrite(space);
703 rbwrite(pulse|PULSE_BIT);
704 ptr = 0;
705 pulse = 0;
707 return;
709 if (!(l&PULSE_BIT)) {
710 if (ptr == 0) {
711 if (l > 20000) {
712 space = l;
713 ptr++;
714 return;
716 } else {
717 if (l > 20000) {
718 space += pulse;
719 if (space > PULSE_MASK)
720 space = PULSE_MASK;
721 space += l;
722 if (space > PULSE_MASK)
723 space = PULSE_MASK;
724 pulse = 0;
725 return;
727 rbwrite(space);
728 rbwrite(pulse|PULSE_BIT);
729 ptr = 0;
730 pulse = 0;
733 rbwrite(l);
736 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
737 static irqreturn_t irq_handler(int i, void *blah)
738 #else
739 static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
740 #endif
742 struct timeval tv;
743 int status, counter, dcd;
744 long deltv;
745 lirc_t data;
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);
753 counter = 0;
754 do {
755 counter++;
756 status = sinp(UART_MSR);
757 if (counter > RS_ISR_PASS_LIMIT) {
758 printk(KERN_WARNING LIRC_DRIVER_NAME ": AIEEEE: "
759 "We're caught!\n");
760 break;
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
785 autodetection. */
787 /* calculate time since last interrupt in
788 microseconds */
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",
794 dcd, sense,
795 tv.tv_sec, lasttv.tv_sec,
796 tv.tv_usec, lasttv.tv_usec);
797 continue;
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 "
806 "backwards\n");
807 printk(KERN_WARNING LIRC_DRIVER_NAME
808 ": %d %d %lx %lx %lx %lx\n",
809 dcd, sense,
810 tv.tv_sec, lasttv.tv_sec,
811 tv.tv_usec, lasttv.tv_usec);
812 data = PULSE_MASK;
813 } else if (deltv > 15) {
814 data = PULSE_MASK; /* really long time */
815 if (!(dcd^sense)) {
816 /* sanity check */
817 printk(KERN_WARNING LIRC_DRIVER_NAME
818 ": AIEEEE: "
819 "%d %d %lx %lx %lx %lx\n",
820 dcd, sense,
821 tv.tv_sec, lasttv.tv_sec,
822 tv.tv_usec, lasttv.tv_usec);
823 /* detecting pulse while this
824 MUST be a space! */
825 sense = sense ? 0:1;
827 } else
828 data = (lirc_t) (deltv*1000000 +
829 tv.tv_usec -
830 lasttv.tv_usec);
831 frbwrite(dcd^sense ? data : (data|PULSE_BIT));
832 lasttv = tv;
833 last_dcd = dcd;
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)
842 unsigned long flags;
843 local_irq_save(flags);
845 /* Set DLAB 0. */
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. */
853 sinp(UART_LSR);
854 sinp(UART_RX);
855 sinp(UART_IIR);
856 sinp(UART_MSR);
858 #if defined(LIRC_SERIAL_NSLU2)
859 if (type == LIRC_NSLU2) {
860 /* Setup NSLU2 UART */
862 /* Enable 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
867 on NSLU2 */
868 soutp(UART_MCR, UART_MCR_RTS|UART_MCR_OUT2);
870 #endif
872 /* Set line for power source */
873 off();
875 /* Clear registers again to be sure. */
876 sinp(UART_LSR);
877 sinp(UART_RX);
878 sinp(UART_IIR);
879 sinp(UART_MSR);
881 switch (type) {
882 case LIRC_IRDEO:
883 case LIRC_IRDEO_REMOTE:
884 /* setup port to 7N1 @ 115200 Baud */
885 /* 7N1+start = 9 bits at 115200 ~ 3 bits at 38kHz */
887 /* Set DLAB 1. */
888 soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
889 /* Set divisor to 1 => 115200 Baud */
890 soutp(UART_DLM, 0);
891 soutp(UART_DLL, 1);
892 /* Set DLAB 0 + 7N1 */
893 soutp(UART_LCR, UART_LCR_WLEN7);
894 /* THR interrupt already disabled at this point */
895 break;
896 default:
897 break;
900 local_irq_restore(flags);
903 static int init_port(void)
905 int i, nlow, nhigh;
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. */
912 if (((iommap != 0)
913 && (request_mem_region(iommap, 8<<ioshift,
914 LIRC_DRIVER_NAME) == NULL))
915 || ((iommap == 0)
916 && (request_region(io, 8, LIRC_DRIVER_NAME) == NULL))) {
917 #else
918 if (request_region(io, 8, LIRC_DRIVER_NAME) == NULL) {
919 #endif
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");
928 return -EBUSY;
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. */
937 if (sense == -1) {
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
944 active high/low */
945 nlow = 0;
946 nhigh = 0;
947 for (i = 0; i < 9; i ++) {
948 if (sinp(UART_MSR) & hardware[type].signal_pin)
949 nlow++;
950 else
951 nhigh++;
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");
957 } else
958 printk(KERN_INFO LIRC_DRIVER_NAME ": Manually using active "
959 "%s receiver\n", sense ? "low":"high");
961 return 0;
964 static int set_use_inc(void *data)
966 int result;
967 unsigned long flags;
969 /* Init read buffer. */
970 if (lirc_buffer_init(&rbuf, sizeof(lirc_t), RBUF_LEN) < 0)
971 return -ENOMEM;
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);
980 switch (result) {
981 case -EBUSY:
982 printk(KERN_ERR LIRC_DRIVER_NAME ": IRQ %d busy\n", irq);
983 lirc_buffer_free(&rbuf);
984 return -EBUSY;
985 case -EINVAL:
986 printk(KERN_ERR LIRC_DRIVER_NAME
987 ": Bad irq number or handler\n");
988 lirc_buffer_free(&rbuf);
989 return -EINVAL;
990 default:
991 dprintk("Interrupt %d, port %04x obtained\n", irq, io);
992 break;
995 local_irq_save(flags);
997 /* Set DLAB 0. */
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);
1004 MOD_INC_USE_COUNT;
1005 return 0;
1008 static void set_use_dec(void *data)
1009 { unsigned long flags;
1011 local_irq_save(flags);
1013 /* Set DLAB 0. */
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);
1026 MOD_DEC_USE_COUNT;
1029 static ssize_t lirc_write(struct file *file, const char *buf,
1030 size_t n, loff_t *ppos)
1032 int i, count;
1033 unsigned long flags;
1034 long delta = 0;
1036 if (!(hardware[type].features&LIRC_CAN_SEND_PULSE))
1037 return -EBADF;
1039 if (n % sizeof(lirc_t))
1040 return -EINVAL;
1041 count = n / sizeof(lirc_t);
1042 if (count > WBUF_LEN || count % 2 == 0)
1043 return -EINVAL;
1044 if (copy_from_user(wbuf, buf, n))
1045 return -EFAULT;
1046 local_irq_save(flags);
1047 if (type == LIRC_IRDEO) {
1048 /* DTR, RTS down */
1049 on();
1051 for (i = 0; i < count; i++) {
1052 if (i%2)
1053 hardware[type].send_space(wbuf[i]-delta);
1054 else
1055 delta = hardware[type].send_pulse(wbuf[i]);
1057 off();
1058 local_irq_restore(flags);
1059 return n;
1062 static int lirc_ioctl(struct inode *node, struct file *filep, unsigned int cmd,
1063 unsigned long arg)
1065 int result;
1066 unsigned long value;
1067 unsigned int ivalue;
1069 switch (cmd) {
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);
1077 if (result)
1078 return result;
1079 break;
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);
1086 if (result)
1087 return result;
1088 /* only LIRC_MODE_PULSE supported */
1089 if (value != LIRC_MODE_PULSE)
1090 return -ENOSYS;
1091 break;
1093 case LIRC_GET_LENGTH:
1094 return -ENOSYS;
1095 break;
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);
1103 if (result)
1104 return result;
1105 if (ivalue <= 0 || ivalue > 100)
1106 return -EINVAL;
1107 return init_timing_params(ivalue, freq);
1108 break;
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);
1116 if (result)
1117 return result;
1118 if (ivalue > 500000 || ivalue < 20000)
1119 return -EINVAL;
1120 return init_timing_params(duty_cycle, ivalue);
1121 break;
1123 default:
1124 return -ENOIOCTLCMD;
1126 return 0;
1129 static struct file_operations lirc_fops = {
1130 .write = lirc_write,
1133 static struct lirc_plugin plugin = {
1134 .name = LIRC_DRIVER_NAME,
1135 .minor = -1,
1136 .code_length = 1,
1137 .sample_rate = 0,
1138 .data = NULL,
1139 .add_to_buf = NULL,
1140 .get_queue = NULL,
1141 .rbuf = &rbuf,
1142 .set_use_inc = set_use_inc,
1143 .set_use_dec = set_use_dec,
1144 .ioctl = lirc_ioctl,
1145 .fops = &lirc_fops,
1146 .dev = NULL,
1147 .owner = THIS_MODULE,
1150 #ifdef 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)
1157 return 0;
1160 static int __devexit lirc_serial_remove(struct platform_device *dev)
1162 return 0;
1165 static int lirc_serial_suspend(struct platform_device *dev,
1166 pm_message_t state)
1168 /* Set DLAB 0. */
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. */
1176 sinp(UART_LSR);
1177 sinp(UART_RX);
1178 sinp(UART_IIR);
1179 sinp(UART_MSR);
1181 return 0;
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);
1194 off();
1196 lirc_buffer_clear(&rbuf);
1198 local_irq_restore(flags);
1200 return 0;
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,
1208 .driver = {
1209 .name = "lirc_serial",
1210 .owner = THIS_MODULE,
1214 static int __init lirc_serial_init(void)
1216 int result;
1218 result = platform_driver_register(&lirc_serial_driver);
1219 if (result) {
1220 printk("lirc register returned %d\n", result);
1221 return result;
1224 lirc_serial_dev = platform_device_alloc("lirc_serial", 0);
1225 if (!lirc_serial_dev) {
1226 result = -ENOMEM;
1227 goto exit_driver_unregister;
1230 result = platform_device_add(lirc_serial_dev);
1231 if (result)
1232 goto exit_device_put;
1234 return 0;
1236 exit_device_put:
1237 platform_device_put(lirc_serial_dev);
1238 exit_driver_unregister:
1239 platform_driver_unregister(&lirc_serial_driver);
1240 return result;
1243 static void __exit lirc_serial_exit(void)
1245 platform_device_unregister(lirc_serial_dev);
1246 platform_driver_unregister(&lirc_serial_driver);
1248 #endif
1250 int __init init_module(void)
1252 int result;
1254 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
1255 result = lirc_serial_init();
1256 if (result)
1257 return result;
1258 #endif
1259 switch (type) {
1260 case LIRC_HOMEBREW:
1261 case LIRC_IRDEO:
1262 case LIRC_IRDEO_REMOTE:
1263 case LIRC_ANIMAX:
1264 case LIRC_IGOR:
1265 #if defined(LIRC_SERIAL_NSLU2)
1266 case LIRC_NSLU2:
1267 #endif
1268 break;
1269 default:
1270 result = -EINVAL;
1271 goto exit_serial_exit;
1273 if (!softcarrier) {
1274 switch (type) {
1275 case LIRC_HOMEBREW:
1276 case LIRC_IGOR:
1277 case LIRC_NSLU2:
1278 hardware[type].features &=
1279 ~(LIRC_CAN_SET_SEND_DUTY_CYCLE|
1280 LIRC_CAN_SET_SEND_CARRIER);
1281 break;
1284 result = init_port();
1285 if (result < 0)
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");
1292 result = -EIO;
1293 goto exit_release;
1295 return 0;
1296 exit_release:
1297 release_region(io, 8);
1298 exit_serial_exit:
1299 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
1300 lirc_serial_exit();
1301 #endif
1302 return result;
1305 void __exit cleanup_module(void)
1307 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
1308 lirc_serial_exit();
1309 #endif
1310 #if defined(LIRC_ALLOW_MMAPPED_IO)
1311 if (iommap != 0)
1312 release_mem_region(iommap, 8<<ioshift);
1313 else
1314 release_region(io, 8);
1315 #else
1316 release_region(io, 8);
1317 #endif
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)");
1332 #else
1333 MODULE_PARM_DESC(type, "Hardware type (0 = home-brew, 1 = IRdeo,"
1334 " 2 = IRdeo Remote, 3 = AnimaX, 4 = IgorPlug)");
1335 #endif
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)");
1351 #endif
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 )");
1367 #endif
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");
1375 EXPORT_NO_SYMBOLS;
1377 #endif /* MODULE */