1 // SPDX-License-Identifier: GPL-2.0-only
3 * EISA bus support functions for sysfs.
5 * (C) 2002, 2003 Marc Zyngier <maz@wild-wind.fr.eu.org>
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/ioport.h>
18 #define SLOT_ADDRESS(r,n) (r->bus_base_addr + (0x1000 * n))
20 #define EISA_DEVINFO(i,s) { .id = { .sig = i }, .name = s }
22 struct eisa_device_info
{
23 struct eisa_device_id id
;
27 #ifdef CONFIG_EISA_NAMES
28 static struct eisa_device_info __initdata eisa_table
[] = {
31 #define EISA_INFOS (sizeof (eisa_table) / (sizeof (struct eisa_device_info)))
34 #define EISA_MAX_FORCED_DEV 16
36 static int enable_dev
[EISA_MAX_FORCED_DEV
];
37 static unsigned int enable_dev_count
;
38 static int disable_dev
[EISA_MAX_FORCED_DEV
];
39 static unsigned int disable_dev_count
;
41 static int is_forced_dev(int *forced_tab
,
43 struct eisa_root_device
*root
,
44 struct eisa_device
*edev
)
48 for (i
= 0; i
< forced_count
; i
++) {
49 x
= (root
->bus_nr
<< 8) | edev
->slot
;
50 if (forced_tab
[i
] == x
)
57 static void __init
eisa_name_device(struct eisa_device
*edev
)
59 #ifdef CONFIG_EISA_NAMES
61 for (i
= 0; i
< EISA_INFOS
; i
++) {
62 if (!strcmp(edev
->id
.sig
, eisa_table
[i
].id
.sig
)) {
63 strlcpy(edev
->pretty_name
,
65 sizeof(edev
->pretty_name
));
70 /* No name was found */
71 sprintf(edev
->pretty_name
, "EISA device %.7s", edev
->id
.sig
);
75 static char __init
*decode_eisa_sig(unsigned long addr
)
77 static char sig_str
[EISA_SIG_LEN
];
82 for (i
= 0; i
< 4; i
++) {
83 #ifdef CONFIG_EISA_VLB_PRIMING
85 * This ugly stuff is used to wake up VL-bus cards
86 * (AHA-284x is the only known example), so we can
89 * Thankfully, this only exists on x86...
93 sig
[i
] = inb(addr
+ i
);
95 if (!i
&& (sig
[0] & 0x80))
99 sig_str
[0] = ((sig
[0] >> 2) & 0x1f) + ('A' - 1);
100 sig_str
[1] = (((sig
[0] & 3) << 3) | (sig
[1] >> 5)) + ('A' - 1);
101 sig_str
[2] = (sig
[1] & 0x1f) + ('A' - 1);
102 rev
= (sig
[2] << 8) | sig
[3];
103 sprintf(sig_str
+ 3, "%04X", rev
);
108 static int eisa_bus_match(struct device
*dev
, struct device_driver
*drv
)
110 struct eisa_device
*edev
= to_eisa_device(dev
);
111 struct eisa_driver
*edrv
= to_eisa_driver(drv
);
112 const struct eisa_device_id
*eids
= edrv
->id_table
;
117 while (strlen(eids
->sig
)) {
118 if (!strcmp(eids
->sig
, edev
->id
.sig
) &&
119 edev
->state
& EISA_CONFIG_ENABLED
) {
120 edev
->id
.driver_data
= eids
->driver_data
;
130 static int eisa_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
132 struct eisa_device
*edev
= to_eisa_device(dev
);
134 add_uevent_var(env
, "MODALIAS=" EISA_DEVICE_MODALIAS_FMT
, edev
->id
.sig
);
138 struct bus_type eisa_bus_type
= {
140 .match
= eisa_bus_match
,
141 .uevent
= eisa_bus_uevent
,
143 EXPORT_SYMBOL(eisa_bus_type
);
145 int eisa_driver_register(struct eisa_driver
*edrv
)
147 edrv
->driver
.bus
= &eisa_bus_type
;
148 return driver_register(&edrv
->driver
);
150 EXPORT_SYMBOL(eisa_driver_register
);
152 void eisa_driver_unregister(struct eisa_driver
*edrv
)
154 driver_unregister(&edrv
->driver
);
156 EXPORT_SYMBOL(eisa_driver_unregister
);
158 static ssize_t
eisa_show_sig(struct device
*dev
, struct device_attribute
*attr
,
161 struct eisa_device
*edev
= to_eisa_device(dev
);
162 return sprintf(buf
, "%s\n", edev
->id
.sig
);
165 static DEVICE_ATTR(signature
, S_IRUGO
, eisa_show_sig
, NULL
);
167 static ssize_t
eisa_show_state(struct device
*dev
,
168 struct device_attribute
*attr
,
171 struct eisa_device
*edev
= to_eisa_device(dev
);
172 return sprintf(buf
, "%d\n", edev
->state
& EISA_CONFIG_ENABLED
);
175 static DEVICE_ATTR(enabled
, S_IRUGO
, eisa_show_state
, NULL
);
177 static ssize_t
eisa_show_modalias(struct device
*dev
,
178 struct device_attribute
*attr
,
181 struct eisa_device
*edev
= to_eisa_device(dev
);
182 return sprintf(buf
, EISA_DEVICE_MODALIAS_FMT
"\n", edev
->id
.sig
);
185 static DEVICE_ATTR(modalias
, S_IRUGO
, eisa_show_modalias
, NULL
);
187 static int __init
eisa_init_device(struct eisa_root_device
*root
,
188 struct eisa_device
*edev
,
192 unsigned long sig_addr
;
195 sig_addr
= SLOT_ADDRESS(root
, slot
) + EISA_VENDOR_ID_OFFSET
;
197 sig
= decode_eisa_sig(sig_addr
);
199 return -1; /* No EISA device here */
201 memcpy(edev
->id
.sig
, sig
, EISA_SIG_LEN
);
203 edev
->state
= inb(SLOT_ADDRESS(root
, slot
) + EISA_CONFIG_OFFSET
)
204 & EISA_CONFIG_ENABLED
;
205 edev
->base_addr
= SLOT_ADDRESS(root
, slot
);
206 edev
->dma_mask
= root
->dma_mask
; /* Default DMA mask */
207 eisa_name_device(edev
);
208 edev
->dev
.parent
= root
->dev
;
209 edev
->dev
.bus
= &eisa_bus_type
;
210 edev
->dev
.dma_mask
= &edev
->dma_mask
;
211 edev
->dev
.coherent_dma_mask
= edev
->dma_mask
;
212 dev_set_name(&edev
->dev
, "%02X:%02X", root
->bus_nr
, slot
);
214 for (i
= 0; i
< EISA_MAX_RESOURCES
; i
++) {
215 #ifdef CONFIG_EISA_NAMES
216 edev
->res
[i
].name
= edev
->pretty_name
;
218 edev
->res
[i
].name
= edev
->id
.sig
;
222 if (is_forced_dev(enable_dev
, enable_dev_count
, root
, edev
))
223 edev
->state
= EISA_CONFIG_ENABLED
| EISA_CONFIG_FORCED
;
225 if (is_forced_dev(disable_dev
, disable_dev_count
, root
, edev
))
226 edev
->state
= EISA_CONFIG_FORCED
;
231 static int __init
eisa_register_device(struct eisa_device
*edev
)
233 int rc
= device_register(&edev
->dev
);
235 put_device(&edev
->dev
);
239 rc
= device_create_file(&edev
->dev
, &dev_attr_signature
);
242 rc
= device_create_file(&edev
->dev
, &dev_attr_enabled
);
245 rc
= device_create_file(&edev
->dev
, &dev_attr_modalias
);
252 device_remove_file(&edev
->dev
, &dev_attr_enabled
);
254 device_remove_file(&edev
->dev
, &dev_attr_signature
);
256 device_unregister(&edev
->dev
);
260 static int __init
eisa_request_resources(struct eisa_root_device
*root
,
261 struct eisa_device
*edev
,
266 for (i
= 0; i
< EISA_MAX_RESOURCES
; i
++) {
267 /* Don't register resource for slot 0, since this is
268 * very likely to fail... :-( Instead, grab the EISA
269 * id, now we can display something in /proc/ioports.
272 /* Only one region for mainboard */
273 if (!slot
&& i
> 0) {
274 edev
->res
[i
].start
= edev
->res
[i
].end
= 0;
279 edev
->res
[i
].name
= NULL
;
280 edev
->res
[i
].start
= SLOT_ADDRESS(root
, slot
)
282 edev
->res
[i
].end
= edev
->res
[i
].start
+ 0xff;
283 edev
->res
[i
].flags
= IORESOURCE_IO
;
285 edev
->res
[i
].name
= NULL
;
286 edev
->res
[i
].start
= SLOT_ADDRESS(root
, slot
)
287 + EISA_VENDOR_ID_OFFSET
;
288 edev
->res
[i
].end
= edev
->res
[i
].start
+ 3;
289 edev
->res
[i
].flags
= IORESOURCE_IO
| IORESOURCE_BUSY
;
292 if (request_resource(root
->res
, &edev
->res
[i
]))
300 release_resource(&edev
->res
[i
]);
305 static void __init
eisa_release_resources(struct eisa_device
*edev
)
309 for (i
= 0; i
< EISA_MAX_RESOURCES
; i
++)
310 if (edev
->res
[i
].start
|| edev
->res
[i
].end
)
311 release_resource(&edev
->res
[i
]);
314 static int __init
eisa_probe(struct eisa_root_device
*root
)
317 struct eisa_device
*edev
;
320 dev_info(root
->dev
, "Probing EISA bus %d\n", root
->bus_nr
);
322 /* First try to get hold of slot 0. If there is no device
323 * here, simply fail, unless root->force_probe is set. */
325 edev
= kzalloc(sizeof(*edev
), GFP_KERNEL
);
329 if (eisa_request_resources(root
, edev
, 0)) {
331 "EISA: Cannot allocate resource for mainboard\n");
333 if (!root
->force_probe
)
338 if (eisa_init_device(root
, edev
, 0)) {
339 eisa_release_resources(edev
);
341 if (!root
->force_probe
)
346 dev_info(&edev
->dev
, "EISA: Mainboard %s detected\n", edev
->id
.sig
);
348 if (eisa_register_device(edev
)) {
349 dev_err(&edev
->dev
, "EISA: Failed to register %s\n",
351 eisa_release_resources(edev
);
357 for (c
= 0, i
= 1; i
<= root
->slots
; i
++) {
358 edev
= kzalloc(sizeof(*edev
), GFP_KERNEL
);
360 dev_err(root
->dev
, "EISA: Out of memory for slot %d\n",
365 if (eisa_request_resources(root
, edev
, i
)) {
367 "Cannot allocate resource for EISA slot %d\n",
373 if (eisa_init_device(root
, edev
, i
)) {
374 eisa_release_resources(edev
);
379 if (edev
->state
== (EISA_CONFIG_ENABLED
| EISA_CONFIG_FORCED
))
380 enabled_str
= " (forced enabled)";
381 else if (edev
->state
== EISA_CONFIG_FORCED
)
382 enabled_str
= " (forced disabled)";
383 else if (edev
->state
== 0)
384 enabled_str
= " (disabled)";
388 dev_info(&edev
->dev
, "EISA: slot %d: %s detected%s\n", i
,
389 edev
->id
.sig
, enabled_str
);
393 if (eisa_register_device(edev
)) {
394 dev_err(&edev
->dev
, "EISA: Failed to register %s\n",
396 eisa_release_resources(edev
);
401 dev_info(root
->dev
, "EISA: Detected %d card%s\n", c
, c
== 1 ? "" : "s");
405 static struct resource eisa_root_res
= {
406 .name
= "EISA root resource",
409 .flags
= IORESOURCE_IO
,
412 static int eisa_bus_count
;
414 int __init
eisa_root_register(struct eisa_root_device
*root
)
418 /* Use our own resources to check if this bus base address has
419 * been already registered. This prevents the virtual root
420 * device from registering after the real one has, for
423 root
->eisa_root_res
.name
= eisa_root_res
.name
;
424 root
->eisa_root_res
.start
= root
->res
->start
;
425 root
->eisa_root_res
.end
= root
->res
->end
;
426 root
->eisa_root_res
.flags
= IORESOURCE_BUSY
;
428 err
= request_resource(&eisa_root_res
, &root
->eisa_root_res
);
432 root
->bus_nr
= eisa_bus_count
++;
434 err
= eisa_probe(root
);
436 release_resource(&root
->eisa_root_res
);
441 static int __init
eisa_init(void)
445 r
= bus_register(&eisa_bus_type
);
449 printk(KERN_INFO
"EISA bus registered\n");
453 module_param_array(enable_dev
, int, &enable_dev_count
, 0444);
454 module_param_array(disable_dev
, int, &disable_dev_count
, 0444);
456 postcore_initcall(eisa_init
);
458 int EISA_bus
; /* for legacy drivers */
459 EXPORT_SYMBOL(EISA_bus
);