eleni@0: /* eleni@0: * Copyright © 2007 Intel Corporation eleni@0: * eleni@0: * Permission is hereby granted, free of charge, to any person obtaining a eleni@0: * copy of this software and associated documentation files (the "Software"), eleni@0: * to deal in the Software without restriction, including without limitation eleni@0: * the rights to use, copy, modify, merge, publish, distribute, sublicense, eleni@0: * and/or sell copies of the Software, and to permit persons to whom the eleni@0: * Software is furnished to do so, subject to the following conditions: eleni@0: * eleni@0: * The above copyright notice and this permission notice (including the next eleni@0: * paragraph) shall be included in all copies or substantial portions of the eleni@0: * Software. eleni@0: * eleni@0: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR eleni@0: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, eleni@0: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL eleni@0: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER eleni@0: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING eleni@0: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS eleni@0: * IN THE SOFTWARE. eleni@0: * eleni@0: * Authors: eleni@0: * Eric Anholt eleni@0: * eleni@0: */ eleni@0: eleni@0: #include eleni@0: eleni@0: #include eleni@0: #include eleni@0: #include eleni@0: #include eleni@0: #include "drmtest.h" eleni@0: eleni@0: #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE eleni@0: #include eleni@0: eleni@0: static int is_master(int fd) eleni@0: { eleni@0: drm_client_t client; eleni@0: int ret; eleni@0: eleni@0: /* Check that we're the only opener and authed. */ eleni@0: client.idx = 0; eleni@0: ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client); eleni@0: assert (ret == 0); eleni@0: if (!client.auth) eleni@0: return 0; eleni@0: client.idx = 1; eleni@0: ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client); eleni@0: if (ret != -1 || errno != EINVAL) eleni@0: return 0; eleni@0: eleni@0: return 1; eleni@0: } eleni@0: eleni@0: /** Open the first DRM device matching the criteria */ eleni@0: int drm_open_matching(const char *pci_glob, int flags) eleni@0: { eleni@0: struct udev *udev; eleni@0: struct udev_enumerate *e; eleni@0: struct udev_device *device, *parent; eleni@0: struct udev_list_entry *entry; eleni@0: const char *pci_id, *path; eleni@0: int fd; eleni@0: eleni@0: udev = udev_new(); eleni@0: if (udev == NULL) { eleni@0: fprintf(stderr, "failed to initialize udev context\n"); eleni@0: abort(); eleni@0: } eleni@0: eleni@0: fd = -1; eleni@0: e = udev_enumerate_new(udev); eleni@0: udev_enumerate_add_match_subsystem(e, "drm"); eleni@0: udev_enumerate_scan_devices(e); eleni@0: udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { eleni@0: path = udev_list_entry_get_name(entry); eleni@0: device = udev_device_new_from_syspath(udev, path); eleni@0: parent = udev_device_get_parent(device); eleni@0: /* Filter out KMS output devices. */ eleni@0: if (strcmp(udev_device_get_subsystem(parent), "pci") != 0) eleni@0: continue; eleni@0: pci_id = udev_device_get_property_value(parent, "PCI_ID"); eleni@0: if (fnmatch(pci_glob, pci_id, 0) != 0) eleni@0: continue; eleni@0: fd = open(udev_device_get_devnode(device), O_RDWR); eleni@0: if (fd < 0) eleni@0: continue; eleni@0: if ((flags & DRM_TEST_MASTER) && !is_master(fd)) { eleni@0: close(fd); eleni@0: fd = -1; eleni@0: continue; eleni@0: } eleni@0: eleni@0: break; eleni@0: } eleni@0: udev_enumerate_unref(e); eleni@0: udev_unref(udev); eleni@0: eleni@0: return fd; eleni@0: } eleni@0: eleni@0: int drm_open_any(void) eleni@0: { eleni@0: int fd = drm_open_matching("*:*", 0); eleni@0: eleni@0: if (fd < 0) { eleni@0: fprintf(stderr, "failed to open any drm device\n"); eleni@0: abort(); eleni@0: } eleni@0: eleni@0: return fd; eleni@0: } eleni@0: eleni@0: /** eleni@0: * Open the first DRM device we can find where we end up being the master. eleni@0: */ eleni@0: int drm_open_any_master(void) eleni@0: { eleni@0: int fd = drm_open_matching("*:*", DRM_TEST_MASTER); eleni@0: eleni@0: if (fd < 0) { eleni@0: fprintf(stderr, "failed to open any drm device\n"); eleni@0: abort(); eleni@0: } eleni@0: eleni@0: return fd; eleni@0: eleni@0: }