devices.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * (C) Copyright 2000
  3. * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <config.h>
  24. #include <common.h>
  25. #include <stdarg.h>
  26. #include <malloc.h>
  27. #include <devices.h>
  28. #include <serial.h>
  29. #ifdef CONFIG_LOGBUFFER
  30. #include <logbuff.h>
  31. #endif
  32. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
  33. #include <i2c.h>
  34. #endif
  35. DECLARE_GLOBAL_DATA_PTR;
  36. static device_t devs;
  37. device_t *stdio_devices[] = { NULL, NULL, NULL };
  38. char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
  39. #if defined(CONFIG_SPLASH_SCREEN) && !defined(CFG_DEVICE_NULLDEV)
  40. #define CFG_DEVICE_NULLDEV 1
  41. #endif
  42. #ifdef CFG_DEVICE_NULLDEV
  43. void nulldev_putc(const char c)
  44. {
  45. /* nulldev is empty! */
  46. }
  47. void nulldev_puts(const char *s)
  48. {
  49. /* nulldev is empty! */
  50. }
  51. int nulldev_input(void)
  52. {
  53. /* nulldev is empty! */
  54. return 0;
  55. }
  56. #endif
  57. /**************************************************************************
  58. * SYSTEM DRIVERS
  59. **************************************************************************
  60. */
  61. static void drv_system_init (void)
  62. {
  63. device_t dev;
  64. memset (&dev, 0, sizeof (dev));
  65. strcpy (dev.name, "serial");
  66. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  67. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  68. dev.putc = serial_buffered_putc;
  69. dev.puts = serial_buffered_puts;
  70. dev.getc = serial_buffered_getc;
  71. dev.tstc = serial_buffered_tstc;
  72. #else
  73. dev.putc = serial_putc;
  74. dev.puts = serial_puts;
  75. dev.getc = serial_getc;
  76. dev.tstc = serial_tstc;
  77. #endif
  78. device_register (&dev);
  79. #ifdef CFG_DEVICE_NULLDEV
  80. memset (&dev, 0, sizeof (dev));
  81. strcpy (dev.name, "nulldev");
  82. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  83. dev.putc = nulldev_putc;
  84. dev.puts = nulldev_puts;
  85. dev.getc = nulldev_input;
  86. dev.tstc = nulldev_input;
  87. device_register (&dev);
  88. #endif
  89. }
  90. /**************************************************************************
  91. * DEVICES
  92. **************************************************************************
  93. */
  94. struct list_head* device_get_list(void)
  95. {
  96. return &(devs.list);
  97. }
  98. device_t* device_get_by_name(char* name)
  99. {
  100. struct list_head *pos;
  101. device_t *dev;
  102. if(!name)
  103. return NULL;
  104. list_for_each(pos, &(devs.list)) {
  105. dev = list_entry(pos, device_t, list);
  106. if(strcmp(dev->name, name) == 0)
  107. return dev;
  108. }
  109. return NULL;
  110. }
  111. int device_register (device_t * dev)
  112. {
  113. list_add(&(dev->list), &(devs.list));
  114. return 0;
  115. }
  116. /* deregister the device "devname".
  117. * returns 0 if success, -1 if device is assigned and 1 if devname not found
  118. */
  119. #ifdef CFG_DEVICE_DEREGISTER
  120. int device_deregister(char *devname)
  121. {
  122. int l;
  123. struct list_head *pos;
  124. device_t *dev;
  125. char temp_names[3][8];
  126. dev = device_get_by_name(devname);
  127. if(!dev) /* device not found */
  128. return -1;
  129. /* get stdio devices (ListRemoveItem changes the dev list) */
  130. for (l=0 ; l< MAX_FILES; l++) {
  131. if (stdio_devices[l] == dev) {
  132. /* Device is assigned -> report error */
  133. return -1;
  134. }
  135. memcpy (&temp_names[l][0],
  136. stdio_devices[l]->name,
  137. sizeof(stdio_devices[l]->name));
  138. }
  139. list_del(&(dev->list));
  140. /* reassign Device list */
  141. list_for_each(pos, &(devs.list)) {
  142. dev = list_entry(pos, device_t, list);
  143. for (l=0 ; l< MAX_FILES; l++) {
  144. if(strcmp(dev->name, temp_names[l]) == 0)
  145. stdio_devices[l] = dev;
  146. }
  147. }
  148. return 0;
  149. }
  150. #endif /* CFG_DEVICE_DEREGISTER */
  151. int devices_init (void)
  152. {
  153. #ifndef CONFIG_ARM /* already relocated for current ARM implementation */
  154. ulong relocation_offset = gd->reloc_off;
  155. int i;
  156. /* relocate device name pointers */
  157. for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
  158. stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
  159. relocation_offset);
  160. }
  161. #endif
  162. /* Initialize the list */
  163. INIT_LIST_HEAD(&(devs.list));
  164. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
  165. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
  166. #endif
  167. #ifdef CONFIG_LCD
  168. drv_lcd_init ();
  169. #endif
  170. #if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
  171. drv_video_init ();
  172. #endif
  173. #ifdef CONFIG_KEYBOARD
  174. drv_keyboard_init ();
  175. #endif
  176. #ifdef CONFIG_LOGBUFFER
  177. drv_logbuff_init ();
  178. #endif
  179. drv_system_init ();
  180. #ifdef CONFIG_SERIAL_MULTI
  181. serial_devices_init ();
  182. #endif
  183. #ifdef CONFIG_USB_TTY
  184. drv_usbtty_init ();
  185. #endif
  186. #ifdef CONFIG_NETCONSOLE
  187. drv_nc_init ();
  188. #endif
  189. return (0);
  190. }