1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Bus implementation for the NuBus subsystem. 4 // 5 // Copyright (C) 2017 Finn Thain 6 7 #include <linux/device.h> 8 #include <linux/dma-mapping.h> 9 #include <linux/list.h> 10 #include <linux/nubus.h> 11 #include <linux/seq_file.h> 12 #include <linux/slab.h> 13 14 #define to_nubus_board(d) container_of(d, struct nubus_board, dev) 15 #define to_nubus_driver(d) container_of(d, struct nubus_driver, driver) 16 17 static int nubus_device_probe(struct device *dev) 18 { 19 struct nubus_driver *ndrv = to_nubus_driver(dev->driver); 20 int err = -ENODEV; 21 22 if (ndrv->probe) 23 err = ndrv->probe(to_nubus_board(dev)); 24 return err; 25 } 26 27 static void nubus_device_remove(struct device *dev) 28 { 29 struct nubus_driver *ndrv = to_nubus_driver(dev->driver); 30 31 if (ndrv->remove) 32 ndrv->remove(to_nubus_board(dev)); 33 } 34 35 static const struct bus_type nubus_bus_type = { 36 .name = "nubus", 37 .probe = nubus_device_probe, 38 .remove = nubus_device_remove, 39 }; 40 41 int nubus_driver_register(struct nubus_driver *ndrv) 42 { 43 ndrv->driver.bus = &nubus_bus_type; 44 return driver_register(&ndrv->driver); 45 } 46 EXPORT_SYMBOL(nubus_driver_register); 47 48 void nubus_driver_unregister(struct nubus_driver *ndrv) 49 { 50 driver_unregister(&ndrv->driver); 51 } 52 EXPORT_SYMBOL(nubus_driver_unregister); 53 54 static int __init nubus_bus_register(void) 55 { 56 return bus_register(&nubus_bus_type); 57 } 58 postcore_initcall(nubus_bus_register); 59 60 static void nubus_device_release(struct device *dev) 61 { 62 struct nubus_board *board = to_nubus_board(dev); 63 struct nubus_rsrc *fres, *tmp; 64 65 list_for_each_entry_safe(fres, tmp, &nubus_func_rsrcs, list) 66 if (fres->board == board) { 67 list_del(&fres->list); 68 kfree(fres); 69 } 70 kfree(board); 71 } 72 73 int nubus_device_register(struct device *parent, struct nubus_board *board) 74 { 75 board->dev.parent = parent; 76 board->dev.release = nubus_device_release; 77 board->dev.bus = &nubus_bus_type; 78 dev_set_name(&board->dev, "slot.%X", board->slot); 79 board->dev.dma_mask = &board->dev.coherent_dma_mask; 80 dma_set_mask(&board->dev, DMA_BIT_MASK(32)); 81 return device_register(&board->dev); 82 } 83 84 static int nubus_print_device_name_fn(struct device *dev, void *data) 85 { 86 struct nubus_board *board = to_nubus_board(dev); 87 struct seq_file *m = data; 88 89 seq_printf(m, "Slot %X: %s\n", board->slot, board->name); 90 return 0; 91 } 92 93 int nubus_proc_show(struct seq_file *m, void *data) 94 { 95 return bus_for_each_dev(&nubus_bus_type, NULL, m, 96 nubus_print_device_name_fn); 97 } 98