devices.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #ifdef CONFIG_LOGBUFFER
  29. #include <logbuff.h>
  30. #endif
  31. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
  32. #include <i2c.h>
  33. #endif
  34. list_t devlist = 0;
  35. device_t *stdio_devices[] = { NULL, NULL, NULL };
  36. char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
  37. #ifdef CFG_DEVICE_NULLDEV
  38. void nulldev_putc(const char c)
  39. {
  40. /* nulldev is empty! */
  41. }
  42. void nulldev_puts(const char *s)
  43. {
  44. /* nulldev is empty! */
  45. }
  46. int nulldev_input(void)
  47. {
  48. /* nulldev is empty! */
  49. return 0;
  50. }
  51. #endif
  52. /**************************************************************************
  53. * SYSTEM DRIVERS
  54. **************************************************************************
  55. */
  56. static void drv_system_init (void)
  57. {
  58. device_t dev;
  59. memset (&dev, 0, sizeof (dev));
  60. strcpy (dev.name, "serial");
  61. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  62. #if CONFIG_SERIAL_SOFTWARE_FIFO
  63. dev.putc = serial_buffered_putc;
  64. dev.puts = serial_buffered_puts;
  65. dev.getc = serial_buffered_getc;
  66. dev.tstc = serial_buffered_tstc;
  67. #else
  68. dev.putc = serial_putc;
  69. dev.puts = serial_puts;
  70. dev.getc = serial_getc;
  71. dev.tstc = serial_tstc;
  72. #endif
  73. device_register (&dev);
  74. #ifdef CFG_DEVICE_NULLDEV
  75. memset (&dev, 0, sizeof (dev));
  76. strcpy (dev.name, "nulldev");
  77. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  78. dev.putc = nulldev_putc;
  79. dev.puts = nulldev_puts;
  80. dev.getc = nulldev_input;
  81. dev.tstc = nulldev_input;
  82. device_register (&dev);
  83. #endif
  84. }
  85. /**************************************************************************
  86. * DEVICES
  87. **************************************************************************
  88. */
  89. int device_register (device_t * dev)
  90. {
  91. ListInsertItem (devlist, dev, LIST_END);
  92. return 0;
  93. }
  94. /* deregister the device "devname".
  95. * returns 0 if success, -1 if device is assigned and 1 if devname not found
  96. */
  97. #ifdef CFG_DEVICE_DEREGISTER
  98. int device_deregister(char *devname)
  99. {
  100. int i,l,dev_index;
  101. device_t *dev = NULL;
  102. char temp_names[3][8];
  103. dev_index=-1;
  104. for (i=1; i<=ListNumItems(devlist); i++) {
  105. dev = ListGetPtrToItem (devlist, i);
  106. if(strcmp(dev->name,devname)==0) {
  107. dev_index=i;
  108. break;
  109. }
  110. }
  111. if(dev_index<0) /* device not found */
  112. return 0;
  113. /* get stdio devices (ListRemoveItem changes the dev list) */
  114. for (l=0 ; l< MAX_FILES; l++) {
  115. if (stdio_devices[l] == dev) {
  116. /* Device is assigned -> report error */
  117. return -1;
  118. }
  119. memcpy (&temp_names[l][0],
  120. stdio_devices[l]->name,
  121. sizeof(stdio_devices[l]->name));
  122. }
  123. ListRemoveItem(devlist,NULL,dev_index);
  124. /* reassign Device list */
  125. for (i=1; i<=ListNumItems(devlist); i++) {
  126. dev = ListGetPtrToItem (devlist, i);
  127. for (l=0 ; l< MAX_FILES; l++) {
  128. if(strcmp(dev->name,temp_names[l])==0) {
  129. stdio_devices[l] = dev;
  130. }
  131. }
  132. }
  133. return 0;
  134. }
  135. #endif /* CFG_DEVICE_DEREGISTER */
  136. int devices_init (void)
  137. {
  138. DECLARE_GLOBAL_DATA_PTR;
  139. int i;
  140. ulong relocation_offset = gd->reloc_off;
  141. /* relocate device name pointers */
  142. for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
  143. stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
  144. relocation_offset);
  145. }
  146. /* Initialize the list */
  147. devlist = ListCreate (sizeof (device_t));
  148. if (devlist == NULL) {
  149. eputs ("Cannot initialize the list of devices!\n");
  150. return -1;
  151. }
  152. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
  153. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
  154. #endif
  155. #ifdef CONFIG_LCD
  156. drv_lcd_init ();
  157. #endif
  158. #ifdef CONFIG_VIDEO
  159. drv_video_init ();
  160. #endif
  161. #ifdef CONFIG_WL_4PPM_KEYBOARD
  162. drv_wlkbd_init ();
  163. #endif
  164. #ifdef CONFIG_LOGBUFFER
  165. drv_logbuff_init ();
  166. #endif
  167. drv_system_init ();
  168. gd-> flags |= GD_FLG_DEVINIT; /* device initialization done */
  169. return (0);
  170. }
  171. int devices_done (void)
  172. {
  173. ListDispose (devlist);
  174. return 0;
  175. }