blob: 80b3a6736dbdbf006f041b071d3c1de8776a3858 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jarod Wilson404f3e92010-07-26 20:32:03 -03002/*
Sean Young402e7b62017-03-07 16:13:15 -03003 * IR SIR driver, (C) 2000 Milan Pikula <www@fornax.sk>
Jarod Wilson404f3e92010-07-26 20:32:03 -03004 *
Sean Youngcc063932016-12-19 20:07:08 -02005 * sir_ir - Device driver for use with SIR (serial infra red)
Jarod Wilson404f3e92010-07-26 20:32:03 -03006 * mode of IrDA on many notebooks.
Jarod Wilson404f3e92010-07-26 20:32:03 -03007 */
8
YAMANE Toshiaki014f0062012-11-08 15:53:37 -03009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Jarod Wilson404f3e92010-07-26 20:32:03 -030011#include <linux/module.h>
Jarod Wilson404f3e92010-07-26 20:32:03 -030012#include <linux/interrupt.h>
Jarod Wilson404f3e92010-07-26 20:32:03 -030013#include <linux/kernel.h>
14#include <linux/serial_reg.h>
Ksenija Stanojevic34668352015-05-22 12:58:42 -030015#include <linux/ktime.h>
Jarod Wilson404f3e92010-07-26 20:32:03 -030016#include <linux/delay.h>
Jarod Wilson4b71ca62012-06-04 13:05:24 -030017#include <linux/platform_device.h>
Jarod Wilson404f3e92010-07-26 20:32:03 -030018
Sean Youngcc063932016-12-19 20:07:08 -020019#include <media/rc-core.h>
Jarod Wilson404f3e92010-07-26 20:32:03 -030020
21/* SECTION: Definitions */
Jarod Wilson404f3e92010-07-26 20:32:03 -030022#define PULSE '['
23
Jarod Wilson404f3e92010-07-26 20:32:03 -030024/* 9bit * 1s/115200bit in milli seconds = 78.125ms*/
Sean Young43371f62017-03-07 16:25:45 -030025#define TIME_CONST (9000000ul / 115200ul)
Jarod Wilson404f3e92010-07-26 20:32:03 -030026
27/* timeout for sequences in jiffies (=5/100s), must be longer than TIME_CONST */
Sean Young43371f62017-03-07 16:25:45 -030028#define SIR_TIMEOUT (HZ * 5 / 100)
Jarod Wilson404f3e92010-07-26 20:32:03 -030029
Jarod Wilson404f3e92010-07-26 20:32:03 -030030/* onboard sir ports are typically com3 */
Sean Young402e7b62017-03-07 16:13:15 -030031static int io = 0x3e8;
32static int irq = 4;
Jarod Wilson404f3e92010-07-26 20:32:03 -030033static int threshold = 3;
Jarod Wilson404f3e92010-07-26 20:32:03 -030034
35static DEFINE_SPINLOCK(timer_lock);
36static struct timer_list timerlist;
37/* time of last signal change detected */
Ksenija Stanojevic34668352015-05-22 12:58:42 -030038static ktime_t last;
Jarod Wilson404f3e92010-07-26 20:32:03 -030039/* time of last UART data ready interrupt */
Ksenija Stanojevic34668352015-05-22 12:58:42 -030040static ktime_t last_intr_time;
Jarod Wilson404f3e92010-07-26 20:32:03 -030041static int last_value;
Sean Youngcc063932016-12-19 20:07:08 -020042static struct rc_dev *rcdev;
Jarod Wilson404f3e92010-07-26 20:32:03 -030043
Sean Youngcc063932016-12-19 20:07:08 -020044static struct platform_device *sir_ir_dev;
Jarod Wilson404f3e92010-07-26 20:32:03 -030045
46static DEFINE_SPINLOCK(hardware_lock);
47
Jarod Wilson404f3e92010-07-26 20:32:03 -030048/* SECTION: Prototypes */
49
50/* Communication with user-space */
Jarod Wilson404f3e92010-07-26 20:32:03 -030051static void add_read_queue(int flag, unsigned long val);
Jarod Wilson404f3e92010-07-26 20:32:03 -030052/* Hardware */
53static irqreturn_t sir_interrupt(int irq, void *dev_id);
54static void send_space(unsigned long len);
55static void send_pulse(unsigned long len);
Sean Young30b4e122017-11-08 16:19:45 -050056static int init_hardware(void);
Jarod Wilson404f3e92010-07-26 20:32:03 -030057static void drop_hardware(void);
58/* Initialisation */
Jarod Wilson404f3e92010-07-26 20:32:03 -030059
Jarod Wilson404f3e92010-07-26 20:32:03 -030060static inline unsigned int sinp(int offset)
61{
62 return inb(io + offset);
63}
64
65static inline void soutp(int offset, int value)
66{
67 outb(value, io + offset);
68}
Jarod Wilson404f3e92010-07-26 20:32:03 -030069
Jarod Wilson404f3e92010-07-26 20:32:03 -030070/* SECTION: Communication with user-space */
Sean Youngcc063932016-12-19 20:07:08 -020071static int sir_tx_ir(struct rc_dev *dev, unsigned int *tx_buf,
72 unsigned int count)
Jarod Wilson404f3e92010-07-26 20:32:03 -030073{
74 unsigned long flags;
Sean Youngcc063932016-12-19 20:07:08 -020075 int i;
Jarod Wilson404f3e92010-07-26 20:32:03 -030076
Jarod Wilson404f3e92010-07-26 20:32:03 -030077 local_irq_save(flags);
Sean Youngcc063932016-12-19 20:07:08 -020078 for (i = 0; i < count;) {
Jarod Wilson404f3e92010-07-26 20:32:03 -030079 if (tx_buf[i])
80 send_pulse(tx_buf[i]);
81 i++;
82 if (i >= count)
83 break;
84 if (tx_buf[i])
85 send_space(tx_buf[i]);
86 i++;
87 }
88 local_irq_restore(flags);
Sean Youngcc063932016-12-19 20:07:08 -020089
Jarod Wilson404f3e92010-07-26 20:32:03 -030090 return count;
91}
92
Jarod Wilson404f3e92010-07-26 20:32:03 -030093static void add_read_queue(int flag, unsigned long val)
94{
Sean Young183e19f2018-08-21 15:57:52 -040095 struct ir_raw_event ev = {};
Jarod Wilson404f3e92010-07-26 20:32:03 -030096
Aya Mahfouzc96bf1d2014-10-26 19:46:00 +020097 pr_debug("add flag %d with val %lu\n", flag, val);
Jarod Wilson404f3e92010-07-26 20:32:03 -030098
Jarod Wilson404f3e92010-07-26 20:32:03 -030099 /*
100 * statistically, pulses are ~TIME_CONST/2 too long. we could
101 * maybe make this more exact, but this is good enough
102 */
103 if (flag) {
104 /* pulse */
Sean Youngcc063932016-12-19 20:07:08 -0200105 if (val > TIME_CONST / 2)
106 val -= TIME_CONST / 2;
Jarod Wilson404f3e92010-07-26 20:32:03 -0300107 else /* should not ever happen */
Sean Youngcc063932016-12-19 20:07:08 -0200108 val = 1;
109 ev.pulse = true;
Jarod Wilson404f3e92010-07-26 20:32:03 -0300110 } else {
Sean Youngcc063932016-12-19 20:07:08 -0200111 val += TIME_CONST / 2;
Jarod Wilson404f3e92010-07-26 20:32:03 -0300112 }
Sean Youngcc063932016-12-19 20:07:08 -0200113 ev.duration = US_TO_NS(val);
114
115 ir_raw_event_store_with_filter(rcdev, &ev);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300116}
117
Jarod Wilson404f3e92010-07-26 20:32:03 -0300118/* SECTION: Hardware */
Kees Cookb17ec78a2017-10-24 11:23:14 -0400119static void sir_timeout(struct timer_list *unused)
Jarod Wilson404f3e92010-07-26 20:32:03 -0300120{
121 /*
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.
126 */
127
128 unsigned long flags;
129 unsigned long pulse_end;
130
131 /* avoid interference with interrupt */
132 spin_lock_irqsave(&timer_lock, flags);
133 if (last_value) {
Jarod Wilson404f3e92010-07-26 20:32:03 -0300134 /* clear unread bits in UART and restart */
135 outb(UART_FCR_CLEAR_RCVR, io + UART_FCR);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300136 /* determine 'virtual' pulse end: */
Ksenija Stanojevic34668352015-05-22 12:58:42 -0300137 pulse_end = min_t(unsigned long,
138 ktime_us_delta(last, last_intr_time),
Sean Youngcc063932016-12-19 20:07:08 -0200139 IR_MAX_DURATION);
140 dev_dbg(&sir_ir_dev->dev, "timeout add %d for %lu usec\n",
141 last_value, pulse_end);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300142 add_read_queue(last_value, pulse_end);
143 last_value = 0;
Ksenija Stanojevic34668352015-05-22 12:58:42 -0300144 last = last_intr_time;
Jarod Wilson404f3e92010-07-26 20:32:03 -0300145 }
146 spin_unlock_irqrestore(&timer_lock, flags);
Sean Youngcc063932016-12-19 20:07:08 -0200147 ir_raw_event_handle(rcdev);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300148}
149
150static irqreturn_t sir_interrupt(int irq, void *dev_id)
151{
152 unsigned char data;
Ksenija Stanojevic34668352015-05-22 12:58:42 -0300153 ktime_t curr_time;
Gustavo A. R. Silva6efa0942017-07-05 14:16:04 -0400154 unsigned long delt;
Ksenija Stanojevic34668352015-05-22 12:58:42 -0300155 unsigned long deltintr;
Jarod Wilson404f3e92010-07-26 20:32:03 -0300156 unsigned long flags;
Sean Young592ddc92017-05-16 04:56:14 -0300157 int counter = 0;
Jarod Wilson404f3e92010-07-26 20:32:03 -0300158 int iir, lsr;
159
160 while ((iir = inb(io + UART_IIR) & UART_IIR_ID)) {
Sean Young592ddc92017-05-16 04:56:14 -0300161 if (++counter > 256) {
162 dev_err(&sir_ir_dev->dev, "Trapped in interrupt");
163 break;
164 }
165
Sean Young43371f62017-03-07 16:25:45 -0300166 switch (iir & UART_IIR_ID) { /* FIXME toto treba preriedit */
Jarod Wilson404f3e92010-07-26 20:32:03 -0300167 case UART_IIR_MSI:
Sean Young43371f62017-03-07 16:25:45 -0300168 (void)inb(io + UART_MSR);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300169 break;
170 case UART_IIR_RLSI:
Jarod Wilson404f3e92010-07-26 20:32:03 -0300171 case UART_IIR_THRI:
Sean Young43371f62017-03-07 16:25:45 -0300172 (void)inb(io + UART_LSR);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300173 break;
174 case UART_IIR_RDI:
175 /* avoid interference with timer */
176 spin_lock_irqsave(&timer_lock, flags);
177 do {
178 del_timer(&timerlist);
179 data = inb(io + UART_RX);
Ksenija Stanojevic34668352015-05-22 12:58:42 -0300180 curr_time = ktime_get();
181 delt = min_t(unsigned long,
182 ktime_us_delta(last, curr_time),
Sean Youngcc063932016-12-19 20:07:08 -0200183 IR_MAX_DURATION);
Ksenija Stanojevic34668352015-05-22 12:58:42 -0300184 deltintr = min_t(unsigned long,
185 ktime_us_delta(last_intr_time,
186 curr_time),
Sean Youngcc063932016-12-19 20:07:08 -0200187 IR_MAX_DURATION);
188 dev_dbg(&sir_ir_dev->dev, "t %lu, d %d\n",
189 deltintr, (int)data);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300190 /*
191 * if nothing came in last X cycles,
192 * it was gap
193 */
Ksenija Stanojevic34668352015-05-22 12:58:42 -0300194 if (deltintr > TIME_CONST * threshold) {
Jarod Wilson404f3e92010-07-26 20:32:03 -0300195 if (last_value) {
Sean Youngcc063932016-12-19 20:07:08 -0200196 dev_dbg(&sir_ir_dev->dev, "GAP\n");
Jarod Wilson404f3e92010-07-26 20:32:03 -0300197 /* simulate signal change */
198 add_read_queue(last_value,
Ksenija Stanojevic34668352015-05-22 12:58:42 -0300199 delt -
200 deltintr);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300201 last_value = 0;
Ksenija Stanojevic34668352015-05-22 12:58:42 -0300202 last = last_intr_time;
203 delt = deltintr;
Jarod Wilson404f3e92010-07-26 20:32:03 -0300204 }
205 }
206 data = 1;
207 if (data ^ last_value) {
208 /*
Ksenija Stanojevic34668352015-05-22 12:58:42 -0300209 * deltintr > 2*TIME_CONST, remember?
Jarod Wilson404f3e92010-07-26 20:32:03 -0300210 * the other case is timeout
211 */
212 add_read_queue(last_value,
Sean Young43371f62017-03-07 16:25:45 -0300213 delt - TIME_CONST);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300214 last_value = data;
Ksenija Stanojevic34668352015-05-22 12:58:42 -0300215 last = curr_time;
216 last = ktime_sub_us(last,
217 TIME_CONST);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300218 }
Ksenija Stanojevic34668352015-05-22 12:58:42 -0300219 last_intr_time = curr_time;
Jarod Wilson404f3e92010-07-26 20:32:03 -0300220 if (data) {
221 /*
222 * start timer for end of
223 * sequence detection
224 */
225 timerlist.expires = jiffies +
226 SIR_TIMEOUT;
227 add_timer(&timerlist);
228 }
229
230 lsr = inb(io + UART_LSR);
231 } while (lsr & UART_LSR_DR); /* data ready */
232 spin_unlock_irqrestore(&timer_lock, flags);
233 break;
234 default:
235 break;
236 }
237 }
Sean Youngcc063932016-12-19 20:07:08 -0200238 ir_raw_event_handle(rcdev);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300239 return IRQ_RETVAL(IRQ_HANDLED);
240}
241
Jarod Wilson404f3e92010-07-26 20:32:03 -0300242static void send_space(unsigned long len)
243{
Sean Young19bc4e02017-03-07 17:01:48 -0300244 usleep_range(len, len + 25);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300245}
246
247static void send_pulse(unsigned long len)
248{
249 long bytes_out = len / TIME_CONST;
Jarod Wilson404f3e92010-07-26 20:32:03 -0300250
Jarod Wilson04f561f2011-05-27 15:46:19 -0300251 if (bytes_out == 0)
Jarod Wilson404f3e92010-07-26 20:32:03 -0300252 bytes_out++;
Jarod Wilson04f561f2011-05-27 15:46:19 -0300253
Jarod Wilson404f3e92010-07-26 20:32:03 -0300254 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))
258 ;
259 }
Jarod Wilson404f3e92010-07-26 20:32:03 -0300260}
Jarod Wilson404f3e92010-07-26 20:32:03 -0300261
Sean Young30b4e122017-11-08 16:19:45 -0500262static int init_hardware(void)
Jarod Wilson404f3e92010-07-26 20:32:03 -0300263{
Sean Young30b4e122017-11-08 16:19:45 -0500264 u8 scratch, scratch2, scratch3;
Jarod Wilson404f3e92010-07-26 20:32:03 -0300265 unsigned long flags;
266
267 spin_lock_irqsave(&hardware_lock, flags);
Sean Young30b4e122017-11-08 16:19:45 -0500268
269 /*
270 * This is a simple port existence test, borrowed from the autoconfig
271 * function in drivers/tty/serial/8250/8250_port.c
272 */
273 scratch = sinp(UART_IER);
274 soutp(UART_IER, 0);
275#ifdef __i386__
276 outb(0xff, 0x080);
277#endif
278 scratch2 = sinp(UART_IER) & 0x0f;
279 soutp(UART_IER, 0x0f);
280#ifdef __i386__
281 outb(0x00, 0x080);
282#endif
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");
289 return -ENODEV;
290 }
291
Jarod Wilson404f3e92010-07-26 20:32:03 -0300292 /* reset UART */
Jarod Wilson404f3e92010-07-26 20:32:03 -0300293 outb(0, io + UART_MCR);
294 outb(0, io + UART_IER);
295 /* init UART */
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);
301 /* FIFO operation */
302 outb(UART_FCR_ENABLE_FIFO, io + UART_FCR);
303 /* interrupts */
304 /* outb(UART_IER_RLSI|UART_IER_RDI|UART_IER_THRI, io + UART_IER); */
305 outb(UART_IER_RDI, io + UART_IER);
306 /* turn on UART */
Sean Young43371f62017-03-07 16:25:45 -0300307 outb(UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2, io + UART_MCR);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300308 spin_unlock_irqrestore(&hardware_lock, flags);
Sean Young30b4e122017-11-08 16:19:45 -0500309
310 return 0;
Jarod Wilson404f3e92010-07-26 20:32:03 -0300311}
312
313static void drop_hardware(void)
314{
315 unsigned long flags;
316
317 spin_lock_irqsave(&hardware_lock, flags);
318
Jarod Wilson404f3e92010-07-26 20:32:03 -0300319 /* turn off interrupts */
320 outb(0, io + UART_IER);
Arnd Bergmannc72374f2014-06-05 22:48:11 +0200321
Jarod Wilson404f3e92010-07-26 20:32:03 -0300322 spin_unlock_irqrestore(&hardware_lock, flags);
323}
324
325/* SECTION: Initialisation */
Sean Youngc52f2ba2017-05-17 14:32:53 -0300326static int sir_ir_probe(struct platform_device *dev)
Jarod Wilson404f3e92010-07-26 20:32:03 -0300327{
328 int retval;
329
Sean Youngc52f2ba2017-05-17 14:32:53 -0300330 rcdev = devm_rc_allocate_device(&sir_ir_dev->dev, RC_DRIVER_IR_RAW);
331 if (!rcdev)
332 return -ENOMEM;
333
Sean Young518f4b22017-07-01 12:13:19 -0400334 rcdev->device_name = "SIR IrDA port";
Sean Youngc52f2ba2017-05-17 14:32:53 -0300335 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;
Sean Young6d741bf2017-08-07 16:20:58 -0400341 rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
Sean Youngc52f2ba2017-05-17 14:32:53 -0300342 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;
346
Kees Cookb17ec78a2017-10-24 11:23:14 -0400347 timer_setup(&timerlist, sir_timeout, 0);
Sean Young8c7c6ca2017-03-25 08:45:38 -0300348
Jarod Wilson404f3e92010-07-26 20:32:03 -0300349 /* get I/O port access and IRQ line */
Sean Youngb462e1b2017-05-17 14:32:51 -0300350 if (!devm_request_region(&sir_ir_dev->dev, io, 8, KBUILD_MODNAME)) {
YAMANE Toshiaki014f0062012-11-08 15:53:37 -0300351 pr_err("i/o port 0x%.4x already in use.\n", io);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300352 return -EBUSY;
353 }
Sean Youngb462e1b2017-05-17 14:32:51 -0300354 retval = devm_request_irq(&sir_ir_dev->dev, irq, sir_interrupt, 0,
355 KBUILD_MODNAME, NULL);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300356 if (retval < 0) {
YAMANE Toshiaki014f0062012-11-08 15:53:37 -0300357 pr_err("IRQ %d already in use.\n", irq);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300358 return retval;
359 }
Sean Young30b4e122017-11-08 16:19:45 -0500360
361 retval = init_hardware();
362 if (retval) {
363 del_timer_sync(&timerlist);
364 return retval;
365 }
366
YAMANE Toshiaki014f0062012-11-08 15:53:37 -0300367 pr_info("I/O port 0x%.4x, IRQ %d.\n", io, irq);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300368
Sean Youngc52f2ba2017-05-17 14:32:53 -0300369 retval = devm_rc_register_device(&sir_ir_dev->dev, rcdev);
Sean Youngcf9ed9a2017-03-25 07:31:57 -0300370 if (retval < 0)
371 return retval;
372
Sean Youngc52f2ba2017-05-17 14:32:53 -0300373 return 0;
Jarod Wilson4b71ca62012-06-04 13:05:24 -0300374}
375
Sean Youngcc063932016-12-19 20:07:08 -0200376static int sir_ir_remove(struct platform_device *dev)
Jarod Wilson4b71ca62012-06-04 13:05:24 -0300377{
Sean Young1beb5a72017-05-17 14:32:50 -0300378 drop_hardware();
Sean Youngf23f54082017-05-17 14:32:52 -0300379 del_timer_sync(&timerlist);
Jarod Wilson4b71ca62012-06-04 13:05:24 -0300380 return 0;
381}
382
Sean Youngcc063932016-12-19 20:07:08 -0200383static struct platform_driver sir_ir_driver = {
384 .probe = sir_ir_probe,
385 .remove = sir_ir_remove,
Jarod Wilson4b71ca62012-06-04 13:05:24 -0300386 .driver = {
Sean Youngcc063932016-12-19 20:07:08 -0200387 .name = "sir_ir",
Jarod Wilson4b71ca62012-06-04 13:05:24 -0300388 },
389};
Jarod Wilson404f3e92010-07-26 20:32:03 -0300390
Sean Youngcc063932016-12-19 20:07:08 -0200391static int __init sir_ir_init(void)
Jarod Wilson404f3e92010-07-26 20:32:03 -0300392{
393 int retval;
394
Sean Youngcc063932016-12-19 20:07:08 -0200395 retval = platform_driver_register(&sir_ir_driver);
Sean Young4d7cf7e2017-03-25 07:41:39 -0300396 if (retval)
397 return retval;
Jarod Wilson4b71ca62012-06-04 13:05:24 -0300398
Sean Youngcc063932016-12-19 20:07:08 -0200399 sir_ir_dev = platform_device_alloc("sir_ir", 0);
400 if (!sir_ir_dev) {
Jarod Wilson4b71ca62012-06-04 13:05:24 -0300401 retval = -ENOMEM;
402 goto pdev_alloc_fail;
403 }
404
Sean Youngcc063932016-12-19 20:07:08 -0200405 retval = platform_device_add(sir_ir_dev);
Sean Young4d7cf7e2017-03-25 07:41:39 -0300406 if (retval)
Jarod Wilson4b71ca62012-06-04 13:05:24 -0300407 goto pdev_add_fail;
Jarod Wilson4b71ca62012-06-04 13:05:24 -0300408
Jarod Wilson404f3e92010-07-26 20:32:03 -0300409 return 0;
Jarod Wilson4b71ca62012-06-04 13:05:24 -0300410
Jarod Wilson4b71ca62012-06-04 13:05:24 -0300411pdev_add_fail:
Sean Youngcc063932016-12-19 20:07:08 -0200412 platform_device_put(sir_ir_dev);
Jarod Wilson4b71ca62012-06-04 13:05:24 -0300413pdev_alloc_fail:
Sean Youngcc063932016-12-19 20:07:08 -0200414 platform_driver_unregister(&sir_ir_driver);
Jarod Wilson4b71ca62012-06-04 13:05:24 -0300415 return retval;
Jarod Wilson404f3e92010-07-26 20:32:03 -0300416}
417
Sean Youngcc063932016-12-19 20:07:08 -0200418static void __exit sir_ir_exit(void)
Jarod Wilson404f3e92010-07-26 20:32:03 -0300419{
Sean Youngcc063932016-12-19 20:07:08 -0200420 platform_device_unregister(sir_ir_dev);
421 platform_driver_unregister(&sir_ir_driver);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300422}
423
Sean Youngcc063932016-12-19 20:07:08 -0200424module_init(sir_ir_init);
425module_exit(sir_ir_exit);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300426
Jarod Wilson404f3e92010-07-26 20:32:03 -0300427MODULE_DESCRIPTION("Infrared receiver driver for SIR type serial ports");
428MODULE_AUTHOR("Milan Pikula");
Jarod Wilson404f3e92010-07-26 20:32:03 -0300429MODULE_LICENSE("GPL");
430
Sean Young6709e032017-06-02 07:19:39 -0300431module_param_hw(io, int, ioport, 0444);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300432MODULE_PARM_DESC(io, "I/O address base (0x3f8 or 0x2f8)");
433
Sean Young6709e032017-06-02 07:19:39 -0300434module_param_hw(irq, int, irq, 0444);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300435MODULE_PARM_DESC(irq, "Interrupt (4 or 3)");
436
Derek Robson5cd65222017-02-10 22:42:38 -0200437module_param(threshold, int, 0444);
Jarod Wilson404f3e92010-07-26 20:32:03 -0300438MODULE_PARM_DESC(threshold, "space detection threshold (3)");