Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / input / gameport / ns558.c
index 4b52ffe..3e2d28f 100644 (file)
 /*
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or 
+ * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- * 
+ *
  * Should you need to contact me, the author, you can do so either by
  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
@@ -35,6 +35,7 @@
 #include <linux/ioport.h>
 #include <linux/config.h>
 #include <linux/init.h>
+#include <linux/delay.h>
 #include <linux/gameport.h>
 #include <linux/slab.h>
 #include <linux/pnp.h>
@@ -43,22 +44,18 @@ MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 MODULE_DESCRIPTION("Classic gameport (ISA/PnP) driver");
 MODULE_LICENSE("GPL");
 
-#define NS558_ISA      1
-#define NS558_PNP      2
-
 static int ns558_isa_portlist[] = { 0x201, 0x200, 0x202, 0x203, 0x204, 0x205, 0x207, 0x209,
                                    0x20b, 0x20c, 0x20e, 0x20f, 0x211, 0x219, 0x101, 0 };
 
 struct ns558 {
        int type;
+       int io;
        int size;
        struct pnp_dev *dev;
+       struct gameport *gameport;
        struct list_head node;
-       struct gameport gameport;
-       char phys[32];
-       char name[32];
 };
-       
+
 static LIST_HEAD(ns558_list);
 
 /*
@@ -67,18 +64,19 @@ static LIST_HEAD(ns558_list);
  * A joystick must be attached for this to work.
  */
 
-static void ns558_isa_probe(int io)
+static int ns558_isa_probe(int io)
 {
        int i, j, b;
        unsigned char c, u, v;
-       struct ns558 *port;
+       struct ns558 *ns558;
+       struct gameport *port;
 
 /*
  * No one should be using this address.
  */
 
        if (!request_region(io, 1, "ns558-isa"))
-               return;
+               return -EBUSY;
 
 /*
  * We must not be able to write arbitrary values to the port.
@@ -89,8 +87,8 @@ static void ns558_isa_probe(int io)
        outb(~c & ~3, io);
        if (~(u = v = inb(io)) & 3) {
                outb(c, io);
-               i = 0;
-               goto out;
+               release_region(io, 1);
+               return -ENODEV;
        }
 /*
  * After a trigger, there must be at least some bits changing.
@@ -100,10 +98,10 @@ static void ns558_isa_probe(int io)
 
        if (u == v) {
                outb(c, io);
-               i = 0;
-               goto out;
+               release_region(io, 1);
+               return -ENODEV;
        }
-       wait_ms(3);
+       msleep(3);
 /*
  * After some time (4ms) the axes shouldn't change anymore.
  */
@@ -112,26 +110,26 @@ static void ns558_isa_probe(int io)
        for (i = 0; i < 1000; i++)
                if ((u ^ inb(io)) & 0xf) {
                        outb(c, io);
-                       i = 0;
-                       goto out;
+                       release_region(io, 1);
+                       return -ENODEV;
                }
-/* 
+/*
  * And now find the number of mirrors of the port.
  */
 
        for (i = 1; i < 5; i++) {
 
-               release_region(io & (-1 << (i-1)), (1 << (i-1)));
+               release_region(io & (-1 << (i - 1)), (1 << (i - 1)));
 
-               if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))     /* Don't disturb anyone */
-                       break;
+               if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
+                       break;                          /* Don't disturb anyone */
 
                outb(0xff, io & (-1 << i));
                for (j = b = 0; j < 1000; j++)
                        if (inb(io & (-1 << i)) != inb((io & (-1 << i)) + (1 << i) - 1)) b++;
-               wait_ms(3);
+               msleep(3);
 
-               if (b > 300) {                                  /* We allow 30% difference */
+               if (b > 300) {                          /* We allow 30% difference */
                        release_region(io & (-1 << i), (1 << i));
                        break;
                }
@@ -141,35 +139,33 @@ static void ns558_isa_probe(int io)
 
        if (i != 4) {
                if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
-                       return;
+                       return -EBUSY;
        }
 
-       if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
+       ns558 = kzalloc(sizeof(struct ns558), GFP_KERNEL);
+       port = gameport_allocate_port();
+       if (!ns558 || !port) {
                printk(KERN_ERR "ns558: Memory allocation failed.\n");
-               goto out;
+               release_region(io & (-1 << i), (1 << i));
+               kfree(ns558);
+               gameport_free_port(port);
+               return -ENOMEM;
        }
-       memset(port, 0, sizeof(struct ns558));
 
-       port->type = NS558_ISA;
-       port->size = (1 << i);
-       port->gameport.io = io;
-       port->gameport.phys = port->phys;
-       port->gameport.name = port->name;
-       port->gameport.id.bustype = BUS_ISA;
+       memset(ns558, 0, sizeof(struct ns558));
+       ns558->io = io;
+       ns558->size = 1 << i;
+       ns558->gameport = port;
 
-       sprintf(port->phys, "isa%04x/gameport0", io & (-1 << i));
-       sprintf(port->name, "NS558 ISA");
+       port->io = io;
+       gameport_set_name(port, "NS558 ISA Gameport");
+       gameport_set_phys(port, "isa%04x/gameport0", io & (-1 << i));
 
-       gameport_register_port(&port->gameport);
+       gameport_register_port(port);
 
-       printk(KERN_INFO "gameport: NS558 ISA at %#x", port->gameport.io);
-       if (port->size > 1) printk(" size %d", port->size);
-       printk(" speed %d kHz\n", port->gameport.speed);
+       list_add(&ns558->node, &ns558_list);
 
-       list_add(&port->node, &ns558_list);
-       return;
-out:
-       release_region(io & (-1 << i), (1 << i));
+       return 0;
 }
 
 #ifdef CONFIG_PNP
