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 
stdio_file_to_flags(const int file)31 int 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 
44 #if CONFIG_IS_ENABLED(SYS_DEVICE_NULLDEV)
nulldev_putc(struct stdio_dev * dev,const char c)45 static void nulldev_putc(struct stdio_dev *dev, const char c)
46 {
47 	/* nulldev is empty! */
48 }
49 
nulldev_puts(struct stdio_dev * dev,const char * s)50 static void nulldev_puts(struct stdio_dev *dev, const char *s)
51 {
52 	/* nulldev is empty! */
53 }
54 
nulldev_input(struct stdio_dev * dev)55 static int nulldev_input(struct stdio_dev *dev)
56 {
57 	/* nulldev is empty! */
58 	return 0;
59 }
60 
nulldev_register(void)61 static 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
nulldev_register(void)77 static inline void nulldev_register(void) {}
78 #endif	/* SYS_DEVICE_NULLDEV */
79 
stdio_serial_putc(struct stdio_dev * dev,const char c)80 static void stdio_serial_putc(struct stdio_dev *dev, const char c)
81 {
82 	serial_putc(c);
83 }
84 
stdio_serial_puts(struct stdio_dev * dev,const char * s)85 static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
86 {
87 	serial_puts(s);
88 }
89 
90 #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
stdio_serial_flush(struct stdio_dev * dev)91 static void stdio_serial_flush(struct stdio_dev *dev)
92 {
93 	serial_flush();
94 }
95 #endif
96 
stdio_serial_getc(struct stdio_dev * dev)97 static int stdio_serial_getc(struct stdio_dev *dev)
98 {
99 	return serial_getc();
100 }
101 
stdio_serial_tstc(struct stdio_dev * dev)102 static int stdio_serial_tstc(struct stdio_dev *dev)
103 {
104 	return serial_tstc();
105 }
106 
107 /**************************************************************************
108  * SYSTEM DRIVERS
109  **************************************************************************
110  */
111 
drv_system_init(void)112 static void drv_system_init (void)
113 {
114 	struct stdio_dev dev;
115 
116 	memset (&dev, 0, sizeof (dev));
117 
118 	strcpy (dev.name, "serial");
119 	dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
120 	dev.putc = stdio_serial_putc;
121 	dev.puts = stdio_serial_puts;
122 	STDIO_DEV_ASSIGN_FLUSH(&dev, stdio_serial_flush);
123 	dev.getc = stdio_serial_getc;
124 	dev.tstc = stdio_serial_tstc;
125 	stdio_register (&dev);
126 
127 	nulldev_register();
128 }
129 
130 /**************************************************************************
131  * DEVICES
132  **************************************************************************
133  */
stdio_get_list(void)134 struct list_head* stdio_get_list(void)
135 {
136 	return &devs.list;
137 }
138 
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(sjg@chromium.org): 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
155  * Return: 0 if found, -ENOENT if no device found with that name, other -ve
156  *	   on other error
157  */
stdio_probe_device(const char * name,enum uclass_id id,struct stdio_dev ** sdevp)158 static 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)
168 		seq = 0;
169 	ret = uclass_get_device_by_seq(id, seq, &dev);
170 	if (ret == -ENODEV)
171 		ret = uclass_first_device_err(id, &dev);
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 }
189 
stdio_get_by_name(const char * name)190 struct stdio_dev *stdio_get_by_name(const char *name)
191 {
192 	struct list_head *pos;
193 	struct stdio_dev *sdev;
194 
195 	if (!name)
196 		return NULL;
197 
198 	list_for_each(pos, &devs.list) {
199 		sdev = list_entry(pos, struct stdio_dev, list);
200 		if (strcmp(sdev->name, name) == 0)
201 			return sdev;
202 	}
203 	if (IS_ENABLED(CONFIG_VIDEO)) {
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
214 		 * console_init_r() -> console_search_dev() -> stdio_get_by_name()
215 		 */
216 		if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
217 		    !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
218 			return sdev;
219 	}
220 
221 	return NULL;
222 }
223 
stdio_clone(struct stdio_dev * dev)224 struct stdio_dev *stdio_clone(struct stdio_dev *dev)
225 {
226 	struct stdio_dev *_dev;
227 
228 	if (!dev)
229 		return NULL;
230 
231 	_dev = calloc(1, sizeof(struct stdio_dev));
232 	if (!_dev)
233 		return NULL;
234 
235 	memcpy(_dev, dev, sizeof(struct stdio_dev));
236 
237 	return _dev;
238 }
239 
stdio_register_dev(struct stdio_dev * dev,struct stdio_dev ** devp)240 int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
241 {
242 	struct stdio_dev *_dev;
243 
244 	_dev = stdio_clone(dev);
245 	if (!_dev)
246 		return -ENODEV;
247 	list_add_tail(&_dev->list, &devs.list);
248 	if (devp)
249 		*devp = _dev;
250 
251 	return 0;
252 }
253 
stdio_register(struct stdio_dev * dev)254 int stdio_register(struct stdio_dev *dev)
255 {
256 	return stdio_register_dev(dev, NULL);
257 }
258 
stdio_deregister_dev(struct stdio_dev * dev,int force)259 int stdio_deregister_dev(struct stdio_dev *dev, int force)
260 {
261 	struct list_head *pos;
262 	char temp_names[3][16];
263 	int i;
264 
265 	/* get stdio devices (ListRemoveItem changes the dev list) */
266 	for (i = 0 ; i < MAX_FILES; i++) {
267 		if (stdio_devices[i] == dev) {
268 			if (force) {
269 				strcpy(temp_names[i], "nulldev");
270 				continue;
271 			}
272 			/* Device is assigned -> report error */
273 			return -EBUSY;
274 		}
275 		memcpy(&temp_names[i][0], stdio_devices[i]->name,
276 		       sizeof(temp_names[i]));
277 	}
278 
279 	list_del(&dev->list);
280 	free(dev);
281 
282 	/* reassign device list */
283 	list_for_each(pos, &devs.list) {
284 		dev = list_entry(pos, struct stdio_dev, list);
285 		for (i = 0 ; i < MAX_FILES; i++) {
286 			if (strcmp(dev->name, temp_names[i]) == 0)
287 				stdio_devices[i] = dev;
288 		}
289 	}
290 
291 	return 0;
292 }
293 
stdio_init_tables(void)294 int stdio_init_tables(void)
295 {
296 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
297 	/* already relocated for current ARM implementation */
298 	ulong relocation_offset = gd->reloc_off;
299 	int i;
300 
301 	/* relocate device name pointers */
302 	for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
303 		stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
304 						relocation_offset);
305 	}
306 #endif /* CONFIG_NEEDS_MANUAL_RELOC */
307 
308 	/* Initialize the list */
309 	INIT_LIST_HEAD(&devs.list);
310 
311 	return 0;
312 }
313 
stdio_add_devices(void)314 int stdio_add_devices(void)
315 {
316 	struct udevice *dev;
317 	int ret;
318 
319 	if (IS_ENABLED(CONFIG_DM_KEYBOARD)) {
320 		/*
321 		 * For now we probe all the devices here. At some point this
322 		 * should be done only when the devices are required - e.g. we
323 		 * have a list of input devices to start up in the stdin
324 		 * environment variable. That work probably makes more sense
325 		 * when stdio itself is converted to driver model.
326 		 */
327 
328 		/*
329 		 * Don't report errors to the caller - assume that they are
330 		 * non-fatal
331 		 */
332 		for (ret = uclass_first_device_check(UCLASS_KEYBOARD, &dev);
333 				dev;
334 				ret = uclass_next_device_check(&dev)) {
335 			if (ret)
336 				printf("%s: Failed to probe keyboard '%s' (ret=%d)\n",
337 				       __func__, dev->name, ret);
338 		}
339 	}
340 #if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
341 	i2c_init_all();
342 #endif
343 	if (IS_ENABLED(CONFIG_VIDEO)) {
344 		/*
345 		 * If the console setting is not in environment variables then
346 		 * console_init_r() will not be calling iomux_doenv() (which
347 		 * calls console_search_dev()). So we will not dynamically add
348 		 * devices by calling stdio_probe_device().
349 		 *
350 		 * So just probe all video devices now so that whichever one is
351 		 * required will be available.
352 		 */
353 		struct udevice *vdev;
354 		int ret;
355 
356 		if (!IS_ENABLED(CONFIG_SYS_CONSOLE_IS_IN_ENV)) {
357 			for (ret = uclass_first_device_check(UCLASS_VIDEO,
358 							     &vdev);
359 					vdev;
360 					ret = uclass_next_device_check(&vdev)) {
361 				if (ret)
362 					printf("%s: Failed to probe video device '%s' (ret=%d)\n",
363 					       __func__, vdev->name, ret);
364 			}
365 		}
366 		if (IS_ENABLED(CONFIG_SPLASH_SCREEN) &&
367 		    IS_ENABLED(CONFIG_CMD_BMP))
368 			splash_display();
369 	}
370 
371 	drv_system_init();
372 	serial_stdio_init();
373 #ifdef CONFIG_USB_TTY
374 	drv_usbtty_init();
375 #endif
376 #ifdef CONFIG_USB_FUNCTION_ACM
377 	drv_usbacm_init ();
378 #endif
379 	if (IS_ENABLED(CONFIG_NETCONSOLE))
380 		drv_nc_init();
381 #ifdef CONFIG_JTAG_CONSOLE
382 	drv_jtag_console_init();
383 #endif
384 	if (IS_ENABLED(CONFIG_CBMEM_CONSOLE))
385 		cbmemc_init();
386 
387 	return 0;
388 }
389 
stdio_init(void)390 int stdio_init(void)
391 {
392 	stdio_init_tables();
393 	stdio_add_devices();
394 
395 	return 0;
396 }
397