osd.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2017
  4. * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
  5. *
  6. * based on the gdsys osd driver, which is
  7. *
  8. * (C) Copyright 2010
  9. * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
  10. */
  11. #include <common.h>
  12. #include <command.h>
  13. #include <dm.h>
  14. #include <hexdump.h>
  15. #include <video_osd.h>
  16. #include <malloc.h>
  17. /* Container for selected OSD device */
  18. static struct udevice *osd_cur;
  19. /**
  20. * cmd_osd_set_osd_num() - Set the OSD selected for operation
  21. *
  22. * Set the OSD device, which will be used by all subsequent OSD commands.
  23. *
  24. * Devices are identified by their uclass sequence number (as listed by 'osd
  25. * show').
  26. *
  27. * @osdnum: The OSD device to be selected, identified by its sequence number.
  28. * Return: 0 if OK, -ve on error
  29. */
  30. static int cmd_osd_set_osd_num(unsigned int osdnum)
  31. {
  32. struct udevice *osd;
  33. int res;
  34. res = uclass_get_device_by_seq(UCLASS_VIDEO_OSD, osdnum, &osd);
  35. if (res) {
  36. printf("%s: No OSD %u (err = %d)\n", __func__, osdnum, res);
  37. return res;
  38. }
  39. osd_cur = osd;
  40. return 0;
  41. }
  42. /**
  43. * osd_get_osd_cur() - Get the selected OSD device
  44. *
  45. * Get the OSD device that is used by all OSD commands.
  46. *
  47. * @osdp: Pointer to structure that will receive the currently selected OSD
  48. * device.
  49. * Return: 0 if OK, -ve on error
  50. */
  51. static int osd_get_osd_cur(struct udevice **osdp)
  52. {
  53. if (!osd_cur) {
  54. puts("No osd selected\n");
  55. return -ENODEV;
  56. }
  57. *osdp = osd_cur;
  58. return 0;
  59. }
  60. /**
  61. * show_osd() - Display information about a OSD device
  62. *
  63. * Display a device's ID (sequence number), and whether it is active (i.e.
  64. * probed) or not.
  65. *
  66. * @osd: OSD device to print information for
  67. */
  68. static void show_osd(struct udevice *osd)
  69. {
  70. printf("OSD %d:\t%s", dev_seq(osd), osd->name);
  71. if (device_active(osd))
  72. printf(" (active)");
  73. printf("\n");
  74. }
  75. static int do_osd_write(struct cmd_tbl *cmdtp, int flag, int argc,
  76. char *const argv[])
  77. {
  78. uint x, y;
  79. uint count;
  80. char *hexstr;
  81. u8 *buffer;
  82. size_t buflen;
  83. int res;
  84. if (argc < 4 || (strlen(argv[3]) % 2))
  85. return CMD_RET_USAGE;
  86. if (!osd_cur) {
  87. puts("No osd selected\n");
  88. return CMD_RET_FAILURE;
  89. }
  90. x = hextoul(argv[1], NULL);
  91. y = hextoul(argv[2], NULL);
  92. hexstr = argv[3];
  93. count = (argc > 4) ? hextoul(argv[4], NULL) : 1;
  94. buflen = strlen(hexstr) / 2;
  95. buffer = malloc(buflen);
  96. if (!buffer) {
  97. puts("Memory allocation failure\n");
  98. return CMD_RET_FAILURE;
  99. }
  100. res = hex2bin(buffer, hexstr, buflen);
  101. if (res) {
  102. free(buffer);
  103. puts("Hexadecimal input contained invalid characters\n");
  104. return CMD_RET_FAILURE;
  105. }
  106. res = video_osd_set_mem(osd_cur, x, y, buffer, buflen, count);
  107. if (res) {
  108. free(buffer);
  109. printf("%s: Could not write to video mem\n",
  110. osd_cur->name);
  111. return CMD_RET_FAILURE;
  112. }
  113. free(buffer);
  114. return CMD_RET_SUCCESS;
  115. }
  116. static int do_osd_print(struct cmd_tbl *cmdtp, int flag, int argc,
  117. char *const argv[])
  118. {
  119. uint x, y;
  120. u8 color;
  121. char *text;
  122. int res;
  123. if (argc < 5)
  124. return CMD_RET_USAGE;
  125. if (!osd_cur) {
  126. puts("No osd selected\n");
  127. return CMD_RET_FAILURE;
  128. }
  129. x = hextoul(argv[1], NULL);
  130. y = hextoul(argv[2], NULL);
  131. color = hextoul(argv[3], NULL);
  132. text = argv[4];
  133. res = video_osd_print(osd_cur, x, y, color, text);
  134. if (res) {
  135. printf("Could not print string to osd %s\n", osd_cur->name);
  136. return CMD_RET_FAILURE;
  137. }
  138. return CMD_RET_SUCCESS;
  139. }
  140. static int do_osd_size(struct cmd_tbl *cmdtp, int flag, int argc,
  141. char *const argv[])
  142. {
  143. uint x, y;
  144. int res;
  145. if (argc < 3)
  146. return CMD_RET_USAGE;
  147. if (!osd_cur) {
  148. puts("No osd selected\n");
  149. return CMD_RET_FAILURE;
  150. }
  151. x = hextoul(argv[1], NULL);
  152. y = hextoul(argv[2], NULL);
  153. res = video_osd_set_size(osd_cur, x, y);
  154. if (res) {
  155. printf("Could not set size on osd %s\n", osd_cur->name);
  156. return CMD_RET_FAILURE;
  157. }
  158. return CMD_RET_SUCCESS;
  159. }
  160. static int do_show_osd(struct cmd_tbl *cmdtp, int flag, int argc,
  161. char *const argv[])
  162. {
  163. struct udevice *osd;
  164. if (argc == 1) {
  165. /* show all OSDs */
  166. struct uclass *uc;
  167. int res;
  168. res = uclass_get(UCLASS_VIDEO_OSD, &uc);
  169. if (res) {
  170. printf("Error while getting OSD uclass (err=%d)\n",
  171. res);
  172. return CMD_RET_FAILURE;
  173. }
  174. uclass_foreach_dev(osd, uc)
  175. show_osd(osd);
  176. } else {
  177. int i, res;
  178. /* show specific OSD */
  179. i = dectoul(argv[1], NULL);
  180. res = uclass_get_device_by_seq(UCLASS_VIDEO_OSD, i, &osd);
  181. if (res) {
  182. printf("Invalid osd %d: err=%d\n", i, res);
  183. return CMD_RET_FAILURE;
  184. }
  185. show_osd(osd);
  186. }
  187. return CMD_RET_SUCCESS;
  188. }
  189. static int do_osd_num(struct cmd_tbl *cmdtp, int flag, int argc,
  190. char *const argv[])
  191. {
  192. int osd_no;
  193. int res = 0;
  194. if (argc == 1) {
  195. /* querying current setting */
  196. struct udevice *osd;
  197. if (!osd_get_osd_cur(&osd))
  198. osd_no = dev_seq(osd);
  199. else
  200. osd_no = -1;
  201. printf("Current osd is %d\n", osd_no);
  202. } else {
  203. osd_no = dectoul(argv[1], NULL);
  204. printf("Setting osd to %d\n", osd_no);
  205. res = cmd_osd_set_osd_num(osd_no);
  206. if (res)
  207. printf("Failure changing osd number (err = %d)\n", res);
  208. }
  209. return res ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
  210. }
  211. static struct cmd_tbl cmd_osd_sub[] = {
  212. U_BOOT_CMD_MKENT(show, 1, 1, do_show_osd, "", ""),
  213. U_BOOT_CMD_MKENT(dev, 1, 1, do_osd_num, "", ""),
  214. U_BOOT_CMD_MKENT(write, 4, 1, do_osd_write, "", ""),
  215. U_BOOT_CMD_MKENT(print, 4, 1, do_osd_print, "", ""),
  216. U_BOOT_CMD_MKENT(size, 2, 1, do_osd_size, "", ""),
  217. };
  218. static int do_osd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  219. {
  220. struct cmd_tbl *c;
  221. if (argc < 2)
  222. return CMD_RET_USAGE;
  223. /* Strip off leading 'osd' command argument */
  224. argc--;
  225. argv++;
  226. c = find_cmd_tbl(argv[0], &cmd_osd_sub[0], ARRAY_SIZE(cmd_osd_sub));
  227. if (c)
  228. return c->cmd(cmdtp, flag, argc, argv);
  229. else
  230. return CMD_RET_USAGE;
  231. }
  232. static char osd_help_text[] =
  233. "show - show OSD info\n"
  234. "osd dev [dev] - show or set current OSD\n"
  235. "write [pos_x] [pos_y] [buffer] [count] - write 8-bit hex encoded buffer to osd memory at a given position\n"
  236. "print [pos_x] [pos_y] [color] [text] - write ASCII buffer (given by text data and driver-specific color information) to osd memory\n"
  237. "size [size_x] [size_y] - set OSD XY size in characters\n";
  238. U_BOOT_CMD(
  239. osd, 6, 1, do_osd,
  240. "OSD sub-system",
  241. osd_help_text
  242. );