devices.c 4.5 KB

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