1 /**************************************************************************
2 *
3 * Copyright 2012 Francisco Jerez
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "pipe_loader_priv.h"
29
30 #include "util/u_inlines.h"
31 #include "util/u_memory.h"
32 #include "util/u_string.h"
33 #include "util/u_dl.h"
34
35 #ifdef _MSC_VER
36 #include <stdlib.h>
37 #define PATH_MAX _MAX_PATH
38 #endif
39
40 #define MODULE_PREFIX "pipe_"
41
42 static int (*backends[])(struct pipe_loader_device **, int) = {
43 #ifdef HAVE_LIBDRM
44 &pipe_loader_drm_probe,
45 #endif
46 &pipe_loader_sw_probe
47 };
48
49 int
pipe_loader_probe(struct pipe_loader_device ** devs,int ndev)50 pipe_loader_probe(struct pipe_loader_device **devs, int ndev)
51 {
52 int i, n = 0;
53
54 for (i = 0; i < ARRAY_SIZE(backends); i++)
55 n += backends[i](&devs[n], MAX2(0, ndev - n));
56
57 return n;
58 }
59
60 void
pipe_loader_release(struct pipe_loader_device ** devs,int ndev)61 pipe_loader_release(struct pipe_loader_device **devs, int ndev)
62 {
63 int i;
64
65 for (i = 0; i < ndev; i++)
66 devs[i]->ops->release(&devs[i]);
67 }
68
69 const struct drm_conf_ret *
pipe_loader_configuration(struct pipe_loader_device * dev,enum drm_conf conf)70 pipe_loader_configuration(struct pipe_loader_device *dev,
71 enum drm_conf conf)
72 {
73 return dev->ops->configuration(dev, conf);
74 }
75
76 struct pipe_screen *
pipe_loader_create_screen(struct pipe_loader_device * dev)77 pipe_loader_create_screen(struct pipe_loader_device *dev)
78 {
79 return dev->ops->create_screen(dev);
80 }
81
82 struct util_dl_library *
pipe_loader_find_module(struct pipe_loader_device * dev,const char * library_paths)83 pipe_loader_find_module(struct pipe_loader_device *dev,
84 const char *library_paths)
85 {
86 struct util_dl_library *lib;
87 const char *next;
88 char path[PATH_MAX];
89 int len, ret;
90
91 for (next = library_paths; *next; library_paths = next + 1) {
92 next = util_strchrnul(library_paths, ':');
93 len = next - library_paths;
94
95 if (len)
96 ret = util_snprintf(path, sizeof(path), "%.*s/%s%s%s",
97 len, library_paths,
98 MODULE_PREFIX, dev->driver_name, UTIL_DL_EXT);
99 else
100 ret = util_snprintf(path, sizeof(path), "%s%s%s",
101 MODULE_PREFIX, dev->driver_name, UTIL_DL_EXT);
102
103 if (ret > 0 && ret < sizeof(path)) {
104 lib = util_dl_open(path);
105 if (lib) {
106 return lib;
107 }
108 }
109 }
110
111 return NULL;
112 }
113