bus.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_bus_match(struct device *dev, struct device_driver *driver)
  15. {
  16. return 1;
  17. }
  18. static int nubus_device_probe(struct device *dev)
  19. {
  20. struct nubus_driver *ndrv = to_nubus_driver(dev->driver);
  21. int err = -ENODEV;
  22. if (ndrv->probe)
  23. err = ndrv->probe(to_nubus_board(dev));
  24. return err;
  25. }
  26. static void nubus_device_remove(struct device *dev)
  27. {
  28. struct nubus_driver *ndrv = to_nubus_driver(dev->driver);
  29. if (ndrv->remove)
  30. ndrv->remove(to_nubus_board(dev));
  31. }
  32. struct bus_type nubus_bus_type = {
  33. .name = "nubus",
  34. .match = nubus_bus_match,
  35. .probe = nubus_device_probe,
  36. .remove = nubus_device_remove,
  37. };
  38. EXPORT_SYMBOL(nubus_bus_type);
  39. int nubus_driver_register(struct nubus_driver *ndrv)
  40. {
  41. ndrv->driver.bus = &nubus_bus_type;
  42. return driver_register(&ndrv->driver);
  43. }
  44. EXPORT_SYMBOL(nubus_driver_register);
  45. void nubus_driver_unregister(struct nubus_driver *ndrv)
  46. {
  47. driver_unregister(&ndrv->driver);
  48. }
  49. EXPORT_SYMBOL(nubus_driver_unregister);
  50. static struct device nubus_parent = {
  51. .init_name = "nubus",
  52. };
  53. static int __init nubus_bus_register(void)
  54. {
  55. return bus_register(&nubus_bus_type);
  56. }
  57. postcore_initcall(nubus_bus_register);
  58. int __init nubus_parent_device_register(void)
  59. {
  60. return device_register(&nubus_parent);
  61. }
  62. static void nubus_device_release(struct device *dev)
  63. {
  64. struct nubus_board *board = to_nubus_board(dev);
  65. struct nubus_rsrc *fres, *tmp;
  66. list_for_each_entry_safe(fres, tmp, &nubus_func_rsrcs, list)
  67. if (fres->board == board) {
  68. list_del(&fres->list);
  69. kfree(fres);
  70. }
  71. kfree(board);
  72. }
  73. int nubus_device_register(struct nubus_board *board)
  74. {
  75. board->dev.parent = &nubus_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. static int nubus_print_device_name_fn(struct device *dev, void *data)
  84. {
  85. struct nubus_board *board = to_nubus_board(dev);
  86. struct seq_file *m = data;
  87. seq_printf(m, "Slot %X: %s\n", board->slot, board->name);
  88. return 0;
  89. }
  90. int nubus_proc_show(struct seq_file *m, void *data)
  91. {
  92. return bus_for_each_dev(&nubus_bus_type, NULL, m,
  93. nubus_print_device_name_fn);
  94. }