blob: d09f3b8cbd69ae902fcdf382b8ee5640f0c25899 [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 Wu15517d82015-02-20 13:41:57 -070033#include "icd-instance.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 Wu15517d82015-02-20 13:41:57 -070080static struct icd_drm_device *probe_syspath(const struct icd_instance *instance,
81 struct icd_drm_device *devices,
Chia-I Wu776ed422015-01-02 22:56:56 +080082 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 {
Chia-I Wu15517d82015-02-20 13:41:57 -0700127 dev = icd_instance_alloc(instance, sizeof(*dev), 0,
128 XGL_SYSTEM_ALLOC_INTERNAL_TEMP);
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800129 if (!dev)
130 return devices;
131
132 memset(dev, 0, sizeof(*dev));
133
134 dev->id = (const void *) parent_syspath;
135 dev->devid = devid;
136 dev->minors[type] = (void *) minor;
137
138 dev->next = devices;
139
140 return dev;
141 }
142}
143
Chia-I Wu15517d82015-02-20 13:41:57 -0700144struct icd_drm_device *icd_drm_enumerate(const struct icd_instance *instance,
145 int vendor_id)
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800146{
147 struct icd_drm_device *devices = NULL;
148 struct udev *udev;
149 struct udev_enumerate *e;
150 struct udev_list_entry *entry;
151
152 udev = udev_new();
153 if (udev == NULL) {
Chia-I Wu15517d82015-02-20 13:41:57 -0700154 icd_instance_log(instance, XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800155 XGL_NULL_HANDLE, 0, 0, "failed to initialize udev context");
156
157 return NULL;
158 }
159
160 e = udev_enumerate_new(udev);
161 if (e == NULL) {
Chia-I Wu15517d82015-02-20 13:41:57 -0700162 icd_instance_log(instance, XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800163 XGL_NULL_HANDLE, 0, 0,
164 "failed to initialize udev enumerate context");
165 udev_unref(udev);
166
167 return NULL;
168 }
169
170 /* we are interested in DRM minors */
171 udev_enumerate_add_match_subsystem(e, "drm");
172 udev_enumerate_add_match_property(e, "DEVTYPE", "drm_minor");
173 udev_enumerate_scan_devices(e);
174
175 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Chia-I Wu15517d82015-02-20 13:41:57 -0700176 devices = probe_syspath(instance, devices, udev,
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800177 udev_list_entry_get_name(entry), vendor_id);
178 }
179
Courtney Goeltzenleuchtere0f35a72014-09-25 18:13:21 -0600180 free(e);
181 free(udev);
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800182 return devices;
183}
184
Chia-I Wu15517d82015-02-20 13:41:57 -0700185void icd_drm_release(const struct icd_instance *instance,
186 struct icd_drm_device *devices)
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800187{
188 struct icd_drm_device *dev = devices;
189
190 while (dev) {
191 struct icd_drm_device *next = dev->next;
Ian Elliott642f8922015-02-13 14:29:21 -0700192 size_t i;
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800193
194 for (i = 0; i < ARRAY_SIZE(dev->minors); i++)
195 udev_device_unref((struct udev_device *) dev->minors[i]);
196
Chia-I Wu15517d82015-02-20 13:41:57 -0700197 icd_instance_free(instance, dev);
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800198 dev = next;
199 }
200}
201
202const char *icd_drm_get_devnode(struct icd_drm_device *dev,
203 enum icd_drm_minor_type minor)
204{
205 return (dev->minors[minor]) ?
206 udev_device_get_devnode((struct udev_device *) dev->minors[minor]) :
207 NULL;
208}