xref: /linux/drivers/eisa/eisa-bus.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * EISA bus support functions for sysfs.
4  *
5  * (C) 2002, 2003 Marc Zyngier <maz@wild-wind.fr.eu.org>
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/device.h>
10 #include <linux/eisa.h>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/string_choices.h>
16 #include <linux/ioport.h>
17 #include <asm/io.h>
18 
19 #define SLOT_ADDRESS(r,n) (r->bus_base_addr + (0x1000 * n))
20 
21 #define EISA_DEVINFO(i,s) { .id = { .sig = i }, .name = s }
22 
23 struct eisa_device_info {
24 	struct eisa_device_id id;
25 	char name[EISA_DEVICE_INFO_NAME_SIZE];
26 };
27 
28 #ifdef CONFIG_EISA_NAMES
29 static struct eisa_device_info __initdata eisa_table[] = {
30 #include "devlist.h"
31 };
32 #define EISA_INFOS (sizeof (eisa_table) / (sizeof (struct eisa_device_info)))
33 #endif
34 
35 #define EISA_MAX_FORCED_DEV 16
36 
37 static int enable_dev[EISA_MAX_FORCED_DEV];
38 static unsigned int enable_dev_count;
39 static int disable_dev[EISA_MAX_FORCED_DEV];
40 static unsigned int disable_dev_count;
41 
42 static int is_forced_dev(int *forced_tab,
43 			 int forced_count,
44 			 struct eisa_root_device *root,
45 			 struct eisa_device *edev)
46 {
47 	int i, x;
48 
49 	for (i = 0; i < forced_count; i++) {
50 		x = (root->bus_nr << 8) | edev->slot;
51 		if (forced_tab[i] == x)
52 			return 1;
53 	}
54 
55 	return 0;
56 }
57 
58 static void __init eisa_name_device(struct eisa_device *edev)
59 {
60 #ifdef CONFIG_EISA_NAMES
61 	int i;
62 	for (i = 0; i < EISA_INFOS; i++) {
63 		if (!strcmp(edev->id.sig, eisa_table[i].id.sig)) {
64 			strscpy(edev->pretty_name,
65 				eisa_table[i].name,
66 				sizeof(edev->pretty_name));
67 			return;
68 		}
69 	}
70 
71 	/* No name was found */
72 	sprintf(edev->pretty_name, "EISA device %.7s", edev->id.sig);
73 #endif
74 }
75 
76 static char __init *decode_eisa_sig(unsigned long addr)
77 {
78 	static char sig_str[EISA_SIG_LEN];
79 	u8 sig[4];
80 	u16 rev;
81 	int i;
82 
83 	for (i = 0; i < 4; i++) {
84 #ifdef CONFIG_EISA_VLB_PRIMING
85 		/*
86 		 * This ugly stuff is used to wake up VL-bus cards
87 		 * (AHA-284x is the only known example), so we can
88 		 * read the EISA id.
89 		 *
90 		 * Thankfully, this only exists on x86...
91 		 */
92 		outb(0x80 + i, addr);
93 #endif
94 		sig[i] = inb(addr + i);
95 
96 		if (!i && (sig[0] & 0x80))
97 			return NULL;
98 	}
99 
100 	sig_str[0] = ((sig[0] >> 2) & 0x1f) + ('A' - 1);
101 	sig_str[1] = (((sig[0] & 3) << 3) | (sig[1] >> 5)) + ('A' - 1);
102 	sig_str[2] = (sig[1] & 0x1f) + ('A' - 1);
103 	rev = (sig[2] << 8) | sig[3];
104 	sprintf(sig_str + 3, "%04X", rev);
105 
106 	return sig_str;
107 }
108 
109 static int eisa_bus_match(struct device *dev, const struct device_driver *drv)
110 {
111 	struct eisa_device *edev = to_eisa_device(dev);
112 	const struct eisa_driver *edrv = to_eisa_driver(drv);
113 	const struct eisa_device_id *eids = edrv->id_table;
114 
115 	if (!eids)
116 		return 0;
117 
118 	while (strlen(eids->sig)) {
119 		if (!strcmp(eids->sig, edev->id.sig) &&
120 		    edev->state & EISA_CONFIG_ENABLED) {
121 			edev->id.driver_data = eids->driver_data;
122 			return 1;
123 		}
124 
125 		eids++;
126 	}
127 
128 	return 0;
129 }
130 
131 static int eisa_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
132 {
133 	const struct eisa_device *edev = to_eisa_device(dev);
134 
135 	add_uevent_var(env, "MODALIAS=" EISA_DEVICE_MODALIAS_FMT, edev->id.sig);
136 	return 0;
137 }
138 
139 const struct bus_type eisa_bus_type = {
140 	.name  = "eisa",
141 	.match = eisa_bus_match,
142 	.uevent = eisa_bus_uevent,
143 };
144 EXPORT_SYMBOL(eisa_bus_type);
145 
146 int eisa_driver_register(struct eisa_driver *edrv)
147 {
148 	edrv->driver.bus = &eisa_bus_type;
149 	return driver_register(&edrv->driver);
150 }
151 EXPORT_SYMBOL(eisa_driver_register);
152 
153 void eisa_driver_unregister(struct eisa_driver *edrv)
154 {
155 	driver_unregister(&edrv->driver);
156 }
157 EXPORT_SYMBOL(eisa_driver_unregister);
158 
159 static ssize_t signature_show(struct device *dev,
160 			      struct device_attribute *attr, char *buf)
161 {
162 	struct eisa_device *edev = to_eisa_device(dev);
163 	return sprintf(buf, "%s\n", edev->id.sig);
164 }
165 static DEVICE_ATTR_RO(signature);
166 
167 static ssize_t enabled_show(struct device *dev,
168 			    struct device_attribute *attr, char *buf)
169 {
170 	struct eisa_device *edev = to_eisa_device(dev);
171 	return sprintf(buf, "%d\n", edev->state & EISA_CONFIG_ENABLED);
172 }
173 static DEVICE_ATTR_RO(enabled);
174 
175 static ssize_t modalias_show(struct device *dev,
176 			     struct device_attribute *attr, char *buf)
177 {
178 	struct eisa_device *edev = to_eisa_device(dev);
179 	return sprintf(buf, EISA_DEVICE_MODALIAS_FMT "\n", edev->id.sig);
180 }
181 static DEVICE_ATTR_RO(modalias);
182 
183 static int __init eisa_init_device(struct eisa_root_device *root,
184 				   struct eisa_device *edev,
185 				   int slot)
186 {
187 	char *sig;
188 	unsigned long sig_addr;
189 	int i;
190 
191 	sig_addr = SLOT_ADDRESS(root, slot) + EISA_VENDOR_ID_OFFSET;
192 
193 	sig = decode_eisa_sig(sig_addr);
194 	if (!sig)
195 		return -1;	/* No EISA device here */
196 
197 	memcpy(edev->id.sig, sig, EISA_SIG_LEN);
198 	edev->slot = slot;
199 	edev->state = inb(SLOT_ADDRESS(root, slot) + EISA_CONFIG_OFFSET)
200 		      & EISA_CONFIG_ENABLED;
201 	edev->base_addr = SLOT_ADDRESS(root, slot);
202 	edev->dma_mask = root->dma_mask; /* Default DMA mask */
203 	eisa_name_device(edev);
204 	edev->dev.parent = root->dev;
205 	edev->dev.bus = &eisa_bus_type;
206 	edev->dev.dma_mask = &edev->dma_mask;
207 	edev->dev.coherent_dma_mask = edev->dma_mask;
208 	dev_set_name(&edev->dev, "%02X:%02X", root->bus_nr, slot);
209 
210 	for (i = 0; i < EISA_MAX_RESOURCES; i++) {
211 #ifdef CONFIG_EISA_NAMES
212 		edev->res[i].name = edev->pretty_name;
213 #else
214 		edev->res[i].name = edev->id.sig;
215 #endif
216 	}
217 
218 	if (is_forced_dev(enable_dev, enable_dev_count, root, edev))
219 		edev->state = EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED;
220 
221 	if (is_forced_dev(disable_dev, disable_dev_count, root, edev))
222 		edev->state = EISA_CONFIG_FORCED;
223 
224 	return 0;
225 }
226 
227 static int __init eisa_register_device(struct eisa_device *edev)
228 {
229 	int rc = device_register(&edev->dev);
230 	if (rc) {
231 		put_device(&edev->dev);
232 		return rc;
233 	}
234 
235 	rc = device_create_file(&edev->dev, &dev_attr_signature);
236 	if (rc)
237 		goto err_devreg;
238 	rc = device_create_file(&edev->dev, &dev_attr_enabled);
239 	if (rc)
240 		goto err_sig;
241 	rc = device_create_file(&edev->dev, &dev_attr_modalias);
242 	if (rc)
243 		goto err_enab;
244 
245 	return 0;
246 
247 err_enab:
248 	device_remove_file(&edev->dev, &dev_attr_enabled);
249 err_sig:
250 	device_remove_file(&edev->dev, &dev_attr_signature);
251 err_devreg:
252 	device_unregister(&edev->dev);
253 	return rc;
254 }
255 
256 static int __init eisa_request_resources(struct eisa_root_device *root,
257 					 struct eisa_device *edev,
258 					 int slot)
259 {
260 	int i;
261 
262 	for (i = 0; i < EISA_MAX_RESOURCES; i++) {
263 		/* Don't register resource for slot 0, since this is
264 		 * very likely to fail... :-( Instead, grab the EISA
265 		 * id, now we can display something in /proc/ioports.
266 		 */
267 
268 		/* Only one region for mainboard */
269 		if (!slot && i > 0) {
270 			edev->res[i].start = edev->res[i].end = 0;
271 			continue;
272 		}
273 
274 		if (slot) {
275 			edev->res[i].name  = NULL;
276 			edev->res[i].start = SLOT_ADDRESS(root, slot)
277 					     + (i * 0x400);
278 			edev->res[i].end   = edev->res[i].start + 0xff;
279 			edev->res[i].flags = IORESOURCE_IO;
280 		} else {
281 			edev->res[i].name  = NULL;
282 			edev->res[i].start = SLOT_ADDRESS(root, slot)
283 					     + EISA_VENDOR_ID_OFFSET;
284 			edev->res[i].end   = edev->res[i].start + 3;
285 			edev->res[i].flags = IORESOURCE_IO | IORESOURCE_BUSY;
286 		}
287 
288 		if (request_resource(root->res, &edev->res[i]))
289 			goto failed;
290 	}
291 
292 	return 0;
293 
294  failed:
295 	while (--i >= 0)
296 		release_resource(&edev->res[i]);
297 
298 	return -1;
299 }
300 
301 static void __init eisa_release_resources(struct eisa_device *edev)
302 {
303 	int i;
304 
305 	for (i = 0; i < EISA_MAX_RESOURCES; i++)
306 		if (edev->res[i].start || edev->res[i].end)
307 			release_resource(&edev->res[i]);
308 }
309 
310 static int __init eisa_probe(struct eisa_root_device *root)
311 {
312 	int i, c;
313 	struct eisa_device *edev;
314 	char *enabled_str;
315 
316 	dev_info(root->dev, "Probing EISA bus %d\n", root->bus_nr);
317 
318 	/* First try to get hold of slot 0. If there is no device
319 	 * here, simply fail, unless root->force_probe is set. */
320 
321 	edev = kzalloc_obj(*edev);
322 	if (!edev)
323 		return -ENOMEM;
324 
325 	if (eisa_request_resources(root, edev, 0)) {
326 		dev_warn(root->dev,
327 			 "EISA: Cannot allocate resource for mainboard\n");
328 		kfree(edev);
329 		if (!root->force_probe)
330 			return -EBUSY;
331 		goto force_probe;
332 	}
333 
334 	if (eisa_init_device(root, edev, 0)) {
335 		eisa_release_resources(edev);
336 		kfree(edev);
337 		if (!root->force_probe)
338 			return -ENODEV;
339 		goto force_probe;
340 	}
341 
342 	dev_info(&edev->dev, "EISA: Mainboard %s detected\n", edev->id.sig);
343 
344 	if (eisa_register_device(edev)) {
345 		dev_err(&edev->dev, "EISA: Failed to register %s\n",
346 			edev->id.sig);
347 		eisa_release_resources(edev);
348 		kfree(edev);
349 	}
350 
351  force_probe:
352 
353 	for (c = 0, i = 1; i <= root->slots; i++) {
354 		edev = kzalloc_obj(*edev);
355 		if (!edev) {
356 			dev_err(root->dev, "EISA: Out of memory for slot %d\n",
357 				i);
358 			continue;
359 		}
360 
361 		if (eisa_request_resources(root, edev, i)) {
362 			dev_warn(root->dev,
363 				 "Cannot allocate resource for EISA slot %d\n",
364 				 i);
365 			kfree(edev);
366 			continue;
367 		}
368 
369 		if (eisa_init_device(root, edev, i)) {
370 			eisa_release_resources(edev);
371 			kfree(edev);
372 			continue;
373 		}
374 
375 		if (edev->state == (EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED))
376 			enabled_str = " (forced enabled)";
377 		else if (edev->state == EISA_CONFIG_FORCED)
378 			enabled_str = " (forced disabled)";
379 		else if (edev->state == 0)
380 			enabled_str = " (disabled)";
381 		else
382 			enabled_str = "";
383 
384 		dev_info(&edev->dev, "EISA: slot %d: %s detected%s\n", i,
385 			 edev->id.sig, enabled_str);
386 
387 		c++;
388 
389 		if (eisa_register_device(edev)) {
390 			dev_err(&edev->dev, "EISA: Failed to register %s\n",
391 				edev->id.sig);
392 			eisa_release_resources(edev);
393 			kfree(edev);
394 		}
395 	}
396 
397 	dev_info(root->dev, "EISA: Detected %d card%s\n", c, str_plural(c));
398 	return 0;
399 }
400 
401 static struct resource eisa_root_res = {
402 	.name  = "EISA root resource",
403 	.start = 0,
404 	.end   = 0xffffffff,
405 	.flags = IORESOURCE_IO,
406 };
407 
408 static int eisa_bus_count;
409 
410 int __init eisa_root_register(struct eisa_root_device *root)
411 {
412 	int err;
413 
414 	/* Use our own resources to check if this bus base address has
415 	 * been already registered. This prevents the virtual root
416 	 * device from registering after the real one has, for
417 	 * example... */
418 
419 	root->eisa_root_res.name  = eisa_root_res.name;
420 	root->eisa_root_res.start = root->res->start;
421 	root->eisa_root_res.end   = root->res->end;
422 	root->eisa_root_res.flags = IORESOURCE_BUSY;
423 
424 	err = request_resource(&eisa_root_res, &root->eisa_root_res);
425 	if (err)
426 		return err;
427 
428 	root->bus_nr = eisa_bus_count++;
429 
430 	err = eisa_probe(root);
431 	if (err)
432 		release_resource(&root->eisa_root_res);
433 
434 	return err;
435 }
436 
437 static int __init eisa_init(void)
438 {
439 	int r;
440 
441 	r = bus_register(&eisa_bus_type);
442 	if (r)
443 		return r;
444 
445 	printk(KERN_INFO "EISA bus registered\n");
446 	return 0;
447 }
448 
449 module_param_array(enable_dev, int, &enable_dev_count, 0444);
450 module_param_array(disable_dev, int, &disable_dev_count, 0444);
451 
452 postcore_initcall(eisa_init);
453 
454 int EISA_bus;		/* for legacy drivers */
455 EXPORT_SYMBOL(EISA_bus);
456