modetest.c revision 3c748557
1/*
2 * DRM based mode setting test program
3 * Copyright 2008 Tungsten Graphics
4 *   Jakob Bornecrantz <jakob@tungstengraphics.com>
5 * Copyright 2008 Intel Corporation
6 *   Jesse Barnes <jesse.barnes@intel.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * IN THE SOFTWARE.
25 */
26
27/*
28 * This fairly simple test program dumps output in a similar format to the
29 * "xrandr" tool everyone knows & loves.  It's necessarily slightly different
30 * since the kernel separates outputs into encoder and connector structures,
31 * each with their own unique ID.  The program also allows test testing of the
32 * memory management and mode setting APIs by allowing the user to specify a
33 * connector and mode to use for mode setting.  If all works as expected, a
34 * blue background should be painted on the monitor attached to the specified
35 * connector after the selected mode is set.
36 *
37 * TODO: use cairo to write the mode info on the selected output once
38 *       the mode has been programmed, along with possible test patterns.
39 */
40#ifdef HAVE_CONFIG_H
41#include "config.h"
42#endif
43
44#include <assert.h>
45#include <ctype.h>
46#include <stdbool.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <stdint.h>
50#include <inttypes.h>
51#include <unistd.h>
52#include <string.h>
53#include <errno.h>
54#include <sys/poll.h>
55#include <sys/time.h>
56
57#include "xf86drm.h"
58#include "xf86drmMode.h"
59#include "drm_fourcc.h"
60
61#include "buffers.h"
62#include "cursor.h"
63
64struct crtc {
65	drmModeCrtc *crtc;
66	drmModeObjectProperties *props;
67	drmModePropertyRes **props_info;
68	drmModeModeInfo *mode;
69};
70
71struct encoder {
72	drmModeEncoder *encoder;
73};
74
75struct connector {
76	drmModeConnector *connector;
77	drmModeObjectProperties *props;
78	drmModePropertyRes **props_info;
79};
80
81struct fb {
82	drmModeFB *fb;
83};
84
85struct plane {
86	drmModePlane *plane;
87	drmModeObjectProperties *props;
88	drmModePropertyRes **props_info;
89};
90
91struct resources {
92	drmModeRes *res;
93	drmModePlaneRes *plane_res;
94
95	struct crtc *crtcs;
96	struct encoder *encoders;
97	struct connector *connectors;
98	struct fb *fbs;
99	struct plane *planes;
100};
101
102struct device {
103	int fd;
104
105	struct resources *resources;
106
107	struct {
108		unsigned int width;
109		unsigned int height;
110
111		unsigned int fb_id;
112		struct bo *bo;
113	} mode;
114};
115
116#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
117static inline int64_t U642I64(uint64_t val)
118{
119	return (int64_t)*((int64_t *)&val);
120}
121
122struct type_name {
123	int type;
124	const char *name;
125};
126
127#define type_name_fn(res) \
128const char * res##_str(int type) {			\
129	unsigned int i;					\
130	for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
131		if (res##_names[i].type == type)	\
132			return res##_names[i].name;	\
133	}						\
134	return "(invalid)";				\
135}
136
137struct type_name encoder_type_names[] = {
138	{ DRM_MODE_ENCODER_NONE, "none" },
139	{ DRM_MODE_ENCODER_DAC, "DAC" },
140	{ DRM_MODE_ENCODER_TMDS, "TMDS" },
141	{ DRM_MODE_ENCODER_LVDS, "LVDS" },
142	{ DRM_MODE_ENCODER_TVDAC, "TVDAC" },
143};
144
145static type_name_fn(encoder_type)
146
147struct type_name connector_status_names[] = {
148	{ DRM_MODE_CONNECTED, "connected" },
149	{ DRM_MODE_DISCONNECTED, "disconnected" },
150	{ DRM_MODE_UNKNOWNCONNECTION, "unknown" },
151};
152
153static type_name_fn(connector_status)
154
155struct type_name connector_type_names[] = {
156	{ DRM_MODE_CONNECTOR_Unknown, "unknown" },
157	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
158	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
159	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
160	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
161	{ DRM_MODE_CONNECTOR_Composite, "composite" },
162	{ DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
163	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
164	{ DRM_MODE_CONNECTOR_Component, "component" },
165	{ DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
166	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
167	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
168	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
169	{ DRM_MODE_CONNECTOR_TV, "TV" },
170	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
171};
172
173static type_name_fn(connector_type)
174
175#define bit_name_fn(res)					\
176const char * res##_str(int type) {				\
177	unsigned int i;						\
178	const char *sep = "";					\
179	for (i = 0; i < ARRAY_SIZE(res##_names); i++) {		\
180		if (type & (1 << i)) {				\
181			printf("%s%s", sep, res##_names[i]);	\
182			sep = ", ";				\
183		}						\
184	}							\
185	return NULL;						\
186}
187
188static const char *mode_type_names[] = {
189	"builtin",
190	"clock_c",
191	"crtc_c",
192	"preferred",
193	"default",
194	"userdef",
195	"driver",
196};
197
198static bit_name_fn(mode_type)
199
200static const char *mode_flag_names[] = {
201	"phsync",
202	"nhsync",
203	"pvsync",
204	"nvsync",
205	"interlace",
206	"dblscan",
207	"csync",
208	"pcsync",
209	"ncsync",
210	"hskew",
211	"bcast",
212	"pixmux",
213	"dblclk",
214	"clkdiv2"
215};
216
217static bit_name_fn(mode_flag)
218
219static void dump_encoders(struct device *dev)
220{
221	drmModeEncoder *encoder;
222	int i;
223
224	printf("Encoders:\n");
225	printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
226	for (i = 0; i < dev->resources->res->count_encoders; i++) {
227		encoder = dev->resources->encoders[i].encoder;
228		if (!encoder)
229			continue;
230
231		printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
232		       encoder->encoder_id,
233		       encoder->crtc_id,
234		       encoder_type_str(encoder->encoder_type),
235		       encoder->possible_crtcs,
236		       encoder->possible_clones);
237	}
238	printf("\n");
239}
240
241static void dump_mode(drmModeModeInfo *mode)
242{
243	printf("  %s %d %d %d %d %d %d %d %d %d",
244	       mode->name,
245	       mode->vrefresh,
246	       mode->hdisplay,
247	       mode->hsync_start,
248	       mode->hsync_end,
249	       mode->htotal,
250	       mode->vdisplay,
251	       mode->vsync_start,
252	       mode->vsync_end,
253	       mode->vtotal);
254
255	printf(" flags: ");
256	mode_flag_str(mode->flags);
257	printf("; type: ");
258	mode_type_str(mode->type);
259	printf("\n");
260}
261
262static void dump_blob(struct device *dev, uint32_t blob_id)
263{
264	uint32_t i;
265	unsigned char *blob_data;
266	drmModePropertyBlobPtr blob;
267
268	blob = drmModeGetPropertyBlob(dev->fd, blob_id);
269	if (!blob) {
270		printf("\n");
271		return;
272	}
273
274	blob_data = blob->data;
275
276	for (i = 0; i < blob->length; i++) {
277		if (i % 16 == 0)
278			printf("\n\t\t\t");
279		printf("%.2hhx", blob_data[i]);
280	}
281	printf("\n");
282
283	drmModeFreePropertyBlob(blob);
284}
285
286static void dump_prop(struct device *dev, drmModePropertyPtr prop,
287		      uint32_t prop_id, uint64_t value)
288{
289	int i;
290	printf("\t%d", prop_id);
291	if (!prop) {
292		printf("\n");
293		return;
294	}
295
296	printf(" %s:\n", prop->name);
297
298	printf("\t\tflags:");
299	if (prop->flags & DRM_MODE_PROP_PENDING)
300		printf(" pending");
301	if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
302		printf(" immutable");
303	if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
304		printf(" signed range");
305	if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
306		printf(" range");
307	if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
308		printf(" enum");
309	if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
310		printf(" bitmask");
311	if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
312		printf(" blob");
313	if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
314		printf(" object");
315	printf("\n");
316
317	if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
318		printf("\t\tvalues:");
319		for (i = 0; i < prop->count_values; i++)
320			printf(" %"PRId64, U642I64(prop->values[i]));
321		printf("\n");
322	}
323
324	if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
325		printf("\t\tvalues:");
326		for (i = 0; i < prop->count_values; i++)
327			printf(" %"PRIu64, prop->values[i]);
328		printf("\n");
329	}
330
331	if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
332		printf("\t\tenums:");
333		for (i = 0; i < prop->count_enums; i++)
334			printf(" %s=%llu", prop->enums[i].name,
335			       prop->enums[i].value);
336		printf("\n");
337	} else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
338		printf("\t\tvalues:");
339		for (i = 0; i < prop->count_enums; i++)
340			printf(" %s=0x%llx", prop->enums[i].name,
341			       (1LL << prop->enums[i].value));
342		printf("\n");
343	} else {
344		assert(prop->count_enums == 0);
345	}
346
347	if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
348		printf("\t\tblobs:\n");
349		for (i = 0; i < prop->count_blobs; i++)
350			dump_blob(dev, prop->blob_ids[i]);
351		printf("\n");
352	} else {
353		assert(prop->count_blobs == 0);
354	}
355
356	printf("\t\tvalue:");
357	if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
358		dump_blob(dev, value);
359	else
360		printf(" %"PRIu64"\n", value);
361}
362
363static void dump_connectors(struct device *dev)
364{
365	int i, j;
366
367	printf("Connectors:\n");
368	printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
369	for (i = 0; i < dev->resources->res->count_connectors; i++) {
370		struct connector *_connector = &dev->resources->connectors[i];
371		drmModeConnector *connector = _connector->connector;
372		if (!connector)
373			continue;
374
375		printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
376		       connector->connector_id,
377		       connector->encoder_id,
378		       connector_status_str(connector->connection),
379		       connector_type_str(connector->connector_type),
380		       connector->mmWidth, connector->mmHeight,
381		       connector->count_modes);
382
383		for (j = 0; j < connector->count_encoders; j++)
384			printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
385		printf("\n");
386
387		if (connector->count_modes) {
388			printf("  modes:\n");
389			printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
390			       "vss vse vtot)\n");
391			for (j = 0; j < connector->count_modes; j++)
392				dump_mode(&connector->modes[j]);
393		}
394
395		if (_connector->props) {
396			printf("  props:\n");
397			for (j = 0; j < (int)_connector->props->count_props; j++)
398				dump_prop(dev, _connector->props_info[j],
399					  _connector->props->props[j],
400					  _connector->props->prop_values[j]);
401		}
402	}
403	printf("\n");
404}
405
406static void dump_crtcs(struct device *dev)
407{
408	int i;
409	uint32_t j;
410
411	printf("CRTCs:\n");
412	printf("id\tfb\tpos\tsize\n");
413	for (i = 0; i < dev->resources->res->count_crtcs; i++) {
414		struct crtc *_crtc = &dev->resources->crtcs[i];
415		drmModeCrtc *crtc = _crtc->crtc;
416		if (!crtc)
417			continue;
418
419		printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
420		       crtc->crtc_id,
421		       crtc->buffer_id,
422		       crtc->x, crtc->y,
423		       crtc->width, crtc->height);
424		dump_mode(&crtc->mode);
425
426		if (_crtc->props) {
427			printf("  props:\n");
428			for (j = 0; j < _crtc->props->count_props; j++)
429				dump_prop(dev, _crtc->props_info[j],
430					  _crtc->props->props[j],
431					  _crtc->props->prop_values[j]);
432		} else {
433			printf("  no properties found\n");
434		}
435	}
436	printf("\n");
437}
438
439static void dump_framebuffers(struct device *dev)
440{
441	drmModeFB *fb;
442	int i;
443
444	printf("Frame buffers:\n");
445	printf("id\tsize\tpitch\n");
446	for (i = 0; i < dev->resources->res->count_fbs; i++) {
447		fb = dev->resources->fbs[i].fb;
448		if (!fb)
449			continue;
450
451		printf("%u\t(%ux%u)\t%u\n",
452		       fb->fb_id,
453		       fb->width, fb->height,
454		       fb->pitch);
455	}
456	printf("\n");
457}
458
459static void dump_planes(struct device *dev)
460{
461	unsigned int i, j;
462
463	printf("Planes:\n");
464	printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
465
466	if (!dev->resources->plane_res)
467		return;
468
469	for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
470		struct plane *plane = &dev->resources->planes[i];
471		drmModePlane *ovr = plane->plane;
472		if (!ovr)
473			continue;
474
475		printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
476		       ovr->plane_id, ovr->crtc_id, ovr->fb_id,
477		       ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
478		       ovr->gamma_size, ovr->possible_crtcs);
479
480		if (!ovr->count_formats)
481			continue;
482
483		printf("  formats:");
484		for (j = 0; j < ovr->count_formats; j++)
485			printf(" %4.4s", (char *)&ovr->formats[j]);
486		printf("\n");
487
488		if (plane->props) {
489			printf("  props:\n");
490			for (j = 0; j < plane->props->count_props; j++)
491				dump_prop(dev, plane->props_info[j],
492					  plane->props->props[j],
493					  plane->props->prop_values[j]);
494		} else {
495			printf("  no properties found\n");
496		}
497	}
498	printf("\n");
499
500	return;
501}
502
503static void free_resources(struct resources *res)
504{
505	if (!res)
506		return;
507
508#define free_resource(_res, __res, type, Type)					\
509	do {									\
510		int i;								\
511		if (!(_res)->type##s)						\
512			break;							\
513		for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {	\
514			if (!(_res)->type##s[i].type)				\
515				break;						\
516			drmModeFree##Type((_res)->type##s[i].type);		\
517		}								\
518		free((_res)->type##s);						\
519	} while (0)
520
521#define free_properties(_res, __res, type)					\
522	do {									\
523		int i;								\
524		for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {	\
525			drmModeFreeObjectProperties(res->type##s[i].props);	\
526			free(res->type##s[i].props_info);			\
527		}								\
528	} while (0)
529
530	if (res->res) {
531		free_properties(res, res, crtc);
532
533		free_resource(res, res, crtc, Crtc);
534		free_resource(res, res, encoder, Encoder);
535		free_resource(res, res, connector, Connector);
536		free_resource(res, res, fb, FB);
537
538		drmModeFreeResources(res->res);
539	}
540
541	if (res->plane_res) {
542		free_properties(res, plane_res, plane);
543
544		free_resource(res, plane_res, plane, Plane);
545
546		drmModeFreePlaneResources(res->plane_res);
547	}
548
549	free(res);
550}
551
552static struct resources *get_resources(struct device *dev)
553{
554	struct resources *res;
555	int i;
556
557	res = malloc(sizeof *res);
558	if (res == 0)
559		return NULL;
560
561	memset(res, 0, sizeof *res);
562
563	drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
564
565	res->res = drmModeGetResources(dev->fd);
566	if (!res->res) {
567		fprintf(stderr, "drmModeGetResources failed: %s\n",
568			strerror(errno));
569		goto error;
570	}
571
572	res->crtcs = malloc(res->res->count_crtcs * sizeof *res->crtcs);
573	res->encoders = malloc(res->res->count_encoders * sizeof *res->encoders);
574	res->connectors = malloc(res->res->count_connectors * sizeof *res->connectors);
575	res->fbs = malloc(res->res->count_fbs * sizeof *res->fbs);
576
577	if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
578		goto error;
579
580	memset(res->crtcs , 0, res->res->count_crtcs * sizeof *res->crtcs);
581	memset(res->encoders, 0, res->res->count_encoders * sizeof *res->encoders);
582	memset(res->connectors, 0, res->res->count_connectors * sizeof *res->connectors);
583	memset(res->fbs, 0, res->res->count_fbs * sizeof *res->fbs);
584
585#define get_resource(_res, __res, type, Type)					\
586	do {									\
587		int i;								\
588		for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {	\
589			(_res)->type##s[i].type =				\
590				drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
591			if (!(_res)->type##s[i].type)				\
592				fprintf(stderr, "could not get %s %i: %s\n",	\
593					#type, (_res)->__res->type##s[i],	\
594					strerror(errno));			\
595		}								\
596	} while (0)
597
598	get_resource(res, res, crtc, Crtc);
599	get_resource(res, res, encoder, Encoder);
600	get_resource(res, res, connector, Connector);
601	get_resource(res, res, fb, FB);
602
603#define get_properties(_res, __res, type, Type)					\
604	do {									\
605		int i;								\
606		for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {	\
607			struct type *obj = &res->type##s[i];			\
608			unsigned int j;						\
609			obj->props =						\
610				drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
611							   DRM_MODE_OBJECT_##Type); \
612			if (!obj->props) {					\
613				fprintf(stderr,					\
614					"could not get %s %i properties: %s\n", \
615					#type, obj->type->type##_id,		\
616					strerror(errno));			\
617				continue;					\
618			}							\
619			obj->props_info = malloc(obj->props->count_props *	\
620						 sizeof *obj->props_info);	\
621			if (!obj->props_info)					\
622				continue;					\
623			for (j = 0; j < obj->props->count_props; ++j)		\
624				obj->props_info[j] =				\
625					drmModeGetProperty(dev->fd, obj->props->props[j]); \
626		}								\
627	} while (0)
628
629	get_properties(res, res, crtc, CRTC);
630	get_properties(res, res, connector, CONNECTOR);
631
632	for (i = 0; i < res->res->count_crtcs; ++i)
633		res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
634
635	res->plane_res = drmModeGetPlaneResources(dev->fd);
636	if (!res->plane_res) {
637		fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
638			strerror(errno));
639		return res;
640	}
641
642	res->planes = malloc(res->plane_res->count_planes * sizeof *res->planes);
643	if (!res->planes)
644		goto error;
645
646	memset(res->planes, 0, res->plane_res->count_planes * sizeof *res->planes);
647
648	get_resource(res, plane_res, plane, Plane);
649	get_properties(res, plane_res, plane, PLANE);
650
651	return res;
652
653error:
654	free_resources(res);
655	return NULL;
656}
657
658static int get_crtc_index(struct device *dev, uint32_t id)
659{
660	int i;
661
662	for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
663		drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
664		if (crtc && crtc->crtc_id == id)
665			return i;
666	}
667
668	return -1;
669}
670
671static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
672{
673	drmModeConnector *connector;
674	int i;
675
676	for (i = 0; i < dev->resources->res->count_connectors; i++) {
677		connector = dev->resources->connectors[i].connector;
678		if (connector && connector->connector_id == id)
679			return connector;
680	}
681
682	return NULL;
683}
684
685static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
686{
687	drmModeEncoder *encoder;
688	int i;
689
690	for (i = 0; i < dev->resources->res->count_encoders; i++) {
691		encoder = dev->resources->encoders[i].encoder;
692		if (encoder && encoder->encoder_id == id)
693			return encoder;
694	}
695
696	return NULL;
697}
698
699/* -----------------------------------------------------------------------------
700 * Pipes and planes
701 */
702
703/*
704 * Mode setting with the kernel interfaces is a bit of a chore.
705 * First you have to find the connector in question and make sure the
706 * requested mode is available.
707 * Then you need to find the encoder attached to that connector so you
708 * can bind it with a free crtc.
709 */
710struct pipe_arg {
711	uint32_t *con_ids;
712	unsigned int num_cons;
713	uint32_t crtc_id;
714	char mode_str[64];
715	char format_str[5];
716	unsigned int vrefresh;
717	unsigned int fourcc;
718	drmModeModeInfo *mode;
719	struct crtc *crtc;
720	unsigned int fb_id[2], current_fb_id;
721	struct timeval start;
722
723	int swap_count;
724};
725
726struct plane_arg {
727	uint32_t crtc_id;  /* the id of CRTC to bind to */
728	bool has_position;
729	int32_t x, y;
730	uint32_t w, h;
731	double scale;
732	unsigned int fb_id;
733	char format_str[5]; /* need to leave room for terminating \0 */
734	unsigned int fourcc;
735};
736
737static drmModeModeInfo *
738connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
739        const unsigned int vrefresh)
740{
741	drmModeConnector *connector;
742	drmModeModeInfo *mode;
743	int i;
744
745	connector = get_connector_by_id(dev, con_id);
746	if (!connector || !connector->count_modes)
747		return NULL;
748
749	for (i = 0; i < connector->count_modes; i++) {
750		mode = &connector->modes[i];
751		if (!strcmp(mode->name, mode_str)) {
752			/* If the vertical refresh frequency is not specified then return the
753			 * first mode that match with the name. Else, return the mode that match
754			 * the name and the specified vertical refresh frequency.
755			 */
756			if (vrefresh == 0)
757				return mode;
758			else if (mode->vrefresh == vrefresh)
759				return mode;
760		}
761	}
762
763	return NULL;
764}
765
766static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
767{
768	uint32_t possible_crtcs = ~0;
769	uint32_t active_crtcs = 0;
770	unsigned int crtc_idx;
771	unsigned int i;
772	int j;
773
774	for (i = 0; i < pipe->num_cons; ++i) {
775		uint32_t crtcs_for_connector = 0;
776		drmModeConnector *connector;
777		drmModeEncoder *encoder;
778		int idx;
779
780		connector = get_connector_by_id(dev, pipe->con_ids[i]);
781		if (!connector)
782			return NULL;
783
784		for (j = 0; j < connector->count_encoders; ++j) {
785			encoder = get_encoder_by_id(dev, connector->encoders[j]);
786			if (!encoder)
787				continue;
788
789			crtcs_for_connector |= encoder->possible_crtcs;
790
791			idx = get_crtc_index(dev, encoder->crtc_id);
792			if (idx >= 0)
793				active_crtcs |= 1 << idx;
794		}
795
796		possible_crtcs &= crtcs_for_connector;
797	}
798
799	if (!possible_crtcs)
800		return NULL;
801
802	/* Return the first possible and active CRTC if one exists, or the first
803	 * possible CRTC otherwise.
804	 */
805	if (possible_crtcs & active_crtcs)
806		crtc_idx = ffs(possible_crtcs & active_crtcs);
807	else
808		crtc_idx = ffs(possible_crtcs);
809
810	return &dev->resources->crtcs[crtc_idx - 1];
811}
812
813static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
814{
815	drmModeModeInfo *mode = NULL;
816	int i;
817
818	pipe->mode = NULL;
819
820	for (i = 0; i < (int)pipe->num_cons; i++) {
821		mode = connector_find_mode(dev, pipe->con_ids[i],
822					   pipe->mode_str, pipe->vrefresh);
823		if (mode == NULL) {
824			fprintf(stderr,
825				"failed to find mode \"%s\" for connector %u\n",
826				pipe->mode_str, pipe->con_ids[i]);
827			return -EINVAL;
828		}
829	}
830
831	/* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
832	 * locate a CRTC that can be attached to all the connectors.
833	 */
834	if (pipe->crtc_id != (uint32_t)-1) {
835		for (i = 0; i < dev->resources->res->count_crtcs; i++) {
836			struct crtc *crtc = &dev->resources->crtcs[i];
837
838			if (pipe->crtc_id == crtc->crtc->crtc_id) {
839				pipe->crtc = crtc;
840				break;
841			}
842		}
843	} else {
844		pipe->crtc = pipe_find_crtc(dev, pipe);
845	}
846
847	if (!pipe->crtc) {
848		fprintf(stderr, "failed to find CRTC for pipe\n");
849		return -EINVAL;
850	}
851
852	pipe->mode = mode;
853	pipe->crtc->mode = mode;
854
855	return 0;
856}
857
858/* -----------------------------------------------------------------------------
859 * Properties
860 */
861
862struct property_arg {
863	uint32_t obj_id;
864	uint32_t obj_type;
865	char name[DRM_PROP_NAME_LEN+1];
866	uint32_t prop_id;
867	uint64_t value;
868};
869
870static void set_property(struct device *dev, struct property_arg *p)
871{
872	drmModeObjectProperties *props = NULL;
873	drmModePropertyRes **props_info = NULL;
874	const char *obj_type;
875	int ret;
876	int i;
877
878	p->obj_type = 0;
879	p->prop_id = 0;
880
881#define find_object(_res, __res, type, Type)					\
882	do {									\
883		for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {	\
884			struct type *obj = &(_res)->type##s[i];			\
885			if (obj->type->type##_id != p->obj_id)			\
886				continue;					\
887			p->obj_type = DRM_MODE_OBJECT_##Type;			\
888			obj_type = #Type;					\
889			props = obj->props;					\
890			props_info = obj->props_info;				\
891		}								\
892	} while(0)								\
893
894	find_object(dev->resources, res, crtc, CRTC);
895	if (p->obj_type == 0)
896		find_object(dev->resources, res, connector, CONNECTOR);
897	if (p->obj_type == 0)
898		find_object(dev->resources, plane_res, plane, PLANE);
899	if (p->obj_type == 0) {
900		fprintf(stderr, "Object %i not found, can't set property\n",
901			p->obj_id);
902			return;
903	}
904
905	if (!props) {
906		fprintf(stderr, "%s %i has no properties\n",
907			obj_type, p->obj_id);
908		return;
909	}
910
911	for (i = 0; i < (int)props->count_props; ++i) {
912		if (!props_info[i])
913			continue;
914		if (strcmp(props_info[i]->name, p->name) == 0)
915			break;
916	}
917
918	if (i == (int)props->count_props) {
919		fprintf(stderr, "%s %i has no %s property\n",
920			obj_type, p->obj_id, p->name);
921		return;
922	}
923
924	p->prop_id = props->props[i];
925
926	ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
927				       p->prop_id, p->value);
928	if (ret < 0)
929		fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
930			obj_type, p->obj_id, p->name, p->value, strerror(errno));
931}
932
933/* -------------------------------------------------------------------------- */
934
935static void
936page_flip_handler(int fd, unsigned int frame,
937		  unsigned int sec, unsigned int usec, void *data)
938{
939	struct pipe_arg *pipe;
940	unsigned int new_fb_id;
941	struct timeval end;
942	double t;
943
944	pipe = data;
945	if (pipe->current_fb_id == pipe->fb_id[0])
946		new_fb_id = pipe->fb_id[1];
947	else
948		new_fb_id = pipe->fb_id[0];
949
950	drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
951			DRM_MODE_PAGE_FLIP_EVENT, pipe);
952	pipe->current_fb_id = new_fb_id;
953	pipe->swap_count++;
954	if (pipe->swap_count == 60) {
955		gettimeofday(&end, NULL);
956		t = end.tv_sec + end.tv_usec * 1e-6 -
957			(pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
958		fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
959		pipe->swap_count = 0;
960		pipe->start = end;
961	}
962}
963
964static int set_plane(struct device *dev, struct plane_arg *p)
965{
966	drmModePlane *ovr;
967	uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
968	uint32_t plane_id = 0;
969	struct bo *plane_bo;
970	uint32_t plane_flags = 0;
971	int crtc_x, crtc_y, crtc_w, crtc_h;
972	struct crtc *crtc = NULL;
973	unsigned int pipe;
974	unsigned int i;
975
976	/* Find an unused plane which can be connected to our CRTC. Find the
977	 * CRTC index first, then iterate over available planes.
978	 */
979	for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
980		if (p->crtc_id == dev->resources->res->crtcs[i]) {
981			crtc = &dev->resources->crtcs[i];
982			pipe = i;
983			break;
984		}
985	}
986
987	if (!crtc) {
988		fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
989		return -1;
990	}
991
992	for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) {
993		ovr = dev->resources->planes[i].plane;
994		if (!ovr)
995			continue;
996
997		if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id)
998			plane_id = ovr->plane_id;
999	}
1000
1001	if (!plane_id) {
1002		fprintf(stderr, "no unused plane available for CRTC %u\n",
1003			crtc->crtc->crtc_id);
1004		return -1;
1005	}
1006
1007	fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1008		p->w, p->h, p->format_str, plane_id);
1009
1010	plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
1011			     pitches, offsets, PATTERN_TILES);
1012	if (plane_bo == NULL)
1013		return -1;
1014
1015	/* just use single plane format for now.. */
1016	if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1017			handles, pitches, offsets, &p->fb_id, plane_flags)) {
1018		fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1019		return -1;
1020	}
1021
1022	crtc_w = p->w * p->scale;
1023	crtc_h = p->h * p->scale;
1024	if (!p->has_position) {
1025		/* Default to the middle of the screen */
1026		crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1027		crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1028	} else {
1029		crtc_x = p->x;
1030		crtc_y = p->y;
1031	}
1032
1033	/* note src coords (last 4 args) are in Q16 format */
1034	if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1035			    plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1036			    0, 0, p->w << 16, p->h << 16)) {
1037		fprintf(stderr, "failed to enable plane: %s\n",
1038			strerror(errno));
1039		return -1;
1040	}
1041
1042	ovr->crtc_id = crtc->crtc->crtc_id;
1043
1044	return 0;
1045}
1046
1047static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1048{
1049	uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1050	unsigned int fb_id;
1051	struct bo *bo;
1052	unsigned int i;
1053	unsigned int j;
1054	int ret, x;
1055
1056	dev->mode.width = 0;
1057	dev->mode.height = 0;
1058
1059	for (i = 0; i < count; i++) {
1060		struct pipe_arg *pipe = &pipes[i];
1061
1062		ret = pipe_find_crtc_and_mode(dev, pipe);
1063		if (ret < 0)
1064			continue;
1065
1066		dev->mode.width += pipe->mode->hdisplay;
1067		if (dev->mode.height < pipe->mode->vdisplay)
1068			dev->mode.height = pipe->mode->vdisplay;
1069	}
1070
1071	bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width, dev->mode.height,
1072		       handles, pitches, offsets, PATTERN_SMPTE);
1073	if (bo == NULL)
1074		return;
1075
1076	ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1077			    pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1078	if (ret) {
1079		fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1080			dev->mode.width, dev->mode.height, strerror(errno));
1081		return;
1082	}
1083
1084	x = 0;
1085	for (i = 0; i < count; i++) {
1086		struct pipe_arg *pipe = &pipes[i];
1087
1088		if (pipe->mode == NULL)
1089			continue;
1090
1091		printf("setting mode %s-%dHz@%s on connectors ",
1092		       pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1093		for (j = 0; j < pipe->num_cons; ++j)
1094			printf("%u, ", pipe->con_ids[j]);
1095		printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1096
1097		ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1098				     x, 0, pipe->con_ids, pipe->num_cons,
1099				     pipe->mode);
1100
1101		/* XXX: Actually check if this is needed */
1102		drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1103
1104		x += pipe->mode->hdisplay;
1105
1106		if (ret) {
1107			fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1108			return;
1109		}
1110	}
1111
1112	dev->mode.bo = bo;
1113	dev->mode.fb_id = fb_id;
1114}
1115
1116static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1117{
1118	unsigned int i;
1119
1120	/* set up planes/overlays */
1121	for (i = 0; i < count; i++)
1122		if (set_plane(dev, &p[i]))
1123			return;
1124}
1125
1126static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1127{
1128	uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1129	struct bo *bo;
1130	unsigned int i;
1131	int ret;
1132
1133	/* maybe make cursor width/height configurable some day */
1134	uint32_t cw = 64;
1135	uint32_t ch = 64;
1136
1137	/* create cursor bo.. just using PATTERN_PLAIN as it has
1138	 * translucent alpha
1139	 */
1140	bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1141		       offsets, PATTERN_PLAIN);
1142	if (bo == NULL)
1143		return;
1144
1145	for (i = 0; i < count; i++) {
1146		struct pipe_arg *pipe = &pipes[i];
1147		ret = cursor_init(dev->fd, handles[0],
1148				pipe->crtc->crtc->crtc_id,
1149				pipe->mode->hdisplay, pipe->mode->vdisplay,
1150				cw, ch);
1151		if (ret) {
1152			fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1153					pipe->crtc_id);
1154			return;
1155		}
1156	}
1157
1158	cursor_start();
1159}
1160
1161static void clear_cursors(struct device *dev)
1162{
1163	cursor_stop();
1164}
1165
1166static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1167{
1168	uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1169	unsigned int other_fb_id;
1170	struct bo *other_bo;
1171	drmEventContext evctx;
1172	unsigned int i;
1173	int ret;
1174
1175	other_bo = bo_create(dev->fd, pipes[0].fourcc,
1176			     dev->mode.width, dev->mode.height,
1177			     handles, pitches, offsets, PATTERN_PLAIN);
1178	if (other_bo == NULL)
1179		return;
1180
1181	ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1182			    pipes[0].fourcc, handles, pitches, offsets,
1183			    &other_fb_id, 0);
1184	if (ret) {
1185		fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1186		return;
1187	}
1188
1189	for (i = 0; i < count; i++) {
1190		struct pipe_arg *pipe = &pipes[i];
1191
1192		if (pipe->mode == NULL)
1193			continue;
1194
1195		ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1196				      other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1197				      pipe);
1198		if (ret) {
1199			fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1200			return;
1201		}
1202		gettimeofday(&pipe->start, NULL);
1203		pipe->swap_count = 0;
1204		pipe->fb_id[0] = dev->mode.fb_id;
1205		pipe->fb_id[1] = other_fb_id;
1206		pipe->current_fb_id = other_fb_id;
1207	}
1208
1209	memset(&evctx, 0, sizeof evctx);
1210	evctx.version = DRM_EVENT_CONTEXT_VERSION;
1211	evctx.vblank_handler = NULL;
1212	evctx.page_flip_handler = page_flip_handler;
1213
1214	while (1) {
1215#if 0
1216		struct pollfd pfd[2];
1217
1218		pfd[0].fd = 0;
1219		pfd[0].events = POLLIN;
1220		pfd[1].fd = fd;
1221		pfd[1].events = POLLIN;
1222
1223		if (poll(pfd, 2, -1) < 0) {
1224			fprintf(stderr, "poll error\n");
1225			break;
1226		}
1227
1228		if (pfd[0].revents)
1229			break;
1230#else
1231		struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1232		fd_set fds;
1233		int ret;
1234
1235		FD_ZERO(&fds);
1236		FD_SET(0, &fds);
1237		FD_SET(dev->fd, &fds);
1238		ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1239
1240		if (ret <= 0) {
1241			fprintf(stderr, "select timed out or error (ret %d)\n",
1242				ret);
1243			continue;
1244		} else if (FD_ISSET(0, &fds)) {
1245			break;
1246		}
1247#endif
1248
1249		drmHandleEvent(dev->fd, &evctx);
1250	}
1251
1252	bo_destroy(other_bo);
1253}
1254
1255#define min(a, b)	((a) < (b) ? (a) : (b))
1256
1257static int parse_connector(struct pipe_arg *pipe, const char *arg)
1258{
1259	unsigned int len;
1260	unsigned int i;
1261	const char *p;
1262	char *endp;
1263
1264	pipe->vrefresh = 0;
1265	pipe->crtc_id = (uint32_t)-1;
1266	strcpy(pipe->format_str, "XR24");
1267
1268	/* Count the number of connectors and allocate them. */
1269	pipe->num_cons = 1;
1270	for (p = arg; isdigit(*p) || *p == ','; ++p) {
1271		if (*p == ',')
1272			pipe->num_cons++;
1273	}
1274
1275	pipe->con_ids = malloc(pipe->num_cons * sizeof *pipe->con_ids);
1276	if (pipe->con_ids == NULL)
1277		return -1;
1278
1279	/* Parse the connectors. */
1280	for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1281		pipe->con_ids[i] = strtoul(p, &endp, 10);
1282		if (*endp != ',')
1283			break;
1284	}
1285
1286	if (i != pipe->num_cons - 1)
1287		return -1;
1288
1289	/* Parse the remaining parameters. */
1290	if (*endp == '@') {
1291		arg = endp + 1;
1292		pipe->crtc_id = strtoul(arg, &endp, 10);
1293	}
1294	if (*endp != ':')
1295		return -1;
1296
1297	arg = endp + 1;
1298
1299	/* Search for the vertical refresh or the format. */
1300	p = strpbrk(arg, "-@");
1301	if (p == NULL)
1302		p = arg + strlen(arg);
1303	len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1304	strncpy(pipe->mode_str, arg, len);
1305	pipe->mode_str[len] = '\0';
1306
1307	if (*p == '-') {
1308		pipe->vrefresh = strtoul(p + 1, &endp, 10);
1309		p = endp;
1310	}
1311
1312	if (*p == '@') {
1313		strncpy(pipe->format_str, p + 1, 4);
1314		pipe->format_str[4] = '\0';
1315	}
1316
1317	pipe->fourcc = format_fourcc(pipe->format_str);
1318	if (pipe->fourcc == 0)  {
1319		fprintf(stderr, "unknown format %s\n", pipe->format_str);
1320		return -1;
1321	}
1322
1323	return 0;
1324}
1325
1326static int parse_plane(struct plane_arg *plane, const char *p)
1327{
1328	char *end;
1329
1330	memset(plane, 0, sizeof *plane);
1331
1332	plane->crtc_id = strtoul(p, &end, 10);
1333	if (*end != ':')
1334		return -EINVAL;
1335
1336	p = end + 1;
1337	plane->w = strtoul(p, &end, 10);
1338	if (*end != 'x')
1339		return -EINVAL;
1340
1341	p = end + 1;
1342	plane->h = strtoul(p, &end, 10);
1343
1344	if (*end == '+' || *end == '-') {
1345		plane->x = strtol(end, &end, 10);
1346		if (*end != '+' && *end != '-')
1347			return -EINVAL;
1348		plane->y = strtol(end, &end, 10);
1349
1350		plane->has_position = true;
1351	}
1352
1353	if (*end == '*') {
1354		p = end + 1;
1355		plane->scale = strtod(p, &end);
1356		if (plane->scale <= 0.0)
1357			return -EINVAL;
1358	} else {
1359		plane->scale = 1.0;
1360	}
1361
1362	if (*end == '@') {
1363		p = end + 1;
1364		if (strlen(p) != 4)
1365			return -EINVAL;
1366
1367		strcpy(plane->format_str, p);
1368	} else {
1369		strcpy(plane->format_str, "XR24");
1370	}
1371
1372	plane->fourcc = format_fourcc(plane->format_str);
1373	if (plane->fourcc == 0) {
1374		fprintf(stderr, "unknown format %s\n", plane->format_str);
1375		return -EINVAL;
1376	}
1377
1378	return 0;
1379}
1380
1381static int parse_property(struct property_arg *p, const char *arg)
1382{
1383	if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1384		return -1;
1385
1386	p->obj_type = 0;
1387	p->name[DRM_PROP_NAME_LEN] = '\0';
1388
1389	return 0;
1390}
1391
1392static void usage(char *name)
1393{
1394	fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1395
1396	fprintf(stderr, "\n Query options:\n\n");
1397	fprintf(stderr, "\t-c\tlist connectors\n");
1398	fprintf(stderr, "\t-e\tlist encoders\n");
1399	fprintf(stderr, "\t-f\tlist framebuffers\n");
1400	fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1401
1402	fprintf(stderr, "\n Test options:\n\n");
1403	fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1404	fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1405	fprintf(stderr, "\t-C\ttest hw cursor\n");
1406	fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1407	fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1408
1409	fprintf(stderr, "\n Generic options:\n\n");
1410	fprintf(stderr, "\t-d\tdrop master after mode set\n");
1411	fprintf(stderr, "\t-M module\tuse the given driver\n");
1412	fprintf(stderr, "\t-D device\tuse the given device\n");
1413
1414	fprintf(stderr, "\n\tDefault is to dump all info.\n");
1415	exit(0);
1416}
1417
1418static int page_flipping_supported(void)
1419{
1420	/*FIXME: generic ioctl needed? */
1421	return 1;
1422#if 0
1423	int ret, value;
1424	struct drm_i915_getparam gp;
1425
1426	gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1427	gp.value = &value;
1428
1429	ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1430	if (ret) {
1431		fprintf(stderr, "drm_i915_getparam: %m\n");
1432		return 0;
1433	}
1434
1435	return *gp.value;
1436#endif
1437}
1438
1439static int cursor_supported(void)
1440{
1441	/*FIXME: generic ioctl needed? */
1442	return 1;
1443}
1444
1445static char optstr[] = "cdD:efM:P:ps:Cvw:";
1446
1447int main(int argc, char **argv)
1448{
1449	struct device dev;
1450
1451	int c;
1452	int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1453	int drop_master = 0;
1454	int test_vsync = 0;
1455	int test_cursor = 0;
1456	const char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "tilcdc", "msm", "sti", "tegra" };
1457	char *device = NULL;
1458	char *module = NULL;
1459	unsigned int i;
1460	int count = 0, plane_count = 0;
1461	unsigned int prop_count = 0;
1462	struct pipe_arg *pipe_args = NULL;
1463	struct plane_arg *plane_args = NULL;
1464	struct property_arg *prop_args = NULL;
1465	unsigned int args = 0;
1466	int ret;
1467
1468	memset(&dev, 0, sizeof dev);
1469
1470	opterr = 0;
1471	while ((c = getopt(argc, argv, optstr)) != -1) {
1472		args++;
1473
1474		switch (c) {
1475		case 'c':
1476			connectors = 1;
1477			break;
1478		case 'D':
1479			device = optarg;
1480			args--;
1481			break;
1482		case 'd':
1483			drop_master = 1;
1484			break;
1485		case 'e':
1486			encoders = 1;
1487			break;
1488		case 'f':
1489			framebuffers = 1;
1490			break;
1491		case 'M':
1492			module = optarg;
1493			/* Preserve the default behaviour of dumping all information. */
1494			args--;
1495			break;
1496		case 'P':
1497			plane_args = realloc(plane_args,
1498					     (plane_count + 1) * sizeof *plane_args);
1499			if (plane_args == NULL) {
1500				fprintf(stderr, "memory allocation failed\n");
1501				return 1;
1502			}
1503
1504			if (parse_plane(&plane_args[plane_count], optarg) < 0)
1505				usage(argv[0]);
1506
1507			plane_count++;
1508			break;
1509		case 'p':
1510			crtcs = 1;
1511			planes = 1;
1512			break;
1513		case 's':
1514			pipe_args = realloc(pipe_args,
1515					    (count + 1) * sizeof *pipe_args);
1516			if (pipe_args == NULL) {
1517				fprintf(stderr, "memory allocation failed\n");
1518				return 1;
1519			}
1520
1521			if (parse_connector(&pipe_args[count], optarg) < 0)
1522				usage(argv[0]);
1523
1524			count++;
1525			break;
1526		case 'C':
1527			test_cursor = 1;
1528			break;
1529		case 'v':
1530			test_vsync = 1;
1531			break;
1532		case 'w':
1533			prop_args = realloc(prop_args,
1534					   (prop_count + 1) * sizeof *prop_args);
1535			if (prop_args == NULL) {
1536				fprintf(stderr, "memory allocation failed\n");
1537				return 1;
1538			}
1539
1540			if (parse_property(&prop_args[prop_count], optarg) < 0)
1541				usage(argv[0]);
1542
1543			prop_count++;
1544			break;
1545		default:
1546			usage(argv[0]);
1547			break;
1548		}
1549	}
1550
1551	if (!args)
1552		encoders = connectors = crtcs = planes = framebuffers = 1;
1553
1554	if (module) {
1555		dev.fd = drmOpen(module, device);
1556		if (dev.fd < 0) {
1557			fprintf(stderr, "failed to open device '%s'.\n", module);
1558			return 1;
1559		}
1560	} else {
1561		for (i = 0; i < ARRAY_SIZE(modules); i++) {
1562			printf("trying to open device '%s'...", modules[i]);
1563			dev.fd = drmOpen(modules[i], device);
1564			if (dev.fd < 0) {
1565				printf("failed.\n");
1566			} else {
1567				printf("success.\n");
1568				break;
1569			}
1570		}
1571
1572		if (dev.fd < 0) {
1573			fprintf(stderr, "no device found.\n");
1574			return 1;
1575		}
1576	}
1577
1578	if (test_vsync && !page_flipping_supported()) {
1579		fprintf(stderr, "page flipping not supported by drm.\n");
1580		return -1;
1581	}
1582
1583	if (test_vsync && !count) {
1584		fprintf(stderr, "page flipping requires at least one -s option.\n");
1585		return -1;
1586	}
1587
1588	if (test_cursor && !cursor_supported()) {
1589		fprintf(stderr, "hw cursor not supported by drm.\n");
1590		return -1;
1591	}
1592
1593	dev.resources = get_resources(&dev);
1594	if (!dev.resources) {
1595		drmClose(dev.fd);
1596		return 1;
1597	}
1598
1599#define dump_resource(dev, res) if (res) dump_##res(dev)
1600
1601	dump_resource(&dev, encoders);
1602	dump_resource(&dev, connectors);
1603	dump_resource(&dev, crtcs);
1604	dump_resource(&dev, planes);
1605	dump_resource(&dev, framebuffers);
1606
1607	for (i = 0; i < prop_count; ++i)
1608		set_property(&dev, &prop_args[i]);
1609
1610	if (count || plane_count) {
1611		uint64_t cap = 0;
1612
1613		ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
1614		if (ret || cap == 0) {
1615			fprintf(stderr, "driver doesn't support the dumb buffer API\n");
1616			return 1;
1617		}
1618
1619		if (count)
1620			set_mode(&dev, pipe_args, count);
1621
1622		if (plane_count)
1623			set_planes(&dev, plane_args, plane_count);
1624
1625		if (test_cursor)
1626			set_cursors(&dev, pipe_args, count);
1627
1628		if (test_vsync)
1629			test_page_flip(&dev, pipe_args, count);
1630
1631		if (drop_master)
1632			drmDropMaster(dev.fd);
1633
1634		getchar();
1635
1636		if (test_cursor)
1637			clear_cursors(&dev);
1638
1639		bo_destroy(dev.mode.bo);
1640	}
1641
1642	free_resources(dev.resources);
1643
1644	return 0;
1645}
1646