stdio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <stdio_dev.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 struct stdio_dev devs;
  37. struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
  38. char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
  39. #if defined(CONFIG_SPLASH_SCREEN) && !defined(CONFIG_SYS_DEVICE_NULLDEV)
  40. #define CONFIG_SYS_DEVICE_NULLDEV 1
  41. #endif
  42. #ifdef CONFIG_SYS_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. struct stdio_dev 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. dev.putc = serial_putc;
  68. dev.puts = serial_puts;
  69. dev.getc = serial_getc;
  70. dev.tstc = serial_tstc;
  71. stdio_register (&dev);
  72. #ifdef CONFIG_SYS_DEVICE_NULLDEV
  73. memset (&dev, 0, sizeof (dev));
  74. strcpy (dev.name, "nulldev");
  75. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  76. dev.putc = nulldev_putc;
  77. dev.puts = nulldev_puts;
  78. dev.getc = nulldev_input;
  79. dev.tstc = nulldev_input;
  80. stdio_register (&dev);
  81. #endif
  82. }
  83. /**************************************************************************
  84. * DEVICES
  85. **************************************************************************
  86. */
  87. struct list_head* stdio_get_list(void)
  88. {
  89. return &(devs.list);
  90. }
  91. struct stdio_dev* stdio_get_by_name(const char *name)
  92. {
  93. struct list_head *pos;
  94. struct stdio_dev *dev;
  95. if(!name)
  96. return NULL;
  97. list_for_each(pos, &(devs.list)) {
  98. dev = list_entry(pos, struct stdio_dev, list);
  99. if(strcmp(dev->name, name) == 0)
  100. return dev;
  101. }
  102. return NULL;
  103. }
  104. struct stdio_dev* stdio_clone(struct stdio_dev *dev)
  105. {
  106. struct stdio_dev *_dev;
  107. if(!dev)
  108. return NULL;
  109. _dev = calloc(1, sizeof(struct stdio_dev));
  110. if(!_dev)
  111. return NULL;
  112. memcpy(_dev, dev, sizeof(struct stdio_dev));
  113. strncpy(_dev->name, dev->name, 16);
  114. return _dev;
  115. }
  116. int stdio_register (struct stdio_dev * dev)
  117. {
  118. struct stdio_dev *_dev;
  119. _dev = stdio_clone(dev);
  120. if(!_dev)
  121. return -1;
  122. list_add_tail(&(_dev->list), &(devs.list));
  123. return 0;
  124. }
  125. /* deregister the device "devname".
  126. * returns 0 if success, -1 if device is assigned and 1 if devname not found
  127. */
  128. #ifdef CONFIG_SYS_STDIO_DEREGISTER
  129. int stdio_deregister(const char *devname)
  130. {
  131. int l;
  132. struct list_head *pos;
  133. struct stdio_dev *dev;
  134. char temp_names[3][16];
  135. dev = stdio_get_by_name(devname);
  136. if(!dev) /* device not found */
  137. return -1;
  138. /* get stdio devices (ListRemoveItem changes the dev list) */
  139. for (l=0 ; l< MAX_FILES; l++) {
  140. if (stdio_devices[l] == dev) {
  141. /* Device is assigned -> report error */
  142. return -1;
  143. }
  144. memcpy (&temp_names[l][0],
  145. stdio_devices[l]->name,
  146. sizeof(temp_names[l]));
  147. }
  148. list_del(&(dev->list));
  149. /* reassign Device list */
  150. list_for_each(pos, &(devs.list)) {
  151. dev = list_entry(pos, struct stdio_dev, list);
  152. for (l=0 ; l< MAX_FILES; l++) {
  153. if(strcmp(dev->name, temp_names[l]) == 0)
  154. stdio_devices[l] = dev;
  155. }
  156. }
  157. return 0;
  158. }
  159. #endif /* CONFIG_SYS_STDIO_DEREGISTER */
  160. int stdio_init (void)
  161. {
  162. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  163. /* already relocated for current ARM implementation */
  164. ulong relocation_offset = gd->reloc_off;
  165. int i;
  166. /* relocate device name pointers */
  167. for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
  168. stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
  169. relocation_offset);
  170. }
  171. #endif /* CONFIG_NEEDS_MANUAL_RELOC */
  172. /* Initialize the list */
  173. INIT_LIST_HEAD(&(devs.list));
  174. #ifdef CONFIG_ARM_DCC_MULTI
  175. drv_arm_dcc_init ();
  176. #endif
  177. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
  178. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  179. #endif
  180. #ifdef CONFIG_LCD
  181. drv_lcd_init ();
  182. #endif
  183. #if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
  184. drv_video_init ();
  185. #endif
  186. #ifdef CONFIG_KEYBOARD
  187. drv_keyboard_init ();
  188. #endif
  189. #ifdef CONFIG_LOGBUFFER
  190. drv_logbuff_init ();
  191. #endif
  192. drv_system_init ();
  193. #ifdef CONFIG_SERIAL_MULTI
  194. serial_stdio_init ();
  195. #endif
  196. #ifdef CONFIG_USB_TTY
  197. drv_usbtty_init ();
  198. #endif
  199. #ifdef CONFIG_NETCONSOLE
  200. drv_nc_init ();
  201. #endif
  202. #ifdef CONFIG_JTAG_CONSOLE
  203. drv_jtag_console_init ();
  204. #endif
  205. return (0);
  206. }