Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (c) 1999-2001 Vojtech Pavlik
0004  *  Copyright (c) 1999 Brian Gerst
0005  */
0006 
0007 /*
0008  * NS558 based standard IBM game port driver for Linux
0009  */
0010 
0011 /*
0012  */
0013 
0014 #include <asm/io.h>
0015 
0016 #include <linux/module.h>
0017 #include <linux/ioport.h>
0018 #include <linux/init.h>
0019 #include <linux/delay.h>
0020 #include <linux/gameport.h>
0021 #include <linux/slab.h>
0022 #include <linux/pnp.h>
0023 
0024 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
0025 MODULE_DESCRIPTION("Classic gameport (ISA/PnP) driver");
0026 MODULE_LICENSE("GPL");
0027 
0028 static int ns558_isa_portlist[] = { 0x201, 0x200, 0x202, 0x203, 0x204, 0x205, 0x207, 0x209,
0029                     0x20b, 0x20c, 0x20e, 0x20f, 0x211, 0x219, 0x101, 0 };
0030 
0031 struct ns558 {
0032     int type;
0033     int io;
0034     int size;
0035     struct pnp_dev *dev;
0036     struct gameport *gameport;
0037     struct list_head node;
0038 };
0039 
0040 static LIST_HEAD(ns558_list);
0041 
0042 /*
0043  * ns558_isa_probe() tries to find an isa gameport at the
0044  * specified address, and also checks for mirrors.
0045  * A joystick must be attached for this to work.
0046  */
0047 
0048 static int ns558_isa_probe(int io)
0049 {
0050     int i, j, b;
0051     unsigned char c, u, v;
0052     struct ns558 *ns558;
0053     struct gameport *port;
0054 
0055 /*
0056  * No one should be using this address.
0057  */
0058 
0059     if (!request_region(io, 1, "ns558-isa"))
0060         return -EBUSY;
0061 
0062 /*
0063  * We must not be able to write arbitrary values to the port.
0064  * The lower two axis bits must be 1 after a write.
0065  */
0066 
0067     c = inb(io);
0068     outb(~c & ~3, io);
0069     if (~(u = v = inb(io)) & 3) {
0070         outb(c, io);
0071         release_region(io, 1);
0072         return -ENODEV;
0073     }
0074 /*
0075  * After a trigger, there must be at least some bits changing.
0076  */
0077 
0078     for (i = 0; i < 1000; i++) v &= inb(io);
0079 
0080     if (u == v) {
0081         outb(c, io);
0082         release_region(io, 1);
0083         return -ENODEV;
0084     }
0085     msleep(3);
0086 /*
0087  * After some time (4ms) the axes shouldn't change anymore.
0088  */
0089 
0090     u = inb(io);
0091     for (i = 0; i < 1000; i++)
0092         if ((u ^ inb(io)) & 0xf) {
0093             outb(c, io);
0094             release_region(io, 1);
0095             return -ENODEV;
0096         }
0097 /*
0098  * And now find the number of mirrors of the port.
0099  */
0100 
0101     for (i = 1; i < 5; i++) {
0102 
0103         release_region(io & (-1 << (i - 1)), (1 << (i - 1)));
0104 
0105         if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
0106             break;              /* Don't disturb anyone */
0107 
0108         outb(0xff, io & (-1 << i));
0109         for (j = b = 0; j < 1000; j++)
0110             if (inb(io & (-1 << i)) != inb((io & (-1 << i)) + (1 << i) - 1)) b++;
0111         msleep(3);
0112 
0113         if (b > 300) {              /* We allow 30% difference */
0114             release_region(io & (-1 << i), (1 << i));
0115             break;
0116         }
0117     }
0118 
0119     i--;
0120 
0121     if (i != 4) {
0122         if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
0123             return -EBUSY;
0124     }
0125 
0126     ns558 = kzalloc(sizeof(struct ns558), GFP_KERNEL);
0127     port = gameport_allocate_port();
0128     if (!ns558 || !port) {
0129         printk(KERN_ERR "ns558: Memory allocation failed.\n");
0130         release_region(io & (-1 << i), (1 << i));
0131         kfree(ns558);
0132         gameport_free_port(port);
0133         return -ENOMEM;
0134     }
0135 
0136     ns558->io = io;
0137     ns558->size = 1 << i;
0138     ns558->gameport = port;
0139 
0140     port->io = io;
0141     gameport_set_name(port, "NS558 ISA Gameport");
0142     gameport_set_phys(port, "isa%04x/gameport0", io & (-1 << i));
0143 
0144     gameport_register_port(port);
0145 
0146     list_add(&ns558->node, &ns558_list);
0147 
0148     return 0;
0149 }
0150 
0151 #ifdef CONFIG_PNP
0152 
0153 static const struct pnp_device_id pnp_devids[] = {
0154     { .id = "@P@0001", .driver_data = 0 }, /* ALS 100 */
0155     { .id = "@P@0020", .driver_data = 0 }, /* ALS 200 */
0156     { .id = "@P@1001", .driver_data = 0 }, /* ALS 100+ */
0157     { .id = "@P@2001", .driver_data = 0 }, /* ALS 120 */
0158     { .id = "ASB16fd", .driver_data = 0 }, /* AdLib NSC16 */
0159     { .id = "AZT3001", .driver_data = 0 }, /* AZT1008 */
0160     { .id = "CDC0001", .driver_data = 0 }, /* Opl3-SAx */
0161     { .id = "CSC0001", .driver_data = 0 }, /* CS4232 */
0162     { .id = "CSC000f", .driver_data = 0 }, /* CS4236 */
0163     { .id = "CSC0101", .driver_data = 0 }, /* CS4327 */
0164     { .id = "CTL7001", .driver_data = 0 }, /* SB16 */
0165     { .id = "CTL7002", .driver_data = 0 }, /* AWE64 */
0166     { .id = "CTL7005", .driver_data = 0 }, /* Vibra16 */
0167     { .id = "ENS2020", .driver_data = 0 }, /* SoundscapeVIVO */
0168     { .id = "ESS0001", .driver_data = 0 }, /* ES1869 */
0169     { .id = "ESS0005", .driver_data = 0 }, /* ES1878 */
0170     { .id = "ESS6880", .driver_data = 0 }, /* ES688 */
0171     { .id = "IBM0012", .driver_data = 0 }, /* CS4232 */
0172     { .id = "OPT0001", .driver_data = 0 }, /* OPTi Audio16 */
0173     { .id = "YMH0006", .driver_data = 0 }, /* Opl3-SA */
0174     { .id = "YMH0022", .driver_data = 0 }, /* Opl3-SAx */
0175     { .id = "PNPb02f", .driver_data = 0 }, /* Generic */
0176     { .id = "", },
0177 };
0178 
0179 MODULE_DEVICE_TABLE(pnp, pnp_devids);
0180 
0181 static int ns558_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *did)
0182 {
0183     int ioport, iolen;
0184     struct ns558 *ns558;
0185     struct gameport *port;
0186 
0187     if (!pnp_port_valid(dev, 0)) {
0188         printk(KERN_WARNING "ns558: No i/o ports on a gameport? Weird\n");
0189         return -ENODEV;
0190     }
0191 
0192     ioport = pnp_port_start(dev, 0);
0193     iolen = pnp_port_len(dev, 0);
0194 
0195     if (!request_region(ioport, iolen, "ns558-pnp"))
0196         return -EBUSY;
0197 
0198     ns558 = kzalloc(sizeof(struct ns558), GFP_KERNEL);
0199     port = gameport_allocate_port();
0200     if (!ns558 || !port) {
0201         printk(KERN_ERR "ns558: Memory allocation failed\n");
0202         kfree(ns558);
0203         gameport_free_port(port);
0204         return -ENOMEM;
0205     }
0206 
0207     ns558->io = ioport;
0208     ns558->size = iolen;
0209     ns558->dev = dev;
0210     ns558->gameport = port;
0211 
0212     gameport_set_name(port, "NS558 PnP Gameport");
0213     gameport_set_phys(port, "pnp%s/gameport0", dev_name(&dev->dev));
0214     port->dev.parent = &dev->dev;
0215     port->io = ioport;
0216 
0217     gameport_register_port(port);
0218 
0219     list_add_tail(&ns558->node, &ns558_list);
0220     return 0;
0221 }
0222 
0223 static struct pnp_driver ns558_pnp_driver = {
0224     .name       = "ns558",
0225     .id_table   = pnp_devids,
0226     .probe      = ns558_pnp_probe,
0227 };
0228 
0229 #else
0230 
0231 static struct pnp_driver ns558_pnp_driver;
0232 
0233 #endif
0234 
0235 static int __init ns558_init(void)
0236 {
0237     int i = 0;
0238     int error;
0239 
0240     error = pnp_register_driver(&ns558_pnp_driver);
0241     if (error && error != -ENODEV)  /* should be ENOSYS really */
0242         return error;
0243 
0244 /*
0245  * Probe ISA ports after PnP, so that PnP ports that are already
0246  * enabled get detected as PnP. This may be suboptimal in multi-device
0247  * configurations, but saves hassle with simple setups.
0248  */
0249 
0250     while (ns558_isa_portlist[i])
0251         ns558_isa_probe(ns558_isa_portlist[i++]);
0252 
0253     return list_empty(&ns558_list) && error ? -ENODEV : 0;
0254 }
0255 
0256 static void __exit ns558_exit(void)
0257 {
0258     struct ns558 *ns558, *safe;
0259 
0260     list_for_each_entry_safe(ns558, safe, &ns558_list, node) {
0261         gameport_unregister_port(ns558->gameport);
0262         release_region(ns558->io & ~(ns558->size - 1), ns558->size);
0263         kfree(ns558);
0264     }
0265 
0266     pnp_unregister_driver(&ns558_pnp_driver);
0267 }
0268 
0269 module_init(ns558_init);
0270 module_exit(ns558_exit);