1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include <asm/io.h>
33
34#include <linux/module.h>
35#include <linux/ioport.h>
36#include <linux/config.h>
37#include <linux/init.h>
38#include <linux/delay.h>
39#include <linux/gameport.h>
40#include <linux/slab.h>
41#include <linux/pnp.h>
42
43MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
44MODULE_DESCRIPTION("Classic gameport (ISA/PnP) driver");
45MODULE_LICENSE("GPL");
46
47#define NS558_ISA 1
48#define NS558_PNP 2
49
50static int ns558_isa_portlist[] = { 0x201, 0x200, 0x202, 0x203, 0x204, 0x205, 0x207, 0x209,
51 0x20b, 0x20c, 0x20e, 0x20f, 0x211, 0x219, 0x101, 0 };
52
53struct ns558 {
54 int type;
55 int size;
56 struct pnp_dev *dev;
57 struct list_head node;
58 struct gameport gameport;
59 char phys[32];
60 char name[32];
61};
62
63static LIST_HEAD(ns558_list);
64
65
66
67
68
69
70
71static void ns558_isa_probe(int io)
72{
73 int i, j, b;
74 unsigned char c, u, v;
75 struct ns558 *port;
76
77
78
79
80
81 if (!request_region(io, 1, "ns558-isa"))
82 return;
83
84
85
86
87
88
89 c = inb(io);
90 outb(~c & ~3, io);
91 if (~(u = v = inb(io)) & 3) {
92 outb(c, io);
93 i = 0;
94 goto out;
95 }
96
97
98
99
100 for (i = 0; i < 1000; i++) v &= inb(io);
101
102 if (u == v) {
103 outb(c, io);
104 i = 0;
105 goto out;
106 }
107 msleep(3);
108
109
110
111
112 u = inb(io);
113 for (i = 0; i < 1000; i++)
114 if ((u ^ inb(io)) & 0xf) {
115 outb(c, io);
116 i = 0;
117 goto out;
118 }
119
120
121
122
123 for (i = 1; i < 5; i++) {
124
125 release_region(io & (-1 << (i-1)), (1 << (i-1)));
126
127 if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
128 break;
129
130 outb(0xff, io & (-1 << i));
131 for (j = b = 0; j < 1000; j++)
132 if (inb(io & (-1 << i)) != inb((io & (-1 << i)) + (1 << i) - 1)) b++;
133 msleep(3);
134
135 if (b > 300) {
136 release_region(io & (-1 << i), (1 << i));
137 break;
138 }
139 }
140
141 i--;
142
143 if (i != 4) {
144 if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
145 return;
146 }
147
148 if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
149 printk(KERN_ERR "ns558: Memory allocation failed.\n");
150 goto out;
151 }
152 memset(port, 0, sizeof(struct ns558));
153
154 port->type = NS558_ISA;
155 port->size = (1 << i);
156 port->gameport.io = io;
157 port->gameport.phys = port->phys;
158 port->gameport.name = port->name;
159 port->gameport.id.bustype = BUS_ISA;
160
161 sprintf(port->phys, "isa%04x/gameport0", io & (-1 << i));
162 sprintf(port->name, "NS558 ISA");
163
164 gameport_register_port(&port->gameport);
165
166 printk(KERN_INFO "gameport: NS558 ISA at %#x", port->gameport.io);
167 if (port->size > 1) printk(" size %d", port->size);
168 printk(" speed %d kHz\n", port->gameport.speed);
169
170 list_add(&port->node, &ns558_list);
171 return;
172out:
173 release_region(io & (-1 << i), (1 << i));
174}
175
176#ifdef CONFIG_PNP
177
178static struct pnp_device_id pnp_devids[] = {
179 { .id = "@P@0001", .driver_data = 0 },
180 { .id = "@P@0020", .driver_data = 0 },
181 { .id = "@P@1001", .driver_data = 0 },
182 { .id = "@P@2001", .driver_data = 0 },
183 { .id = "ASB16fd", .driver_data = 0 },
184 { .id = "AZT3001", .driver_data = 0 },
185 { .id = "CDC0001", .driver_data = 0 },
186 { .id = "CSC0001", .driver_data = 0 },
187 { .id = "CSC000f", .driver_data = 0 },
188 { .id = "CSC0101", .driver_data = 0 },
189 { .id = "CTL7001", .driver_data = 0 },
190 { .id = "CTL7002", .driver_data = 0 },
191 { .id = "CTL7005", .driver_data = 0 },
192 { .id = "ENS2020", .driver_data = 0 },
193 { .id = "ESS0001", .driver_data = 0 },
194 { .id = "ESS0005", .driver_data = 0 },
195 { .id = "ESS6880", .driver_data = 0 },
196 { .id = "IBM0012", .driver_data = 0 },
197 { .id = "OPT0001", .driver_data = 0 },
198 { .id = "YMH0006", .driver_data = 0 },
199 { .id = "YMH0022", .driver_data = 0 },
200 { .id = "PNPb02f", .driver_data = 0 },
201 { .id = "", },
202};
203
204MODULE_DEVICE_TABLE(pnp, pnp_devids);
205
206static int ns558_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *did)
207{
208 int ioport, iolen;
209 struct ns558 *port;
210
211 if (!pnp_port_valid(dev, 0)) {
212 printk(KERN_WARNING "ns558: No i/o ports on a gameport? Weird\n");
213 return -ENODEV;
214 }
215
216 ioport = pnp_port_start(dev,0);
217 iolen = pnp_port_len(dev,0);
218
219 if (!request_region(ioport, iolen, "ns558-pnp"))
220 return -EBUSY;
221
222 if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
223 printk(KERN_ERR "ns558: Memory allocation failed.\n");
224 return -ENOMEM;
225 }
226 memset(port, 0, sizeof(struct ns558));
227
228 port->type = NS558_PNP;
229 port->size = iolen;
230 port->dev = dev;
231
232 port->gameport.io = ioport;
233 port->gameport.phys = port->phys;
234 port->gameport.name = port->name;
235 port->gameport.id.bustype = BUS_ISAPNP;
236 port->gameport.id.version = 0x100;
237
238 sprintf(port->phys, "pnp%s/gameport0", dev->dev.bus_id);
239 sprintf(port->name, "%s", "NS558 PnP Gameport");
240
241 gameport_register_port(&port->gameport);
242
243 printk(KERN_INFO "gameport: NS558 PnP at pnp%s io %#x",
244 dev->dev.bus_id, port->gameport.io);
245 if (iolen > 1) printk(" size %d", iolen);
246 printk(" speed %d kHz\n", port->gameport.speed);
247
248 list_add_tail(&port->node, &ns558_list);
249 return 0;
250}
251
252static struct pnp_driver ns558_pnp_driver = {
253 .name = "ns558",
254 .id_table = pnp_devids,
255 .probe = ns558_pnp_probe,
256};
257
258#else
259
260static struct pnp_driver ns558_pnp_driver;
261
262#endif
263
264int __init ns558_init(void)
265{
266 int i = 0;
267
268
269
270
271
272 while (ns558_isa_portlist[i])
273 ns558_isa_probe(ns558_isa_portlist[i++]);
274
275 pnp_register_driver(&ns558_pnp_driver);
276 return list_empty(&ns558_list) ? -ENODEV : 0;
277}
278
279void __exit ns558_exit(void)
280{
281 struct ns558 *port;
282
283 list_for_each_entry(port, &ns558_list, node) {
284 gameport_unregister_port(&port->gameport);
285 switch (port->type) {
286
287#ifdef CONFIG_PNP
288 case NS558_PNP:
289
290#endif
291 case NS558_ISA:
292 release_region(port->gameport.io & ~(port->size - 1), port->size);
293 kfree(port);
294 break;
295
296 default:
297 break;
298 }
299 }
300 pnp_unregister_driver(&ns558_pnp_driver);
301}
302
303module_init(ns558_init);
304module_exit(ns558_exit);
305