| 1 |
/* |
| 2 |
* Copyright (c) 2016, Oracle and/or its affiliates. |
| 3 |
* Copyright 2023 Oxide Computer Company |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <config.h> |
| 21 |
|
| 22 |
#include <sys/time.h> |
| 23 |
#include <sys/types.h> |
| 24 |
#include <sys/stat.h> |
| 25 |
#include <strings.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <fcntl.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include <wait.h> |
| 32 |
#include <unistd.h> |
| 33 |
#include <aio.h> |
| 34 |
#include <libdevinfo.h> |
| 35 |
#include <sys/nvpair.h> |
| 36 |
#include <sys/devctl.h> |
| 37 |
#include <sys/usb/clients/ugen/usb_ugen.h> |
| 38 |
#include <sys/usb/usba.h> |
| 39 |
#include <sys/pci.h> |
| 40 |
|
| 41 |
#include "libusbi.h" |
| 42 |
#include "sunos_usb.h" |
| 43 |
|
| 44 |
#define UPDATEDRV_PATH "/usr/sbin/update_drv" |
| 45 |
#define UPDATEDRV "update_drv" |
| 46 |
|
| 47 |
#define DEFAULT_LISTSIZE 6 |
| 48 |
|
| 49 |
typedef struct { |
| 50 |
int nargs; |
| 51 |
int listsize; |
| 52 |
char **string; |
| 53 |
} string_list_t; |
| 54 |
|
| 55 |
/* |
| 56 |
* Backend functions |
| 57 |
*/ |
| 58 |
static int sunos_get_device_list(struct libusb_context *, |
| 59 |
struct discovered_devs **); |
| 60 |
static int sunos_open(struct libusb_device_handle *); |
| 61 |
static void sunos_close(struct libusb_device_handle *); |
| 62 |
static int sunos_get_active_config_descriptor(struct libusb_device *, |
| 63 |
void *, size_t); |
| 64 |
static int sunos_get_config_descriptor(struct libusb_device *, uint8_t, |
| 65 |
void *, size_t); |
| 66 |
static int sunos_get_configuration(struct libusb_device_handle *, uint8_t *); |
| 67 |
static int sunos_set_configuration(struct libusb_device_handle *, int); |
| 68 |
static int sunos_claim_interface(struct libusb_device_handle *, uint8_t); |
| 69 |
static int sunos_release_interface(struct libusb_device_handle *, uint8_t); |
| 70 |
static int sunos_set_interface_altsetting(struct libusb_device_handle *, |
| 71 |
uint8_t, uint8_t); |
| 72 |
static int sunos_clear_halt(struct libusb_device_handle *, unsigned char); |
| 73 |
static void sunos_destroy_device(struct libusb_device *); |
| 74 |
static int sunos_submit_transfer(struct usbi_transfer *); |
| 75 |
static int sunos_cancel_transfer(struct usbi_transfer *); |
| 76 |
static int sunos_handle_transfer_completion(struct usbi_transfer *); |
| 77 |
static int sunos_kernel_driver_active(struct libusb_device_handle *, uint8_t); |
| 78 |
static int sunos_detach_kernel_driver(struct libusb_device_handle *, uint8_t); |
| 79 |
static int sunos_attach_kernel_driver(struct libusb_device_handle *, uint8_t); |
| 80 |
static int sunos_usb_open_ep0(sunos_dev_handle_priv_t *hpriv, sunos_dev_priv_t *dpriv); |
| 81 |
static int sunos_usb_ioctl(struct libusb_device *dev, int cmd); |
| 82 |
|
| 83 |
static int sunos_get_link(di_devlink_t devlink, void *arg) |
| 84 |
{ |
| 85 |
walk_link_t *link_arg = (walk_link_t *)arg; |
| 86 |
const char *p; |
| 87 |
const char *q; |
| 88 |
|
| 89 |
if (link_arg->path) { |
| 90 |
char *content = (char *)di_devlink_content(devlink); |
| 91 |
char *start = strstr(content, "/devices/"); |
| 92 |
start += strlen("/devices"); |
| 93 |
usbi_dbg(NULL, "%s", start); |
| 94 |
|
| 95 |
/* line content must have minor node */ |
| 96 |
if (start == NULL || |
| 97 |
strncmp(start, link_arg->path, link_arg->len) != 0 || |
| 98 |
start[link_arg->len] != ':') |
| 99 |
return (DI_WALK_CONTINUE); |
| 100 |
} |
| 101 |
|
| 102 |
p = di_devlink_path(devlink); |
| 103 |
q = strrchr(p, '/'); |
| 104 |
usbi_dbg(NULL, "%s", q); |
| 105 |
|
| 106 |
*(link_arg->linkpp) = strndup(p, strlen(p) - strlen(q)); |
| 107 |
|
| 108 |
return (DI_WALK_TERMINATE); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
static int sunos_physpath_to_devlink( |
| 113 |
const char *node_path, const char *match, char **link_path) |
| 114 |
{ |
| 115 |
walk_link_t link_arg; |
| 116 |
di_devlink_handle_t hdl; |
| 117 |
|
| 118 |
*link_path = NULL; |
| 119 |
link_arg.linkpp = link_path; |
| 120 |
if ((hdl = di_devlink_init(NULL, 0)) == NULL) { |
| 121 |
usbi_dbg(NULL, "di_devlink_init failure"); |
| 122 |
return (-1); |
| 123 |
} |
| 124 |
|
| 125 |
link_arg.len = strlen(node_path); |
| 126 |
link_arg.path = (char *)node_path; |
| 127 |
|
| 128 |
(void) di_devlink_walk(hdl, match, NULL, DI_PRIMARY_LINK, |
| 129 |
(void *)&link_arg, sunos_get_link); |
| 130 |
|
| 131 |
(void) di_devlink_fini(&hdl); |
| 132 |
|
| 133 |
if (*link_path == NULL) { |
| 134 |
usbi_dbg(NULL, "there is no devlink for this path"); |
| 135 |
return (-1); |
| 136 |
} |
| 137 |
|
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
static int |
| 142 |
sunos_usb_ioctl(struct libusb_device *dev, int cmd) |
| 143 |
{ |
| 144 |
int fd; |
| 145 |
nvlist_t *nvlist; |
| 146 |
char *end; |
| 147 |
char *phypath; |
| 148 |
char *hubpath; |
| 149 |
char path_arg[PATH_MAX]; |
| 150 |
sunos_dev_priv_t *dpriv; |
| 151 |
devctl_ap_state_t devctl_ap_state; |
| 152 |
struct devctl_iocdata iocdata; |
| 153 |
|
| 154 |
dpriv = usbi_get_device_priv(dev); |
| 155 |
phypath = dpriv->phypath; |
| 156 |
|
| 157 |
end = strrchr(phypath, '/'); |
| 158 |
if (end == NULL) |
| 159 |
return (-1); |
| 160 |
hubpath = strndup(phypath, end - phypath); |
| 161 |
if (hubpath == NULL) |
| 162 |
return (-1); |
| 163 |
|
| 164 |
end = strrchr(hubpath, '@'); |
| 165 |
if (end == NULL) { |
| 166 |
free(hubpath); |
| 167 |
return (-1); |
| 168 |
} |
| 169 |
end++; |
| 170 |
usbi_dbg(DEVICE_CTX(dev), "unitaddr: %s", end); |
| 171 |
|
| 172 |
nvlist_alloc(&nvlist, NV_UNIQUE_NAME_TYPE, KM_NOSLEEP); |
| 173 |
nvlist_add_int32(nvlist, "port", dev->port_number); |
| 174 |
/* find the hub path */ |
| 175 |
snprintf(path_arg, sizeof(path_arg), "/devices%s:hubd", hubpath); |
| 176 |
usbi_dbg(DEVICE_CTX(dev), "ioctl hub path: %s", path_arg); |
| 177 |
|
| 178 |
fd = open(path_arg, O_RDONLY); |
| 179 |
if (fd < 0) { |
| 180 |
usbi_err(DEVICE_CTX(dev), "open failed: errno %d (%s)", errno, strerror(errno)); |
| 181 |
nvlist_free(nvlist); |
| 182 |
free(hubpath); |
| 183 |
return (-1); |
| 184 |
} |
| 185 |
|
| 186 |
memset(&iocdata, 0, sizeof(iocdata)); |
| 187 |
memset(&devctl_ap_state, 0, sizeof(devctl_ap_state)); |
| 188 |
|
| 189 |
nvlist_pack(nvlist, (char **)&iocdata.nvl_user, &iocdata.nvl_usersz, NV_ENCODE_NATIVE, 0); |
| 190 |
|
| 191 |
iocdata.cmd = DEVCTL_AP_GETSTATE; |
| 192 |
iocdata.flags = 0; |
| 193 |
iocdata.c_nodename = (char *)"hub"; |
| 194 |
iocdata.c_unitaddr = end; |
| 195 |
iocdata.cpyout_buf = &devctl_ap_state; |
| 196 |
usbi_dbg(DEVICE_CTX(dev), "%p, %" PRIuPTR, iocdata.nvl_user, iocdata.nvl_usersz); |
| 197 |
|
| 198 |
errno = 0; |
| 199 |
if (ioctl(fd, DEVCTL_AP_GETSTATE, &iocdata) == -1) { |
| 200 |
usbi_err(DEVICE_CTX(dev), "ioctl failed: fd %d, cmd %x, errno %d (%s)", |
| 201 |
fd, DEVCTL_AP_GETSTATE, errno, strerror(errno)); |
| 202 |
} else { |
| 203 |
usbi_dbg(DEVICE_CTX(dev), "dev rstate: %d", devctl_ap_state.ap_rstate); |
| 204 |
usbi_dbg(DEVICE_CTX(dev), "dev ostate: %d", devctl_ap_state.ap_ostate); |
| 205 |
} |
| 206 |
|
| 207 |
errno = 0; |
| 208 |
iocdata.cmd = cmd; |
| 209 |
if (ioctl(fd, (int)cmd, &iocdata) != 0) { |
| 210 |
usbi_err(DEVICE_CTX(dev), "ioctl failed: fd %d, cmd %x, errno %d (%s)", |
| 211 |
fd, cmd, errno, strerror(errno)); |
| 212 |
sleep(2); |
| 213 |
} |
| 214 |
|
| 215 |
close(fd); |
| 216 |
free(iocdata.nvl_user); |
| 217 |
nvlist_free(nvlist); |
| 218 |
free(hubpath); |
| 219 |
|
| 220 |
return (-errno); |
| 221 |
} |
| 222 |
|
| 223 |
static int |
| 224 |
sunos_kernel_driver_active(struct libusb_device_handle *dev_handle, uint8_t interface) |
| 225 |
{ |
| 226 |
sunos_dev_priv_t *dpriv = usbi_get_device_priv(dev_handle->dev); |
| 227 |
|
| 228 |
UNUSED(interface); |
| 229 |
|
| 230 |
usbi_dbg(HANDLE_CTX(dev_handle), "%s", dpriv->ugenpath); |
| 231 |
|
| 232 |
return (dpriv->ugenpath == NULL); |
| 233 |
} |
| 234 |
|
| 235 |
/* |
| 236 |
* Private functions |
| 237 |
*/ |
| 238 |
static int _errno_to_libusb(int); |
| 239 |
static int sunos_usb_get_status(struct libusb_context *ctx, int fd); |
| 240 |
|
| 241 |
static string_list_t * |
| 242 |
sunos_new_string_list(void) |
| 243 |
{ |
| 244 |
string_list_t *list; |
| 245 |
|
| 246 |
list = calloc(1, sizeof(string_list_t)); |
| 247 |
if (list == NULL) |
| 248 |
return (NULL); |
| 249 |
list->string = calloc(DEFAULT_LISTSIZE, sizeof(char *)); |
| 250 |
if (list->string == NULL) { |
| 251 |
free(list); |
| 252 |
return (NULL); |
| 253 |
} |
| 254 |
list->nargs = 0; |
| 255 |
list->listsize = DEFAULT_LISTSIZE; |
| 256 |
|
| 257 |
return (list); |
| 258 |
} |
| 259 |
|
| 260 |
static int |
| 261 |
sunos_append_to_string_list(string_list_t *list, const char *arg) |
| 262 |
{ |
| 263 |
char *str = strdup(arg); |
| 264 |
|
| 265 |
if (str == NULL) |
| 266 |
return (-1); |
| 267 |
|
| 268 |
if ((list->nargs + 1) == list->listsize) { /* +1 is for NULL */ |
| 269 |
char **tmp = realloc(list->string, |
| 270 |
sizeof(char *) * (list->listsize + 1)); |
| 271 |
if (tmp == NULL) { |
| 272 |
free(str); |
| 273 |
return (-1); |
| 274 |
} |
| 275 |
list->string = tmp; |
| 276 |
list->string[list->listsize++] = NULL; |
| 277 |
} |
| 278 |
list->string[list->nargs++] = str; |
| 279 |
|
| 280 |
return (0); |
| 281 |
} |
| 282 |
|
| 283 |
static void |
| 284 |
sunos_free_string_list(string_list_t *list) |
| 285 |
{ |
| 286 |
int i; |
| 287 |
|
| 288 |
for (i = 0; i < list->nargs; i++) { |
| 289 |
free(list->string[i]); |
| 290 |
} |
| 291 |
|
| 292 |
free(list->string); |
| 293 |
free(list); |
| 294 |
} |
| 295 |
|
| 296 |
static char ** |
| 297 |
sunos_build_argv_list(string_list_t *list) |
| 298 |
{ |
| 299 |
return (list->string); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
static int |
| 304 |
sunos_exec_command(struct libusb_context *ctx, const char *path, |
| 305 |
string_list_t *list) |
| 306 |
{ |
| 307 |
pid_t pid; |
| 308 |
int status; |
| 309 |
int waitstat; |
| 310 |
int exit_status; |
| 311 |
char **argv_list; |
| 312 |
|
| 313 |
argv_list = sunos_build_argv_list(list); |
| 314 |
if (argv_list == NULL) |
| 315 |
return (-1); |
| 316 |
|
| 317 |
pid = fork(); |
| 318 |
if (pid == 0) { |
| 319 |
/* child */ |
| 320 |
execv(path, argv_list); |
| 321 |
_exit(127); |
| 322 |
} else if (pid > 0) { |
| 323 |
/* parent */ |
| 324 |
do { |
| 325 |
waitstat = waitpid(pid, &status, 0); |
| 326 |
} while ((waitstat == -1 && errno == EINTR) || |
| 327 |
(waitstat == 0 && !WIFEXITED(status) && !WIFSIGNALED(status))); |
| 328 |
|
| 329 |
if (waitstat == 0) { |
| 330 |
if (WIFEXITED(status)) |
| 331 |
exit_status = WEXITSTATUS(status); |
| 332 |
else |
| 333 |
exit_status = WTERMSIG(status); |
| 334 |
} else { |
| 335 |
usbi_err(ctx, "waitpid failed: errno %d (%s)", errno, strerror(errno)); |
| 336 |
exit_status = -1; |
| 337 |
} |
| 338 |
} else { |
| 339 |
/* fork failed */ |
| 340 |
usbi_err(ctx, "fork failed: errno %d (%s)", errno, strerror(errno)); |
| 341 |
exit_status = -1; |
| 342 |
} |
| 343 |
|
| 344 |
return (exit_status); |
| 345 |
} |
| 346 |
|
| 347 |
static int |
| 348 |
sunos_detach_kernel_driver(struct libusb_device_handle *dev_handle, |
| 349 |
uint8_t interface_number) |
| 350 |
{ |
| 351 |
struct libusb_context *ctx = HANDLE_CTX(dev_handle); |
| 352 |
string_list_t *list; |
| 353 |
char path_arg[PATH_MAX]; |
| 354 |
sunos_dev_priv_t *dpriv; |
| 355 |
int r; |
| 356 |
|
| 357 |
UNUSED(interface_number); |
| 358 |
|
| 359 |
dpriv = usbi_get_device_priv(dev_handle->dev); |
| 360 |
snprintf(path_arg, sizeof(path_arg), "\'\"%s\"\'", dpriv->phypath); |
| 361 |
usbi_dbg(HANDLE_CTX(dev_handle), "%s", path_arg); |
| 362 |
|
| 363 |
list = sunos_new_string_list(); |
| 364 |
if (list == NULL) |
| 365 |
return (LIBUSB_ERROR_NO_MEM); |
| 366 |
|
| 367 |
/* attach ugen driver */ |
| 368 |
r = 0; |
| 369 |
r |= sunos_append_to_string_list(list, UPDATEDRV); |
| 370 |
r |= sunos_append_to_string_list(list, "-a"); /* add rule */ |
| 371 |
r |= sunos_append_to_string_list(list, "-i"); /* specific device */ |
| 372 |
r |= sunos_append_to_string_list(list, path_arg); /* physical path */ |
| 373 |
r |= sunos_append_to_string_list(list, "ugen"); |
| 374 |
if (r) { |
| 375 |
sunos_free_string_list(list); |
| 376 |
return (LIBUSB_ERROR_NO_MEM); |
| 377 |
} |
| 378 |
|
| 379 |
r = sunos_exec_command(ctx, UPDATEDRV_PATH, list); |
| 380 |
sunos_free_string_list(list); |
| 381 |
if (r < 0) |
| 382 |
return (LIBUSB_ERROR_OTHER); |
| 383 |
|
| 384 |
/* reconfigure the driver node */ |
| 385 |
r = 0; |
| 386 |
r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_DISCONNECT); |
| 387 |
r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_CONFIGURE); |
| 388 |
if (r) |
| 389 |
usbi_warn(HANDLE_CTX(dev_handle), "one or more ioctls failed"); |
| 390 |
|
| 391 |
snprintf(path_arg, sizeof(path_arg), "^usb/%x.%x", |
| 392 |
dev_handle->dev->device_descriptor.idVendor, |
| 393 |
dev_handle->dev->device_descriptor.idProduct); |
| 394 |
sunos_physpath_to_devlink(dpriv->phypath, path_arg, &dpriv->ugenpath); |
| 395 |
|
| 396 |
if (access(dpriv->ugenpath, F_OK) == -1) { |
| 397 |
usbi_err(HANDLE_CTX(dev_handle), "fail to detach kernel driver"); |
| 398 |
return (LIBUSB_ERROR_IO); |
| 399 |
} |
| 400 |
|
| 401 |
return sunos_usb_open_ep0(usbi_get_device_handle_priv(dev_handle), dpriv); |
| 402 |
} |
| 403 |
|
| 404 |
static int |
| 405 |
sunos_attach_kernel_driver(struct libusb_device_handle *dev_handle, |
| 406 |
uint8_t interface_number) |
| 407 |
{ |
| 408 |
struct libusb_context *ctx = HANDLE_CTX(dev_handle); |
| 409 |
string_list_t *list; |
| 410 |
char path_arg[PATH_MAX]; |
| 411 |
sunos_dev_priv_t *dpriv; |
| 412 |
int r; |
| 413 |
|
| 414 |
UNUSED(interface_number); |
| 415 |
|
| 416 |
/* we open the dev in detach driver, so we need close it first. */ |
| 417 |
sunos_close(dev_handle); |
| 418 |
|
| 419 |
dpriv = usbi_get_device_priv(dev_handle->dev); |
| 420 |
snprintf(path_arg, sizeof(path_arg), "\'\"%s\"\'", dpriv->phypath); |
| 421 |
usbi_dbg(HANDLE_CTX(dev_handle), "%s", path_arg); |
| 422 |
|
| 423 |
list = sunos_new_string_list(); |
| 424 |
if (list == NULL) |
| 425 |
return (LIBUSB_ERROR_NO_MEM); |
| 426 |
|
| 427 |
/* detach ugen driver */ |
| 428 |
r = 0; |
| 429 |
r |= sunos_append_to_string_list(list, UPDATEDRV); |
| 430 |
r |= sunos_append_to_string_list(list, "-d"); /* add rule */ |
| 431 |
r |= sunos_append_to_string_list(list, "-i"); /* specific device */ |
| 432 |
r |= sunos_append_to_string_list(list, path_arg); /* physical path */ |
| 433 |
r |= sunos_append_to_string_list(list, "ugen"); |
| 434 |
if (r) { |
| 435 |
sunos_free_string_list(list); |
| 436 |
return (LIBUSB_ERROR_NO_MEM); |
| 437 |
} |
| 438 |
|
| 439 |
r = sunos_exec_command(ctx, UPDATEDRV_PATH, list); |
| 440 |
sunos_free_string_list(list); |
| 441 |
if (r < 0) |
| 442 |
return (LIBUSB_ERROR_OTHER); |
| 443 |
|
| 444 |
/* reconfigure the driver node */ |
| 445 |
r = 0; |
| 446 |
r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_CONFIGURE); |
| 447 |
r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_DISCONNECT); |
| 448 |
r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_CONFIGURE); |
| 449 |
if (r) |
| 450 |
usbi_warn(HANDLE_CTX(dev_handle), "one or more ioctls failed"); |
| 451 |
|
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
static int |
| 456 |
sunos_fill_in_dev_info(di_node_t node, struct libusb_device *dev) |
| 457 |
{ |
| 458 |
int proplen; |
| 459 |
int *i, n, *addr, *port_prop; |
| 460 |
char *phypath; |
| 461 |
uint8_t *rdata; |
| 462 |
sunos_dev_priv_t *dpriv = usbi_get_device_priv(dev); |
| 463 |
char match_str[PATH_MAX]; |
| 464 |
|
| 465 |
/* Device descriptors */ |
| 466 |
proplen = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, |
| 467 |
"usb-dev-descriptor", &rdata); |
| 468 |
if (proplen <= 0) { |
| 469 |
return (LIBUSB_ERROR_IO); |
| 470 |
} |
| 471 |
bcopy(rdata, &dev->device_descriptor, LIBUSB_DT_DEVICE_SIZE); |
| 472 |
|
| 473 |
/* Raw configuration descriptors */ |
| 474 |
proplen = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, |
| 475 |
"usb-raw-cfg-descriptors", &rdata); |
| 476 |
if (proplen <= 0) { |
| 477 |
usbi_dbg(DEVICE_CTX(dev), "can't find raw config descriptors"); |
| 478 |
|
| 479 |
return (LIBUSB_ERROR_IO); |
| 480 |
} |
| 481 |
dpriv->raw_cfgdescr = calloc(1, proplen); |
| 482 |
if (dpriv->raw_cfgdescr == NULL) { |
| 483 |
return (LIBUSB_ERROR_NO_MEM); |
| 484 |
} else { |
| 485 |
bcopy(rdata, dpriv->raw_cfgdescr, proplen); |
| 486 |
dpriv->cfgvalue = ((struct libusb_config_descriptor *) |
| 487 |
rdata)->bConfigurationValue; |
| 488 |
} |
| 489 |
|
| 490 |
n = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &port_prop); |
| 491 |
|
| 492 |
if ((n != 1) || (*port_prop <= 0)) { |
| 493 |
return (LIBUSB_ERROR_IO); |
| 494 |
} |
| 495 |
dev->port_number = *port_prop; |
| 496 |
|
| 497 |
/* device physical path */ |
| 498 |
phypath = di_devfs_path(node); |
| 499 |
if (phypath) { |
| 500 |
dpriv->phypath = strdup(phypath); |
| 501 |
snprintf(match_str, sizeof(match_str), "^usb/%x.%x", |
| 502 |
dev->device_descriptor.idVendor, |
| 503 |
dev->device_descriptor.idProduct); |
| 504 |
usbi_dbg(DEVICE_CTX(dev), "match is %s", match_str); |
| 505 |
sunos_physpath_to_devlink(dpriv->phypath, match_str, &dpriv->ugenpath); |
| 506 |
di_devfs_path_free(phypath); |
| 507 |
|
| 508 |
} else { |
| 509 |
free(dpriv->raw_cfgdescr); |
| 510 |
|
| 511 |
return (LIBUSB_ERROR_IO); |
| 512 |
} |
| 513 |
|
| 514 |
/* address */ |
| 515 |
n = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "assigned-address", &addr); |
| 516 |
if (n != 1 || *addr == 0) { |
| 517 |
usbi_dbg(DEVICE_CTX(dev), "can't get address"); |
| 518 |
} else { |
| 519 |
dev->device_address = *addr; |
| 520 |
} |
| 521 |
|
| 522 |
/* speed */ |
| 523 |
if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "low-speed", &i) >= 0) { |
| 524 |
dev->speed = LIBUSB_SPEED_LOW; |
| 525 |
} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "high-speed", &i) >= 0) { |
| 526 |
dev->speed = LIBUSB_SPEED_HIGH; |
| 527 |
} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "full-speed", &i) >= 0) { |
| 528 |
dev->speed = LIBUSB_SPEED_FULL; |
| 529 |
} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "super-speed", &i) >= 0) { |
| 530 |
dev->speed = LIBUSB_SPEED_SUPER; |
| 531 |
} |
| 532 |
|
| 533 |
usbi_dbg(DEVICE_CTX(dev), "vid=%x pid=%x, path=%s, bus_nmber=0x%x, port_number=%d, speed=%d", |
| 534 |
dev->device_descriptor.idVendor, dev->device_descriptor.idProduct, |
| 535 |
dpriv->phypath, dev->bus_number, dev->port_number, dev->speed); |
| 536 |
|
| 537 |
return (LIBUSB_SUCCESS); |
| 538 |
} |
| 539 |
|
| 540 |
static int |
| 541 |
sunos_add_devices(di_devlink_t link, void *arg) |
| 542 |
{ |
| 543 |
struct devlink_cbarg *largs = (struct devlink_cbarg *)arg; |
| 544 |
struct node_args *nargs; |
| 545 |
di_node_t myself, dn; |
| 546 |
uint64_t session_id = 0; |
| 547 |
uint64_t sid = 0; |
| 548 |
uint64_t bdf = 0; |
| 549 |
struct libusb_device *dev; |
| 550 |
sunos_dev_priv_t *devpriv; |
| 551 |
int n, *j; |
| 552 |
int i = 0; |
| 553 |
int *addr_prop; |
| 554 |
uint8_t bus_number = 0; |
| 555 |
uint32_t * regbuf = NULL; |
| 556 |
uint32_t reg; |
| 557 |
|
| 558 |
UNUSED(link); |
| 559 |
|
| 560 |
nargs = (struct node_args *)largs->nargs; |
| 561 |
myself = largs->myself; |
| 562 |
|
| 563 |
/* |
| 564 |
* Construct session ID. |
| 565 |
* session ID = dev_addr | hub addr |parent hub addr|...|root hub bdf |
| 566 |
* 8 bits 8bits 8 bits 16bits |
| 567 |
*/ |
| 568 |
if (myself == DI_NODE_NIL) |
| 569 |
return (DI_WALK_CONTINUE); |
| 570 |
|
| 571 |
dn = myself; |
| 572 |
/* find the root hub */ |
| 573 |
while (di_prop_lookup_ints(DDI_DEV_T_ANY, dn, "root-hub", &j) != 0) { |
| 574 |
usbi_dbg(NULL, "find_root_hub:%s", di_devfs_path(dn)); |
| 575 |
n = di_prop_lookup_ints(DDI_DEV_T_ANY, dn, |
| 576 |
"assigned-address", &addr_prop); |
| 577 |
session_id |= ((addr_prop[0] & 0xff) << i++ * 8); |
| 578 |
dn = di_parent_node(dn); |
| 579 |
} |
| 580 |
|
| 581 |
/* dn is the root hub node */ |
| 582 |
n = di_prop_lookup_ints(DDI_DEV_T_ANY, dn, "reg", (int **)®buf); |
| 583 |
reg = regbuf[0]; |
| 584 |
bdf = (PCI_REG_BUS_G(reg) << 8) | (PCI_REG_DEV_G(reg) << 3) | PCI_REG_FUNC_G(reg); |
| 585 |
/* bdf must larger than i*8 bits */ |
| 586 |
session_id |= (bdf << i * 8); |
| 587 |
bus_number = (PCI_REG_DEV_G(reg) << 3) | PCI_REG_FUNC_G(reg); |
| 588 |
|
| 589 |
usbi_dbg(NULL, "device bus address=%s:%x, name:%s", |
| 590 |
di_bus_addr(myself), bus_number, di_node_name(dn)); |
| 591 |
usbi_dbg(NULL, "session id org:%" PRIx64, session_id); |
| 592 |
|
| 593 |
/* dn is the usb device */ |
| 594 |
for (dn = di_child_node(myself); dn != DI_NODE_NIL; dn = di_sibling_node(dn)) { |
| 595 |
usbi_dbg(NULL, "device path:%s", di_devfs_path(dn)); |
| 596 |
/* skip hub devices, because its driver can not been unload */ |
| 597 |
if (di_prop_lookup_ints(DDI_DEV_T_ANY, dn, "usb-port-count", &addr_prop) != -1) |
| 598 |
continue; |
| 599 |
/* usb_addr */ |
| 600 |
n = di_prop_lookup_ints(DDI_DEV_T_ANY, dn, |
| 601 |
"assigned-address", &addr_prop); |
| 602 |
if ((n != 1) || (addr_prop[0] == 0)) { |
| 603 |
usbi_dbg(NULL, "cannot get valid usb_addr"); |
| 604 |
continue; |
| 605 |
} |
| 606 |
|
| 607 |
sid = (session_id << 8) | (addr_prop[0] & 0xff) ; |
| 608 |
usbi_dbg(NULL, "session id %" PRIX64, sid); |
| 609 |
|
| 610 |
dev = usbi_get_device_by_session_id(nargs->ctx, sid); |
| 611 |
if (dev == NULL) { |
| 612 |
dev = usbi_alloc_device(nargs->ctx, sid); |
| 613 |
if (dev == NULL) { |
| 614 |
usbi_dbg(NULL, "can't alloc device"); |
| 615 |
continue; |
| 616 |
} |
| 617 |
devpriv = usbi_get_device_priv(dev); |
| 618 |
dev->bus_number = bus_number; |
| 619 |
|
| 620 |
if (sunos_fill_in_dev_info(dn, dev) != LIBUSB_SUCCESS) { |
| 621 |
libusb_unref_device(dev); |
| 622 |
usbi_dbg(NULL, "get information fail"); |
| 623 |
continue; |
| 624 |
} |
| 625 |
if (usbi_sanitize_device(dev) < 0) { |
| 626 |
libusb_unref_device(dev); |
| 627 |
usbi_dbg(NULL, "sanitize failed: "); |
| 628 |
return (DI_WALK_TERMINATE); |
| 629 |
} |
| 630 |
|
| 631 |
usbi_connect_device(dev); |
| 632 |
} else { |
| 633 |
devpriv = usbi_get_device_priv(dev); |
| 634 |
usbi_dbg(NULL, "Dev %s exists", devpriv->ugenpath); |
| 635 |
} |
| 636 |
|
| 637 |
if (discovered_devs_append(*(nargs->discdevs), dev) == NULL) { |
| 638 |
usbi_dbg(NULL, "cannot append device"); |
| 639 |
} |
| 640 |
|
| 641 |
/* |
| 642 |
* we alloc and hence ref this dev. We don't need to ref it |
| 643 |
* hereafter. Front end or app should take care of their ref. |
| 644 |
*/ |
| 645 |
libusb_unref_device(dev); |
| 646 |
|
| 647 |
usbi_dbg(NULL, "Device %s %s id=0x%" PRIx64 ", devcount:%" PRIuPTR |
| 648 |
", bdf=%" PRIx64, |
| 649 |
devpriv->ugenpath, di_devfs_path(dn), (uint64_t)sid, |
| 650 |
(*nargs->discdevs)->len, bdf); |
| 651 |
} |
| 652 |
|
| 653 |
return (DI_WALK_CONTINUE); |
| 654 |
} |
| 655 |
|
| 656 |
static int |
| 657 |
sunos_walk_minor_node_link(di_node_t node, void *args) |
| 658 |
{ |
| 659 |
di_minor_t minor = DI_MINOR_NIL; |
| 660 |
char *minor_path; |
| 661 |
struct devlink_cbarg arg; |
| 662 |
struct node_args *nargs = (struct node_args *)args; |
| 663 |
di_devlink_handle_t devlink_hdl = nargs->dlink_hdl; |
| 664 |
|
| 665 |
/* walk each minor to find usb devices */ |
| 666 |
while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) { |
| 667 |
minor_path = di_devfs_minor_path(minor); |
| 668 |
arg.nargs = args; |
| 669 |
arg.myself = node; |
| 670 |
arg.minor = minor; |
| 671 |
(void) di_devlink_walk(devlink_hdl, |
| 672 |
"^usb/hub[0-9]+", minor_path, |
| 673 |
DI_PRIMARY_LINK, (void *)&arg, sunos_add_devices); |
| 674 |
di_devfs_path_free(minor_path); |
| 675 |
} |
| 676 |
|
| 677 |
/* switch to a different node */ |
| 678 |
nargs->last_ugenpath = NULL; |
| 679 |
|
| 680 |
return (DI_WALK_CONTINUE); |
| 681 |
} |
| 682 |
|
| 683 |
int |
| 684 |
sunos_get_device_list(struct libusb_context * ctx, |
| 685 |
struct discovered_devs **discdevs) |
| 686 |
{ |
| 687 |
di_node_t root_node; |
| 688 |
struct node_args args; |
| 689 |
di_devlink_handle_t devlink_hdl; |
| 690 |
|
| 691 |
args.ctx = ctx; |
| 692 |
args.discdevs = discdevs; |
| 693 |
args.last_ugenpath = NULL; |
| 694 |
if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) { |
| 695 |
usbi_dbg(ctx, "di_int() failed: errno %d (%s)", errno, strerror(errno)); |
| 696 |
return (LIBUSB_ERROR_IO); |
| 697 |
} |
| 698 |
|
| 699 |
if ((devlink_hdl = di_devlink_init(NULL, 0)) == NULL) { |
| 700 |
di_fini(root_node); |
| 701 |
usbi_dbg(ctx, "di_devlink_init() failed: errno %d (%s)", errno, strerror(errno)); |
| 702 |
|
| 703 |
return (LIBUSB_ERROR_IO); |
| 704 |
} |
| 705 |
args.dlink_hdl = devlink_hdl; |
| 706 |
|
| 707 |
/* walk each node to find USB devices */ |
| 708 |
if (di_walk_node(root_node, DI_WALK_SIBFIRST, &args, |
| 709 |
sunos_walk_minor_node_link) == -1) { |
| 710 |
usbi_dbg(ctx, "di_walk_node() failed: errno %d (%s)", errno, strerror(errno)); |
| 711 |
di_fini(root_node); |
| 712 |
|
| 713 |
return (LIBUSB_ERROR_IO); |
| 714 |
} |
| 715 |
|
| 716 |
di_fini(root_node); |
| 717 |
di_devlink_fini(&devlink_hdl); |
| 718 |
|
| 719 |
usbi_dbg(ctx, "%zu devices", (*discdevs)->len); |
| 720 |
|
| 721 |
return ((*discdevs)->len); |
| 722 |
} |
| 723 |
|
| 724 |
static int |
| 725 |
sunos_usb_open_ep0(sunos_dev_handle_priv_t *hpriv, sunos_dev_priv_t *dpriv) |
| 726 |
{ |
| 727 |
char filename[PATH_MAX + 1]; |
| 728 |
|
| 729 |
if (hpriv->eps[0].datafd > 0) { |
| 730 |
return (LIBUSB_SUCCESS); |
| 731 |
} |
| 732 |
snprintf(filename, PATH_MAX, "%s/cntrl0", dpriv->ugenpath); |
| 733 |
|
| 734 |
usbi_dbg(NULL, "opening %s", filename); |
| 735 |
hpriv->eps[0].datafd = open(filename, O_RDWR); |
| 736 |
if (hpriv->eps[0].datafd < 0) { |
| 737 |
return(_errno_to_libusb(errno)); |
| 738 |
} |
| 739 |
|
| 740 |
snprintf(filename, PATH_MAX, "%s/cntrl0stat", dpriv->ugenpath); |
| 741 |
hpriv->eps[0].statfd = open(filename, O_RDONLY); |
| 742 |
if (hpriv->eps[0].statfd < 0) { |
| 743 |
close(hpriv->eps[0].datafd); |
| 744 |
hpriv->eps[0].datafd = -1; |
| 745 |
|
| 746 |
return(_errno_to_libusb(errno)); |
| 747 |
} |
| 748 |
|
| 749 |
return (LIBUSB_SUCCESS); |
| 750 |
} |
| 751 |
|
| 752 |
static void |
| 753 |
sunos_usb_close_all_eps(sunos_dev_handle_priv_t *hdev) |
| 754 |
{ |
| 755 |
int i; |
| 756 |
|
| 757 |
/* not close ep0 */ |
| 758 |
for (i = 1; i < USB_MAXENDPOINTS; i++) { |
| 759 |
if (hdev->eps[i].datafd != -1) { |
| 760 |
(void) close(hdev->eps[i].datafd); |
| 761 |
hdev->eps[i].datafd = -1; |
| 762 |
} |
| 763 |
if (hdev->eps[i].statfd != -1) { |
| 764 |
(void) close(hdev->eps[i].statfd); |
| 765 |
hdev->eps[i].statfd = -1; |
| 766 |
} |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
static void |
| 771 |
sunos_usb_close_ep0(sunos_dev_handle_priv_t *hdev) |
| 772 |
{ |
| 773 |
if (hdev->eps[0].datafd >= 0) { |
| 774 |
close(hdev->eps[0].datafd); |
| 775 |
close(hdev->eps[0].statfd); |
| 776 |
hdev->eps[0].datafd = -1; |
| 777 |
hdev->eps[0].statfd = -1; |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
static uchar_t |
| 782 |
sunos_usb_ep_index(uint8_t ep_addr) |
| 783 |
{ |
| 784 |
return ((ep_addr & LIBUSB_ENDPOINT_ADDRESS_MASK) + |
| 785 |
((ep_addr & LIBUSB_ENDPOINT_DIR_MASK) ? 16 : 0)); |
| 786 |
} |
| 787 |
|
| 788 |
static int |
| 789 |
sunos_find_interface(struct libusb_device_handle *hdev, |
| 790 |
uint8_t endpoint, uint8_t *interface) |
| 791 |
{ |
| 792 |
struct libusb_config_descriptor *config; |
| 793 |
int r; |
| 794 |
int iface_idx; |
| 795 |
|
| 796 |
r = libusb_get_active_config_descriptor(hdev->dev, &config); |
| 797 |
if (r < 0) { |
| 798 |
return (LIBUSB_ERROR_INVALID_PARAM); |
| 799 |
} |
| 800 |
|
| 801 |
for (iface_idx = 0; iface_idx < config->bNumInterfaces; iface_idx++) { |
| 802 |
const struct libusb_interface *iface = |
| 803 |
&config->interface[iface_idx]; |
| 804 |
int altsetting_idx; |
| 805 |
|
| 806 |
for (altsetting_idx = 0; altsetting_idx < iface->num_altsetting; |
| 807 |
altsetting_idx++) { |
| 808 |
const struct libusb_interface_descriptor *altsetting = |
| 809 |
&iface->altsetting[altsetting_idx]; |
| 810 |
int ep_idx; |
| 811 |
|
| 812 |
for (ep_idx = 0; ep_idx < altsetting->bNumEndpoints; |
| 813 |
ep_idx++) { |
| 814 |
const struct libusb_endpoint_descriptor *ep = |
| 815 |
&altsetting->endpoint[ep_idx]; |
| 816 |
if (ep->bEndpointAddress == endpoint) { |
| 817 |
*interface = iface_idx; |
| 818 |
libusb_free_config_descriptor(config); |
| 819 |
|
| 820 |
return (LIBUSB_SUCCESS); |
| 821 |
} |
| 822 |
} |
| 823 |
} |
| 824 |
} |
| 825 |
libusb_free_config_descriptor(config); |
| 826 |
|
| 827 |
return (LIBUSB_ERROR_INVALID_PARAM); |
| 828 |
} |
| 829 |
|
| 830 |
static int |
| 831 |
sunos_check_device_and_status_open(struct libusb_device_handle *hdl, |
| 832 |
uint8_t ep_addr, int ep_type) |
| 833 |
{ |
| 834 |
char filename[PATH_MAX + 1], statfilename[PATH_MAX + 1]; |
| 835 |
char cfg_num[16], alt_num[16]; |
| 836 |
int fd, fdstat, mode, e; |
| 837 |
uint8_t ifc = 0; |
| 838 |
uint8_t ep_index; |
| 839 |
sunos_dev_handle_priv_t *hpriv; |
| 840 |
|
| 841 |
usbi_dbg(HANDLE_CTX(hdl), "open ep 0x%02x", ep_addr); |
| 842 |
hpriv = usbi_get_device_handle_priv(hdl); |
| 843 |
ep_index = sunos_usb_ep_index(ep_addr); |
| 844 |
/* ep already opened */ |
| 845 |
if ((hpriv->eps[ep_index].datafd > 0) && |
| 846 |
(hpriv->eps[ep_index].statfd > 0)) { |
| 847 |
usbi_dbg(HANDLE_CTX(hdl), "ep 0x%02x already opened, return success", |
| 848 |
ep_addr); |
| 849 |
|
| 850 |
return (0); |
| 851 |
} |
| 852 |
|
| 853 |
if (sunos_find_interface(hdl, ep_addr, &ifc) < 0) { |
| 854 |
usbi_dbg(HANDLE_CTX(hdl), "can't find interface for endpoint 0x%02x", |
| 855 |
ep_addr); |
| 856 |
|
| 857 |
return (EACCES); |
| 858 |
} |
| 859 |
|
| 860 |
/* create filename */ |
| 861 |
if (hpriv->config_index > 0) { |
| 862 |
(void) snprintf(cfg_num, sizeof(cfg_num), "cfg%d", |
| 863 |
hpriv->config_index + 1); |
| 864 |
} else { |
| 865 |
bzero(cfg_num, sizeof(cfg_num)); |
| 866 |
} |
| 867 |
|
| 868 |
if (hpriv->altsetting[ifc] > 0) { |
| 869 |
(void) snprintf(alt_num, sizeof(alt_num), ".%d", |
| 870 |
hpriv->altsetting[ifc]); |
| 871 |
} else { |
| 872 |
bzero(alt_num, sizeof(alt_num)); |
| 873 |
} |
| 874 |
|
| 875 |
e = snprintf(filename, sizeof (filename), "%s/%sif%d%s%s%d", |
| 876 |
hpriv->dpriv->ugenpath, cfg_num, ifc, alt_num, |
| 877 |
(ep_addr & LIBUSB_ENDPOINT_DIR_MASK) ? "in" : "out", |
| 878 |
ep_addr & LIBUSB_ENDPOINT_ADDRESS_MASK); |
| 879 |
if (e < 0 || e >= (int)sizeof (filename)) { |
| 880 |
usbi_dbg(HANDLE_CTX(hdl), |
| 881 |
"path buffer overflow for endpoint 0x%02x", ep_addr); |
| 882 |
return (EINVAL); |
| 883 |
} |
| 884 |
|
| 885 |
e = snprintf(statfilename, sizeof (statfilename), "%sstat", filename); |
| 886 |
if (e < 0 || e >= (int)sizeof (statfilename)) { |
| 887 |
usbi_dbg(HANDLE_CTX(hdl), |
| 888 |
"path buffer overflow for endpoint 0x%02x stat", ep_addr); |
| 889 |
return (EINVAL); |
| 890 |
} |
| 891 |
|
| 892 |
/* |
| 893 |
* In case configuration has been switched, the xfer endpoint needs |
| 894 |
* to be opened before the status endpoint, due to a ugen issue. |
| 895 |
* However, to enable the one transfer mode for an Interrupt-In pipe, |
| 896 |
* the status endpoint needs to be opened before the xfer endpoint. |
| 897 |
* So, open the xfer mode first and close it immediately |
| 898 |
* as a workaround. This will handle the configuration switch. |
| 899 |
* Then, open the status endpoint. If for an Interrupt-in pipe, |
| 900 |
* write the USB_EP_INTR_ONE_XFER control to the status endpoint |
| 901 |
* to enable the one transfer mode. Then, re-open the xfer mode. |
| 902 |
*/ |
| 903 |
if (ep_type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) { |
| 904 |
mode = O_RDWR; |
| 905 |
} else if (ep_addr & LIBUSB_ENDPOINT_IN) { |
| 906 |
mode = O_RDONLY; |
| 907 |
} else { |
| 908 |
mode = O_WRONLY; |
| 909 |
} |
| 910 |
/* Open the xfer endpoint first */ |
| 911 |
if ((fd = open(filename, mode)) == -1) { |
| 912 |
usbi_dbg(HANDLE_CTX(hdl), "can't open %s: errno %d (%s)", filename, errno, |
| 913 |
strerror(errno)); |
| 914 |
|
| 915 |
return (errno); |
| 916 |
} |
| 917 |
/* And immediately close the xfer endpoint */ |
| 918 |
(void) close(fd); |
| 919 |
|
| 920 |
/* |
| 921 |
* Open the status endpoint. |
| 922 |
* If for an Interrupt-IN pipe, need to enable the one transfer mode |
| 923 |
* by writing USB_EP_INTR_ONE_XFER control to the status endpoint |
| 924 |
* before opening the xfer endpoint |
| 925 |
*/ |
| 926 |
if ((ep_type == LIBUSB_TRANSFER_TYPE_INTERRUPT) && |
| 927 |
(ep_addr & LIBUSB_ENDPOINT_IN)) { |
| 928 |
char control = USB_EP_INTR_ONE_XFER; |
| 929 |
ssize_t count; |
| 930 |
|
| 931 |
/* Open the status endpoint with RDWR */ |
| 932 |
if ((fdstat = open(statfilename, O_RDWR)) == -1) { |
| 933 |
usbi_dbg(HANDLE_CTX(hdl), "can't open %s RDWR: errno %d (%s)", |
| 934 |
statfilename, errno, strerror(errno)); |
| 935 |
|
| 936 |
return (errno); |
| 937 |
} else { |
| 938 |
count = write(fdstat, &control, sizeof(control)); |
| 939 |
if (count != 1) { |
| 940 |
/* this should have worked */ |
| 941 |
usbi_dbg(HANDLE_CTX(hdl), "can't write to %s: errno %d (%s)", |
| 942 |
statfilename, errno, strerror(errno)); |
| 943 |
(void) close(fdstat); |
| 944 |
|
| 945 |
return (errno); |
| 946 |
} |
| 947 |
} |
| 948 |
} else { |
| 949 |
if ((fdstat = open(statfilename, O_RDONLY)) == -1) { |
| 950 |
usbi_dbg(HANDLE_CTX(hdl), "can't open %s: errno %d (%s)", statfilename, errno, |
| 951 |
strerror(errno)); |
| 952 |
|
| 953 |
return (errno); |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
/* Re-open the xfer endpoint */ |
| 958 |
if ((fd = open(filename, mode)) == -1) { |
| 959 |
usbi_dbg(HANDLE_CTX(hdl), "can't open %s: errno %d (%s)", filename, errno, |
| 960 |
strerror(errno)); |
| 961 |
(void) close(fdstat); |
| 962 |
|
| 963 |
return (errno); |
| 964 |
} |
| 965 |
|
| 966 |
hpriv->eps[ep_index].datafd = fd; |
| 967 |
hpriv->eps[ep_index].statfd = fdstat; |
| 968 |
usbi_dbg(HANDLE_CTX(hdl), "ep=0x%02x datafd=%d, statfd=%d", ep_addr, fd, fdstat); |
| 969 |
|
| 970 |
return (0); |
| 971 |
} |
| 972 |
|
| 973 |
int |
| 974 |
sunos_open(struct libusb_device_handle *handle) |
| 975 |
{ |
| 976 |
sunos_dev_handle_priv_t *hpriv; |
| 977 |
sunos_dev_priv_t *dpriv; |
| 978 |
int i; |
| 979 |
int ret; |
| 980 |
|
| 981 |
hpriv = usbi_get_device_handle_priv(handle); |
| 982 |
dpriv = usbi_get_device_priv(handle->dev); |
| 983 |
hpriv->dpriv = dpriv; |
| 984 |
|
| 985 |
/* set all file descriptors to "closed" */ |
| 986 |
for (i = 0; i < USB_MAXENDPOINTS; i++) { |
| 987 |
hpriv->eps[i].datafd = -1; |
| 988 |
hpriv->eps[i].statfd = -1; |
| 989 |
} |
| 990 |
|
| 991 |
if (sunos_kernel_driver_active(handle, 0)) { |
| 992 |
/* pretend we can open the device */ |
| 993 |
return (LIBUSB_SUCCESS); |
| 994 |
} |
| 995 |
|
| 996 |
if ((ret = sunos_usb_open_ep0(hpriv, dpriv)) != LIBUSB_SUCCESS) { |
| 997 |
usbi_dbg(HANDLE_CTX(handle), "fail: %d", ret); |
| 998 |
return (ret); |
| 999 |
} |
| 1000 |
|
| 1001 |
return (LIBUSB_SUCCESS); |
| 1002 |
} |
| 1003 |
|
| 1004 |
void |
| 1005 |
sunos_close(struct libusb_device_handle *handle) |
| 1006 |
{ |
| 1007 |
sunos_dev_handle_priv_t *hpriv; |
| 1008 |
|
| 1009 |
usbi_dbg(HANDLE_CTX(handle), " "); |
| 1010 |
|
| 1011 |
hpriv = usbi_get_device_handle_priv(handle); |
| 1012 |
|
| 1013 |
sunos_usb_close_all_eps(hpriv); |
| 1014 |
sunos_usb_close_ep0(hpriv); |
| 1015 |
} |
| 1016 |
|
| 1017 |
int |
| 1018 |
sunos_get_active_config_descriptor(struct libusb_device *dev, |
| 1019 |
void *buf, size_t len) |
| 1020 |
{ |
| 1021 |
sunos_dev_priv_t *dpriv = usbi_get_device_priv(dev); |
| 1022 |
struct libusb_config_descriptor *cfg; |
| 1023 |
int proplen; |
| 1024 |
di_node_t node; |
| 1025 |
uint8_t *rdata; |
| 1026 |
|
| 1027 |
/* |
| 1028 |
* Keep raw configuration descriptors updated, in case config |
| 1029 |
* has ever been changed through setCfg. |
| 1030 |
*/ |
| 1031 |
if ((node = di_init(dpriv->phypath, DINFOCPYALL)) == DI_NODE_NIL) { |
| 1032 |
usbi_dbg(DEVICE_CTX(dev), "di_int() failed: errno %d (%s)", errno, |
| 1033 |
strerror(errno)); |
| 1034 |
return (LIBUSB_ERROR_IO); |
| 1035 |
} |
| 1036 |
proplen = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, |
| 1037 |
"usb-raw-cfg-descriptors", &rdata); |
| 1038 |
if (proplen <= 0) { |
| 1039 |
usbi_dbg(DEVICE_CTX(dev), "can't find raw config descriptors"); |
| 1040 |
|
| 1041 |
return (LIBUSB_ERROR_IO); |
| 1042 |
} |
| 1043 |
dpriv->raw_cfgdescr = realloc(dpriv->raw_cfgdescr, proplen); |
| 1044 |
if (dpriv->raw_cfgdescr == NULL) { |
| 1045 |
return (LIBUSB_ERROR_NO_MEM); |
| 1046 |
} else { |
| 1047 |
bcopy(rdata, dpriv->raw_cfgdescr, proplen); |
| 1048 |
dpriv->cfgvalue = ((struct libusb_config_descriptor *) |
| 1049 |
rdata)->bConfigurationValue; |
| 1050 |
} |
| 1051 |
di_fini(node); |
| 1052 |
|
| 1053 |
cfg = (struct libusb_config_descriptor *)dpriv->raw_cfgdescr; |
| 1054 |
len = MIN(len, libusb_le16_to_cpu(cfg->wTotalLength)); |
| 1055 |
memcpy(buf, dpriv->raw_cfgdescr, len); |
| 1056 |
usbi_dbg(DEVICE_CTX(dev), "path:%s len %zu", dpriv->phypath, len); |
| 1057 |
|
| 1058 |
return (len); |
| 1059 |
} |
| 1060 |
|
| 1061 |
int |
| 1062 |
sunos_get_config_descriptor(struct libusb_device *dev, uint8_t idx, |
| 1063 |
void *buf, size_t len) |
| 1064 |
{ |
| 1065 |
UNUSED(idx); |
| 1066 |
/* XXX */ |
| 1067 |
return(sunos_get_active_config_descriptor(dev, buf, len)); |
| 1068 |
} |
| 1069 |
|
| 1070 |
int |
| 1071 |
sunos_get_configuration(struct libusb_device_handle *handle, uint8_t *config) |
| 1072 |
{ |
| 1073 |
sunos_dev_priv_t *dpriv = usbi_get_device_priv(handle->dev); |
| 1074 |
|
| 1075 |
*config = dpriv->cfgvalue; |
| 1076 |
|
| 1077 |
usbi_dbg(HANDLE_CTX(handle), "bConfigurationValue %u", *config); |
| 1078 |
|
| 1079 |
return (LIBUSB_SUCCESS); |
| 1080 |
} |
| 1081 |
|
| 1082 |
int |
| 1083 |
sunos_set_configuration(struct libusb_device_handle *handle, int config) |
| 1084 |
{ |
| 1085 |
sunos_dev_priv_t *dpriv = usbi_get_device_priv(handle->dev); |
| 1086 |
sunos_dev_handle_priv_t *hpriv; |
| 1087 |
|
| 1088 |
usbi_dbg(HANDLE_CTX(handle), "bConfigurationValue %d", config); |
| 1089 |
hpriv = usbi_get_device_handle_priv(handle); |
| 1090 |
|
| 1091 |
if (dpriv->ugenpath == NULL) |
| 1092 |
return (LIBUSB_ERROR_NOT_SUPPORTED); |
| 1093 |
|
| 1094 |
if (config < 1) |
| 1095 |
return (LIBUSB_ERROR_NOT_SUPPORTED); |
| 1096 |
|
| 1097 |
dpriv->cfgvalue = config; |
| 1098 |
hpriv->config_index = config - 1; |
| 1099 |
|
| 1100 |
return (LIBUSB_SUCCESS); |
| 1101 |
} |
| 1102 |
|
| 1103 |
int |
| 1104 |
sunos_claim_interface(struct libusb_device_handle *handle, uint8_t iface) |
| 1105 |
{ |
| 1106 |
UNUSED(handle); |
| 1107 |
|
| 1108 |
usbi_dbg(HANDLE_CTX(handle), "iface %u", iface); |
| 1109 |
|
| 1110 |
return (LIBUSB_SUCCESS); |
| 1111 |
} |
| 1112 |
|
| 1113 |
int |
| 1114 |
sunos_release_interface(struct libusb_device_handle *handle, uint8_t iface) |
| 1115 |
{ |
| 1116 |
sunos_dev_handle_priv_t *hpriv = usbi_get_device_handle_priv(handle); |
| 1117 |
|
| 1118 |
usbi_dbg(HANDLE_CTX(handle), "iface %u", iface); |
| 1119 |
|
| 1120 |
/* XXX: can we release it? */ |
| 1121 |
hpriv->altsetting[iface] = 0; |
| 1122 |
|
| 1123 |
return (LIBUSB_SUCCESS); |
| 1124 |
} |
| 1125 |
|
| 1126 |
int |
| 1127 |
sunos_set_interface_altsetting(struct libusb_device_handle *handle, uint8_t iface, |
| 1128 |
uint8_t altsetting) |
| 1129 |
{ |
| 1130 |
sunos_dev_priv_t *dpriv = usbi_get_device_priv(handle->dev); |
| 1131 |
sunos_dev_handle_priv_t *hpriv = usbi_get_device_handle_priv(handle); |
| 1132 |
|
| 1133 |
usbi_dbg(HANDLE_CTX(handle), "iface %u, setting %u", iface, altsetting); |
| 1134 |
|
| 1135 |
if (dpriv->ugenpath == NULL) |
| 1136 |
return (LIBUSB_ERROR_NOT_FOUND); |
| 1137 |
|
| 1138 |
/* XXX: can we switch altsetting? */ |
| 1139 |
hpriv->altsetting[iface] = altsetting; |
| 1140 |
|
| 1141 |
return (LIBUSB_SUCCESS); |
| 1142 |
} |
| 1143 |
|
| 1144 |
static void |
| 1145 |
usb_dump_data(const void *data, size_t size) |
| 1146 |
{ |
| 1147 |
const uint8_t *p = data; |
| 1148 |
size_t i; |
| 1149 |
|
| 1150 |
if (getenv("LIBUSB_DEBUG") == NULL) { |
| 1151 |
return; |
| 1152 |
} |
| 1153 |
|
| 1154 |
(void) fprintf(stderr, "data dump:"); |
| 1155 |
for (i = 0; i < size; i++) { |
| 1156 |
if (i % 16 == 0) { |
| 1157 |
(void) fprintf(stderr, "\n%08zx ", i); |
| 1158 |
} |
| 1159 |
(void) fprintf(stderr, "%02x ", p[i]); |
| 1160 |
} |
| 1161 |
(void) fprintf(stderr, "\n"); |
| 1162 |
} |
| 1163 |
|
| 1164 |
static void |
| 1165 |
sunos_async_callback(union sigval arg) |
| 1166 |
{ |
| 1167 |
struct sunos_transfer_priv *tpriv = |
| 1168 |
(struct sunos_transfer_priv *)arg.sival_ptr; |
| 1169 |
struct libusb_transfer *xfer = tpriv->transfer; |
| 1170 |
struct aiocb *aiocb = &tpriv->aiocb; |
| 1171 |
int ret; |
| 1172 |
sunos_dev_handle_priv_t *hpriv; |
| 1173 |
uint8_t ep; |
| 1174 |
libusb_device_handle *dev_handle; |
| 1175 |
|
| 1176 |
dev_handle = xfer->dev_handle; |
| 1177 |
|
| 1178 |
/* libusb can forcibly interrupt transfer in do_close() */ |
| 1179 |
if (dev_handle != NULL) { |
| 1180 |
hpriv = usbi_get_device_handle_priv(dev_handle); |
| 1181 |
ep = sunos_usb_ep_index(xfer->endpoint); |
| 1182 |
|
| 1183 |
ret = aio_error(aiocb); |
| 1184 |
if (ret != 0) { |
| 1185 |
xfer->status = sunos_usb_get_status(TRANSFER_CTX(xfer), hpriv->eps[ep].statfd); |
| 1186 |
} else { |
| 1187 |
xfer->actual_length = |
| 1188 |
LIBUSB_TRANSFER_TO_USBI_TRANSFER(xfer)->transferred = |
| 1189 |
aio_return(aiocb); |
| 1190 |
} |
| 1191 |
|
| 1192 |
usb_dump_data(xfer->buffer, xfer->actual_length); |
| 1193 |
|
| 1194 |
usbi_dbg(TRANSFER_CTX(xfer), "ret=%d, len=%d, actual_len=%d", ret, xfer->length, |
| 1195 |
xfer->actual_length); |
| 1196 |
|
| 1197 |
/* async notification */ |
| 1198 |
usbi_signal_transfer_completion(LIBUSB_TRANSFER_TO_USBI_TRANSFER(xfer)); |
| 1199 |
} |
| 1200 |
} |
| 1201 |
|
| 1202 |
static int |
| 1203 |
sunos_do_async_io(struct libusb_transfer *transfer) |
| 1204 |
{ |
| 1205 |
int ret = -1; |
| 1206 |
struct aiocb *aiocb; |
| 1207 |
sunos_dev_handle_priv_t *hpriv; |
| 1208 |
uint8_t ep; |
| 1209 |
struct sunos_transfer_priv *tpriv; |
| 1210 |
|
| 1211 |
usbi_dbg(TRANSFER_CTX(transfer), " "); |
| 1212 |
|
| 1213 |
tpriv = usbi_get_transfer_priv(LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)); |
| 1214 |
hpriv = usbi_get_device_handle_priv(transfer->dev_handle); |
| 1215 |
ep = sunos_usb_ep_index(transfer->endpoint); |
| 1216 |
|
| 1217 |
tpriv->transfer = transfer; |
| 1218 |
aiocb = &tpriv->aiocb; |
| 1219 |
bzero(aiocb, sizeof(*aiocb)); |
| 1220 |
aiocb->aio_fildes = hpriv->eps[ep].datafd; |
| 1221 |
aiocb->aio_buf = transfer->buffer; |
| 1222 |
aiocb->aio_nbytes = transfer->length; |
| 1223 |
aiocb->aio_lio_opcode = |
| 1224 |
((transfer->endpoint & LIBUSB_ENDPOINT_DIR_MASK) == |
| 1225 |
LIBUSB_ENDPOINT_IN) ? LIO_READ:LIO_WRITE; |
| 1226 |
aiocb->aio_sigevent.sigev_notify = SIGEV_THREAD; |
| 1227 |
aiocb->aio_sigevent.sigev_value.sival_ptr = tpriv; |
| 1228 |
aiocb->aio_sigevent.sigev_notify_function = sunos_async_callback; |
| 1229 |
|
| 1230 |
if (aiocb->aio_lio_opcode == LIO_READ) { |
| 1231 |
ret = aio_read(aiocb); |
| 1232 |
} else { |
| 1233 |
ret = aio_write(aiocb); |
| 1234 |
} |
| 1235 |
|
| 1236 |
return (ret); |
| 1237 |
} |
| 1238 |
|
| 1239 |
/* return the number of bytes read/written */ |
| 1240 |
static ssize_t |
| 1241 |
usb_do_io(struct libusb_context *ctx, int fd, int stat_fd, void *data, size_t size, int flag, int *status) |
| 1242 |
{ |
| 1243 |
int error; |
| 1244 |
ssize_t ret = -1; |
| 1245 |
|
| 1246 |
usbi_dbg(ctx, "usb_do_io(): datafd=%d statfd=%d size=0x%zx flag=%s", |
| 1247 |
fd, stat_fd, size, flag? "WRITE":"READ"); |
| 1248 |
|
| 1249 |
switch (flag) { |
| 1250 |
case READ: |
| 1251 |
errno = 0; |
| 1252 |
ret = read(fd, data, size); |
| 1253 |
usb_dump_data(data, size); |
| 1254 |
break; |
| 1255 |
case WRITE: |
| 1256 |
usb_dump_data(data, size); |
| 1257 |
errno = 0; |
| 1258 |
ret = write(fd, data, size); |
| 1259 |
break; |
| 1260 |
} |
| 1261 |
|
| 1262 |
usbi_dbg(ctx, "usb_do_io(): amount=%zd", ret); |
| 1263 |
|
| 1264 |
if (ret < 0) { |
| 1265 |
int save_errno = errno; |
| 1266 |
|
| 1267 |
usbi_dbg(ctx, "TID=%x io %s errno %d (%s)", pthread_self(), |
| 1268 |
flag?"WRITE":"READ", errno, strerror(errno)); |
| 1269 |
|
| 1270 |
/* sunos_usb_get_status will do a read and overwrite errno */ |
| 1271 |
error = sunos_usb_get_status(ctx, stat_fd); |
| 1272 |
usbi_dbg(ctx, "io status=%d errno %d (%s)", error, |
| 1273 |
save_errno, strerror(save_errno)); |
| 1274 |
|
| 1275 |
if (status) { |
| 1276 |
*status = save_errno; |
| 1277 |
} |
| 1278 |
|
| 1279 |
return (save_errno); |
| 1280 |
|
| 1281 |
} else if (status) { |
| 1282 |
*status = 0; |
| 1283 |
} |
| 1284 |
|
| 1285 |
return (ret); |
| 1286 |
} |
| 1287 |
|
| 1288 |
static int |
| 1289 |
solaris_submit_ctrl_on_default(struct libusb_transfer *transfer) |
| 1290 |
{ |
| 1291 |
ssize_t ret = -1, setup_ret; |
| 1292 |
int status; |
| 1293 |
sunos_dev_handle_priv_t *hpriv; |
| 1294 |
struct libusb_device_handle *hdl = transfer->dev_handle; |
| 1295 |
uint16_t wLength; |
| 1296 |
uint8_t *data = transfer->buffer; |
| 1297 |
|
| 1298 |
hpriv = usbi_get_device_handle_priv(hdl); |
| 1299 |
wLength = transfer->length - LIBUSB_CONTROL_SETUP_SIZE; |
| 1300 |
|
| 1301 |
if (hpriv->eps[0].datafd == -1) { |
| 1302 |
usbi_dbg(TRANSFER_CTX(transfer), "ep0 not opened"); |
| 1303 |
|
| 1304 |
return (LIBUSB_ERROR_NOT_FOUND); |
| 1305 |
} |
| 1306 |
|
| 1307 |
if ((data[0] & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN) { |
| 1308 |
usbi_dbg(TRANSFER_CTX(transfer), "IN request"); |
| 1309 |
ret = usb_do_io(TRANSFER_CTX(transfer), hpriv->eps[0].datafd, |
| 1310 |
hpriv->eps[0].statfd, data, LIBUSB_CONTROL_SETUP_SIZE, |
| 1311 |
WRITE, &status); |
| 1312 |
} else { |
| 1313 |
usbi_dbg(TRANSFER_CTX(transfer), "OUT request"); |
| 1314 |
ret = usb_do_io(TRANSFER_CTX(transfer), hpriv->eps[0].datafd, hpriv->eps[0].statfd, |
| 1315 |
transfer->buffer, transfer->length, WRITE, |
| 1316 |
(int *)&transfer->status); |
| 1317 |
} |
| 1318 |
|
| 1319 |
setup_ret = ret; |
| 1320 |
if (ret < (ssize_t)LIBUSB_CONTROL_SETUP_SIZE) { |
| 1321 |
usbi_dbg(TRANSFER_CTX(transfer), "error sending control msg: %zd", ret); |
| 1322 |
|
| 1323 |
return (LIBUSB_ERROR_IO); |
| 1324 |
} |
| 1325 |
|
| 1326 |
ret = transfer->length - LIBUSB_CONTROL_SETUP_SIZE; |
| 1327 |
|
| 1328 |
/* Read the remaining bytes for IN request */ |
| 1329 |
if ((wLength) && ((data[0] & LIBUSB_ENDPOINT_DIR_MASK) == |
| 1330 |
LIBUSB_ENDPOINT_IN)) { |
| 1331 |
usbi_dbg(TRANSFER_CTX(transfer), "DATA: %d", transfer->length - (int)setup_ret); |
| 1332 |
ret = usb_do_io(TRANSFER_CTX(transfer), hpriv->eps[0].datafd, |
| 1333 |
hpriv->eps[0].statfd, |
| 1334 |
transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE, |
| 1335 |
wLength, READ, (int *)&transfer->status); |
| 1336 |
} |
| 1337 |
|
| 1338 |
if (ret >= 0) { |
| 1339 |
LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)->transferred = ret; |
| 1340 |
} |
| 1341 |
usbi_dbg(TRANSFER_CTX(transfer), "Done: ctrl data bytes %zd", ret); |
| 1342 |
|
| 1343 |
/** |
| 1344 |
* Sync transfer handling. |
| 1345 |
* We should release transfer lock here and later get it back |
| 1346 |
* as usbi_handle_transfer_completion() takes its own transfer lock. |
| 1347 |
*/ |
| 1348 |
usbi_mutex_unlock(&LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)->lock); |
| 1349 |
ret = usbi_handle_transfer_completion(LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer), |
| 1350 |
transfer->status); |
| 1351 |
usbi_mutex_lock(&LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)->lock); |
| 1352 |
|
| 1353 |
return (ret); |
| 1354 |
} |
| 1355 |
|
| 1356 |
int |
| 1357 |
sunos_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint) |
| 1358 |
{ |
| 1359 |
int ret; |
| 1360 |
|
| 1361 |
usbi_dbg(HANDLE_CTX(handle), "endpoint=0x%02x", endpoint); |
| 1362 |
|
| 1363 |
ret = libusb_control_transfer(handle, LIBUSB_ENDPOINT_OUT | |
| 1364 |
LIBUSB_RECIPIENT_ENDPOINT | LIBUSB_REQUEST_TYPE_STANDARD, |
| 1365 |
LIBUSB_REQUEST_CLEAR_FEATURE, 0, endpoint, NULL, 0, 1000); |
| 1366 |
|
| 1367 |
usbi_dbg(HANDLE_CTX(handle), "ret=%d", ret); |
| 1368 |
|
| 1369 |
return (ret); |
| 1370 |
} |
| 1371 |
|
| 1372 |
void |
| 1373 |
sunos_destroy_device(struct libusb_device *dev) |
| 1374 |
{ |
| 1375 |
sunos_dev_priv_t *dpriv = usbi_get_device_priv(dev); |
| 1376 |
|
| 1377 |
usbi_dbg(DEVICE_CTX(dev), "destroy everything"); |
| 1378 |
free(dpriv->raw_cfgdescr); |
| 1379 |
free(dpriv->ugenpath); |
| 1380 |
free(dpriv->phypath); |
| 1381 |
} |
| 1382 |
|
| 1383 |
int |
| 1384 |
sunos_submit_transfer(struct usbi_transfer *itransfer) |
| 1385 |
{ |
| 1386 |
struct libusb_transfer *transfer; |
| 1387 |
struct libusb_device_handle *hdl; |
| 1388 |
int err = 0; |
| 1389 |
|
| 1390 |
transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 1391 |
hdl = transfer->dev_handle; |
| 1392 |
|
| 1393 |
err = sunos_check_device_and_status_open(hdl, |
| 1394 |
transfer->endpoint, transfer->type); |
| 1395 |
if (err != 0) { |
| 1396 |
return (_errno_to_libusb(err)); |
| 1397 |
} |
| 1398 |
|
| 1399 |
switch (transfer->type) { |
| 1400 |
case LIBUSB_TRANSFER_TYPE_CONTROL: |
| 1401 |
/* sync transfer */ |
| 1402 |
usbi_dbg(ITRANSFER_CTX(itransfer), "CTRL transfer: %d", transfer->length); |
| 1403 |
err = solaris_submit_ctrl_on_default(transfer); |
| 1404 |
break; |
| 1405 |
|
| 1406 |
case LIBUSB_TRANSFER_TYPE_BULK: |
| 1407 |
/* fallthru */ |
| 1408 |
case LIBUSB_TRANSFER_TYPE_INTERRUPT: |
| 1409 |
if (transfer->type == LIBUSB_TRANSFER_TYPE_BULK) |
| 1410 |
usbi_dbg(ITRANSFER_CTX(itransfer), "BULK transfer: %d", transfer->length); |
| 1411 |
else |
| 1412 |
usbi_dbg(ITRANSFER_CTX(itransfer), "INTR transfer: %d", transfer->length); |
| 1413 |
err = sunos_do_async_io(transfer); |
| 1414 |
break; |
| 1415 |
|
| 1416 |
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: |
| 1417 |
/* Isochronous/Stream is not supported */ |
| 1418 |
|
| 1419 |
/* fallthru */ |
| 1420 |
case LIBUSB_TRANSFER_TYPE_BULK_STREAM: |
| 1421 |
if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) |
| 1422 |
usbi_dbg(ITRANSFER_CTX(itransfer), "ISOC transfer: %d", transfer->length); |
| 1423 |
else |
| 1424 |
usbi_dbg(ITRANSFER_CTX(itransfer), "BULK STREAM transfer: %d", transfer->length); |
| 1425 |
err = LIBUSB_ERROR_NOT_SUPPORTED; |
| 1426 |
break; |
| 1427 |
} |
| 1428 |
|
| 1429 |
return (err); |
| 1430 |
} |
| 1431 |
|
| 1432 |
int |
| 1433 |
sunos_cancel_transfer(struct usbi_transfer *itransfer) |
| 1434 |
{ |
| 1435 |
sunos_xfer_priv_t *tpriv; |
| 1436 |
sunos_dev_handle_priv_t *hpriv; |
| 1437 |
struct libusb_transfer *transfer; |
| 1438 |
struct aiocb *aiocb; |
| 1439 |
uint8_t ep; |
| 1440 |
int ret; |
| 1441 |
|
| 1442 |
tpriv = usbi_get_transfer_priv(itransfer); |
| 1443 |
aiocb = &tpriv->aiocb; |
| 1444 |
transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 1445 |
hpriv = usbi_get_device_handle_priv(transfer->dev_handle); |
| 1446 |
ep = sunos_usb_ep_index(transfer->endpoint); |
| 1447 |
|
| 1448 |
ret = aio_cancel(hpriv->eps[ep].datafd, aiocb); |
| 1449 |
|
| 1450 |
usbi_dbg(ITRANSFER_CTX(itransfer), "aio->fd=%d fd=%d ret = %d, %s", aiocb->aio_fildes, |
| 1451 |
hpriv->eps[ep].datafd, ret, (ret == AIO_CANCELED)? |
| 1452 |
strerror(0):strerror(errno)); |
| 1453 |
|
| 1454 |
if (ret != AIO_CANCELED) { |
| 1455 |
ret = _errno_to_libusb(errno); |
| 1456 |
} else { |
| 1457 |
/* |
| 1458 |
* we don't need to call usbi_handle_transfer_cancellation(), |
| 1459 |
* because we'll handle everything in sunos_async_callback. |
| 1460 |
*/ |
| 1461 |
ret = LIBUSB_SUCCESS; |
| 1462 |
} |
| 1463 |
|
| 1464 |
return (ret); |
| 1465 |
} |
| 1466 |
|
| 1467 |
int |
| 1468 |
sunos_handle_transfer_completion(struct usbi_transfer *itransfer) |
| 1469 |
{ |
| 1470 |
return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED); |
| 1471 |
} |
| 1472 |
|
| 1473 |
int |
| 1474 |
_errno_to_libusb(int err) |
| 1475 |
{ |
| 1476 |
usbi_dbg(NULL, "error: %s (%d)", strerror(err), err); |
| 1477 |
|
| 1478 |
switch (err) { |
| 1479 |
case EIO: |
| 1480 |
return (LIBUSB_ERROR_IO); |
| 1481 |
case EACCES: |
| 1482 |
return (LIBUSB_ERROR_ACCESS); |
| 1483 |
case ENOENT: |
| 1484 |
return (LIBUSB_ERROR_NO_DEVICE); |
| 1485 |
case ENOMEM: |
| 1486 |
return (LIBUSB_ERROR_NO_MEM); |
| 1487 |
case ETIMEDOUT: |
| 1488 |
return (LIBUSB_ERROR_TIMEOUT); |
| 1489 |
} |
| 1490 |
|
| 1491 |
return (LIBUSB_ERROR_OTHER); |
| 1492 |
} |
| 1493 |
|
| 1494 |
/* |
| 1495 |
* sunos_usb_get_status: |
| 1496 |
* gets status of endpoint |
| 1497 |
* |
| 1498 |
* Returns: ugen's last cmd status |
| 1499 |
*/ |
| 1500 |
static int |
| 1501 |
sunos_usb_get_status(struct libusb_context *ctx, int fd) |
| 1502 |
{ |
| 1503 |
int status; |
| 1504 |
ssize_t ret; |
| 1505 |
|
| 1506 |
usbi_dbg(ctx, "sunos_usb_get_status(): fd=%d", fd); |
| 1507 |
|
| 1508 |
ret = read(fd, &status, sizeof(status)); |
| 1509 |
if (ret == sizeof(status)) { |
| 1510 |
switch (status) { |
| 1511 |
case USB_LC_STAT_NOERROR: |
| 1512 |
usbi_dbg(ctx, "No Error"); |
| 1513 |
break; |
| 1514 |
case USB_LC_STAT_CRC: |
| 1515 |
usbi_dbg(ctx, "CRC Timeout Detected\n"); |
| 1516 |
break; |
| 1517 |
case USB_LC_STAT_BITSTUFFING: |
| 1518 |
usbi_dbg(ctx, "Bit Stuffing Violation\n"); |
| 1519 |
break; |
| 1520 |
case USB_LC_STAT_DATA_TOGGLE_MM: |
| 1521 |
usbi_dbg(ctx, "Data Toggle Mismatch\n"); |
| 1522 |
break; |
| 1523 |
case USB_LC_STAT_STALL: |
| 1524 |
usbi_dbg(ctx, "End Point Stalled\n"); |
| 1525 |
break; |
| 1526 |
case USB_LC_STAT_DEV_NOT_RESP: |
| 1527 |
usbi_dbg(ctx, "Device is Not Responding\n"); |
| 1528 |
break; |
| 1529 |
case USB_LC_STAT_PID_CHECKFAILURE: |
| 1530 |
usbi_dbg(ctx, "PID Check Failure\n"); |
| 1531 |
break; |
| 1532 |
case USB_LC_STAT_UNEXP_PID: |
| 1533 |
usbi_dbg(ctx, "Unexpected PID\n"); |
| 1534 |
break; |
| 1535 |
case USB_LC_STAT_DATA_OVERRUN: |
| 1536 |
usbi_dbg(ctx, "Data Exceeded Size\n"); |
| 1537 |
break; |
| 1538 |
case USB_LC_STAT_DATA_UNDERRUN: |
| 1539 |
usbi_dbg(ctx, "Less data received\n"); |
| 1540 |
break; |
| 1541 |
case USB_LC_STAT_BUFFER_OVERRUN: |
| 1542 |
usbi_dbg(ctx, "Buffer Size Exceeded\n"); |
| 1543 |
break; |
| 1544 |
case USB_LC_STAT_BUFFER_UNDERRUN: |
| 1545 |
usbi_dbg(ctx, "Buffer Underrun\n"); |
| 1546 |
break; |
| 1547 |
case USB_LC_STAT_TIMEOUT: |
| 1548 |
usbi_dbg(ctx, "Command Timed Out\n"); |
| 1549 |
break; |
| 1550 |
case USB_LC_STAT_NOT_ACCESSED: |
| 1551 |
usbi_dbg(ctx, "Not Accessed by h/w\n"); |
| 1552 |
break; |
| 1553 |
case USB_LC_STAT_UNSPECIFIED_ERR: |
| 1554 |
usbi_dbg(ctx, "Unspecified Error\n"); |
| 1555 |
break; |
| 1556 |
case USB_LC_STAT_NO_BANDWIDTH: |
| 1557 |
usbi_dbg(ctx, "No Bandwidth\n"); |
| 1558 |
break; |
| 1559 |
case USB_LC_STAT_HW_ERR: |
| 1560 |
usbi_dbg(ctx, "Host Controller h/w Error\n"); |
| 1561 |
break; |
| 1562 |
case USB_LC_STAT_SUSPENDED: |
| 1563 |
usbi_dbg(ctx, "Device was Suspended\n"); |
| 1564 |
break; |
| 1565 |
case USB_LC_STAT_DISCONNECTED: |
| 1566 |
usbi_dbg(ctx, "Device was Disconnected\n"); |
| 1567 |
break; |
| 1568 |
case USB_LC_STAT_INTR_BUF_FULL: |
| 1569 |
usbi_dbg(ctx, "Interrupt buffer was full\n"); |
| 1570 |
break; |
| 1571 |
case USB_LC_STAT_INVALID_REQ: |
| 1572 |
usbi_dbg(ctx, "Request was Invalid\n"); |
| 1573 |
break; |
| 1574 |
case USB_LC_STAT_INTERRUPTED: |
| 1575 |
usbi_dbg(ctx, "Request was Interrupted\n"); |
| 1576 |
break; |
| 1577 |
case USB_LC_STAT_NO_RESOURCES: |
| 1578 |
usbi_dbg(ctx, "No resources available for " |
| 1579 |
"request\n"); |
| 1580 |
break; |
| 1581 |
case USB_LC_STAT_INTR_POLLING_FAILED: |
| 1582 |
usbi_dbg(ctx, "Failed to Restart Poll"); |
| 1583 |
break; |
| 1584 |
default: |
| 1585 |
usbi_dbg(ctx, "Error Not Determined %d\n", |
| 1586 |
status); |
| 1587 |
break; |
| 1588 |
} |
| 1589 |
} else { |
| 1590 |
usbi_dbg(ctx, "read stat error: %s",strerror(errno)); |
| 1591 |
status = -1; |
| 1592 |
} |
| 1593 |
|
| 1594 |
return (status); |
| 1595 |
} |
| 1596 |
|
| 1597 |
const struct usbi_os_backend usbi_backend = { |
| 1598 |
.name = "Solaris", |
| 1599 |
.caps = 0, |
| 1600 |
.get_device_list = sunos_get_device_list, |
| 1601 |
.get_active_config_descriptor = sunos_get_active_config_descriptor, |
| 1602 |
.get_config_descriptor = sunos_get_config_descriptor, |
| 1603 |
.open = sunos_open, |
| 1604 |
.close = sunos_close, |
| 1605 |
.get_configuration = sunos_get_configuration, |
| 1606 |
.set_configuration = sunos_set_configuration, |
| 1607 |
.claim_interface = sunos_claim_interface, |
| 1608 |
.release_interface = sunos_release_interface, |
| 1609 |
.set_interface_altsetting = sunos_set_interface_altsetting, |
| 1610 |
.clear_halt = sunos_clear_halt, |
| 1611 |
.kernel_driver_active = sunos_kernel_driver_active, |
| 1612 |
.detach_kernel_driver = sunos_detach_kernel_driver, |
| 1613 |
.attach_kernel_driver = sunos_attach_kernel_driver, |
| 1614 |
.destroy_device = sunos_destroy_device, |
| 1615 |
.submit_transfer = sunos_submit_transfer, |
| 1616 |
.cancel_transfer = sunos_cancel_transfer, |
| 1617 |
.handle_transfer_completion = sunos_handle_transfer_completion, |
| 1618 |
.device_priv_size = sizeof(sunos_dev_priv_t), |
| 1619 |
.device_handle_priv_size = sizeof(sunos_dev_handle_priv_t), |
| 1620 |
.transfer_priv_size = sizeof(sunos_xfer_priv_t), |
| 1621 |
}; |