1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
5 * Changes for multibus/multiadapter I2C support.
8 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
17 #include <stdio_dev.h>
21 #include <asm/global_data.h>
22 #include <dm/device-internal.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 static struct stdio_dev devs;
27 struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
28 char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
30 int stdio_file_to_flags(const int file)
34 return DEV_FLAGS_INPUT;
37 return DEV_FLAGS_OUTPUT;
43 #if CONFIG_IS_ENABLED(SYS_DEVICE_NULLDEV)
44 static void nulldev_putc(struct stdio_dev *dev, const char c)
46 /* nulldev is empty! */
49 static void nulldev_puts(struct stdio_dev *dev, const char *s)
51 /* nulldev is empty! */
54 static int nulldev_input(struct stdio_dev *dev)
56 /* nulldev is empty! */
60 static void nulldev_register(void)
64 memset(&dev, '\0', sizeof(dev));
66 strcpy(dev.name, "nulldev");
67 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
68 dev.putc = nulldev_putc;
69 dev.puts = nulldev_puts;
70 dev.getc = nulldev_input;
71 dev.tstc = nulldev_input;
76 static inline void nulldev_register(void) {}
77 #endif /* SYS_DEVICE_NULLDEV */
79 static void stdio_serial_putc(struct stdio_dev *dev, const char c)
84 static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
89 #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
90 static void stdio_serial_flush(struct stdio_dev *dev)
96 static int stdio_serial_getc(struct stdio_dev *dev)
101 static int stdio_serial_tstc(struct stdio_dev *dev)
103 return serial_tstc();
106 /**************************************************************************
108 **************************************************************************
111 static void drv_system_init (void)
113 struct stdio_dev dev;
115 memset (&dev, 0, sizeof (dev));
117 strcpy (dev.name, "serial");
118 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
119 dev.putc = stdio_serial_putc;
120 dev.puts = stdio_serial_puts;
121 STDIO_DEV_ASSIGN_FLUSH(&dev, stdio_serial_flush);
122 dev.getc = stdio_serial_getc;
123 dev.tstc = stdio_serial_tstc;
124 stdio_register (&dev);
129 /**************************************************************************
131 **************************************************************************
133 struct list_head* stdio_get_list(void)
139 * stdio_probe_device() - Find a device which provides the given stdio device
141 * This looks for a device of the given uclass which provides a particular
142 * stdio device. It is currently really only useful for UCLASS_VIDEO.
144 * Ultimately we want to be able to probe a device by its stdio name. At
145 * present devices register in their probe function (for video devices this
146 * is done in vidconsole_post_probe()) and we don't know what name they will
147 * use until they do so.
148 * TODO(sjg@chromium.org): We should be able to determine the name before
149 * probing, and probe the required device.
151 * @name: stdio device name (e.g. "vidconsole")
152 * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO)
153 * @sdevp: Returns stdout device, if found, else NULL
154 * Return: 0 if found, -ENOENT if no device found with that name, other -ve
157 static int stdio_probe_device(const char *name, enum uclass_id id,
158 struct stdio_dev **sdevp)
160 struct stdio_dev *sdev;
165 seq = trailing_strtoln(name, NULL);
168 ret = uclass_get_device_by_seq(id, seq, &dev);
170 ret = uclass_first_device_err(id, &dev);
172 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
176 /* The device should be be the last one registered */
177 sdev = list_empty(&devs.list) ? NULL :
178 list_last_entry(&devs.list, struct stdio_dev, list);
179 if (!sdev || strcmp(sdev->name, name)) {
180 debug("Device '%s' did not register with stdio as '%s'\n",
189 struct stdio_dev *stdio_get_by_name(const char *name)
191 struct list_head *pos;
192 struct stdio_dev *sdev;
197 list_for_each(pos, &devs.list) {
198 sdev = list_entry(pos, struct stdio_dev, list);
199 if (strcmp(sdev->name, name) == 0)
202 if (IS_ENABLED(CONFIG_VIDEO)) {
204 * We did not find a suitable stdio device. If there is a video
205 * driver with a name starting with 'vidconsole', we can try
206 * probing that in the hope that it will produce the required
209 * This function is sometimes called with the entire value of
210 * 'stdout', which may include a list of devices separate by
211 * commas. Obviously this is not going to work, so we ignore
212 * that case. The call path in that case is
213 * console_init_r() -> console_search_dev() -> stdio_get_by_name()
215 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
216 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
223 struct stdio_dev *stdio_clone(struct stdio_dev *dev)
225 struct stdio_dev *_dev;
230 _dev = calloc(1, sizeof(struct stdio_dev));
234 memcpy(_dev, dev, sizeof(struct stdio_dev));
239 int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
241 struct stdio_dev *_dev;
243 _dev = stdio_clone(dev);
246 list_add_tail(&_dev->list, &devs.list);
253 int stdio_register(struct stdio_dev *dev)
255 return stdio_register_dev(dev, NULL);
258 int stdio_deregister_dev(struct stdio_dev *dev, int force)
260 struct list_head *pos;
261 char temp_names[3][STDIO_NAME_LEN];
264 /* get stdio devices (ListRemoveItem changes the dev list) */
265 for (i = 0 ; i < MAX_FILES; i++) {
266 if (stdio_devices[i] == dev) {
268 strcpy(temp_names[i], "nulldev");
271 /* Device is assigned -> report error */
274 strlcpy(&temp_names[i][0], stdio_devices[i]->name,
275 sizeof(temp_names[i]));
278 list_del(&dev->list);
281 /* reassign device list */
282 list_for_each(pos, &devs.list) {
283 dev = list_entry(pos, struct stdio_dev, list);
284 for (i = 0 ; i < MAX_FILES; i++) {
285 if (strcmp(dev->name, temp_names[i]) == 0)
286 stdio_devices[i] = dev;
293 int stdio_init_tables(void)
295 /* Initialize the list */
296 INIT_LIST_HEAD(&devs.list);
301 int stdio_add_devices(void)
306 if (IS_ENABLED(CONFIG_DM_KEYBOARD)) {
308 * For now we probe all the devices here. At some point this
309 * should be done only when the devices are required - e.g. we
310 * have a list of input devices to start up in the stdin
311 * environment variable. That work probably makes more sense
312 * when stdio itself is converted to driver model.
316 * Don't report errors to the caller - assume that they are
319 for (ret = uclass_first_device_check(UCLASS_KEYBOARD, &dev);
321 ret = uclass_next_device_check(&dev)) {
323 printf("%s: Failed to probe keyboard '%s' (ret=%d)\n",
324 __func__, dev->name, ret);
327 #if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
330 if (IS_ENABLED(CONFIG_VIDEO)) {
332 * If the console setting is not in environment variables then
333 * console_init_r() will not be calling iomux_doenv() (which
334 * calls console_search_dev()). So we will not dynamically add
335 * devices by calling stdio_probe_device().
337 * So just probe all video devices now so that whichever one is
338 * required will be available.
340 struct udevice *vdev;
343 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_IS_IN_ENV)) {
344 for (ret = uclass_first_device_check(UCLASS_VIDEO,
347 ret = uclass_next_device_check(&vdev)) {
349 printf("%s: Failed to probe video device '%s' (ret=%d)\n",
350 __func__, vdev->name, ret);
353 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) &&
354 IS_ENABLED(CONFIG_CMD_BMP))
360 #ifdef CONFIG_USB_TTY
363 #ifdef CONFIG_USB_FUNCTION_ACM
366 if (IS_ENABLED(CONFIG_NETCONSOLE))
368 #ifdef CONFIG_JTAG_CONSOLE
369 drv_jtag_console_init();
371 if (IS_ENABLED(CONFIG_CBMEM_CONSOLE))