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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <string.h>
  21. #include <ipxe/list.h>
  22. #include <ipxe/tables.h>
  23. #include <ipxe/init.h>
  24. #include <ipxe/interface.h>
  25. #include <ipxe/device.h>
  26. /**
  27. * @file
  28. *
  29. * Device model
  30. *
  31. */
  32. /** Registered root devices */
  33. static LIST_HEAD ( devices );
  34. /** Device removal inhibition counter */
  35. int device_keep_count = 0;
  36. /**
  37. * Probe a root device
  38. *
  39. * @v rootdev Root device
  40. * @ret rc Return status code
  41. */
  42. static int rootdev_probe ( struct root_device *rootdev ) {
  43. int rc;
  44. DBG ( "Adding %s root bus\n", rootdev->dev.name );
  45. if ( ( rc = rootdev->driver->probe ( rootdev ) ) != 0 ) {
  46. DBG ( "Failed to add %s root bus: %s\n",
  47. rootdev->dev.name, strerror ( rc ) );
  48. return rc;
  49. }
  50. return 0;
  51. }
  52. /**
  53. * Remove a root device
  54. *
  55. * @v rootdev Root device
  56. */
  57. static void rootdev_remove ( struct root_device *rootdev ) {
  58. rootdev->driver->remove ( rootdev );
  59. DBG ( "Removed %s root bus\n", rootdev->dev.name );
  60. }
  61. /**
  62. * Probe all devices
  63. *
  64. * This initiates probing for all devices in the system. After this
  65. * call, the device hierarchy will be populated, and all hardware
  66. * should be ready to use.
  67. */
  68. static void probe_devices ( void ) {
  69. struct root_device *rootdev;
  70. int rc;
  71. for_each_table_entry ( rootdev, ROOT_DEVICES ) {
  72. list_add ( &rootdev->dev.siblings, &devices );
  73. INIT_LIST_HEAD ( &rootdev->dev.children );
  74. if ( ( rc = rootdev_probe ( rootdev ) ) != 0 )
  75. list_del ( &rootdev->dev.siblings );
  76. }
  77. }
  78. /**
  79. * Remove all devices
  80. *
  81. */
  82. static void remove_devices ( int booting __unused ) {
  83. struct root_device *rootdev;
  84. struct root_device *tmp;
  85. if ( device_keep_count != 0 ) {
  86. DBG ( "Refusing to remove devices on shutdown\n" );
  87. return;
  88. }
  89. list_for_each_entry_safe ( rootdev, tmp, &devices, dev.siblings ) {
  90. rootdev_remove ( rootdev );
  91. list_del ( &rootdev->dev.siblings );
  92. }
  93. }
  94. struct startup_fn startup_devices __startup_fn ( STARTUP_NORMAL ) = {
  95. .startup = probe_devices,
  96. .shutdown = remove_devices,
  97. };
  98. /**
  99. * Identify a device behind an interface
  100. *
  101. * @v intf Interface
  102. * @ret device Device, or NULL
  103. */
  104. struct device * identify_device ( struct interface *intf ) {
  105. struct interface *dest;
  106. identify_device_TYPE ( void * ) *op =
  107. intf_get_dest_op ( intf, identify_device, &dest );
  108. void *object = intf_object ( dest );
  109. void *device;
  110. if ( op ) {
  111. device = op ( object );
  112. } else {
  113. /* Default is to return NULL */
  114. device = NULL;
  115. }
  116. intf_put ( dest );
  117. return device;
  118. }