Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / nubus / bus.c
blobd306c348c857d87ae4cc1611dc1cdfecaa8be980
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Bus implementation for the NuBus subsystem.
4 //
5 // Copyright (C) 2017 Finn Thain
7 #include <linux/device.h>
8 #include <linux/list.h>
9 #include <linux/nubus.h>
10 #include <linux/seq_file.h>
11 #include <linux/slab.h>
13 #define to_nubus_board(d) container_of(d, struct nubus_board, dev)
14 #define to_nubus_driver(d) container_of(d, struct nubus_driver, driver)
16 static int nubus_bus_match(struct device *dev, struct device_driver *driver)
18 return 1;
21 static int nubus_device_probe(struct device *dev)
23 struct nubus_driver *ndrv = to_nubus_driver(dev->driver);
24 int err = -ENODEV;
26 if (ndrv->probe)
27 err = ndrv->probe(to_nubus_board(dev));
28 return err;
31 static int nubus_device_remove(struct device *dev)
33 struct nubus_driver *ndrv = to_nubus_driver(dev->driver);
34 int err = -ENODEV;
36 if (dev->driver && ndrv->remove)
37 err = ndrv->remove(to_nubus_board(dev));
38 return err;
41 struct bus_type nubus_bus_type = {
42 .name = "nubus",
43 .match = nubus_bus_match,
44 .probe = nubus_device_probe,
45 .remove = nubus_device_remove,
47 EXPORT_SYMBOL(nubus_bus_type);
49 int nubus_driver_register(struct nubus_driver *ndrv)
51 ndrv->driver.bus = &nubus_bus_type;
52 return driver_register(&ndrv->driver);
54 EXPORT_SYMBOL(nubus_driver_register);
56 void nubus_driver_unregister(struct nubus_driver *ndrv)
58 driver_unregister(&ndrv->driver);
60 EXPORT_SYMBOL(nubus_driver_unregister);
62 static struct device nubus_parent = {
63 .init_name = "nubus",
66 int __init nubus_bus_register(void)
68 int err;
70 err = device_register(&nubus_parent);
71 if (err)
72 return err;
74 err = bus_register(&nubus_bus_type);
75 if (!err)
76 return 0;
78 device_unregister(&nubus_parent);
79 return err;
82 static void nubus_device_release(struct device *dev)
84 struct nubus_board *board = to_nubus_board(dev);
85 struct nubus_rsrc *fres, *tmp;
87 list_for_each_entry_safe(fres, tmp, &nubus_func_rsrcs, list)
88 if (fres->board == board) {
89 list_del(&fres->list);
90 kfree(fres);
92 kfree(board);
95 int nubus_device_register(struct nubus_board *board)
97 board->dev.parent = &nubus_parent;
98 board->dev.release = nubus_device_release;
99 board->dev.bus = &nubus_bus_type;
100 dev_set_name(&board->dev, "slot.%X", board->slot);
101 return device_register(&board->dev);
104 static int nubus_print_device_name_fn(struct device *dev, void *data)
106 struct nubus_board *board = to_nubus_board(dev);
107 struct seq_file *m = data;
109 seq_printf(m, "Slot %X: %s\n", board->slot, board->name);
110 return 0;
113 int nubus_proc_show(struct seq_file *m, void *data)
115 return bus_for_each_dev(&nubus_bus_type, NULL, m,
116 nubus_print_device_name_fn);