Merge tag 'xilinx-for-v2025.01-rc3-v2' of https://source.denx.de/u-boot/custodians...
[u-boot.git] / cmd / osd.c
blob5671338d9e7bc27173c9852a7b387de1d4dbd881
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2017
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
6 * based on the gdsys osd driver, which is
8 * (C) Copyright 2010
9 * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
12 #include <command.h>
13 #include <dm.h>
14 #include <hexdump.h>
15 #include <video_osd.h>
16 #include <malloc.h>
18 /* Container for selected OSD device */
19 static struct udevice *osd_cur;
21 /**
22 * cmd_osd_set_osd_num() - Set the OSD selected for operation
24 * Set the OSD device, which will be used by all subsequent OSD commands.
26 * Devices are identified by their uclass sequence number (as listed by 'osd
27 * show').
29 * @osdnum: The OSD device to be selected, identified by its sequence number.
30 * Return: 0 if OK, -ve on error
32 static int cmd_osd_set_osd_num(unsigned int osdnum)
34 struct udevice *osd;
35 int res;
37 res = uclass_get_device_by_seq(UCLASS_VIDEO_OSD, osdnum, &osd);
38 if (res) {
39 printf("%s: No OSD %u (err = %d)\n", __func__, osdnum, res);
40 return res;
42 osd_cur = osd;
44 return 0;
47 /**
48 * osd_get_osd_cur() - Get the selected OSD device
50 * Get the OSD device that is used by all OSD commands.
52 * @osdp: Pointer to structure that will receive the currently selected OSD
53 * device.
54 * Return: 0 if OK, -ve on error
56 static int osd_get_osd_cur(struct udevice **osdp)
58 if (!osd_cur) {
59 puts("No osd selected\n");
60 return -ENODEV;
62 *osdp = osd_cur;
64 return 0;
67 /**
68 * show_osd() - Display information about a OSD device
70 * Display a device's ID (sequence number), and whether it is active (i.e.
71 * probed) or not.
73 * @osd: OSD device to print information for
75 static void show_osd(struct udevice *osd)
77 printf("OSD %d:\t%s", dev_seq(osd), osd->name);
78 if (device_active(osd))
79 printf(" (active)");
80 printf("\n");
83 static int do_osd_write(struct cmd_tbl *cmdtp, int flag, int argc,
84 char *const argv[])
86 uint x, y;
87 uint count;
88 char *hexstr;
89 u8 *buffer;
90 size_t buflen;
91 int res;
93 if (argc < 4 || (strlen(argv[3]) % 2))
94 return CMD_RET_USAGE;
96 if (!osd_cur) {
97 puts("No osd selected\n");
98 return CMD_RET_FAILURE;
101 x = hextoul(argv[1], NULL);
102 y = hextoul(argv[2], NULL);
103 hexstr = argv[3];
104 count = (argc > 4) ? hextoul(argv[4], NULL) : 1;
106 buflen = strlen(hexstr) / 2;
108 buffer = malloc(buflen);
109 if (!buffer) {
110 puts("Memory allocation failure\n");
111 return CMD_RET_FAILURE;
114 res = hex2bin(buffer, hexstr, buflen);
115 if (res) {
116 free(buffer);
117 puts("Hexadecimal input contained invalid characters\n");
118 return CMD_RET_FAILURE;
121 res = video_osd_set_mem(osd_cur, x, y, buffer, buflen, count);
122 if (res) {
123 free(buffer);
124 printf("%s: Could not write to video mem\n",
125 osd_cur->name);
126 return CMD_RET_FAILURE;
129 free(buffer);
131 return CMD_RET_SUCCESS;
134 static int do_osd_print(struct cmd_tbl *cmdtp, int flag, int argc,
135 char *const argv[])
137 uint x, y;
138 u8 color;
139 char *text;
140 int res;
142 if (argc < 5)
143 return CMD_RET_USAGE;
145 if (!osd_cur) {
146 puts("No osd selected\n");
147 return CMD_RET_FAILURE;
150 x = hextoul(argv[1], NULL);
151 y = hextoul(argv[2], NULL);
152 color = hextoul(argv[3], NULL);
153 text = argv[4];
155 res = video_osd_print(osd_cur, x, y, color, text);
156 if (res) {
157 printf("Could not print string to osd %s\n", osd_cur->name);
158 return CMD_RET_FAILURE;
161 return CMD_RET_SUCCESS;
164 static int do_osd_size(struct cmd_tbl *cmdtp, int flag, int argc,
165 char *const argv[])
167 uint x, y;
168 int res;
170 if (argc < 3)
171 return CMD_RET_USAGE;
173 if (!osd_cur) {
174 puts("No osd selected\n");
175 return CMD_RET_FAILURE;
178 x = hextoul(argv[1], NULL);
179 y = hextoul(argv[2], NULL);
181 res = video_osd_set_size(osd_cur, x, y);
182 if (res) {
183 printf("Could not set size on osd %s\n", osd_cur->name);
184 return CMD_RET_FAILURE;
187 return CMD_RET_SUCCESS;
190 static int do_show_osd(struct cmd_tbl *cmdtp, int flag, int argc,
191 char *const argv[])
193 struct udevice *osd;
195 if (argc == 1) {
196 /* show all OSDs */
197 struct uclass *uc;
198 int res;
200 res = uclass_get(UCLASS_VIDEO_OSD, &uc);
201 if (res) {
202 printf("Error while getting OSD uclass (err=%d)\n",
203 res);
204 return CMD_RET_FAILURE;
207 uclass_foreach_dev(osd, uc)
208 show_osd(osd);
209 } else {
210 int i, res;
212 /* show specific OSD */
213 i = dectoul(argv[1], NULL);
215 res = uclass_get_device_by_seq(UCLASS_VIDEO_OSD, i, &osd);
216 if (res) {
217 printf("Invalid osd %d: err=%d\n", i, res);
218 return CMD_RET_FAILURE;
220 show_osd(osd);
223 return CMD_RET_SUCCESS;
226 static int do_osd_num(struct cmd_tbl *cmdtp, int flag, int argc,
227 char *const argv[])
229 int osd_no;
230 int res = 0;
232 if (argc == 1) {
233 /* querying current setting */
234 struct udevice *osd;
236 if (!osd_get_osd_cur(&osd))
237 osd_no = dev_seq(osd);
238 else
239 osd_no = -1;
240 printf("Current osd is %d\n", osd_no);
241 } else {
242 osd_no = dectoul(argv[1], NULL);
243 printf("Setting osd to %d\n", osd_no);
245 res = cmd_osd_set_osd_num(osd_no);
246 if (res)
247 printf("Failure changing osd number (err = %d)\n", res);
250 return res ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
253 static struct cmd_tbl cmd_osd_sub[] = {
254 U_BOOT_CMD_MKENT(show, 1, 1, do_show_osd, "", ""),
255 U_BOOT_CMD_MKENT(dev, 1, 1, do_osd_num, "", ""),
256 U_BOOT_CMD_MKENT(write, 4, 1, do_osd_write, "", ""),
257 U_BOOT_CMD_MKENT(print, 4, 1, do_osd_print, "", ""),
258 U_BOOT_CMD_MKENT(size, 2, 1, do_osd_size, "", ""),
261 static int do_osd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
263 struct cmd_tbl *c;
265 if (argc < 2)
266 return CMD_RET_USAGE;
268 /* Strip off leading 'osd' command argument */
269 argc--;
270 argv++;
272 c = find_cmd_tbl(argv[0], &cmd_osd_sub[0], ARRAY_SIZE(cmd_osd_sub));
274 if (c)
275 return c->cmd(cmdtp, flag, argc, argv);
276 else
277 return CMD_RET_USAGE;
280 U_BOOT_LONGHELP(osd,
281 "show - show OSD info\n"
282 "osd dev [dev] - show or set current OSD\n"
283 "write [pos_x] [pos_y] [buffer] [count] - write 8-bit hex encoded buffer to osd memory at a given position\n"
284 "print [pos_x] [pos_y] [color] [text] - write ASCII buffer (given by text data and driver-specific color information) to osd memory\n"
285 "size [size_x] [size_y] - set OSD XY size in characters\n");
287 U_BOOT_CMD(
288 osd, 6, 1, do_osd,
289 "OSD sub-system",
290 osd_help_text