0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/delay.h>
0014 #include <linux/module.h>
0015 #include <linux/ioport.h>
0016 #include <linux/init.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/serio.h>
0019 #include <linux/errno.h>
0020 #include <linux/err.h>
0021 #include <linux/platform_device.h>
0022 #include <linux/slab.h>
0023
0024 #include <asm/io.h>
0025
0026 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
0027 MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
0028 MODULE_LICENSE("GPL");
0029
0030
0031
0032
0033
0034 #define CT82C710_DEV_IDLE 0x01
0035 #define CT82C710_RX_FULL 0x02
0036 #define CT82C710_TX_IDLE 0x04
0037 #define CT82C710_RESET 0x08
0038 #define CT82C710_INTS_ON 0x10
0039 #define CT82C710_ERROR_FLAG 0x20
0040 #define CT82C710_CLEAR 0x40
0041 #define CT82C710_ENABLE 0x80
0042
0043 #define CT82C710_IRQ 12
0044
0045 #define CT82C710_DATA ct82c710_iores.start
0046 #define CT82C710_STATUS (ct82c710_iores.start + 1)
0047
0048 static struct serio *ct82c710_port;
0049 static struct platform_device *ct82c710_device;
0050 static struct resource ct82c710_iores;
0051
0052
0053
0054
0055
0056
0057 static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id)
0058 {
0059 return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0);
0060 }
0061
0062
0063
0064
0065
0066 static int ct82c170_wait(void)
0067 {
0068 int timeout = 60000;
0069
0070 while ((inb(CT82C710_STATUS) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE))
0071 != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) {
0072
0073 if (inb_p(CT82C710_STATUS) & CT82C710_RX_FULL) inb_p(CT82C710_DATA);
0074
0075 udelay(1);
0076 timeout--;
0077 }
0078
0079 return !timeout;
0080 }
0081
0082 static void ct82c710_close(struct serio *serio)
0083 {
0084 if (ct82c170_wait())
0085 printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
0086
0087 outb_p(inb_p(CT82C710_STATUS) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), CT82C710_STATUS);
0088
0089 if (ct82c170_wait())
0090 printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
0091
0092 free_irq(CT82C710_IRQ, NULL);
0093 }
0094
0095 static int ct82c710_open(struct serio *serio)
0096 {
0097 unsigned char status;
0098 int err;
0099
0100 err = request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL);
0101 if (err)
0102 return err;
0103
0104 status = inb_p(CT82C710_STATUS);
0105
0106 status |= (CT82C710_ENABLE | CT82C710_RESET);
0107 outb_p(status, CT82C710_STATUS);
0108
0109 status &= ~(CT82C710_RESET);
0110 outb_p(status, CT82C710_STATUS);
0111
0112 status |= CT82C710_INTS_ON;
0113 outb_p(status, CT82C710_STATUS);
0114
0115 while (ct82c170_wait()) {
0116 printk(KERN_ERR "ct82c710: Device busy in open()\n");
0117 status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
0118 outb_p(status, CT82C710_STATUS);
0119 free_irq(CT82C710_IRQ, NULL);
0120 return -EBUSY;
0121 }
0122
0123 return 0;
0124 }
0125
0126
0127
0128
0129
0130 static int ct82c710_write(struct serio *port, unsigned char c)
0131 {
0132 if (ct82c170_wait()) return -1;
0133 outb_p(c, CT82C710_DATA);
0134 return 0;
0135 }
0136
0137
0138
0139
0140
0141 static int __init ct82c710_detect(void)
0142 {
0143 outb_p(0x55, 0x2fa);
0144 outb_p(0xaa, 0x3fa);
0145 outb_p(0x36, 0x3fa);
0146 outb_p(0xe4, 0x3fa);
0147 outb_p(0x1b, 0x2fa);
0148 outb_p(0x0f, 0x390);
0149 if (inb_p(0x391) != 0xe4)
0150 return -ENODEV;
0151
0152 outb_p(0x0d, 0x390);
0153 ct82c710_iores.start = inb_p(0x391) << 2;
0154 ct82c710_iores.end = ct82c710_iores.start + 1;
0155 ct82c710_iores.flags = IORESOURCE_IO;
0156 outb_p(0x0f, 0x390);
0157 outb_p(0x0f, 0x391);
0158
0159 return 0;
0160 }
0161
0162 static int ct82c710_probe(struct platform_device *dev)
0163 {
0164 ct82c710_port = kzalloc(sizeof(struct serio), GFP_KERNEL);
0165 if (!ct82c710_port)
0166 return -ENOMEM;
0167
0168 ct82c710_port->id.type = SERIO_8042;
0169 ct82c710_port->dev.parent = &dev->dev;
0170 ct82c710_port->open = ct82c710_open;
0171 ct82c710_port->close = ct82c710_close;
0172 ct82c710_port->write = ct82c710_write;
0173 strlcpy(ct82c710_port->name, "C&T 82c710 mouse port",
0174 sizeof(ct82c710_port->name));
0175 snprintf(ct82c710_port->phys, sizeof(ct82c710_port->phys),
0176 "isa%16llx/serio0", (unsigned long long)CT82C710_DATA);
0177
0178 serio_register_port(ct82c710_port);
0179
0180 printk(KERN_INFO "serio: C&T 82c710 mouse port at %#llx irq %d\n",
0181 (unsigned long long)CT82C710_DATA, CT82C710_IRQ);
0182
0183 return 0;
0184 }
0185
0186 static int ct82c710_remove(struct platform_device *dev)
0187 {
0188 serio_unregister_port(ct82c710_port);
0189
0190 return 0;
0191 }
0192
0193 static struct platform_driver ct82c710_driver = {
0194 .driver = {
0195 .name = "ct82c710",
0196 },
0197 .probe = ct82c710_probe,
0198 .remove = ct82c710_remove,
0199 };
0200
0201
0202 static int __init ct82c710_init(void)
0203 {
0204 int error;
0205
0206 error = ct82c710_detect();
0207 if (error)
0208 return error;
0209
0210 error = platform_driver_register(&ct82c710_driver);
0211 if (error)
0212 return error;
0213
0214 ct82c710_device = platform_device_alloc("ct82c710", -1);
0215 if (!ct82c710_device) {
0216 error = -ENOMEM;
0217 goto err_unregister_driver;
0218 }
0219
0220 error = platform_device_add_resources(ct82c710_device, &ct82c710_iores, 1);
0221 if (error)
0222 goto err_free_device;
0223
0224 error = platform_device_add(ct82c710_device);
0225 if (error)
0226 goto err_free_device;
0227
0228 return 0;
0229
0230 err_free_device:
0231 platform_device_put(ct82c710_device);
0232 err_unregister_driver:
0233 platform_driver_unregister(&ct82c710_driver);
0234 return error;
0235 }
0236
0237 static void __exit ct82c710_exit(void)
0238 {
0239 platform_device_unregister(ct82c710_device);
0240 platform_driver_unregister(&ct82c710_driver);
0241 }
0242
0243 module_init(ct82c710_init);
0244 module_exit(ct82c710_exit);