stdio.c 9.1 KB

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