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