Blame view
common/stdio.c
9.41 KB
83d290c56 SPDX: Convert all... |
1 |
// SPDX-License-Identifier: GPL-2.0+ |
91d3256c6 Initial revision |
2 |
/* |
3f4978c71 i2c: common chang... |
3 4 5 6 |
* Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net> * * Changes for multibus/multiadapter I2C support. * |
91d3256c6 Initial revision |
7 8 |
* (C) Copyright 2000 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it |
91d3256c6 Initial revision |
9 10 11 12 |
*/ #include <config.h> #include <common.h> |
b206cd737 dm: stdio: Plumb ... |
13 |
#include <dm.h> |
d97143a67 stdio: Provide fu... |
14 |
#include <errno.h> |
91d3256c6 Initial revision |
15 16 |
#include <stdarg.h> #include <malloc.h> |
52cb4d4fb stdio/device: rew... |
17 |
#include <stdio_dev.h> |
281e00a3b * Code cleanup |
18 |
#include <serial.h> |
ea818dbbc i2c, soft-i2c: sw... |
19 |
|
69153988a i2c: Finish dropp... |
20 |
#if defined(CONFIG_SYS_I2C) |
91d3256c6 Initial revision |
21 |
#include <i2c.h> |
7f6c2cbc2 * Vince Husovsky,... |
22 |
#endif |
91d3256c6 Initial revision |
23 |
|
b206cd737 dm: stdio: Plumb ... |
24 |
#include <dm/device-internal.h> |
d87080b72 GCC-4.x fixes: cl... |
25 |
DECLARE_GLOBAL_DATA_PTR; |
52cb4d4fb stdio/device: rew... |
26 27 |
static struct stdio_dev devs; struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL }; |
91d3256c6 Initial revision |
28 |
char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" }; |
6d0f6bcf3 rename CFG_ macro... |
29 30 |
#if defined(CONFIG_SPLASH_SCREEN) && !defined(CONFIG_SYS_DEVICE_NULLDEV) #define CONFIG_SYS_DEVICE_NULLDEV 1 |
d791b1dc3 * Make sure Block... |
31 |
#endif |
869588dec Convert CONFIG_SY... |
32 |
#if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) |
32d019265 stdio: Add force ... |
33 34 |
#define CONFIG_SYS_DEVICE_NULLDEV 1 #endif |
d791b1dc3 * Make sure Block... |
35 |
|
6d0f6bcf3 rename CFG_ macro... |
36 |
#ifdef CONFIG_SYS_DEVICE_NULLDEV |
654f8d0f3 serial: make loca... |
37 |
static void nulldev_putc(struct stdio_dev *dev, const char c) |
91d3256c6 Initial revision |
38 |
{ |
c1de7a6da devices: merge to... |
39 |
/* nulldev is empty! */ |
91d3256c6 Initial revision |
40 |
} |
654f8d0f3 serial: make loca... |
41 |
static void nulldev_puts(struct stdio_dev *dev, const char *s) |
91d3256c6 Initial revision |
42 |
{ |
c1de7a6da devices: merge to... |
43 |
/* nulldev is empty! */ |
91d3256c6 Initial revision |
44 |
} |
654f8d0f3 serial: make loca... |
45 |
static int nulldev_input(struct stdio_dev *dev) |
91d3256c6 Initial revision |
46 |
{ |
c1de7a6da devices: merge to... |
47 48 |
/* nulldev is empty! */ return 0; |
91d3256c6 Initial revision |
49 50 |
} #endif |
654f8d0f3 serial: make loca... |
51 |
static void stdio_serial_putc(struct stdio_dev *dev, const char c) |
709ea543b stdio: Pass devic... |
52 53 54 |
{ serial_putc(c); } |
654f8d0f3 serial: make loca... |
55 |
static void stdio_serial_puts(struct stdio_dev *dev, const char *s) |
709ea543b stdio: Pass devic... |
56 57 58 |
{ serial_puts(s); } |
654f8d0f3 serial: make loca... |
59 |
static int stdio_serial_getc(struct stdio_dev *dev) |
709ea543b stdio: Pass devic... |
60 61 62 |
{ return serial_getc(); } |
654f8d0f3 serial: make loca... |
63 |
static int stdio_serial_tstc(struct stdio_dev *dev) |
709ea543b stdio: Pass devic... |
64 65 66 |
{ return serial_tstc(); } |
91d3256c6 Initial revision |
67 68 69 70 71 72 73 |
/************************************************************************** * SYSTEM DRIVERS ************************************************************************** */ static void drv_system_init (void) { |
52cb4d4fb stdio/device: rew... |
74 |
struct stdio_dev dev; |
91d3256c6 Initial revision |
75 76 77 78 |
memset (&dev, 0, sizeof (dev)); strcpy (dev.name, "serial"); |
1caf934a0 video: Drop DEV_F... |
79 |
dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT; |
709ea543b stdio: Pass devic... |
80 81 82 83 |
dev.putc = stdio_serial_putc; dev.puts = stdio_serial_puts; dev.getc = stdio_serial_getc; dev.tstc = stdio_serial_tstc; |
52cb4d4fb stdio/device: rew... |
84 |
stdio_register (&dev); |
91d3256c6 Initial revision |
85 |
|
6d0f6bcf3 rename CFG_ macro... |
86 |
#ifdef CONFIG_SYS_DEVICE_NULLDEV |
91d3256c6 Initial revision |
87 88 89 |
memset (&dev, 0, sizeof (dev)); strcpy (dev.name, "nulldev"); |
1caf934a0 video: Drop DEV_F... |
90 |
dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT; |
91d3256c6 Initial revision |
91 92 93 94 |
dev.putc = nulldev_putc; dev.puts = nulldev_puts; dev.getc = nulldev_input; dev.tstc = nulldev_input; |
52cb4d4fb stdio/device: rew... |
95 |
stdio_register (&dev); |
91d3256c6 Initial revision |
96 97 98 99 100 101 102 |
#endif } /************************************************************************** * DEVICES ************************************************************************** */ |
52cb4d4fb stdio/device: rew... |
103 |
struct list_head* stdio_get_list(void) |
c1de7a6da devices: merge to... |
104 105 106 |
{ return &(devs.list); } |
d8441ea27 dm: stdio: Allow ... |
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
#ifdef CONFIG_DM_VIDEO /** * stdio_probe_device() - Find a device which provides the given stdio device * * This looks for a device of the given uclass which provides a particular * stdio device. It is currently really only useful for UCLASS_VIDEO. * * Ultimately we want to be able to probe a device by its stdio name. At * present devices register in their probe function (for video devices this * is done in vidconsole_post_probe()) and we don't know what name they will * use until they do so. * TODO(sjg@chromium.org): We should be able to determine the name before * probing, and probe the required device. * * @name: stdio device name (e.g. "vidconsole") * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO) * @sdevp: Returns stdout device, if found, else NULL * @return 0 if found, -ENOENT if no device found with that name, other -ve * on other error */ static int stdio_probe_device(const char *name, enum uclass_id id, struct stdio_dev **sdevp) { struct stdio_dev *sdev; struct udevice *dev; int seq, ret; *sdevp = NULL; seq = trailing_strtoln(name, NULL); if (seq == -1) |
d844efec4 stdio: Correct nu... |
137 138 139 |
seq = 0; ret = uclass_get_device_by_seq(id, seq, &dev); if (ret == -ENODEV) |
d8441ea27 dm: stdio: Allow ... |
140 |
ret = uclass_first_device_err(id, &dev); |
d8441ea27 dm: stdio: Allow ... |
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
if (ret) { debug("No %s device for seq %d (%s) ", uclass_get_name(id), seq, name); return ret; } /* The device should be be the last one registered */ sdev = list_empty(&devs.list) ? NULL : list_last_entry(&devs.list, struct stdio_dev, list); if (!sdev || strcmp(sdev->name, name)) { debug("Device '%s' did not register with stdio as '%s' ", dev->name, name); return -ENOENT; } *sdevp = sdev; return 0; } #endif |
ab29a34a5 stdio: Correct co... |
161 |
struct stdio_dev *stdio_get_by_name(const char *name) |
c1de7a6da devices: merge to... |
162 163 |
{ struct list_head *pos; |
d8441ea27 dm: stdio: Allow ... |
164 |
struct stdio_dev *sdev; |
c1de7a6da devices: merge to... |
165 |
|
ab29a34a5 stdio: Correct co... |
166 |
if (!name) |
c1de7a6da devices: merge to... |
167 168 169 |
return NULL; list_for_each(pos, &(devs.list)) { |
d8441ea27 dm: stdio: Allow ... |
170 171 172 |
sdev = list_entry(pos, struct stdio_dev, list); if (strcmp(sdev->name, name) == 0) return sdev; |
c1de7a6da devices: merge to... |
173 |
} |
d8441ea27 dm: stdio: Allow ... |
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
#ifdef CONFIG_DM_VIDEO /* * We did not find a suitable stdio device. If there is a video * driver with a name starting with 'vidconsole', we can try probing * that in the hope that it will produce the required stdio device. * * This function is sometimes called with the entire value of * 'stdout', which may include a list of devices separate by commas. * Obviously this is not going to work, so we ignore that case. The * call path in that case is console_init_r() -> search_device() -> * stdio_get_by_name(). */ if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') && !stdio_probe_device(name, UCLASS_VIDEO, &sdev)) return sdev; #endif |
c1de7a6da devices: merge to... |
190 191 192 |
return NULL; } |
52cb4d4fb stdio/device: rew... |
193 |
struct stdio_dev* stdio_clone(struct stdio_dev *dev) |
628ffd73b device: make devi... |
194 |
{ |
52cb4d4fb stdio/device: rew... |
195 |
struct stdio_dev *_dev; |
628ffd73b device: make devi... |
196 197 198 |
if(!dev) return NULL; |
52cb4d4fb stdio/device: rew... |
199 |
_dev = calloc(1, sizeof(struct stdio_dev)); |
628ffd73b device: make devi... |
200 201 202 |
if(!_dev) return NULL; |
52cb4d4fb stdio/device: rew... |
203 |
memcpy(_dev, dev, sizeof(struct stdio_dev)); |
628ffd73b device: make devi... |
204 205 206 |
return _dev; } |
91d3256c6 Initial revision |
207 |
|
d97143a67 stdio: Provide fu... |
208 |
int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp) |
91d3256c6 Initial revision |
209 |
{ |
52cb4d4fb stdio/device: rew... |
210 |
struct stdio_dev *_dev; |
628ffd73b device: make devi... |
211 |
|
52cb4d4fb stdio/device: rew... |
212 |
_dev = stdio_clone(dev); |
628ffd73b device: make devi... |
213 |
if(!_dev) |
d97143a67 stdio: Provide fu... |
214 |
return -ENODEV; |
3e3c026ed devices: Use list... |
215 |
list_add_tail(&(_dev->list), &(devs.list)); |
d97143a67 stdio: Provide fu... |
216 217 |
if (devp) *devp = _dev; |
91d3256c6 Initial revision |
218 219 |
return 0; } |
d97143a67 stdio: Provide fu... |
220 221 222 223 |
int stdio_register(struct stdio_dev *dev) { return stdio_register_dev(dev, NULL); } |
91d3256c6 Initial revision |
224 225 226 |
/* deregister the device "devname". * returns 0 if success, -1 if device is assigned and 1 if devname not found */ |
869588dec Convert CONFIG_SY... |
227 |
#if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) |
32d019265 stdio: Add force ... |
228 |
int stdio_deregister_dev(struct stdio_dev *dev, int force) |
91d3256c6 Initial revision |
229 |
{ |
c1de7a6da devices: merge to... |
230 231 |
int l; struct list_head *pos; |
03bf22f55 stdio: Fix a poss... |
232 |
char temp_names[3][16]; |
91d3256c6 Initial revision |
233 |
|
91d3256c6 Initial revision |
234 235 236 |
/* get stdio devices (ListRemoveItem changes the dev list) */ for (l=0 ; l< MAX_FILES; l++) { if (stdio_devices[l] == dev) { |
32d019265 stdio: Add force ... |
237 238 239 240 |
if (force) { strcpy(temp_names[l], "nulldev"); continue; } |
91d3256c6 Initial revision |
241 242 243 244 245 |
/* Device is assigned -> report error */ return -1; } memcpy (&temp_names[l][0], stdio_devices[l]->name, |
03bf22f55 stdio: Fix a poss... |
246 |
sizeof(temp_names[l])); |
91d3256c6 Initial revision |
247 |
} |
c1de7a6da devices: merge to... |
248 249 |
list_del(&(dev->list)); |
88274b6c4 stdio: Fix memlea... |
250 |
free(dev); |
c1de7a6da devices: merge to... |
251 |
|
91d3256c6 Initial revision |
252 |
/* reassign Device list */ |
c1de7a6da devices: merge to... |
253 |
list_for_each(pos, &(devs.list)) { |
52cb4d4fb stdio/device: rew... |
254 |
dev = list_entry(pos, struct stdio_dev, list); |
91d3256c6 Initial revision |
255 |
for (l=0 ; l< MAX_FILES; l++) { |
c1de7a6da devices: merge to... |
256 |
if(strcmp(dev->name, temp_names[l]) == 0) |
91d3256c6 Initial revision |
257 |
stdio_devices[l] = dev; |
91d3256c6 Initial revision |
258 259 260 261 |
} } return 0; } |
d97143a67 stdio: Provide fu... |
262 |
|
32d019265 stdio: Add force ... |
263 |
int stdio_deregister(const char *devname, int force) |
d97143a67 stdio: Provide fu... |
264 265 266 267 268 269 270 |
{ struct stdio_dev *dev; dev = stdio_get_by_name(devname); if (!dev) /* device not found */ return -ENODEV; |
32d019265 stdio: Add force ... |
271 |
return stdio_deregister_dev(dev, force); |
d97143a67 stdio: Provide fu... |
272 |
} |
869588dec Convert CONFIG_SY... |
273 |
#endif /* CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) */ |
91d3256c6 Initial revision |
274 |
|
9fb02491f dm: Make driver m... |
275 |
int stdio_init_tables(void) |
91d3256c6 Initial revision |
276 |
{ |
2e5167cca Replace CONFIG_RE... |
277 |
#if defined(CONFIG_NEEDS_MANUAL_RELOC) |
521af04d8 Conditionally per... |
278 |
/* already relocated for current ARM implementation */ |
91d3256c6 Initial revision |
279 |
ulong relocation_offset = gd->reloc_off; |
3595ac497 * Patch by Rune T... |
280 |
int i; |
91d3256c6 Initial revision |
281 282 283 284 285 286 |
/* relocate device name pointers */ for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) { stdio_names[i] = (char *) (((ulong) stdio_names[i]) + relocation_offset); } |
2e5167cca Replace CONFIG_RE... |
287 |
#endif /* CONFIG_NEEDS_MANUAL_RELOC */ |
91d3256c6 Initial revision |
288 289 |
/* Initialize the list */ |
c1de7a6da devices: merge to... |
290 |
INIT_LIST_HEAD(&(devs.list)); |
91d3256c6 Initial revision |
291 |
|
9fb02491f dm: Make driver m... |
292 293 294 295 296 |
return 0; } int stdio_add_devices(void) { |
b206cd737 dm: stdio: Plumb ... |
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
#ifdef CONFIG_DM_KEYBOARD struct udevice *dev; struct uclass *uc; int ret; /* * For now we probe all the devices here. At some point this should be * done only when the devices are required - e.g. we have a list of * input devices to start up in the stdin environment variable. That * work probably makes more sense when stdio itself is converted to * driver model. * * TODO(sjg@chromium.org): Convert changing uclass_first_device() etc. * to return the device even on error. Then we could use that here. */ ret = uclass_get(UCLASS_KEYBOARD, &uc); if (ret) return ret; /* Don't report errors to the caller - assume that they are non-fatal */ uclass_foreach_dev(dev, uc) { ret = device_probe(dev); if (ret) printf("Failed to probe keyboard '%s' ", dev->name); } #endif |
3f4978c71 i2c: common chang... |
324 |
#ifdef CONFIG_SYS_I2C |
3f4978c71 i2c: common chang... |
325 |
i2c_init_all(); |
3f4978c71 i2c: common chang... |
326 |
#else |
3f4978c71 i2c: common chang... |
327 |
#endif |
e3b81c1c0 dm: stdio: video:... |
328 |
#ifdef CONFIG_DM_VIDEO |
d8441ea27 dm: stdio: Allow ... |
329 330 331 332 333 334 335 336 337 338 |
/* * If the console setting is not in environment variables then * console_init_r() will not be calling iomux_doenv() (which calls * search_device()). So we will not dynamically add devices by * calling stdio_probe_device(). * * So just probe all video devices now so that whichever one is * required will be available. */ #ifndef CONFIG_SYS_CONSOLE_IS_IN_ENV |
e3b81c1c0 dm: stdio: video:... |
339 |
struct udevice *vdev; |
35a1f0dfa stdio: Correct a ... |
340 341 342 |
# ifndef CONFIG_DM_KEYBOARD int ret; # endif |
e3b81c1c0 dm: stdio: video:... |
343 344 345 346 347 348 349 350 |
for (ret = uclass_first_device(UCLASS_VIDEO, &vdev); vdev; ret = uclass_next_device(&vdev)) ; if (ret) printf("%s: Video device failed (ret=%d) ", __func__, ret); |
d8441ea27 dm: stdio: Allow ... |
351 |
#endif /* !CONFIG_SYS_CONSOLE_IS_IN_ENV */ |
e3b81c1c0 dm: stdio: video:... |
352 353 |
#else # if defined(CONFIG_LCD) |
91d3256c6 Initial revision |
354 |
drv_lcd_init (); |
e3b81c1c0 dm: stdio: video:... |
355 356 |
# endif # if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE) |
91d3256c6 Initial revision |
357 |
drv_video_init (); |
e3b81c1c0 dm: stdio: video:... |
358 359 |
# endif #endif /* CONFIG_DM_VIDEO */ |
b206cd737 dm: stdio: Plumb ... |
360 |
#if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD) |
682011ff6 * Patches by Udi ... |
361 |
drv_keyboard_init (); |
91d3256c6 Initial revision |
362 363 |
#endif drv_system_init (); |
52cb4d4fb stdio/device: rew... |
364 |
serial_stdio_init (); |
232c150a2 Add support for S... |
365 366 367 |
#ifdef CONFIG_USB_TTY drv_usbtty_init (); #endif |
68ceb29e7 Add support for c... |
368 369 370 |
#ifdef CONFIG_NETCONSOLE drv_nc_init (); #endif |
36ea8e9ad Blackfin: support... |
371 372 373 |
#ifdef CONFIG_JTAG_CONSOLE drv_jtag_console_init (); #endif |
98ab435f7 x86: Add CBMEM co... |
374 375 376 |
#ifdef CONFIG_CBMEM_CONSOLE cbmemc_init(); #endif |
9fb02491f dm: Make driver m... |
377 378 379 380 381 382 383 384 385 386 |
return 0; } int stdio_init(void) { stdio_init_tables(); stdio_add_devices(); return 0; |
91d3256c6 Initial revision |
387 |
} |