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.

dev.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "etherboot.h"
  2. #include "stddef.h"
  3. #include "dev.h"
  4. /*
  5. * Each bus driver defines several methods, which are described in
  6. * dev.h. This file provides a centralised, bus-independent mechanism
  7. * for locating devices and drivers.
  8. *
  9. */
  10. /* Linker symbols for the various tables */
  11. static struct bus_driver bus_drivers[0] __table_start ( bus_driver );
  12. static struct bus_driver bus_drivers_end[0] __table_end ( bus_driver );
  13. static struct device_driver device_drivers[0] __table_start ( device_driver );
  14. static struct device_driver device_drivers_end[0] __table_end (device_driver );
  15. /* Current attempted boot device */
  16. struct dev dev = {
  17. .bus_driver = bus_drivers,
  18. .device_driver = device_drivers,
  19. };
  20. /*
  21. * Print all drivers
  22. *
  23. */
  24. void print_drivers ( void ) {
  25. struct device_driver *driver;
  26. for ( driver = device_drivers ;
  27. driver < device_drivers_end ;
  28. driver++ ) {
  29. printf ( "%s ", driver->name );
  30. }
  31. }
  32. /*
  33. * Move to the next location on any bus
  34. *
  35. */
  36. static inline int next_location ( struct bus_driver **bus_driver,
  37. struct bus_loc *bus_loc ) {
  38. /* Move to next location on this bus, if any */
  39. if ( (*bus_driver)->next_location ( bus_loc ) )
  40. return 1;
  41. /* Move to first (zeroed) location on next bus, if any */
  42. if ( ++(*bus_driver) < bus_drivers_end ) {
  43. DBG ( "DEV scanning %s bus\n", (*bus_driver)->name );
  44. return 1;
  45. }
  46. /* Reset to first bus, return "no more locations" */
  47. *bus_driver = bus_drivers;
  48. return 0;
  49. }
  50. /*
  51. * Find the next available device on any bus
  52. *
  53. * Set skip=1 to skip over the current device
  54. *
  55. */
  56. int find_any ( struct bus_driver **bus_driver, struct bus_loc *bus_loc,
  57. struct bus_dev *bus_dev, signed int skip ) {
  58. DBG ( "DEV scanning %s bus\n", (*bus_driver)->name );
  59. do {
  60. if ( --skip >= 0 )
  61. continue;
  62. if ( ! (*bus_driver)->fill_device ( bus_dev, bus_loc ) )
  63. continue;
  64. DBG ( "DEV found device %s\n",
  65. (*bus_driver)->describe_device ( bus_dev ) );
  66. return 1;
  67. } while ( next_location ( bus_driver, bus_loc ) );
  68. DBG ( "DEV found no more devices\n" );
  69. return 0;
  70. }
  71. /*
  72. * Find a driver by specified device.
  73. *
  74. * Set skip=1 to skip over the current driver
  75. *
  76. */
  77. int find_by_device ( struct device_driver **device_driver,
  78. struct bus_driver *bus_driver, struct bus_dev *bus_dev,
  79. signed int skip ) {
  80. do {
  81. if ( --skip >= 0 )
  82. continue;
  83. if ( (*device_driver)->bus_driver != bus_driver )
  84. continue;
  85. if ( ! bus_driver->check_driver ( bus_dev, *device_driver ))
  86. continue;
  87. DBG ( "DEV found driver %s for device %s\n",
  88. (*device_driver)->name,
  89. bus_driver->describe_device ( bus_dev ) );
  90. return 1;
  91. } while ( ++(*device_driver) < device_drivers_end );
  92. /* Reset to first driver, return "not found" */
  93. DBG ( "DEV found no driver for device %s\n",
  94. bus_driver->describe_device ( bus_dev ) );
  95. *device_driver = device_drivers;
  96. return 0;
  97. }
  98. /*
  99. * Find a device by specified driver.
  100. *
  101. * Set skip=1 to skip over the current device
  102. *
  103. */
  104. int find_by_driver ( struct bus_loc *bus_loc, struct bus_dev *bus_dev,
  105. struct device_driver *device_driver,
  106. signed int skip ) {
  107. struct bus_driver *bus_driver = device_driver->bus_driver;
  108. do {
  109. if ( --skip >= 0 )
  110. continue;
  111. if ( ! bus_driver->fill_device ( bus_dev, bus_loc ) )
  112. continue;
  113. if ( ! bus_driver->check_driver ( bus_dev, device_driver ) )
  114. continue;
  115. DBG ( "DEV found device %s for driver %s\n",
  116. bus_driver->describe_device ( bus_dev ),
  117. device_driver->name );
  118. return 1;
  119. } while ( bus_driver->next_location ( bus_loc ) );
  120. DBG ( "DEV found no device for driver %s\n", device_driver->name );
  121. return 0;
  122. }
  123. /*
  124. * Find the next available (device,driver) combination
  125. *
  126. * Set skip=1 to skip over the current (device,driver)
  127. *
  128. * Note that the struct dev may not have been previously used, and so
  129. * may not contain a valid (device,driver) combination.
  130. *
  131. */
  132. int find_any_with_driver ( struct dev *dev, signed int skip ) {
  133. signed int skip_device = 0;
  134. signed int skip_driver = skip;
  135. while ( find_any ( &dev->bus_driver, &dev->bus_loc, &dev->bus_dev,
  136. skip_device ) ) {
  137. if ( find_by_device ( &dev->device_driver, dev->bus_driver,
  138. &dev->bus_dev, skip_driver ) ) {
  139. /* Set type_driver to be that of the device
  140. * driver
  141. */
  142. dev->type_driver = dev->device_driver->type_driver;
  143. /* Set type device instance to be the single
  144. * instance provided by the type driver
  145. */
  146. dev->type_dev = dev->type_driver->type_dev;
  147. return 1;
  148. }
  149. skip_driver = 0;
  150. skip_device = 1;
  151. }
  152. return 0;
  153. }