1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IR SIR driver, (C) 2000 Milan Pikula <www@fornax.sk>
5 * sir_ir - Device driver for use with SIR (serial infra red)
6 * mode of IrDA on many notebooks.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/serial_reg.h>
15 #include <linux/ktime.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
19 #include <media/rc-core.h>
21 /* SECTION: Definitions */
24 /* 9bit * 1s/115200bit in milli seconds = 78.125ms*/
25 #define TIME_CONST (9000000ul / 115200ul)
27 /* timeout for sequences in jiffies (=5/100s), must be longer than TIME_CONST */
28 #define SIR_TIMEOUT (HZ * 5 / 100)
30 /* onboard sir ports are typically com3 */
31 static int io
= 0x3e8;
33 static int threshold
= 3;
35 static DEFINE_SPINLOCK(timer_lock
);
36 static struct timer_list timerlist
;
37 /* time of last signal change detected */
39 /* time of last UART data ready interrupt */
40 static ktime_t last_intr_time
;
41 static int last_value
;
42 static struct rc_dev
*rcdev
;
44 static struct platform_device
*sir_ir_dev
;
46 static DEFINE_SPINLOCK(hardware_lock
);
48 /* SECTION: Prototypes */
50 /* Communication with user-space */
51 static void add_read_queue(int flag
, unsigned long val
);
53 static irqreturn_t
sir_interrupt(int irq
, void *dev_id
);
54 static void send_space(unsigned long len
);
55 static void send_pulse(unsigned long len
);
56 static int init_hardware(void);
57 static void drop_hardware(void);
60 static inline unsigned int sinp(int offset
)
62 return inb(io
+ offset
);
65 static inline void soutp(int offset
, int value
)
67 outb(value
, io
+ offset
);
70 /* SECTION: Communication with user-space */
71 static int sir_tx_ir(struct rc_dev
*dev
, unsigned int *tx_buf
,
77 local_irq_save(flags
);
78 for (i
= 0; i
< count
;) {
80 send_pulse(tx_buf
[i
]);
85 send_space(tx_buf
[i
]);
88 local_irq_restore(flags
);
93 static void add_read_queue(int flag
, unsigned long val
)
95 struct ir_raw_event ev
= {};
97 pr_debug("add flag %d with val %lu\n", flag
, val
);
100 * statistically, pulses are ~TIME_CONST/2 too long. we could
101 * maybe make this more exact, but this is good enough
105 if (val
> TIME_CONST
/ 2)
106 val
-= TIME_CONST
/ 2;
107 else /* should not ever happen */
111 val
+= TIME_CONST
/ 2;
115 ir_raw_event_store_with_filter(rcdev
, &ev
);
118 /* SECTION: Hardware */
119 static void sir_timeout(struct timer_list
*unused
)
122 * if last received signal was a pulse, but receiving stopped
123 * within the 9 bit frame, we need to finish this pulse and
124 * simulate a signal change to from pulse to space. Otherwise
125 * upper layers will receive two sequences next time.
129 unsigned long pulse_end
;
131 /* avoid interference with interrupt */
132 spin_lock_irqsave(&timer_lock
, flags
);
134 /* clear unread bits in UART and restart */
135 outb(UART_FCR_CLEAR_RCVR
, io
+ UART_FCR
);
136 /* determine 'virtual' pulse end: */
137 pulse_end
= min_t(unsigned long,
138 ktime_us_delta(last
, last_intr_time
),
140 dev_dbg(&sir_ir_dev
->dev
, "timeout add %d for %lu usec\n",
141 last_value
, pulse_end
);
142 add_read_queue(last_value
, pulse_end
);
144 last
= last_intr_time
;
146 spin_unlock_irqrestore(&timer_lock
, flags
);
147 ir_raw_event_handle(rcdev
);
150 static irqreturn_t
sir_interrupt(int irq
, void *dev_id
)
155 unsigned long deltintr
;
160 while ((iir
= inb(io
+ UART_IIR
) & UART_IIR_ID
)) {
161 if (++counter
> 256) {
162 dev_err(&sir_ir_dev
->dev
, "Trapped in interrupt");
166 switch (iir
& UART_IIR_ID
) { /* FIXME toto treba preriedit */
168 (void)inb(io
+ UART_MSR
);
172 (void)inb(io
+ UART_LSR
);
175 /* avoid interference with timer */
176 spin_lock_irqsave(&timer_lock
, flags
);
178 del_timer(&timerlist
);
179 data
= inb(io
+ UART_RX
);
180 curr_time
= ktime_get();
181 delt
= min_t(unsigned long,
182 ktime_us_delta(last
, curr_time
),
184 deltintr
= min_t(unsigned long,
185 ktime_us_delta(last_intr_time
,
188 dev_dbg(&sir_ir_dev
->dev
, "t %lu, d %d\n",
189 deltintr
, (int)data
);
191 * if nothing came in last X cycles,
194 if (deltintr
> TIME_CONST
* threshold
) {
196 dev_dbg(&sir_ir_dev
->dev
, "GAP\n");
197 /* simulate signal change */
198 add_read_queue(last_value
,
202 last
= last_intr_time
;
207 if (data
^ last_value
) {
209 * deltintr > 2*TIME_CONST, remember?
210 * the other case is timeout
212 add_read_queue(last_value
,
216 last
= ktime_sub_us(last
,
219 last_intr_time
= curr_time
;
222 * start timer for end of
225 timerlist
.expires
= jiffies
+
227 add_timer(&timerlist
);
230 lsr
= inb(io
+ UART_LSR
);
231 } while (lsr
& UART_LSR_DR
); /* data ready */
232 spin_unlock_irqrestore(&timer_lock
, flags
);
238 ir_raw_event_handle(rcdev
);
239 return IRQ_RETVAL(IRQ_HANDLED
);
242 static void send_space(unsigned long len
)
244 usleep_range(len
, len
+ 25);
247 static void send_pulse(unsigned long len
)
249 long bytes_out
= len
/ TIME_CONST
;
254 while (bytes_out
--) {
255 outb(PULSE
, io
+ UART_TX
);
256 /* FIXME treba seriozne cakanie z char/serial.c */
257 while (!(inb(io
+ UART_LSR
) & UART_LSR_THRE
))
262 static int init_hardware(void)
264 u8 scratch
, scratch2
, scratch3
;
267 spin_lock_irqsave(&hardware_lock
, flags
);
270 * This is a simple port existence test, borrowed from the autoconfig
271 * function in drivers/tty/serial/8250/8250_port.c
273 scratch
= sinp(UART_IER
);
278 scratch2
= sinp(UART_IER
) & 0x0f;
279 soutp(UART_IER
, 0x0f);
283 scratch3
= sinp(UART_IER
) & 0x0f;
284 soutp(UART_IER
, scratch
);
285 if (scratch2
!= 0 || scratch3
!= 0x0f) {
286 /* we fail, there's nothing here */
287 spin_unlock_irqrestore(&hardware_lock
, flags
);
288 pr_err("port existence test failed, cannot continue\n");
293 outb(0, io
+ UART_MCR
);
294 outb(0, io
+ UART_IER
);
296 /* set DLAB, speed = 115200 */
297 outb(UART_LCR_DLAB
| UART_LCR_WLEN7
, io
+ UART_LCR
);
298 outb(1, io
+ UART_DLL
); outb(0, io
+ UART_DLM
);
299 /* 7N1+start = 9 bits at 115200 ~ 3 bits at 44000 */
300 outb(UART_LCR_WLEN7
, io
+ UART_LCR
);
302 outb(UART_FCR_ENABLE_FIFO
, io
+ UART_FCR
);
304 /* outb(UART_IER_RLSI|UART_IER_RDI|UART_IER_THRI, io + UART_IER); */
305 outb(UART_IER_RDI
, io
+ UART_IER
);
307 outb(UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
, io
+ UART_MCR
);
308 spin_unlock_irqrestore(&hardware_lock
, flags
);
313 static void drop_hardware(void)
317 spin_lock_irqsave(&hardware_lock
, flags
);
319 /* turn off interrupts */
320 outb(0, io
+ UART_IER
);
322 spin_unlock_irqrestore(&hardware_lock
, flags
);
325 /* SECTION: Initialisation */
326 static int sir_ir_probe(struct platform_device
*dev
)
330 rcdev
= devm_rc_allocate_device(&sir_ir_dev
->dev
, RC_DRIVER_IR_RAW
);
334 rcdev
->device_name
= "SIR IrDA port";
335 rcdev
->input_phys
= KBUILD_MODNAME
"/input0";
336 rcdev
->input_id
.bustype
= BUS_HOST
;
337 rcdev
->input_id
.vendor
= 0x0001;
338 rcdev
->input_id
.product
= 0x0001;
339 rcdev
->input_id
.version
= 0x0100;
340 rcdev
->tx_ir
= sir_tx_ir
;
341 rcdev
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
342 rcdev
->driver_name
= KBUILD_MODNAME
;
343 rcdev
->map_name
= RC_MAP_RC6_MCE
;
344 rcdev
->timeout
= IR_DEFAULT_TIMEOUT
;
345 rcdev
->dev
.parent
= &sir_ir_dev
->dev
;
347 timer_setup(&timerlist
, sir_timeout
, 0);
349 /* get I/O port access and IRQ line */
350 if (!devm_request_region(&sir_ir_dev
->dev
, io
, 8, KBUILD_MODNAME
)) {
351 pr_err("i/o port 0x%.4x already in use.\n", io
);
354 retval
= devm_request_irq(&sir_ir_dev
->dev
, irq
, sir_interrupt
, 0,
355 KBUILD_MODNAME
, NULL
);
357 pr_err("IRQ %d already in use.\n", irq
);
361 retval
= init_hardware();
363 del_timer_sync(&timerlist
);
367 pr_info("I/O port 0x%.4x, IRQ %d.\n", io
, irq
);
369 retval
= devm_rc_register_device(&sir_ir_dev
->dev
, rcdev
);
376 static int sir_ir_remove(struct platform_device
*dev
)
379 del_timer_sync(&timerlist
);
383 static struct platform_driver sir_ir_driver
= {
384 .probe
= sir_ir_probe
,
385 .remove
= sir_ir_remove
,
391 static int __init
sir_ir_init(void)
395 retval
= platform_driver_register(&sir_ir_driver
);
399 sir_ir_dev
= platform_device_alloc("sir_ir", 0);
402 goto pdev_alloc_fail
;
405 retval
= platform_device_add(sir_ir_dev
);
412 platform_device_put(sir_ir_dev
);
414 platform_driver_unregister(&sir_ir_driver
);
418 static void __exit
sir_ir_exit(void)
420 platform_device_unregister(sir_ir_dev
);
421 platform_driver_unregister(&sir_ir_driver
);
424 module_init(sir_ir_init
);
425 module_exit(sir_ir_exit
);
427 MODULE_DESCRIPTION("Infrared receiver driver for SIR type serial ports");
428 MODULE_AUTHOR("Milan Pikula");
429 MODULE_LICENSE("GPL");
431 module_param_hw(io
, int, ioport
, 0444);
432 MODULE_PARM_DESC(io
, "I/O address base (0x3f8 or 0x2f8)");
434 module_param_hw(irq
, int, irq
, 0444);
435 MODULE_PARM_DESC(irq
, "Interrupt (4 or 3)");
437 module_param(threshold
, int, 0444);
438 MODULE_PARM_DESC(threshold
, "space detection threshold (3)");