2 * Copyright © 2007 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
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
18 * THE 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
24 * Eric Anholt <eric@anholt.net>
32 #include <sys/ioctl.h>
35 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
38 static int is_master(int fd)
43 /* Check that we're the only opener and authed. */
45 ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
50 ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
51 if (ret != -1 || errno != EINVAL)
57 /** Open the first DRM device matching the criteria */
58 int drm_open_matching(const char *pci_glob, int flags)
61 struct udev_enumerate *e;
62 struct udev_device *device, *parent;
63 struct udev_list_entry *entry;
64 const char *pci_id, *path;
65 const char *usub, *dnode;
70 fprintf(stderr, "failed to initialize udev context\n");
75 e = udev_enumerate_new(udev);
76 udev_enumerate_add_match_subsystem(e, "drm");
77 udev_enumerate_scan_devices(e);
78 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
79 path = udev_list_entry_get_name(entry);
80 device = udev_device_new_from_syspath(udev, path);
81 parent = udev_device_get_parent(device);
82 usub = udev_device_get_subsystem(parent);
83 /* Filter out KMS output devices. */
84 if (!usub || (strcmp(usub, "pci") != 0))
86 pci_id = udev_device_get_property_value(parent, "PCI_ID");
87 if (fnmatch(pci_glob, pci_id, 0) != 0)
89 dnode = udev_device_get_devnode(device);
90 if (strstr(dnode, "control"))
92 fd = open(dnode, O_RDWR);
95 if ((flags & DRM_TEST_MASTER) && !is_master(fd)) {
103 udev_enumerate_unref(e);
109 int drm_open_any(void)
111 int fd = drm_open_matching("*:*", 0);
114 fprintf(stderr, "failed to open any drm device\n");
122 * Open the first DRM device we can find where we end up being the master.
124 int drm_open_any_master(void)
126 int fd = drm_open_matching("*:*", DRM_TEST_MASTER);
129 fprintf(stderr, "failed to open any drm device\n");