1 /*
2 * Copyright 2017 Intel Corporation
3 * Jesse Barnes <jesse.barnes@intel.com>
4 * Manasi Navare <manasi.d.navare@intel.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "igt.h"
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <sys/stat.h>
31
32 #include "intel_dp_compliance.h"
33 #include <libudev.h>
34 static struct udev_monitor *uevent_monitor;
35 static struct udev *udev;
36 static GIOChannel *udevchannel;
37
hotplug_event(GIOChannel * source,GIOCondition condition,gpointer data)38 static gboolean hotplug_event(GIOChannel *source, GIOCondition condition,
39 gpointer data)
40 {
41 struct udev_device *dev;
42 dev_t udev_devnum;
43 struct stat s;
44 const char *hotplug;
45
46 dev = udev_monitor_receive_device(uevent_monitor);
47 if (!dev)
48 goto out;
49
50 udev_devnum = udev_device_get_devnum(dev);
51 fstat(drm_fd, &s);
52
53 hotplug = udev_device_get_property_value(dev, "HOTPLUG");
54
55 if (memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)) == 0 &&
56 hotplug && atoi(hotplug) == 1)
57 update_display(0, false);
58
59 udev_device_unref(dev);
60 out:
61 return TRUE;
62 }
63
64
intel_dp_compliance_setup_hotplug(void)65 gboolean intel_dp_compliance_setup_hotplug(void)
66 {
67 int ret;
68
69 udev = udev_new();
70 if (!udev) {
71 igt_warn("Failed to create udev object\n");
72 goto out;
73 }
74
75 uevent_monitor = udev_monitor_new_from_netlink(udev, "udev");
76 if (!uevent_monitor) {
77 igt_warn("Failed to create udev event monitor\n");
78 goto out;
79 }
80
81 ret = udev_monitor_filter_add_match_subsystem_devtype(uevent_monitor,
82 "drm",
83 "drm_minor");
84 if (ret < 0) {
85 igt_warn("Failed to filter for drm events\n");
86 goto out;
87 }
88
89 ret = udev_monitor_enable_receiving(uevent_monitor);
90 if (ret < 0) {
91 igt_warn("Failed to enable udev event reception\n");
92 goto out;
93 }
94
95 udevchannel =
96 g_io_channel_unix_new(udev_monitor_get_fd(uevent_monitor));
97 if (!udevchannel) {
98 igt_warn("Failed to create udev GIOChannel\n");
99 goto out;
100 }
101
102 ret = g_io_add_watch(udevchannel, G_IO_IN | G_IO_ERR, hotplug_event,
103 udev);
104 if (ret < 0) {
105 igt_warn("Failed to add watch on udev GIOChannel\n");
106 goto out;
107 }
108
109 return TRUE;
110
111 out:
112 intel_dp_compliance_cleanup_hotplug();
113 return FALSE;
114 }
115
intel_dp_compliance_cleanup_hotplug(void)116 void intel_dp_compliance_cleanup_hotplug(void)
117 {
118 if (udevchannel)
119 g_io_channel_shutdown(udevchannel, TRUE, NULL);
120 if (uevent_monitor)
121 udev_monitor_unref(uevent_monitor);
122 if (udev)
123 udev_unref(udev);
124 }
125