OSDN Git Service

libdrm/radeon: Fix section size mismatch to reset the section.
[android-x86/external-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 <stdio.h>
44 #include <stdlib.h>
45 #include <stdint.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <sys/poll.h>
50
51 #include "xf86drm.h"
52 #include "xf86drmMode.h"
53 #include "intel_bufmgr.h"
54 #include "i915_drm.h"
55
56 #ifdef HAVE_CAIRO
57 #include <math.h>
58 #include <cairo.h>
59 #endif
60
61 drmModeRes *resources;
62 int fd, modes;
63
64 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
65
66 struct type_name {
67         int type;
68         char *name;
69 };
70
71 #define type_name_fn(res) \
72 char * res##_str(int type) {                    \
73         int i;                                          \
74         for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
75                 if (res##_names[i].type == type)        \
76                         return res##_names[i].name;     \
77         }                                               \
78         return "(invalid)";                             \
79 }
80
81 struct type_name encoder_type_names[] = {
82         { DRM_MODE_ENCODER_NONE, "none" },
83         { DRM_MODE_ENCODER_DAC, "DAC" },
84         { DRM_MODE_ENCODER_TMDS, "TMDS" },
85         { DRM_MODE_ENCODER_LVDS, "LVDS" },
86         { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
87 };
88
89 type_name_fn(encoder_type)
90
91 struct type_name connector_status_names[] = {
92         { DRM_MODE_CONNECTED, "connected" },
93         { DRM_MODE_DISCONNECTED, "disconnected" },
94         { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
95 };
96
97 type_name_fn(connector_status)
98
99 struct type_name connector_type_names[] = {
100         { DRM_MODE_CONNECTOR_Unknown, "unknown" },
101         { DRM_MODE_CONNECTOR_VGA, "VGA" },
102         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
103         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
104         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
105         { DRM_MODE_CONNECTOR_Composite, "composite" },
106         { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
107         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
108         { DRM_MODE_CONNECTOR_Component, "component" },
109         { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
110         { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
111         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
112         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
113 };
114
115 type_name_fn(connector_type)
116
117 void dump_encoders(void)
118 {
119         drmModeEncoder *encoder;
120         int i;
121
122         printf("Encoders:\n");
123         printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
124         for (i = 0; i < resources->count_encoders; i++) {
125                 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
126
127                 if (!encoder) {
128                         fprintf(stderr, "could not get encoder %i: %s\n",
129                                 resources->encoders[i], strerror(errno));
130                         continue;
131                 }
132                 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
133                        encoder->encoder_id,
134                        encoder->crtc_id,
135                        encoder_type_str(encoder->encoder_type),
136                        encoder->possible_crtcs,
137                        encoder->possible_clones);
138                 drmModeFreeEncoder(encoder);
139         }
140         printf("\n");
141 }
142
143 void dump_mode(drmModeModeInfo *mode)
144 {
145         printf("  %s %.02f %d %d %d %d %d %d %d %d\n",
146                mode->name,
147                (float)mode->vrefresh / 1000,
148                mode->hdisplay,
149                mode->hsync_start,
150                mode->hsync_end,
151                mode->htotal,
152                mode->vdisplay,
153                mode->vsync_start,
154                mode->vsync_end,
155                mode->vtotal);
156 }
157
158 static void
159 dump_props(drmModeConnector *connector)
160 {
161         drmModePropertyPtr props;
162         int i;
163
164         for (i = 0; i < connector->count_props; i++) {
165                 props = drmModeGetProperty(fd, connector->props[i]);
166                 printf("\t%s, flags %d\n", props->name, props->flags);
167                 drmModeFreeProperty(props);
168         }
169 }
170
171 void dump_connectors(void)
172 {
173         drmModeConnector *connector;
174         int i, j;
175
176         printf("Connectors:\n");
177         printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
178         for (i = 0; i < resources->count_connectors; i++) {
179                 connector = drmModeGetConnector(fd, resources->connectors[i]);
180
181                 if (!connector) {
182                         fprintf(stderr, "could not get connector %i: %s\n",
183                                 resources->connectors[i], strerror(errno));
184                         continue;
185                 }
186
187                 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
188                        connector->connector_id,
189                        connector->encoder_id,
190                        connector_status_str(connector->connection),
191                        connector_type_str(connector->connector_type),
192                        connector->mmWidth, connector->mmHeight,
193                        connector->count_modes);
194
195                 for (j = 0; j < connector->count_encoders; j++)
196                         printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
197                 printf("\n");
198
199                 if (!connector->count_modes)
200                         continue;
201
202                 printf("  modes:\n");
203                 printf("  name refresh (Hz) hdisp hss hse htot vdisp "
204                        "vss vse vtot)\n");
205                 for (j = 0; j < connector->count_modes; j++)
206                         dump_mode(&connector->modes[j]);
207
208                 drmModeFreeConnector(connector);
209
210                 printf("  props:\n");
211                 dump_props(connector);
212         }
213         printf("\n");
214 }
215
216 void dump_crtcs(void)
217 {
218         drmModeCrtc *crtc;
219         int i;
220
221         printf("CRTCs:\n");
222         printf("id\tfb\tpos\tsize\n");
223         for (i = 0; i < resources->count_crtcs; i++) {
224                 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
225
226                 if (!crtc) {
227                         fprintf(stderr, "could not get crtc %i: %s\n",
228                                 resources->crtcs[i], strerror(errno));
229                         continue;
230                 }
231                 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
232                        crtc->crtc_id,
233                        crtc->buffer_id,
234                        crtc->x, crtc->y,
235                        crtc->width, crtc->height);
236                 dump_mode(&crtc->mode);
237
238                 drmModeFreeCrtc(crtc);
239         }
240         printf("\n");
241 }
242
243 void dump_framebuffers(void)
244 {
245         drmModeFB *fb;
246         int i;
247
248         printf("Frame buffers:\n");
249         printf("id\tsize\tpitch\n");
250         for (i = 0; i < resources->count_fbs; i++) {
251                 fb = drmModeGetFB(fd, resources->fbs[i]);
252
253                 if (!fb) {
254                         fprintf(stderr, "could not get fb %i: %s\n",
255                                 resources->fbs[i], strerror(errno));
256                         continue;
257                 }
258                 printf("%d\t(%dx%d)\t%d\n",
259                        fb->fb_id,
260                        fb->width, fb->height);
261
262                 drmModeFreeFB(fb);
263         }
264         printf("\n");
265 }
266
267 /*
268  * Mode setting with the kernel interfaces is a bit of a chore.
269  * First you have to find the connector in question and make sure the
270  * requested mode is available.
271  * Then you need to find the encoder attached to that connector so you
272  * can bind it with a free crtc.
273  */
274 struct connector {
275         int id;
276         char mode_str[64];
277         drmModeModeInfo *mode;
278         drmModeEncoder *encoder;
279         int crtc;
280         unsigned int fb_id[2], current_fb_id;
281         struct timeval start;
282
283         int swap_count;
284 };      
285
286 static void
287 connector_find_mode(struct connector *c)
288 {
289         drmModeConnector *connector;
290         int i, j, size, ret, width, height;
291
292         /* First, find the connector & mode */
293         c->mode = NULL;
294         for (i = 0; i < resources->count_connectors; i++) {
295                 connector = drmModeGetConnector(fd, resources->connectors[i]);
296
297                 if (!connector) {
298                         fprintf(stderr, "could not get connector %i: %s\n",
299                                 resources->connectors[i], strerror(errno));
300                         drmModeFreeConnector(connector);
301                         continue;
302                 }
303
304                 if (!connector->count_modes) {
305                         drmModeFreeConnector(connector);
306                         continue;
307                 }
308
309                 if (connector->connector_id != c->id) {
310                         drmModeFreeConnector(connector);
311                         continue;
312                 }
313
314                 for (j = 0; j < connector->count_modes; j++) {
315                         c->mode = &connector->modes[j];
316                         if (!strcmp(c->mode->name, c->mode_str))
317                                 break;
318                 }
319
320                 /* Found it, break out */
321                 if (c->mode)
322                         break;
323
324                 drmModeFreeConnector(connector);
325         }
326
327         if (!c->mode) {
328                 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
329                 return;
330         }
331
332         /* Now get the encoder */
333         for (i = 0; i < resources->count_encoders; i++) {
334                 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
335
336                 if (!c->encoder) {
337                         fprintf(stderr, "could not get encoder %i: %s\n",
338                                 resources->encoders[i], strerror(errno));
339                         drmModeFreeEncoder(c->encoder);
340                         continue;
341                 }
342
343                 if (c->encoder->encoder_id  == connector->encoder_id)
344                         break;
345
346                 drmModeFreeEncoder(c->encoder);
347         }
348
349         if (c->crtc == -1)
350                 c->crtc = c->encoder->crtc_id;
351 }
352
353 #ifdef HAVE_CAIRO
354
355 static int
356 create_test_buffer(drm_intel_bufmgr *bufmgr,
357                    int width, int height, int *stride_out, drm_intel_bo **bo_out)
358 {
359         drm_intel_bo *bo;
360         unsigned int *fb_ptr;
361         int size, ret, i, stride;
362         div_t d;
363         cairo_surface_t *surface;
364         cairo_t *cr;
365         char buf[64];
366         int x, y;
367
368         surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
369         stride = cairo_image_surface_get_stride(surface);
370         size = stride * height;
371         fb_ptr = (unsigned int *) cairo_image_surface_get_data(surface);
372
373         /* paint the buffer with colored tiles */
374         for (i = 0; i < width * height; i++) {
375                 d = div(i, width);
376                 fb_ptr[i] = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
377         }
378
379         cr = cairo_create(surface);
380         cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
381         for (x = 0; x < width; x += 250)
382                 for (y = 0; y < height; y += 250) {
383                         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
384                         cairo_move_to(cr, x, y - 20);
385                         cairo_line_to(cr, x, y + 20);
386                         cairo_move_to(cr, x - 20, y);
387                         cairo_line_to(cr, x + 20, y);
388                         cairo_new_sub_path(cr);
389                         cairo_arc(cr, x, y, 10, 0, M_PI * 2);
390                         cairo_set_line_width(cr, 4);
391                         cairo_set_source_rgb(cr, 0, 0, 0);
392                         cairo_stroke_preserve(cr);
393                         cairo_set_source_rgb(cr, 1, 1, 1);
394                         cairo_set_line_width(cr, 2);
395                         cairo_stroke(cr);
396                         snprintf(buf, sizeof buf, "%d, %d", x, y);
397                         cairo_move_to(cr, x + 20, y + 20);
398                         cairo_text_path(cr, buf);
399                         cairo_set_source_rgb(cr, 0, 0, 0);
400                         cairo_stroke_preserve(cr);
401                         cairo_set_source_rgb(cr, 1, 1, 1);
402                         cairo_fill(cr);
403                 }
404
405         cairo_destroy(cr);
406
407         bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
408         if (!bo) {
409                 fprintf(stderr, "failed to alloc buffer: %s\n",
410                         strerror(errno));
411                 return -1;
412         }
413
414         drm_intel_bo_subdata(bo, 0, size, fb_ptr);
415
416         cairo_surface_destroy(surface);
417
418         *bo_out = bo;
419         *stride_out = stride;
420
421         return 0;
422 }
423
424 #else
425
426 static int
427 create_test_buffer(drm_intel_bufmgr *bufmgr,
428                    int width, int height, int *stride_out, drm_intel_bo **bo_out)
429 {
430         drm_intel_bo *bo;
431         unsigned int *fb_ptr;
432         int size, ret, i, stride;
433         div_t d;
434
435         /* Mode size at 32 bpp */
436         stride = width * 4;
437         size = stride * height;
438
439         bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
440         if (!bo) {
441                 fprintf(stderr, "failed to alloc buffer: %s\n",
442                         strerror(errno));
443                 return -1;
444         }
445
446         ret = drm_intel_gem_bo_map_gtt(bo);
447         if (ret) {
448                 fprintf(stderr, "failed to GTT map buffer: %s\n",
449                         strerror(errno));
450                 return -1;
451         }
452
453         fb_ptr = bo->virtual;
454
455         /* paint the buffer with colored tiles */
456         for (i = 0; i < width * height; i++) {
457                 d = div(i, width);
458                 fb_ptr[i] = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
459         }
460         drm_intel_gem_bo_unmap_gtt(bo);
461
462         *bo_out = bo;
463         *stride_out = stride;
464
465         return 0;
466 }
467
468 #endif
469
470 static int
471 create_grey_buffer(drm_intel_bufmgr *bufmgr,
472                    int width, int height, int *stride_out, drm_intel_bo **bo_out)
473 {
474         drm_intel_bo *bo;
475         unsigned int *fb_ptr;
476         int size, ret, i, stride;
477         div_t d;
478
479         /* Mode size at 32 bpp */
480         stride = width * 4;
481         size = stride * height;
482
483         bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
484         if (!bo) {
485                 fprintf(stderr, "failed to alloc buffer: %s\n",
486                         strerror(errno));
487                 return -1;
488         }
489
490         ret = drm_intel_gem_bo_map_gtt(bo);
491         if (ret) {
492                 fprintf(stderr, "failed to GTT map buffer: %s\n",
493                         strerror(errno));
494                 return -1;
495         }
496
497         memset(bo->virtual, 0x77, size);
498         drm_intel_gem_bo_unmap_gtt(bo);
499
500         *bo_out = bo;
501         *stride_out = stride;
502
503         return 0;
504 }
505
506 void
507 page_flip_handler(int fd, unsigned int frame,
508                   unsigned int sec, unsigned int usec, void *data)
509 {
510         struct connector *c;
511         unsigned int new_fb_id;
512         int len, ms;
513         struct timeval end;
514         double t;
515
516         c = data;
517         if (c->current_fb_id == c->fb_id[0])
518                 new_fb_id = c->fb_id[1];
519         else
520                 new_fb_id = c->fb_id[0];
521                         
522         drmModePageFlip(fd, c->crtc, new_fb_id,
523                         DRM_MODE_PAGE_FLIP_EVENT, c);
524         c->current_fb_id = new_fb_id;
525         c->swap_count++;
526         if (c->swap_count == 60) {
527                 gettimeofday(&end, NULL);
528                 t = end.tv_sec + end.tv_usec * 1e-6 -
529                         (c->start.tv_sec + c->start.tv_usec * 1e-6);
530                 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
531                 c->swap_count = 0;
532                 c->start = end;
533         }
534 }
535
536 static void
537 set_mode(struct connector *c, int count, int page_flip)
538 {
539         drmModeConnector *connector;
540         drmModeEncoder *encoder = NULL;
541         struct drm_mode_modeinfo *mode = NULL;
542         drm_intel_bufmgr *bufmgr;
543         drm_intel_bo *bo, *other_bo;
544         unsigned int fb_id, other_fb_id;
545         int i, j, ret, width, height, x, stride;
546         drmEventContext evctx;
547
548         width = 0;
549         height = 0;
550         for (i = 0; i < count; i++) {
551                 connector_find_mode(&c[i]);
552                 if (c[i].mode == NULL)
553                         continue;
554                 width += c[i].mode->hdisplay;
555                 if (height < c[i].mode->vdisplay)
556                         height = c[i].mode->vdisplay;
557         }
558
559         bufmgr = drm_intel_bufmgr_gem_init(fd, 2<<20);
560         if (!bufmgr) {
561                 fprintf(stderr, "failed to init bufmgr: %s\n", strerror(errno));
562                 return;
563         }
564
565         if (create_test_buffer(bufmgr, width, height, &stride, &bo))
566                 return;
567
568         ret = drmModeAddFB(fd, width, height, 32, 32, stride, bo->handle,
569                            &fb_id);
570         if (ret) {
571                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
572                 return;
573         }
574
575         x = 0;
576         for (i = 0; i < count; i++) {
577                 if (c[i].mode == NULL)
578                         continue;
579
580                 printf("setting mode %s on connector %d, crtc %d\n",
581                        c[i].mode_str, c[i].id, c[i].crtc);
582
583                 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
584                                      &c[i].id, 1, c[i].mode);
585                 x += c[i].mode->hdisplay;
586
587                 if (ret) {
588                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
589                         return;
590                 }
591         }
592
593         if (!page_flip)
594                 return;
595
596         if (create_grey_buffer(bufmgr, width, height, &stride, &other_bo))
597                 return;
598
599         ret = drmModeAddFB(fd, width, height, 32, 32, stride, other_bo->handle,
600                            &other_fb_id);
601         if (ret) {
602                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
603                 return;
604         }
605
606         for (i = 0; i < count; i++) {
607                 if (c[i].mode == NULL)
608                         continue;
609
610                 drmModePageFlip(fd, c[i].crtc, other_fb_id,
611                                 DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
612                 gettimeofday(&c[i].start, NULL);
613                 c[i].swap_count = 0;
614                 c[i].fb_id[0] = fb_id;
615                 c[i].fb_id[1] = other_fb_id;
616                 c[i].current_fb_id = fb_id;
617         }
618
619         memset(&evctx, 0, sizeof evctx);
620         evctx.version = DRM_EVENT_CONTEXT_VERSION;
621         evctx.vblank_handler = NULL;
622         evctx.page_flip_handler = page_flip_handler;
623         
624         while (1) {
625                 struct pollfd pfd[2];
626
627                 pfd[0].fd = 0;
628                 pfd[0].events = POLLIN;
629                 pfd[1].fd = fd;
630                 pfd[1].events = POLLIN;
631
632                 if (poll(pfd, 2, -1) < 0) {
633                         fprintf(stderr, "poll error\n");
634                         break;
635                 }
636
637                 if (pfd[0].revents)
638                         break;
639
640                 drmHandleEvent(fd, &evctx);
641         }
642 }
643
644 extern char *optarg;
645 extern int optind, opterr, optopt;
646 static char optstr[] = "ecpmfs:v";
647
648 void usage(char *name)
649 {
650         fprintf(stderr, "usage: %s [-ecpmf]\n", name);
651         fprintf(stderr, "\t-e\tlist encoders\n");
652         fprintf(stderr, "\t-c\tlist connectors\n");
653         fprintf(stderr, "\t-p\tlist CRTCs (pipes)\n");
654         fprintf(stderr, "\t-m\tlist modes\n");
655         fprintf(stderr, "\t-f\tlist framebuffers\n");
656         fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
657         fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
658         fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
659         fprintf(stderr, "\n\tDefault is to dump all info.\n");
660         exit(0);
661 }
662
663 #define dump_resource(res) if (res) dump_##res()
664
665 static int page_flipping_supported(int fd)
666 {
667         int ret, value;
668         struct drm_i915_getparam gp;
669
670         gp.param = I915_PARAM_HAS_PAGEFLIPPING;
671         gp.value = &value;
672
673         ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
674         if (ret) {
675                 fprintf(stderr, "drm_i915_getparam: %m\n");
676                 return 0;
677         }
678
679         return gp.value;
680 }
681
682 int main(int argc, char **argv)
683 {
684         int c;
685         int encoders = 0, connectors = 0, crtcs = 0, framebuffers = 0;
686         int test_vsync = 0;
687         char *modules[] = { "i915", "radeon" };
688         char *modeset = NULL, *mode, *connector;
689         int i, connector_id, count = 0;
690         struct connector con_args[2];
691         
692         opterr = 0;
693         while ((c = getopt(argc, argv, optstr)) != -1) {
694                 switch (c) {
695                 case 'e':
696                         encoders = 1;
697                         break;
698                 case 'c':
699                         connectors = 1;
700                         break;
701                 case 'p':
702                         crtcs = 1;
703                         break;
704                 case 'm':
705                         modes = 1;
706                         break;
707                 case 'f':
708                         framebuffers = 1;
709                         break;
710                 case 'v':
711                         test_vsync = 1;
712                         break;
713                 case 's':
714                         modeset = strdup(optarg);
715                         con_args[count].crtc = -1;
716                         if (sscanf(optarg, "%d:%64s",
717                                    &con_args[count].id,
718                                    &con_args[count].mode_str) != 2 &&
719                             sscanf(optarg, "%d@%d:%64s",
720                                    &con_args[count].id,
721                                    &con_args[count].crtc,
722                                    &con_args[count].mode_str) != 3)
723                                 usage(argv[0]);
724                         count++;                                      
725                         break;
726                 default:
727                         usage(argv[0]);
728                         break;
729                 }
730         }
731
732         if (argc == 1)
733                 encoders = connectors = crtcs = modes = framebuffers = 1;
734
735         for (i = 0; i < ARRAY_SIZE(modules); i++) {
736                 printf("trying to load module %s...", modules[i]);
737                 fd = drmOpen(modules[i], NULL);
738                 if (fd < 0) {
739                         printf("failed.\n");
740                 } else {
741                         printf("success.\n");
742                         break;
743                 }
744         }
745
746         if (test_vsync && !page_flipping_supported(fd)) {
747                 fprintf(stderr, "page flipping not supported by drm.\n");
748                 return -1;
749         }
750
751         if (i == ARRAY_SIZE(modules)) {
752                 fprintf(stderr, "failed to load any modules, aborting.\n");
753                 return -1;
754         }
755
756         resources = drmModeGetResources(fd);
757         if (!resources) {
758                 fprintf(stderr, "drmModeGetResources failed: %s\n",
759                         strerror(errno));
760                 drmClose(fd);
761                 return 1;
762         }
763
764         dump_resource(encoders);
765         dump_resource(connectors);
766         dump_resource(crtcs);
767         dump_resource(framebuffers);
768
769         if (count > 0) {
770                 set_mode(con_args, count, test_vsync);
771                 getchar();
772         }
773
774         drmModeFreeResources(resources);
775
776         return 0;
777 }