packaging: only on x86* arch
[platform/upstream/intel-gpu-tools.git] / tests / testdisplay.c
1 /*
2  * Copyright 2010 Intel Corporation
3  *   Jesse Barnes <jesse.barnes@intel.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23
24 /*
25  * This program is intended for testing of display functionality.  It should
26  * allow for testing of
27  *   - hotplug
28  *   - mode setting
29  *   - clone & twin modes
30  *   - panel fitting
31  *   - test patterns & pixel generators
32  * Additional programs can test the detected outputs against VBT provided
33  * device lists (both docked & undocked).
34  *
35  * TODO:
36  * - pixel generator in transcoder
37  * - test pattern reg in pipe
38  * - test patterns on outputs (e.g. TV)
39  * - handle hotplug (leaks crtcs, can't handle clones)
40  * - allow mode force
41  * - expose output specific controls
42  *  - e.g. DDC-CI brightness
43  *  - HDMI controls
44  *  - panel brightness
45  *  - DP commands (e.g. poweroff)
46  * - verify outputs against VBT/physical connectors
47  */
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52 #include <cairo.h>
53 #include <errno.h>
54 #include <math.h>
55 #include <stdint.h>
56 #include <stdbool.h>
57 #include <strings.h>
58 #include <unistd.h>
59 #include <termios.h>
60 #include <sys/poll.h>
61 #include <sys/time.h>
62 #include <sys/ioctl.h>
63 #include <sys/types.h>
64 #include <sys/stat.h>
65
66 #include "ioctl_wrappers.h"
67 #include "drmtest.h"
68 #include "testdisplay.h"
69 #include "igt_kms.h"
70
71 #include <stdlib.h>
72 #include <signal.h>
73
74 #define SUBTEST_OPTS 1
75
76 static int tio_fd;
77 struct termios saved_tio;
78
79 drmModeRes *resources;
80 int drm_fd, modes;
81 int test_all_modes = 0, test_preferred_mode = 0, force_mode = 0, test_plane,
82     test_stereo_modes;
83 unsigned int tiling = I915_TILING_NONE;
84 int sleep_between_modes = 5;
85 int do_dpms = 0; /* This aliases to DPMS_ON */
86 uint32_t depth = 24, stride, bpp;
87 int qr_code = 0;
88 int specified_mode_num = -1, specified_disp_id = -1;
89
90 drmModeModeInfo force_timing;
91
92 int crtc_x, crtc_y, crtc_w, crtc_h, width, height;
93 unsigned int plane_fb_id;
94 unsigned int plane_crtc_id;
95 unsigned int plane_id;
96 int plane_width, plane_height;
97 static const uint32_t SPRITE_COLOR_KEY = 0x00aaaaaa;
98
99 /*
100  * Mode setting with the kernel interfaces is a bit of a chore.
101  * First you have to find the connector in question and make sure the
102  * requested mode is available.
103  * Then you need to find the encoder attached to that connector so you
104  * can bind it with a free crtc.
105  */
106 struct connector {
107         uint32_t id;
108         int mode_valid;
109         drmModeModeInfo mode;
110         drmModeEncoder *encoder;
111         drmModeConnector *connector;
112         int crtc;
113         int crtc_idx;
114         int pipe;
115 };
116
117 static void dump_connectors_fd(int drmfd)
118 {
119         int i, j;
120
121         drmModeRes *mode_resources = drmModeGetResources(drmfd);
122
123         if (!mode_resources) {
124                 igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
125                 return;
126         }
127
128         igt_info("Connectors:\n");
129         igt_info("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\n");
130         for (i = 0; i < mode_resources->count_connectors; i++) {
131                 drmModeConnector *connector;
132
133                 connector = drmModeGetConnector(drmfd, mode_resources->connectors[i]);
134                 if (!connector) {
135                         igt_warn("could not get connector %i: %s\n", mode_resources->connectors[i], strerror(errno));
136                         continue;
137                 }
138
139                 igt_info("%d\t%d\t%s\t%s\t%dx%d\t\t%d\n", connector->connector_id, connector->encoder_id, kmstest_connector_status_str(connector->connection), kmstest_connector_type_str(connector->connector_type), connector->mmWidth, connector->mmHeight, connector->count_modes);
140
141                 if (!connector->count_modes)
142                         continue;
143
144                 igt_info("  modes:\n");
145                 igt_info("  name refresh (Hz) hdisp hss hse htot vdisp ""vss vse vtot flags type clock\n");
146                 for (j = 0; j < connector->count_modes; j++){
147                         igt_info("[%d]", j);
148                         kmstest_dump_mode(&connector->modes[j]);
149                 }
150
151                 drmModeFreeConnector(connector);
152         }
153         igt_info("\n");
154
155         drmModeFreeResources(mode_resources);
156 }
157
158 static void dump_crtcs_fd(int drmfd)
159 {
160         int i;
161         drmModeRes *mode_resources = drmModeGetResources(drmfd);
162
163         igt_info("CRTCs:\n");
164         igt_info("id\tfb\tpos\tsize\n");
165         for (i = 0; i < mode_resources->count_crtcs; i++) {
166                 drmModeCrtc *crtc;
167
168                 crtc = drmModeGetCrtc(drmfd, mode_resources->crtcs[i]);
169                 if (!crtc) {
170                         igt_warn("could not get crtc %i: %s\n", mode_resources->crtcs[i], strerror(errno));
171                         continue;
172                 }
173                 igt_info("%d\t%d\t(%d,%d)\t(%dx%d)\n", crtc->crtc_id, crtc->buffer_id, crtc->x, crtc->y, crtc->width, crtc->height);
174                 kmstest_dump_mode(&crtc->mode);
175
176                 drmModeFreeCrtc(crtc);
177         }
178         igt_info("\n");
179
180         drmModeFreeResources(mode_resources);
181 }
182
183 static void dump_info(void)
184 {
185         dump_connectors_fd(drm_fd);
186         dump_crtcs_fd(drm_fd);
187 }
188
189 static void connector_find_preferred_mode(uint32_t connector_id,
190                                           unsigned long crtc_idx_mask,
191                                           int mode_num, struct connector *c)
192 {
193         struct kmstest_connector_config config;
194
195         if (!kmstest_get_connector_config(drm_fd, connector_id, crtc_idx_mask,
196                                           &config)) {
197                 c->mode_valid = 0;
198                 return;
199         }
200
201         c->connector = config.connector;
202         c->encoder = config.encoder;
203         c->crtc = config.crtc->crtc_id;
204         c->crtc_idx = config.crtc_idx;
205         c->pipe = config.pipe;
206
207         if (mode_num != -1) {
208                 igt_assert(mode_num < config.connector->count_modes);
209                 c->mode = config.connector->modes[mode_num];
210         } else {
211                 c->mode = config.default_mode;
212         }
213         c->mode_valid = 1;
214 }
215
216 static void
217 paint_color_key(struct igt_fb *fb_info)
218 {
219         int i, j;
220         uint32_t *fb_ptr;
221
222         fb_ptr = gem_mmap(drm_fd, fb_info->gem_handle,
223                           fb_info->size, PROT_READ | PROT_WRITE);
224         igt_assert(fb_ptr);
225
226         for (i = crtc_y; i < crtc_y + crtc_h; i++)
227                 for (j = crtc_x; j < crtc_x + crtc_w; j++) {
228                         uint32_t offset;
229
230                         offset = (i * fb_info->stride / 4) + j;
231                         fb_ptr[offset] = SPRITE_COLOR_KEY;
232                 }
233
234         munmap(fb_ptr, fb_info->size);
235 }
236
237 static void paint_image(cairo_t *cr, const char *file)
238 {
239         int img_x, img_y, img_w, img_h, img_w_o, img_h_o;
240         double img_w_scale, img_h_scale;
241
242         cairo_surface_t *image;
243
244         img_y = height * (0.10 );
245         img_h = height * 0.08 * 4;
246         img_w = img_h;
247
248         img_x = (width / 2) - (img_w / 2);
249
250         image = cairo_image_surface_create_from_png(file);
251
252         img_w_o = cairo_image_surface_get_width(image);
253         img_h_o = cairo_image_surface_get_height(image);
254
255         cairo_translate(cr, img_x, img_y);
256
257         img_w_scale = (double)img_w / (double)img_w_o;
258         img_h_scale = (double)img_h / (double)img_h_o;
259         cairo_scale(cr, img_w_scale, img_h_scale);
260
261         cairo_set_source_surface(cr, image, 0, 0);
262         cairo_scale(cr, 1, 1);
263
264         cairo_paint(cr);
265         cairo_surface_destroy(image);
266 }
267
268 static void paint_output_info(struct connector *c, struct igt_fb *fb)
269 {
270         cairo_t *cr = igt_get_cairo_ctx(drm_fd, fb);
271         int l_width = fb->width;
272         int l_height = fb->height;
273         double str_width;
274         double x, y, top_y;
275         double max_width;
276         int i;
277
278         igt_paint_test_pattern(cr, l_width, l_height);
279
280         cairo_move_to(cr, l_width / 2, l_height / 2);
281
282         /* Print connector and mode name */
283         cairo_set_font_size(cr, 48);
284         igt_cairo_printf_line(cr, align_hcenter, 10, "%s",
285                  kmstest_connector_type_str(c->connector->connector_type));
286
287         cairo_set_font_size(cr, 36);
288         str_width = igt_cairo_printf_line(cr, align_hcenter, 10,
289                 "%s @ %dHz on %s encoder", c->mode.name, c->mode.vrefresh,
290                 kmstest_encoder_type_str(c->encoder->encoder_type));
291
292         cairo_rel_move_to(cr, -str_width / 2, 0);
293
294         /* List available modes */
295         cairo_set_font_size(cr, 18);
296         str_width = igt_cairo_printf_line(cr, align_left, 10,
297                                               "Available modes:");
298         cairo_rel_move_to(cr, str_width, 0);
299         cairo_get_current_point(cr, &x, &top_y);
300
301         max_width = 0;
302         for (i = 0; i < c->connector->count_modes; i++) {
303                 cairo_get_current_point(cr, &x, &y);
304                 if (y >= l_height) {
305                         x += max_width + 10;
306                         max_width = 0;
307                         cairo_move_to(cr, x, top_y);
308                 }
309                 str_width = igt_cairo_printf_line(cr, align_right, 10,
310                         "%s @ %dHz", c->connector->modes[i].name,
311                          c->connector->modes[i].vrefresh);
312                 if (str_width > max_width)
313                         max_width = str_width;
314         }
315
316         if (qr_code)
317                 paint_image(cr, IGT_DATADIR"/pass.png");
318
319         igt_assert(!cairo_status(cr));
320
321         cairo_destroy(cr);
322 }
323
324 static void sighandler(int signo)
325 {
326         return;
327 }
328
329 static void set_single(void)
330 {
331         int sigs[] = { SIGUSR1 };
332         struct sigaction sa;
333         sa.sa_handler = sighandler;
334
335         sigemptyset(&sa.sa_mask);
336
337         igt_warn_on_f(sigaction(sigs[0], &sa, NULL) == -1,
338                       "Could not set signal handler");
339 }
340
341 static void
342 set_mode(struct connector *c)
343 {
344         unsigned int fb_id = 0;
345         struct igt_fb fb_info[2] = { };
346         int j, test_mode_num, current_fb = 0, old_fb = -1;
347
348         test_mode_num = 1;
349         if (force_mode){
350                 memcpy( &c->mode, &force_timing, sizeof(force_timing));
351                 c->mode.vrefresh =(force_timing.clock*1e3)/(force_timing.htotal*force_timing.vtotal);
352                 c->mode_valid = 1;
353                 sprintf(c->mode.name, "%dx%d", force_timing.hdisplay, force_timing.vdisplay);
354         } else if (test_all_modes)
355                 test_mode_num = c->connector->count_modes;
356
357         for (j = 0; j < test_mode_num; j++) {
358
359                 if (test_all_modes)
360                         c->mode = c->connector->modes[j];
361
362                 /* set_mode() only tests 2D modes */
363                 if (c->mode.flags & DRM_MODE_FLAG_3D_MASK)
364                         continue;
365
366                 if (!c->mode_valid)
367                         continue;
368
369                 width = c->mode.hdisplay;
370                 height = c->mode.vdisplay;
371
372                 fb_id = igt_create_fb(drm_fd, width, height,
373                                       igt_bpp_depth_to_drm_format(bpp, depth),
374                                       tiling, &fb_info[current_fb]);
375                 paint_output_info(c, &fb_info[current_fb]);
376                 paint_color_key(&fb_info[current_fb]);
377
378                 igt_info("CRTC(%u):[%d]", c->crtc, j);
379                 kmstest_dump_mode(&c->mode);
380                 if (drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0,
381                                    &c->id, 1, &c->mode)) {
382                         igt_warn("failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
383                         continue;
384                 }
385
386                 if (old_fb != -1)
387                         igt_remove_fb(drm_fd, &fb_info[old_fb]);
388                 old_fb = current_fb;
389                 current_fb = 1 - current_fb;
390
391                 if (sleep_between_modes && test_all_modes && !qr_code)
392                         sleep(sleep_between_modes);
393
394                 if (do_dpms) {
395                         kmstest_set_connector_dpms(drm_fd, c->connector, do_dpms);
396                         sleep(sleep_between_modes);
397                         kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
398                 }
399
400                 if (qr_code){
401                         set_single();
402                         pause();
403                 }
404         }
405
406         if (test_all_modes)
407                 igt_remove_fb(drm_fd, &fb_info[old_fb]);
408
409         drmModeFreeEncoder(c->encoder);
410         drmModeFreeConnector(c->connector);
411 }
412
413 static void do_set_stereo_mode(struct connector *c)
414 {
415         uint32_t fb_id;
416
417         fb_id = igt_create_stereo_fb(drm_fd, &c->mode,
418                                      igt_bpp_depth_to_drm_format(bpp, depth),
419                                      tiling);
420
421         igt_warn_on_f(drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0, &c->id, 1, &c->mode),
422                       "failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
423 }
424
425 static void
426 set_stereo_mode(struct connector *c)
427 {
428         int i, n;
429
430
431         if (specified_mode_num != -1)
432                 n = 1;
433         else
434                 n = c->connector->count_modes;
435
436         for (i = 0; i < n; i++) {
437                 if (specified_mode_num == -1)
438                         c->mode = c->connector->modes[i];
439
440                 if (!c->mode_valid)
441                         continue;
442
443                 if (!(c->mode.flags & DRM_MODE_FLAG_3D_MASK))
444                         continue;
445
446                 igt_info("CRTC(%u): [%d]", c->crtc, i);
447                 kmstest_dump_mode(&c->mode);
448                 do_set_stereo_mode(c);
449
450                 if (qr_code) {
451                         set_single();
452                         pause();
453                 } else if (sleep_between_modes)
454                         sleep(sleep_between_modes);
455
456                 if (do_dpms) {
457                         kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_OFF);
458                         sleep(sleep_between_modes);
459                         kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
460                 }
461         }
462
463         drmModeFreeEncoder(c->encoder);
464         drmModeFreeConnector(c->connector);
465 }
466
467 /*
468  * Re-probe outputs and light up as many as possible.
469  *
470  * On Intel, we have two CRTCs that we can drive independently with
471  * different timings and scanout buffers.
472  *
473  * Each connector has a corresponding encoder, except in the SDVO case
474  * where an encoder may have multiple connectors.
475  */
476 int update_display(void)
477 {
478         struct connector *connectors;
479         int c;
480
481         resources = drmModeGetResources(drm_fd);
482         if (!resources) {
483                 igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
484                 return 0;
485         }
486
487         connectors = calloc(resources->count_connectors,
488                             sizeof(struct connector));
489         if (!connectors)
490                 return 0;
491
492         if (test_preferred_mode || test_all_modes ||
493             force_mode || specified_disp_id != -1) {
494                 unsigned long crtc_idx_mask = -1UL;
495
496                 /* Find any connected displays */
497                 for (c = 0; c < resources->count_connectors; c++) {
498                         struct connector *connector = &connectors[c];
499
500                         connector->id = resources->connectors[c];
501                         if (specified_disp_id != -1 &&
502                             connector->id != specified_disp_id)
503                                 continue;
504
505                         connector_find_preferred_mode(connector->id,
506                                                       crtc_idx_mask,
507                                                       specified_mode_num,
508                                                       connector);
509                         if (!connector->mode_valid)
510                                 continue;
511
512                         set_mode(connector);
513
514                         if (test_preferred_mode || force_mode ||
515                             specified_mode_num != -1)
516                                 crtc_idx_mask &= ~(1 << connector->crtc_idx);
517
518                 }
519         }
520
521         if (test_stereo_modes) {
522                 for (c = 0; c < resources->count_connectors; c++) {
523                         struct connector *connector = &connectors[c];
524
525                         connector->id = resources->connectors[c];
526                         if (specified_disp_id != -1 &&
527                             connector->id != specified_disp_id)
528                                 continue;
529
530                         connector_find_preferred_mode(connector->id,
531                                                       -1UL,
532                                                       specified_mode_num,
533                                                       connector);
534                         if (!connector->mode_valid)
535                                 continue;
536
537                         set_stereo_mode(connector);
538                 }
539         }
540
541         free(connectors);
542         drmModeFreeResources(resources);
543         return 1;
544 }
545
546 static char optstr[] = "3hiaf:s:d:p:mrto:j:";
547
548 static void __attribute__((noreturn)) usage(char *name)
549 {
550         igt_info("usage: %s [-hiasdpmtf]\n", name);
551         igt_info("\t-i\tdump info\n");
552         igt_info("\t-a\ttest all modes\n");
553         igt_info("\t-s\t<duration>\tsleep between each mode test\n");
554         igt_info("\t-d\t<depth>\tbit depth of scanout buffer\n");
555         igt_info("\t-p\t<planew,h>,<crtcx,y>,<crtcw,h> test overlay plane\n");
556         igt_info("\t-m\ttest the preferred mode\n");
557         igt_info("\t-3\ttest all 3D modes\n");
558         igt_info("\t-t\tuse a tiled framebuffer\n");
559         igt_info("\t-j\tdo dpms off, optional arg to select dpms leve (1-3)\n");
560         igt_info("\t-r\tprint a QR code on the screen whose content is \"pass\" for the automatic test\n");
561         igt_info("\t-o\t<id of the display>,<number of the mode>\tonly test specified mode on the specified display\n");
562         igt_info("\t-f\t<clock MHz>,<hdisp>,<hsync-start>,<hsync-end>,<htotal>,\n");
563         igt_info("\t\t<vdisp>,<vsync-start>,<vsync-end>,<vtotal>\n");
564         igt_info("\t\ttest force mode\n");
565         igt_info("\tDefault is to test all modes.\n");
566         exit((optopt) ? -1 : 0);
567 }
568
569 #define dump_resource(res) if (res) dump_##res()
570
571 static void cleanup_and_exit(int ret)
572 {
573         close(drm_fd);
574         exit(ret);
575 }
576
577 static gboolean input_event(GIOChannel *source, GIOCondition condition,
578                                 gpointer data)
579 {
580         gchar buf[2];
581         gsize count;
582
583         count = read(g_io_channel_unix_get_fd(source), buf, sizeof(buf));
584         if (buf[0] == 'q' && (count == 1 || buf[1] == '\n')) {
585                 cleanup_and_exit(0);
586         }
587
588         return TRUE;
589 }
590
591 static void enter_exec_path( char **argv )
592 {
593         char *exec_path = NULL;
594         char *pos = NULL;
595         short len_path = 0;
596         int ret;
597
598         len_path = strlen( argv[0] );
599         exec_path = (char*) malloc(len_path);
600
601         memcpy(exec_path, argv[0], len_path);
602         pos = strrchr(exec_path, '/');
603         if (pos != NULL)
604                 *(pos+1) = '\0';
605
606         ret = chdir(exec_path);
607         igt_assert(ret == 0);
608         free(exec_path);
609 }
610
611 static void restore_termio_mode(int sig)
612 {
613         tcsetattr(tio_fd, TCSANOW, &saved_tio);
614         close(tio_fd);
615 }
616
617 static void set_termio_mode(void)
618 {
619         struct termios tio;
620
621         /* don't attempt to set terminal attributes if not in the foreground
622          * process group */
623         if (getpgrp() != tcgetpgrp(STDOUT_FILENO))
624                 return;
625
626         tio_fd = dup(STDIN_FILENO);
627         tcgetattr(tio_fd, &saved_tio);
628         igt_install_exit_handler(restore_termio_mode);
629         tio = saved_tio;
630         tio.c_lflag &= ~(ICANON | ECHO);
631         tcsetattr(tio_fd, TCSANOW, &tio);
632 }
633
634 int main(int argc, char **argv)
635 {
636         int c;
637         int ret = 0;
638         GIOChannel *stdinchannel;
639         GMainLoop *mainloop;
640         float force_clock;
641         bool opt_dump_info = false;
642         struct option long_opts[] = {
643                 {"list-subtests", 0, 0, SUBTEST_OPTS},
644                 {"run-subtest", 1, 0, SUBTEST_OPTS},
645                 { 0, 0, 0, 0 }
646         };
647
648         igt_skip_on_simulation();
649
650         enter_exec_path( argv );
651
652         while ((c = getopt_long(argc, argv, optstr, long_opts, NULL)) != -1) {
653                 switch (c) {
654                 case '3':
655                         test_stereo_modes = 1;
656                         break;
657                 case 'i':
658                         opt_dump_info = true;
659                         break;
660                 case 'a':
661                         test_all_modes = 1;
662                         break;
663                 case 'f':
664                         force_mode = 1;
665                         if(sscanf(optarg,"%f,%hu,%hu,%hu,%hu,%hu,%hu,%hu,%hu",
666                                 &force_clock,&force_timing.hdisplay, &force_timing.hsync_start,&force_timing.hsync_end,&force_timing.htotal,
667                                 &force_timing.vdisplay, &force_timing.vsync_start, &force_timing.vsync_end, &force_timing.vtotal)!= 9)
668                                 usage(argv[0]);
669                         force_timing.clock = force_clock*1000;
670
671                         break;
672                 case 's':
673                         sleep_between_modes = atoi(optarg);
674                         break;
675                 case 'j':
676                         do_dpms = atoi(optarg);
677                         if (do_dpms == 0)
678                                 do_dpms = DRM_MODE_DPMS_OFF;
679                         break;
680                 case 'd':
681                         depth = atoi(optarg);
682                         igt_info("using depth %d\n", depth);
683                         break;
684                 case 'p':
685                         if (sscanf(optarg, "%d,%d,%d,%d,%d,%d", &plane_width,
686                                    &plane_height, &crtc_x, &crtc_y,
687                                    &crtc_w, &crtc_h) != 6)
688                                 usage(argv[0]);
689                         test_plane = 1;
690                         break;
691                 case 'm':
692                         test_preferred_mode = 1;
693                         break;
694                 case 't':
695                         tiling = I915_TILING_X;
696                         break;
697                 case 'r':
698                         qr_code = 1;
699                         break;
700                 case 'o':
701                         sscanf(optarg, "%d,%d", &specified_disp_id, &specified_mode_num);
702                         break;
703                 case SUBTEST_OPTS:
704                         /* invalid subtest options */
705                         exit(IGT_EXIT_INVALID);
706                         break;
707                 default:
708                         /* fall through */
709                 case 'h':
710                         usage(argv[0]);
711                         break;
712                 }
713         }
714
715         set_termio_mode();
716
717         if (depth <= 8)
718                 bpp = 8;
719         else if (depth <= 16)
720                 bpp = 16;
721         else if (depth <= 32)
722                 bpp = 32;
723
724         if (!test_all_modes && !force_mode && !test_preferred_mode &&
725             specified_mode_num == -1 && !test_stereo_modes)
726                 test_all_modes = 1;
727
728         drm_fd = drm_open_any();
729
730         if (test_stereo_modes &&
731             drmSetClientCap(drm_fd, DRM_CLIENT_CAP_STEREO_3D, 1) < 0) {
732                 igt_warn("DRM_CLIENT_CAP_STEREO_3D failed\n");
733                 goto out_close;
734         }
735
736         if (opt_dump_info) {
737                 dump_info();
738                 goto out_close;
739         }
740
741         kmstest_set_vt_graphics_mode();
742
743         mainloop = g_main_loop_new(NULL, FALSE);
744         if (!mainloop) {
745                 igt_warn("failed to create glib mainloop\n");
746                 ret = -1;
747                 goto out_close;
748         }
749
750         if (!testdisplay_setup_hotplug()) {
751                 igt_warn("failed to initialize hotplug support\n");
752                 goto out_mainloop;
753         }
754
755         stdinchannel = g_io_channel_unix_new(0);
756         if (!stdinchannel) {
757                 igt_warn("failed to create stdin GIO channel\n");
758                 goto out_hotplug;
759         }
760
761         ret = g_io_add_watch(stdinchannel, G_IO_IN | G_IO_ERR, input_event,
762                              NULL);
763         if (ret < 0) {
764                 igt_warn("failed to add watch on stdin GIO channel\n");
765                 goto out_stdio;
766         }
767
768         ret = 0;
769
770         if (!update_display()) {
771                 ret = 1;
772                 goto out_stdio;
773         }
774
775         if (test_all_modes)
776                 goto out_stdio;
777
778         g_main_loop_run(mainloop);
779
780 out_stdio:
781         g_io_channel_shutdown(stdinchannel, TRUE, NULL);
782 out_hotplug:
783         testdisplay_cleanup_hotplug();
784 out_mainloop:
785         g_main_loop_unref(mainloop);
786 out_close:
787         close(drm_fd);
788
789         return ret;
790 }