OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright © 2007 Intel Corporation |
| 3 * |
| 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: |
| 10 * |
| 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 |
| 13 * 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 |
| 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 |
| 21 * IN THE SOFTWARE. |
| 22 * |
| 23 * Authors: |
| 24 * Eric Anholt <eric@anholt.net> |
| 25 * |
| 26 */ |
| 27 |
| 28 #include <string.h> |
| 29 #include <fcntl.h> |
| 30 #include <fnmatch.h> |
| 31 #include <sys/stat.h> |
| 32 #include <sys/ioctl.h> |
| 33 #include "drmtest.h" |
| 34 |
| 35 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE |
| 36 #include <libudev.h> |
| 37 |
| 38 static int is_master(int fd) |
| 39 { |
| 40 drm_client_t client; |
| 41 int ret; |
| 42 |
| 43 /* Check that we're the only opener and authed. */ |
| 44 client.idx = 0; |
| 45 ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client); |
| 46 assert (ret == 0); |
| 47 if (!client.auth) |
| 48 return 0; |
| 49 client.idx = 1; |
| 50 ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client); |
| 51 if (ret != -1 || errno != EINVAL) |
| 52 return 0; |
| 53 |
| 54 return 1; |
| 55 } |
| 56 |
| 57 /** Open the first DRM device matching the criteria */ |
| 58 int drm_open_matching(const char *pci_glob, int flags, int *vendor_id, int *devi
ce_id) |
| 59 { |
| 60 struct udev *udev; |
| 61 struct udev_enumerate *e; |
| 62 struct udev_device *device, *parent; |
| 63 struct udev_list_entry *entry; |
| 64 const char *pci_id, *path; |
| 65 char *tmp; |
| 66 int fd; |
| 67 |
| 68 *vendor_id = 0; |
| 69 *device_id = 0; |
| 70 |
| 71 udev = udev_new(); |
| 72 if (udev == NULL) { |
| 73 fprintf(stderr, "failed to initialize udev context\n"); |
| 74 return -1; |
| 75 //abort(); |
| 76 } |
| 77 |
| 78 fd = -1; |
| 79 e = udev_enumerate_new(udev); |
| 80 udev_enumerate_add_match_subsystem(e, "drm"); |
| 81 udev_enumerate_scan_devices(e); |
| 82 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 83 path = udev_list_entry_get_name(entry); |
| 84 device = udev_device_new_from_syspath(udev, path); |
| 85 parent = udev_device_get_parent(device); |
| 86 /* Filter out KMS output devices. */ |
| 87 if (strcmp(udev_device_get_subsystem(parent), "pci") != 0) |
| 88 continue; |
| 89 pci_id = udev_device_get_property_value(parent, "PCI_ID"); |
| 90 if (fnmatch(pci_glob, pci_id, 0) != 0) |
| 91 continue; |
| 92 fd = open(udev_device_get_devnode(device), O_RDWR); |
| 93 if (fd < 0) |
| 94 continue; |
| 95 if ((flags & DRM_TEST_MASTER) && !is_master(fd)) { |
| 96 close(fd); |
| 97 fd = -1; |
| 98 continue; |
| 99 } |
| 100 |
| 101 break; |
| 102 } |
| 103 udev_enumerate_unref(e); |
| 104 udev_unref(udev); |
| 105 |
| 106 *vendor_id = (int) strtol(pci_id, &tmp, 16); |
| 107 *device_id = (int) strtol((tmp+1), NULL, 16); |
| 108 |
| 109 return fd; |
| 110 } |
| 111 |
| 112 int drm_open_any(int *vendor_id, int *device_id) |
| 113 { |
| 114 int fd = drm_open_matching("*:*", 0, vendor_id, device_id); |
| 115 |
| 116 if (fd < 0) { |
| 117 fprintf(stderr, "failed to open any drm device\n"); |
| 118 //abort(); |
| 119 } |
| 120 |
| 121 return fd; |
| 122 } |
| 123 |
| 124 /** |
| 125 * Open the first DRM device we can find where we end up being the master. |
| 126 */ |
| 127 int drm_open_any_master(void) |
| 128 { |
| 129 int vendor_id, device_id; |
| 130 int fd = drm_open_matching("*:*", DRM_TEST_MASTER, &vendor_id, &device_i
d); |
| 131 |
| 132 if (fd < 0) { |
| 133 fprintf(stderr, "failed to open any drm device\n"); |
| 134 abort(); |
| 135 } |
| 136 |
| 137 return fd; |
| 138 |
| 139 } |
OLD | NEW |