| 1 |
/* |
| 2 |
* Media controller test application |
| 3 |
* |
| 4 |
* Copyright (C) 2010-2014 Ideas on board SPRL |
| 5 |
* |
| 6 |
* Contact: Laurent Pinchart <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published |
| 10 |
* by the Free Software Foundation; either version 2.1 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <ctype.h> |
| 23 |
#include <dirent.h> |
| 24 |
#include <fcntl.h> |
| 25 |
#include <getopt.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <sys/ioctl.h> |
| 30 |
#include <unistd.h> |
| 31 |
|
| 32 |
#include <linux/media.h> |
| 33 |
#include <linux/videodev2.h> |
| 34 |
|
| 35 |
#include "options.h" |
| 36 |
#include "v4l2subdev.h" |
| 37 |
|
| 38 |
#define MEDIA_DEVNAME_DEFAULT "/dev/media0" |
| 39 |
|
| 40 |
struct media_options media_opts = { |
| 41 |
.devname = MEDIA_DEVNAME_DEFAULT, |
| 42 |
.which = V4L2_SUBDEV_FORMAT_ACTIVE, |
| 43 |
}; |
| 44 |
|
| 45 |
static void print_version() |
| 46 |
{ |
| 47 |
#define STR(x) #x |
| 48 |
#define STRING(x) STR(x) |
| 49 |
printf("media-ctl %s%s\n", PACKAGE_VERSION, STRING(GIT_COMMIT_CNT)); |
| 50 |
if (strlen(STRING(GIT_SHA))) |
| 51 |
printf("media-ctl SHA: %s %s\n", |
| 52 |
STRING(GIT_SHA), STRING(GIT_COMMIT_DATE)); |
| 53 |
} |
| 54 |
|
| 55 |
static void usage(const char *argv0) |
| 56 |
{ |
| 57 |
unsigned int i; |
| 58 |
|
| 59 |
printf("%s [options]\n", argv0); |
| 60 |
printf("-d, --device dev Media device name (default: %s)\n", MEDIA_DEVNAME_DEFAULT); |
| 61 |
printf(" If <dev> starts with a digit, then /dev/media<dev> is used.\n"); |
| 62 |
printf(" If <dev> doesn't exist, then find a media device that\n"); |
| 63 |
printf(" reports a bus info string equal to <dev>.\n"); |
| 64 |
printf("-e, --entity name Print the device name associated with the given entity\n"); |
| 65 |
printf("-V, --set-v4l2 v4l2 Comma-separated list of formats to setup\n"); |
| 66 |
printf(" --get-v4l2 pad Print the active format on a given pad\n"); |
| 67 |
printf(" --get-dv pad Print detected and current DV timings on a given pad\n"); |
| 68 |
printf(" --set-dv pad Configure DV timings on a given pad\n"); |
| 69 |
printf("-R, --set-routes routes Configure routes on a given subdev entity\n"); |
| 70 |
printf("-h, --help Show verbose help and exit\n"); |
| 71 |
printf("-i, --interactive Modify links interactively\n"); |
| 72 |
printf("-l, --links links Comma-separated list of link descriptors to setup\n"); |
| 73 |
printf(" --known-mbus-fmts List known media bus formats and their numeric values\n"); |
| 74 |
printf("-p, --print-topology Print the device topology. If an entity\n"); |
| 75 |
printf(" is specified through the -e option, print\n"); |
| 76 |
printf(" information for that entity only.\n"); |
| 77 |
printf(" --print-dot Print the device topology as a dot graph\n"); |
| 78 |
printf("-r, --reset Reset all links to inactive\n"); |
| 79 |
printf("-v, --verbose Be verbose\n"); |
| 80 |
printf(" --version Show version information\n"); |
| 81 |
printf("-W, --which which Select the subdev state to operate on\n"); |
| 82 |
printf("\n"); |
| 83 |
printf("Links and formats are defined as\n"); |
| 84 |
printf("\tlinks = link { ',' link } ;\n"); |
| 85 |
printf("\tlink = pad '->' pad '[' flags ']' ;\n"); |
| 86 |
printf("\tpad = entity ':' pad-number { '/' stream-number } ;\n"); |
| 87 |
printf("\tentity = entity-number | ( '\"' entity-name '\"' ) ;\n"); |
| 88 |
printf("\n"); |
| 89 |
printf("\tv4l2 = pad '[' v4l2-properties ']' ;\n"); |
| 90 |
printf("\tv4l2-properties = v4l2-property { ',' v4l2-property } ;\n"); |
| 91 |
printf("\tv4l2-property = v4l2-mbusfmt | v4l2-crop | v4l2-interval\n"); |
| 92 |
printf("\t | v4l2-compose | v4l2-interval ;\n"); |
| 93 |
printf("\tv4l2-mbusfmt = 'fmt:' fcc '/' size ; { 'field:' v4l2-field ; } { 'colorspace:' v4l2-colorspace ; }\n"); |
| 94 |
printf("\t { 'xfer:' v4l2-xfer-func ; } { 'ycbcr:' v4l2-ycbcr-enc ; }\n"); |
| 95 |
printf("\t { 'quantization:' v4l2-quant ; }\n"); |
| 96 |
printf("\tv4l2-crop = 'crop:' rectangle ;\n"); |
| 97 |
printf("\tv4l2-compose = 'compose:' rectangle ;\n"); |
| 98 |
printf("\tv4l2-interval = '@' numerator '/' denominator ;\n"); |
| 99 |
printf("\n"); |
| 100 |
printf("\trectangle = '(' left ',' top, ')' '/' size ;\n"); |
| 101 |
printf("\tsize = width 'x' height ;\n"); |
| 102 |
printf("\n"); |
| 103 |
printf("\troutes = entity '[' route { ',' route } ']' ;\n"); |
| 104 |
printf("\troute = pad-number '/' stream-number '->' pad-number '/' stream-number '[' route-flags ']' ;\n"); |
| 105 |
printf("\n"); |
| 106 |
printf("where the fields are\n"); |
| 107 |
printf("\tentity-number Entity numeric identifier\n"); |
| 108 |
printf("\tentity-name Entity name (string) \n"); |
| 109 |
printf("\tpad-number Pad numeric identifier\n"); |
| 110 |
printf("\tstream-number Stream numeric identifier\n"); |
| 111 |
printf("\tflags Link flags (0: inactive, 1: active)\n"); |
| 112 |
printf("\troute-flags Route flags (bitmask of route flags: active - 0x1)\n"); |
| 113 |
printf("\tfcc Format FourCC\n"); |
| 114 |
printf("\twidth Image width in pixels\n"); |
| 115 |
printf("\theight Image height in pixels\n"); |
| 116 |
printf("\tnumerator Frame interval numerator\n"); |
| 117 |
printf("\tdenominator Frame interval denominator\n"); |
| 118 |
printf("\tv4l2-field One of the following:\n"); |
| 119 |
|
| 120 |
for (i = V4L2_FIELD_ANY; i <= V4L2_FIELD_INTERLACED_BT; i++) |
| 121 |
printf("\t %s\n", |
| 122 |
v4l2_subdev_field_to_string(i)); |
| 123 |
|
| 124 |
printf("\tv4l2-colorspace One of the following:\n"); |
| 125 |
|
| 126 |
for (i = V4L2_COLORSPACE_DEFAULT; i <= V4L2_COLORSPACE_DCI_P3; i++) |
| 127 |
printf("\t %s\n", |
| 128 |
v4l2_subdev_colorspace_to_string(i)); |
| 129 |
|
| 130 |
printf("\tv4l2-xfer-func One of the following:\n"); |
| 131 |
|
| 132 |
for (i = V4L2_XFER_FUNC_DEFAULT; i <= V4L2_XFER_FUNC_SMPTE2084; i++) |
| 133 |
printf("\t %s\n", |
| 134 |
v4l2_subdev_xfer_func_to_string(i)); |
| 135 |
|
| 136 |
printf("\tv4l2-quant One of the following:\n"); |
| 137 |
|
| 138 |
for (i = V4L2_QUANTIZATION_DEFAULT; i <= V4L2_QUANTIZATION_LIM_RANGE; i++) |
| 139 |
printf("\t %s\n", |
| 140 |
v4l2_subdev_quantization_to_string(i)); |
| 141 |
|
| 142 |
printf("\tv4l2-ycbcr-enc One of the following:\n"); |
| 143 |
|
| 144 |
for (i = V4L2_YCBCR_ENC_DEFAULT; i <= V4L2_YCBCR_ENC_SMPTE240M; i++) |
| 145 |
printf("\t %s\n", |
| 146 |
v4l2_subdev_ycbcr_encoding_to_string(i)); |
| 147 |
|
| 148 |
printf("\twhich Subdev state ('active' or 'try')\n"); |
| 149 |
} |
| 150 |
|
| 151 |
#define OPT_PRINT_DOT 256 |
| 152 |
#define OPT_GET_FORMAT 257 |
| 153 |
#define OPT_SET_DV 258 |
| 154 |
#define OPT_LIST_KNOWN_MBUS_FMTS 259 |
| 155 |
#define OPT_GET_DV 260 |
| 156 |
#define OPT_VERSION 261 |
| 157 |
|
| 158 |
static struct option opts[] = { |
| 159 |
{"device", 1, 0, 'd'}, |
| 160 |
{"entity", 1, 0, 'e'}, |
| 161 |
{"set-format", 1, 0, 'f'}, |
| 162 |
{"set-v4l2", 1, 0, 'V'}, |
| 163 |
{"get-format", 1, 0, OPT_GET_FORMAT}, |
| 164 |
{"get-v4l2", 1, 0, OPT_GET_FORMAT}, |
| 165 |
{"get-dv", 1, 0, OPT_GET_DV}, |
| 166 |
{"set-dv", 1, 0, OPT_SET_DV}, |
| 167 |
{"set-routes", 1, 0, 'R'}, |
| 168 |
{"help", 0, 0, 'h'}, |
| 169 |
{"interactive", 0, 0, 'i'}, |
| 170 |
{"links", 1, 0, 'l'}, |
| 171 |
{"known-mbus-fmts", 0, 0, OPT_LIST_KNOWN_MBUS_FMTS}, |
| 172 |
{"print-dot", 0, 0, OPT_PRINT_DOT}, |
| 173 |
{"print-topology", 0, 0, 'p'}, |
| 174 |
{"reset", 0, 0, 'r'}, |
| 175 |
{"verbose", 0, 0, 'v'}, |
| 176 |
{"version", 0, 0, OPT_VERSION}, |
| 177 |
{"which", 1, 0, 'W'}, |
| 178 |
{ }, |
| 179 |
}; |
| 180 |
|
| 181 |
static void list_known_mbus_formats(void) |
| 182 |
{ |
| 183 |
unsigned int ncodes; |
| 184 |
const unsigned int *code = v4l2_subdev_pixelcode_list(&ncodes); |
| 185 |
|
| 186 |
while (ncodes--) { |
| 187 |
const char *str = v4l2_subdev_pixelcode_to_string(*code); |
| 188 |
int spaces = 30 - (int)strlen(str); |
| 189 |
|
| 190 |
if (*code == 0) |
| 191 |
break; |
| 192 |
|
| 193 |
if (spaces < 0) |
| 194 |
spaces = 0; |
| 195 |
|
| 196 |
printf("%s %*c 0x%4.4x\n", str, spaces, ' ', *code); |
| 197 |
|
| 198 |
code++; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
static const char *make_devname(const char *device) |
| 203 |
{ |
| 204 |
static char newdev[300]; |
| 205 |
struct dirent *ep; |
| 206 |
DIR *dp; |
| 207 |
|
| 208 |
if (!access(device, F_OK)) |
| 209 |
return device; |
| 210 |
|
| 211 |
if (device[0] >= '0' && device[0] <= '9' && strlen(device) <= 3) { |
| 212 |
snprintf(newdev, sizeof(newdev), "/dev/media%s", device); |
| 213 |
return newdev; |
| 214 |
} |
| 215 |
|
| 216 |
dp = opendir("/dev"); |
| 217 |
if (dp == NULL) |
| 218 |
return device; |
| 219 |
|
| 220 |
while ((ep = readdir(dp))) { |
| 221 |
const char *name = ep->d_name; |
| 222 |
|
| 223 |
if (!memcmp(name, "media", 5) && isdigit(name[5])) { |
| 224 |
struct media_device_info mdi; |
| 225 |
int ret; |
| 226 |
int fd; |
| 227 |
|
| 228 |
snprintf(newdev, sizeof(newdev), "/dev/%s", name); |
| 229 |
fd = open(newdev, O_RDWR); |
| 230 |
if (fd < 0) |
| 231 |
continue; |
| 232 |
ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi); |
| 233 |
close(fd); |
| 234 |
if (!ret && !strcmp(device, mdi.bus_info)) { |
| 235 |
closedir(dp); |
| 236 |
return newdev; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
closedir(dp); |
| 241 |
return device; |
| 242 |
} |
| 243 |
|
| 244 |
int parse_cmdline(int argc, char **argv) |
| 245 |
{ |
| 246 |
int opt; |
| 247 |
|
| 248 |
if (argc == 1) { |
| 249 |
usage(argv[0]); |
| 250 |
return 1; |
| 251 |
} |
| 252 |
|
| 253 |
/* parse options */ |
| 254 |
while ((opt = getopt_long(argc, argv, "d:e:f:hil:prvV:R:W:", |
| 255 |
opts, NULL)) != -1) { |
| 256 |
switch (opt) { |
| 257 |
case 'd': |
| 258 |
media_opts.devname = make_devname(optarg); |
| 259 |
break; |
| 260 |
|
| 261 |
case 'e': |
| 262 |
media_opts.entity = optarg; |
| 263 |
break; |
| 264 |
|
| 265 |
/* 'f' is supported for backward compatibility reasons and will |
| 266 |
* be removed later. |
| 267 |
*/ |
| 268 |
case 'f': |
| 269 |
fprintf(stderr, "Warning: the -f option is deprecated " |
| 270 |
"and has been replaced by -V.\n"); |
| 271 |
/* fall through */ |
| 272 |
case 'V': |
| 273 |
media_opts.formats = optarg; |
| 274 |
break; |
| 275 |
|
| 276 |
case 'h': |
| 277 |
usage(argv[0]); |
| 278 |
exit(0); |
| 279 |
|
| 280 |
case 'i': |
| 281 |
media_opts.interactive = 1; |
| 282 |
break; |
| 283 |
|
| 284 |
case 'l': |
| 285 |
media_opts.links = optarg; |
| 286 |
break; |
| 287 |
|
| 288 |
case 'p': |
| 289 |
media_opts.print = 1; |
| 290 |
break; |
| 291 |
|
| 292 |
case 'r': |
| 293 |
media_opts.reset = 1; |
| 294 |
break; |
| 295 |
|
| 296 |
case 'v': |
| 297 |
media_opts.verbose = 1; |
| 298 |
break; |
| 299 |
|
| 300 |
case 'R': |
| 301 |
media_opts.routes = optarg; |
| 302 |
break; |
| 303 |
|
| 304 |
case 'W': |
| 305 |
if (!strcmp(optarg, "active")) |
| 306 |
media_opts.which = V4L2_SUBDEV_FORMAT_ACTIVE; |
| 307 |
else if (!strcmp(optarg, "try")) |
| 308 |
media_opts.which = V4L2_SUBDEV_FORMAT_TRY; |
| 309 |
else { |
| 310 |
printf("Invalid 'which' value '%s'\n", optarg); |
| 311 |
return 1; |
| 312 |
} |
| 313 |
break; |
| 314 |
|
| 315 |
case OPT_PRINT_DOT: |
| 316 |
media_opts.print_dot = 1; |
| 317 |
break; |
| 318 |
|
| 319 |
case OPT_GET_FORMAT: |
| 320 |
media_opts.fmt_pad = optarg; |
| 321 |
break; |
| 322 |
|
| 323 |
case OPT_GET_DV: |
| 324 |
media_opts.get_dv_pad = optarg; |
| 325 |
break; |
| 326 |
|
| 327 |
case OPT_SET_DV: |
| 328 |
media_opts.dv_pad = optarg; |
| 329 |
break; |
| 330 |
|
| 331 |
case OPT_LIST_KNOWN_MBUS_FMTS: |
| 332 |
list_known_mbus_formats(); |
| 333 |
exit(0); |
| 334 |
|
| 335 |
case OPT_VERSION: |
| 336 |
print_version(); |
| 337 |
exit(0); |
| 338 |
|
| 339 |
default: |
| 340 |
printf("Invalid option -%c\n", opt); |
| 341 |
printf("Run %s -h for help.\n", argv[0]); |
| 342 |
return 1; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
return 0; |
| 347 |
} |
| 348 |
|