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.4KB

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