blob: ca1d849d978d253619913d9ea39f8e3c60416554 [file] [log] [blame]
Daniel Vetter7f7cafe2012-01-24 10:50:05 +01001/*
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
Thomas Wood804e11f2015-08-17 17:57:43 +010024#include "igt.h"
Daniel Vetter7f7cafe2012-01-24 10:50:05 +010025#include <stdio.h>
26#include <string.h>
27#include <stdlib.h>
Daniel Vetter7f7cafe2012-01-24 10:50:05 +010028
Chris Wilson4fbce7e2015-06-18 10:38:04 +010029#include <sys/stat.h>
30
Daniel Vetter7f7cafe2012-01-24 10:50:05 +010031#include "testdisplay.h"
Daniel Vetterfa461202012-01-24 11:28:25 +010032#include "config.h"
Daniel Vetter7f7cafe2012-01-24 10:50:05 +010033
Daniel Vetterc9c55452014-06-13 18:27:59 +020034
Arkadiusz Hiler17f9e362017-04-12 09:19:50 +020035#ifdef HAVE_UDEV
Daniel Vetterfa461202012-01-24 11:28:25 +010036#include <libudev.h>
Daniel Vetter7f7cafe2012-01-24 10:50:05 +010037static struct udev_monitor *uevent_monitor;
38static struct udev *udev;
39static GIOChannel *udevchannel;
40
41static gboolean hotplug_event(GIOChannel *source, GIOCondition condition,
42 gpointer data)
43{
44 struct udev_device *dev;
45 dev_t udev_devnum;
46 struct stat s;
47 const char *hotplug;
48
49 dev = udev_monitor_receive_device(uevent_monitor);
50 if (!dev)
51 goto out;
52
53 udev_devnum = udev_device_get_devnum(dev);
54 fstat(drm_fd, &s);
55
56 hotplug = udev_device_get_property_value(dev, "HOTPLUG");
57
58 if (memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)) == 0 &&
59 hotplug && atoi(hotplug) == 1)
Jesse Barnese28acef2016-01-14 14:03:53 -080060 update_display(true);
Daniel Vetter7f7cafe2012-01-24 10:50:05 +010061
62 udev_device_unref(dev);
63out:
64 return TRUE;
65}
66
67
68gboolean testdisplay_setup_hotplug(void)
69{
70 int ret;
71
72 udev = udev_new();
73 if (!udev) {
Daniel Vetterc9c55452014-06-13 18:27:59 +020074 igt_warn("failed to create udev object\n");
Daniel Vetter7f7cafe2012-01-24 10:50:05 +010075 goto out;
76 }
77
78 uevent_monitor = udev_monitor_new_from_netlink(udev, "udev");
79 if (!uevent_monitor) {
Daniel Vetterc9c55452014-06-13 18:27:59 +020080 igt_warn("failed to create udev event monitor\n");
Daniel Vetter7f7cafe2012-01-24 10:50:05 +010081 goto out;
82 }
83
84 ret = udev_monitor_filter_add_match_subsystem_devtype(uevent_monitor,
85 "drm",
86 "drm_minor");
87 if (ret < 0) {
Daniel Vetterc9c55452014-06-13 18:27:59 +020088 igt_warn("failed to filter for drm events\n");
Daniel Vetter7f7cafe2012-01-24 10:50:05 +010089 goto out;
90 }
91
92 ret = udev_monitor_enable_receiving(uevent_monitor);
93 if (ret < 0) {
Daniel Vetterc9c55452014-06-13 18:27:59 +020094 igt_warn("failed to enable udev event reception\n");
Daniel Vetter7f7cafe2012-01-24 10:50:05 +010095 goto out;
96 }
97
98 udevchannel =
99 g_io_channel_unix_new(udev_monitor_get_fd(uevent_monitor));
100 if (!udevchannel) {
Daniel Vetterc9c55452014-06-13 18:27:59 +0200101 igt_warn("failed to create udev GIO channel\n");
Daniel Vetter7f7cafe2012-01-24 10:50:05 +0100102 goto out;
103 }
104
105 ret = g_io_add_watch(udevchannel, G_IO_IN | G_IO_ERR, hotplug_event,
106 udev);
107 if (ret < 0) {
Daniel Vetterc9c55452014-06-13 18:27:59 +0200108 igt_warn("failed to add watch on udev GIO channel\n");
Daniel Vetter7f7cafe2012-01-24 10:50:05 +0100109 goto out;
110 }
111
112 return TRUE;
113
114out:
115 testdisplay_cleanup_hotplug();
116 return FALSE;
117}
118
119void testdisplay_cleanup_hotplug(void)
120{
121 if (udevchannel)
122 g_io_channel_shutdown(udevchannel, TRUE, NULL);
123 if (uevent_monitor)
124 udev_monitor_unref(uevent_monitor);
125 if (udev)
126 udev_unref(udev);
127}
Daniel Vetterfa461202012-01-24 11:28:25 +0100128#else
129gboolean testdisplay_setup_hotplug(void)
130{
Daniel Vetterc9c55452014-06-13 18:27:59 +0200131 igt_warn("no hotplug support on this platform\n");
Daniel Vetterfa461202012-01-24 11:28:25 +0100132 return TRUE;
133}
134
135void testdisplay_cleanup_hotplug(void)
136{
137}
138#endif