bus.c 2.4 KB

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