1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * serial_ir.c
4 *
5 * serial_ir - Device driver that records pulse- and pause-lengths
6 * (space-lengths) between DDCD event on a serial port.
7 *
8 * Copyright (C) 1996,97 Ralph Metzler <rjkm@thp.uni-koeln.de>
9 * Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>
10 * Copyright (C) 1998 Ben Pfaff <blp@gnu.org>
11 * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
12 * Copyright (C) 2007 Andrei Tanas <andrei@tanas.ca> (suspend/resume support)
13 * Copyright (C) 2016 Sean Young <sean@mess.org> (port to rc-core)
14 */
15
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/interrupt.h>
21 #include <linux/io.h>
22 #include <linux/kernel.h>
23 #include <linux/serial_reg.h>
24 #include <linux/types.h>
25 #include <linux/delay.h>
26 #include <linux/platform_device.h>
27 #include <linux/spinlock.h>
28 #include <media/rc-core.h>
29
30 struct serial_ir_hw {
31 int signal_pin;
32 int signal_pin_change;
33 u8 on;
34 u8 off;
35 unsigned set_send_carrier:1;
36 unsigned set_duty_cycle:1;
37 void (*send_pulse)(unsigned int length, ktime_t edge);
38 void (*send_space)(void);
39 spinlock_t lock;
40 };
41
42 #define IR_HOMEBREW 0
43 #define IR_IRDEO 1
44 #define IR_IRDEO_REMOTE 2
45 #define IR_ANIMAX 3
46 #define IR_IGOR 4
47
48 /* module parameters */
49 static int type;
50 static int io;
51 static int irq;
52 static ulong iommap;
53 static int ioshift;
54 static bool softcarrier = true;
55 static bool share_irq;
56 static int sense = -1; /* -1 = auto, 0 = active high, 1 = active low */
57 static bool txsense; /* 0 = active high, 1 = active low */
58
59 /* forward declarations */
60 static void send_pulse_irdeo(unsigned int length, ktime_t edge);
61 static void send_space_irdeo(void);
62 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
63 static void send_pulse_homebrew(unsigned int length, ktime_t edge);
64 static void send_space_homebrew(void);
65 #endif
66
67 static struct serial_ir_hw hardware[] = {
68 [IR_HOMEBREW] = {
69 .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_HOMEBREW].lock),
70 .signal_pin = UART_MSR_DCD,
71 .signal_pin_change = UART_MSR_DDCD,
72 .on = (UART_MCR_RTS | UART_MCR_OUT2 | UART_MCR_DTR),
73 .off = (UART_MCR_RTS | UART_MCR_OUT2),
74 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
75 .send_pulse = send_pulse_homebrew,
76 .send_space = send_space_homebrew,
77 .set_send_carrier = true,
78 .set_duty_cycle = true,
79 #endif
80 },
81
82 [IR_IRDEO] = {
83 .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_IRDEO].lock),
84 .signal_pin = UART_MSR_DSR,
85 .signal_pin_change = UART_MSR_DDSR,
86 .on = UART_MCR_OUT2,
87 .off = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
88 .send_pulse = send_pulse_irdeo,
89 .send_space = send_space_irdeo,
90 .set_duty_cycle = true,
91 },
92
93 [IR_IRDEO_REMOTE] = {
94 .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_IRDEO_REMOTE].lock),
95 .signal_pin = UART_MSR_DSR,
96 .signal_pin_change = UART_MSR_DDSR,
97 .on = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
98 .off = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
99 .send_pulse = send_pulse_irdeo,
100 .send_space = send_space_irdeo,
101 .set_duty_cycle = true,
102 },
103
104 [IR_ANIMAX] = {
105 .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_ANIMAX].lock),
106 .signal_pin = UART_MSR_DCD,
107 .signal_pin_change = UART_MSR_DDCD,
108 .on = 0,
109 .off = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
110 },
111
112 [IR_IGOR] = {
113 .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_IGOR].lock),
114 .signal_pin = UART_MSR_DSR,
115 .signal_pin_change = UART_MSR_DDSR,
116 .on = (UART_MCR_RTS | UART_MCR_OUT2 | UART_MCR_DTR),
117 .off = (UART_MCR_RTS | UART_MCR_OUT2),
118 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
119 .send_pulse = send_pulse_homebrew,
120 .send_space = send_space_homebrew,
121 .set_send_carrier = true,
122 .set_duty_cycle = true,
123 #endif
124 },
125 };
126
127 #define RS_ISR_PASS_LIMIT 256
128
129 struct serial_ir {
130 ktime_t lastkt;
131 struct rc_dev *rcdev;
132 struct platform_device *pdev;
133 struct timer_list timeout_timer;
134
135 unsigned int carrier;
136 unsigned int duty_cycle;
137 };
138
139 static struct serial_ir serial_ir;
140
141 /* fetch serial input packet (1 byte) from register offset */
sinp(int offset)142 static u8 sinp(int offset)
143 {
144 if (iommap)
145 /* the register is memory-mapped */
146 offset <<= ioshift;
147
148 return inb(io + offset);
149 }
150
151 /* write serial output packet (1 byte) of value to register offset */
soutp(int offset,u8 value)152 static void soutp(int offset, u8 value)
153 {
154 if (iommap)
155 /* the register is memory-mapped */
156 offset <<= ioshift;
157
158 outb(value, io + offset);
159 }
160
on(void)161 static void on(void)
162 {
163 if (txsense)
164 soutp(UART_MCR, hardware[type].off);
165 else
166 soutp(UART_MCR, hardware[type].on);
167 }
168
off(void)169 static void off(void)
170 {
171 if (txsense)
172 soutp(UART_MCR, hardware[type].on);
173 else
174 soutp(UART_MCR, hardware[type].off);
175 }
176
send_pulse_irdeo(unsigned int length,ktime_t target)177 static void send_pulse_irdeo(unsigned int length, ktime_t target)
178 {
179 long rawbits;
180 int i;
181 unsigned char output;
182 unsigned char chunk, shifted;
183
184 /* how many bits have to be sent ? */
185 rawbits = length * 1152 / 10000;
186 if (serial_ir.duty_cycle > 50)
187 chunk = 3;
188 else
189 chunk = 1;
190 for (i = 0, output = 0x7f; rawbits > 0; rawbits -= 3) {
191 shifted = chunk << (i * 3);
192 shifted >>= 1;
193 output &= (~shifted);
194 i++;
195 if (i == 3) {
196 soutp(UART_TX, output);
197 while (!(sinp(UART_LSR) & UART_LSR_THRE))
198 ;
199 output = 0x7f;
200 i = 0;
201 }
202 }
203 if (i != 0) {
204 soutp(UART_TX, output);
205 while (!(sinp(UART_LSR) & UART_LSR_TEMT))
206 ;
207 }
208 }
209
send_space_irdeo(void)210 static void send_space_irdeo(void)
211 {
212 }
213
214 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
send_pulse_homebrew_softcarrier(unsigned int length,ktime_t edge)215 static void send_pulse_homebrew_softcarrier(unsigned int length, ktime_t edge)
216 {
217 ktime_t now, target = ktime_add_us(edge, length);
218 /*
219 * delta should never exceed 4 seconds and on m68k
220 * ndelay(s64) does not compile; so use s32 rather than s64.
221 */
222 s32 delta;
223 unsigned int pulse, space;
224
225 /* Ensure the dividend fits into 32 bit */
226 pulse = DIV_ROUND_CLOSEST(serial_ir.duty_cycle * (NSEC_PER_SEC / 100),
227 serial_ir.carrier);
228 space = DIV_ROUND_CLOSEST((100 - serial_ir.duty_cycle) *
229 (NSEC_PER_SEC / 100), serial_ir.carrier);
230
231 for (;;) {
232 now = ktime_get();
233 if (ktime_compare(now, target) >= 0)
234 break;
235 on();
236 edge = ktime_add_ns(edge, pulse);
237 delta = ktime_to_ns(ktime_sub(edge, now));
238 if (delta > 0)
239 ndelay(delta);
240 now = ktime_get();
241 off();
242 if (ktime_compare(now, target) >= 0)
243 break;
244 edge = ktime_add_ns(edge, space);
245 delta = ktime_to_ns(ktime_sub(edge, now));
246 if (delta > 0)
247 ndelay(delta);
248 }
249 }
250
send_pulse_homebrew(unsigned int length,ktime_t edge)251 static void send_pulse_homebrew(unsigned int length, ktime_t edge)
252 {
253 if (softcarrier)
254 send_pulse_homebrew_softcarrier(length, edge);
255 else
256 on();
257 }
258
send_space_homebrew(void)259 static void send_space_homebrew(void)
260 {
261 off();
262 }
263 #endif
264
frbwrite(unsigned int l,bool is_pulse)265 static void frbwrite(unsigned int l, bool is_pulse)
266 {
267 /* simple noise filter */
268 static unsigned int ptr, pulse, space;
269 struct ir_raw_event ev = {};
270
271 if (ptr > 0 && is_pulse) {
272 pulse += l;
273 if (pulse > 250) {
274 ev.duration = space;
275 ev.pulse = false;
276 ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
277 ev.duration = pulse;
278 ev.pulse = true;
279 ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
280 ptr = 0;
281 pulse = 0;
282 }
283 return;
284 }
285 if (!is_pulse) {
286 if (ptr == 0) {
287 if (l > 20000) {
288 space = l;
289 ptr++;
290 return;
291 }
292 } else {
293 if (l > 20000) {
294 space += pulse;
295 if (space > IR_MAX_DURATION)
296 space = IR_MAX_DURATION;
297 space += l;
298 if (space > IR_MAX_DURATION)
299 space = IR_MAX_DURATION;
300 pulse = 0;
301 return;
302 }
303
304 ev.duration = space;
305 ev.pulse = false;
306 ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
307 ev.duration = pulse;
308 ev.pulse = true;
309 ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
310 ptr = 0;
311 pulse = 0;
312 }
313 }
314
315 ev.duration = l;
316 ev.pulse = is_pulse;
317 ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
318 }
319
serial_ir_irq_handler(int i,void * blah)320 static irqreturn_t serial_ir_irq_handler(int i, void *blah)
321 {
322 ktime_t kt;
323 int counter, dcd;
324 u8 status;
325 ktime_t delkt;
326 unsigned int data;
327 static int last_dcd = -1;
328
329 if ((sinp(UART_IIR) & UART_IIR_NO_INT)) {
330 /* not our interrupt */
331 return IRQ_NONE;
332 }
333
334 counter = 0;
335 do {
336 counter++;
337 status = sinp(UART_MSR);
338 if (counter > RS_ISR_PASS_LIMIT) {
339 dev_err(&serial_ir.pdev->dev, "Trapped in interrupt");
340 break;
341 }
342 if ((status & hardware[type].signal_pin_change) &&
343 sense != -1) {
344 /* get current time */
345 kt = ktime_get();
346
347 /*
348 * The driver needs to know if your receiver is
349 * active high or active low, or the space/pulse
350 * sense could be inverted.
351 */
352
353 /* calc time since last interrupt in nanoseconds */
354 dcd = (status & hardware[type].signal_pin) ? 1 : 0;
355
356 if (dcd == last_dcd) {
357 dev_dbg(&serial_ir.pdev->dev,
358 "ignoring spike: %d %d %lldns %lldns\n",
359 dcd, sense, ktime_to_ns(kt),
360 ktime_to_ns(serial_ir.lastkt));
361 continue;
362 }
363
364 delkt = ktime_sub(kt, serial_ir.lastkt);
365 if (ktime_compare(delkt, ktime_set(15, 0)) > 0) {
366 data = IR_MAX_DURATION; /* really long time */
367 if (!(dcd ^ sense)) {
368 /* sanity check */
369 dev_err(&serial_ir.pdev->dev,
370 "dcd unexpected: %d %d %lldns %lldns\n",
371 dcd, sense, ktime_to_ns(kt),
372 ktime_to_ns(serial_ir.lastkt));
373 /*
374 * detecting pulse while this
375 * MUST be a space!
376 */
377 sense = sense ? 0 : 1;
378 }
379 } else {
380 data = ktime_to_us(delkt);
381 }
382 frbwrite(data, !(dcd ^ sense));
383 serial_ir.lastkt = kt;
384 last_dcd = dcd;
385 }
386 } while (!(sinp(UART_IIR) & UART_IIR_NO_INT)); /* still pending ? */
387
388 mod_timer(&serial_ir.timeout_timer,
389 jiffies + usecs_to_jiffies(serial_ir.rcdev->timeout));
390
391 ir_raw_event_handle(serial_ir.rcdev);
392
393 return IRQ_HANDLED;
394 }
395
hardware_init_port(void)396 static int hardware_init_port(void)
397 {
398 u8 scratch, scratch2, scratch3;
399
400 /*
401 * This is a simple port existence test, borrowed from the autoconfig
402 * function in drivers/tty/serial/8250/8250_port.c
403 */
404 scratch = sinp(UART_IER);
405 soutp(UART_IER, 0);
406 #ifdef __i386__
407 outb(0xff, 0x080);
408 #endif
409 scratch2 = sinp(UART_IER) & 0x0f;
410 soutp(UART_IER, 0x0f);
411 #ifdef __i386__
412 outb(0x00, 0x080);
413 #endif
414 scratch3 = sinp(UART_IER) & 0x0f;
415 soutp(UART_IER, scratch);
416 if (scratch2 != 0 || scratch3 != 0x0f) {
417 /* we fail, there's nothing here */
418 pr_err("port existence test failed, cannot continue\n");
419 return -ENODEV;
420 }
421
422 /* Set DLAB 0. */
423 soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
424
425 /* First of all, disable all interrupts */
426 soutp(UART_IER, sinp(UART_IER) &
427 (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
428
429 /* Clear registers. */
430 sinp(UART_LSR);
431 sinp(UART_RX);
432 sinp(UART_IIR);
433 sinp(UART_MSR);
434
435 /* Set line for power source */
436 off();
437
438 /* Clear registers again to be sure. */
439 sinp(UART_LSR);
440 sinp(UART_RX);
441 sinp(UART_IIR);
442 sinp(UART_MSR);
443
444 switch (type) {
445 case IR_IRDEO:
446 case IR_IRDEO_REMOTE:
447 /* setup port to 7N1 @ 115200 Baud */
448 /* 7N1+start = 9 bits at 115200 ~ 3 bits at 38kHz */
449
450 /* Set DLAB 1. */
451 soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
452 /* Set divisor to 1 => 115200 Baud */
453 soutp(UART_DLM, 0);
454 soutp(UART_DLL, 1);
455 /* Set DLAB 0 + 7N1 */
456 soutp(UART_LCR, UART_LCR_WLEN7);
457 /* THR interrupt already disabled at this point */
458 break;
459 default:
460 break;
461 }
462
463 return 0;
464 }
465
serial_ir_timeout(struct timer_list * unused)466 static void serial_ir_timeout(struct timer_list *unused)
467 {
468 struct ir_raw_event ev = {
469 .timeout = true,
470 .duration = serial_ir.rcdev->timeout
471 };
472 ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
473 ir_raw_event_handle(serial_ir.rcdev);
474 }
475
476 /* Needed by serial_ir_probe() */
477 static int serial_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
478 unsigned int count);
479 static int serial_ir_tx_duty_cycle(struct rc_dev *dev, u32 cycle);
480 static int serial_ir_tx_carrier(struct rc_dev *dev, u32 carrier);
481 static int serial_ir_open(struct rc_dev *rcdev);
482 static void serial_ir_close(struct rc_dev *rcdev);
483
serial_ir_probe(struct platform_device * dev)484 static int serial_ir_probe(struct platform_device *dev)
485 {
486 struct rc_dev *rcdev;
487 int i, nlow, nhigh, result;
488
489 rcdev = devm_rc_allocate_device(&dev->dev, RC_DRIVER_IR_RAW);
490 if (!rcdev)
491 return -ENOMEM;
492
493 if (hardware[type].send_pulse && hardware[type].send_space)
494 rcdev->tx_ir = serial_ir_tx;
495 if (hardware[type].set_send_carrier)
496 rcdev->s_tx_carrier = serial_ir_tx_carrier;
497 if (hardware[type].set_duty_cycle)
498 rcdev->s_tx_duty_cycle = serial_ir_tx_duty_cycle;
499
500 switch (type) {
501 case IR_HOMEBREW:
502 rcdev->device_name = "Serial IR type home-brew";
503 break;
504 case IR_IRDEO:
505 rcdev->device_name = "Serial IR type IRdeo";
506 break;
507 case IR_IRDEO_REMOTE:
508 rcdev->device_name = "Serial IR type IRdeo remote";
509 break;
510 case IR_ANIMAX:
511 rcdev->device_name = "Serial IR type AnimaX";
512 break;
513 case IR_IGOR:
514 rcdev->device_name = "Serial IR type IgorPlug";
515 break;
516 }
517
518 rcdev->input_phys = KBUILD_MODNAME "/input0";
519 rcdev->input_id.bustype = BUS_HOST;
520 rcdev->input_id.vendor = 0x0001;
521 rcdev->input_id.product = 0x0001;
522 rcdev->input_id.version = 0x0100;
523 rcdev->open = serial_ir_open;
524 rcdev->close = serial_ir_close;
525 rcdev->dev.parent = &serial_ir.pdev->dev;
526 rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
527 rcdev->driver_name = KBUILD_MODNAME;
528 rcdev->map_name = RC_MAP_RC6_MCE;
529 rcdev->min_timeout = 1;
530 rcdev->timeout = IR_DEFAULT_TIMEOUT;
531 rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
532 rcdev->rx_resolution = 250;
533
534 serial_ir.rcdev = rcdev;
535
536 timer_setup(&serial_ir.timeout_timer, serial_ir_timeout, 0);
537
538 result = devm_request_irq(&dev->dev, irq, serial_ir_irq_handler,
539 share_irq ? IRQF_SHARED : 0,
540 KBUILD_MODNAME, &hardware);
541 if (result < 0) {
542 if (result == -EBUSY)
543 dev_err(&dev->dev, "IRQ %d busy\n", irq);
544 else if (result == -EINVAL)
545 dev_err(&dev->dev, "Bad irq number or handler\n");
546 return result;
547 }
548
549 /* Reserve io region. */
550 if ((iommap &&
551 (devm_request_mem_region(&dev->dev, iommap, 8UL << ioshift,
552 KBUILD_MODNAME) == NULL)) ||
553 (!iommap && (devm_request_region(&dev->dev, io, 8,
554 KBUILD_MODNAME) == NULL))) {
555 dev_err(&dev->dev, "port %04x already in use\n", io);
556 dev_warn(&dev->dev, "use 'setserial /dev/ttySX uart none'\n");
557 dev_warn(&dev->dev,
558 "or compile the serial port driver as module and\n");
559 dev_warn(&dev->dev, "make sure this module is loaded first\n");
560 return -EBUSY;
561 }
562
563 result = hardware_init_port();
564 if (result < 0)
565 return result;
566
567 /* Initialize pulse/space widths */
568 serial_ir.duty_cycle = 50;
569 serial_ir.carrier = 38000;
570
571 /* If pin is high, then this must be an active low receiver. */
572 if (sense == -1) {
573 /* wait 1/2 sec for the power supply */
574 msleep(500);
575
576 /*
577 * probe 9 times every 0.04s, collect "votes" for
578 * active high/low
579 */
580 nlow = 0;
581 nhigh = 0;
582 for (i = 0; i < 9; i++) {
583 if (sinp(UART_MSR) & hardware[type].signal_pin)
584 nlow++;
585 else
586 nhigh++;
587 msleep(40);
588 }
589 sense = nlow >= nhigh ? 1 : 0;
590 dev_info(&dev->dev, "auto-detected active %s receiver\n",
591 sense ? "low" : "high");
592 } else
593 dev_info(&dev->dev, "Manually using active %s receiver\n",
594 sense ? "low" : "high");
595
596 dev_dbg(&dev->dev, "Interrupt %d, port %04x obtained\n", irq, io);
597
598 return devm_rc_register_device(&dev->dev, rcdev);
599 }
600
serial_ir_open(struct rc_dev * rcdev)601 static int serial_ir_open(struct rc_dev *rcdev)
602 {
603 unsigned long flags;
604
605 /* initialize timestamp */
606 serial_ir.lastkt = ktime_get();
607
608 spin_lock_irqsave(&hardware[type].lock, flags);
609
610 /* Set DLAB 0. */
611 soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
612
613 soutp(UART_IER, sinp(UART_IER) | UART_IER_MSI);
614
615 spin_unlock_irqrestore(&hardware[type].lock, flags);
616
617 return 0;
618 }
619
serial_ir_close(struct rc_dev * rcdev)620 static void serial_ir_close(struct rc_dev *rcdev)
621 {
622 unsigned long flags;
623
624 spin_lock_irqsave(&hardware[type].lock, flags);
625
626 /* Set DLAB 0. */
627 soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
628
629 /* First of all, disable all interrupts */
630 soutp(UART_IER, sinp(UART_IER) &
631 (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
632 spin_unlock_irqrestore(&hardware[type].lock, flags);
633 }
634
serial_ir_tx(struct rc_dev * dev,unsigned int * txbuf,unsigned int count)635 static int serial_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
636 unsigned int count)
637 {
638 unsigned long flags;
639 ktime_t edge;
640 s64 delta;
641 int i;
642
643 spin_lock_irqsave(&hardware[type].lock, flags);
644 if (type == IR_IRDEO) {
645 /* DTR, RTS down */
646 on();
647 }
648
649 edge = ktime_get();
650 for (i = 0; i < count; i++) {
651 if (i % 2)
652 hardware[type].send_space();
653 else
654 hardware[type].send_pulse(txbuf[i], edge);
655
656 edge = ktime_add_us(edge, txbuf[i]);
657 delta = ktime_us_delta(edge, ktime_get());
658 if (delta > 25) {
659 spin_unlock_irqrestore(&hardware[type].lock, flags);
660 usleep_range(delta - 25, delta + 25);
661 spin_lock_irqsave(&hardware[type].lock, flags);
662 } else if (delta > 0) {
663 udelay(delta);
664 }
665 }
666 off();
667 spin_unlock_irqrestore(&hardware[type].lock, flags);
668 return count;
669 }
670
serial_ir_tx_duty_cycle(struct rc_dev * dev,u32 cycle)671 static int serial_ir_tx_duty_cycle(struct rc_dev *dev, u32 cycle)
672 {
673 serial_ir.duty_cycle = cycle;
674 return 0;
675 }
676
serial_ir_tx_carrier(struct rc_dev * dev,u32 carrier)677 static int serial_ir_tx_carrier(struct rc_dev *dev, u32 carrier)
678 {
679 if (carrier > 500000 || carrier < 20000)
680 return -EINVAL;
681
682 serial_ir.carrier = carrier;
683 return 0;
684 }
685
serial_ir_suspend(struct platform_device * dev,pm_message_t state)686 static int serial_ir_suspend(struct platform_device *dev,
687 pm_message_t state)
688 {
689 /* Set DLAB 0. */
690 soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
691
692 /* Disable all interrupts */
693 soutp(UART_IER, sinp(UART_IER) &
694 (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
695
696 /* Clear registers. */
697 sinp(UART_LSR);
698 sinp(UART_RX);
699 sinp(UART_IIR);
700 sinp(UART_MSR);
701
702 return 0;
703 }
704
serial_ir_resume(struct platform_device * dev)705 static int serial_ir_resume(struct platform_device *dev)
706 {
707 unsigned long flags;
708 int result;
709
710 result = hardware_init_port();
711 if (result < 0)
712 return result;
713
714 spin_lock_irqsave(&hardware[type].lock, flags);
715 /* Enable Interrupt */
716 serial_ir.lastkt = ktime_get();
717 soutp(UART_IER, sinp(UART_IER) | UART_IER_MSI);
718 off();
719
720 spin_unlock_irqrestore(&hardware[type].lock, flags);
721
722 return 0;
723 }
724
725 static struct platform_driver serial_ir_driver = {
726 .probe = serial_ir_probe,
727 .suspend = serial_ir_suspend,
728 .resume = serial_ir_resume,
729 .driver = {
730 .name = "serial_ir",
731 },
732 };
733
serial_ir_init(void)734 static int __init serial_ir_init(void)
735 {
736 int result;
737
738 result = platform_driver_register(&serial_ir_driver);
739 if (result)
740 return result;
741
742 serial_ir.pdev = platform_device_alloc("serial_ir", 0);
743 if (!serial_ir.pdev) {
744 result = -ENOMEM;
745 goto exit_driver_unregister;
746 }
747
748 result = platform_device_add(serial_ir.pdev);
749 if (result)
750 goto exit_device_put;
751
752 return 0;
753
754 exit_device_put:
755 platform_device_put(serial_ir.pdev);
756 exit_driver_unregister:
757 platform_driver_unregister(&serial_ir_driver);
758 return result;
759 }
760
serial_ir_exit(void)761 static void serial_ir_exit(void)
762 {
763 platform_device_unregister(serial_ir.pdev);
764 platform_driver_unregister(&serial_ir_driver);
765 }
766
serial_ir_init_module(void)767 static int __init serial_ir_init_module(void)
768 {
769 switch (type) {
770 case IR_HOMEBREW:
771 case IR_IRDEO:
772 case IR_IRDEO_REMOTE:
773 case IR_ANIMAX:
774 case IR_IGOR:
775 /* if nothing specified, use ttyS0/com1 and irq 4 */
776 io = io ? io : 0x3f8;
777 irq = irq ? irq : 4;
778 break;
779 default:
780 return -EINVAL;
781 }
782 if (!softcarrier) {
783 switch (type) {
784 case IR_HOMEBREW:
785 case IR_IGOR:
786 hardware[type].set_send_carrier = false;
787 hardware[type].set_duty_cycle = false;
788 break;
789 }
790 }
791
792 /* make sure sense is either -1, 0, or 1 */
793 if (sense != -1)
794 sense = !!sense;
795
796 return serial_ir_init();
797 }
798
serial_ir_exit_module(void)799 static void __exit serial_ir_exit_module(void)
800 {
801 del_timer_sync(&serial_ir.timeout_timer);
802 serial_ir_exit();
803 }
804
805 module_init(serial_ir_init_module);
806 module_exit(serial_ir_exit_module);
807
808 MODULE_DESCRIPTION("Infra-red receiver driver for serial ports.");
809 MODULE_AUTHOR("Ralph Metzler, Trent Piepho, Ben Pfaff, Christoph Bartelmus, Andrei Tanas");
810 MODULE_LICENSE("GPL");
811
812 module_param(type, int, 0444);
813 MODULE_PARM_DESC(type, "Hardware type (0 = home-brew, 1 = IRdeo, 2 = IRdeo Remote, 3 = AnimaX, 4 = IgorPlug");
814
815 module_param_hw(io, int, ioport, 0444);
816 MODULE_PARM_DESC(io, "I/O address base (0x3f8 or 0x2f8)");
817
818 /* some architectures (e.g. intel xscale) have memory mapped registers */
819 module_param_hw(iommap, ulong, other, 0444);
820 MODULE_PARM_DESC(iommap, "physical base for memory mapped I/O (0 = no memory mapped io)");
821
822 /*
823 * some architectures (e.g. intel xscale) align the 8bit serial registers
824 * on 32bit word boundaries.
825 * See linux-kernel/drivers/tty/serial/8250/8250.c serial_in()/out()
826 */
827 module_param_hw(ioshift, int, other, 0444);
828 MODULE_PARM_DESC(ioshift, "shift I/O register offset (0 = no shift)");
829
830 module_param_hw(irq, int, irq, 0444);
831 MODULE_PARM_DESC(irq, "Interrupt (4 or 3)");
832
833 module_param_hw(share_irq, bool, other, 0444);
834 MODULE_PARM_DESC(share_irq, "Share interrupts (0 = off, 1 = on)");
835
836 module_param(sense, int, 0444);
837 MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit (0 = active high, 1 = active low )");
838
839 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
840 module_param(txsense, bool, 0444);
841 MODULE_PARM_DESC(txsense, "Sense of transmitter circuit (0 = active high, 1 = active low )");
842 #endif
843
844 module_param(softcarrier, bool, 0444);
845 MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
846