1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/device.h>
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/isa.h>
14 static struct device isa_bus
= {
24 #define to_isa_dev(x) container_of((x), struct isa_dev, dev)
26 static int isa_bus_match(struct device
*dev
, struct device_driver
*driver
)
28 struct isa_driver
*isa_driver
= to_isa_driver(driver
);
30 if (dev
->platform_data
== isa_driver
) {
31 if (!isa_driver
->match
||
32 isa_driver
->match(dev
, to_isa_dev(dev
)->id
))
34 dev
->platform_data
= NULL
;
39 static int isa_bus_probe(struct device
*dev
)
41 struct isa_driver
*isa_driver
= dev
->platform_data
;
43 if (isa_driver
&& isa_driver
->probe
)
44 return isa_driver
->probe(dev
, to_isa_dev(dev
)->id
);
49 static int isa_bus_remove(struct device
*dev
)
51 struct isa_driver
*isa_driver
= dev
->platform_data
;
53 if (isa_driver
&& isa_driver
->remove
)
54 return isa_driver
->remove(dev
, to_isa_dev(dev
)->id
);
59 static void isa_bus_shutdown(struct device
*dev
)
61 struct isa_driver
*isa_driver
= dev
->platform_data
;
63 if (isa_driver
&& isa_driver
->shutdown
)
64 isa_driver
->shutdown(dev
, to_isa_dev(dev
)->id
);
67 static int isa_bus_suspend(struct device
*dev
, pm_message_t state
)
69 struct isa_driver
*isa_driver
= dev
->platform_data
;
71 if (isa_driver
&& isa_driver
->suspend
)
72 return isa_driver
->suspend(dev
, to_isa_dev(dev
)->id
, state
);
77 static int isa_bus_resume(struct device
*dev
)
79 struct isa_driver
*isa_driver
= dev
->platform_data
;
81 if (isa_driver
&& isa_driver
->resume
)
82 return isa_driver
->resume(dev
, to_isa_dev(dev
)->id
);
87 static struct bus_type isa_bus_type
= {
89 .match
= isa_bus_match
,
90 .probe
= isa_bus_probe
,
91 .remove
= isa_bus_remove
,
92 .shutdown
= isa_bus_shutdown
,
93 .suspend
= isa_bus_suspend
,
94 .resume
= isa_bus_resume
97 static void isa_dev_release(struct device
*dev
)
99 kfree(to_isa_dev(dev
));
102 void isa_unregister_driver(struct isa_driver
*isa_driver
)
104 struct device
*dev
= isa_driver
->devices
;
107 struct device
*tmp
= to_isa_dev(dev
)->next
;
108 device_unregister(dev
);
111 driver_unregister(&isa_driver
->driver
);
113 EXPORT_SYMBOL_GPL(isa_unregister_driver
);
115 int isa_register_driver(struct isa_driver
*isa_driver
, unsigned int ndev
)
120 isa_driver
->driver
.bus
= &isa_bus_type
;
121 isa_driver
->devices
= NULL
;
123 error
= driver_register(&isa_driver
->driver
);
127 for (id
= 0; id
< ndev
; id
++) {
128 struct isa_dev
*isa_dev
;
130 isa_dev
= kzalloc(sizeof *isa_dev
, GFP_KERNEL
);
136 isa_dev
->dev
.parent
= &isa_bus
;
137 isa_dev
->dev
.bus
= &isa_bus_type
;
139 dev_set_name(&isa_dev
->dev
, "%s.%u",
140 isa_driver
->driver
.name
, id
);
141 isa_dev
->dev
.platform_data
= isa_driver
;
142 isa_dev
->dev
.release
= isa_dev_release
;
145 isa_dev
->dev
.coherent_dma_mask
= DMA_BIT_MASK(24);
146 isa_dev
->dev
.dma_mask
= &isa_dev
->dev
.coherent_dma_mask
;
148 error
= device_register(&isa_dev
->dev
);
150 put_device(&isa_dev
->dev
);
154 if (isa_dev
->dev
.platform_data
) {
155 isa_dev
->next
= isa_driver
->devices
;
156 isa_driver
->devices
= &isa_dev
->dev
;
158 device_unregister(&isa_dev
->dev
);
161 if (!error
&& !isa_driver
->devices
)
165 isa_unregister_driver(isa_driver
);
169 EXPORT_SYMBOL_GPL(isa_register_driver
);
171 static int __init
isa_bus_init(void)
175 error
= bus_register(&isa_bus_type
);
177 error
= device_register(&isa_bus
);
179 bus_unregister(&isa_bus_type
);
184 postcore_initcall(isa_bus_init
);