0
|
1 |
/*
|
|
2 |
* CDDL HEADER START
|
|
3 |
*
|
|
4 |
* The contents of this file are subject to the terms of the
|
|
5 |
* Common Development and Distribution License (the "License").
|
|
6 |
* You may not use this file except in compliance with the License.
|
|
7 |
*
|
|
8 |
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
9 |
* or http://www.opensolaris.org/os/licensing.
|
|
10 |
* See the License for the specific language governing permissions
|
|
11 |
* and limitations under the License.
|
|
12 |
*
|
|
13 |
* When distributing Covered Code, include this CDDL HEADER in each
|
|
14 |
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
15 |
* If applicable, add the following below this CDDL HEADER, with the
|
|
16 |
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
17 |
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
18 |
*
|
|
19 |
* CDDL HEADER END
|
|
20 |
*/
|
|
21 |
/*
|
62
|
22 |
* Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
|
0
|
23 |
*/
|
|
24 |
|
62
|
25 |
#pragma ident "@(#)wr_libusb.c 1.6 10/12/20 SMI"
|
0
|
26 |
|
|
27 |
#include <stdlib.h>
|
|
28 |
#include <stdio.h>
|
|
29 |
#include <sys/types.h>
|
|
30 |
#include <dirent.h>
|
|
31 |
#include <errno.h>
|
|
32 |
#include <strings.h>
|
|
33 |
#include <dlfcn.h>
|
|
34 |
#include <thread.h>
|
|
35 |
#include <synch.h>
|
|
36 |
#include <stdarg.h>
|
|
37 |
#include "usb.h"
|
|
38 |
#include "wr_libusb.h"
|
|
39 |
|
|
40 |
/*
|
|
41 |
* libusb wrapper library
|
|
42 |
*
|
|
43 |
* Implements the libusb specification and calls into platform dependent
|
|
44 |
* library implementations of sunray and solaris.
|
|
45 |
*
|
|
46 |
* Loads the .so's found in the default directory or pointed
|
|
47 |
* to by the environment variable SUN_LIBUSBPLUGIN_DIR
|
|
48 |
* If there are problems with the module then unload
|
|
49 |
* the module and continue
|
|
50 |
*
|
|
51 |
* Reads plugin versions from the libusb_version variable
|
|
52 |
* Reads plugin prefixes from libusb_prefix variable
|
|
53 |
* Calls libusb_init implementation of the plugin to determine whether
|
|
54 |
* the plugin bus is supported, unsupported, or exclusive.
|
|
55 |
* This is from the return values 0, -1, 2
|
|
56 |
*
|
|
57 |
* In the exclusive case all others plugins will be disabled
|
|
58 |
* Case1: ret = PLUGIN_EXCLUSIVE [2]:
|
|
59 |
* Sun Ray Environment where server bus is not requested
|
|
60 |
* via an environment variable
|
|
61 |
* Case2: ret = FAILURE [-1]
|
|
62 |
* In the failure case the scenario is an app running
|
|
63 |
* in a workstation where the sunray bus cannot be made available.
|
|
64 |
* Case3: ret = SUCCESS [0]:
|
|
65 |
* All busses are supported
|
|
66 |
*
|
|
67 |
* The plugins implement certain policies to determine whether access
|
|
68 |
* to their bus will be granted and how.
|
|
69 |
* These policies are internal to the plugin and not relevant to the wrapper
|
|
70 |
* The wrapper merely supports the implementation of these policies
|
|
71 |
* via environment variables and init return values.
|
|
72 |
*
|
|
73 |
* Loads all the symbols of the plugins and maps apps calls -> plugin
|
|
74 |
*
|
|
75 |
* The wrapper library maintains the open device handle states to be
|
|
76 |
* able to map the device handle to the correct plugin. As such each
|
|
77 |
* open and close results in updates to the dev handle list.
|
|
78 |
*
|
|
79 |
*/
|
|
80 |
|
|
81 |
|
|
82 |
static int read_plugin_entries(void);
|
|
83 |
static int load_plugin_syms(plugin_info_t *, void *, int);
|
|
84 |
static void wusb_symbol(char **, int, char *, char *);
|
|
85 |
static int get_pindex(struct usb_dev_handle *, const char *);
|
|
86 |
static void unload_plugin(int);
|
|
87 |
static int load_plugins();
|
|
88 |
static int get_index_devicep(struct usb_device *);
|
|
89 |
static int get_index_devhdl(struct usb_dev_handle *);
|
|
90 |
static int usb_add_dev(struct usb_dev_handle *, struct usb_device *, int);
|
|
91 |
static void usb_del_dev(struct usb_dev_handle *, int);
|
|
92 |
static int check_sym_hdl(int, int);
|
|
93 |
static void *lookup_sym_hdl(usb_dev_handle *, int, const char *);
|
|
94 |
static void usb_dprintf(int level, char *format, ...);
|
|
95 |
static int is_libusb_plugin(char *);
|
|
96 |
|
|
97 |
static int libusb_debug = DEBUG_NONE;
|
|
98 |
struct usb_bus *usb_busses;
|
|
99 |
static mutex_t bus_lock = DEFAULTMUTEX;
|
|
100 |
static wrapper_info_t winfo;
|
|
101 |
static plugin_info_t p_info[MAX_PLUGINS];
|
|
102 |
|
|
103 |
#ifdef _LP64
|
|
104 |
|
|
105 |
#define PLUGIN_PREFIX "/64/"
|
|
106 |
|
|
107 |
#else /* _LP64 */
|
|
108 |
|
|
109 |
#define PLUGIN_PREFIX ""
|
|
110 |
|
|
111 |
#endif /* _LP64 */
|
|
112 |
|
|
113 |
|
|
114 |
/*
|
|
115 |
* Reads the plugin libraries from the specified dir
|
|
116 |
* or the user supplied SUN_LIBUSBPLUGIN_DIR
|
|
117 |
* populates the p_name and p_path fields of the array of plugin_info structs
|
|
118 |
* increments the array index pindex after each .so is read
|
|
119 |
* returns -1(FAILURE) on error
|
|
120 |
*/
|
|
121 |
static int
|
|
122 |
read_plugin_entries()
|
|
123 |
{
|
|
124 |
int pindex = 0;
|
|
125 |
int plugin_found = 0;
|
|
126 |
char *alt_dir;
|
|
127 |
DIR *dirp;
|
|
128 |
struct dirent *dp;
|
|
129 |
char *tmpplugindir = PLUGIN_DIR;
|
|
130 |
char *suffix;
|
|
131 |
char modpath[PATH_MAX + 1];
|
|
132 |
char plugindir[PATH_MAX + 1];
|
|
133 |
|
|
134 |
if ((alt_dir = getenv("SUN_LIBUSBPLUGIN_DIR")) != NULL) {
|
|
135 |
tmpplugindir = alt_dir;
|
|
136 |
}
|
|
137 |
|
|
138 |
if (strnlen(tmpplugindir, PATH_MAX) >
|
|
139 |
(PATH_MAX - sizeof (PLUGIN_PREFIX))) {
|
|
140 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
141 |
"Invalid plugin directory: %s\n", tmpplugindir);
|
|
142 |
|
|
143 |
return (FAILURE);
|
|
144 |
}
|
|
145 |
|
|
146 |
/*
|
|
147 |
* Construct the plugin directory
|
|
148 |
* Default plugin for 32 bit: /usr/lib/libusb_plugins/
|
|
149 |
* for 64 bit: /usr/lib/libusb_plugins/64/
|
|
150 |
*
|
|
151 |
* If SUN_LIBUSBPLUGIN_DIR is set, the plugin is:
|
|
152 |
* 32bit: SUN_LIBUSBPLUGIN_DIR/
|
|
153 |
* 64bit: SUN_LIBUSBPLUGIN_DIR/64/
|
|
154 |
*/
|
|
155 |
snprintf(plugindir, PATH_MAX, "%s%s", tmpplugindir,
|
|
156 |
PLUGIN_PREFIX);
|
|
157 |
|
|
158 |
usb_dprintf(DEBUG_FUNCTIONS, "plugins dir: %s\n", plugindir);
|
|
159 |
|
|
160 |
if ((dirp = opendir(plugindir)) == NULL) {
|
|
161 |
usb_dprintf(DEBUG_ERRORS, "%s: %s \n",
|
|
162 |
plugindir, strerror(errno));
|
|
163 |
return (FAILURE);
|
|
164 |
}
|
|
165 |
while (dirp) {
|
|
166 |
errno = 0;
|
|
167 |
if ((dp = readdir(dirp)) != NULL) {
|
|
168 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
169 |
"reading entry %s\n", dp->d_name);
|
|
170 |
|
|
171 |
/* skip . and .. entries */
|
|
172 |
if (strncmp(dp->d_name, ".", 1) == 0) {
|
|
173 |
continue;
|
|
174 |
}
|
|
175 |
if (strncmp(dp->d_name, "..", 2) == 0) {
|
|
176 |
continue;
|
|
177 |
}
|
|
178 |
|
|
179 |
/*
|
|
180 |
* Ignore files that are not *.so.X
|
|
181 |
* this ensures that libusb.so.1 and plugins.so.1
|
|
182 |
* and libusb.so.2 and plugin.so.2 can both be
|
|
183 |
* supported
|
|
184 |
*/
|
|
185 |
if ((suffix = strstr(dp->d_name, MOD_SUFFIX)) == NULL) {
|
|
186 |
continue;
|
|
187 |
} else if (suffix[strlen(MOD_SUFFIX)] != '.') {
|
|
188 |
usb_dprintf(DEBUG_RECOVERABLE,
|
|
189 |
"did not load %s:%s\n",
|
|
190 |
plugindir, dp->d_name);
|
|
191 |
continue;
|
|
192 |
}
|
|
193 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
194 |
"reading .so file %s:%s\n", plugindir, dp->d_name);
|
|
195 |
p_info[pindex].p_name = strdup(dp->d_name);
|
|
196 |
|
|
197 |
/*
|
|
198 |
* using PATH_MAX len here
|
|
199 |
*/
|
|
200 |
if (strlen(p_info[pindex].p_name) > PATH_MAX) {
|
|
201 |
usb_dprintf(DEBUG_RECOVERABLE,
|
|
202 |
"pathname > PATH_MAX %s",
|
|
203 |
p_info[pindex].p_name);
|
|
204 |
/* go on to try read next module */
|
|
205 |
free(p_info[pindex].p_name);
|
|
206 |
continue;
|
|
207 |
}
|
|
208 |
(void) strncpy(modpath, plugindir, PATH_MAX);
|
|
209 |
(void) strncat(modpath, "/", 1);
|
|
210 |
(void) strncat(modpath,
|
|
211 |
p_info[pindex].p_name, PATH_MAX);
|
|
212 |
p_info[pindex].p_path = strdup(modpath);
|
|
213 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
214 |
"Path is %s\n", p_info[pindex].p_path);
|
|
215 |
|
|
216 |
/*
|
|
217 |
* If we do not detect a valid libusb plugin
|
|
218 |
* then let's skip and continue
|
|
219 |
* do not update pindex if we want to continue
|
|
220 |
* we will dlopen and dlclose in this check
|
|
221 |
*/
|
|
222 |
if (is_libusb_plugin(p_info[pindex].p_path) !=
|
|
223 |
SUCCESS) {
|
|
224 |
free(p_info[pindex].p_path);
|
|
225 |
free(p_info[pindex].p_name);
|
|
226 |
continue;
|
|
227 |
}
|
|
228 |
plugin_found = 1;
|
|
229 |
if (++pindex == MAX_PLUGINS) {
|
|
230 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
231 |
"Max plugins read %d\n", pindex);
|
|
232 |
break;
|
|
233 |
}
|
|
234 |
} else {
|
|
235 |
if (errno == 0) {
|
|
236 |
(void) closedir(dirp);
|
|
237 |
if (!plugin_found) {
|
|
238 |
usb_dprintf(DEBUG_ERRORS,
|
|
239 |
"No plugin found \n");
|
|
240 |
return (FAILURE);
|
|
241 |
} else {
|
|
242 |
/* end of dir stream */
|
|
243 |
return (SUCCESS);
|
|
244 |
}
|
|
245 |
} else {
|
|
246 |
(void) closedir(dirp);
|
|
247 |
return (FAILURE);
|
|
248 |
}
|
|
249 |
}
|
|
250 |
}
|
|
251 |
return (SUCCESS);
|
|
252 |
}
|
|
253 |
|
|
254 |
/*
|
|
255 |
* In a directory crowded with a lot of .so's
|
|
256 |
* filter out potential libusb plugins - helps
|
|
257 |
* the plugin loader to only load valid plugins
|
|
258 |
* uses the libusb_version symbol as a filter
|
|
259 |
*/
|
|
260 |
static int
|
|
261 |
is_libusb_plugin(char *modname)
|
|
262 |
{
|
|
263 |
void *hdl;
|
|
264 |
void *hdl_sym1;
|
|
265 |
void *hdl_sym2;
|
|
266 |
char *symbol1 = "usb_init";
|
|
267 |
char *symbol2 = "libusb_prefix";
|
|
268 |
|
|
269 |
hdl = dlopen(modname, RTLD_NOW);
|
|
270 |
if (hdl == NULL) {
|
|
271 |
usb_dprintf(DEBUG_RECOVERABLE,
|
|
272 |
"%s could not be loaded \n", modname);
|
|
273 |
return (FAILURE);
|
|
274 |
}
|
|
275 |
hdl_sym1 = (void *)(dlsym(hdl, symbol1));
|
|
276 |
hdl_sym2 = (void *)(dlsym(hdl, symbol2));
|
|
277 |
(void) dlclose(hdl);
|
|
278 |
if ((hdl_sym1 == NULL) && (hdl_sym2 == NULL)) {
|
|
279 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
280 |
"%s not a libusb plugin: unload\n", modname);
|
|
281 |
return (FAILURE);
|
|
282 |
} else {
|
|
283 |
return (SUCCESS);
|
|
284 |
}
|
|
285 |
}
|
|
286 |
|
|
287 |
/*
|
|
288 |
* Read the plugin entry names
|
|
289 |
* Load the plugins and populate the proper data structures
|
|
290 |
* this includes things like bus/dev ptr mappings, handles, etc.
|
|
291 |
* Also loads all the symbols also and store the handles
|
|
292 |
* Lock: called with bus_lock held
|
|
293 |
* called from usb_init
|
|
294 |
*/
|
|
295 |
static int
|
|
296 |
load_plugins()
|
|
297 |
{
|
|
298 |
int pindex = 0;
|
|
299 |
void *handle;
|
|
300 |
int module_loaded = 0;
|
|
301 |
|
|
302 |
if (read_plugin_entries() != SUCCESS) {
|
|
303 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
304 |
"Failed to load libusb plugins \n");
|
|
305 |
return (FAILURE);
|
|
306 |
}
|
|
307 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
308 |
"load_plugin: modname is %s\n", p_info[pindex].p_name);
|
|
309 |
/*
|
|
310 |
* Will load at most MAX_PLUGINS
|
|
311 |
*/
|
|
312 |
while (pindex < MAX_PLUGINS) {
|
|
313 |
/* reached the end of modules read into the array */
|
|
314 |
if (p_info[pindex].p_name == NULL) {
|
|
315 |
break;
|
|
316 |
}
|
|
317 |
usb_dprintf(DEBUG_FUNCTIONS, "loading:%s pindex:%d\n",
|
|
318 |
p_info[pindex].p_name, pindex);
|
|
319 |
handle = dlopen(p_info[pindex].p_path, RTLD_NOW);
|
|
320 |
if (handle == NULL) {
|
|
321 |
usb_dprintf(DEBUG_RECOVERABLE,
|
|
322 |
"handle for %s is null\n", p_info[pindex].p_name);
|
|
323 |
usb_dprintf(DEBUG_RECOVERABLE, dlerror());
|
|
324 |
p_info[pindex].p_handle = NULL;
|
|
325 |
free(p_info[pindex].p_name);
|
|
326 |
free(p_info[pindex].p_path);
|
|
327 |
/* just try to load the next one */
|
|
328 |
pindex += 1;
|
|
329 |
continue;
|
|
330 |
} else {
|
|
331 |
p_info[pindex].p_handle = handle;
|
|
332 |
}
|
|
333 |
if (load_plugin_syms(p_info, handle, pindex) != SUCCESS) {
|
|
334 |
usb_dprintf(DEBUG_FUNCTIONS, "Failed to load"
|
|
335 |
"symbols for plugin %s\n",
|
|
336 |
p_info[pindex].p_name);
|
|
337 |
unload_plugin(pindex);
|
|
338 |
pindex += 1;
|
|
339 |
/* try the next plugin */
|
|
340 |
continue;
|
|
341 |
}
|
|
342 |
module_loaded = 1;
|
|
343 |
pindex += 1;
|
|
344 |
}
|
|
345 |
if (!module_loaded) {
|
|
346 |
usb_dprintf(DEBUG_FUNCTIONS, "No module could be loaded \n");
|
|
347 |
return (FAILURE);
|
|
348 |
} else {
|
|
349 |
/* ploaded is the highest index that had a module entry */
|
|
350 |
winfo.ploaded = pindex;
|
|
351 |
return (SUCCESS);
|
|
352 |
}
|
|
353 |
}
|
|
354 |
|
|
355 |
/*
|
|
356 |
* For debugging of bus pointers
|
|
357 |
*/
|
|
358 |
static void
|
|
359 |
dump_busses()
|
|
360 |
{
|
|
361 |
struct usb_bus *busp;
|
|
362 |
|
|
363 |
for (busp = usb_busses; busp != NULL; busp = busp->next) {
|
|
364 |
usb_dprintf(DEBUG_DETAILED, "busp is 0x%x\n", busp);
|
|
365 |
}
|
|
366 |
}
|
|
367 |
|
|
368 |
/*
|
|
369 |
* Used to unload plugin
|
|
370 |
* calling libusb_fini
|
|
371 |
*/
|
|
372 |
static void
|
|
373 |
unload_plugin(int pindex)
|
|
374 |
{
|
|
375 |
int sym_idx;
|
|
376 |
int (*hdl_libusb_fini)(void);
|
|
377 |
|
|
378 |
free(p_info[pindex].p_name);
|
|
379 |
free(p_info[pindex].p_path);
|
|
380 |
p_info[pindex].p_name = NULL;
|
|
381 |
|
|
382 |
/* call the plugins libusb_fini here */
|
|
383 |
if (check_sym_hdl(pindex, LIBUSB_FINI) < 0) {
|
|
384 |
(void) usb_dprintf(DEBUG_RECOVERABLE,
|
|
385 |
"hdl_libusb_fini is NULL \n");
|
|
386 |
} else {
|
|
387 |
hdl_libusb_fini =
|
|
388 |
LIBUSB_FINI_CAST(p_info[pindex].sym_hdl[LIBUSB_FINI]);
|
|
389 |
(*hdl_libusb_fini)();
|
|
390 |
}
|
|
391 |
if (p_info[pindex].p_handle != NULL) {
|
|
392 |
(void) dlclose(p_info[pindex].p_handle);
|
|
393 |
p_info[pindex].exclusive_flag = 0;
|
|
394 |
p_info[pindex].active_flag = 0;
|
|
395 |
p_info[pindex].p_handle = NULL;
|
|
396 |
}
|
|
397 |
for (sym_idx = 0; sym_idx < NUM_SYMS; sym_idx ++) {
|
|
398 |
p_info[pindex].sym_hdl[sym_idx] = NULL;
|
|
399 |
}
|
|
400 |
}
|
|
401 |
|
|
402 |
/*
|
|
403 |
* In trying to map the device handle to a bus
|
|
404 |
* walk through the dev handle pointers added
|
|
405 |
* during open and find the matching dev handle
|
|
406 |
* that exists for that bus. On no match return
|
|
407 |
* FAILURE on match return the bus index for this
|
|
408 |
* dev handle
|
|
409 |
*/
|
|
410 |
static int
|
|
411 |
get_index_devhdl(struct usb_dev_handle *dev)
|
|
412 |
{
|
|
413 |
int pindex;
|
|
414 |
dev_handles_t *devh;
|
|
415 |
|
|
416 |
for (pindex = 0; pindex < winfo.ploaded; pindex ++) {
|
|
417 |
for (devh = p_info[pindex].dev_handles; devh != NULL;
|
|
418 |
devh = devh->next) {
|
|
419 |
if (dev == devh->dev) {
|
|
420 |
return (pindex);
|
|
421 |
}
|
|
422 |
}
|
|
423 |
}
|
|
424 |
return (FAILURE);
|
|
425 |
}
|
|
426 |
|
|
427 |
/*
|
|
428 |
* upon a call to open adds the device handle to the
|
|
429 |
* list of handles the wrapper will maintain
|
|
430 |
* so that it can map device handles to bus
|
|
431 |
* holds bus_lock
|
|
432 |
* This is needed simply because we want to able
|
|
433 |
* to map the device handles to the plugin module
|
|
434 |
*/
|
|
435 |
static int
|
|
436 |
usb_add_dev(struct usb_dev_handle *dev, struct usb_device *device,
|
|
437 |
int pindex)
|
|
438 |
{
|
|
439 |
dev_handles_t *devh, *curr_devh;
|
|
440 |
|
|
441 |
(void) mutex_lock(&bus_lock);
|
|
442 |
|
|
443 |
/* first device handle to be added */
|
|
444 |
if (p_info[pindex].dev_handles == NULL) {
|
|
445 |
devh = (dev_handles_t *)malloc(sizeof (dev_handles_t));
|
|
446 |
if (devh == NULL) {
|
|
447 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
448 |
"Error adding device to list \n");
|
|
449 |
(void) mutex_unlock(&bus_lock);
|
|
450 |
return (FAILURE);
|
|
451 |
}
|
|
452 |
p_info[pindex].dev_handles = devh;
|
|
453 |
devh->prev = NULL;
|
|
454 |
devh->next = NULL;
|
|
455 |
} else {
|
|
456 |
curr_devh = p_info[pindex].dev_handles;
|
|
457 |
while (curr_devh->next != NULL) {
|
|
458 |
curr_devh = curr_devh->next;
|
|
459 |
}
|
|
460 |
/* curr_devh points to last devh handle */
|
|
461 |
curr_devh->next = (dev_handles_t *)
|
|
462 |
malloc(sizeof (dev_handles_t));
|
|
463 |
if (curr_devh->next == NULL) {
|
|
464 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
465 |
"Error adding device to list \n");
|
|
466 |
(void) mutex_unlock(&bus_lock);
|
|
467 |
return (FAILURE);
|
|
468 |
}
|
|
469 |
devh = curr_devh->next;
|
|
470 |
devh->next = NULL;
|
|
471 |
devh->prev = curr_devh;
|
|
472 |
}
|
|
473 |
devh->device = device;
|
|
474 |
devh->dev = dev;
|
|
475 |
(void) mutex_unlock(&bus_lock);
|
|
476 |
|
|
477 |
return (SUCCESS);
|
|
478 |
}
|
|
479 |
|
|
480 |
/*
|
|
481 |
* upon a call to usb_close removes the device handle from the
|
|
482 |
* list of handles the wrapper will maintain
|
|
483 |
* entries do not get removed on a device removal only on close
|
|
484 |
* holds bus_lock
|
|
485 |
*/
|
|
486 |
static void
|
|
487 |
usb_del_dev(struct usb_dev_handle *dev, int pindex)
|
|
488 |
{
|
|
489 |
dev_handles_t *d_dev;
|
|
490 |
|
|
491 |
(void) mutex_lock(&bus_lock);
|
|
492 |
d_dev = p_info[pindex].dev_handles;
|
|
493 |
|
|
494 |
while (d_dev != NULL) {
|
|
495 |
if (d_dev->dev == dev) {
|
|
496 |
/* Not the last dev hdl */
|
|
497 |
if (d_dev->next != NULL) {
|
|
498 |
usb_dprintf(DEBUG_DETAILED,
|
|
499 |
"d_dev->next != NULL\n");
|
|
500 |
d_dev->next->prev = d_dev->prev;
|
|
501 |
}
|
|
502 |
/* Not the first dev hdl */
|
|
503 |
if (d_dev->prev != NULL) {
|
|
504 |
usb_dprintf(DEBUG_DETAILED,
|
|
505 |
"d_dev->prev != NULL\n");
|
|
506 |
d_dev->prev->next = d_dev->next;
|
|
507 |
} else {
|
|
508 |
/*
|
|
509 |
* first dev hdl on list
|
|
510 |
* if only handle then point to NULL
|
|
511 |
*/
|
|
512 |
p_info[pindex].dev_handles = d_dev->next;
|
|
513 |
if (d_dev->next != NULL) {
|
|
514 |
usb_dprintf(DEBUG_DETAILED,
|
|
515 |
"d_dev->next != NULL\n");
|
|
516 |
d_dev->next->prev = NULL;
|
|
517 |
}
|
|
518 |
}
|
|
519 |
free(d_dev);
|
|
520 |
break;
|
|
521 |
}
|
|
522 |
d_dev = d_dev->next;
|
|
523 |
}
|
|
524 |
|
|
525 |
(void) mutex_unlock(&bus_lock);
|
|
526 |
}
|
|
527 |
|
|
528 |
/*
|
|
529 |
* checks if a function has a valid symbol handle
|
|
530 |
* there also needs to be a valid dlopen hdl for the plugin
|
|
531 |
*/
|
|
532 |
static int
|
|
533 |
check_sym_hdl(int pindex, int sym_index)
|
|
534 |
{
|
|
535 |
if (p_info[pindex].p_handle == NULL) {
|
|
536 |
return (FAILURE);
|
|
537 |
} else if (p_info[pindex].sym_hdl[sym_index] == NULL) {
|
|
538 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
539 |
"sym_hdl[%d] is null \n", sym_index);
|
|
540 |
return (FAILURE);
|
|
541 |
} else {
|
|
542 |
return (SUCCESS);
|
|
543 |
}
|
|
544 |
}
|
|
545 |
|
|
546 |
/*
|
|
547 |
* returns a valid symbol handle or NULL
|
|
548 |
*/
|
|
549 |
static void *
|
|
550 |
lookup_sym_hdl(usb_dev_handle *dev, int sym_index, const char *func)
|
|
551 |
{
|
|
552 |
int pindex;
|
|
553 |
|
|
554 |
if ((pindex = get_pindex(dev, func)) < 0) {
|
|
555 |
return (NULL);
|
|
556 |
}
|
|
557 |
if (p_info[pindex].p_handle == NULL) {
|
|
558 |
return (NULL);
|
|
559 |
} else if (p_info[pindex].sym_hdl[sym_index] == NULL) {
|
|
560 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
561 |
"sym_hdl[%d] is null \n", sym_index);
|
|
562 |
return (NULL);
|
|
563 |
} else {
|
|
564 |
/* this is needed to support strerror() of last call */
|
|
565 |
(void) mutex_lock(&bus_lock);
|
|
566 |
winfo.last_pindex = pindex;
|
|
567 |
(void) mutex_unlock(&bus_lock);
|
|
568 |
return (p_info[pindex].sym_hdl[sym_index]);
|
|
569 |
}
|
|
570 |
}
|
|
571 |
|
|
572 |
/*
|
|
573 |
* Used to find the plugin whose bus links this device ptr
|
|
574 |
* We will walk the bus list and then traverse the device list
|
|
575 |
* of each bus to find the matching device
|
|
576 |
* Once we have a match we know the bus that this device hangs off of
|
|
577 |
* so we do backtrack and find a match for this bus and plugins.
|
|
578 |
* A match means we have a plugin index which essentially tells us
|
|
579 |
* the plugin to use
|
|
580 |
*/
|
|
581 |
static int
|
|
582 |
get_index_devicep(struct usb_device *device)
|
|
583 |
{
|
|
584 |
int pindex = 0;
|
|
585 |
struct usb_device *devicep;
|
|
586 |
struct usb_bus *busp;
|
|
587 |
|
|
588 |
busp = usb_busses;
|
|
589 |
while (busp != NULL) {
|
|
590 |
usb_dprintf(DEBUG_DETAILED,
|
|
591 |
"get_index_: busp is 0x%x\n", busp);
|
|
592 |
for (devicep = busp->devices; devicep != NULL;
|
|
593 |
devicep = devicep->next) {
|
|
594 |
usb_dprintf(DEBUG_DETAILED,
|
|
595 |
"devicep = 0x%x\n", devicep);
|
|
596 |
if (devicep == device) {
|
|
597 |
for (pindex = 0; pindex <
|
|
598 |
winfo.ploaded; pindex ++) {
|
|
599 |
if (p_info[pindex].busp == busp) {
|
|
600 |
usb_dprintf(DEBUG_DETAILED,
|
|
601 |
"devicep: pindex = %d\n",
|
|
602 |
pindex);
|
|
603 |
return (pindex);
|
|
604 |
}
|
|
605 |
}
|
|
606 |
}
|
|
607 |
}
|
|
608 |
busp = busp->next;
|
|
609 |
}
|
|
610 |
return (FAILURE);
|
|
611 |
}
|
|
612 |
|
|
613 |
static int
|
|
614 |
load_plugin_syms(plugin_info_t *p_info, void *handle, int pindex)
|
|
615 |
{
|
|
616 |
char *symbol;
|
|
617 |
char *prefix;
|
|
618 |
int prefix_len = 0;
|
|
619 |
int sym_len;
|
|
620 |
int sym_idx;
|
|
621 |
char **handle_libusb_prefix;
|
|
622 |
|
|
623 |
handle_libusb_prefix = (char **)(dlsym(handle, "libusb_prefix"));
|
|
624 |
|
|
625 |
/* can have a valid handle but a null prefix */
|
|
626 |
if ((handle_libusb_prefix != NULL) &&
|
|
627 |
(*handle_libusb_prefix != NULL)) {
|
|
628 |
prefix_len = (int)strlen(*handle_libusb_prefix);
|
|
629 |
p_info[pindex].prefix = *handle_libusb_prefix;
|
|
630 |
} else {
|
|
631 |
p_info[pindex].prefix = NULL;
|
|
632 |
}
|
|
633 |
|
|
634 |
prefix = p_info[pindex].prefix;
|
|
635 |
if (prefix != NULL) {
|
|
636 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
637 |
"load_plugin_syms():prefix is %s\n", prefix);
|
|
638 |
}
|
|
639 |
usb_dprintf(DEBUG_DETAILED, "NUM_SYMS is %d\n", NUM_SYMS);
|
|
640 |
for (sym_idx = 0; sym_idx < NUM_SYMS; sym_idx ++) {
|
|
641 |
sym_len = (int)strlen(sym_names[sym_idx]) + prefix_len + 2;
|
|
642 |
symbol = (char *)malloc(sym_len);
|
|
643 |
if (symbol == NULL) {
|
|
644 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
645 |
"could not alloc space for prefix\n");
|
|
646 |
return (FAILURE);
|
|
647 |
}
|
|
648 |
wusb_symbol(&symbol, sym_len, prefix, sym_names[sym_idx]);
|
|
649 |
p_info[pindex].sym_hdl[sym_idx] =
|
|
650 |
(void *)dlsym(handle, symbol);
|
|
651 |
usb_dprintf(DEBUG_DETAILED, "handle[%d]=0x%x, name = %s\n",
|
|
652 |
sym_idx, p_info[pindex].sym_hdl[sym_idx], symbol);
|
|
653 |
free(symbol);
|
|
654 |
}
|
|
655 |
|
|
656 |
return (SUCCESS);
|
|
657 |
}
|
|
658 |
|
|
659 |
/*
|
|
660 |
* Used to form prefixed interface symbols for plugins
|
|
661 |
*/
|
|
662 |
static void
|
|
663 |
wusb_symbol(char **init_str, int len, char *prefix, char *func_name)
|
|
664 |
{
|
|
665 |
if (prefix != NULL) {
|
|
666 |
(void) snprintf(*init_str, len, "%s", prefix);
|
|
667 |
} else {
|
|
668 |
(void) memset(*init_str, 0, len);
|
|
669 |
}
|
|
670 |
(void) strncat(*init_str, func_name, len);
|
|
671 |
}
|
|
672 |
|
|
673 |
/*
|
|
674 |
* Given a device handle map it to a bus
|
|
675 |
* if active_index = -1 it means more than
|
|
676 |
* a single plugin bus is active so we need
|
|
677 |
* walk the dev handles lists. Else active
|
|
678 |
* index simply points to the index of the
|
|
679 |
* single bus that is active and so we use that
|
|
680 |
*/
|
|
681 |
int
|
|
682 |
get_pindex(struct usb_dev_handle *dev, const char *func)
|
|
683 |
{
|
|
684 |
int pindex;
|
|
685 |
|
|
686 |
if (dev == NULL) {
|
|
687 |
usb_dprintf(DEBUG_ERRORS,
|
|
688 |
"%s: Invalid device handle \n", func);
|
|
689 |
return (-EINVAL);
|
|
690 |
}
|
|
691 |
if (winfo.active_index == -1) {
|
|
692 |
pindex = get_index_devhdl(dev);
|
|
693 |
if (pindex < 0) {
|
|
694 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
695 |
"%s: device handle not found\n", func);
|
|
696 |
return (-ENODEV);
|
|
697 |
}
|
|
698 |
} else {
|
|
699 |
pindex = winfo.active_index;
|
|
700 |
}
|
|
701 |
|
|
702 |
return (pindex);
|
|
703 |
}
|
|
704 |
|
|
705 |
/*
|
|
706 |
* Entry points for standard libusb interfaces
|
|
707 |
* Given a device handle, device pointer
|
|
708 |
* map the device to the bus and call into
|
|
709 |
* the loaded module. For calls without
|
|
710 |
* arguments - cycle through all modules
|
|
711 |
* that are active (per some policies implemented by plugin)
|
|
712 |
* and make calls into the implementation specific
|
|
713 |
* functions.
|
|
714 |
*/
|
|
715 |
|
|
716 |
/*
|
|
717 |
* usb_init() entry point:
|
|
718 |
*
|
|
719 |
* This will call the libusb-init implementation of each plugin
|
|
720 |
* loaded and will determine which busses will be supported.
|
|
721 |
* For the plugin busses that are supported usb_init
|
|
722 |
* of those plugins will be called. This routine will also
|
|
723 |
* invalidate plugin entries that are not supported
|
|
724 |
*/
|
|
725 |
void
|
|
726 |
usb_init(void)
|
|
727 |
{
|
|
728 |
int pindex;
|
|
729 |
boolean_t exclusive = 0;
|
|
730 |
int (*hdl_libusb_init)(void);
|
|
731 |
void (*hdl_usb_init)(void);
|
|
732 |
char **hdl_libusb_version;
|
|
733 |
int ret;
|
|
734 |
char *func = "usb_init";
|
|
735 |
char *version;
|
|
736 |
char version_store[MAX_VERSION_LEN + 1];
|
62
|
737 |
char wr_version_store[sizeof (LIBUSB_WRAPPER_VERSION)];
|
0
|
738 |
char *token;
|
|
739 |
char *wr_token;
|
|
740 |
char *debug_str;
|
|
741 |
int active_count = 0;
|
|
742 |
|
|
743 |
/*
|
|
744 |
* If usb_init() already called then do not reinit
|
|
745 |
*/
|
|
746 |
if (winfo.head_busp != NULL) {
|
|
747 |
return;
|
|
748 |
}
|
|
749 |
|
|
750 |
(void) mutex_lock(&bus_lock);
|
|
751 |
|
|
752 |
if ((debug_str = getenv("SUN_LIBUSB_DEBUG")) != NULL) {
|
|
753 |
libusb_debug = atoi(debug_str);
|
|
754 |
}
|
|
755 |
usb_dprintf(DEBUG_FUNCTIONS, "the wrapper's debug level is %d\n",
|
|
756 |
libusb_debug);
|
|
757 |
|
|
758 |
ret = load_plugins();
|
|
759 |
if (ret < 0) {
|
|
760 |
usb_dprintf(DEBUG_ERRORS,
|
|
761 |
"%s: could not load plugin modules\n", func);
|
|
762 |
(void) mutex_unlock(&bus_lock);
|
|
763 |
return;
|
|
764 |
}
|
|
765 |
|
|
766 |
winfo.exclusive_index = -1;
|
|
767 |
winfo.active_index = -1;
|
|
768 |
winfo.head_busp = NULL;
|
|
769 |
|
|
770 |
for (pindex = 0; pindex < winfo.ploaded; pindex ++) {
|
|
771 |
if (p_info[pindex].p_handle == NULL) {
|
|
772 |
continue;
|
|
773 |
}
|
|
774 |
/* condition for libusb_init not implemented */
|
|
775 |
if (check_sym_hdl(pindex, LIBUSB_INIT) < 0) {
|
|
776 |
(void) usb_dprintf(DEBUG_RECOVERABLE,
|
|
777 |
"hdl_libusb_init is NULL \n");
|
|
778 |
p_info[pindex].exclusive_flag = 0;
|
|
779 |
p_info[pindex].active_flag = 1;
|
|
780 |
active_count += 1;
|
|
781 |
continue;
|
|
782 |
}
|
|
783 |
hdl_libusb_init =
|
|
784 |
LIBUSB_INIT_CAST(p_info[pindex].sym_hdl[LIBUSB_INIT]);
|
|
785 |
ret = (*hdl_libusb_init)();
|
|
786 |
|
|
787 |
(void) usb_dprintf(DEBUG_DETAILED,
|
|
788 |
"%s: libusb_init() returned %d\n",
|
|
789 |
p_info[pindex].p_name, ret);
|
|
790 |
|
|
791 |
switch (ret) {
|
|
792 |
case SUCCESS:
|
|
793 |
p_info[pindex].exclusive_flag = 0;
|
|
794 |
p_info[pindex].active_flag = 1;
|
|
795 |
active_count += 1;
|
|
796 |
winfo.active_index = pindex;
|
|
797 |
break;
|
|
798 |
case FAILURE:
|
|
799 |
usb_dprintf(DEBUG_FUNCTIONS, "unloading plugin %s\n",
|
|
800 |
p_info[pindex].p_name);
|
|
801 |
unload_plugin(pindex);
|
|
802 |
continue;
|
|
803 |
case PLUGIN_EXCLUSIVE:
|
|
804 |
/* first plugin to set exclusive */
|
|
805 |
if (exclusive != 1) {
|
|
806 |
p_info[pindex].exclusive_flag = 1;
|
|
807 |
exclusive = 1;
|
|
808 |
winfo.exclusive_index = pindex;
|
|
809 |
}
|
|
810 |
p_info[pindex].active_flag = 1;
|
|
811 |
active_count += 1;
|
|
812 |
break;
|
|
813 |
default:
|
|
814 |
usb_dprintf(DEBUG_RECOVERABLE, "unsupported return"
|
|
815 |
"value for libusb_init\n");
|
|
816 |
}
|
|
817 |
|
|
818 |
/*
|
|
819 |
* If there is no version defined we accept it
|
|
820 |
* but if there is a version mismatch we will skip
|
|
821 |
*/
|
|
822 |
if (check_sym_hdl(pindex, LIBUSB_VERSION) < 0) {
|
|
823 |
usb_dprintf(DEBUG_RECOVERABLE,
|
|
824 |
"No Version number for plugin found \n");
|
|
825 |
} else {
|
|
826 |
hdl_libusb_version = (char **)
|
|
827 |
(p_info[pindex].sym_hdl[LIBUSB_VERSION]);
|
|
828 |
if ((version = *hdl_libusb_version) != NULL) {
|
|
829 |
if (strlen(version) > MAX_VERSION_LEN) {
|
|
830 |
usb_dprintf(DEBUG_RECOVERABLE,
|
|
831 |
"version string exceeds max"
|
|
832 |
"characters, truncating\n");
|
|
833 |
}
|
|
834 |
(void) strncpy(version_store, version,
|
|
835 |
MAX_VERSION_LEN);
|
|
836 |
token = strtok(version_store, ".");
|
62
|
837 |
(void) strncpy(wr_version_store,
|
|
838 |
LIBUSB_WRAPPER_VERSION,
|
|
839 |
sizeof (wr_version_store));
|
|
840 |
wr_token = strtok(wr_version_store, ".");
|
0
|
841 |
|
|
842 |
/*
|
|
843 |
* Initial wrapper version is 1.1
|
|
844 |
* if plugin major_rev is != wrapper major_rev
|
|
845 |
* then do not load. If the version is not
|
|
846 |
* supported set active to FALSE
|
|
847 |
*/
|
|
848 |
usb_dprintf(DEBUG_DETAILED,
|
|
849 |
"plugin rev is %d\n", atoi(token));
|
|
850 |
usb_dprintf(DEBUG_DETAILED,
|
|
851 |
"wrapper rev is %d\n", atoi(wr_token));
|
|
852 |
if (atoi(token) != atoi(wr_token)) {
|
|
853 |
usb_dprintf(DEBUG_ERRORS,
|
|
854 |
"plugin version %s not supported\n",
|
|
855 |
version);
|
|
856 |
unload_plugin(pindex);
|
|
857 |
}
|
|
858 |
}
|
|
859 |
winfo.last_pindex = pindex;
|
|
860 |
}
|
|
861 |
if (active_count != 1) {
|
|
862 |
winfo.active_index = -1;
|
|
863 |
}
|
|
864 |
}
|
|
865 |
(void) usb_dprintf(DEBUG_DETAILED,
|
|
866 |
"winfo.ploaded is %d\n", winfo.ploaded);
|
|
867 |
for (pindex = 0; pindex < winfo.ploaded; pindex ++) {
|
|
868 |
if (p_info[pindex].p_handle == NULL) {
|
|
869 |
continue;
|
|
870 |
}
|
|
871 |
if (exclusive && p_info[pindex].exclusive_flag == 1) {
|
|
872 |
winfo.exclusive_index = pindex;
|
|
873 |
winfo.active_index = pindex;
|
|
874 |
}
|
|
875 |
if (exclusive && p_info[pindex].exclusive_flag != 1) {
|
|
876 |
unload_plugin(pindex);
|
|
877 |
}
|
|
878 |
if (p_info[pindex].active_flag) {
|
|
879 |
if (check_sym_hdl(pindex, USB_INIT) < 0) {
|
|
880 |
usb_dprintf(DEBUG_ERRORS,
|
|
881 |
"could not get symbol for %s\n", func);
|
|
882 |
}
|
|
883 |
hdl_usb_init = USB_INIT_CAST(p_info[pindex].
|
|
884 |
sym_hdl[USB_INIT]);
|
|
885 |
(*hdl_usb_init)();
|
|
886 |
}
|
|
887 |
}
|
|
888 |
(void) mutex_unlock(&bus_lock);
|
|
889 |
usb_dprintf(DEBUG_DETAILED, "completed usb init()\n");
|
|
890 |
}
|
|
891 |
|
|
892 |
void
|
|
893 |
usb_set_debug(int level)
|
|
894 |
{
|
|
895 |
int pindex;
|
|
896 |
char *func = "usb_set_debug";
|
|
897 |
void (*hdl)(int);
|
|
898 |
char *debug_str;
|
|
899 |
|
|
900 |
/* env debug variables override completely what the app sets */
|
|
901 |
if ((debug_str = getenv("SUN_LIBUSB_DEBUG")) != NULL) {
|
|
902 |
libusb_debug = atoi(debug_str);
|
|
903 |
|
|
904 |
} else {
|
|
905 |
|
|
906 |
if (level < 0)
|
|
907 |
return;
|
|
908 |
|
|
909 |
libusb_debug = level;
|
|
910 |
}
|
|
911 |
|
|
912 |
usb_dprintf(DEBUG_FUNCTIONS, "libusb debug level is %d\n",
|
|
913 |
libusb_debug);
|
|
914 |
|
|
915 |
for (pindex = 0; pindex < winfo.ploaded; pindex ++) {
|
|
916 |
if (check_sym_hdl(pindex, USB_SET_DEBUG) < 0) {
|
|
917 |
usb_dprintf(DEBUG_ERRORS,
|
|
918 |
"could not find symbol for %s\n", func);
|
|
919 |
continue;
|
|
920 |
}
|
|
921 |
hdl = USB_SET_DEBUG_CAST
|
|
922 |
(p_info[pindex].sym_hdl[USB_SET_DEBUG]);
|
|
923 |
(*hdl)(libusb_debug);
|
|
924 |
}
|
|
925 |
}
|
|
926 |
|
|
927 |
/*
|
|
928 |
* This will manage the usb_busses pointer for each plugin
|
|
929 |
* The wrapper library will expose its own usb_busses pointer
|
|
930 |
* This will be built by loading the plugin usb_busses pointer
|
|
931 |
* and linking all the bussses. The wrapper libraries usb_bus
|
|
932 |
* pointer will in sync every time usb_find_busses is called.
|
|
933 |
* Applications are shielded from the underlying plugin usb_busses
|
|
934 |
* pointers.
|
|
935 |
* ret_bus is supposed to be the number of busses changed
|
|
936 |
* since last call
|
|
937 |
*/
|
|
938 |
int
|
|
939 |
usb_find_busses(void)
|
|
940 |
{
|
|
941 |
int pindex;
|
|
942 |
char *func = "usb_find_busses";
|
|
943 |
int (*hdl_usb_find_busses)(void);
|
|
944 |
struct usb_bus **hdl_usb_busses;
|
|
945 |
int ret_find_busses[MAX_PLUGINS];
|
|
946 |
struct usb_bus *tmp_usb_busses = NULL;
|
|
947 |
struct usb_bus *mv_usb_busses = NULL;
|
|
948 |
struct usb_bus *last_usb_busses = NULL;
|
|
949 |
int ret_bus = 0;
|
|
950 |
int found_bus = 0;
|
|
951 |
|
|
952 |
(void) mutex_lock(&bus_lock);
|
|
953 |
|
|
954 |
for (pindex = 0; pindex < winfo.ploaded; pindex ++) {
|
|
955 |
if (check_sym_hdl(pindex, USB_FIND_BUSSES) < 0) {
|
|
956 |
usb_dprintf(DEBUG_ERRORS,
|
|
957 |
"could not get symbol for %s\n", func);
|
|
958 |
continue;
|
|
959 |
}
|
|
960 |
hdl_usb_find_busses =
|
|
961 |
USB_FIND_BUSSES_CAST
|
|
962 |
(p_info[pindex].sym_hdl[USB_FIND_BUSSES]);
|
|
963 |
|
|
964 |
/* calling the find_busses whose symbols can be found */
|
|
965 |
ret_find_busses[pindex] = (*hdl_usb_find_busses)();
|
|
966 |
ret_bus += ret_find_busses[pindex];
|
|
967 |
|
|
968 |
/*
|
|
969 |
* updated usb_busses pointer for the plugins
|
|
970 |
* this could be NULL
|
|
971 |
*/
|
|
972 |
if (check_sym_hdl(pindex, USB_BUSSES) < 0) {
|
|
973 |
usb_dprintf(DEBUG_ERRORS,
|
|
974 |
"could not get symbol for %s\n", usb_busses);
|
|
975 |
p_info[pindex].busp = NULL;
|
|
976 |
continue;
|
|
977 |
}
|
|
978 |
hdl_usb_busses =
|
|
979 |
USB_BUSSES_CAST(p_info[pindex].sym_hdl[USB_BUSSES]);
|
|
980 |
p_info[pindex].busp = *hdl_usb_busses;
|
|
981 |
usb_dprintf(DEBUG_DETAILED,
|
|
982 |
"usb_bus ptr = 0x%x\n", p_info[pindex].busp);
|
|
983 |
found_bus = 1;
|
|
984 |
winfo.last_pindex = pindex;
|
|
985 |
}
|
|
986 |
if (!found_bus) {
|
|
987 |
usb_dprintf(DEBUG_ERRORS,
|
|
988 |
"could not find a usb_bus pointer \n");
|
|
989 |
(void) mutex_unlock(&bus_lock);
|
|
990 |
return (FAILURE);
|
|
991 |
}
|
|
992 |
|
|
993 |
/* Init tmp_usb_busses */
|
|
994 |
for (pindex = 0; pindex < winfo.ploaded; pindex ++) {
|
|
995 |
if (tmp_usb_busses == NULL) {
|
|
996 |
if (p_info[pindex].busp == NULL) {
|
|
997 |
continue;
|
|
998 |
}
|
|
999 |
tmp_usb_busses = p_info[pindex].busp;
|
|
1000 |
winfo.head_busp = tmp_usb_busses;
|
|
1001 |
mv_usb_busses = tmp_usb_busses;
|
|
1002 |
while (mv_usb_busses->next != NULL) {
|
|
1003 |
mv_usb_busses = mv_usb_busses->next;
|
|
1004 |
}
|
|
1005 |
last_usb_busses = mv_usb_busses;
|
|
1006 |
continue;
|
|
1007 |
}
|
|
1008 |
if (p_info[pindex].busp == NULL) {
|
|
1009 |
continue;
|
|
1010 |
}
|
|
1011 |
last_usb_busses->next = p_info[pindex].busp;
|
|
1012 |
mv_usb_busses = p_info[pindex].busp;
|
|
1013 |
while (mv_usb_busses->next != NULL) {
|
|
1014 |
mv_usb_busses = mv_usb_busses->next;
|
|
1015 |
}
|
|
1016 |
last_usb_busses = mv_usb_busses;
|
|
1017 |
}
|
|
1018 |
usb_busses = winfo.head_busp;
|
|
1019 |
dump_busses();
|
|
1020 |
(void) mutex_unlock(&bus_lock);
|
|
1021 |
|
|
1022 |
return (ret_bus);
|
|
1023 |
}
|
|
1024 |
|
|
1025 |
struct usb_dev_handle *
|
|
1026 |
usb_open(struct usb_device *device)
|
|
1027 |
{
|
|
1028 |
int pindex;
|
|
1029 |
struct usb_dev_handle *dev;
|
|
1030 |
struct usb_dev_handle *(*hdl)(struct usb_device *);
|
|
1031 |
char *func = "usb_open";
|
|
1032 |
|
|
1033 |
usb_dprintf(DEBUG_DETAILED, "usb_open: device ptr is 0x%x\n", device);
|
|
1034 |
if (winfo.active_index == -1) {
|
|
1035 |
usb_dprintf(DEBUG_DETAILED, "usb_open: active_index = -1 \n");
|
|
1036 |
pindex = get_index_devicep(device);
|
|
1037 |
/* could not find this device pointer */
|
|
1038 |
if (pindex < 0) {
|
|
1039 |
usb_dprintf(DEBUG_ERRORS,
|
|
1040 |
"%s: could not map device pointer to bus\n", func);
|
|
1041 |
return (NULL);
|
|
1042 |
}
|
|
1043 |
} else {
|
|
1044 |
usb_dprintf(DEBUG_FUNCTIONS,
|
|
1045 |
"usb_open: pindex = %d\n", winfo.active_index);
|
|
1046 |
pindex = winfo.active_index;
|
|
1047 |
}
|
|
1048 |
if (check_sym_hdl(pindex, USB_OPEN) < 0) {
|
|
1049 |
usb_dprintf(DEBUG_ERRORS, "%s: Symbol not found \n", func);
|
|
1050 |
return (NULL);
|
|
1051 |
}
|
|
1052 |
hdl = USB_OPEN_CAST(p_info[pindex].sym_hdl[USB_OPEN]);
|
|
1053 |
dev = (*hdl)(device);
|
|
1054 |
if (usb_add_dev(dev, device, pindex) == SUCCESS) {
|
|
1055 |
return (dev);
|
|
1056 |
} else {
|
|
1057 |
usb_dprintf(DEBUG_ERRORS, "%s:No Memory to add device\n", func);
|
|
1058 |
return (NULL);
|
|
1059 |
}
|
|
1060 |
}
|
|
1061 |
|
|
1062 |
int
|
|
1063 |
usb_close(usb_dev_handle *dev)
|
|
1064 |
{
|
|
1065 |
int pindex;
|
|
1066 |
int ret;
|
|
1067 |
char *func = "usb_close";
|
|
1068 |
int (*hdl)(usb_dev_handle *);
|
|
1069 |
|
|
1070 |
pindex = get_pindex(dev, func);
|
|
1071 |
if (pindex < 0) {
|
|
1072 |
return (pindex);
|
|
1073 |
}
|
|
1074 |
if (check_sym_hdl(pindex, USB_CLOSE) < 0) {
|
|
1075 |
usb_dprintf(DEBUG_ERRORS,
|
|
1076 |
"could not find symbol for %s\n", func);
|
|
1077 |
return (FAILURE);
|
|
1078 |
}
|
|
1079 |
hdl = USB_CLOSE_CAST(p_info[pindex].sym_hdl[USB_CLOSE]);
|
|
1080 |
usb_del_dev(dev, pindex);
|
|
1081 |
ret = (*hdl)(dev);
|
|
1082 |
|
|
1083 |
return (ret);
|
|
1084 |
}
|
|
1085 |
|
|
1086 |
int
|
|
1087 |
usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf,
|
|
1088 |
size_t buflen)
|
|
1089 |
{
|
|
1090 |
char *func = "usb_get_string";
|
|
1091 |
int (*hdl)(usb_dev_handle *, int, int, char *, size_t);
|
|
1092 |
|
|
1093 |
if ((hdl = USB_GET_STRING_CAST
|
|
1094 |
(lookup_sym_hdl(dev, USB_GET_STRING, func))) == NULL) {
|
|
1095 |
usb_dprintf(DEBUG_ERRORS,
|
|
1096 |
"could not find symbol for %s\n", func);
|
|
1097 |
return (-ENOTSUP);
|
|
1098 |
}
|
|
1099 |
|
|
1100 |
return ((*hdl)(dev, index, langid, buf, buflen));
|
|
1101 |
}
|
|
1102 |
|
|
1103 |
int
|
|
1104 |
usb_get_string_simple(usb_dev_handle *dev, int index, char *buf,
|
|
1105 |
size_t buflen)
|
|
1106 |
{
|
|
1107 |
char *func = "usb_get_string_simple";
|
|
1108 |
int (*hdl)(usb_dev_handle *, int, char *, size_t);
|
|
1109 |
|
|
1110 |
if ((hdl = USB_GET_STRING_SIMPLE_CAST
|
|
1111 |
(lookup_sym_hdl(dev, USB_GET_STRING_SIMPLE, func))) == NULL) {
|
|
1112 |
usb_dprintf(DEBUG_ERRORS,
|
|
1113 |
"could not find symbol for %s\n", func);
|
|
1114 |
return (-ENOTSUP);
|
|
1115 |
}
|
|
1116 |
|
|
1117 |
return ((*hdl)(dev, index, buf, buflen));
|
|
1118 |
}
|
|
1119 |
|
|
1120 |
int
|
|
1121 |
usb_get_descriptor_by_endpoint(usb_dev_handle *dev, int ep,
|
|
1122 |
unsigned char type, unsigned char index, void *buf, int size)
|
|
1123 |
{
|
|
1124 |
char *func = "usb_get_descriptor_by_endpoint";
|
|
1125 |
int (*hdl)(usb_dev_handle *, int, unsigned char,
|
|
1126 |
unsigned char, void *, int);
|
|
1127 |
|
|
1128 |
if ((hdl = USB_GET_DESCRIPTOR_BY_ENDPOINT_CAST
|
|
1129 |
(lookup_sym_hdl(dev, USB_GET_DESCRIPTOR_BY_ENDPOINT, func)))
|
|
1130 |
== NULL) {
|
|
1131 |
usb_dprintf(DEBUG_ERRORS,
|
|
1132 |
"could not find symbol for %s\n", func);
|
|
1133 |
return (-ENOTSUP);
|
|
1134 |
}
|
|
1135 |
|
|
1136 |
return ((*hdl)(dev, ep, type, index, buf, size));
|
|
1137 |
}
|
|
1138 |
|
|
1139 |
int
|
|
1140 |
usb_get_descriptor(usb_dev_handle *dev, unsigned char type,
|
|
1141 |
unsigned char index, void *buf, int size)
|
|
1142 |
{
|
|
1143 |
char *func = "usb_get_descriptor";
|
|
1144 |
int (*hdl)(usb_dev_handle *, unsigned char,
|
|
1145 |
unsigned char, void *, int);
|
|
1146 |
|
|
1147 |
if ((hdl = USB_GET_DESCRIPTOR_CAST
|
|
1148 |
(lookup_sym_hdl(dev, USB_GET_DESCRIPTOR, func))) == NULL) {
|
|
1149 |
usb_dprintf(DEBUG_ERRORS,
|
|
1150 |
"could not find symbol for %s\n", func);
|
|
1151 |
return (-ENOTSUP);
|
|
1152 |
}
|
|
1153 |
|
|
1154 |
return ((*hdl)(dev, type, index, buf, size));
|
|
1155 |
}
|
|
1156 |
|
|
1157 |
int
|
|
1158 |
usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout)
|
|
1159 |
{
|
|
1160 |
char *func = "usb_bulk_write";
|
|
1161 |
int (*hdl)(usb_dev_handle *, int, char *, int, int);
|
|
1162 |
|
|
1163 |
if ((hdl = USB_BULK_WRITE_CAST
|
|
1164 |
(lookup_sym_hdl(dev, USB_BULK_WRITE, func))) == NULL) {
|
|
1165 |
usb_dprintf(DEBUG_ERRORS,
|
|
1166 |
"could not find symbol for %s\n", func);
|
|
1167 |
return (-ENOTSUP);
|
|
1168 |
}
|
|
1169 |
return (*hdl)(dev, ep, bytes, size, timeout);
|
|
1170 |
}
|
|
1171 |
|
|
1172 |
int
|
|
1173 |
usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout)
|
|
1174 |
{
|
|
1175 |
char *func = "usb_bulk_read";
|
|
1176 |
int (*hdl)(usb_dev_handle *, int, char *, int, int);
|
|
1177 |
|
|
1178 |
if ((hdl = USB_BULK_READ_CAST
|
|
1179 |
(lookup_sym_hdl(dev, USB_BULK_READ, func))) == NULL) {
|
|
1180 |
usb_dprintf(DEBUG_ERRORS,
|
|
1181 |
"could not find symbol for %s\n", func);
|
|
1182 |
return (-ENOTSUP);
|
|
1183 |
}
|
|
1184 |
|
|
1185 |
return ((*hdl)(dev, ep, bytes, size, timeout));
|
|
1186 |
}
|
|
1187 |
|
|
1188 |
int
|
|
1189 |
usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes,
|
|
1190 |
int size, int timeout)
|
|
1191 |
{
|
|
1192 |
char *func = "usb_interrupt_read";
|
|
1193 |
int (*hdl)(usb_dev_handle *, int, char *, int, int);
|
|
1194 |
|
|
1195 |
if ((hdl = USB_INTERRUPT_READ_CAST
|
|
1196 |
(lookup_sym_hdl(dev, USB_INTERRUPT_READ, func))) == NULL) {
|
|
1197 |
usb_dprintf(DEBUG_ERRORS,
|
|
1198 |
"could not find symbol for %s\n", func);
|
|
1199 |
return (-ENOTSUP);
|
|
1200 |
}
|
|
1201 |
|
|
1202 |
return ((*hdl)(dev, ep, bytes, size, timeout));
|
|
1203 |
}
|
|
1204 |
|
|
1205 |
int
|
|
1206 |
usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes,
|
|
1207 |
int size, int timeout)
|
|
1208 |
{
|
|
1209 |
char *func = "usb_interrupt_write";
|
|
1210 |
int (*hdl)(usb_dev_handle *, int, char *, int, int);
|
|
1211 |
|
|
1212 |
if ((hdl = USB_INTERRUPT_WRITE_CAST
|
|
1213 |
(lookup_sym_hdl(dev, USB_INTERRUPT_WRITE, func))) == NULL) {
|
|
1214 |
usb_dprintf(DEBUG_ERRORS,
|
|
1215 |
"could not find symbol for %s\n", func);
|
|
1216 |
return (-ENOTSUP);
|
|
1217 |
}
|
|
1218 |
|
|
1219 |
return ((*hdl)(dev, ep, bytes, size, timeout));
|
|
1220 |
}
|
|
1221 |
|
|
1222 |
int
|
|
1223 |
usb_control_msg(usb_dev_handle *dev, int requesttype, int request,
|
|
1224 |
int value, int index, char *bytes, int size, int timeout)
|
|
1225 |
{
|
|
1226 |
char *func = "usb_control_msg";
|
|
1227 |
int (*hdl)(usb_dev_handle *, int, int, int, int, char *, int, int);
|
|
1228 |
|
|
1229 |
if ((hdl = USB_CONTROL_MSG_CAST
|
|
1230 |
(lookup_sym_hdl(dev, USB_CONTROL_MSG, func))) == NULL) {
|
|
1231 |
usb_dprintf(DEBUG_ERRORS,
|
|
1232 |
"could not find symbol for %s\n", func);
|
|
1233 |
return (-ENOTSUP);
|
|
1234 |
}
|
|
1235 |
|
|
1236 |
return ((*hdl)(dev, requesttype, request, value, index,
|
|
1237 |
bytes, size, timeout));
|
|
1238 |
}
|
|
1239 |
|
|
1240 |
int
|
|
1241 |
usb_set_configuration(usb_dev_handle *dev, int configuration)
|
|
1242 |
{
|
|
1243 |
char *func = "usb_set_configuration";
|
|
1244 |
int (*hdl)(usb_dev_handle *, int);
|
|
1245 |
|
|
1246 |
if ((hdl = USB_SET_CONFIGURATION_CAST
|
|
1247 |
(lookup_sym_hdl(dev, USB_SET_CONFIGURATION, func))) == NULL) {
|
|
1248 |
usb_dprintf(DEBUG_ERRORS,
|
|
1249 |
"could not find symbol for %s\n", func);
|
|
1250 |
return (-ENOTSUP);
|
|
1251 |
}
|
|
1252 |
|
|
1253 |
return ((*hdl)(dev, configuration));
|
|
1254 |
}
|
|
1255 |
|
|
1256 |
int
|
|
1257 |
usb_claim_interface(usb_dev_handle *dev, int interface)
|
|
1258 |
{
|
|
1259 |
char *func = "usb_claim_interface";
|
|
1260 |
int (*hdl)(usb_dev_handle *, int);
|
|
1261 |
|
|
1262 |
if ((hdl = USB_CLAIM_INTERFACE_CAST
|
|
1263 |
(lookup_sym_hdl(dev, USB_CLAIM_INTERFACE, func))) == NULL) {
|
|
1264 |
usb_dprintf(DEBUG_ERRORS,
|
|
1265 |
"could not find symbol for %s\n", func);
|
|
1266 |
return (-ENOTSUP);
|
|
1267 |
}
|
|
1268 |
|
|
1269 |
return ((*hdl)(dev, interface));
|
|
1270 |
}
|
|
1271 |
|
|
1272 |
int
|
|
1273 |
usb_release_interface(usb_dev_handle *dev, int interface)
|
|
1274 |
{
|
|
1275 |
char *func = "usb_release_interface";
|
|
1276 |
int (*hdl)(usb_dev_handle *, int);
|
|
1277 |
|
|
1278 |
if ((hdl = USB_RELEASE_INTERFACE_CAST
|
|
1279 |
(lookup_sym_hdl(dev, USB_RELEASE_INTERFACE, func))) == NULL) {
|
|
1280 |
usb_dprintf(DEBUG_ERRORS,
|
|
1281 |
"could not find symbol for %s\n", func);
|
|
1282 |
return (-ENOTSUP);
|
|
1283 |
}
|
|
1284 |
|
|
1285 |
return ((*hdl)(dev, interface));
|
|
1286 |
}
|
|
1287 |
|
|
1288 |
int
|
|
1289 |
usb_set_altinterface(usb_dev_handle *dev, int alternate)
|
|
1290 |
{
|
|
1291 |
char *func = "usb_set_altinterface";
|
|
1292 |
int (*hdl)(usb_dev_handle *, int);
|
|
1293 |
|
|
1294 |
if ((hdl = USB_SET_ALTINTERFACE_CAST
|
|
1295 |
(lookup_sym_hdl(dev, USB_SET_ALTINTERFACE, func))) == NULL) {
|
|
1296 |
usb_dprintf(DEBUG_ERRORS,
|
|
1297 |
"could not find symbol for %s\n", func);
|
|
1298 |
return (-ENOTSUP);
|
|
1299 |
}
|
|
1300 |
|
|
1301 |
return ((*hdl)(dev, alternate));
|
|
1302 |
}
|
|
1303 |
|
|
1304 |
int
|
|
1305 |
usb_resetep(usb_dev_handle *dev, unsigned int ep)
|
|
1306 |
{
|
|
1307 |
char *func = "usb_resetep";
|
|
1308 |
int (*hdl)(usb_dev_handle *, unsigned int);
|
|
1309 |
|
|
1310 |
if ((hdl = USB_RESETEP_CAST
|
|
1311 |
(lookup_sym_hdl(dev, USB_RESETEP, func))) == NULL) {
|
|
1312 |
usb_dprintf(DEBUG_ERRORS,
|
|
1313 |
"could not find symbol for %s\n", func);
|
|
1314 |
return (-ENOTSUP);
|
|
1315 |
}
|
|
1316 |
|
|
1317 |
return ((*hdl)(dev, ep));
|
|
1318 |
}
|
|
1319 |
|
|
1320 |
int
|
|
1321 |
usb_clear_halt(usb_dev_handle *dev, unsigned int ep)
|
|
1322 |
{
|
|
1323 |
|
|
1324 |
char *func = "usb_clear_halt";
|
|
1325 |
int (*hdl)(usb_dev_handle *, unsigned int);
|
|
1326 |
|
|
1327 |
if ((hdl = USB_CLEAR_HALT_CAST
|
|
1328 |
(lookup_sym_hdl(dev, USB_CLEAR_HALT, func))) == NULL) {
|
|
1329 |
usb_dprintf(DEBUG_ERRORS,
|
|
1330 |
"could not find symbol for %s\n", func);
|
|
1331 |
return (-ENOTSUP);
|
|
1332 |
}
|
|
1333 |
|
|
1334 |
return ((*hdl)(dev, ep));
|
|
1335 |
}
|
|
1336 |
|
|
1337 |
int
|
|
1338 |
usb_reset(usb_dev_handle *dev)
|
|
1339 |
{
|
|
1340 |
char *func = "usb_reset";
|
|
1341 |
int (*hdl)(usb_dev_handle *);
|
|
1342 |
|
|
1343 |
if ((hdl = USB_RESET_CAST
|
|
1344 |
(lookup_sym_hdl(dev, USB_RESET, func))) == NULL) {
|
|
1345 |
usb_dprintf(DEBUG_ERRORS,
|
|
1346 |
"could not find symbol for %s\n", func);
|
|
1347 |
return (-ENOTSUP);
|
|
1348 |
}
|
|
1349 |
return ((*hdl)(dev));
|
|
1350 |
}
|
|
1351 |
|
|
1352 |
int
|
|
1353 |
usb_find_devices(void)
|
|
1354 |
{
|
|
1355 |
int pindex;
|
|
1356 |
int (*hdl_usb_find_devices)(void);
|
|
1357 |
int ret_val = 0;
|
|
1358 |
int err_store = 0;
|
|
1359 |
|
|
1360 |
/*
|
|
1361 |
* devices can go away here.
|
|
1362 |
* devices can get added also.
|
|
1363 |
* the dev handles list does not update it only updates
|
|
1364 |
* for usb_close and usb_open
|
|
1365 |
*/
|
|
1366 |
for (pindex = 0; pindex < winfo.ploaded; pindex ++) {
|
|
1367 |
if (check_sym_hdl(pindex, USB_FIND_DEVICES) < 0) {
|
|
1368 |
continue;
|
|
1369 |
}
|
|
1370 |
hdl_usb_find_devices = USB_FIND_DEVICES_CAST
|
|
1371 |
(p_info[pindex].sym_hdl[USB_FIND_DEVICES]);
|
|
1372 |
ret_val = (*hdl_usb_find_devices)();
|
|
1373 |
if (ret_val < 0) {
|
|
1374 |
err_store = ret_val;
|
|
1375 |
} else {
|
|
1376 |
ret_val += ret_val;
|
|
1377 |
}
|
|
1378 |
}
|
|
1379 |
if (!err_store) {
|
|
1380 |
return (ret_val);
|
|
1381 |
} else {
|
|
1382 |
/* return any error for multiple busses */
|
|
1383 |
return (err_store);
|
|
1384 |
}
|
|
1385 |
}
|
|
1386 |
|
|
1387 |
struct usb_device *
|
|
1388 |
usb_device(usb_dev_handle *dev)
|
|
1389 |
{
|
|
1390 |
char *func = "usb_device";
|
|
1391 |
struct usb_device *((*hdl)(usb_dev_handle *));
|
|
1392 |
|
|
1393 |
if ((hdl = USB_DEVICE_CAST
|
|
1394 |
(lookup_sym_hdl(dev, USB_DEVICE, func))) == NULL) {
|
|
1395 |
usb_dprintf(DEBUG_ERRORS,
|
|
1396 |
"could not find symbol for %s\n", func);
|
|
1397 |
return (NULL);
|
|
1398 |
}
|
|
1399 |
|
|
1400 |
return ((*hdl)(dev));
|
|
1401 |
}
|
|
1402 |
|
|
1403 |
/*
|
|
1404 |
* This returns the wrapper's usb_busses pointer not the plugins
|
|
1405 |
*/
|
|
1406 |
struct usb_bus *
|
|
1407 |
usb_get_busses(void)
|
|
1408 |
{
|
|
1409 |
return (usb_busses);
|
|
1410 |
}
|
|
1411 |
|
|
1412 |
/*
|
|
1413 |
* Makes sense to only return a single
|
|
1414 |
* str error - so using the strerror of the
|
|
1415 |
* last plugin that was used
|
|
1416 |
*/
|
|
1417 |
|
|
1418 |
char *
|
|
1419 |
usb_strerror(void)
|
|
1420 |
{
|
|
1421 |
int pindex;
|
|
1422 |
char *func = "usb_strerror";
|
|
1423 |
char *(*hdl_usb_strerror)(void);
|
|
1424 |
|
|
1425 |
/*
|
|
1426 |
* usb_strerror is only of interest for the last
|
|
1427 |
* call to the plugin. So call it for the last
|
|
1428 |
* plugin used
|
|
1429 |
*/
|
|
1430 |
for (pindex = 0; pindex < winfo.ploaded; pindex ++) {
|
|
1431 |
if (check_sym_hdl(pindex, USB_STRERROR) < 0) {
|
|
1432 |
usb_dprintf(DEBUG_ERRORS,
|
|
1433 |
"could not find symbol for %s\n", func);
|
|
1434 |
continue;
|
|
1435 |
}
|
|
1436 |
if (pindex == winfo.last_pindex) {
|
|
1437 |
hdl_usb_strerror = USB_STRERROR_CAST
|
|
1438 |
(p_info[pindex].sym_hdl[USB_STRERROR]);
|
|
1439 |
return ((*hdl_usb_strerror)());
|
|
1440 |
}
|
|
1441 |
}
|
|
1442 |
|
|
1443 |
return (NULL);
|
|
1444 |
}
|
|
1445 |
|
|
1446 |
static void
|
|
1447 |
usb_dprintf(int level, char *format, ...)
|
|
1448 |
{
|
|
1449 |
va_list ap;
|
|
1450 |
char buf[512];
|
|
1451 |
|
|
1452 |
va_start(ap, format);
|
|
1453 |
(void) vsnprintf(buf, sizeof (buf), format, ap);
|
|
1454 |
if (libusb_debug >= level) {
|
|
1455 |
(void) fprintf(stderr, buf);
|
|
1456 |
}
|
|
1457 |
va_end(ap);
|
|
1458 |
}
|