^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * ISA bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/isa.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static struct device isa_bus = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) .init_name = "isa"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct isa_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct device dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct device *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define to_isa_dev(x) container_of((x), struct isa_dev, dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int isa_bus_match(struct device *dev, struct device_driver *driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct isa_driver *isa_driver = to_isa_driver(driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (dev->platform_data == isa_driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (!isa_driver->match ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) isa_driver->match(dev, to_isa_dev(dev)->id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) dev->platform_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static int isa_bus_probe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct isa_driver *isa_driver = dev->platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (isa_driver && isa_driver->probe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return isa_driver->probe(dev, to_isa_dev(dev)->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int isa_bus_remove(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct isa_driver *isa_driver = dev->platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (isa_driver && isa_driver->remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return isa_driver->remove(dev, to_isa_dev(dev)->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static void isa_bus_shutdown(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct isa_driver *isa_driver = dev->platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (isa_driver && isa_driver->shutdown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) isa_driver->shutdown(dev, to_isa_dev(dev)->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int isa_bus_suspend(struct device *dev, pm_message_t state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct isa_driver *isa_driver = dev->platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (isa_driver && isa_driver->suspend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return isa_driver->suspend(dev, to_isa_dev(dev)->id, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static int isa_bus_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct isa_driver *isa_driver = dev->platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (isa_driver && isa_driver->resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return isa_driver->resume(dev, to_isa_dev(dev)->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static struct bus_type isa_bus_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .name = "isa",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .match = isa_bus_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .probe = isa_bus_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .remove = isa_bus_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .shutdown = isa_bus_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .suspend = isa_bus_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .resume = isa_bus_resume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static void isa_dev_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) kfree(to_isa_dev(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) void isa_unregister_driver(struct isa_driver *isa_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct device *dev = isa_driver->devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) while (dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct device *tmp = to_isa_dev(dev)->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) device_unregister(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) dev = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) driver_unregister(&isa_driver->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) EXPORT_SYMBOL_GPL(isa_unregister_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int isa_register_driver(struct isa_driver *isa_driver, unsigned int ndev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) isa_driver->driver.bus = &isa_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) isa_driver->devices = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) error = driver_register(&isa_driver->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) for (id = 0; id < ndev; id++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct isa_dev *isa_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) isa_dev = kzalloc(sizeof *isa_dev, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (!isa_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) isa_dev->dev.parent = &isa_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) isa_dev->dev.bus = &isa_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) dev_set_name(&isa_dev->dev, "%s.%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) isa_driver->driver.name, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) isa_dev->dev.platform_data = isa_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) isa_dev->dev.release = isa_dev_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) isa_dev->id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) isa_dev->dev.coherent_dma_mask = DMA_BIT_MASK(24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) isa_dev->dev.dma_mask = &isa_dev->dev.coherent_dma_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) error = device_register(&isa_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) put_device(&isa_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (isa_dev->dev.platform_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) isa_dev->next = isa_driver->devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) isa_driver->devices = &isa_dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) device_unregister(&isa_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (!error && !isa_driver->devices)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) error = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) isa_unregister_driver(isa_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) EXPORT_SYMBOL_GPL(isa_register_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int __init isa_bus_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) error = bus_register(&isa_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) error = device_register(&isa_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) bus_unregister(&isa_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) postcore_initcall(isa_bus_init);