| 1 |
/* |
| 2 |
* Copyright (C) 2010 Nokia Corporation <[email protected]> |
| 3 |
|
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2.1 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <stdarg.h> |
| 20 |
#include <dlfcn.h> |
| 21 |
#include <fcntl.h> |
| 22 |
#include <glob.h> |
| 23 |
#include <sys/stat.h> |
| 24 |
#include <sys/mman.h> |
| 25 |
#include <sys/types.h> |
| 26 |
#include <dirent.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include "libv4l2.h" |
| 30 |
#include "libv4l2-priv.h" |
| 31 |
#include "libv4l-plugin.h" |
| 32 |
|
| 33 |
/* libv4l plugin support: |
| 34 |
it is provided by functions v4l2_plugin_[open,close,etc]. |
| 35 |
|
| 36 |
When open() is called libv4l dlopens files in /usr/lib[64]/libv4l/plugins |
| 37 |
1 at a time and call open callback passing through the applications |
| 38 |
parameters unmodified. |
| 39 |
|
| 40 |
If a plugin is relevant for the specified device node, it can indicate so |
| 41 |
by returning a value other than -1 (the actual file descriptor). |
| 42 |
As soon as a plugin returns another value than -1 plugin loading stops and |
| 43 |
information about it (fd and corresponding library handle) is stored. For |
| 44 |
each function v4l2_[ioctl,read,close,etc] is called corresponding |
| 45 |
v4l2_plugin_* function which looks if there is loaded plugin for that file |
| 46 |
and call it's callbacks. |
| 47 |
|
| 48 |
v4l2_plugin_* function indicates by it's first argument if plugin was used, |
| 49 |
and if it was not then v4l2_* functions proceed with their usual behavior. |
| 50 |
*/ |
| 51 |
|
| 52 |
/* list of plugin search paths */ |
| 53 |
static const char *g_plugin_search_paths[] = { |
| 54 |
"/system/lib/libv4l/plugins", |
| 55 |
"/vendor/lib/libv4l/plugins", |
| 56 |
NULL /* list terminator */ |
| 57 |
}; |
| 58 |
|
| 59 |
void v4l2_plugin_init(int fd, void **plugin_lib_ret, void **plugin_priv_ret, |
| 60 |
const struct libv4l_dev_ops **dev_ops_ret) |
| 61 |
{ |
| 62 |
char *error; |
| 63 |
void *plugin_library = NULL; |
| 64 |
const struct libv4l_dev_ops *libv4l2_plugin = NULL; |
| 65 |
DIR *plugin_dir = NULL; |
| 66 |
struct dirent *entry; |
| 67 |
char *suffix = NULL; |
| 68 |
int length, i; |
| 69 |
char filename[256]; |
| 70 |
|
| 71 |
/* initialize output params */ |
| 72 |
*dev_ops_ret = v4lconvert_get_default_dev_ops(); |
| 73 |
*plugin_lib_ret = NULL; |
| 74 |
*plugin_priv_ret = NULL; |
| 75 |
|
| 76 |
/* read the plugin directory for "*.so" files */ |
| 77 |
for (i = 0; g_plugin_search_paths[i] != NULL; i++) { |
| 78 |
plugin_dir = opendir(g_plugin_search_paths[i]); |
| 79 |
if (plugin_dir == NULL) { |
| 80 |
V4L2_LOG_ERR("PLUGIN: opening plugin directory (%s) failed\n", |
| 81 |
g_plugin_search_paths[i]); |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
while (entry = readdir(plugin_dir)) { |
| 86 |
/* get last 3 letter suffix from the filename */ |
| 87 |
length = strlen(entry->d_name); |
| 88 |
if (length > 3) |
| 89 |
suffix = entry->d_name + (length - 3); |
| 90 |
|
| 91 |
if (!suffix || strcmp(suffix, ".so")) { |
| 92 |
suffix = NULL; /* reset for next iteration */ |
| 93 |
continue; |
| 94 |
} |
| 95 |
|
| 96 |
/* load library and get desired symbol */ |
| 97 |
sprintf(filename, "%s/%s", g_plugin_search_paths[i], entry->d_name); |
| 98 |
V4L2_LOG("PLUGIN: dlopen(%s);\n", filename); |
| 99 |
plugin_library = dlopen(filename, RTLD_LAZY); |
| 100 |
if (!plugin_library) |
| 101 |
continue; |
| 102 |
|
| 103 |
dlerror(); /* Clear any existing error */ |
| 104 |
libv4l2_plugin = (struct libv4l_dev_ops *) |
| 105 |
dlsym(plugin_library, "libv4l2_plugin"); |
| 106 |
error = dlerror(); |
| 107 |
if (error != NULL) { |
| 108 |
V4L2_LOG_ERR("PLUGIN: dlsym failed: %s\n", error); |
| 109 |
dlclose(plugin_library); |
| 110 |
continue; |
| 111 |
} |
| 112 |
|
| 113 |
if (!libv4l2_plugin->init || |
| 114 |
!libv4l2_plugin->close || |
| 115 |
!libv4l2_plugin->ioctl) { |
| 116 |
V4L2_LOG("PLUGIN: does not have all mandatory ops\n"); |
| 117 |
dlclose(plugin_library); |
| 118 |
continue; |
| 119 |
} |
| 120 |
|
| 121 |
*plugin_priv_ret = libv4l2_plugin->init(fd); |
| 122 |
if (!*plugin_priv_ret) { |
| 123 |
V4L2_LOG("PLUGIN: plugin open() returned NULL\n"); |
| 124 |
dlclose(plugin_library); |
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
/* exit loop when a suitable plugin is found */ |
| 129 |
*plugin_lib_ret = plugin_library; |
| 130 |
*dev_ops_ret = libv4l2_plugin; |
| 131 |
break; |
| 132 |
} |
| 133 |
closedir(plugin_dir); |
| 134 |
|
| 135 |
/* exit loop when a suitable plugin is found */ |
| 136 |
if (*plugin_lib_ret && *plugin_priv_ret && *dev_ops_ret) |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void v4l2_plugin_cleanup(void *plugin_lib, void *plugin_priv, |
| 142 |
const struct libv4l_dev_ops *dev_ops) |
| 143 |
{ |
| 144 |
if (plugin_lib) { |
| 145 |
dev_ops->close(plugin_priv); |
| 146 |
dlclose(plugin_lib); |
| 147 |
} |
| 148 |
} |