Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (c) 1999-2001 Vojtech Pavlik
0004  *
0005  *  Based on the work of:
0006  *  James Banks     Matthew Dillon
0007  *  David Giller        Nathan Laredo
0008  *  Linus Torvalds      Johan Myreen
0009  *  Cliff Matthews      Philip Blundell
0010  *  Russell King
0011  */
0012 
0013 /*
0014  * Logitech Bus Mouse Driver for Linux
0015  */
0016 
0017 /*
0018  */
0019 
0020 #include <linux/module.h>
0021 #include <linux/delay.h>
0022 #include <linux/ioport.h>
0023 #include <linux/init.h>
0024 #include <linux/input.h>
0025 #include <linux/interrupt.h>
0026 
0027 #include <asm/io.h>
0028 #include <asm/irq.h>
0029 
0030 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
0031 MODULE_DESCRIPTION("Logitech busmouse driver");
0032 MODULE_LICENSE("GPL");
0033 
0034 #define LOGIBM_BASE     0x23c
0035 #define LOGIBM_EXTENT       4
0036 
0037 #define LOGIBM_DATA_PORT    LOGIBM_BASE + 0
0038 #define LOGIBM_SIGNATURE_PORT   LOGIBM_BASE + 1
0039 #define LOGIBM_CONTROL_PORT LOGIBM_BASE + 2
0040 #define LOGIBM_CONFIG_PORT  LOGIBM_BASE + 3
0041 
0042 #define LOGIBM_ENABLE_IRQ   0x00
0043 #define LOGIBM_DISABLE_IRQ  0x10
0044 #define LOGIBM_READ_X_LOW   0x80
0045 #define LOGIBM_READ_X_HIGH  0xa0
0046 #define LOGIBM_READ_Y_LOW   0xc0
0047 #define LOGIBM_READ_Y_HIGH  0xe0
0048 
0049 #define LOGIBM_DEFAULT_MODE 0x90
0050 #define LOGIBM_CONFIG_BYTE  0x91
0051 #define LOGIBM_SIGNATURE_BYTE   0xa5
0052 
0053 #define LOGIBM_IRQ      5
0054 
0055 static int logibm_irq = LOGIBM_IRQ;
0056 module_param_hw_named(irq, logibm_irq, uint, irq, 0);
0057 MODULE_PARM_DESC(irq, "IRQ number (5=default)");
0058 
0059 static struct input_dev *logibm_dev;
0060 
0061 static irqreturn_t logibm_interrupt(int irq, void *dev_id)
0062 {
0063     char dx, dy;
0064     unsigned char buttons;
0065 
0066     outb(LOGIBM_READ_X_LOW, LOGIBM_CONTROL_PORT);
0067     dx = (inb(LOGIBM_DATA_PORT) & 0xf);
0068     outb(LOGIBM_READ_X_HIGH, LOGIBM_CONTROL_PORT);
0069     dx |= (inb(LOGIBM_DATA_PORT) & 0xf) << 4;
0070     outb(LOGIBM_READ_Y_LOW, LOGIBM_CONTROL_PORT);
0071     dy = (inb(LOGIBM_DATA_PORT) & 0xf);
0072     outb(LOGIBM_READ_Y_HIGH, LOGIBM_CONTROL_PORT);
0073     buttons = inb(LOGIBM_DATA_PORT);
0074     dy |= (buttons & 0xf) << 4;
0075     buttons = ~buttons >> 5;
0076 
0077     input_report_rel(logibm_dev, REL_X, dx);
0078     input_report_rel(logibm_dev, REL_Y, dy);
0079     input_report_key(logibm_dev, BTN_RIGHT,  buttons & 1);
0080     input_report_key(logibm_dev, BTN_MIDDLE, buttons & 2);
0081     input_report_key(logibm_dev, BTN_LEFT,   buttons & 4);
0082     input_sync(logibm_dev);
0083 
0084     outb(LOGIBM_ENABLE_IRQ, LOGIBM_CONTROL_PORT);
0085     return IRQ_HANDLED;
0086 }
0087 
0088 static int logibm_open(struct input_dev *dev)
0089 {
0090     if (request_irq(logibm_irq, logibm_interrupt, 0, "logibm", NULL)) {
0091         printk(KERN_ERR "logibm.c: Can't allocate irq %d\n", logibm_irq);
0092         return -EBUSY;
0093     }
0094     outb(LOGIBM_ENABLE_IRQ, LOGIBM_CONTROL_PORT);
0095     return 0;
0096 }
0097 
0098 static void logibm_close(struct input_dev *dev)
0099 {
0100     outb(LOGIBM_DISABLE_IRQ, LOGIBM_CONTROL_PORT);
0101     free_irq(logibm_irq, NULL);
0102 }
0103 
0104 static int __init logibm_init(void)
0105 {
0106     int err;
0107 
0108     if (!request_region(LOGIBM_BASE, LOGIBM_EXTENT, "logibm")) {
0109         printk(KERN_ERR "logibm.c: Can't allocate ports at %#x\n", LOGIBM_BASE);
0110         return -EBUSY;
0111     }
0112 
0113     outb(LOGIBM_CONFIG_BYTE, LOGIBM_CONFIG_PORT);
0114     outb(LOGIBM_SIGNATURE_BYTE, LOGIBM_SIGNATURE_PORT);
0115     udelay(100);
0116 
0117     if (inb(LOGIBM_SIGNATURE_PORT) != LOGIBM_SIGNATURE_BYTE) {
0118         printk(KERN_INFO "logibm.c: Didn't find Logitech busmouse at %#x\n", LOGIBM_BASE);
0119         err = -ENODEV;
0120         goto err_release_region;
0121     }
0122 
0123     outb(LOGIBM_DEFAULT_MODE, LOGIBM_CONFIG_PORT);
0124     outb(LOGIBM_DISABLE_IRQ, LOGIBM_CONTROL_PORT);
0125 
0126     logibm_dev = input_allocate_device();
0127     if (!logibm_dev) {
0128         printk(KERN_ERR "logibm.c: Not enough memory for input device\n");
0129         err = -ENOMEM;
0130         goto err_release_region;
0131     }
0132 
0133     logibm_dev->name = "Logitech bus mouse";
0134     logibm_dev->phys = "isa023c/input0";
0135     logibm_dev->id.bustype = BUS_ISA;
0136     logibm_dev->id.vendor  = 0x0003;
0137     logibm_dev->id.product = 0x0001;
0138     logibm_dev->id.version = 0x0100;
0139 
0140     logibm_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
0141     logibm_dev->keybit[BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) |
0142         BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
0143     logibm_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
0144 
0145     logibm_dev->open  = logibm_open;
0146     logibm_dev->close = logibm_close;
0147 
0148     err = input_register_device(logibm_dev);
0149     if (err)
0150         goto err_free_dev;
0151 
0152     return 0;
0153 
0154  err_free_dev:
0155     input_free_device(logibm_dev);
0156  err_release_region:
0157     release_region(LOGIBM_BASE, LOGIBM_EXTENT);
0158 
0159     return err;
0160 }
0161 
0162 static void __exit logibm_exit(void)
0163 {
0164     input_unregister_device(logibm_dev);
0165     release_region(LOGIBM_BASE, LOGIBM_EXTENT);
0166 }
0167 
0168 module_init(logibm_init);
0169 module_exit(logibm_exit);