You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

device.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <string.h>
  19. #include <gpxe/list.h>
  20. #include <gpxe/tables.h>
  21. #include <gpxe/device.h>
  22. #include <gpxe/init.h>
  23. /**
  24. * @file
  25. *
  26. * Device model
  27. *
  28. */
  29. static struct root_device root_devices[0]
  30. __table_start ( struct root_device, root_devices );
  31. static struct root_device root_devices_end[0]
  32. __table_end ( struct root_device, root_devices );
  33. /** Registered root devices */
  34. static LIST_HEAD ( devices );
  35. /**
  36. * Probe a root device
  37. *
  38. * @v rootdev Root device
  39. * @ret rc Return status code
  40. */
  41. static int rootdev_probe ( struct root_device *rootdev ) {
  42. int rc;
  43. DBG ( "Adding %s root bus\n", rootdev->dev.name );
  44. if ( ( rc = rootdev->driver->probe ( rootdev ) ) != 0 ) {
  45. DBG ( "Failed to add %s root bus: %s\n",
  46. rootdev->dev.name, strerror ( rc ) );
  47. return rc;
  48. }
  49. return 0;
  50. }
  51. /**
  52. * Remove a root device
  53. *
  54. * @v rootdev Root device
  55. */
  56. static void rootdev_remove ( struct root_device *rootdev ) {
  57. rootdev->driver->remove ( rootdev );
  58. DBG ( "Removed %s root bus\n", rootdev->dev.name );
  59. }
  60. /**
  61. * Probe all devices
  62. *
  63. * This initiates probing for all devices in the system. After this
  64. * call, the device hierarchy will be populated, and all hardware
  65. * should be ready to use.
  66. */
  67. static void probe_devices ( void ) {
  68. struct root_device *rootdev;
  69. int rc;
  70. for ( rootdev = root_devices; rootdev < root_devices_end; rootdev++ ) {
  71. list_add ( &rootdev->dev.siblings, &devices );
  72. INIT_LIST_HEAD ( &rootdev->dev.children );
  73. if ( ( rc = rootdev_probe ( rootdev ) ) != 0 )
  74. list_del ( &rootdev->dev.siblings );
  75. }
  76. }
  77. /**
  78. * Remove all devices
  79. *
  80. */
  81. static void remove_devices ( int flags ) {
  82. struct root_device *rootdev;
  83. struct root_device *tmp;
  84. if ( flags & SHUTDOWN_KEEP_DEVICES ) {
  85. DBG ( "Refusing to remove devices on shutdown\n" );
  86. return;
  87. }
  88. list_for_each_entry_safe ( rootdev, tmp, &devices, dev.siblings ) {
  89. rootdev_remove ( rootdev );
  90. list_del ( &rootdev->dev.siblings );
  91. }
  92. }
  93. struct startup_fn startup_devices __startup_fn ( STARTUP_NORMAL ) = {
  94. .startup = probe_devices,
  95. .shutdown = remove_devices,
  96. };