blob: c00bd5d10cae282adb0c09f40389977ad7130d79 [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
Chia-I Wu900364b2015-01-03 13:55:22 +080033#include "icd-alloc.h"
Chia-I Wu412ce1b2014-09-15 13:08:49 +080034#include "icd-utils.h"
35#include "icd-enumerate-drm.h"
36
37static enum icd_drm_minor_type get_minor_type(struct udev_device *minor_dev)
38{
39 const char *minor;
40
41 minor = udev_device_get_property_value(minor_dev, "MINOR");
42 if (!minor)
43 return ICD_DRM_MINOR_INVALID;
44
45 switch (atoi(minor) >> 6) {
46 case 0:
47 return ICD_DRM_MINOR_LEGACY;
48 case 2:
49 return ICD_DRM_MINOR_RENDER;
50 default:
51 return ICD_DRM_MINOR_INVALID;
52 }
53}
54
55static void get_pci_id(struct udev_device *pci_dev, int *vendor, int *devid)
56{
57 const char *pci_id;
58
59 pci_id = udev_device_get_property_value(pci_dev, "PCI_ID");
60 if (sscanf(pci_id, "%x:%x", vendor, devid) != 2) {
61 *vendor = 0;
62 *devid = 0;
63 }
64}
65
66static struct icd_drm_device *find_dev(struct icd_drm_device *devices,
67 const char *parent_syspath)
68{
69 struct icd_drm_device *dev = devices;
70
71 while (dev) {
72 if (!strcmp((const char *) dev->id, parent_syspath))
73 break;
74 dev = dev->next;
75 }
76
77 return dev;
78}
79
Chia-I Wu776ed422015-01-02 22:56:56 +080080static struct icd_drm_device *probe_syspath(struct icd_drm_device *devices,
81 struct udev *udev, const char *syspath,
82 int vendor_id_match)
Chia-I Wu412ce1b2014-09-15 13:08:49 +080083{
84 struct udev_device *minor, *parent;
85 enum icd_drm_minor_type type;
86 const char *parent_syspath;
87 struct icd_drm_device *dev;
88 int vendor, devid;
89
90 minor = udev_device_new_from_syspath(udev, syspath);
91 if (!minor)
92 return devices;
93
94 type = get_minor_type(minor);
95 if (type == ICD_DRM_MINOR_INVALID) {
96 udev_device_unref(minor);
97 return devices;
98 }
99
100 parent = udev_device_get_parent(minor);
101 if (!parent) {
102 udev_device_unref(minor);
103 return devices;
104 }
105
106 get_pci_id(parent, &vendor, &devid);
107 if (vendor_id_match && vendor != vendor_id_match) {
108 udev_device_unref(minor);
109 return devices;
110 }
111
112 parent_syspath = udev_device_get_syspath(parent);
113
114 dev = find_dev(devices, parent_syspath);
115 if (dev) {
116 assert(dev->devid == devid);
117
118 assert(!dev->minors[type]);
119 if (dev->minors[type])
120 udev_device_unref((struct udev_device *) dev->minors[type]);
121
122 dev->minors[type] = (void *) minor;
123
124 return devices;
125 } else {
126 dev = icd_alloc(sizeof(*dev), 0, XGL_SYSTEM_ALLOC_INTERNAL_TEMP);
127 if (!dev)
128 return devices;
129
130 memset(dev, 0, sizeof(*dev));
131
132 dev->id = (const void *) parent_syspath;
133 dev->devid = devid;
134 dev->minors[type] = (void *) minor;
135
136 dev->next = devices;
137
138 return dev;
139 }
140}
141
142struct icd_drm_device *icd_drm_enumerate(int vendor_id)
143{
144 struct icd_drm_device *devices = NULL;
145 struct udev *udev;
146 struct udev_enumerate *e;
147 struct udev_list_entry *entry;
148
149 udev = udev_new();
150 if (udev == NULL) {
151 icd_log(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
152 XGL_NULL_HANDLE, 0, 0, "failed to initialize udev context");
153
154 return NULL;
155 }
156
157 e = udev_enumerate_new(udev);
158 if (e == NULL) {
159 icd_log(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
160 XGL_NULL_HANDLE, 0, 0,
161 "failed to initialize udev enumerate context");
162 udev_unref(udev);
163
164 return NULL;
165 }
166
167 /* we are interested in DRM minors */
168 udev_enumerate_add_match_subsystem(e, "drm");
169 udev_enumerate_add_match_property(e, "DEVTYPE", "drm_minor");
170 udev_enumerate_scan_devices(e);
171
172 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
173 devices = probe_syspath(devices, udev,
174 udev_list_entry_get_name(entry), vendor_id);
175 }
176
Courtney Goeltzenleuchtere0f35a72014-09-25 18:13:21 -0600177 free(e);
178 free(udev);
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800179 return devices;
180}
181
182void icd_drm_release(struct icd_drm_device *devices)
183{
184 struct icd_drm_device *dev = devices;
185
186 while (dev) {
187 struct icd_drm_device *next = dev->next;
188 int i;
189
190 for (i = 0; i < ARRAY_SIZE(dev->minors); i++)
191 udev_device_unref((struct udev_device *) dev->minors[i]);
192
193 icd_free(dev);
194 dev = next;
195 }
196}
197
198const char *icd_drm_get_devnode(struct icd_drm_device *dev,
199 enum icd_drm_minor_type minor)
200{
201 return (dev->minors[minor]) ?
202 udev_device_get_devnode((struct udev_device *) dev->minors[minor]) :
203 NULL;
204}