devreg.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * $Log$
  35. * Revision 1.7 2009/01/17 15:37:52 haraldkipp
  36. * Added some NUTASSERT macros to check function parameters.
  37. *
  38. * Revision 1.6 2009/01/17 11:26:52 haraldkipp
  39. * Getting rid of two remaining BSD types in favor of stdint.
  40. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  41. *
  42. * Revision 1.5 2008/08/11 07:00:33 haraldkipp
  43. * BSD types replaced by stdint types (feature request #1282721).
  44. *
  45. * Revision 1.4 2004/09/08 10:24:26 haraldkipp
  46. * RAMSTART is too platform dependant
  47. *
  48. * Revision 1.3 2004/03/18 13:42:50 haraldkipp
  49. * Deprecated header file removed
  50. *
  51. * Revision 1.2 2004/03/16 16:48:45 haraldkipp
  52. * Added Jan Dubiec's H8/300 port.
  53. *
  54. * Revision 1.1.1.1 2003/05/09 14:41:47 haraldkipp
  55. * Initial using 3.2.1
  56. *
  57. * Revision 1.3 2003/04/21 17:06:40 harald
  58. * Device init moved to registration
  59. *
  60. * Revision 1.2 2003/02/04 18:15:56 harald
  61. * Version 3 released
  62. *
  63. * Revision 1.1 2003/01/17 18:55:24 harald
  64. * Device routines splitted
  65. *
  66. */
  67. #include <string.h>
  68. #include <sys/nutdebug.h>
  69. #include <dev/uart.h>
  70. /*!
  71. * \addtogroup xgDevice
  72. */
  73. /*@{*/
  74. /*!
  75. * \brief Linked list of all registered devices.
  76. */
  77. NUTDEVICE *nutDeviceList = 0;
  78. /*!
  79. * \brief Find device entry by name.
  80. *
  81. * \param name Unique device name.
  82. *
  83. * \return Pointer to the ::NUTDEVICE structure.
  84. */
  85. NUTDEVICE *NutDeviceLookup(const char *name)
  86. {
  87. NUTDEVICE *dev;
  88. NUTASSERT(name != NULL);
  89. for (dev = nutDeviceList; dev; dev = dev->dev_next)
  90. if (strcmp(dev->dev_name, name) == 0)
  91. break;
  92. return dev;
  93. }
  94. /*!
  95. * \brief Find device entry by type.
  96. *
  97. * \param dev Pointer to the device returned by the last call. Set to
  98. * NULL to start searching at the first entry.
  99. * \param type Device type. May be any of the following:
  100. * - \ref IFTYP_RAM
  101. * - \ref IFTYP_ROM
  102. * - \ref IFTYP_STREAM
  103. * - \ref IFTYP_NET
  104. * - \ref IFTYP_TCPSOCK
  105. * - \ref IFTYP_CHAR
  106. * - \ref IFTYP_CAN
  107. * - \ref IFTYP_BLKIO
  108. * - \ref IFTYP_FS
  109. *
  110. * \return Pointer to the \ref NUTDEVICE structure or NULL if no
  111. * more devices were found.
  112. */
  113. NUTDEVICE *NutDeviceLookupType(NUTDEVICE *dev, uint_fast8_t type)
  114. {
  115. if (dev == NULL) {
  116. dev = nutDeviceList;
  117. } else {
  118. dev = dev->dev_next;
  119. }
  120. while (dev) {
  121. if (dev->dev_type == type) {
  122. break;
  123. }
  124. dev = dev->dev_next;
  125. }
  126. return dev;
  127. }
  128. /*!
  129. * \brief Register and initialize a device.
  130. *
  131. * Initializes the device and adds it to the system device list.
  132. * Applications should call this function during initialization
  133. * for each device they intend to use.
  134. *
  135. * \param dev Pointer to the ::NUTDEVICE structure, which is
  136. * provided by the device driver. This structure
  137. * contains a hardware device name, which must be
  138. * unique among all registered devices. Drivers may
  139. * operate in a different mode using the same hardware,
  140. * like interrupt driven or polling UART drivers.
  141. * Only one of those drivers can be registered, because
  142. * they specify the same hardware device name.
  143. * \param base Hardware base address of this device. Set to 0,
  144. * if the device driver has a fixed hardware address.
  145. * \param irq Hardware interrupt used by this device. Set to 0,
  146. * if the device driver doesn't support configurable
  147. * interupts.
  148. * \return 0 if the device has been registered for the first time
  149. * and initialization was successful.
  150. * The function returns -1 if any device with the same name
  151. * had been registered previously, if the ::NUTDEVICE
  152. * structure is invalid or if the device initialization
  153. * failed.
  154. */
  155. int NutRegisterDevice(NUTDEVICE * dev, uintptr_t base, uint8_t irq)
  156. {
  157. int rc = -1;
  158. NUTASSERT(dev != NULL);
  159. if (base)
  160. dev->dev_base = base;
  161. if (irq)
  162. dev->dev_irq = irq;
  163. if (NutDeviceLookup(dev->dev_name) == 0) {
  164. if(dev->dev_init == 0 || (*dev->dev_init)(dev) == 0) {
  165. dev->dev_next = nutDeviceList;
  166. nutDeviceList = dev;
  167. rc = 0;
  168. }
  169. }
  170. return rc;
  171. }
  172. /*@}*/