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