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