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