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
18 #include <stdio_dev.h>
23 #include <dm/device-internal.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 static struct stdio_dev devs;
28 struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
29 char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
31 static void nulldev_putc(struct stdio_dev *dev, const char c)
33 /* nulldev is empty! */
36 static void nulldev_puts(struct stdio_dev *dev, const char *s)
38 /* nulldev is empty! */
41 static int nulldev_input(struct stdio_dev *dev)
43 /* nulldev is empty! */
47 static void stdio_serial_putc(struct stdio_dev *dev, const char c)
52 static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
57 static int stdio_serial_getc(struct stdio_dev *dev)
62 static int stdio_serial_tstc(struct stdio_dev *dev)
67 /**************************************************************************
69 **************************************************************************
72 static void drv_system_init (void)
76 memset (&dev, 0, sizeof (dev));
78 strcpy (dev.name, "serial");
79 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
80 dev.putc = stdio_serial_putc;
81 dev.puts = stdio_serial_puts;
82 dev.getc = stdio_serial_getc;
83 dev.tstc = stdio_serial_tstc;
84 stdio_register (&dev);
86 if (CONFIG_IS_ENABLED(SYS_DEVICE_NULLDEV)) {
87 memset(&dev, '\0', sizeof(dev));
89 strcpy(dev.name, "nulldev");
90 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
91 dev.putc = nulldev_putc;
92 dev.puts = nulldev_puts;
93 dev.getc = nulldev_input;
94 dev.tstc = nulldev_input;
100 /**************************************************************************
102 **************************************************************************
104 struct list_head* stdio_get_list(void)
110 * stdio_probe_device() - Find a device which provides the given stdio device
112 * This looks for a device of the given uclass which provides a particular
113 * stdio device. It is currently really only useful for UCLASS_VIDEO.
115 * Ultimately we want to be able to probe a device by its stdio name. At
116 * present devices register in their probe function (for video devices this
117 * is done in vidconsole_post_probe()) and we don't know what name they will
118 * use until they do so.
119 * TODO(sjg@chromium.org): We should be able to determine the name before
120 * probing, and probe the required device.
122 * @name: stdio device name (e.g. "vidconsole")
123 * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO)
124 * @sdevp: Returns stdout device, if found, else NULL
125 * @return 0 if found, -ENOENT if no device found with that name, other -ve
128 static int stdio_probe_device(const char *name, enum uclass_id id,
129 struct stdio_dev **sdevp)
131 struct stdio_dev *sdev;
136 seq = trailing_strtoln(name, NULL);
139 ret = uclass_get_device_by_seq(id, seq, &dev);
141 ret = uclass_first_device_err(id, &dev);
143 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
147 /* The device should be be the last one registered */
148 sdev = list_empty(&devs.list) ? NULL :
149 list_last_entry(&devs.list, struct stdio_dev, list);
150 if (!sdev || strcmp(sdev->name, name)) {
151 debug("Device '%s' did not register with stdio as '%s'\n",
160 struct stdio_dev *stdio_get_by_name(const char *name)
162 struct list_head *pos;
163 struct stdio_dev *sdev;
168 list_for_each(pos, &devs.list) {
169 sdev = list_entry(pos, struct stdio_dev, list);
170 if (strcmp(sdev->name, name) == 0)
173 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
175 * We did not find a suitable stdio device. If there is a video
176 * driver with a name starting with 'vidconsole', we can try
177 * probing that in the hope that it will produce the required
180 * This function is sometimes called with the entire value of
181 * 'stdout', which may include a list of devices separate by
182 * commas. Obviously this is not going to work, so we ignore
183 * that case. The call path in that case is
184 * console_init_r() -> search_device() -> stdio_get_by_name()
186 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
187 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
194 struct stdio_dev* stdio_clone(struct stdio_dev *dev)
196 struct stdio_dev *_dev;
201 _dev = calloc(1, sizeof(struct stdio_dev));
206 memcpy(_dev, dev, sizeof(struct stdio_dev));
211 int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
213 struct stdio_dev *_dev;
215 _dev = stdio_clone(dev);
218 list_add_tail(&_dev->list, &devs.list);
225 int stdio_register(struct stdio_dev *dev)
227 return stdio_register_dev(dev, NULL);
230 /* deregister the device "devname".
231 * returns 0 if success, -1 if device is assigned and 1 if devname not found
233 int stdio_deregister_dev(struct stdio_dev *dev, int force)
236 struct list_head *pos;
237 char temp_names[3][16];
239 /* get stdio devices (ListRemoveItem changes the dev list) */
240 for (l=0 ; l< MAX_FILES; l++) {
241 if (stdio_devices[l] == dev) {
243 strcpy(temp_names[l], "nulldev");
246 /* Device is assigned -> report error */
249 memcpy (&temp_names[l][0],
250 stdio_devices[l]->name,
251 sizeof(temp_names[l]));
254 list_del(&dev->list);
257 /* reassign Device list */
258 list_for_each(pos, &devs.list) {
259 dev = list_entry(pos, struct stdio_dev, list);
260 for (l=0 ; l< MAX_FILES; l++) {
261 if(strcmp(dev->name, temp_names[l]) == 0)
262 stdio_devices[l] = dev;
268 int stdio_deregister(const char *devname, int force)
270 struct stdio_dev *dev;
272 dev = stdio_get_by_name(devname);
274 if (!dev) /* device not found */
277 return stdio_deregister_dev(dev, force);
280 int stdio_init_tables(void)
282 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
283 /* already relocated for current ARM implementation */
284 ulong relocation_offset = gd->reloc_off;
287 /* relocate device name pointers */
288 for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
289 stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
292 #endif /* CONFIG_NEEDS_MANUAL_RELOC */
294 /* Initialize the list */
295 INIT_LIST_HEAD(&devs.list);
300 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.
314 * TODO(sjg@chromium.org): Convert changing
315 * uclass_first_device() etc. to return the device even on
316 * error. Then we could use that here.
318 ret = uclass_get(UCLASS_KEYBOARD, &uc);
323 * Don't report errors to the caller - assume that they are
326 uclass_foreach_dev(dev, uc) {
327 ret = device_probe(dev);
329 printf("Failed to probe keyboard '%s'\n",
333 #ifdef CONFIG_SYS_I2C
336 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
338 * If the console setting is not in environment variables then
339 * console_init_r() will not be calling iomux_doenv() (which
340 * calls search_device()). So we will not dynamically add
341 * devices by calling stdio_probe_device().
343 * So just probe all video devices now so that whichever one is
344 * required will be available.
346 struct udevice *vdev;
349 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_IS_IN_ENV)) {
350 for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
352 ret = uclass_next_device(&vdev))
355 printf("%s: Video device failed (ret=%d)\n",
358 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) &&
359 IS_ENABLED(CONFIG_CMD_BMP))
362 if (IS_ENABLED(CONFIG_LCD))
364 if (IS_ENABLED(CONFIG_VIDEO) || IS_ENABLED(CONFIG_CFB_CONSOLE))
368 #if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
373 #ifdef CONFIG_USB_TTY
376 if (IS_ENABLED(CONFIG_NETCONSOLE))
378 #ifdef CONFIG_JTAG_CONSOLE
379 drv_jtag_console_init();
381 if (IS_ENABLED(CONFIG_CBMEM_CONSOLE))