]> Git Repo - J-u-boot.git/blame - common/stdio.c
usb: storage: Use the correct CBW lengths
[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
78b52431
T
90#ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
91static void stdio_serial_flush(struct stdio_dev *dev)
92{
93 serial_flush();
94}
95#endif
96
654f8d0f 97static int stdio_serial_getc(struct stdio_dev *dev)
709ea543
SG
98{
99 return serial_getc();
100}
101
654f8d0f 102static int stdio_serial_tstc(struct stdio_dev *dev)
709ea543
SG
103{
104 return serial_tstc();
105}
106
91d3256c
WD
107/**************************************************************************
108 * SYSTEM DRIVERS
109 **************************************************************************
110 */
111
112static void drv_system_init (void)
113{
52cb4d4f 114 struct stdio_dev dev;
91d3256c
WD
115
116 memset (&dev, 0, sizeof (dev));
117
118 strcpy (dev.name, "serial");
1caf934a 119 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
709ea543
SG
120 dev.putc = stdio_serial_putc;
121 dev.puts = stdio_serial_puts;
78b52431 122 STDIO_DEV_ASSIGN_FLUSH(&dev, stdio_serial_flush);
709ea543
SG
123 dev.getc = stdio_serial_getc;
124 dev.tstc = stdio_serial_tstc;
52cb4d4f 125 stdio_register (&dev);
91d3256c 126
99cb2b99 127 nulldev_register();
91d3256c
WD
128}
129
130/**************************************************************************
131 * DEVICES
132 **************************************************************************
133 */
52cb4d4f 134struct list_head* stdio_get_list(void)
c1de7a6d 135{
18c587d0 136 return &devs.list;
c1de7a6d
JCPV
137}
138
d8441ea2
SG
139/**
140 * stdio_probe_device() - Find a device which provides the given stdio device
141 *
142 * This looks for a device of the given uclass which provides a particular
143 * stdio device. It is currently really only useful for UCLASS_VIDEO.
144 *
145 * Ultimately we want to be able to probe a device by its stdio name. At
146 * present devices register in their probe function (for video devices this
147 * is done in vidconsole_post_probe()) and we don't know what name they will
148 * use until they do so.
149 * TODO([email protected]): We should be able to determine the name before
150 * probing, and probe the required device.
151 *
152 * @name: stdio device name (e.g. "vidconsole")
153 * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO)
154 * @sdevp: Returns stdout device, if found, else NULL
185f812c 155 * Return: 0 if found, -ENOENT if no device found with that name, other -ve
d8441ea2
SG
156 * on other error
157 */
158static int stdio_probe_device(const char *name, enum uclass_id id,
159 struct stdio_dev **sdevp)
160{
161 struct stdio_dev *sdev;
162 struct udevice *dev;
163 int seq, ret;
164
165 *sdevp = NULL;
166 seq = trailing_strtoln(name, NULL);
167 if (seq == -1)
d844efec
SG
168 seq = 0;
169 ret = uclass_get_device_by_seq(id, seq, &dev);
170 if (ret == -ENODEV)
d8441ea2 171 ret = uclass_first_device_err(id, &dev);
d8441ea2
SG
172 if (ret) {
173 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
174 seq, name);
175 return ret;
176 }
177 /* The device should be be the last one registered */
178 sdev = list_empty(&devs.list) ? NULL :
179 list_last_entry(&devs.list, struct stdio_dev, list);
180 if (!sdev || strcmp(sdev->name, name)) {
181 debug("Device '%s' did not register with stdio as '%s'\n",
182 dev->name, name);
183 return -ENOENT;
184 }
185 *sdevp = sdev;
186
187 return 0;
188}
d8441ea2 189
ab29a34a 190struct stdio_dev *stdio_get_by_name(const char *name)
c1de7a6d
JCPV
191{
192 struct list_head *pos;
d8441ea2 193 struct stdio_dev *sdev;
c1de7a6d 194
ab29a34a 195 if (!name)
c1de7a6d
JCPV
196 return NULL;
197
18c587d0 198 list_for_each(pos, &devs.list) {
d8441ea2
SG
199 sdev = list_entry(pos, struct stdio_dev, list);
200 if (strcmp(sdev->name, name) == 0)
201 return sdev;
c1de7a6d 202 }
b86986c7 203 if (IS_ENABLED(CONFIG_VIDEO)) {
5d4b6b17
SG
204 /*
205 * We did not find a suitable stdio device. If there is a video
206 * driver with a name starting with 'vidconsole', we can try
207 * probing that in the hope that it will produce the required
208 * stdio device.
209 *
210 * This function is sometimes called with the entire value of
211 * 'stdout', which may include a list of devices separate by
212 * commas. Obviously this is not going to work, so we ignore
213 * that case. The call path in that case is
3232487d 214 * console_init_r() -> console_search_dev() -> stdio_get_by_name()
5d4b6b17
SG
215 */
216 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
217 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
218 return sdev;
219 }
c1de7a6d
JCPV
220
221 return NULL;
222}
223
4225f2f5 224struct stdio_dev *stdio_clone(struct stdio_dev *dev)
628ffd73 225{
52cb4d4f 226 struct stdio_dev *_dev;
628ffd73 227
4225f2f5 228 if (!dev)
628ffd73
JCPV
229 return NULL;
230
52cb4d4f 231 _dev = calloc(1, sizeof(struct stdio_dev));
4225f2f5 232 if (!_dev)
628ffd73
JCPV
233 return NULL;
234
52cb4d4f 235 memcpy(_dev, dev, sizeof(struct stdio_dev));
628ffd73
JCPV
236
237 return _dev;
238}
91d3256c 239
d97143a6 240int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
91d3256c 241{
52cb4d4f 242 struct stdio_dev *_dev;
628ffd73 243
52cb4d4f 244 _dev = stdio_clone(dev);
4225f2f5 245 if (!_dev)
d97143a6 246 return -ENODEV;
18c587d0 247 list_add_tail(&_dev->list, &devs.list);
d97143a6
SG
248 if (devp)
249 *devp = _dev;
250
91d3256c
WD
251 return 0;
252}
253
d97143a6
SG
254int stdio_register(struct stdio_dev *dev)
255{
256 return stdio_register_dev(dev, NULL);
257}
258
32d01926 259int stdio_deregister_dev(struct stdio_dev *dev, int force)
91d3256c 260{
c1de7a6d 261 struct list_head *pos;
6da11cc8 262 char temp_names[3][STDIO_NAME_LEN];
4225f2f5 263 int i;
91d3256c 264
91d3256c 265 /* get stdio devices (ListRemoveItem changes the dev list) */
4225f2f5
SG
266 for (i = 0 ; i < MAX_FILES; i++) {
267 if (stdio_devices[i] == dev) {
32d01926 268 if (force) {
4225f2f5 269 strcpy(temp_names[i], "nulldev");
32d01926
HG
270 continue;
271 }
91d3256c 272 /* Device is assigned -> report error */
4225f2f5 273 return -EBUSY;
91d3256c 274 }
6da11cc8
HS
275 strlcpy(&temp_names[i][0], stdio_devices[i]->name,
276 sizeof(temp_names[i]));
91d3256c 277 }
c1de7a6d 278
18c587d0 279 list_del(&dev->list);
88274b6c 280 free(dev);
c1de7a6d 281
4225f2f5 282 /* reassign device list */
18c587d0 283 list_for_each(pos, &devs.list) {
52cb4d4f 284 dev = list_entry(pos, struct stdio_dev, list);
4225f2f5
SG
285 for (i = 0 ; i < MAX_FILES; i++) {
286 if (strcmp(dev->name, temp_names[i]) == 0)
287 stdio_devices[i] = dev;
91d3256c
WD
288 }
289 }
4225f2f5 290
91d3256c
WD
291 return 0;
292}
d97143a6 293
9fb02491 294int stdio_init_tables(void)
91d3256c 295{
91d3256c 296 /* Initialize the list */
18c587d0 297 INIT_LIST_HEAD(&devs.list);
91d3256c 298
9fb02491
SG
299 return 0;
300}
301
302int stdio_add_devices(void)
303{
b206cd73 304 struct udevice *dev;
b206cd73
SG
305 int ret;
306
5d4b6b17
SG
307 if (IS_ENABLED(CONFIG_DM_KEYBOARD)) {
308 /*
309 * For now we probe all the devices here. At some point this
310 * should be done only when the devices are required - e.g. we
311 * have a list of input devices to start up in the stdin
312 * environment variable. That work probably makes more sense
313 * when stdio itself is converted to driver model.
5d4b6b17 314 */
5d4b6b17
SG
315
316 /*
317 * Don't report errors to the caller - assume that they are
318 * non-fatal
319 */
7ff12631
MS
320 for (ret = uclass_first_device_check(UCLASS_KEYBOARD, &dev);
321 dev;
322 ret = uclass_next_device_check(&dev)) {
5d4b6b17 323 if (ret)
7ff12631
MS
324 printf("%s: Failed to probe keyboard '%s' (ret=%d)\n",
325 __func__, dev->name, ret);
5d4b6b17 326 }
b206cd73 327 }
55dabcc8 328#if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
3f4978c7 329 i2c_init_all();
3f4978c7 330#endif
b86986c7 331 if (IS_ENABLED(CONFIG_VIDEO)) {
5d4b6b17
SG
332 /*
333 * If the console setting is not in environment variables then
334 * console_init_r() will not be calling iomux_doenv() (which
3232487d 335 * calls console_search_dev()). So we will not dynamically add
5d4b6b17
SG
336 * devices by calling stdio_probe_device().
337 *
338 * So just probe all video devices now so that whichever one is
339 * required will be available.
340 */
341 struct udevice *vdev;
342 int ret;
343
344 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_IS_IN_ENV)) {
7ff12631
MS
345 for (ret = uclass_first_device_check(UCLASS_VIDEO,
346 &vdev);
347 vdev;
348 ret = uclass_next_device_check(&vdev)) {
349 if (ret)
350 printf("%s: Failed to probe video device '%s' (ret=%d)\n",
351 __func__, vdev->name, ret);
352 }
5d4b6b17
SG
353 }
354 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) &&
355 IS_ENABLED(CONFIG_CMD_BMP))
356 splash_display();
5d4b6b17
SG
357 }
358
5d4b6b17
SG
359 drv_system_init();
360 serial_stdio_init();
232c150a 361#ifdef CONFIG_USB_TTY
5d4b6b17 362 drv_usbtty_init();
fc2b399a
LP
363#endif
364#ifdef CONFIG_USB_FUNCTION_ACM
365 drv_usbacm_init ();
68ceb29e 366#endif
5d4b6b17
SG
367 if (IS_ENABLED(CONFIG_NETCONSOLE))
368 drv_nc_init();
36ea8e9a 369#ifdef CONFIG_JTAG_CONSOLE
5d4b6b17 370 drv_jtag_console_init();
98ab435f 371#endif
5d4b6b17
SG
372 if (IS_ENABLED(CONFIG_CBMEM_CONSOLE))
373 cbmemc_init();
9fb02491
SG
374
375 return 0;
376}
This page took 0.622768 seconds and 4 git commands to generate.