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