3 * Copyright (c) 2016, Oracle and/or its affiliates.
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.
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.
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
23 #include <sys/types.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>
42 #include "sunos_usb.h"
44 #define UPDATEDRV_PATH "/usr/sbin/update_drv"
45 #define UPDATEDRV "update_drv"
47 #define DEFAULT_LISTSIZE 6
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 *,
64 static int sunos_get_config_descriptor(struct libusb_device *, uint8_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 *,
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);
83 static int sunos_get_link(di_devlink_t devlink, void *arg)
85 walk_link_t *larg = (walk_link_t *)arg;
90 char *content = (char *)di_devlink_content(devlink);
91 char *start = strstr(content, "/devices/");
92 start += strlen("/devices");
93 usbi_dbg(NULL, "%s", start);
95 /* line content must have minor node */
97 strncmp(start, larg->path, larg->len) != 0 ||
98 start[larg->len] != ':')
99 return (DI_WALK_CONTINUE);
102 p = di_devlink_path(devlink);
104 usbi_dbg(NULL, "%s", q);
106 *(larg->linkpp) = strndup(p, strlen(p) - strlen(q));
108 return (DI_WALK_TERMINATE);
112 static int sunos_physpath_to_devlink(
113 const char *node_path, const char *match, char **link_path)
116 di_devlink_handle_t hdl;
119 larg.linkpp = link_path;
120 if ((hdl = di_devlink_init(NULL, 0)) == NULL) {
121 usbi_dbg(NULL, "di_devlink_init failure");
125 larg.len = strlen(node_path);
126 larg.path = (char *)node_path;
128 (void) di_devlink_walk(hdl, match, NULL, DI_PRIMARY_LINK,
129 (void *)&larg, sunos_get_link);
131 (void) di_devlink_fini(&hdl);
133 if (*link_path == NULL) {
134 usbi_dbg(NULL, "there is no devlink for this path");
142 sunos_usb_ioctl(struct libusb_device *dev, int cmd)
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;
154 dpriv = usbi_get_device_priv(dev);
155 phypath = dpriv->phypath;
157 end = strrchr(phypath, '/');
160 hubpath = strndup(phypath, end - phypath);
164 end = strrchr(hubpath, '@');
170 usbi_dbg(DEVICE_CTX(dev), "unitaddr: %s", end);
172 nvlist_alloc(&nvlist, NV_UNIQUE_NAME_TYPE, KM_NOSLEEP);
173 nvlist_add_int32(nvlist, "port", dev->port_number);
175 snprintf(path_arg, sizeof(path_arg), "/devices%s:hubd", hubpath);
176 usbi_dbg(DEVICE_CTX(dev), "ioctl hub path: %s", path_arg);
178 fd = open(path_arg, O_RDONLY);
180 usbi_err(DEVICE_CTX(dev), "open failed: errno %d (%s)", errno, strerror(errno));
186 memset(&iocdata, 0, sizeof(iocdata));
187 memset(&devctl_ap_state, 0, sizeof(devctl_ap_state));
189 nvlist_pack(nvlist, (char **)&iocdata.nvl_user, &iocdata.nvl_usersz, NV_ENCODE_NATIVE, 0);
191 iocdata.cmd = DEVCTL_AP_GETSTATE;
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);
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));
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);
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));
216 free(iocdata.nvl_user);
224 sunos_kernel_driver_active(struct libusb_device_handle *dev_handle, uint8_t interface)
226 sunos_dev_priv_t *dpriv = usbi_get_device_priv(dev_handle->dev);
230 usbi_dbg(HANDLE_CTX(dev_handle), "%s", dpriv->ugenpath);
232 return (dpriv->ugenpath == NULL);
238 static int _errno_to_libusb(int);
239 static int sunos_usb_get_status(struct libusb_context *ctx, int fd);
241 static string_list_t *
242 sunos_new_string_list(void)
246 list = calloc(1, sizeof(string_list_t));
249 list->string = calloc(DEFAULT_LISTSIZE, sizeof(char *));
250 if (list->string == NULL) {
255 list->listsize = DEFAULT_LISTSIZE;
261 sunos_append_to_string_list(string_list_t *list, const char *arg)
263 char *str = strdup(arg);
268 if ((list->nargs + 1) == list->listsize) { /* +1 is for NULL */
269 char **tmp = realloc(list->string,
270 sizeof(char *) * (list->listsize + 1));
276 list->string[list->listsize++] = NULL;
278 list->string[list->nargs++] = str;
284 sunos_free_string_list(string_list_t *list)
288 for (i = 0; i < list->nargs; i++) {
289 free(list->string[i]);
297 sunos_build_argv_list(string_list_t *list)
299 return (list->string);
304 sunos_exec_command(struct libusb_context *ctx, const char *path,
313 argv_list = sunos_build_argv_list(list);
314 if (argv_list == NULL)
320 execv(path, argv_list);
322 } else if (pid > 0) {
325 waitstat = waitpid(pid, &status, 0);
326 } while ((waitstat == -1 && errno == EINTR) ||
327 (waitstat == 0 && !WIFEXITED(status) && !WIFSIGNALED(status)));
330 if (WIFEXITED(status))
331 exit_status = WEXITSTATUS(status);
333 exit_status = WTERMSIG(status);
335 usbi_err(ctx, "waitpid failed: errno %d (%s)", errno, strerror(errno));
340 usbi_err(ctx, "fork failed: errno %d (%s)", errno, strerror(errno));
344 return (exit_status);
348 sunos_detach_kernel_driver(struct libusb_device_handle *dev_handle,
349 uint8_t interface_number)
351 struct libusb_context *ctx = HANDLE_CTX(dev_handle);
353 char path_arg[PATH_MAX];
354 sunos_dev_priv_t *dpriv;
357 UNUSED(interface_number);
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);
363 list = sunos_new_string_list();
365 return (LIBUSB_ERROR_NO_MEM);
367 /* attach ugen driver */
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");
375 sunos_free_string_list(list);
376 return (LIBUSB_ERROR_NO_MEM);
379 r = sunos_exec_command(ctx, UPDATEDRV_PATH, list);
380 sunos_free_string_list(list);
382 return (LIBUSB_ERROR_OTHER);
384 /* reconfigure the driver node */
386 r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_DISCONNECT);
387 r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_CONFIGURE);
389 usbi_warn(HANDLE_CTX(dev_handle), "one or more ioctls failed");
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);
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);
401 return sunos_usb_open_ep0(usbi_get_device_handle_priv(dev_handle), dpriv);
405 sunos_attach_kernel_driver(struct libusb_device_handle *dev_handle,
406 uint8_t interface_number)
408 struct libusb_context *ctx = HANDLE_CTX(dev_handle);
410 char path_arg[PATH_MAX];
411 sunos_dev_priv_t *dpriv;
414 UNUSED(interface_number);
416 /* we open the dev in detach driver, so we need close it first. */
417 sunos_close(dev_handle);
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);
423 list = sunos_new_string_list();
425 return (LIBUSB_ERROR_NO_MEM);
427 /* detach ugen driver */
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");
435 sunos_free_string_list(list);
436 return (LIBUSB_ERROR_NO_MEM);
439 r = sunos_exec_command(ctx, UPDATEDRV_PATH, list);
440 sunos_free_string_list(list);
442 return (LIBUSB_ERROR_OTHER);
444 /* reconfigure the driver node */
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);
450 usbi_warn(HANDLE_CTX(dev_handle), "one or more ioctls failed");
456 sunos_fill_in_dev_info(di_node_t node, struct libusb_device *dev)
459 int *i, n, *addr, *port_prop;
462 sunos_dev_priv_t *dpriv = usbi_get_device_priv(dev);
463 char match_str[PATH_MAX];
465 /* Device descriptors */
466 proplen = di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
467 "usb-dev-descriptor", &rdata);
469 return (LIBUSB_ERROR_IO);
471 bcopy(rdata, &dev->device_descriptor, LIBUSB_DT_DEVICE_SIZE);
473 /* Raw configuration descriptors */
474 proplen = di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
475 "usb-raw-cfg-descriptors", &rdata);
477 usbi_dbg(DEVICE_CTX(dev), "can't find raw config descriptors");
479 return (LIBUSB_ERROR_IO);
481 dpriv->raw_cfgdescr = calloc(1, proplen);
482 if (dpriv->raw_cfgdescr == NULL) {
483 return (LIBUSB_ERROR_NO_MEM);
485 bcopy(rdata, dpriv->raw_cfgdescr, proplen);
486 dpriv->cfgvalue = ((struct libusb_config_descriptor *)
487 rdata)->bConfigurationValue;
490 n = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &port_prop);
492 if ((n != 1) || (*port_prop <= 0)) {
493 return (LIBUSB_ERROR_IO);
495 dev->port_number = *port_prop;
497 /* device physical path */
498 phypath = di_devfs_path(node);
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);
509 free(dpriv->raw_cfgdescr);
511 return (LIBUSB_ERROR_IO);
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");
519 dev->device_address = *addr;
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;
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);
537 return (LIBUSB_SUCCESS);
541 sunos_add_devices(di_devlink_t link, void *arg)
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;
549 struct libusb_device *dev;
550 sunos_dev_priv_t *devpriv;
554 uint8_t bus_number = 0;
555 uint32_t * regbuf = NULL;
560 nargs = (struct node_args *)largs->nargs;
561 myself = largs->myself;
564 * Construct session ID.
565 * session ID = dev_addr | hub addr |parent hub addr|...|root hub bdf
566 * 8 bits 8bits 8 bits 16bits
568 if (myself == DI_NODE_NIL)
569 return (DI_WALK_CONTINUE);
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);
581 /* dn is the root hub node */
582 n = di_prop_lookup_ints(DDI_DEV_T_ANY, dn, "reg", (int **)®buf);
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);
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);
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)
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");
607 sid = (session_id << 8) | (addr_prop[0] & 0xff) ;
608 usbi_dbg(NULL, "session id %" PRIX64, sid);
610 dev = usbi_get_device_by_session_id(nargs->ctx, sid);
612 dev = usbi_alloc_device(nargs->ctx, sid);
614 usbi_dbg(NULL, "can't alloc device");
617 devpriv = usbi_get_device_priv(dev);
618 dev->bus_number = bus_number;
620 if (sunos_fill_in_dev_info(dn, dev) != LIBUSB_SUCCESS) {
621 libusb_unref_device(dev);
622 usbi_dbg(NULL, "get information fail");
625 if (usbi_sanitize_device(dev) < 0) {
626 libusb_unref_device(dev);
627 usbi_dbg(NULL, "sanatize failed: ");
628 return (DI_WALK_TERMINATE);
631 devpriv = usbi_get_device_priv(dev);
632 usbi_dbg(NULL, "Dev %s exists", devpriv->ugenpath);
635 if (discovered_devs_append(*(nargs->discdevs), dev) == NULL) {
636 usbi_dbg(NULL, "cannot append device");
640 * we alloc and hence ref this dev. We don't need to ref it
641 * hereafter. Front end or app should take care of their ref.
643 libusb_unref_device(dev);
645 usbi_dbg(NULL, "Device %s %s id=0x%" PRIx64 ", devcount:%" PRIuPTR
647 devpriv->ugenpath, di_devfs_path(dn), (uint64_t)sid,
648 (*nargs->discdevs)->len, bdf);
651 return (DI_WALK_CONTINUE);
655 sunos_walk_minor_node_link(di_node_t node, void *args)
657 di_minor_t minor = DI_MINOR_NIL;
659 struct devlink_cbarg arg;
660 struct node_args *nargs = (struct node_args *)args;
661 di_devlink_handle_t devlink_hdl = nargs->dlink_hdl;
663 /* walk each minor to find usb devices */
664 while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
665 minor_path = di_devfs_minor_path(minor);
669 (void) di_devlink_walk(devlink_hdl,
670 "^usb/hub[0-9]+", minor_path,
671 DI_PRIMARY_LINK, (void *)&arg, sunos_add_devices);
672 di_devfs_path_free(minor_path);
675 /* switch to a different node */
676 nargs->last_ugenpath = NULL;
678 return (DI_WALK_CONTINUE);
682 sunos_get_device_list(struct libusb_context * ctx,
683 struct discovered_devs **discdevs)
686 struct node_args args;
687 di_devlink_handle_t devlink_hdl;
690 args.discdevs = discdevs;
691 args.last_ugenpath = NULL;
692 if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
693 usbi_dbg(ctx, "di_int() failed: errno %d (%s)", errno, strerror(errno));
694 return (LIBUSB_ERROR_IO);
697 if ((devlink_hdl = di_devlink_init(NULL, 0)) == NULL) {
699 usbi_dbg(ctx, "di_devlink_init() failed: errno %d (%s)", errno, strerror(errno));
701 return (LIBUSB_ERROR_IO);
703 args.dlink_hdl = devlink_hdl;
705 /* walk each node to find USB devices */
706 if (di_walk_node(root_node, DI_WALK_SIBFIRST, &args,
707 sunos_walk_minor_node_link) == -1) {
708 usbi_dbg(ctx, "di_walk_node() failed: errno %d (%s)", errno, strerror(errno));
711 return (LIBUSB_ERROR_IO);
715 di_devlink_fini(&devlink_hdl);
717 usbi_dbg(ctx, "%zu devices", (*discdevs)->len);
719 return ((*discdevs)->len);
723 sunos_usb_open_ep0(sunos_dev_handle_priv_t *hpriv, sunos_dev_priv_t *dpriv)
725 char filename[PATH_MAX + 1];
727 if (hpriv->eps[0].datafd > 0) {
728 return (LIBUSB_SUCCESS);
730 snprintf(filename, PATH_MAX, "%s/cntrl0", dpriv->ugenpath);
732 usbi_dbg(NULL, "opening %s", filename);
733 hpriv->eps[0].datafd = open(filename, O_RDWR);
734 if (hpriv->eps[0].datafd < 0) {
735 return(_errno_to_libusb(errno));
738 snprintf(filename, PATH_MAX, "%s/cntrl0stat", dpriv->ugenpath);
739 hpriv->eps[0].statfd = open(filename, O_RDONLY);
740 if (hpriv->eps[0].statfd < 0) {
741 close(hpriv->eps[0].datafd);
742 hpriv->eps[0].datafd = -1;
744 return(_errno_to_libusb(errno));
747 return (LIBUSB_SUCCESS);
751 sunos_usb_close_all_eps(sunos_dev_handle_priv_t *hdev)
756 for (i = 1; i < USB_MAXENDPOINTS; i++) {
757 if (hdev->eps[i].datafd != -1) {
758 (void) close(hdev->eps[i].datafd);
759 hdev->eps[i].datafd = -1;
761 if (hdev->eps[i].statfd != -1) {
762 (void) close(hdev->eps[i].statfd);
763 hdev->eps[i].statfd = -1;
769 sunos_usb_close_ep0(sunos_dev_handle_priv_t *hdev)
771 if (hdev->eps[0].datafd >= 0) {
772 close(hdev->eps[0].datafd);
773 close(hdev->eps[0].statfd);
774 hdev->eps[0].datafd = -1;
775 hdev->eps[0].statfd = -1;
780 sunos_usb_ep_index(uint8_t ep_addr)
782 return ((ep_addr & LIBUSB_ENDPOINT_ADDRESS_MASK) +
783 ((ep_addr & LIBUSB_ENDPOINT_DIR_MASK) ? 16 : 0));
787 sunos_find_interface(struct libusb_device_handle *hdev,
788 uint8_t endpoint, uint8_t *interface)
790 struct libusb_config_descriptor *config;
794 r = libusb_get_active_config_descriptor(hdev->dev, &config);
796 return (LIBUSB_ERROR_INVALID_PARAM);
799 for (iface_idx = 0; iface_idx < config->bNumInterfaces; iface_idx++) {
800 const struct libusb_interface *iface =
801 &config->interface[iface_idx];
804 for (altsetting_idx = 0; altsetting_idx < iface->num_altsetting;
806 const struct libusb_interface_descriptor *altsetting =
807 &iface->altsetting[altsetting_idx];
810 for (ep_idx = 0; ep_idx < altsetting->bNumEndpoints;
812 const struct libusb_endpoint_descriptor *ep =
813 &altsetting->endpoint[ep_idx];
814 if (ep->bEndpointAddress == endpoint) {
815 *interface = iface_idx;
816 libusb_free_config_descriptor(config);
818 return (LIBUSB_SUCCESS);
823 libusb_free_config_descriptor(config);
825 return (LIBUSB_ERROR_INVALID_PARAM);
829 sunos_check_device_and_status_open(struct libusb_device_handle *hdl,
830 uint8_t ep_addr, int ep_type)
832 char filename[PATH_MAX + 1], statfilename[PATH_MAX + 1];
833 char cfg_num[16], alt_num[16];
834 int fd, fdstat, mode;
837 sunos_dev_handle_priv_t *hpriv;
839 usbi_dbg(HANDLE_CTX(hdl), "open ep 0x%02x", ep_addr);
840 hpriv = usbi_get_device_handle_priv(hdl);
841 ep_index = sunos_usb_ep_index(ep_addr);
842 /* ep already opened */
843 if ((hpriv->eps[ep_index].datafd > 0) &&
844 (hpriv->eps[ep_index].statfd > 0)) {
845 usbi_dbg(HANDLE_CTX(hdl), "ep 0x%02x already opened, return success",
851 if (sunos_find_interface(hdl, ep_addr, &ifc) < 0) {
852 usbi_dbg(HANDLE_CTX(hdl), "can't find interface for endpoint 0x%02x",
858 /* create filename */
859 if (hpriv->config_index > 0) {
860 (void) snprintf(cfg_num, sizeof(cfg_num), "cfg%d",
861 hpriv->config_index + 1);
863 bzero(cfg_num, sizeof(cfg_num));
866 if (hpriv->altsetting[ifc] > 0) {
867 (void) snprintf(alt_num, sizeof(alt_num), ".%d",
868 hpriv->altsetting[ifc]);
870 bzero(alt_num, sizeof(alt_num));
873 (void) snprintf(filename, PATH_MAX, "%s/%sif%d%s%s%d",
874 hpriv->dpriv->ugenpath, cfg_num, ifc, alt_num,
875 (ep_addr & LIBUSB_ENDPOINT_DIR_MASK) ? "in" :
876 "out", (ep_addr & LIBUSB_ENDPOINT_ADDRESS_MASK));
877 (void) snprintf(statfilename, PATH_MAX, "%sstat", filename);
880 * In case configuration has been switched, the xfer endpoint needs
881 * to be opened before the status endpoint, due to a ugen issue.
882 * However, to enable the one transfer mode for an Interrupt-In pipe,
883 * the status endpoint needs to be opened before the xfer endpoint.
884 * So, open the xfer mode first and close it immediately
885 * as a workaround. This will handle the configuration switch.
886 * Then, open the status endpoint. If for an Interrupt-in pipe,
887 * write the USB_EP_INTR_ONE_XFER control to the status endpoint
888 * to enable the one transfer mode. Then, re-open the xfer mode.
890 if (ep_type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
892 } else if (ep_addr & LIBUSB_ENDPOINT_IN) {
897 /* Open the xfer endpoint first */
898 if ((fd = open(filename, mode)) == -1) {
899 usbi_dbg(HANDLE_CTX(hdl), "can't open %s: errno %d (%s)", filename, errno,
904 /* And immediately close the xfer endpoint */
908 * Open the status endpoint.
909 * If for an Interrupt-IN pipe, need to enable the one transfer mode
910 * by writing USB_EP_INTR_ONE_XFER control to the status endpoint
911 * before opening the xfer endpoint
913 if ((ep_type == LIBUSB_TRANSFER_TYPE_INTERRUPT) &&
914 (ep_addr & LIBUSB_ENDPOINT_IN)) {
915 char control = USB_EP_INTR_ONE_XFER;
918 /* Open the status endpoint with RDWR */
919 if ((fdstat = open(statfilename, O_RDWR)) == -1) {
920 usbi_dbg(HANDLE_CTX(hdl), "can't open %s RDWR: errno %d (%s)",
921 statfilename, errno, strerror(errno));
925 count = write(fdstat, &control, sizeof(control));
927 /* this should have worked */
928 usbi_dbg(HANDLE_CTX(hdl), "can't write to %s: errno %d (%s)",
929 statfilename, errno, strerror(errno));
930 (void) close(fdstat);
936 if ((fdstat = open(statfilename, O_RDONLY)) == -1) {
937 usbi_dbg(HANDLE_CTX(hdl), "can't open %s: errno %d (%s)", statfilename, errno,
944 /* Re-open the xfer endpoint */
945 if ((fd = open(filename, mode)) == -1) {
946 usbi_dbg(HANDLE_CTX(hdl), "can't open %s: errno %d (%s)", filename, errno,
948 (void) close(fdstat);
953 hpriv->eps[ep_index].datafd = fd;
954 hpriv->eps[ep_index].statfd = fdstat;
955 usbi_dbg(HANDLE_CTX(hdl), "ep=0x%02x datafd=%d, statfd=%d", ep_addr, fd, fdstat);
961 sunos_open(struct libusb_device_handle *handle)
963 sunos_dev_handle_priv_t *hpriv;
964 sunos_dev_priv_t *dpriv;
968 hpriv = usbi_get_device_handle_priv(handle);
969 dpriv = usbi_get_device_priv(handle->dev);
970 hpriv->dpriv = dpriv;
972 /* set all file descriptors to "closed" */
973 for (i = 0; i < USB_MAXENDPOINTS; i++) {
974 hpriv->eps[i].datafd = -1;
975 hpriv->eps[i].statfd = -1;
978 if (sunos_kernel_driver_active(handle, 0)) {
979 /* pretend we can open the device */
980 return (LIBUSB_SUCCESS);
983 if ((ret = sunos_usb_open_ep0(hpriv, dpriv)) != LIBUSB_SUCCESS) {
984 usbi_dbg(HANDLE_CTX(handle), "fail: %d", ret);
988 return (LIBUSB_SUCCESS);
992 sunos_close(struct libusb_device_handle *handle)
994 sunos_dev_handle_priv_t *hpriv;
996 usbi_dbg(HANDLE_CTX(handle), " ");
998 hpriv = usbi_get_device_handle_priv(handle);
1000 sunos_usb_close_all_eps(hpriv);
1001 sunos_usb_close_ep0(hpriv);
1005 sunos_get_active_config_descriptor(struct libusb_device *dev,
1006 void *buf, size_t len)
1008 sunos_dev_priv_t *dpriv = usbi_get_device_priv(dev);
1009 struct libusb_config_descriptor *cfg;
1015 * Keep raw configuration descriptors updated, in case config
1016 * has ever been changed through setCfg.
1018 if ((node = di_init(dpriv->phypath, DINFOCPYALL)) == DI_NODE_NIL) {
1019 usbi_dbg(DEVICE_CTX(dev), "di_int() failed: errno %d (%s)", errno,
1021 return (LIBUSB_ERROR_IO);
1023 proplen = di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
1024 "usb-raw-cfg-descriptors", &rdata);
1026 usbi_dbg(DEVICE_CTX(dev), "can't find raw config descriptors");
1028 return (LIBUSB_ERROR_IO);
1030 dpriv->raw_cfgdescr = realloc(dpriv->raw_cfgdescr, proplen);
1031 if (dpriv->raw_cfgdescr == NULL) {
1032 return (LIBUSB_ERROR_NO_MEM);
1034 bcopy(rdata, dpriv->raw_cfgdescr, proplen);
1035 dpriv->cfgvalue = ((struct libusb_config_descriptor *)
1036 rdata)->bConfigurationValue;
1040 cfg = (struct libusb_config_descriptor *)dpriv->raw_cfgdescr;
1041 len = MIN(len, libusb_le16_to_cpu(cfg->wTotalLength));
1042 memcpy(buf, dpriv->raw_cfgdescr, len);
1043 usbi_dbg(DEVICE_CTX(dev), "path:%s len %zu", dpriv->phypath, len);
1049 sunos_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
1050 void *buf, size_t len)
1054 return(sunos_get_active_config_descriptor(dev, buf, len));
1058 sunos_get_configuration(struct libusb_device_handle *handle, uint8_t *config)
1060 sunos_dev_priv_t *dpriv = usbi_get_device_priv(handle->dev);
1062 *config = dpriv->cfgvalue;
1064 usbi_dbg(HANDLE_CTX(handle), "bConfigurationValue %u", *config);
1066 return (LIBUSB_SUCCESS);
1070 sunos_set_configuration(struct libusb_device_handle *handle, int config)
1072 sunos_dev_priv_t *dpriv = usbi_get_device_priv(handle->dev);
1073 sunos_dev_handle_priv_t *hpriv;
1075 usbi_dbg(HANDLE_CTX(handle), "bConfigurationValue %d", config);
1076 hpriv = usbi_get_device_handle_priv(handle);
1078 if (dpriv->ugenpath == NULL)
1079 return (LIBUSB_ERROR_NOT_SUPPORTED);
1082 return (LIBUSB_ERROR_NOT_SUPPORTED);
1084 dpriv->cfgvalue = config;
1085 hpriv->config_index = config - 1;
1087 return (LIBUSB_SUCCESS);
1091 sunos_claim_interface(struct libusb_device_handle *handle, uint8_t iface)
1095 usbi_dbg(HANDLE_CTX(handle), "iface %u", iface);
1097 return (LIBUSB_SUCCESS);
1101 sunos_release_interface(struct libusb_device_handle *handle, uint8_t iface)
1103 sunos_dev_handle_priv_t *hpriv = usbi_get_device_handle_priv(handle);
1105 usbi_dbg(HANDLE_CTX(handle), "iface %u", iface);
1107 /* XXX: can we release it? */
1108 hpriv->altsetting[iface] = 0;
1110 return (LIBUSB_SUCCESS);
1114 sunos_set_interface_altsetting(struct libusb_device_handle *handle, uint8_t iface,
1117 sunos_dev_priv_t *dpriv = usbi_get_device_priv(handle->dev);
1118 sunos_dev_handle_priv_t *hpriv = usbi_get_device_handle_priv(handle);
1120 usbi_dbg(HANDLE_CTX(handle), "iface %u, setting %u", iface, altsetting);
1122 if (dpriv->ugenpath == NULL)
1123 return (LIBUSB_ERROR_NOT_FOUND);
1125 /* XXX: can we switch altsetting? */
1126 hpriv->altsetting[iface] = altsetting;
1128 return (LIBUSB_SUCCESS);
1132 usb_dump_data(const void *data, size_t size)
1134 const uint8_t *p = data;
1137 if (getenv("LIBUSB_DEBUG") == NULL) {
1141 (void) fprintf(stderr, "data dump:");
1142 for (i = 0; i < size; i++) {
1144 (void) fprintf(stderr, "\n%08zx ", i);
1146 (void) fprintf(stderr, "%02x ", p[i]);
1148 (void) fprintf(stderr, "\n");
1152 sunos_async_callback(union sigval arg)
1154 struct sunos_transfer_priv *tpriv =
1155 (struct sunos_transfer_priv *)arg.sival_ptr;
1156 struct libusb_transfer *xfer = tpriv->transfer;
1157 struct aiocb *aiocb = &tpriv->aiocb;
1159 sunos_dev_handle_priv_t *hpriv;
1161 libusb_device_handle *dev_handle;
1163 dev_handle = xfer->dev_handle;
1165 /* libusb can forcibly interrupt transfer in do_close() */
1166 if (dev_handle != NULL) {
1167 hpriv = usbi_get_device_handle_priv(dev_handle);
1168 ep = sunos_usb_ep_index(xfer->endpoint);
1170 ret = aio_error(aiocb);
1172 xfer->status = sunos_usb_get_status(TRANSFER_CTX(xfer), hpriv->eps[ep].statfd);
1174 xfer->actual_length =
1175 LIBUSB_TRANSFER_TO_USBI_TRANSFER(xfer)->transferred =
1179 usb_dump_data(xfer->buffer, xfer->actual_length);
1181 usbi_dbg(TRANSFER_CTX(xfer), "ret=%d, len=%d, actual_len=%d", ret, xfer->length,
1182 xfer->actual_length);
1184 /* async notification */
1185 usbi_signal_transfer_completion(LIBUSB_TRANSFER_TO_USBI_TRANSFER(xfer));
1190 sunos_do_async_io(struct libusb_transfer *transfer)
1193 struct aiocb *aiocb;
1194 sunos_dev_handle_priv_t *hpriv;
1196 struct sunos_transfer_priv *tpriv;
1198 usbi_dbg(TRANSFER_CTX(transfer), " ");
1200 tpriv = usbi_get_transfer_priv(LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer));
1201 hpriv = usbi_get_device_handle_priv(transfer->dev_handle);
1202 ep = sunos_usb_ep_index(transfer->endpoint);
1204 tpriv->transfer = transfer;
1205 aiocb = &tpriv->aiocb;
1206 bzero(aiocb, sizeof(*aiocb));
1207 aiocb->aio_fildes = hpriv->eps[ep].datafd;
1208 aiocb->aio_buf = transfer->buffer;
1209 aiocb->aio_nbytes = transfer->length;
1210 aiocb->aio_lio_opcode =
1211 ((transfer->endpoint & LIBUSB_ENDPOINT_DIR_MASK) ==
1212 LIBUSB_ENDPOINT_IN) ? LIO_READ:LIO_WRITE;
1213 aiocb->aio_sigevent.sigev_notify = SIGEV_THREAD;
1214 aiocb->aio_sigevent.sigev_value.sival_ptr = tpriv;
1215 aiocb->aio_sigevent.sigev_notify_function = sunos_async_callback;
1217 if (aiocb->aio_lio_opcode == LIO_READ) {
1218 ret = aio_read(aiocb);
1220 ret = aio_write(aiocb);
1226 /* return the number of bytes read/written */
1228 usb_do_io(struct libusb_context *ctx, int fd, int stat_fd, void *data, size_t size, int flag, int *status)
1233 usbi_dbg(ctx, "usb_do_io(): datafd=%d statfd=%d size=0x%zx flag=%s",
1234 fd, stat_fd, size, flag? "WRITE":"READ");
1239 ret = read(fd, data, size);
1240 usb_dump_data(data, size);
1243 usb_dump_data(data, size);
1245 ret = write(fd, data, size);
1249 usbi_dbg(ctx, "usb_do_io(): amount=%zd", ret);
1252 int save_errno = errno;
1254 usbi_dbg(ctx, "TID=%x io %s errno %d (%s)", pthread_self(),
1255 flag?"WRITE":"READ", errno, strerror(errno));
1257 /* sunos_usb_get_status will do a read and overwrite errno */
1258 error = sunos_usb_get_status(ctx, stat_fd);
1259 usbi_dbg(ctx, "io status=%d errno %d (%s)", error,
1260 save_errno, strerror(save_errno));
1263 *status = save_errno;
1266 return (save_errno);
1268 } else if (status) {
1276 solaris_submit_ctrl_on_default(struct libusb_transfer *transfer)
1278 ssize_t ret = -1, setup_ret;
1280 sunos_dev_handle_priv_t *hpriv;
1281 struct libusb_device_handle *hdl = transfer->dev_handle;
1283 uint8_t *data = transfer->buffer;
1285 hpriv = usbi_get_device_handle_priv(hdl);
1286 wLength = transfer->length - LIBUSB_CONTROL_SETUP_SIZE;
1288 if (hpriv->eps[0].datafd == -1) {
1289 usbi_dbg(TRANSFER_CTX(transfer), "ep0 not opened");
1291 return (LIBUSB_ERROR_NOT_FOUND);
1294 if ((data[0] & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN) {
1295 usbi_dbg(TRANSFER_CTX(transfer), "IN request");
1296 ret = usb_do_io(TRANSFER_CTX(transfer), hpriv->eps[0].datafd,
1297 hpriv->eps[0].statfd, data, LIBUSB_CONTROL_SETUP_SIZE,
1300 usbi_dbg(TRANSFER_CTX(transfer), "OUT request");
1301 ret = usb_do_io(TRANSFER_CTX(transfer), hpriv->eps[0].datafd, hpriv->eps[0].statfd,
1302 transfer->buffer, transfer->length, WRITE,
1303 (int *)&transfer->status);
1307 if (ret < (ssize_t)LIBUSB_CONTROL_SETUP_SIZE) {
1308 usbi_dbg(TRANSFER_CTX(transfer), "error sending control msg: %zd", ret);
1310 return (LIBUSB_ERROR_IO);
1313 ret = transfer->length - LIBUSB_CONTROL_SETUP_SIZE;
1315 /* Read the remaining bytes for IN request */
1316 if ((wLength) && ((data[0] & LIBUSB_ENDPOINT_DIR_MASK) ==
1317 LIBUSB_ENDPOINT_IN)) {
1318 usbi_dbg(TRANSFER_CTX(transfer), "DATA: %d", transfer->length - (int)setup_ret);
1319 ret = usb_do_io(TRANSFER_CTX(transfer), hpriv->eps[0].datafd,
1320 hpriv->eps[0].statfd,
1321 transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE,
1322 wLength, READ, (int *)&transfer->status);
1326 LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)->transferred = ret;
1328 usbi_dbg(TRANSFER_CTX(transfer), "Done: ctrl data bytes %zd", ret);
1331 * Sync transfer handling.
1332 * We should release transfer lock here and later get it back
1333 * as usbi_handle_transfer_completion() takes its own transfer lock.
1335 usbi_mutex_unlock(&LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)->lock);
1336 ret = usbi_handle_transfer_completion(LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer),
1338 usbi_mutex_lock(&LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)->lock);
1344 sunos_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
1348 usbi_dbg(HANDLE_CTX(handle), "endpoint=0x%02x", endpoint);
1350 ret = libusb_control_transfer(handle, LIBUSB_ENDPOINT_OUT |
1351 LIBUSB_RECIPIENT_ENDPOINT | LIBUSB_REQUEST_TYPE_STANDARD,
1352 LIBUSB_REQUEST_CLEAR_FEATURE, 0, endpoint, NULL, 0, 1000);
1354 usbi_dbg(HANDLE_CTX(handle), "ret=%d", ret);
1360 sunos_destroy_device(struct libusb_device *dev)
1362 sunos_dev_priv_t *dpriv = usbi_get_device_priv(dev);
1364 usbi_dbg(DEVICE_CTX(dev), "destroy everything");
1365 free(dpriv->raw_cfgdescr);
1366 free(dpriv->ugenpath);
1367 free(dpriv->phypath);
1371 sunos_submit_transfer(struct usbi_transfer *itransfer)
1373 struct libusb_transfer *transfer;
1374 struct libusb_device_handle *hdl;
1377 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1378 hdl = transfer->dev_handle;
1380 err = sunos_check_device_and_status_open(hdl,
1381 transfer->endpoint, transfer->type);
1384 return (_errno_to_libusb(err));
1387 switch (transfer->type) {
1388 case LIBUSB_TRANSFER_TYPE_CONTROL:
1390 usbi_dbg(ITRANSFER_CTX(itransfer), "CTRL transfer: %d", transfer->length);
1391 err = solaris_submit_ctrl_on_default(transfer);
1394 case LIBUSB_TRANSFER_TYPE_BULK:
1396 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1397 if (transfer->type == LIBUSB_TRANSFER_TYPE_BULK)
1398 usbi_dbg(ITRANSFER_CTX(itransfer), "BULK transfer: %d", transfer->length);
1400 usbi_dbg(ITRANSFER_CTX(itransfer), "INTR transfer: %d", transfer->length);
1401 err = sunos_do_async_io(transfer);
1404 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1405 /* Isochronous/Stream is not supported */
1408 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
1409 if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS)
1410 usbi_dbg(ITRANSFER_CTX(itransfer), "ISOC transfer: %d", transfer->length);
1412 usbi_dbg(ITRANSFER_CTX(itransfer), "BULK STREAM transfer: %d", transfer->length);
1413 err = LIBUSB_ERROR_NOT_SUPPORTED;
1421 sunos_cancel_transfer(struct usbi_transfer *itransfer)
1423 sunos_xfer_priv_t *tpriv;
1424 sunos_dev_handle_priv_t *hpriv;
1425 struct libusb_transfer *transfer;
1426 struct aiocb *aiocb;
1430 tpriv = usbi_get_transfer_priv(itransfer);
1431 aiocb = &tpriv->aiocb;
1432 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1433 hpriv = usbi_get_device_handle_priv(transfer->dev_handle);
1434 ep = sunos_usb_ep_index(transfer->endpoint);
1436 ret = aio_cancel(hpriv->eps[ep].datafd, aiocb);
1438 usbi_dbg(ITRANSFER_CTX(itransfer), "aio->fd=%d fd=%d ret = %d, %s", aiocb->aio_fildes,
1439 hpriv->eps[ep].datafd, ret, (ret == AIO_CANCELED)?
1440 strerror(0):strerror(errno));
1442 if (ret != AIO_CANCELED) {
1443 ret = _errno_to_libusb(errno);
1446 * we don't need to call usbi_handle_transfer_cancellation(),
1447 * because we'll handle everything in sunos_async_callback.
1449 ret = LIBUSB_SUCCESS;
1456 sunos_handle_transfer_completion(struct usbi_transfer *itransfer)
1458 return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED);
1462 _errno_to_libusb(int err)
1464 usbi_dbg(NULL, "error: %s (%d)", strerror(err), err);
1468 return (LIBUSB_ERROR_IO);
1470 return (LIBUSB_ERROR_ACCESS);
1472 return (LIBUSB_ERROR_NO_DEVICE);
1474 return (LIBUSB_ERROR_NO_MEM);
1476 return (LIBUSB_ERROR_TIMEOUT);
1479 return (LIBUSB_ERROR_OTHER);
1483 * sunos_usb_get_status:
1484 * gets status of endpoint
1486 * Returns: ugen's last cmd status
1489 sunos_usb_get_status(struct libusb_context *ctx, int fd)
1494 usbi_dbg(ctx, "sunos_usb_get_status(): fd=%d", fd);
1496 ret = read(fd, &status, sizeof(status));
1497 if (ret == sizeof(status)) {
1499 case USB_LC_STAT_NOERROR:
1500 usbi_dbg(ctx, "No Error");
1502 case USB_LC_STAT_CRC:
1503 usbi_dbg(ctx, "CRC Timeout Detected\n");
1505 case USB_LC_STAT_BITSTUFFING:
1506 usbi_dbg(ctx, "Bit Stuffing Violation\n");
1508 case USB_LC_STAT_DATA_TOGGLE_MM:
1509 usbi_dbg(ctx, "Data Toggle Mismatch\n");
1511 case USB_LC_STAT_STALL:
1512 usbi_dbg(ctx, "End Point Stalled\n");
1514 case USB_LC_STAT_DEV_NOT_RESP:
1515 usbi_dbg(ctx, "Device is Not Responding\n");
1517 case USB_LC_STAT_PID_CHECKFAILURE:
1518 usbi_dbg(ctx, "PID Check Failure\n");
1520 case USB_LC_STAT_UNEXP_PID:
1521 usbi_dbg(ctx, "Unexpected PID\n");
1523 case USB_LC_STAT_DATA_OVERRUN:
1524 usbi_dbg(ctx, "Data Exceeded Size\n");
1526 case USB_LC_STAT_DATA_UNDERRUN:
1527 usbi_dbg(ctx, "Less data received\n");
1529 case USB_LC_STAT_BUFFER_OVERRUN:
1530 usbi_dbg(ctx, "Buffer Size Exceeded\n");
1532 case USB_LC_STAT_BUFFER_UNDERRUN:
1533 usbi_dbg(ctx, "Buffer Underrun\n");
1535 case USB_LC_STAT_TIMEOUT:
1536 usbi_dbg(ctx, "Command Timed Out\n");
1538 case USB_LC_STAT_NOT_ACCESSED:
1539 usbi_dbg(ctx, "Not Accessed by h/w\n");
1541 case USB_LC_STAT_UNSPECIFIED_ERR:
1542 usbi_dbg(ctx, "Unspecified Error\n");
1544 case USB_LC_STAT_NO_BANDWIDTH:
1545 usbi_dbg(ctx, "No Bandwidth\n");
1547 case USB_LC_STAT_HW_ERR:
1548 usbi_dbg(ctx, "Host Controller h/w Error\n");
1550 case USB_LC_STAT_SUSPENDED:
1551 usbi_dbg(ctx, "Device was Suspended\n");
1553 case USB_LC_STAT_DISCONNECTED:
1554 usbi_dbg(ctx, "Device was Disconnected\n");
1556 case USB_LC_STAT_INTR_BUF_FULL:
1557 usbi_dbg(ctx, "Interrupt buffer was full\n");
1559 case USB_LC_STAT_INVALID_REQ:
1560 usbi_dbg(ctx, "Request was Invalid\n");
1562 case USB_LC_STAT_INTERRUPTED:
1563 usbi_dbg(ctx, "Request was Interrupted\n");
1565 case USB_LC_STAT_NO_RESOURCES:
1566 usbi_dbg(ctx, "No resources available for "
1569 case USB_LC_STAT_INTR_POLLING_FAILED:
1570 usbi_dbg(ctx, "Failed to Restart Poll");
1573 usbi_dbg(ctx, "Error Not Determined %d\n",
1578 usbi_dbg(ctx, "read stat error: %s",strerror(errno));
1585 const struct usbi_os_backend usbi_backend = {
1588 .get_device_list = sunos_get_device_list,
1589 .get_active_config_descriptor = sunos_get_active_config_descriptor,
1590 .get_config_descriptor = sunos_get_config_descriptor,
1592 .close = sunos_close,
1593 .get_configuration = sunos_get_configuration,
1594 .set_configuration = sunos_set_configuration,
1595 .claim_interface = sunos_claim_interface,
1596 .release_interface = sunos_release_interface,
1597 .set_interface_altsetting = sunos_set_interface_altsetting,
1598 .clear_halt = sunos_clear_halt,
1599 .kernel_driver_active = sunos_kernel_driver_active,
1600 .detach_kernel_driver = sunos_detach_kernel_driver,
1601 .attach_kernel_driver = sunos_attach_kernel_driver,
1602 .destroy_device = sunos_destroy_device,
1603 .submit_transfer = sunos_submit_transfer,
1604 .cancel_transfer = sunos_cancel_transfer,
1605 .handle_transfer_completion = sunos_handle_transfer_completion,
1606 .device_priv_size = sizeof(sunos_dev_priv_t),
1607 .device_handle_priv_size = sizeof(sunos_dev_handle_priv_t),
1608 .transfer_priv_size = sizeof(sunos_xfer_priv_t),