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 TUNGSTEN GRAPHICS 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 #define MODULE_PREFIX "pipe_"
36 
37 static int (*backends[])(struct pipe_loader_device **, int) = {
38 #ifdef HAVE_PIPE_LOADER_DRM
39    &pipe_loader_drm_probe,
40 #endif
41    &pipe_loader_sw_probe
42 };
43 
44 int
pipe_loader_probe(struct pipe_loader_device ** devs,int ndev)45 pipe_loader_probe(struct pipe_loader_device **devs, int ndev)
46 {
47    int i, n = 0;
48 
49    for (i = 0; i < Elements(backends); i++)
50       n += backends[i](&devs[n], MAX2(0, ndev - n));
51 
52    return n;
53 }
54 
55 void
pipe_loader_release(struct pipe_loader_device ** devs,int ndev)56 pipe_loader_release(struct pipe_loader_device **devs, int ndev)
57 {
58    int i;
59 
60    for (i = 0; i < ndev; i++)
61       devs[i]->ops->release(&devs[i]);
62 }
63 
64 struct pipe_screen *
pipe_loader_create_screen(struct pipe_loader_device * dev,const char * library_paths)65 pipe_loader_create_screen(struct pipe_loader_device *dev,
66                           const char *library_paths)
67 {
68    return dev->ops->create_screen(dev, library_paths);
69 }
70 
71 struct util_dl_library *
pipe_loader_find_module(struct pipe_loader_device * dev,const char * library_paths)72 pipe_loader_find_module(struct pipe_loader_device *dev,
73                         const char *library_paths)
74 {
75    struct util_dl_library *lib;
76    const char *next;
77    char path[PATH_MAX];
78    int len, ret;
79 
80    for (next = library_paths; *next; library_paths = next + 1) {
81       next = util_strchrnul(library_paths, ':');
82       len = next - library_paths;
83 
84       if (len)
85          ret = util_snprintf(path, sizeof(path), "%.*s/%s%s%s",
86                              len, library_paths,
87                              MODULE_PREFIX, dev->driver_name, UTIL_DL_EXT);
88       else
89          ret = util_snprintf(path, sizeof(path), "%s%s%s",
90                              MODULE_PREFIX, dev->driver_name, UTIL_DL_EXT);
91 
92       if (ret > 0 && ret < sizeof(path)) {
93          lib = util_dl_open(path);
94          if (lib) {
95             debug_printf("loaded %s\n", path);
96             return lib;
97          }
98       }
99    }
100 
101    return NULL;
102 }
103