stdio: Drop brackets around &devs.list
[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
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 static void nulldev_putc(struct stdio_dev *dev, const char c)
32 {
33         /* nulldev is empty! */
34 }
35
36 static void nulldev_puts(struct stdio_dev *dev, const char *s)
37 {
38         /* nulldev is empty! */
39 }
40
41 static int nulldev_input(struct stdio_dev *dev)
42 {
43         /* nulldev is empty! */
44         return 0;
45 }
46
47 static void stdio_serial_putc(struct stdio_dev *dev, const char c)
48 {
49         serial_putc(c);
50 }
51
52 static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
53 {
54         serial_puts(s);
55 }
56
57 static int stdio_serial_getc(struct stdio_dev *dev)
58 {
59         return serial_getc();
60 }
61
62 static int stdio_serial_tstc(struct stdio_dev *dev)
63 {
64         return serial_tstc();
65 }
66
67 /**************************************************************************
68  * SYSTEM DRIVERS
69  **************************************************************************
70  */
71
72 static void drv_system_init (void)
73 {
74         struct stdio_dev dev;
75
76         memset (&dev, 0, sizeof (dev));
77
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);
85
86         if (CONFIG_IS_ENABLED(SYS_DEVICE_NULLDEV)) {
87                 memset(&dev, '\0', sizeof(dev));
88
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;
95
96                 stdio_register(&dev);
97         }
98 }
99
100 /**************************************************************************
101  * DEVICES
102  **************************************************************************
103  */
104 struct list_head* stdio_get_list(void)
105 {
106         return &devs.list;
107 }
108
109 /**
110  * stdio_probe_device() - Find a device which provides the given stdio device
111  *
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.
114  *
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.
121  *
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
126  *         on other error
127  */
128 static int stdio_probe_device(const char *name, enum uclass_id id,
129                               struct stdio_dev **sdevp)
130 {
131         struct stdio_dev *sdev;
132         struct udevice *dev;
133         int seq, ret;
134
135         *sdevp = NULL;
136         seq = trailing_strtoln(name, NULL);
137         if (seq == -1)
138                 seq = 0;
139         ret = uclass_get_device_by_seq(id, seq, &dev);
140         if (ret == -ENODEV)
141                 ret = uclass_first_device_err(id, &dev);
142         if (ret) {
143                 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
144                       seq, name);
145                 return ret;
146         }
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",
152                       dev->name, name);
153                 return -ENOENT;
154         }
155         *sdevp = sdev;
156
157         return 0;
158 }
159
160 struct stdio_dev *stdio_get_by_name(const char *name)
161 {
162         struct list_head *pos;
163         struct stdio_dev *sdev;
164
165         if (!name)
166                 return NULL;
167
168         list_for_each(pos, &devs.list) {
169                 sdev = list_entry(pos, struct stdio_dev, list);
170                 if (strcmp(sdev->name, name) == 0)
171                         return sdev;
172         }
173         if (IS_ENABLED(CONFIG_DM_VIDEO)) {
174                 /*
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
178                  * stdio device.
179                  *
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()
185                  */
186                 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
187                     !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
188                         return sdev;
189         }
190
191         return NULL;
192 }
193
194 struct stdio_dev* stdio_clone(struct stdio_dev *dev)
195 {
196         struct stdio_dev *_dev;
197
198         if(!dev)
199                 return NULL;
200
201         _dev = calloc(1, sizeof(struct stdio_dev));
202
203         if(!_dev)
204                 return NULL;
205
206         memcpy(_dev, dev, sizeof(struct stdio_dev));
207
208         return _dev;
209 }
210
211 int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
212 {
213         struct stdio_dev *_dev;
214
215         _dev = stdio_clone(dev);
216         if(!_dev)
217                 return -ENODEV;
218         list_add_tail(&_dev->list, &devs.list);
219         if (devp)
220                 *devp = _dev;
221
222         return 0;
223 }
224
225 int stdio_register(struct stdio_dev *dev)
226 {
227         return stdio_register_dev(dev, NULL);
228 }
229
230 /* deregister the device "devname".
231  * returns 0 if success, -1 if device is assigned and 1 if devname not found
232  */
233 int stdio_deregister_dev(struct stdio_dev *dev, int force)
234 {
235         int l;
236         struct list_head *pos;
237         char temp_names[3][16];
238
239         /* get stdio devices (ListRemoveItem changes the dev list) */
240         for (l=0 ; l< MAX_FILES; l++) {
241                 if (stdio_devices[l] == dev) {
242                         if (force) {
243                                 strcpy(temp_names[l], "nulldev");
244                                 continue;
245                         }
246                         /* Device is assigned -> report error */
247                         return -1;
248                 }
249                 memcpy (&temp_names[l][0],
250                         stdio_devices[l]->name,
251                         sizeof(temp_names[l]));
252         }
253
254         list_del(&dev->list);
255         free(dev);
256
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;
263                 }
264         }
265         return 0;
266 }
267
268 int stdio_deregister(const char *devname, int force)
269 {
270         struct stdio_dev *dev;
271
272         dev = stdio_get_by_name(devname);
273
274         if (!dev) /* device not found */
275                 return -ENODEV;
276
277         return stdio_deregister_dev(dev, force);
278 }
279
280 int stdio_init_tables(void)
281 {
282 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
283         /* already relocated for current ARM implementation */
284         ulong relocation_offset = gd->reloc_off;
285         int i;
286
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]) +
290                                                 relocation_offset);
291         }
292 #endif /* CONFIG_NEEDS_MANUAL_RELOC */
293
294         /* Initialize the list */
295         INIT_LIST_HEAD(&devs.list);
296
297         return 0;
298 }
299
300 int stdio_add_devices(void)
301 {
302         struct udevice *dev;
303         struct uclass *uc;
304         int ret;
305
306         if (IS_ENABLED(CONFIG_DM_KEYBOARD)) {
307                 /*
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.
313                  *
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.
317                  */
318                 ret = uclass_get(UCLASS_KEYBOARD, &uc);
319                 if (ret)
320                         return ret;
321
322                 /*
323                  * Don't report errors to the caller - assume that they are
324                  * non-fatal
325                  */
326                 uclass_foreach_dev(dev, uc) {
327                         ret = device_probe(dev);
328                         if (ret)
329                                 printf("Failed to probe keyboard '%s'\n",
330                                        dev->name);
331                 }
332         }
333 #ifdef CONFIG_SYS_I2C
334         i2c_init_all();
335 #endif
336         if (IS_ENABLED(CONFIG_DM_VIDEO)) {
337                 /*
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().
342                  *
343                  * So just probe all video devices now so that whichever one is
344                  * required will be available.
345                  */
346                 struct udevice *vdev;
347                 int ret;
348
349                 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_IS_IN_ENV)) {
350                         for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
351                              vdev;
352                              ret = uclass_next_device(&vdev))
353                                 ;
354                         if (ret)
355                                 printf("%s: Video device failed (ret=%d)\n",
356                                        __func__, ret);
357                 }
358                 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) &&
359                     IS_ENABLED(CONFIG_CMD_BMP))
360                         splash_display();
361         } else {
362                 if (IS_ENABLED(CONFIG_LCD))
363                         drv_lcd_init();
364                 if (IS_ENABLED(CONFIG_VIDEO) || IS_ENABLED(CONFIG_CFB_CONSOLE))
365                         drv_video_init();
366         }
367
368 #if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
369         drv_keyboard_init();
370 #endif
371         drv_system_init();
372         serial_stdio_init();
373 #ifdef CONFIG_USB_TTY
374         drv_usbtty_init();
375 #endif
376         if (IS_ENABLED(CONFIG_NETCONSOLE))
377                 drv_nc_init();
378 #ifdef CONFIG_JTAG_CONSOLE
379         drv_jtag_console_init();
380 #endif
381         if (IS_ENABLED(CONFIG_CBMEM_CONSOLE))
382                 cbmemc_init();
383
384         return 0;
385 }
386
387 int stdio_init(void)
388 {
389         stdio_init_tables();
390         stdio_add_devices();
391
392         return 0;
393 }