bus.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 struct device nubus_parent = {
  45. .init_name = "nubus",
  46. };
  47. static int __init nubus_bus_register(void)
  48. {
  49. return bus_register(&nubus_bus_type);
  50. }
  51. postcore_initcall(nubus_bus_register);
  52. int __init nubus_parent_device_register(void)
  53. {
  54. return device_register(&nubus_parent);
  55. }
  56. static void nubus_device_release(struct device *dev)
  57. {
  58. struct nubus_board *board = to_nubus_board(dev);
  59. struct nubus_rsrc *fres, *tmp;
  60. list_for_each_entry_safe(fres, tmp, &nubus_func_rsrcs, list)
  61. if (fres->board == board) {
  62. list_del(&fres->list);
  63. kfree(fres);
  64. }
  65. kfree(board);
  66. }
  67. int nubus_device_register(struct nubus_board *board)
  68. {
  69. board->dev.parent = &nubus_parent;
  70. board->dev.release = nubus_device_release;
  71. board->dev.bus = &nubus_bus_type;
  72. dev_set_name(&board->dev, "slot.%X", board->slot);
  73. board->dev.dma_mask = &board->dev.coherent_dma_mask;
  74. dma_set_mask(&board->dev, DMA_BIT_MASK(32));
  75. return device_register(&board->dev);
  76. }
  77. static int nubus_print_device_name_fn(struct device *dev, void *data)
  78. {
  79. struct nubus_board *board = to_nubus_board(dev);
  80. struct seq_file *m = data;
  81. seq_printf(m, "Slot %X: %s\n", board->slot, board->name);
  82. return 0;
  83. }
  84. int nubus_proc_show(struct seq_file *m, void *data)
  85. {
  86. return bus_for_each_dev(&nubus_bus_type, NULL, m,
  87. nubus_print_device_name_fn);
  88. }