• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "igt.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 
29 #include <sys/stat.h>
30 
31 #include "testdisplay.h"
32 #include "config.h"
33 
34 
35 #include <libudev.h>
36 static struct udev_monitor *uevent_monitor;
37 static struct udev *udev;
38 static GIOChannel *udevchannel;
39 
hotplug_event(GIOChannel * source,GIOCondition condition,gpointer data)40 static gboolean hotplug_event(GIOChannel *source, GIOCondition condition,
41 			      gpointer data)
42 {
43 	struct udev_device *dev;
44 	dev_t udev_devnum;
45 	struct stat s;
46 	const char *hotplug;
47 
48 	dev = udev_monitor_receive_device(uevent_monitor);
49 	if (!dev)
50 		goto out;
51 
52 	udev_devnum = udev_device_get_devnum(dev);
53 	fstat(drm_fd, &s);
54 
55 	hotplug = udev_device_get_property_value(dev, "HOTPLUG");
56 
57 	if (memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)) == 0 &&
58 	    hotplug && atoi(hotplug) == 1)
59 		update_display(true);
60 
61 	udev_device_unref(dev);
62 out:
63 	return TRUE;
64 }
65 
66 
testdisplay_setup_hotplug(void)67 gboolean testdisplay_setup_hotplug(void)
68 {
69 	int ret;
70 
71 	udev = udev_new();
72 	if (!udev) {
73 		igt_warn("failed to create udev object\n");
74 		goto out;
75 	}
76 
77 	uevent_monitor = udev_monitor_new_from_netlink(udev, "udev");
78 	if (!uevent_monitor) {
79 		igt_warn("failed to create udev event monitor\n");
80 		goto out;
81 	}
82 
83 	ret = udev_monitor_filter_add_match_subsystem_devtype(uevent_monitor,
84 							      "drm",
85 							      "drm_minor");
86 	if (ret < 0) {
87 		igt_warn("failed to filter for drm events\n");
88 		goto out;
89 	}
90 
91 	ret = udev_monitor_enable_receiving(uevent_monitor);
92 	if (ret < 0) {
93 		igt_warn("failed to enable udev event reception\n");
94 		goto out;
95 	}
96 
97 	udevchannel =
98 		g_io_channel_unix_new(udev_monitor_get_fd(uevent_monitor));
99 	if (!udevchannel) {
100 		igt_warn("failed to create udev GIO channel\n");
101 		goto out;
102 	}
103 
104 	ret = g_io_add_watch(udevchannel, G_IO_IN | G_IO_ERR, hotplug_event,
105 			     udev);
106 	if (ret < 0) {
107 		igt_warn("failed to add watch on udev GIO channel\n");
108 		goto out;
109 	}
110 
111 	return TRUE;
112 
113 out:
114 	testdisplay_cleanup_hotplug();
115 	return FALSE;
116 }
117 
testdisplay_cleanup_hotplug(void)118 void testdisplay_cleanup_hotplug(void)
119 {
120 	if (udevchannel)
121 		g_io_channel_shutdown(udevchannel, TRUE, NULL);
122 	if (uevent_monitor)
123 		udev_monitor_unref(uevent_monitor);
124 	if (udev)
125 		udev_unref(udev);
126 }
127