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