bus.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 int nubus_device_remove(struct device *dev)
  27. {
  28. struct nubus_driver *ndrv = to_nubus_driver(dev->driver);
  29. int err = -ENODEV;
  30. if (dev->driver && ndrv->remove)
  31. err = ndrv->remove(to_nubus_board(dev));
  32. return err;
  33. }
  34. struct bus_type nubus_bus_type = {
  35. .name = "nubus",
  36. .match = nubus_bus_match,
  37. .probe = nubus_device_probe,
  38. .remove = nubus_device_remove,
  39. };
  40. EXPORT_SYMBOL(nubus_bus_type);
  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. void nubus_driver_unregister(struct nubus_driver *ndrv)
  48. {
  49. driver_unregister(&ndrv->driver);
  50. }
  51. EXPORT_SYMBOL(nubus_driver_unregister);
  52. static struct device nubus_parent = {
  53. .init_name = "nubus",
  54. };
  55. static int __init nubus_bus_register(void)
  56. {
  57. return bus_register(&nubus_bus_type);
  58. }
  59. postcore_initcall(nubus_bus_register);
  60. int __init nubus_parent_device_register(void)
  61. {
  62. return device_register(&nubus_parent);
  63. }
  64. static void nubus_device_release(struct device *dev)
  65. {
  66. struct nubus_board *board = to_nubus_board(dev);
  67. struct nubus_rsrc *fres, *tmp;
  68. list_for_each_entry_safe(fres, tmp, &nubus_func_rsrcs, list)
  69. if (fres->board == board) {
  70. list_del(&fres->list);
  71. kfree(fres);
  72. }
  73. kfree(board);
  74. }
  75. int nubus_device_register(struct nubus_board *board)
  76. {
  77. board->dev.parent = &nubus_parent;
  78. board->dev.release = nubus_device_release;
  79. board->dev.bus = &nubus_bus_type;
  80. dev_set_name(&board->dev, "slot.%X", board->slot);
  81. board->dev.dma_mask = &board->dev.coherent_dma_mask;
  82. dma_set_mask(&board->dev, DMA_BIT_MASK(32));
  83. return device_register(&board->dev);
  84. }
  85. static int nubus_print_device_name_fn(struct device *dev, void *data)
  86. {
  87. struct nubus_board *board = to_nubus_board(dev);
  88. struct seq_file *m = data;
  89. seq_printf(m, "Slot %X: %s\n", board->slot, board->name);
  90. return 0;
  91. }
  92. int nubus_proc_show(struct seq_file *m, void *data)
  93. {
  94. return bus_for_each_dev(&nubus_bus_type, NULL, m,
  95. nubus_print_device_name_fn);
  96. }