]> Git Repo - J-u-boot.git/blame - common/stdio.c
net: dsa: remove NULL check for priv and platform data
[J-u-boot.git] / common / stdio.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
91d3256c 2/*
3f4978c7
HS
3 * Copyright (C) 2009 Sergey Kubushyn <[email protected]>
4 *
5 * Changes for multibus/multiadapter I2C support.
6 *
91d3256c
WD
7 * (C) Copyright 2000
8 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), [email protected]
91d3256c
WD
9 */
10
11#include <config.h>
12#include <common.h>
b206cd73 13#include <dm.h>
d97143a6 14#include <errno.h>
f7ae49fc 15#include <log.h>
91d3256c
WD
16#include <stdarg.h>
17#include <malloc.h>
52cb4d4f 18#include <stdio_dev.h>
281e00a3 19#include <serial.h>
5eb83c0a 20#include <splash.h>
91d3256c 21#include <i2c.h>
401d1c4f 22#include <asm/global_data.h>
b206cd73
SG
23#include <dm/device-internal.h>
24
d87080b7
WD
25DECLARE_GLOBAL_DATA_PTR;
26
52cb4d4f
JCPV
27static struct stdio_dev devs;
28struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
91d3256c
WD
29char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
30
d9b0ac90
AS
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
99cb2b99 44#if CONFIG_IS_ENABLED(SYS_DEVICE_NULLDEV)
654f8d0f 45static void nulldev_putc(struct stdio_dev *dev, const char c)
91d3256c 46{
c1de7a6d 47 /* nulldev is empty! */
91d3256c
WD
48}
49
654f8d0f 50static void nulldev_puts(struct stdio_dev *dev, const char *s)
91d3256c 51{
c1de7a6d 52 /* nulldev is empty! */
91d3256c
WD
53}
54
654f8d0f 55static int nulldev_input(struct stdio_dev *dev)
91d3256c 56{
c1de7a6d
JCPV
57 /* nulldev is empty! */
58 return 0;
91d3256c 59}
91d3256c 60
99cb2b99
AS
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 /* SYS_DEVICE_NULLDEV */
79
654f8d0f 80static void stdio_serial_putc(struct stdio_dev *dev, const char c)
709ea543
SG
81{
82 serial_putc(c);
83}
84
654f8d0f 85static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
709ea543
SG
86{
87 serial_puts(s);
88}
89
654f8d0f 90static int stdio_serial_getc(struct stdio_dev *dev)
709ea543
SG
91{
92 return serial_getc();
93}
94
654f8d0f 95static int stdio_serial_tstc(struct stdio_dev *dev)
709ea543
SG
96{
97 return serial_tstc();
98}
99
91d3256c
WD
100/**************************************************************************
101 * SYSTEM DRIVERS
102 **************************************************************************
103 */
104
105static void drv_system_init (void)
106{
52cb4d4f 107 struct stdio_dev dev;
91d3256c
WD
108
109 memset (&dev, 0, sizeof (dev));
110
111 strcpy (dev.name, "serial");
1caf934a 112 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
709ea543
SG
113 dev.putc = stdio_serial_putc;
114 dev.puts = stdio_serial_puts;
115 dev.getc = stdio_serial_getc;
116 dev.tstc = stdio_serial_tstc;
52cb4d4f 117 stdio_register (&dev);
91d3256c 118
99cb2b99 119 nulldev_register();
91d3256c
WD
120}
121
122/**************************************************************************
123 * DEVICES
124 **************************************************************************
125 */
52cb4d4f 126struct list_head* stdio_get_list(void)
c1de7a6d 127{
18c587d0 128 return &devs.list;
c1de7a6d
JCPV
129}
130
d8441ea2
SG
131/**
132 * stdio_probe_device() - Find a device which provides the given stdio device
133 *
134 * This looks for a device of the given uclass which provides a particular
135 * stdio device. It is currently really only useful for UCLASS_VIDEO.
136 *
137 * Ultimately we want to be able to probe a device by its stdio name. At
138 * present devices register in their probe function (for video devices this
139 * is done in vidconsole_post_probe()) and we don't know what name they will
140 * use until they do so.
141 * TODO([email protected]): We should be able to determine the name before
142 * probing, and probe the required device.
143 *
144 * @name: stdio device name (e.g. "vidconsole")
145 * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO)
146 * @sdevp: Returns stdout device, if found, else NULL
147 * @return 0 if found, -ENOENT if no device found with that name, other -ve
148 * on other error
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)
d844efec
SG
160 seq = 0;
161 ret = uclass_get_device_by_seq(id, seq, &dev);
162 if (ret == -ENODEV)
d8441ea2 163 ret = uclass_first_device_err(id, &dev);
d8441ea2
SG
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 /* The device should be be the last one registered */
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}
d8441ea2 181
ab29a34a 182struct stdio_dev *stdio_get_by_name(const char *name)
c1de7a6d
JCPV
183{
184 struct list_head *pos;
d8441ea2 185 struct stdio_dev *sdev;
c1de7a6d 186
ab29a34a 187 if (!name)
c1de7a6d
JCPV
188 return NULL;
189
18c587d0 190 list_for_each(pos, &devs.list) {
d8441ea2
SG
191 sdev = list_entry(pos, struct stdio_dev, list);
192 if (strcmp(sdev->name, name) == 0)
193 return sdev;
c1de7a6d 194 }
5d4b6b17
SG
195 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
196 /*
197 * We did not find a suitable stdio device. If there is a video
198 * driver with a name starting with 'vidconsole', we can try
199 * probing that in the hope that it will produce the required
200 * stdio device.
201 *
202 * This function is sometimes called with the entire value of
203 * 'stdout', which may include a list of devices separate by
204 * commas. Obviously this is not going to work, so we ignore
205 * that case. The call path in that case is
3232487d 206 * console_init_r() -> console_search_dev() -> stdio_get_by_name()
5d4b6b17
SG
207 */
208 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
209 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
210 return sdev;
211 }
c1de7a6d
JCPV
212
213 return NULL;
214}
215
4225f2f5 216struct stdio_dev *stdio_clone(struct stdio_dev *dev)
628ffd73 217{
52cb4d4f 218 struct stdio_dev *_dev;
628ffd73 219
4225f2f5 220 if (!dev)
628ffd73
JCPV
221 return NULL;
222
52cb4d4f 223 _dev = calloc(1, sizeof(struct stdio_dev));
4225f2f5 224 if (!_dev)
628ffd73
JCPV
225 return NULL;
226
52cb4d4f 227 memcpy(_dev, dev, sizeof(struct stdio_dev));
628ffd73
JCPV
228
229 return _dev;
230}
91d3256c 231
d97143a6 232int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
91d3256c 233{
52cb4d4f 234 struct stdio_dev *_dev;
628ffd73 235
52cb4d4f 236 _dev = stdio_clone(dev);
4225f2f5 237 if (!_dev)
d97143a6 238 return -ENODEV;
18c587d0 239 list_add_tail(&_dev->list, &devs.list);
d97143a6
SG
240 if (devp)
241 *devp = _dev;
242
91d3256c
WD
243 return 0;
244}
245
d97143a6
SG
246int stdio_register(struct stdio_dev *dev)
247{
248 return stdio_register_dev(dev, NULL);
249}
250
32d01926 251int stdio_deregister_dev(struct stdio_dev *dev, int force)
91d3256c 252{
c1de7a6d 253 struct list_head *pos;
03bf22f5 254 char temp_names[3][16];
4225f2f5 255 int i;
91d3256c 256
91d3256c 257 /* get stdio devices (ListRemoveItem changes the dev list) */
4225f2f5
SG
258 for (i = 0 ; i < MAX_FILES; i++) {
259 if (stdio_devices[i] == dev) {
32d01926 260 if (force) {
4225f2f5 261 strcpy(temp_names[i], "nulldev");
32d01926
HG
262 continue;
263 }
91d3256c 264 /* Device is assigned -> report error */
4225f2f5 265 return -EBUSY;
91d3256c 266 }
4225f2f5
SG
267 memcpy(&temp_names[i][0], stdio_devices[i]->name,
268 sizeof(temp_names[i]));
91d3256c 269 }
c1de7a6d 270
18c587d0 271 list_del(&dev->list);
88274b6c 272 free(dev);
c1de7a6d 273
4225f2f5 274 /* reassign device list */
18c587d0 275 list_for_each(pos, &devs.list) {
52cb4d4f 276 dev = list_entry(pos, struct stdio_dev, list);
4225f2f5
SG
277 for (i = 0 ; i < MAX_FILES; i++) {
278 if (strcmp(dev->name, temp_names[i]) == 0)
279 stdio_devices[i] = dev;
91d3256c
WD
280 }
281 }
4225f2f5 282
91d3256c
WD
283 return 0;
284}
d97143a6 285
9fb02491 286int stdio_init_tables(void)
91d3256c 287{
2e5167cc 288#if defined(CONFIG_NEEDS_MANUAL_RELOC)
521af04d 289 /* already relocated for current ARM implementation */
91d3256c 290 ulong relocation_offset = gd->reloc_off;
3595ac49 291 int i;
91d3256c
WD
292
293 /* relocate device name pointers */
294 for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
295 stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
296 relocation_offset);
297 }
2e5167cc 298#endif /* CONFIG_NEEDS_MANUAL_RELOC */
91d3256c
WD
299
300 /* Initialize the list */
18c587d0 301 INIT_LIST_HEAD(&devs.list);
91d3256c 302
9fb02491
SG
303 return 0;
304}
305
306int stdio_add_devices(void)
307{
b206cd73
SG
308 struct udevice *dev;
309 struct uclass *uc;
310 int ret;
311
5d4b6b17
SG
312 if (IS_ENABLED(CONFIG_DM_KEYBOARD)) {
313 /*
314 * For now we probe all the devices here. At some point this
315 * should be done only when the devices are required - e.g. we
316 * have a list of input devices to start up in the stdin
317 * environment variable. That work probably makes more sense
318 * when stdio itself is converted to driver model.
319 *
320 * TODO([email protected]): Convert changing
321 * uclass_first_device() etc. to return the device even on
322 * error. Then we could use that here.
323 */
324 ret = uclass_get(UCLASS_KEYBOARD, &uc);
b206cd73 325 if (ret)
5d4b6b17
SG
326 return ret;
327
328 /*
329 * Don't report errors to the caller - assume that they are
330 * non-fatal
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 }
b206cd73 338 }
3f4978c7 339#ifdef CONFIG_SYS_I2C
3f4978c7 340 i2c_init_all();
3f4978c7 341#endif
5d4b6b17
SG
342 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
343 /*
344 * If the console setting is not in environment variables then
345 * console_init_r() will not be calling iomux_doenv() (which
3232487d 346 * calls console_search_dev()). So we will not dynamically add
5d4b6b17
SG
347 * devices by calling stdio_probe_device().
348 *
349 * So just probe all video devices now so that whichever one is
350 * required will be available.
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();
8c9940d5
AG
370 if (IS_ENABLED(CONFIG_VIDEO) ||
371 IS_ENABLED(CONFIG_CFB_CONSOLE) ||
372 IS_ENABLED(CONFIG_VIDEO_VCXK))
5d4b6b17
SG
373 drv_video_init();
374 }
375
b206cd73 376#if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
5d4b6b17 377 drv_keyboard_init();
91d3256c 378#endif
5d4b6b17
SG
379 drv_system_init();
380 serial_stdio_init();
232c150a 381#ifdef CONFIG_USB_TTY
5d4b6b17 382 drv_usbtty_init();
68ceb29e 383#endif
5d4b6b17
SG
384 if (IS_ENABLED(CONFIG_NETCONSOLE))
385 drv_nc_init();
36ea8e9a 386#ifdef CONFIG_JTAG_CONSOLE
5d4b6b17 387 drv_jtag_console_init();
98ab435f 388#endif
5d4b6b17
SG
389 if (IS_ENABLED(CONFIG_CBMEM_CONSOLE))
390 cbmemc_init();
9fb02491
SG
391
392 return 0;
393}
394
395int stdio_init(void)
396{
397 stdio_init_tables();
398 stdio_add_devices();
399
400 return 0;
91d3256c 401}
This page took 0.592031 seconds and 4 git commands to generate.