1
2
3
4
5
6
7
8
9
10
11#include <config.h>
12#include <common.h>
13#include <dm.h>
14#include <errno.h>
15#include <log.h>
16#include <stdarg.h>
17#include <malloc.h>
18#include <stdio_dev.h>
19#include <serial.h>
20#include <splash.h>
21#include <i2c.h>
22#include <asm/global_data.h>
23#include <dm/device-internal.h>
24
25DECLARE_GLOBAL_DATA_PTR;
26
27static struct stdio_dev devs;
28struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
29char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
30
31int stdio_file_to_flags(const int file)
32{
33 switch (file) {
34 case stdin:
35 return DEV_FLAGS_INPUT;
36 case stdout:
37 case stderr:
38 return DEV_FLAGS_OUTPUT;
39 default:
40 return -EINVAL;
41 }
42}
43
44#if CONFIG_IS_ENABLED(SYS_DEVICE_NULLDEV)
45static void nulldev_putc(struct stdio_dev *dev, const char c)
46{
47
48}
49
50static void nulldev_puts(struct stdio_dev *dev, const char *s)
51{
52
53}
54
55static int nulldev_input(struct stdio_dev *dev)
56{
57
58 return 0;
59}
60
61static void nulldev_register(void)
62{
63 struct stdio_dev dev;
64
65 memset(&dev, '\0', sizeof(dev));
66
67 strcpy(dev.name, "nulldev");
68 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
69 dev.putc = nulldev_putc;
70 dev.puts = nulldev_puts;
71 dev.getc = nulldev_input;
72 dev.tstc = nulldev_input;
73
74 stdio_register(&dev);
75}
76#else
77static inline void nulldev_register(void) {}
78#endif
79
80static void stdio_serial_putc(struct stdio_dev *dev, const char c)
81{
82 serial_putc(c);
83}
84
85static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
86{
87 serial_puts(s);
88}
89
90static int stdio_serial_getc(struct stdio_dev *dev)
91{
92 return serial_getc();
93}
94
95static int stdio_serial_tstc(struct stdio_dev *dev)
96{
97 return serial_tstc();
98}
99
100
101
102
103
104
105static void drv_system_init (void)
106{
107 struct stdio_dev dev;
108
109 memset (&dev, 0, sizeof (dev));
110
111 strcpy (dev.name, "serial");
112 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
113 dev.putc = stdio_serial_putc;
114 dev.puts = stdio_serial_puts;
115 dev.getc = stdio_serial_getc;
116 dev.tstc = stdio_serial_tstc;
117 stdio_register (&dev);
118
119 nulldev_register();
120}
121
122
123
124
125
126struct list_head* stdio_get_list(void)
127{
128 return &devs.list;
129}
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150static int stdio_probe_device(const char *name, enum uclass_id id,
151 struct stdio_dev **sdevp)
152{
153 struct stdio_dev *sdev;
154 struct udevice *dev;
155 int seq, ret;
156
157 *sdevp = NULL;
158 seq = trailing_strtoln(name, NULL);
159 if (seq == -1)
160 seq = 0;
161 ret = uclass_get_device_by_seq(id, seq, &dev);
162 if (ret == -ENODEV)
163 ret = uclass_first_device_err(id, &dev);
164 if (ret) {
165 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
166 seq, name);
167 return ret;
168 }
169
170 sdev = list_empty(&devs.list) ? NULL :
171 list_last_entry(&devs.list, struct stdio_dev, list);
172 if (!sdev || strcmp(sdev->name, name)) {
173 debug("Device '%s' did not register with stdio as '%s'\n",
174 dev->name, name);
175 return -ENOENT;
176 }
177 *sdevp = sdev;
178
179 return 0;
180}
181
182struct stdio_dev *stdio_get_by_name(const char *name)
183{
184 struct list_head *pos;
185 struct stdio_dev *sdev;
186
187 if (!name)
188 return NULL;
189
190 list_for_each(pos, &devs.list) {
191 sdev = list_entry(pos, struct stdio_dev, list);
192 if (strcmp(sdev->name, name) == 0)
193 return sdev;
194 }
195 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
196
197
198
199
200
201
202
203
204
205
206
207
208 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
209 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
210 return sdev;
211 }
212
213 return NULL;
214}
215
216struct stdio_dev *stdio_clone(struct stdio_dev *dev)
217{
218 struct stdio_dev *_dev;
219
220 if (!dev)
221 return NULL;
222
223 _dev = calloc(1, sizeof(struct stdio_dev));
224 if (!_dev)
225 return NULL;
226
227 memcpy(_dev, dev, sizeof(struct stdio_dev));
228
229 return _dev;
230}
231
232int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
233{
234 struct stdio_dev *_dev;
235
236 _dev = stdio_clone(dev);
237 if (!_dev)
238 return -ENODEV;
239 list_add_tail(&_dev->list, &devs.list);
240 if (devp)
241 *devp = _dev;
242
243 return 0;
244}
245
246int stdio_register(struct stdio_dev *dev)
247{
248 return stdio_register_dev(dev, NULL);
249}
250
251int stdio_deregister_dev(struct stdio_dev *dev, int force)
252{
253 struct list_head *pos;
254 char temp_names[3][16];
255 int i;
256
257
258 for (i = 0 ; i < MAX_FILES; i++) {
259 if (stdio_devices[i] == dev) {
260 if (force) {
261 strcpy(temp_names[i], "nulldev");
262 continue;
263 }
264
265 return -EBUSY;
266 }
267 memcpy(&temp_names[i][0], stdio_devices[i]->name,
268 sizeof(temp_names[i]));
269 }
270
271 list_del(&dev->list);
272 free(dev);
273
274
275 list_for_each(pos, &devs.list) {
276 dev = list_entry(pos, struct stdio_dev, list);
277 for (i = 0 ; i < MAX_FILES; i++) {
278 if (strcmp(dev->name, temp_names[i]) == 0)
279 stdio_devices[i] = dev;
280 }
281 }
282
283 return 0;
284}
285
286int stdio_init_tables(void)
287{
288#if defined(CONFIG_NEEDS_MANUAL_RELOC)
289
290 ulong relocation_offset = gd->reloc_off;
291 int i;
292
293
294 for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
295 stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
296 relocation_offset);
297 }
298#endif
299
300
301 INIT_LIST_HEAD(&devs.list);
302
303 return 0;
304}
305
306int stdio_add_devices(void)
307{
308 struct udevice *dev;
309 struct uclass *uc;
310 int ret;
311
312 if (IS_ENABLED(CONFIG_DM_KEYBOARD)) {
313
314
315
316
317
318
319
320
321
322
323
324 ret = uclass_get(UCLASS_KEYBOARD, &uc);
325 if (ret)
326 return ret;
327
328
329
330
331
332 uclass_foreach_dev(dev, uc) {
333 ret = device_probe(dev);
334 if (ret)
335 printf("Failed to probe keyboard '%s'\n",
336 dev->name);
337 }
338 }
339#ifdef CONFIG_SYS_I2C
340 i2c_init_all();
341#endif
342 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
343
344
345
346
347
348
349
350
351
352 struct udevice *vdev;
353 int ret;
354
355 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_IS_IN_ENV)) {
356 for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
357 vdev;
358 ret = uclass_next_device(&vdev))
359 ;
360 if (ret)
361 printf("%s: Video device failed (ret=%d)\n",
362 __func__, ret);
363 }
364 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) &&
365 IS_ENABLED(CONFIG_CMD_BMP))
366 splash_display();
367 } else {
368 if (IS_ENABLED(CONFIG_LCD))
369 drv_lcd_init();
370 if (IS_ENABLED(CONFIG_VIDEO) ||
371 IS_ENABLED(CONFIG_CFB_CONSOLE) ||
372 IS_ENABLED(CONFIG_VIDEO_VCXK))
373 drv_video_init();
374 }
375
376#if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
377 drv_keyboard_init();
378#endif
379 drv_system_init();
380 serial_stdio_init();
381#ifdef CONFIG_USB_TTY
382 drv_usbtty_init();
383#endif
384 if (IS_ENABLED(CONFIG_NETCONSOLE))
385 drv_nc_init();
386#ifdef CONFIG_JTAG_CONSOLE
387 drv_jtag_console_init();
388#endif
389 if (IS_ENABLED(CONFIG_CBMEM_CONSOLE))
390 cbmemc_init();
391
392 return 0;
393}
394
395int stdio_init(void)
396{
397 stdio_init_tables();
398 stdio_add_devices();
399
400 return 0;
401}
402