Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * drivers/input/serio/gscps2.c
0003  *
0004  * Copyright (c) 2004-2006 Helge Deller <deller@gmx.de>
0005  * Copyright (c) 2002 Laurent Canet <canetl@esiee.fr>
0006  * Copyright (c) 2002 Thibaut Varene <varenet@parisc-linux.org>
0007  *
0008  * Pieces of code based on linux-2.4's hp_mouse.c & hp_keyb.c
0009  *  Copyright (c) 1999 Alex deVries <alex@onefishtwo.ca>
0010  *  Copyright (c) 1999-2000 Philipp Rumpf <prumpf@tux.org>
0011  *  Copyright (c) 2000 Xavier Debacker <debackex@esiee.fr>
0012  *  Copyright (c) 2000-2001 Thomas Marteau <marteaut@esiee.fr>
0013  *
0014  * HP GSC PS/2 port driver, found in PA/RISC Workstations
0015  *
0016  * This file is subject to the terms and conditions of the GNU General Public
0017  * License.  See the file "COPYING" in the main directory of this archive
0018  * for more details.
0019  *
0020  * TODO:
0021  * - Dino testing (did HP ever shipped a machine on which this port
0022  *                 was usable/enabled ?)
0023  */
0024 
0025 #include <linux/init.h>
0026 #include <linux/module.h>
0027 #include <linux/slab.h>
0028 #include <linux/serio.h>
0029 #include <linux/input.h>
0030 #include <linux/interrupt.h>
0031 #include <linux/spinlock.h>
0032 #include <linux/delay.h>
0033 #include <linux/ioport.h>
0034 
0035 #include <asm/irq.h>
0036 #include <asm/io.h>
0037 #include <asm/parisc-device.h>
0038 
0039 MODULE_AUTHOR("Laurent Canet <canetl@esiee.fr>, Thibaut Varene <varenet@parisc-linux.org>, Helge Deller <deller@gmx.de>");
0040 MODULE_DESCRIPTION("HP GSC PS2 port driver");
0041 MODULE_LICENSE("GPL");
0042 
0043 #define PFX "gscps2.c: "
0044 
0045 /*
0046  * Driver constants
0047  */
0048 
0049 /* various constants */
0050 #define ENABLE          1
0051 #define DISABLE         0
0052 
0053 #define GSC_DINO_OFFSET     0x0800  /* offset for DINO controller versus LASI one */
0054 
0055 /* PS/2 IO port offsets */
0056 #define GSC_ID          0x00    /* device ID offset (see: GSC_ID_XXX) */
0057 #define GSC_RESET       0x00    /* reset port offset */
0058 #define GSC_RCVDATA     0x04    /* receive port offset */
0059 #define GSC_XMTDATA     0x04    /* transmit port offset */
0060 #define GSC_CONTROL     0x08    /* see: Control register bits */
0061 #define GSC_STATUS      0x0C    /* see: Status register bits */
0062 
0063 /* Control register bits */
0064 #define GSC_CTRL_ENBL       0x01    /* enable interface */
0065 #define GSC_CTRL_LPBXR      0x02    /* loopback operation */
0066 #define GSC_CTRL_DIAG       0x20    /* directly control clock/data line */
0067 #define GSC_CTRL_DATDIR     0x40    /* data line direct control */
0068 #define GSC_CTRL_CLKDIR     0x80    /* clock line direct control */
0069 
0070 /* Status register bits */
0071 #define GSC_STAT_RBNE       0x01    /* Receive Buffer Not Empty */
0072 #define GSC_STAT_TBNE       0x02    /* Transmit Buffer Not Empty */
0073 #define GSC_STAT_TERR       0x04    /* Timeout Error */
0074 #define GSC_STAT_PERR       0x08    /* Parity Error */
0075 #define GSC_STAT_CMPINTR    0x10    /* Composite Interrupt = irq on any port */
0076 #define GSC_STAT_DATSHD     0x40    /* Data Line Shadow */
0077 #define GSC_STAT_CLKSHD     0x80    /* Clock Line Shadow */
0078 
0079 /* IDs returned by GSC_ID port register */
0080 #define GSC_ID_KEYBOARD     0   /* device ID values */
0081 #define GSC_ID_MOUSE        1
0082 
0083 
0084 static irqreturn_t gscps2_interrupt(int irq, void *dev);
0085 
0086 #define BUFFER_SIZE 0x0f
0087 
0088 /* GSC PS/2 port device struct */
0089 struct gscps2port {
0090     struct list_head node;
0091     struct parisc_device *padev;
0092     struct serio *port;
0093     spinlock_t lock;
0094     char __iomem *addr;
0095     u8 act, append; /* position in buffer[] */
0096     struct {
0097         u8 data;
0098         u8 str;
0099     } buffer[BUFFER_SIZE+1];
0100     int id;
0101 };
0102 
0103 /*
0104  * Various HW level routines
0105  */
0106 
0107 #define gscps2_readb_input(x)       readb((x)+GSC_RCVDATA)
0108 #define gscps2_readb_control(x)     readb((x)+GSC_CONTROL)
0109 #define gscps2_readb_status(x)      readb((x)+GSC_STATUS)
0110 #define gscps2_writeb_control(x, y) writeb((x), (y)+GSC_CONTROL)
0111 
0112 
0113 /*
0114  * wait_TBE() - wait for Transmit Buffer Empty
0115  */
0116 
0117 static int wait_TBE(char __iomem *addr)
0118 {
0119     int timeout = 25000; /* device is expected to react within 250 msec */
0120     while (gscps2_readb_status(addr) & GSC_STAT_TBNE) {
0121         if (!--timeout)
0122             return 0;   /* This should not happen */
0123         udelay(10);
0124     }
0125     return 1;
0126 }
0127 
0128 
0129 /*
0130  * gscps2_flush() - flush the receive buffer
0131  */
0132 
0133 static void gscps2_flush(struct gscps2port *ps2port)
0134 {
0135     while (gscps2_readb_status(ps2port->addr) & GSC_STAT_RBNE)
0136         gscps2_readb_input(ps2port->addr);
0137     ps2port->act = ps2port->append = 0;
0138 }
0139 
0140 /*
0141  * gscps2_writeb_output() - write a byte to the port
0142  *
0143  * returns 1 on success, 0 on error
0144  */
0145 
0146 static inline int gscps2_writeb_output(struct gscps2port *ps2port, u8 data)
0147 {
0148     unsigned long flags;
0149     char __iomem *addr = ps2port->addr;
0150 
0151     if (!wait_TBE(addr)) {
0152         printk(KERN_DEBUG PFX "timeout - could not write byte %#x\n", data);
0153         return 0;
0154     }
0155 
0156     while (gscps2_readb_status(addr) & GSC_STAT_RBNE)
0157         /* wait */;
0158 
0159     spin_lock_irqsave(&ps2port->lock, flags);
0160     writeb(data, addr+GSC_XMTDATA);
0161     spin_unlock_irqrestore(&ps2port->lock, flags);
0162 
0163     /* this is ugly, but due to timing of the port it seems to be necessary. */
0164     mdelay(6);
0165 
0166     /* make sure any received data is returned as fast as possible */
0167     /* this is important e.g. when we set the LEDs on the keyboard */
0168     gscps2_interrupt(0, NULL);
0169 
0170     return 1;
0171 }
0172 
0173 
0174 /*
0175  * gscps2_enable() - enables or disables the port
0176  */
0177 
0178 static void gscps2_enable(struct gscps2port *ps2port, int enable)
0179 {
0180     unsigned long flags;
0181     u8 data;
0182 
0183     /* now enable/disable the port */
0184     spin_lock_irqsave(&ps2port->lock, flags);
0185     gscps2_flush(ps2port);
0186     data = gscps2_readb_control(ps2port->addr);
0187     if (enable)
0188         data |= GSC_CTRL_ENBL;
0189     else
0190         data &= ~GSC_CTRL_ENBL;
0191     gscps2_writeb_control(data, ps2port->addr);
0192     spin_unlock_irqrestore(&ps2port->lock, flags);
0193     wait_TBE(ps2port->addr);
0194     gscps2_flush(ps2port);
0195 }
0196 
0197 /*
0198  * gscps2_reset() - resets the PS/2 port
0199  */
0200 
0201 static void gscps2_reset(struct gscps2port *ps2port)
0202 {
0203     unsigned long flags;
0204 
0205     /* reset the interface */
0206     spin_lock_irqsave(&ps2port->lock, flags);
0207     gscps2_flush(ps2port);
0208     writeb(0xff, ps2port->addr + GSC_RESET);
0209     gscps2_flush(ps2port);
0210     spin_unlock_irqrestore(&ps2port->lock, flags);
0211 }
0212 
0213 static LIST_HEAD(ps2port_list);
0214 
0215 /**
0216  * gscps2_interrupt() - Interruption service routine
0217  *
0218  * This function reads received PS/2 bytes and processes them on
0219  * all interfaces.
0220  * The problematic part here is, that the keyboard and mouse PS/2 port
0221  * share the same interrupt and it's not possible to send data if any
0222  * one of them holds input data. To solve this problem we try to receive
0223  * the data as fast as possible and handle the reporting to the upper layer
0224  * later.
0225  */
0226 
0227 static irqreturn_t gscps2_interrupt(int irq, void *dev)
0228 {
0229     struct gscps2port *ps2port;
0230 
0231     list_for_each_entry(ps2port, &ps2port_list, node) {
0232 
0233       unsigned long flags;
0234       spin_lock_irqsave(&ps2port->lock, flags);
0235 
0236       while ( (ps2port->buffer[ps2port->append].str =
0237            gscps2_readb_status(ps2port->addr)) & GSC_STAT_RBNE ) {
0238         ps2port->buffer[ps2port->append].data =
0239                 gscps2_readb_input(ps2port->addr);
0240         ps2port->append = ((ps2port->append+1) & BUFFER_SIZE);
0241       }
0242 
0243       spin_unlock_irqrestore(&ps2port->lock, flags);
0244 
0245     } /* list_for_each_entry */
0246 
0247     /* all data was read from the ports - now report the data to upper layer */
0248 
0249     list_for_each_entry(ps2port, &ps2port_list, node) {
0250 
0251       while (ps2port->act != ps2port->append) {
0252 
0253         unsigned int rxflags;
0254         u8 data, status;
0255 
0256         /* Did new data arrived while we read existing data ?
0257            If yes, exit now and let the new irq handler start over again */
0258         if (gscps2_readb_status(ps2port->addr) & GSC_STAT_CMPINTR)
0259         return IRQ_HANDLED;
0260 
0261         status = ps2port->buffer[ps2port->act].str;
0262         data   = ps2port->buffer[ps2port->act].data;
0263 
0264         ps2port->act = ((ps2port->act+1) & BUFFER_SIZE);
0265         rxflags =   ((status & GSC_STAT_TERR) ? SERIO_TIMEOUT : 0 ) |
0266             ((status & GSC_STAT_PERR) ? SERIO_PARITY  : 0 );
0267 
0268         serio_interrupt(ps2port->port, data, rxflags);
0269 
0270       } /* while() */
0271 
0272     } /* list_for_each_entry */
0273 
0274     return IRQ_HANDLED;
0275 }
0276 
0277 
0278 /*
0279  * gscps2_write() - send a byte out through the aux interface.
0280  */
0281 
0282 static int gscps2_write(struct serio *port, unsigned char data)
0283 {
0284     struct gscps2port *ps2port = port->port_data;
0285 
0286     if (!gscps2_writeb_output(ps2port, data)) {
0287         printk(KERN_DEBUG PFX "sending byte %#x failed.\n", data);
0288         return -1;
0289     }
0290     return 0;
0291 }
0292 
0293 /*
0294  * gscps2_open() is called when a port is opened by the higher layer.
0295  * It resets and enables the port.
0296  */
0297 
0298 static int gscps2_open(struct serio *port)
0299 {
0300     struct gscps2port *ps2port = port->port_data;
0301 
0302     gscps2_reset(ps2port);
0303 
0304     /* enable it */
0305     gscps2_enable(ps2port, ENABLE);
0306 
0307     gscps2_interrupt(0, NULL);
0308 
0309     return 0;
0310 }
0311 
0312 /*
0313  * gscps2_close() disables the port
0314  */
0315 
0316 static void gscps2_close(struct serio *port)
0317 {
0318     struct gscps2port *ps2port = port->port_data;
0319     gscps2_enable(ps2port, DISABLE);
0320 }
0321 
0322 /**
0323  * gscps2_probe() - Probes PS2 devices
0324  * @return: success/error report
0325  */
0326 
0327 static int __init gscps2_probe(struct parisc_device *dev)
0328 {
0329     struct gscps2port *ps2port;
0330     struct serio *serio;
0331     unsigned long hpa = dev->hpa.start;
0332     int ret;
0333 
0334     if (!dev->irq)
0335         return -ENODEV;
0336 
0337     /* Offset for DINO PS/2. Works with LASI even */
0338     if (dev->id.sversion == 0x96)
0339         hpa += GSC_DINO_OFFSET;
0340 
0341     ps2port = kzalloc(sizeof(struct gscps2port), GFP_KERNEL);
0342     serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
0343     if (!ps2port || !serio) {
0344         ret = -ENOMEM;
0345         goto fail_nomem;
0346     }
0347 
0348     dev_set_drvdata(&dev->dev, ps2port);
0349 
0350     ps2port->port = serio;
0351     ps2port->padev = dev;
0352     ps2port->addr = ioremap(hpa, GSC_STATUS + 4);
0353     if (!ps2port->addr) {
0354         ret = -ENOMEM;
0355         goto fail_nomem;
0356     }
0357     spin_lock_init(&ps2port->lock);
0358 
0359     gscps2_reset(ps2port);
0360     ps2port->id = readb(ps2port->addr + GSC_ID) & 0x0f;
0361 
0362     snprintf(serio->name, sizeof(serio->name), "gsc-ps2-%s",
0363          (ps2port->id == GSC_ID_KEYBOARD) ? "keyboard" : "mouse");
0364     strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
0365     serio->id.type      = SERIO_8042;
0366     serio->write        = gscps2_write;
0367     serio->open     = gscps2_open;
0368     serio->close        = gscps2_close;
0369     serio->port_data    = ps2port;
0370     serio->dev.parent   = &dev->dev;
0371 
0372     ret = -EBUSY;
0373     if (request_irq(dev->irq, gscps2_interrupt, IRQF_SHARED, ps2port->port->name, ps2port))
0374         goto fail_miserably;
0375 
0376     if (ps2port->id != GSC_ID_KEYBOARD && ps2port->id != GSC_ID_MOUSE) {
0377         printk(KERN_WARNING PFX "Unsupported PS/2 port at 0x%08lx (id=%d) ignored\n",
0378                 hpa, ps2port->id);
0379         ret = -ENODEV;
0380         goto fail;
0381     }
0382 
0383 #if 0
0384     if (!request_mem_region(hpa, GSC_STATUS + 4, ps2port->port.name))
0385         goto fail;
0386 #endif
0387 
0388     pr_info("serio: %s port at 0x%08lx irq %d @ %s\n",
0389         ps2port->port->name,
0390         hpa,
0391         ps2port->padev->irq,
0392         ps2port->port->phys);
0393 
0394     serio_register_port(ps2port->port);
0395 
0396     list_add_tail(&ps2port->node, &ps2port_list);
0397 
0398     return 0;
0399 
0400 fail:
0401     free_irq(dev->irq, ps2port);
0402 
0403 fail_miserably:
0404     iounmap(ps2port->addr);
0405     release_mem_region(dev->hpa.start, GSC_STATUS + 4);
0406 
0407 fail_nomem:
0408     kfree(ps2port);
0409     kfree(serio);
0410     return ret;
0411 }
0412 
0413 /**
0414  * gscps2_remove() - Removes PS2 devices
0415  * @return: success/error report
0416  */
0417 
0418 static void __exit gscps2_remove(struct parisc_device *dev)
0419 {
0420     struct gscps2port *ps2port = dev_get_drvdata(&dev->dev);
0421 
0422     serio_unregister_port(ps2port->port);
0423     free_irq(dev->irq, ps2port);
0424     gscps2_flush(ps2port);
0425     list_del(&ps2port->node);
0426     iounmap(ps2port->addr);
0427 #if 0
0428     release_mem_region(dev->hpa, GSC_STATUS + 4);
0429 #endif
0430     dev_set_drvdata(&dev->dev, NULL);
0431     kfree(ps2port);
0432 }
0433 
0434 
0435 static const struct parisc_device_id gscps2_device_tbl[] __initconst = {
0436     { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00084 }, /* LASI PS/2 */
0437 #ifdef DINO_TESTED
0438     { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00096 }, /* DINO PS/2 */
0439 #endif
0440     { 0, }  /* 0 terminated list */
0441 };
0442 MODULE_DEVICE_TABLE(parisc, gscps2_device_tbl);
0443 
0444 static struct parisc_driver parisc_ps2_driver __refdata = {
0445     .name       = "gsc_ps2",
0446     .id_table   = gscps2_device_tbl,
0447     .probe      = gscps2_probe,
0448     .remove     = __exit_p(gscps2_remove),
0449 };
0450 
0451 static int __init gscps2_init(void)
0452 {
0453     register_parisc_driver(&parisc_ps2_driver);
0454     return 0;
0455 }
0456 
0457 static void __exit gscps2_exit(void)
0458 {
0459     unregister_parisc_driver(&parisc_ps2_driver);
0460 }
0461 
0462 
0463 module_init(gscps2_init);
0464 module_exit(gscps2_exit);
0465