| 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 <sys/ioctl.h> |
| 23 |
#include <sys/mman.h> |
| 24 |
#include <sys/stat.h> |
| 25 |
#include <sys/time.h> |
| 26 |
|
| 27 |
#include <ctype.h> |
| 28 |
#include <errno.h> |
| 29 |
#include <fcntl.h> |
| 30 |
#include <stdbool.h> |
| 31 |
#include <stdint.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <unistd.h> |
| 36 |
|
| 37 |
#include <linux/media.h> |
| 38 |
#include <linux/types.h> |
| 39 |
#include <linux/v4l2-mediabus.h> |
| 40 |
#include <linux/v4l2-subdev.h> |
| 41 |
#include <linux/videodev2.h> |
| 42 |
|
| 43 |
#include "mediactl.h" |
| 44 |
#include "options.h" |
| 45 |
#include "tools.h" |
| 46 |
#include "v4l2subdev.h" |
| 47 |
|
| 48 |
/* ----------------------------------------------------------------------------- |
| 49 |
* Printing |
| 50 |
*/ |
| 51 |
|
| 52 |
struct flag_name { |
| 53 |
__u32 flag; |
| 54 |
char *name; |
| 55 |
}; |
| 56 |
|
| 57 |
static void print_flags(const struct flag_name *flag_names, unsigned int num_entries, __u32 flags) |
| 58 |
{ |
| 59 |
bool first = true; |
| 60 |
unsigned int i; |
| 61 |
|
| 62 |
for (i = 0; i < num_entries; i++) { |
| 63 |
if (!(flags & flag_names[i].flag)) |
| 64 |
continue; |
| 65 |
if (!first) |
| 66 |
printf(","); |
| 67 |
printf("%s", flag_names[i].name); |
| 68 |
flags &= ~flag_names[i].flag; |
| 69 |
first = false; |
| 70 |
} |
| 71 |
|
| 72 |
if (flags) { |
| 73 |
if (!first) |
| 74 |
printf(","); |
| 75 |
printf("0x%x", flags); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
static void v4l2_subdev_print_routes(struct media_entity *entity, |
| 80 |
struct v4l2_subdev_route *routes, |
| 81 |
unsigned int num_routes) |
| 82 |
{ |
| 83 |
unsigned int i; |
| 84 |
|
| 85 |
if (num_routes) |
| 86 |
printf("\troutes:\n"); |
| 87 |
|
| 88 |
for (i = 0; i < num_routes; i++) { |
| 89 |
const struct v4l2_subdev_route *route = &routes[i]; |
| 90 |
|
| 91 |
printf("\t\t%u/%u -> %u/%u [%s]\n", |
| 92 |
route->sink_pad, route->sink_stream, |
| 93 |
route->source_pad, route->source_stream, |
| 94 |
route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE ? "ACTIVE" : "INACTIVE"); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
static void v4l2_subdev_print_format(struct media_entity *entity, |
| 99 |
unsigned int pad, unsigned int stream, |
| 100 |
enum v4l2_subdev_format_whence which) |
| 101 |
{ |
| 102 |
struct v4l2_mbus_framefmt format; |
| 103 |
struct v4l2_fract interval = { 0, 0 }; |
| 104 |
struct v4l2_rect rect; |
| 105 |
int ret; |
| 106 |
|
| 107 |
ret = v4l2_subdev_get_format(entity, &format, pad, stream, which); |
| 108 |
if (ret != 0) |
| 109 |
return; |
| 110 |
|
| 111 |
ret = v4l2_subdev_get_frame_interval(entity, &interval, pad, stream, |
| 112 |
which); |
| 113 |
if (ret != 0 && ret != -ENOTTY && ret != -EINVAL) |
| 114 |
return; |
| 115 |
|
| 116 |
printf("\t\t[stream:%u fmt:%s/%ux%u", stream, |
| 117 |
v4l2_subdev_pixelcode_to_string(format.code), |
| 118 |
format.width, format.height); |
| 119 |
|
| 120 |
if (interval.numerator || interval.denominator) |
| 121 |
printf("@%u/%u", interval.numerator, interval.denominator); |
| 122 |
|
| 123 |
if (format.field) |
| 124 |
printf(" field:%s", v4l2_subdev_field_to_string(format.field)); |
| 125 |
|
| 126 |
if (format.colorspace) { |
| 127 |
printf(" colorspace:%s", |
| 128 |
v4l2_subdev_colorspace_to_string(format.colorspace)); |
| 129 |
|
| 130 |
if (format.xfer_func) |
| 131 |
printf(" xfer:%s", |
| 132 |
v4l2_subdev_xfer_func_to_string(format.xfer_func)); |
| 133 |
|
| 134 |
if (format.ycbcr_enc) |
| 135 |
printf(" ycbcr:%s", |
| 136 |
v4l2_subdev_ycbcr_encoding_to_string(format.ycbcr_enc)); |
| 137 |
|
| 138 |
if (format.quantization) |
| 139 |
printf(" quantization:%s", |
| 140 |
v4l2_subdev_quantization_to_string(format.quantization)); |
| 141 |
} |
| 142 |
|
| 143 |
ret = v4l2_subdev_get_selection(entity, &rect, pad, stream, |
| 144 |
V4L2_SEL_TGT_CROP_BOUNDS, |
| 145 |
which); |
| 146 |
if (ret == 0) |
| 147 |
printf("\n\t\t crop.bounds:(%d,%d)/%ux%u", rect.left, rect.top, |
| 148 |
rect.width, rect.height); |
| 149 |
|
| 150 |
ret = v4l2_subdev_get_selection(entity, &rect, pad, stream, |
| 151 |
V4L2_SEL_TGT_CROP, |
| 152 |
which); |
| 153 |
if (ret == 0) |
| 154 |
printf("\n\t\t crop:(%d,%d)/%ux%u", rect.left, rect.top, |
| 155 |
rect.width, rect.height); |
| 156 |
|
| 157 |
ret = v4l2_subdev_get_selection(entity, &rect, pad, stream, |
| 158 |
V4L2_SEL_TGT_COMPOSE_BOUNDS, |
| 159 |
which); |
| 160 |
if (ret == 0) |
| 161 |
printf("\n\t\t compose.bounds:(%d,%d)/%ux%u", |
| 162 |
rect.left, rect.top, rect.width, rect.height); |
| 163 |
|
| 164 |
ret = v4l2_subdev_get_selection(entity, &rect, pad, stream, |
| 165 |
V4L2_SEL_TGT_COMPOSE, |
| 166 |
which); |
| 167 |
if (ret == 0) |
| 168 |
printf("\n\t\t compose:(%d,%d)/%ux%u", |
| 169 |
rect.left, rect.top, rect.width, rect.height); |
| 170 |
|
| 171 |
printf("]\n"); |
| 172 |
} |
| 173 |
|
| 174 |
static const char *v4l2_dv_type_to_string(unsigned int type) |
| 175 |
{ |
| 176 |
static const struct { |
| 177 |
__u32 type; |
| 178 |
const char *name; |
| 179 |
} types[] = { |
| 180 |
{ V4L2_DV_BT_656_1120, "BT.656/1120" }, |
| 181 |
}; |
| 182 |
|
| 183 |
static char unknown[21]; |
| 184 |
unsigned int i; |
| 185 |
|
| 186 |
for (i = 0; i < ARRAY_SIZE(types); i++) { |
| 187 |
if (types[i].type == type) |
| 188 |
return types[i].name; |
| 189 |
} |
| 190 |
|
| 191 |
sprintf(unknown, "Unknown (%u)", type); |
| 192 |
return unknown; |
| 193 |
} |
| 194 |
|
| 195 |
static const struct flag_name bt_standards[] = { |
| 196 |
{ V4L2_DV_BT_STD_CEA861, "CEA-861" }, |
| 197 |
{ V4L2_DV_BT_STD_DMT, "DMT" }, |
| 198 |
{ V4L2_DV_BT_STD_CVT, "CVT" }, |
| 199 |
{ V4L2_DV_BT_STD_GTF, "GTF" }, |
| 200 |
{ V4L2_DV_BT_STD_SDI, "SDI" }, |
| 201 |
}; |
| 202 |
|
| 203 |
static const struct flag_name bt_capabilities[] = { |
| 204 |
{ V4L2_DV_BT_CAP_INTERLACED, "interlaced" }, |
| 205 |
{ V4L2_DV_BT_CAP_PROGRESSIVE, "progressive" }, |
| 206 |
{ V4L2_DV_BT_CAP_REDUCED_BLANKING, "reduced-blanking" }, |
| 207 |
{ V4L2_DV_BT_CAP_CUSTOM, "custom" }, |
| 208 |
}; |
| 209 |
|
| 210 |
static const struct flag_name bt_flags[] = { |
| 211 |
{ V4L2_DV_FL_REDUCED_BLANKING, "reduced-blanking" }, |
| 212 |
{ V4L2_DV_FL_CAN_REDUCE_FPS, "can-reduce-fps" }, |
| 213 |
{ V4L2_DV_FL_REDUCED_FPS, "reduced-fps" }, |
| 214 |
{ V4L2_DV_FL_HALF_LINE, "half-line" }, |
| 215 |
{ V4L2_DV_FL_IS_CE_VIDEO, "CE-video" }, |
| 216 |
{ V4L2_DV_FL_FIRST_FIELD_EXTRA_LINE, "first-field-extra-line" }, |
| 217 |
{ V4L2_DV_FL_HAS_PICTURE_ASPECT, "has-picture-aspect" }, |
| 218 |
{ V4L2_DV_FL_HAS_CEA861_VIC, "has-cea861-vic" }, |
| 219 |
{ V4L2_DV_FL_HAS_HDMI_VIC, "has-hdmi-vic" }, |
| 220 |
{ V4L2_DV_FL_CAN_DETECT_REDUCED_FPS, "can-detect-reduced-fps" }, |
| 221 |
}; |
| 222 |
|
| 223 |
static void v4l2_subdev_print_dv_timings(const struct v4l2_dv_timings *timings, |
| 224 |
const char *name) |
| 225 |
{ |
| 226 |
printf("\t\t[dv.%s:%s", name, v4l2_dv_type_to_string(timings->type)); |
| 227 |
|
| 228 |
switch (timings->type) { |
| 229 |
case V4L2_DV_BT_656_1120: { |
| 230 |
const struct v4l2_bt_timings *bt = &timings->bt; |
| 231 |
unsigned int htotal, vtotal; |
| 232 |
|
| 233 |
htotal = V4L2_DV_BT_FRAME_WIDTH(bt); |
| 234 |
vtotal = V4L2_DV_BT_FRAME_HEIGHT(bt); |
| 235 |
|
| 236 |
printf(" %ux%u%s%llu (%ux%u)", |
| 237 |
bt->width, bt->height, bt->interlaced ? "i" : "p", |
| 238 |
(htotal * vtotal) > 0 ? (bt->pixelclock / (htotal * vtotal)) : 0ULL, |
| 239 |
htotal, vtotal); |
| 240 |
|
| 241 |
printf(" stds:"); |
| 242 |
print_flags(bt_standards, ARRAY_SIZE(bt_standards), |
| 243 |
bt->standards); |
| 244 |
printf(" flags:"); |
| 245 |
print_flags(bt_flags, ARRAY_SIZE(bt_flags), |
| 246 |
bt->flags); |
| 247 |
|
| 248 |
break; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
printf("]\n"); |
| 253 |
} |
| 254 |
|
| 255 |
static void v4l2_subdev_print_pad_dv(struct media_entity *entity, |
| 256 |
unsigned int pad, enum v4l2_subdev_format_whence which) |
| 257 |
{ |
| 258 |
struct v4l2_dv_timings_cap caps; |
| 259 |
int ret; |
| 260 |
|
| 261 |
caps.pad = pad; |
| 262 |
ret = v4l2_subdev_get_dv_timings_caps(entity, &caps); |
| 263 |
if (ret != 0) |
| 264 |
return; |
| 265 |
|
| 266 |
printf("\t\t[dv.caps:%s", v4l2_dv_type_to_string(caps.type)); |
| 267 |
|
| 268 |
switch (caps.type) { |
| 269 |
case V4L2_DV_BT_656_1120: |
| 270 |
printf(" min:%ux%u@%llu max:%ux%u@%llu", |
| 271 |
caps.bt.min_width, caps.bt.min_height, caps.bt.min_pixelclock, |
| 272 |
caps.bt.max_width, caps.bt.max_height, caps.bt.max_pixelclock); |
| 273 |
|
| 274 |
printf(" stds:"); |
| 275 |
print_flags(bt_standards, ARRAY_SIZE(bt_standards), |
| 276 |
caps.bt.standards); |
| 277 |
printf(" caps:"); |
| 278 |
print_flags(bt_capabilities, ARRAY_SIZE(bt_capabilities), |
| 279 |
caps.bt.capabilities); |
| 280 |
|
| 281 |
break; |
| 282 |
} |
| 283 |
|
| 284 |
printf("]\n"); |
| 285 |
} |
| 286 |
|
| 287 |
static void v4l2_subdev_print_subdev_dv(struct media_entity *entity) |
| 288 |
{ |
| 289 |
struct v4l2_dv_timings timings; |
| 290 |
int ret; |
| 291 |
|
| 292 |
ret = v4l2_subdev_query_dv_timings(entity, &timings); |
| 293 |
switch (ret) { |
| 294 |
case -ENOLINK: |
| 295 |
printf("\t\t[dv.query:no-link]\n"); |
| 296 |
break; |
| 297 |
case -ENOLCK: |
| 298 |
printf("\t\t[dv.query:no-lock]\n"); |
| 299 |
break; |
| 300 |
case -ERANGE: |
| 301 |
printf("\t\t[dv.query:out-of-range]\n"); |
| 302 |
break; |
| 303 |
case 0: |
| 304 |
v4l2_subdev_print_dv_timings(&timings, "detect"); |
| 305 |
break; |
| 306 |
default: |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
ret = v4l2_subdev_get_dv_timings(entity, &timings); |
| 311 |
if (ret == 0) |
| 312 |
v4l2_subdev_print_dv_timings(&timings, "current"); |
| 313 |
} |
| 314 |
|
| 315 |
static const char *media_entity_type_to_string(unsigned type) |
| 316 |
{ |
| 317 |
static const struct { |
| 318 |
__u32 type; |
| 319 |
const char *name; |
| 320 |
} types[] = { |
| 321 |
{ MEDIA_ENT_T_DEVNODE, "Node" }, |
| 322 |
{ MEDIA_ENT_T_V4L2_SUBDEV, "V4L2 subdev" }, |
| 323 |
}; |
| 324 |
|
| 325 |
unsigned int i; |
| 326 |
|
| 327 |
type &= MEDIA_ENT_TYPE_MASK; |
| 328 |
|
| 329 |
for (i = 0; i < ARRAY_SIZE(types); i++) { |
| 330 |
if (types[i].type == type) |
| 331 |
return types[i].name; |
| 332 |
} |
| 333 |
|
| 334 |
return "Unknown"; |
| 335 |
} |
| 336 |
|
| 337 |
static const char *media_entity_subtype_to_string(unsigned type) |
| 338 |
{ |
| 339 |
static const char *node_types[] = { |
| 340 |
"Unknown", |
| 341 |
"V4L", |
| 342 |
"FB", |
| 343 |
"ALSA", |
| 344 |
"DVB", |
| 345 |
}; |
| 346 |
static const char *subdev_types[] = { |
| 347 |
"Unknown", |
| 348 |
"Sensor", |
| 349 |
"Flash", |
| 350 |
"Lens", |
| 351 |
"Decoder", |
| 352 |
"Tuner", |
| 353 |
}; |
| 354 |
|
| 355 |
unsigned int subtype = type & MEDIA_ENT_SUBTYPE_MASK; |
| 356 |
|
| 357 |
switch (type & MEDIA_ENT_TYPE_MASK) { |
| 358 |
case MEDIA_ENT_T_DEVNODE: |
| 359 |
if (subtype >= ARRAY_SIZE(node_types)) |
| 360 |
subtype = 0; |
| 361 |
return node_types[subtype]; |
| 362 |
|
| 363 |
case MEDIA_ENT_T_V4L2_SUBDEV: |
| 364 |
if (subtype >= ARRAY_SIZE(subdev_types)) |
| 365 |
subtype = 0; |
| 366 |
return subdev_types[subtype]; |
| 367 |
default: |
| 368 |
return node_types[0]; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
static void media_print_topology_dot(struct media_device *media) |
| 373 |
{ |
| 374 |
unsigned int nents = media_get_entities_count(media); |
| 375 |
unsigned int i, j; |
| 376 |
|
| 377 |
printf("digraph board {\n"); |
| 378 |
printf("\trankdir=TB\n"); |
| 379 |
|
| 380 |
for (i = 0; i < nents; ++i) { |
| 381 |
struct media_entity *entity = media_get_entity(media, i); |
| 382 |
const struct media_entity_desc *info = media_entity_get_info(entity); |
| 383 |
const char *devname = media_entity_get_devname(entity); |
| 384 |
unsigned int num_links = media_entity_get_links_count(entity); |
| 385 |
unsigned int npads; |
| 386 |
|
| 387 |
if (!devname) |
| 388 |
devname = ""; |
| 389 |
|
| 390 |
switch (media_entity_type(entity)) { |
| 391 |
case MEDIA_ENT_T_DEVNODE: |
| 392 |
printf("\tn%08x [label=\"%s\\n%s\", shape=box, style=filled, " |
| 393 |
"fillcolor=yellow]\n", |
| 394 |
info->id, info->name, devname); |
| 395 |
break; |
| 396 |
|
| 397 |
case MEDIA_ENT_T_V4L2_SUBDEV: |
| 398 |
printf("\tn%08x [label=\"{{", info->id); |
| 399 |
|
| 400 |
for (j = 0, npads = 0; j < info->pads; ++j) { |
| 401 |
const struct media_pad *pad = media_entity_get_pad(entity, j); |
| 402 |
|
| 403 |
if (!(pad->flags & MEDIA_PAD_FL_SINK)) |
| 404 |
continue; |
| 405 |
|
| 406 |
printf("%s<port%u> %u", npads ? " | " : "", j, j); |
| 407 |
npads++; |
| 408 |
} |
| 409 |
|
| 410 |
printf("} | %s", info->name); |
| 411 |
if (devname) |
| 412 |
printf("\\n%s", devname); |
| 413 |
printf(" | {"); |
| 414 |
|
| 415 |
for (j = 0, npads = 0; j < info->pads; ++j) { |
| 416 |
const struct media_pad *pad = media_entity_get_pad(entity, j); |
| 417 |
|
| 418 |
if (!(pad->flags & MEDIA_PAD_FL_SOURCE)) |
| 419 |
continue; |
| 420 |
|
| 421 |
printf("%s<port%u> %u", npads ? " | " : "", j, j); |
| 422 |
npads++; |
| 423 |
} |
| 424 |
|
| 425 |
printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n"); |
| 426 |
break; |
| 427 |
|
| 428 |
default: |
| 429 |
continue; |
| 430 |
} |
| 431 |
|
| 432 |
for (j = 0; j < num_links; j++) { |
| 433 |
const struct media_link *link = media_entity_get_link(entity, j); |
| 434 |
const struct media_pad *source = link->source; |
| 435 |
const struct media_pad *sink = link->sink; |
| 436 |
|
| 437 |
if (source->entity != entity) |
| 438 |
continue; |
| 439 |
|
| 440 |
printf("\tn%08x", media_entity_get_info(source->entity)->id); |
| 441 |
if (media_entity_type(source->entity) == MEDIA_ENT_T_V4L2_SUBDEV) |
| 442 |
printf(":port%u", source->index); |
| 443 |
printf(" -> "); |
| 444 |
printf("n%08x", media_entity_get_info(sink->entity)->id); |
| 445 |
if (media_entity_type(sink->entity) == MEDIA_ENT_T_V4L2_SUBDEV) |
| 446 |
printf(":port%u", sink->index); |
| 447 |
|
| 448 |
if (link->flags & MEDIA_LNK_FL_IMMUTABLE) |
| 449 |
printf(" [style=bold]"); |
| 450 |
else if (!(link->flags & MEDIA_LNK_FL_ENABLED)) |
| 451 |
printf(" [style=dashed]"); |
| 452 |
printf("\n"); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
printf("}\n"); |
| 457 |
} |
| 458 |
|
| 459 |
static void media_print_pad_text(struct media_entity *entity, |
| 460 |
const struct media_pad *pad, |
| 461 |
struct v4l2_subdev_route *routes, |
| 462 |
unsigned int num_routes, |
| 463 |
enum v4l2_subdev_format_whence which) |
| 464 |
{ |
| 465 |
uint64_t printed_streams_mask = 0; |
| 466 |
unsigned int i; |
| 467 |
|
| 468 |
if (media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
| 469 |
return; |
| 470 |
|
| 471 |
if (!routes) { |
| 472 |
v4l2_subdev_print_format(entity, pad->index, 0, which); |
| 473 |
} else { |
| 474 |
for (i = 0; i < num_routes; ++i) { |
| 475 |
const struct v4l2_subdev_route *route = &routes[i]; |
| 476 |
unsigned int stream; |
| 477 |
|
| 478 |
if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE)) |
| 479 |
continue; |
| 480 |
|
| 481 |
if (pad->flags & MEDIA_PAD_FL_SINK) { |
| 482 |
if (route->sink_pad != pad->index) |
| 483 |
continue; |
| 484 |
|
| 485 |
stream = route->sink_stream; |
| 486 |
} else { |
| 487 |
if (route->source_pad != pad->index) |
| 488 |
continue; |
| 489 |
|
| 490 |
stream = route->source_stream; |
| 491 |
} |
| 492 |
|
| 493 |
if (printed_streams_mask & (1ULL << stream)) |
| 494 |
continue; |
| 495 |
|
| 496 |
v4l2_subdev_print_format(entity, pad->index, stream, |
| 497 |
which); |
| 498 |
|
| 499 |
printed_streams_mask |= (1ULL << stream); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
v4l2_subdev_print_pad_dv(entity, pad->index, which); |
| 504 |
|
| 505 |
if (pad->flags & MEDIA_PAD_FL_SOURCE) |
| 506 |
v4l2_subdev_print_subdev_dv(entity); |
| 507 |
} |
| 508 |
|
| 509 |
static void media_print_topology_text_entity(struct media_device *media, |
| 510 |
struct media_entity *entity, |
| 511 |
enum v4l2_subdev_format_whence which) |
| 512 |
{ |
| 513 |
static const struct flag_name link_flags[] = { |
| 514 |
{ MEDIA_LNK_FL_ENABLED, "ENABLED" }, |
| 515 |
{ MEDIA_LNK_FL_IMMUTABLE, "IMMUTABLE" }, |
| 516 |
{ MEDIA_LNK_FL_DYNAMIC, "DYNAMIC" }, |
| 517 |
}; |
| 518 |
static const struct flag_name pad_flags[] = { |
| 519 |
{ MEDIA_PAD_FL_SINK, "SINK" }, |
| 520 |
{ MEDIA_PAD_FL_SOURCE, "SOURCE" }, |
| 521 |
{ MEDIA_PAD_FL_MUST_CONNECT, "MUST_CONNECT" }, |
| 522 |
}; |
| 523 |
const struct media_entity_desc *info = media_entity_get_info(entity); |
| 524 |
const char *devname = media_entity_get_devname(entity); |
| 525 |
unsigned int num_links = media_entity_get_links_count(entity); |
| 526 |
struct v4l2_subdev_route *routes = NULL; |
| 527 |
unsigned int num_routes = 0; |
| 528 |
unsigned int j, k; |
| 529 |
unsigned int padding; |
| 530 |
|
| 531 |
if (media_entity_type(entity) == MEDIA_ENT_T_V4L2_SUBDEV) |
| 532 |
v4l2_subdev_get_routing(entity, &routes, &num_routes, which); |
| 533 |
|
| 534 |
padding = printf("- entity %u: ", info->id); |
| 535 |
printf("%s (%u pad%s, %u link%s", info->name, |
| 536 |
info->pads, info->pads > 1 ? "s" : "", |
| 537 |
num_links, num_links > 1 ? "s" : ""); |
| 538 |
|
| 539 |
if (media_entity_type(entity) == MEDIA_ENT_T_V4L2_SUBDEV) |
| 540 |
printf(", %u route%s", num_routes, num_routes != 1 ? "s" : ""); |
| 541 |
|
| 542 |
printf(")\n"); |
| 543 |
|
| 544 |
printf("%*ctype %s subtype %s flags %x\n", padding, ' ', |
| 545 |
media_entity_type_to_string(info->type), |
| 546 |
media_entity_subtype_to_string(info->type), |
| 547 |
info->flags); |
| 548 |
if (devname) |
| 549 |
printf("%*cdevice node name %s\n", padding, ' ', devname); |
| 550 |
|
| 551 |
if (media_entity_type(entity) == MEDIA_ENT_T_V4L2_SUBDEV) |
| 552 |
v4l2_subdev_print_routes(entity, routes, num_routes); |
| 553 |
|
| 554 |
for (j = 0; j < info->pads; j++) { |
| 555 |
const struct media_pad *pad = media_entity_get_pad(entity, j); |
| 556 |
|
| 557 |
printf("\tpad%u: ", j); |
| 558 |
print_flags(pad_flags, ARRAY_SIZE(pad_flags), pad->flags); |
| 559 |
printf("\n"); |
| 560 |
media_print_pad_text(entity, pad, routes, num_routes, which); |
| 561 |
|
| 562 |
for (k = 0; k < num_links; k++) { |
| 563 |
const struct media_link *link = media_entity_get_link(entity, k); |
| 564 |
const struct media_pad *source = link->source; |
| 565 |
const struct media_pad *sink = link->sink; |
| 566 |
|
| 567 |
if (source->entity == entity && source->index == j) |
| 568 |
printf("\t\t-> \"%s\":%u [", |
| 569 |
media_entity_get_info(sink->entity)->name, |
| 570 |
sink->index); |
| 571 |
else if (sink->entity == entity && sink->index == j) |
| 572 |
printf("\t\t<- \"%s\":%u [", |
| 573 |
media_entity_get_info(source->entity)->name, |
| 574 |
source->index); |
| 575 |
else |
| 576 |
continue; |
| 577 |
|
| 578 |
print_flags(link_flags, ARRAY_SIZE(link_flags), link->flags); |
| 579 |
|
| 580 |
printf("]\n"); |
| 581 |
} |
| 582 |
} |
| 583 |
printf("\n"); |
| 584 |
|
| 585 |
free(routes); |
| 586 |
} |
| 587 |
|
| 588 |
static void media_print_topology_text(struct media_device *media, |
| 589 |
enum v4l2_subdev_format_whence which) |
| 590 |
{ |
| 591 |
unsigned int nents = media_get_entities_count(media); |
| 592 |
unsigned int i; |
| 593 |
|
| 594 |
printf("Device topology\n"); |
| 595 |
|
| 596 |
for (i = 0; i < nents; ++i) |
| 597 |
media_print_topology_text_entity( |
| 598 |
media, media_get_entity(media, i), which); |
| 599 |
} |
| 600 |
|
| 601 |
int main(int argc, char **argv) |
| 602 |
{ |
| 603 |
struct media_device *media; |
| 604 |
struct media_entity *entity = NULL; |
| 605 |
int ret = -1; |
| 606 |
|
| 607 |
if (parse_cmdline(argc, argv)) |
| 608 |
return EXIT_FAILURE; |
| 609 |
|
| 610 |
media = media_device_new(media_opts.devname); |
| 611 |
if (media == NULL) { |
| 612 |
printf("Failed to create media device\n"); |
| 613 |
goto out; |
| 614 |
} |
| 615 |
|
| 616 |
if (media_opts.verbose) |
| 617 |
media_debug_set_handler(media, |
| 618 |
(void (*)(void *, ...))fprintf, stdout); |
| 619 |
|
| 620 |
/* Enumerate entities, pads and links. */ |
| 621 |
ret = media_device_enumerate(media); |
| 622 |
if (ret < 0) { |
| 623 |
printf("Failed to enumerate %s (%d)\n", media_opts.devname, ret); |
| 624 |
goto out; |
| 625 |
} |
| 626 |
|
| 627 |
if (media_opts.print) { |
| 628 |
const struct media_device_info *info = media_get_info(media); |
| 629 |
|
| 630 |
printf("Media controller API version %u.%u.%u\n\n", |
| 631 |
(info->media_version >> 16) & 0xff, |
| 632 |
(info->media_version >> 8) & 0xff, |
| 633 |
(info->media_version >> 0) & 0xff); |
| 634 |
printf("Media device information\n" |
| 635 |
"------------------------\n" |
| 636 |
"driver %s\n" |
| 637 |
"model %s\n" |
| 638 |
"serial %s\n" |
| 639 |
"bus info %s\n" |
| 640 |
"hw revision 0x%x\n" |
| 641 |
"driver version %u.%u.%u\n\n", |
| 642 |
info->driver, info->model, |
| 643 |
info->serial, info->bus_info, |
| 644 |
info->hw_revision, |
| 645 |
(info->driver_version >> 16) & 0xff, |
| 646 |
(info->driver_version >> 8) & 0xff, |
| 647 |
(info->driver_version >> 0) & 0xff); |
| 648 |
} |
| 649 |
|
| 650 |
if (media_opts.entity) { |
| 651 |
entity = media_get_entity_by_name(media, media_opts.entity); |
| 652 |
if (entity == NULL) { |
| 653 |
printf("Entity '%s' not found\n", media_opts.entity); |
| 654 |
goto out; |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
if (media_opts.fmt_pad) { |
| 659 |
struct media_pad *pad; |
| 660 |
unsigned int stream; |
| 661 |
char *p; |
| 662 |
|
| 663 |
pad = media_parse_pad_stream(media, media_opts.fmt_pad, &stream, &p); |
| 664 |
if (pad == NULL) { |
| 665 |
printf("Pad '%s' not found\n", media_opts.fmt_pad); |
| 666 |
goto out; |
| 667 |
} |
| 668 |
|
| 669 |
v4l2_subdev_print_format(pad->entity, pad->index, stream, |
| 670 |
media_opts.which); |
| 671 |
} |
| 672 |
|
| 673 |
if (media_opts.get_dv_pad) { |
| 674 |
struct media_pad *pad; |
| 675 |
|
| 676 |
pad = media_parse_pad(media, media_opts.get_dv_pad, NULL); |
| 677 |
if (pad == NULL) { |
| 678 |
printf("Pad '%s' not found\n", media_opts.get_dv_pad); |
| 679 |
goto out; |
| 680 |
} |
| 681 |
|
| 682 |
v4l2_subdev_print_subdev_dv(pad->entity); |
| 683 |
} |
| 684 |
|
| 685 |
if (media_opts.dv_pad) { |
| 686 |
struct v4l2_dv_timings timings; |
| 687 |
struct media_pad *pad; |
| 688 |
|
| 689 |
pad = media_parse_pad(media, media_opts.dv_pad, NULL); |
| 690 |
if (pad == NULL) { |
| 691 |
printf("Pad '%s' not found\n", media_opts.dv_pad); |
| 692 |
goto out; |
| 693 |
} |
| 694 |
|
| 695 |
ret = v4l2_subdev_query_dv_timings(pad->entity, &timings); |
| 696 |
if (ret < 0) { |
| 697 |
printf("Failed to query DV timings: %s\n", strerror(-ret)); |
| 698 |
goto out; |
| 699 |
} |
| 700 |
|
| 701 |
ret = v4l2_subdev_set_dv_timings(pad->entity, &timings); |
| 702 |
if (ret < 0) { |
| 703 |
printf("Failed to set DV timings: %s\n", strerror(-ret)); |
| 704 |
goto out; |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
if (media_opts.print_dot) { |
| 709 |
media_print_topology_dot(media); |
| 710 |
} else if (media_opts.print) { |
| 711 |
if (entity) |
| 712 |
media_print_topology_text_entity(media, entity, |
| 713 |
media_opts.which); |
| 714 |
else |
| 715 |
media_print_topology_text(media, media_opts.which); |
| 716 |
} else if (entity) { |
| 717 |
const char *devname; |
| 718 |
|
| 719 |
devname = media_entity_get_devname(entity); |
| 720 |
if (devname) |
| 721 |
printf("%s\n", devname); |
| 722 |
} |
| 723 |
|
| 724 |
if (media_opts.reset) { |
| 725 |
if (media_opts.verbose) |
| 726 |
printf("Resetting all links to inactive\n"); |
| 727 |
ret = media_reset_links(media); |
| 728 |
if (ret) { |
| 729 |
printf("Unable to reset links: %s (%d)\n", |
| 730 |
strerror(-ret), -ret); |
| 731 |
goto out; |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
if (media_opts.links) { |
| 736 |
ret = media_parse_setup_links(media, media_opts.links); |
| 737 |
if (ret) { |
| 738 |
printf("Unable to parse link: %s (%d)\n", |
| 739 |
strerror(-ret), -ret); |
| 740 |
goto out; |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
if (media_opts.routes) { |
| 745 |
ret = v4l2_subdev_parse_setup_routes(media, media_opts.which, |
| 746 |
media_opts.routes); |
| 747 |
if (ret) { |
| 748 |
printf("Unable to setup routes: %s (%d)\n", |
| 749 |
strerror(-ret), -ret); |
| 750 |
goto out; |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
if (media_opts.formats) { |
| 755 |
ret = v4l2_subdev_parse_setup_formats(media, media_opts.which, |
| 756 |
media_opts.formats); |
| 757 |
if (ret) { |
| 758 |
printf("Unable to setup formats: %s (%d)\n", |
| 759 |
strerror(-ret), -ret); |
| 760 |
goto out; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
if (media_opts.interactive) { |
| 765 |
while (1) { |
| 766 |
char buffer[32]; |
| 767 |
char *end; |
| 768 |
|
| 769 |
printf("Enter a link to modify or enter to stop\n"); |
| 770 |
if (fgets(buffer, sizeof buffer, stdin) == NULL) |
| 771 |
break; |
| 772 |
|
| 773 |
if (buffer[0] == '\n') |
| 774 |
break; |
| 775 |
|
| 776 |
ret = media_parse_setup_link(media, buffer, &end); |
| 777 |
if (ret) |
| 778 |
printf("Unable to parse link: %s (%d)\n", |
| 779 |
strerror(-ret), -ret); |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
ret = 0; |
| 784 |
|
| 785 |
out: |
| 786 |
if (media) |
| 787 |
media_device_unref(media); |
| 788 |
|
| 789 |
return ret ? EXIT_FAILURE : EXIT_SUCCESS; |
| 790 |
} |
| 791 |
|