stdio.c 9.4 KB

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