@@ -205,46 +201,42 @@ MODULE_DEVICE_TABLE(pnp, pnp_devids);
 static int ns558_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *did)
 {
        int ioport, iolen;
-       struct ns558 *port;
+       struct ns558 *ns558;
+       struct gameport *port;
 
        if (!pnp_port_valid(dev, 0)) {
                printk(KERN_WARNING "ns558: No i/o ports on a gameport? Weird\n");
                return -ENODEV;
        }
 
-       ioport = pnp_port_start(dev,0);
-       iolen = pnp_port_len(dev,0);
+       ioport = pnp_port_start(dev, 0);
+       iolen = pnp_port_len(dev, 0);
 
        if (!request_region(ioport, iolen, "ns558-pnp"))
                return -EBUSY;
 
-       if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
-               printk(KERN_ERR "ns558: Memory allocation failed.\n");
+       ns558 = kzalloc(sizeof(struct ns558), GFP_KERNEL);
+       port = gameport_allocate_port();
+       if (!ns558 || !port) {
+               printk(KERN_ERR "ns558: Memory allocation failed\n");
+               kfree(ns558);
+               gameport_free_port(port);
                return -ENOMEM;
        }
-       memset(port, 0, sizeof(struct ns558));
 
-       port->type = NS558_PNP;
-       port->size = iolen;
-       port->dev = dev;
+       ns558->io = ioport;
+       ns558->size = iolen;
+       ns558->dev = dev;
+       ns558->gameport = port;
 
-       port->gameport.io = ioport;
-       port->gameport.phys = port->phys;
-       port->gameport.name = port->name;
-       port->gameport.id.bustype = BUS_ISAPNP;
-       port->gameport.id.version = 0x100;
+       gameport_set_name(port, "NS558 PnP Gameport");
+       gameport_set_phys(port, "pnp%s/gameport0", dev->dev.bus_id);
+       port->dev.parent = &dev->dev;
+       port->io = ioport;
 
-       sprintf(port->phys, "pnp%s/gameport0", dev->dev.bus_id);
-       sprintf(port->name, "%s", "NS558 PnP Gameport");
+       gameport_register_port(port);
 
-       gameport_register_port(&port->gameport);
-
-       printk(KERN_INFO "gameport: NS558 PnP at pnp%s io %#x",
-               dev->dev.bus_id, port->gameport.io);
-       if (iolen > 1) printk(" size %d", iolen);
-       printk(" speed %d kHz\n", port->gameport.speed);
-
-       list_add_tail(&port->node, &ns558_list);
+       list_add_tail(&ns558->node, &ns558_list);
        return 0;
 }
 
@@ -260,42 +252,37 @@ static struct pnp_driver ns558_pnp_driver;
 
 #endif
 
-int __init ns558_init(void)
+static int __init ns558_init(void)
 {
        int i = 0;
+       int error;
+
+       error = pnp_register_driver(&ns558_pnp_driver);
+       if (error && error != -ENODEV)  /* should be ENOSYS really */
+               return error;
 
 /*
- * Probe for ISA ports.
+ * Probe ISA ports after PnP, so that PnP ports that are already
+ * enabled get detected as PnP. This may be suboptimal in multi-device
+ * configurations, but saves hassle with simple setups.
  */
 
        while (ns558_isa_portlist[i])
                ns558_isa_probe(ns558_isa_portlist[i++]);
 
-       pnp_register_driver(&ns558_pnp_driver);
-       return list_empty(&ns558_list) ? -ENODEV : 0;
+       return list_empty(&ns558_list) && error ? -ENODEV : 0;
 }
 
-void __exit ns558_exit(void)
+static void __exit ns558_exit(void)
 {
-       struct ns558 *port;
+       struct ns558 *ns558, *safe;
 
-       list_for_each_entry(port, &ns558_list, node) {
-               gameport_unregister_port(&port->gameport);
-               switch (port->type) {
-
-#ifdef CONFIG_PNP
-                       case NS558_PNP:
-                               /* fall through */
-#endif
-                       case NS558_ISA:
-                               release_region(port->gameport.io & ~(port->size - 1), port->size);
-                               kfree(port);
-                               break;
-               
-                       default:
-                               break;
-               }
+       list_for_each_entry_safe(ns558, safe, &ns558_list, node) {
+               gameport_unregister_port(ns558->gameport);
+               release_region(ns558->io & ~(ns558->size - 1), ns558->size);
+               kfree(ns558);
        }
+
        pnp_unregister_driver(&ns558_pnp_driver);
 }