blob: e3ceea8dc02ac43b92a389627ca75edac1acb37c [file] [log] [blame]
Chia-I Wu412ce1b2014-09-15 13:08:49 +08001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include <libudev.h>
32
33#include "icd-utils.h"
34#include "icd-enumerate-drm.h"
35
36static enum icd_drm_minor_type get_minor_type(struct udev_device *minor_dev)
37{
38 const char *minor;
39
40 minor = udev_device_get_property_value(minor_dev, "MINOR");
41 if (!minor)
42 return ICD_DRM_MINOR_INVALID;
43
44 switch (atoi(minor) >> 6) {
45 case 0:
46 return ICD_DRM_MINOR_LEGACY;
47 case 2:
48 return ICD_DRM_MINOR_RENDER;
49 default:
50 return ICD_DRM_MINOR_INVALID;
51 }
52}
53
54static void get_pci_id(struct udev_device *pci_dev, int *vendor, int *devid)
55{
56 const char *pci_id;
57
58 pci_id = udev_device_get_property_value(pci_dev, "PCI_ID");
59 if (sscanf(pci_id, "%x:%x", vendor, devid) != 2) {
60 *vendor = 0;
61 *devid = 0;
62 }
63}
64
65static struct icd_drm_device *find_dev(struct icd_drm_device *devices,
66 const char *parent_syspath)
67{
68 struct icd_drm_device *dev = devices;
69
70 while (dev) {
71 if (!strcmp((const char *) dev->id, parent_syspath))
72 break;
73 dev = dev->next;
74 }
75
76 return dev;
77}
78
79struct icd_drm_device *probe_syspath(struct icd_drm_device *devices,
80 struct udev *udev, const char *syspath,
81 int vendor_id_match)
82{
83 struct udev_device *minor, *parent;
84 enum icd_drm_minor_type type;
85 const char *parent_syspath;
86 struct icd_drm_device *dev;
87 int vendor, devid;
88
89 minor = udev_device_new_from_syspath(udev, syspath);
90 if (!minor)
91 return devices;
92
93 type = get_minor_type(minor);
94 if (type == ICD_DRM_MINOR_INVALID) {
95 udev_device_unref(minor);
96 return devices;
97 }
98
99 parent = udev_device_get_parent(minor);
100 if (!parent) {
101 udev_device_unref(minor);
102 return devices;
103 }
104
105 get_pci_id(parent, &vendor, &devid);
106 if (vendor_id_match && vendor != vendor_id_match) {
107 udev_device_unref(minor);
108 return devices;
109 }
110
111 parent_syspath = udev_device_get_syspath(parent);
112
113 dev = find_dev(devices, parent_syspath);
114 if (dev) {
115 assert(dev->devid == devid);
116
117 assert(!dev->minors[type]);
118 if (dev->minors[type])
119 udev_device_unref((struct udev_device *) dev->minors[type]);
120
121 dev->minors[type] = (void *) minor;
122
123 return devices;
124 } else {
125 dev = icd_alloc(sizeof(*dev), 0, XGL_SYSTEM_ALLOC_INTERNAL_TEMP);
126 if (!dev)
127 return devices;
128
129 memset(dev, 0, sizeof(*dev));
130
131 dev->id = (const void *) parent_syspath;
132 dev->devid = devid;
133 dev->minors[type] = (void *) minor;
134
135 dev->next = devices;
136
137 return dev;
138 }
139}
140
141struct icd_drm_device *icd_drm_enumerate(int vendor_id)
142{
143 struct icd_drm_device *devices = NULL;
144 struct udev *udev;
145 struct udev_enumerate *e;
146 struct udev_list_entry *entry;
147
148 udev = udev_new();
149 if (udev == NULL) {
150 icd_log(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
151 XGL_NULL_HANDLE, 0, 0, "failed to initialize udev context");
152
153 return NULL;
154 }
155
156 e = udev_enumerate_new(udev);
157 if (e == NULL) {
158 icd_log(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
159 XGL_NULL_HANDLE, 0, 0,
160 "failed to initialize udev enumerate context");
161 udev_unref(udev);
162
163 return NULL;
164 }
165
166 /* we are interested in DRM minors */
167 udev_enumerate_add_match_subsystem(e, "drm");
168 udev_enumerate_add_match_property(e, "DEVTYPE", "drm_minor");
169 udev_enumerate_scan_devices(e);
170
171 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
172 devices = probe_syspath(devices, udev,
173 udev_list_entry_get_name(entry), vendor_id);
174 }
175
176 return devices;
177}
178
179void icd_drm_release(struct icd_drm_device *devices)
180{
181 struct icd_drm_device *dev = devices;
182
183 while (dev) {
184 struct icd_drm_device *next = dev->next;
185 int i;
186
187 for (i = 0; i < ARRAY_SIZE(dev->minors); i++)
188 udev_device_unref((struct udev_device *) dev->minors[i]);
189
190 icd_free(dev);
191 dev = next;
192 }
193}
194
195const char *icd_drm_get_devnode(struct icd_drm_device *dev,
196 enum icd_drm_minor_type minor)
197{
198 return (dev->minors[minor]) ?
199 udev_device_get_devnode((struct udev_device *) dev->minors[minor]) :
200 NULL;
201}