Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * serial_ir.c
0004  *
0005  * serial_ir - Device driver that records pulse- and pause-lengths
0006  *         (space-lengths) between DDCD event on a serial port.
0007  *
0008  * Copyright (C) 1996,97 Ralph Metzler <rjkm@thp.uni-koeln.de>
0009  * Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>
0010  * Copyright (C) 1998 Ben Pfaff <blp@gnu.org>
0011  * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
0012  * Copyright (C) 2007 Andrei Tanas <andrei@tanas.ca> (suspend/resume support)
0013  * Copyright (C) 2016 Sean Young <sean@mess.org> (port to rc-core)
0014  */
0015 
0016 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0017 
0018 #include <linux/module.h>
0019 #include <linux/errno.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/kernel.h>
0022 #include <linux/serial_reg.h>
0023 #include <linux/types.h>
0024 #include <linux/delay.h>
0025 #include <linux/platform_device.h>
0026 #include <linux/spinlock.h>
0027 #include <media/rc-core.h>
0028 
0029 struct serial_ir_hw {
0030     int signal_pin;
0031     int signal_pin_change;
0032     u8 on;
0033     u8 off;
0034     unsigned set_send_carrier:1;
0035     unsigned set_duty_cycle:1;
0036     void (*send_pulse)(unsigned int length, ktime_t edge);
0037     void (*send_space)(void);
0038     spinlock_t lock;
0039 };
0040 
0041 #define IR_HOMEBREW 0
0042 #define IR_IRDEO    1
0043 #define IR_IRDEO_REMOTE 2
0044 #define IR_ANIMAX   3
0045 #define IR_IGOR     4
0046 
0047 /* module parameters */
0048 static int type;
0049 static int io;
0050 static int irq;
0051 static ulong iommap;
0052 static int ioshift;
0053 static bool softcarrier = true;
0054 static bool share_irq;
0055 static int sense = -1;  /* -1 = auto, 0 = active high, 1 = active low */
0056 static bool txsense;    /* 0 = active high, 1 = active low */
0057 
0058 /* forward declarations */
0059 static void send_pulse_irdeo(unsigned int length, ktime_t edge);
0060 static void send_space_irdeo(void);
0061 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
0062 static void send_pulse_homebrew(unsigned int length, ktime_t edge);
0063 static void send_space_homebrew(void);
0064 #endif
0065 
0066 static struct serial_ir_hw hardware[] = {
0067     [IR_HOMEBREW] = {
0068         .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_HOMEBREW].lock),
0069         .signal_pin    = UART_MSR_DCD,
0070         .signal_pin_change = UART_MSR_DDCD,
0071         .on  = (UART_MCR_RTS | UART_MCR_OUT2 | UART_MCR_DTR),
0072         .off = (UART_MCR_RTS | UART_MCR_OUT2),
0073 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
0074         .send_pulse = send_pulse_homebrew,
0075         .send_space = send_space_homebrew,
0076         .set_send_carrier = true,
0077         .set_duty_cycle = true,
0078 #endif
0079     },
0080 
0081     [IR_IRDEO] = {
0082         .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_IRDEO].lock),
0083         .signal_pin    = UART_MSR_DSR,
0084         .signal_pin_change = UART_MSR_DDSR,
0085         .on  = UART_MCR_OUT2,
0086         .off = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
0087         .send_pulse = send_pulse_irdeo,
0088         .send_space = send_space_irdeo,
0089         .set_duty_cycle = true,
0090     },
0091 
0092     [IR_IRDEO_REMOTE] = {
0093         .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_IRDEO_REMOTE].lock),
0094         .signal_pin    = UART_MSR_DSR,
0095         .signal_pin_change = UART_MSR_DDSR,
0096         .on  = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
0097         .off = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
0098         .send_pulse = send_pulse_irdeo,
0099         .send_space = send_space_irdeo,
0100         .set_duty_cycle = true,
0101     },
0102 
0103     [IR_ANIMAX] = {
0104         .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_ANIMAX].lock),
0105         .signal_pin    = UART_MSR_DCD,
0106         .signal_pin_change = UART_MSR_DDCD,
0107         .on  = 0,
0108         .off = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
0109     },
0110 
0111     [IR_IGOR] = {
0112         .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_IGOR].lock),
0113         .signal_pin    = UART_MSR_DSR,
0114         .signal_pin_change = UART_MSR_DDSR,
0115         .on  = (UART_MCR_RTS | UART_MCR_OUT2 | UART_MCR_DTR),
0116         .off = (UART_MCR_RTS | UART_MCR_OUT2),
0117 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
0118         .send_pulse = send_pulse_homebrew,
0119         .send_space = send_space_homebrew,
0120         .set_send_carrier = true,
0121         .set_duty_cycle = true,
0122 #endif
0123     },
0124 };
0125 
0126 #define RS_ISR_PASS_LIMIT 256
0127 
0128 struct serial_ir {
0129     ktime_t lastkt;
0130     struct rc_dev *rcdev;
0131     struct platform_device *pdev;
0132     struct timer_list timeout_timer;
0133 
0134     unsigned int carrier;
0135     unsigned int duty_cycle;
0136 };
0137 
0138 static struct serial_ir serial_ir;
0139 
0140 /* fetch serial input packet (1 byte) from register offset */
0141 static u8 sinp(int offset)
0142 {
0143     if (iommap)
0144         /* the register is memory-mapped */
0145         offset <<= ioshift;
0146 
0147     return inb(io + offset);
0148 }
0149 
0150 /* write serial output packet (1 byte) of value to register offset */
0151 static void soutp(int offset, u8 value)
0152 {
0153     if (iommap)
0154         /* the register is memory-mapped */
0155         offset <<= ioshift;
0156 
0157     outb(value, io + offset);
0158 }
0159 
0160 static void on(void)
0161 {
0162     if (txsense)
0163         soutp(UART_MCR, hardware[type].off);
0164     else
0165         soutp(UART_MCR, hardware[type].on);
0166 }
0167 
0168 static void off(void)
0169 {
0170     if (txsense)
0171         soutp(UART_MCR, hardware[type].on);
0172     else
0173         soutp(UART_MCR, hardware[type].off);
0174 }
0175 
0176 static void send_pulse_irdeo(unsigned int length, ktime_t target)
0177 {
0178     long rawbits;
0179     int i;
0180     unsigned char output;
0181     unsigned char chunk, shifted;
0182 
0183     /* how many bits have to be sent ? */
0184     rawbits = length * 1152 / 10000;
0185     if (serial_ir.duty_cycle > 50)
0186         chunk = 3;
0187     else
0188         chunk = 1;
0189     for (i = 0, output = 0x7f; rawbits > 0; rawbits -= 3) {
0190         shifted = chunk << (i * 3);
0191         shifted >>= 1;
0192         output &= (~shifted);
0193         i++;
0194         if (i == 3) {
0195             soutp(UART_TX, output);
0196             while (!(sinp(UART_LSR) & UART_LSR_THRE))
0197                 ;
0198             output = 0x7f;
0199             i = 0;
0200         }
0201     }
0202     if (i != 0) {
0203         soutp(UART_TX, output);
0204         while (!(sinp(UART_LSR) & UART_LSR_TEMT))
0205             ;
0206     }
0207 }
0208 
0209 static void send_space_irdeo(void)
0210 {
0211 }
0212 
0213 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
0214 static void send_pulse_homebrew_softcarrier(unsigned int length, ktime_t edge)
0215 {
0216     ktime_t now, target = ktime_add_us(edge, length);
0217     /*
0218      * delta should never exceed 4 seconds and on m68k
0219      * ndelay(s64) does not compile; so use s32 rather than s64.
0220      */
0221     s32 delta;
0222     unsigned int pulse, space;
0223 
0224     /* Ensure the dividend fits into 32 bit */
0225     pulse = DIV_ROUND_CLOSEST(serial_ir.duty_cycle * (NSEC_PER_SEC / 100),
0226                   serial_ir.carrier);
0227     space = DIV_ROUND_CLOSEST((100 - serial_ir.duty_cycle) *
0228                   (NSEC_PER_SEC / 100), serial_ir.carrier);
0229 
0230     for (;;) {
0231         now = ktime_get();
0232         if (ktime_compare(now, target) >= 0)
0233             break;
0234         on();
0235         edge = ktime_add_ns(edge, pulse);
0236         delta = ktime_to_ns(ktime_sub(edge, now));
0237         if (delta > 0)
0238             ndelay(delta);
0239         now = ktime_get();
0240         off();
0241         if (ktime_compare(now, target) >= 0)
0242             break;
0243         edge = ktime_add_ns(edge, space);
0244         delta = ktime_to_ns(ktime_sub(edge, now));
0245         if (delta > 0)
0246             ndelay(delta);
0247     }
0248 }
0249 
0250 static void send_pulse_homebrew(unsigned int length, ktime_t edge)
0251 {
0252     if (softcarrier)
0253         send_pulse_homebrew_softcarrier(length, edge);
0254     else
0255         on();
0256 }
0257 
0258 static void send_space_homebrew(void)
0259 {
0260     off();
0261 }
0262 #endif
0263 
0264 static void frbwrite(unsigned int l, bool is_pulse)
0265 {
0266     /* simple noise filter */
0267     static unsigned int ptr, pulse, space;
0268     struct ir_raw_event ev = {};
0269 
0270     if (ptr > 0 && is_pulse) {
0271         pulse += l;
0272         if (pulse > 250) {
0273             ev.duration = space;
0274             ev.pulse = false;
0275             ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
0276             ev.duration = pulse;
0277             ev.pulse = true;
0278             ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
0279             ptr = 0;
0280             pulse = 0;
0281         }
0282         return;
0283     }
0284     if (!is_pulse) {
0285         if (ptr == 0) {
0286             if (l > 20000) {
0287                 space = l;
0288                 ptr++;
0289                 return;
0290             }
0291         } else {
0292             if (l > 20000) {
0293                 space += pulse;
0294                 if (space > IR_MAX_DURATION)
0295                     space = IR_MAX_DURATION;
0296                 space += l;
0297                 if (space > IR_MAX_DURATION)
0298                     space = IR_MAX_DURATION;
0299                 pulse = 0;
0300                 return;
0301             }
0302 
0303             ev.duration = space;
0304             ev.pulse = false;
0305             ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
0306             ev.duration = pulse;
0307             ev.pulse = true;
0308             ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
0309             ptr = 0;
0310             pulse = 0;
0311         }
0312     }
0313 
0314     ev.duration = l;
0315     ev.pulse = is_pulse;
0316     ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
0317 }
0318 
0319 static irqreturn_t serial_ir_irq_handler(int i, void *blah)
0320 {
0321     ktime_t kt;
0322     int counter, dcd;
0323     u8 status;
0324     ktime_t delkt;
0325     unsigned int data;
0326     static int last_dcd = -1;
0327 
0328     if ((sinp(UART_IIR) & UART_IIR_NO_INT)) {
0329         /* not our interrupt */
0330         return IRQ_NONE;
0331     }
0332 
0333     counter = 0;
0334     do {
0335         counter++;
0336         status = sinp(UART_MSR);
0337         if (counter > RS_ISR_PASS_LIMIT) {
0338             dev_err(&serial_ir.pdev->dev, "Trapped in interrupt");
0339             break;
0340         }
0341         if ((status & hardware[type].signal_pin_change) &&
0342             sense != -1) {
0343             /* get current time */
0344             kt = ktime_get();
0345 
0346             /*
0347              * The driver needs to know if your receiver is
0348              * active high or active low, or the space/pulse
0349              * sense could be inverted.
0350              */
0351 
0352             /* calc time since last interrupt in nanoseconds */
0353             dcd = (status & hardware[type].signal_pin) ? 1 : 0;
0354 
0355             if (dcd == last_dcd) {
0356                 dev_dbg(&serial_ir.pdev->dev,
0357                     "ignoring spike: %d %d %lldns %lldns\n",
0358                     dcd, sense, ktime_to_ns(kt),
0359                     ktime_to_ns(serial_ir.lastkt));
0360                 continue;
0361             }
0362 
0363             delkt = ktime_sub(kt, serial_ir.lastkt);
0364             if (ktime_compare(delkt, ktime_set(15, 0)) > 0) {
0365                 data = IR_MAX_DURATION; /* really long time */
0366                 if (!(dcd ^ sense)) {
0367                     /* sanity check */
0368                     dev_err(&serial_ir.pdev->dev,
0369                         "dcd unexpected: %d %d %lldns %lldns\n",
0370                         dcd, sense, ktime_to_ns(kt),
0371                         ktime_to_ns(serial_ir.lastkt));
0372                     /*
0373                      * detecting pulse while this
0374                      * MUST be a space!
0375                      */
0376                     sense = sense ? 0 : 1;
0377                 }
0378             } else {
0379                 data = ktime_to_us(delkt);
0380             }
0381             frbwrite(data, !(dcd ^ sense));
0382             serial_ir.lastkt = kt;
0383             last_dcd = dcd;
0384         }
0385     } while (!(sinp(UART_IIR) & UART_IIR_NO_INT)); /* still pending ? */
0386 
0387     mod_timer(&serial_ir.timeout_timer,
0388           jiffies + usecs_to_jiffies(serial_ir.rcdev->timeout));
0389 
0390     ir_raw_event_handle(serial_ir.rcdev);
0391 
0392     return IRQ_HANDLED;
0393 }
0394 
0395 static int hardware_init_port(void)
0396 {
0397     u8 scratch, scratch2, scratch3;
0398 
0399     /*
0400      * This is a simple port existence test, borrowed from the autoconfig
0401      * function in drivers/tty/serial/8250/8250_port.c
0402      */
0403     scratch = sinp(UART_IER);
0404     soutp(UART_IER, 0);
0405 #ifdef __i386__
0406     outb(0xff, 0x080);
0407 #endif
0408     scratch2 = sinp(UART_IER) & 0x0f;
0409     soutp(UART_IER, 0x0f);
0410 #ifdef __i386__
0411     outb(0x00, 0x080);
0412 #endif
0413     scratch3 = sinp(UART_IER) & 0x0f;
0414     soutp(UART_IER, scratch);
0415     if (scratch2 != 0 || scratch3 != 0x0f) {
0416         /* we fail, there's nothing here */
0417         pr_err("port existence test failed, cannot continue\n");
0418         return -ENODEV;
0419     }
0420 
0421     /* Set DLAB 0. */
0422     soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
0423 
0424     /* First of all, disable all interrupts */
0425     soutp(UART_IER, sinp(UART_IER) &
0426           (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
0427 
0428     /* Clear registers. */
0429     sinp(UART_LSR);
0430     sinp(UART_RX);
0431     sinp(UART_IIR);
0432     sinp(UART_MSR);
0433 
0434     /* Set line for power source */
0435     off();
0436 
0437     /* Clear registers again to be sure. */
0438     sinp(UART_LSR);
0439     sinp(UART_RX);
0440     sinp(UART_IIR);
0441     sinp(UART_MSR);
0442 
0443     switch (type) {
0444     case IR_IRDEO:
0445     case IR_IRDEO_REMOTE:
0446         /* setup port to 7N1 @ 115200 Baud */
0447         /* 7N1+start = 9 bits at 115200 ~ 3 bits at 38kHz */
0448 
0449         /* Set DLAB 1. */
0450         soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
0451         /* Set divisor to 1 => 115200 Baud */
0452         soutp(UART_DLM, 0);
0453         soutp(UART_DLL, 1);
0454         /* Set DLAB 0 +  7N1 */
0455         soutp(UART_LCR, UART_LCR_WLEN7);
0456         /* THR interrupt already disabled at this point */
0457         break;
0458     default:
0459         break;
0460     }
0461 
0462     return 0;
0463 }
0464 
0465 static void serial_ir_timeout(struct timer_list *unused)
0466 {
0467     struct ir_raw_event ev = {
0468         .timeout = true,
0469         .duration = serial_ir.rcdev->timeout
0470     };
0471     ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
0472     ir_raw_event_handle(serial_ir.rcdev);
0473 }
0474 
0475 /* Needed by serial_ir_probe() */
0476 static int serial_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
0477             unsigned int count);
0478 static int serial_ir_tx_duty_cycle(struct rc_dev *dev, u32 cycle);
0479 static int serial_ir_tx_carrier(struct rc_dev *dev, u32 carrier);
0480 static int serial_ir_open(struct rc_dev *rcdev);
0481 static void serial_ir_close(struct rc_dev *rcdev);
0482 
0483 static int serial_ir_probe(struct platform_device *dev)
0484 {
0485     struct rc_dev *rcdev;
0486     int i, nlow, nhigh, result;
0487 
0488     rcdev = devm_rc_allocate_device(&dev->dev, RC_DRIVER_IR_RAW);
0489     if (!rcdev)
0490         return -ENOMEM;
0491 
0492     if (hardware[type].send_pulse && hardware[type].send_space)
0493         rcdev->tx_ir = serial_ir_tx;
0494     if (hardware[type].set_send_carrier)
0495         rcdev->s_tx_carrier = serial_ir_tx_carrier;
0496     if (hardware[type].set_duty_cycle)
0497         rcdev->s_tx_duty_cycle = serial_ir_tx_duty_cycle;
0498 
0499     switch (type) {
0500     case IR_HOMEBREW:
0501         rcdev->device_name = "Serial IR type home-brew";
0502         break;
0503     case IR_IRDEO:
0504         rcdev->device_name = "Serial IR type IRdeo";
0505         break;
0506     case IR_IRDEO_REMOTE:
0507         rcdev->device_name = "Serial IR type IRdeo remote";
0508         break;
0509     case IR_ANIMAX:
0510         rcdev->device_name = "Serial IR type AnimaX";
0511         break;
0512     case IR_IGOR:
0513         rcdev->device_name = "Serial IR type IgorPlug";
0514         break;
0515     }
0516 
0517     rcdev->input_phys = KBUILD_MODNAME "/input0";
0518     rcdev->input_id.bustype = BUS_HOST;
0519     rcdev->input_id.vendor = 0x0001;
0520     rcdev->input_id.product = 0x0001;
0521     rcdev->input_id.version = 0x0100;
0522     rcdev->open = serial_ir_open;
0523     rcdev->close = serial_ir_close;
0524     rcdev->dev.parent = &serial_ir.pdev->dev;
0525     rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
0526     rcdev->driver_name = KBUILD_MODNAME;
0527     rcdev->map_name = RC_MAP_RC6_MCE;
0528     rcdev->min_timeout = 1;
0529     rcdev->timeout = IR_DEFAULT_TIMEOUT;
0530     rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
0531     rcdev->rx_resolution = 250;
0532 
0533     serial_ir.rcdev = rcdev;
0534 
0535     timer_setup(&serial_ir.timeout_timer, serial_ir_timeout, 0);
0536 
0537     result = devm_request_irq(&dev->dev, irq, serial_ir_irq_handler,
0538                   share_irq ? IRQF_SHARED : 0,
0539                   KBUILD_MODNAME, &hardware);
0540     if (result < 0) {
0541         if (result == -EBUSY)
0542             dev_err(&dev->dev, "IRQ %d busy\n", irq);
0543         else if (result == -EINVAL)
0544             dev_err(&dev->dev, "Bad irq number or handler\n");
0545         return result;
0546     }
0547 
0548     /* Reserve io region. */
0549     if ((iommap &&
0550          (devm_request_mem_region(&dev->dev, iommap, 8UL << ioshift,
0551                       KBUILD_MODNAME) == NULL)) ||
0552          (!iommap && (devm_request_region(&dev->dev, io, 8,
0553               KBUILD_MODNAME) == NULL))) {
0554         dev_err(&dev->dev, "port %04x already in use\n", io);
0555         dev_warn(&dev->dev, "use 'setserial /dev/ttySX uart none'\n");
0556         dev_warn(&dev->dev,
0557              "or compile the serial port driver as module and\n");
0558         dev_warn(&dev->dev, "make sure this module is loaded first\n");
0559         return -EBUSY;
0560     }
0561 
0562     result = hardware_init_port();
0563     if (result < 0)
0564         return result;
0565 
0566     /* Initialize pulse/space widths */
0567     serial_ir.duty_cycle = 50;
0568     serial_ir.carrier = 38000;
0569 
0570     /* If pin is high, then this must be an active low receiver. */
0571     if (sense == -1) {
0572         /* wait 1/2 sec for the power supply */
0573         msleep(500);
0574 
0575         /*
0576          * probe 9 times every 0.04s, collect "votes" for
0577          * active high/low
0578          */
0579         nlow = 0;
0580         nhigh = 0;
0581         for (i = 0; i < 9; i++) {
0582             if (sinp(UART_MSR) & hardware[type].signal_pin)
0583                 nlow++;
0584             else
0585                 nhigh++;
0586             msleep(40);
0587         }
0588         sense = nlow >= nhigh ? 1 : 0;
0589         dev_info(&dev->dev, "auto-detected active %s receiver\n",
0590              sense ? "low" : "high");
0591     } else
0592         dev_info(&dev->dev, "Manually using active %s receiver\n",
0593              sense ? "low" : "high");
0594 
0595     dev_dbg(&dev->dev, "Interrupt %d, port %04x obtained\n", irq, io);
0596 
0597     return devm_rc_register_device(&dev->dev, rcdev);
0598 }
0599 
0600 static int serial_ir_open(struct rc_dev *rcdev)
0601 {
0602     unsigned long flags;
0603 
0604     /* initialize timestamp */
0605     serial_ir.lastkt = ktime_get();
0606 
0607     spin_lock_irqsave(&hardware[type].lock, flags);
0608 
0609     /* Set DLAB 0. */
0610     soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
0611 
0612     soutp(UART_IER, sinp(UART_IER) | UART_IER_MSI);
0613 
0614     spin_unlock_irqrestore(&hardware[type].lock, flags);
0615 
0616     return 0;
0617 }
0618 
0619 static void serial_ir_close(struct rc_dev *rcdev)
0620 {
0621     unsigned long flags;
0622 
0623     spin_lock_irqsave(&hardware[type].lock, flags);
0624 
0625     /* Set DLAB 0. */
0626     soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
0627 
0628     /* First of all, disable all interrupts */
0629     soutp(UART_IER, sinp(UART_IER) &
0630           (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
0631     spin_unlock_irqrestore(&hardware[type].lock, flags);
0632 }
0633 
0634 static int serial_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
0635             unsigned int count)
0636 {
0637     unsigned long flags;
0638     ktime_t edge;
0639     s64 delta;
0640     int i;
0641 
0642     spin_lock_irqsave(&hardware[type].lock, flags);
0643     if (type == IR_IRDEO) {
0644         /* DTR, RTS down */
0645         on();
0646     }
0647 
0648     edge = ktime_get();
0649     for (i = 0; i < count; i++) {
0650         if (i % 2)
0651             hardware[type].send_space();
0652         else
0653             hardware[type].send_pulse(txbuf[i], edge);
0654 
0655         edge = ktime_add_us(edge, txbuf[i]);
0656         delta = ktime_us_delta(edge, ktime_get());
0657         if (delta > 25) {
0658             spin_unlock_irqrestore(&hardware[type].lock, flags);
0659             usleep_range(delta - 25, delta + 25);
0660             spin_lock_irqsave(&hardware[type].lock, flags);
0661         } else if (delta > 0) {
0662             udelay(delta);
0663         }
0664     }
0665     off();
0666     spin_unlock_irqrestore(&hardware[type].lock, flags);
0667     return count;
0668 }
0669 
0670 static int serial_ir_tx_duty_cycle(struct rc_dev *dev, u32 cycle)
0671 {
0672     serial_ir.duty_cycle = cycle;
0673     return 0;
0674 }
0675 
0676 static int serial_ir_tx_carrier(struct rc_dev *dev, u32 carrier)
0677 {
0678     if (carrier > 500000 || carrier < 20000)
0679         return -EINVAL;
0680 
0681     serial_ir.carrier = carrier;
0682     return 0;
0683 }
0684 
0685 static int serial_ir_suspend(struct platform_device *dev,
0686                  pm_message_t state)
0687 {
0688     /* Set DLAB 0. */
0689     soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
0690 
0691     /* Disable all interrupts */
0692     soutp(UART_IER, sinp(UART_IER) &
0693           (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
0694 
0695     /* Clear registers. */
0696     sinp(UART_LSR);
0697     sinp(UART_RX);
0698     sinp(UART_IIR);
0699     sinp(UART_MSR);
0700 
0701     return 0;
0702 }
0703 
0704 static int serial_ir_resume(struct platform_device *dev)
0705 {
0706     unsigned long flags;
0707     int result;
0708 
0709     result = hardware_init_port();
0710     if (result < 0)
0711         return result;
0712 
0713     spin_lock_irqsave(&hardware[type].lock, flags);
0714     /* Enable Interrupt */
0715     serial_ir.lastkt = ktime_get();
0716     soutp(UART_IER, sinp(UART_IER) | UART_IER_MSI);
0717     off();
0718 
0719     spin_unlock_irqrestore(&hardware[type].lock, flags);
0720 
0721     return 0;
0722 }
0723 
0724 static struct platform_driver serial_ir_driver = {
0725     .probe      = serial_ir_probe,
0726     .suspend    = serial_ir_suspend,
0727     .resume     = serial_ir_resume,
0728     .driver     = {
0729         .name   = "serial_ir",
0730     },
0731 };
0732 
0733 static int __init serial_ir_init(void)
0734 {
0735     int result;
0736 
0737     result = platform_driver_register(&serial_ir_driver);
0738     if (result)
0739         return result;
0740 
0741     serial_ir.pdev = platform_device_alloc("serial_ir", 0);
0742     if (!serial_ir.pdev) {
0743         result = -ENOMEM;
0744         goto exit_driver_unregister;
0745     }
0746 
0747     result = platform_device_add(serial_ir.pdev);
0748     if (result)
0749         goto exit_device_put;
0750 
0751     return 0;
0752 
0753 exit_device_put:
0754     platform_device_put(serial_ir.pdev);
0755 exit_driver_unregister:
0756     platform_driver_unregister(&serial_ir_driver);
0757     return result;
0758 }
0759 
0760 static void serial_ir_exit(void)
0761 {
0762     platform_device_unregister(serial_ir.pdev);
0763     platform_driver_unregister(&serial_ir_driver);
0764 }
0765 
0766 static int __init serial_ir_init_module(void)
0767 {
0768     switch (type) {
0769     case IR_HOMEBREW:
0770     case IR_IRDEO:
0771     case IR_IRDEO_REMOTE:
0772     case IR_ANIMAX:
0773     case IR_IGOR:
0774         /* if nothing specified, use ttyS0/com1 and irq 4 */
0775         io = io ? io : 0x3f8;
0776         irq = irq ? irq : 4;
0777         break;
0778     default:
0779         return -EINVAL;
0780     }
0781     if (!softcarrier) {
0782         switch (type) {
0783         case IR_HOMEBREW:
0784         case IR_IGOR:
0785             hardware[type].set_send_carrier = false;
0786             hardware[type].set_duty_cycle = false;
0787             break;
0788         }
0789     }
0790 
0791     /* make sure sense is either -1, 0, or 1 */
0792     if (sense != -1)
0793         sense = !!sense;
0794 
0795     return serial_ir_init();
0796 }
0797 
0798 static void __exit serial_ir_exit_module(void)
0799 {
0800     del_timer_sync(&serial_ir.timeout_timer);
0801     serial_ir_exit();
0802 }
0803 
0804 module_init(serial_ir_init_module);
0805 module_exit(serial_ir_exit_module);
0806 
0807 MODULE_DESCRIPTION("Infra-red receiver driver for serial ports.");
0808 MODULE_AUTHOR("Ralph Metzler, Trent Piepho, Ben Pfaff, Christoph Bartelmus, Andrei Tanas");
0809 MODULE_LICENSE("GPL");
0810 
0811 module_param(type, int, 0444);
0812 MODULE_PARM_DESC(type, "Hardware type (0 = home-brew, 1 = IRdeo, 2 = IRdeo Remote, 3 = AnimaX, 4 = IgorPlug");
0813 
0814 module_param_hw(io, int, ioport, 0444);
0815 MODULE_PARM_DESC(io, "I/O address base (0x3f8 or 0x2f8)");
0816 
0817 /* some architectures (e.g. intel xscale) have memory mapped registers */
0818 module_param_hw(iommap, ulong, other, 0444);
0819 MODULE_PARM_DESC(iommap, "physical base for memory mapped I/O (0 = no memory mapped io)");
0820 
0821 /*
0822  * some architectures (e.g. intel xscale) align the 8bit serial registers
0823  * on 32bit word boundaries.
0824  * See linux-kernel/drivers/tty/serial/8250/8250.c serial_in()/out()
0825  */
0826 module_param_hw(ioshift, int, other, 0444);
0827 MODULE_PARM_DESC(ioshift, "shift I/O register offset (0 = no shift)");
0828 
0829 module_param_hw(irq, int, irq, 0444);
0830 MODULE_PARM_DESC(irq, "Interrupt (4 or 3)");
0831 
0832 module_param_hw(share_irq, bool, other, 0444);
0833 MODULE_PARM_DESC(share_irq, "Share interrupts (0 = off, 1 = on)");
0834 
0835 module_param(sense, int, 0444);
0836 MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit (0 = active high, 1 = active low )");
0837 
0838 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
0839 module_param(txsense, bool, 0444);
0840 MODULE_PARM_DESC(txsense, "Sense of transmitter circuit (0 = active high, 1 = active low )");
0841 #endif
0842 
0843 module_param(softcarrier, bool, 0444);
0844 MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");