osd.c 6.2 KB

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