xusb_libusb.c 21.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
/*
 * Written by Oron Peled <[email protected]>
 * Copyright (C) 2008, Xorcom
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <syslog.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <usb.h>
#include <xtalk/debug.h>
#include <xtalk/xusb.h>
#include <autoconfig.h>
#include "xusb_common.h"

#define	DBG_MASK	0x01
#define	TIMEOUT	500
#define	MAX_RETRIES	10

#define	EP_IS_IN(ep)	(((ep) & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN)
#define	EP_IS_OUT(ep)	(((ep) & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT)

struct libusb_implementation {
	struct usb_device	*dev;
	usb_dev_handle		*handle;
	struct usb_config_descriptor	*config_desc;
};

static void xusb_init();

/*
 * USB handling
 */

static int get_usb_string(struct xusb_device *xusb_device, uint8_t item, char *buf)
{
	char	tmp[BUFSIZ];
	int	ret;

	if (!xusb_device->impl->handle) {
		ERR("%s: device closed\n", xusb_device->devpath_tail);
		return -ENXIO;
	}
	if (!item)
		return 0;
	ret = usb_get_string_simple(xusb_device->impl->handle, item, tmp, BUFSIZ);
	if (ret <= 0)
		return ret;
	return snprintf(buf, BUFSIZ, "%s", tmp);
}

static const struct usb_interface_descriptor *get_iface_descriptor(
	const struct xusb_device *xusb_device, int i)
{
	const struct usb_config_descriptor	*config_desc;
	const struct usb_interface		*interface;
	const struct usb_interface_descriptor	*iface_desc;
	int					num_altsetting;

	assert(xusb_device);
	assert(xusb_device->impl);
	config_desc = xusb_device->impl->config_desc;
	assert(config_desc);
	assert(config_desc->bNumInterfaces < XUSB_MAX_INTERFACES);
	if (i >= XUSB_MAX_INTERFACES)
		return NULL;
	interface = &config_desc->interface[i];
	assert(interface != NULL);
	iface_desc = interface->altsetting;
	num_altsetting = interface->num_altsetting;
	assert(num_altsetting != 0);
	assert(iface_desc != NULL);
	return iface_desc;
}

#define	GET_USB_STRING(xusb_device, from, item) \
		get_usb_string((xusb_device), (from)->item, (xusb_device)->item)

static int xusb_fill_strings(struct xusb_device *xusb_device, int interface_num)
{
	const struct usb_device_descriptor *dev_desc;
	const struct usb_interface_descriptor *iface_desc;
	struct xusb_iface *iface = xusb_device->interfaces[interface_num];
	int ret;

	dev_desc = &xusb_device->impl->dev->descriptor;
	assert(dev_desc);
	ret = GET_USB_STRING(xusb_device, dev_desc, iManufacturer);
	if (ret < 0) {
		XUSB_ERR(iface, "Failed reading iManufacturer string: %s\n",
			usb_strerror());
		return 0;
	}
	ret = GET_USB_STRING(xusb_device, dev_desc, iProduct);
	if (ret < 0) {
		XUSB_ERR(iface, "Failed reading iProduct string: %s\n",
			usb_strerror());
		return 0;
	}
	ret = GET_USB_STRING(xusb_device, dev_desc, iSerialNumber);
	if (ret < 0) {
		XUSB_ERR(iface, "Failed reading iSerialNumber string: %s\n",
			usb_strerror());
		return 0;
	}
	iface_desc = get_iface_descriptor(xusb_device, interface_num);
	if (!iface_desc) {
		XUSB_ERR(iface, "Could not get interface descriptor of device\n");
		return 0;
	}
	ret = GET_USB_STRING(xusb_device, iface_desc, iInterface);
	if (ret < 0) {
		XUSB_ERR(iface, "Failed reading iInterface string: %s\n",
			usb_strerror());
		return 0;
	}
	return 1;
}

static int xusb_open(struct xusb_device *xusb_device)
{
	int ret = 0;

	assert(xusb_device);
	DBG("%s\n", xusb_device->devpath_tail);
	if (xusb_device->impl->handle) {
		ERR("%s: already open\n", xusb_device->devpath_tail);
		ret = -EBUSY;
		goto out;
	}
	xusb_device->impl->handle = usb_open(xusb_device->impl->dev);
	if (!xusb_device->impl->handle) {
		ERR("%s: Failed to open usb device: %s\n",
			xusb_device->devpath_tail,
			usb_strerror());
		xusb_device->impl->handle = NULL;
		goto out;
	}
	return 1;
out:
	return ret;
}

void xusb_release(struct xusb_iface *iface)
{
	if (iface && iface->is_claimed) {
		usb_dev_handle *handle;
		int ret;

		assert(iface->xusb_device);
		handle = iface->xusb_device->impl->handle;
		XUSB_DBG(iface, "Releasing interface\n");
		if (!handle) {
			XUSB_ERR(iface, "device closed\n");
			iface->is_claimed = 0;
			return;
		}
		ret = usb_release_interface(handle, iface->interface_num);
		if (ret < 0)
			XUSB_ERR(iface, "Releasing interface: %s\n",
					usb_strerror());
		iface->is_claimed = 0;
	}
}

static int xusb_clear_halt(struct xusb_iface *xusb_iface)
{
	struct xusb_device *xusb_device;
	int ret = 0;
	int ep;

	xusb_device = xusb_iface->xusb_device;
	/*
	 * WE DO NOT CALL HALT for problematic devices:
	 * - It cause problem with our usb-dongle (cypress CY7C63803, interrupt driven)
	 */
	if (xusb_device->idVendor == 0xe4e4 && xusb_device->idProduct == 0x11a3) {
		XUSB_DBG(xusb_iface, "Skipping clear_halt()\n");
		goto out;
	}
	if (!xtalk_option_use_clear_halt()) {
		XUSB_DBG(xusb_iface, "Don't use clear_halt()\n");
		goto out;
	}
	ep = EP_OUT(xusb_iface);
	ret = usb_clear_halt(xusb_device->impl->handle, ep);
	if (ret < 0) {
		XUSB_ERR(xusb_iface, "Clearing output endpoint 0x%02X: %s\n",
			ep, usb_strerror());
		goto out;
	}
	ep = EP_IN(xusb_iface);
	ret = usb_clear_halt(xusb_device->impl->handle, ep);
	if (ret < 0) {
		XUSB_ERR(xusb_iface, "Clearing input endpoint 0x%02X: %s\n",
			ep, usb_strerror());
		goto out;
	}
out:
	return ret;
}

int xusb_claim(struct xusb_device *xusb_device, unsigned int interface_num,
	struct xusb_iface **xusb_iface)
{
	struct xusb_iface *iface = NULL;
	enum xusb_transfer_type	iface_tt = XUSB_TT_ILLEGAL;
	int ret = 0;

	*xusb_iface = NULL;
	assert(xusb_device);
	if (!xusb_device->impl->handle) {
		XUSB_ERR(iface, "device closed\n");
		ret = -ENXIO;
		goto failed;
	}
	if (interface_num >= XUSB_MAX_INTERFACES) {
		ERR("%s: interface number %d is too big\n",
			xusb_device->devpath_tail, interface_num);
		ret = -EINVAL;
		goto failed;
	}
	iface = xusb_device->interfaces[interface_num];
	if (!iface) {
		ERR("%s: No interface number %d\n",
			xusb_device->devpath_tail, interface_num);
		ret = -EINVAL;
		goto failed;
	}
	if (iface->is_claimed) {
		XUSB_ERR(iface, "Already claimed\n");
		ret = -EBUSY;
		goto failed;
	}
	ret = usb_claim_interface(xusb_device->impl->handle, iface->interface_num);
	if (ret < 0) {
		XUSB_ERR(iface, "usb_claim_interface: %s\n", usb_strerror());
		goto failed;
	}
	iface->is_claimed = 1;
	iface_tt = xusb_transfer_type(iface);
	if (iface_tt == XUSB_TT_ILLEGAL) {
		ret = -ENOTSUP;
		goto failed;
	}
	iface->transfer_type = iface_tt;
	ret = xusb_clear_halt(iface);
	if (ret < 0)
		goto failed;
	ret = xusb_flushread(iface);
	if (ret < 0) {
		XUSB_ERR(iface, "xusb_flushread failed: %s\n", usb_strerror());
		goto failed;
	}
	xusb_fill_strings(xusb_device, interface_num);
	XUSB_DBG(iface, "ID=%04X:%04X Manufacturer=[%s] Product=[%s] "
		"SerialNumber=[%s] Interface=[%s] TT=%s\n",
		xusb_device->idVendor,
		xusb_device->idProduct,
		xusb_device->iManufacturer,
		xusb_device->iProduct,
		xusb_device->iSerialNumber,
		iface->iInterface,
		xusb_tt_name(iface->transfer_type));
	*xusb_iface = iface;
	return 0;
failed:
	xusb_release(iface);
	return ret;
}

void xusb_destroy(struct xusb_device *xusb_device)
{
	if (xusb_device) {
		struct xusb_iface **piface;
		struct libusb_implementation *impl;

		for (piface = xusb_device->interfaces; *piface; piface++) {
			xusb_destroy_interface(*piface);
			*piface = NULL;
		}
		impl = xusb_device->impl;
		if (impl) {
			if (impl->handle) {
				xusb_close(xusb_device);
				impl->handle = NULL;
			}
		}
		DBG("%s: MEM: FREE device\n", xusb_device->devpath_tail);
		memset(xusb_device, 0, sizeof(*xusb_device));
		free(xusb_device);
		xusb_device = NULL;
	}
}

static int init_interfaces(struct xusb_device *xusb_device)
{
	const struct usb_config_descriptor *config_desc;
	const struct usb_interface_descriptor *iface_desc;
	struct xusb_iface *iface;
	int max_packet_size = 0;
	int packet_size;
	int if_idx;

	assert(xusb_device);
	assert(xusb_device->impl);
	config_desc = xusb_device->impl->config_desc;
	assert(config_desc);
	assert(config_desc->bNumInterfaces < XUSB_MAX_INTERFACES);
	for (if_idx = 0; if_idx < config_desc->bNumInterfaces; if_idx++) {
		int ep_idx;

		iface_desc = get_iface_descriptor(xusb_device, if_idx);
		if (iface_desc->bInterfaceNumber != if_idx) {
			ERR("%s: interface %d is number %d\n",
				xusb_device->devpath_tail,
				if_idx, iface_desc->bInterfaceNumber);
			return -EINVAL;
		}
		if (iface_desc->bNumEndpoints != 2) {
			ERR("%s: interface %d has %d endpoints\n",
				xusb_device->devpath_tail,
				if_idx, iface_desc->bNumEndpoints);
			return -EINVAL;
		}
		iface = calloc(sizeof(*iface), 1);
		if (!iface) {
			ERR("%s: interface %d -- out of memory\n",
				xusb_device->devpath_tail, if_idx);
			return -ENOMEM;
		}
		DBG("MEM: ALLOC interface: %p\n", iface);
		xusb_device->interfaces[if_idx] = iface;
		iface->xusb_device = xusb_device;
		iface->interface_num = iface_desc->bInterfaceNumber;

		/* Search Endpoints */
		iface->ep_in = 0;
		iface->ep_out = 0;
		for (ep_idx = 0; ep_idx < iface_desc->bNumEndpoints; ep_idx++) {
			int ep_num;

			ep_num = iface_desc->endpoint[ep_idx].bEndpointAddress;
			packet_size = iface_desc->endpoint[ep_idx].wMaxPacketSize;
			if (!max_packet_size || packet_size < max_packet_size)
				max_packet_size = packet_size;
			if (EP_IS_OUT(ep_num))
				iface->ep_out = ep_num;
			if (EP_IS_IN(ep_num))
				iface->ep_in = ep_num;
		}
		/* Validation */
		if (!iface->ep_out) {
			ERR("%s[%d]: Missing output endpoint\n",
				xusb_device->devpath_tail, if_idx);
			return -EINVAL;
		}
		if (!iface->ep_in) {
			ERR("%s[%d]: Missing input endpoint\n",
				xusb_device->devpath_tail, if_idx);
			return -EINVAL;
		}

		iface->is_claimed = 0;
		XUSB_DBG(iface, "ep_out=0x%X ep_in=0x%X packet_size=%d\n",
			iface->ep_out, iface->ep_in, max_packet_size);
	}
	if (xusb_device->packet_size < max_packet_size)
		xusb_device->packet_size = max_packet_size;
	xusb_device->is_usb2 = (xusb_device->packet_size == 512);
	return 0;
}

static struct xusb_device *xusb_new(struct usb_device *dev,
	const struct xusb_spec *spec,
	xusb_filter_t filterfunc,
	void *data)
{
	struct usb_device_descriptor	*dev_desc;
	int				ret;
	struct xusb_device		*xusb_device = NULL;

	xusb_device = calloc(sizeof(*xusb_device) + sizeof(struct libusb_implementation), 1);
	if (!xusb_device) {
		ERR("Out of memory");
		goto fail;
	}
	DBG("MEM: ALLOC device: %p\n", xusb_device);
	/* Fill xusb_device */
	xusb_device->impl = (void *)xusb_device + sizeof(*xusb_device);
	xusb_device->impl->dev = dev;
	xusb_device->spec = spec;
	/*
	 * Get information from the usb_device
	 */
	dev_desc = &dev->descriptor;
	if (!dev_desc) {
		ERR("usb device without a device descriptor\n");
		goto fail;
	}
	xusb_device->idVendor = dev_desc->idVendor;
	xusb_device->idProduct = dev_desc->idProduct;
	sscanf(dev->bus->dirname, "%d", &xusb_device->bus_num);
	sscanf(dev->filename, "%d", &xusb_device->device_num);
	snprintf(xusb_device->devpath_tail, PATH_MAX, "%03d/%03d",
		xusb_device->bus_num, xusb_device->device_num);
	if (!match_device(xusb_device, spec)) {
		DBG("[%04X:%04X] did not match\n",
			xusb_device->idVendor, xusb_device->idProduct);
		goto fail;
	}
	xusb_device->impl->config_desc = dev->config;
	if (!xusb_device->impl->config_desc) {
		ERR("usb device without a configuration descriptor\n");
		goto fail;
	}
	ret = init_interfaces(xusb_device);
	if (ret < 0) {
		ERR("%s: init_interfaces() failed (ret = %d)\n",
			xusb_device->devpath_tail, ret);
		goto fail;
	}
	if (!xusb_open(xusb_device)) {
		ERR("%s: Failed opening device: %04X:%04X\n",
			xusb_device->devpath_tail,
			xusb_device->idVendor,
			xusb_device->idProduct);
		goto fail;
	}
	DBG("%s: %04X:%04X\n",
		xusb_device->devpath_tail,
		xusb_device->idVendor,
		xusb_device->idProduct);
	return xusb_device;
fail:
	xusb_destroy(xusb_device);
	return NULL;
}

struct xusb_iface *xusb_find_iface(const char *devpath,
	int iface_num,
	int ep_out,
	int ep_in,
	struct xusb_spec *dummy_spec)
{
	return NULL;
}

struct xusb_device *xusb_find_bypath(const char *path)
{
	struct usb_bus *bus;
	struct usb_device *dev;
	char devpath_tail[PATH_MAX];
	struct xusb_spec *spec;

	DBG("path='%s'\n", path);
	spec = calloc(sizeof(*spec), 1);
	if (!spec) {
		ERR("Failed allocating spec\n");
		goto failed;
	}
	xusb_init();
	for (bus = usb_get_busses(); bus; bus = bus->next) {
		for (dev = bus->devices; dev; dev = dev->next) {
			struct usb_device_descriptor *dev_desc;
			struct xusb_device *xusb_device = NULL;

			dev_desc = &dev->descriptor;
			assert(dev_desc);
			DBG("usb:%s/%s: ID=%04X:%04X\n",
				dev->bus->dirname,
				dev->filename,
				dev_desc->idVendor,
				dev_desc->idProduct);
			snprintf(devpath_tail, PATH_MAX, "%3s/%3s",
				dev->bus->dirname, dev->filename);
			if (!match_devpath(path, devpath_tail))
				continue;
			DBG("Found: usb:%s/%s: ID=%04X:%04X\n",
				dev->bus->dirname,
				dev->filename,
				dev_desc->idVendor,
				dev_desc->idProduct);
			xusb_init_spec(spec, "<BYPATH>",
				dev_desc->idVendor, dev_desc->idProduct);
			xusb_device = xusb_new(dev, spec, NULL, NULL);
			if (!xusb_device) {
				ERR("Failed creating xusb for %s\n",
					devpath_tail);
				xusb_init_spec(spec, "<EMPTY>", 0, 0);
				continue;
			}
			return xusb_device;
		}
	}
failed:
	if (spec)
		free(spec);
	return NULL;
}

struct xlist_node *xusb_find_byproduct(const struct xusb_spec *specs,
		int numspecs, xusb_filter_t filterfunc, void *data)
{
	struct xlist_node	*xlist;
	struct usb_bus		*bus;
	struct usb_device	*dev;

	DBG("specs(%d)\n", numspecs);
	xlist = xlist_new(NULL);
	if (!xlist) {
		ERR("Failed allocation new xlist");
		goto failed;
	}
	xusb_init();
	for (bus = usb_get_busses(); bus; bus = bus->next) {
		for (dev = bus->devices; dev; dev = dev->next) {
			struct usb_device_descriptor	*dev_desc;
			struct xlist_node		*item;
			int				i;

			dev_desc = &dev->descriptor;
			assert(dev_desc);
			DBG("usb:%s/%s: ID=%04X:%04X\n",
				dev->bus->dirname,
				dev->filename,
				dev_desc->idVendor,
				dev_desc->idProduct);
			for (i = 0; i < numspecs; i++) {
				struct xusb_device	*xusb_device;
				const struct xusb_spec	*sp = &specs[i];

				xusb_device = xusb_new(dev, sp, filterfunc, data);
				if (!xusb_device)
					continue;
				if (filterfunc && !filterfunc(xusb_device, data)) {
					DBG("%s: %04X:%04X filtered out\n",
						xusb_device->devpath_tail,
						dev_desc->idVendor,
						dev_desc->idProduct);
					xusb_destroy(xusb_device);
					continue;
				}
				item = xlist_new(xusb_device);
				xlist_append_item(xlist, item);
				break;
			}
		}
	}
	xusb_list_dump(xlist);
	return xlist;
failed:
	if (xlist)
		xlist_destroy(xlist, NULL);
	return NULL;
}

void xusb_showinfo(const struct xusb_device *xusb_device)
{
	struct usb_device_descriptor	*dev_desc;
	struct usb_device		*dev;
	const struct xusb_iface **piface;

	assert(xusb_device);
	dev = xusb_device->impl->dev;
	assert(dev);
	dev_desc = &dev->descriptor;
	assert(dev_desc);
	if (verbose <= LOG_INFO) {
		INFO("%s: [%04X:%04X] [%s / %s / %s]\n",
			xusb_device->devpath_tail,
			dev_desc->idVendor,
			dev_desc->idProduct,
			xusb_device->iManufacturer,
			xusb_device->iProduct,
			xusb_device->iSerialNumber);
	} else {
		printf("USB    Bus/Device:    [%s/%s] (%s)\n",
			dev->bus->dirname,
			dev->filename,
			(xusb_device->impl->handle) ? "open" : "closed");
		printf("USB    Spec name:     [%s]\n", xusb_device->spec->name);
		printf("USB    iManufacturer: [%s]\n", xusb_device->iManufacturer);
		printf("USB    iProduct:      [%s]\n", xusb_device->iProduct);
		printf("USB    iSerialNumber: [%s]\n", xusb_device->iSerialNumber);
		piface = (const struct xusb_iface **)xusb_device->interfaces;
		for (; *piface; piface++) {
			printf("USB    Interface[%d]:  ep_out=0x%02X ep_in=0x%02X claimed=%d [%s]\n",
				(*piface)->interface_num,
				(*piface)->ep_out,
				(*piface)->ep_in,
				(*piface)->is_claimed,
				(*piface)->iInterface);
		}
	}
}

int xusb_close(struct xusb_device *xusb_device)
{
	if (xusb_device && xusb_device->impl && xusb_device->impl->handle) {
		assert(xusb_device->spec);
		assert(xusb_device->spec->name);
		DBG("%s: Closing device \"%s\"\n",
			xusb_device->devpath_tail,
			xusb_device->spec->name);
		usb_close(xusb_device->impl->handle);
		xusb_device->impl->handle = NULL;
	}
	return 0;
}

enum xusb_transfer_type xusb_transfer_type(const struct xusb_iface *iface)
{
	const struct xusb_device *xusb_device;
	const struct usb_interface_descriptor *iface_desc;
	const struct usb_endpoint_descriptor *ep;
	enum xusb_transfer_type ret = XUSB_TT_ILLEGAL;

	assert(iface);
	xusb_device = iface->xusb_device;
	assert(xusb_device);
	iface_desc = get_iface_descriptor(xusb_device, iface->interface_num);
	assert(iface_desc);
	ep = iface_desc->endpoint;
	assert(ep);
	switch (ep->bmAttributes) {
        case USB_ENDPOINT_TYPE_CONTROL:
        case USB_ENDPOINT_TYPE_ISOCHRONOUS:
		break;
        case USB_ENDPOINT_TYPE_BULK:
		ret = XUSB_TT_BULK;
		break;
        case USB_ENDPOINT_TYPE_INTERRUPT:
		ret = XUSB_TT_INTERRUPT;
		break;
	}
	return ret;
}

int xusb_send(struct xusb_iface *iface, const char *buf, int len, int timeout)
{
	struct xusb_device *xusb_device = iface->xusb_device;
	int ep_out = EP_OUT(iface);
	int retries = 0;
	int ret;

	dump_packet(LOG_DEBUG, DBG_MASK, __func__, buf, len);
	if (!xusb_device->impl->handle) {
		XUSB_ERR(iface, "device closed\n");
		return -ENXIO;
	}
	if (!EP_IS_OUT(ep_out)) {
		XUSB_ERR(iface, "%s called with an input endpoint 0x%x\n",
			__func__, ep_out);
		return -EINVAL;
	}
retry_write:
	switch (iface->transfer_type) {
	case XUSB_TT_BULK:
		ret = usb_bulk_write(xusb_device->impl->handle, ep_out, (char *)buf, len, timeout);
		break;
	case XUSB_TT_INTERRUPT:
		ret = usb_interrupt_write(xusb_device->impl->handle, ep_out, (char *)buf, len, timeout);
		break;
	default:
		ret = -EINVAL;
		break;
	}
	if (ret < 0) {
		/*
		 * If the device was gone, it may be the
		 * result of renumeration. Ignore it.
		 */
		if (ret != -ENODEV) {
			XUSB_ERR(iface, "write to endpoint 0x%x failed: (%d) %s\n",
				ep_out, ret, usb_strerror());
			dump_packet(LOG_ERR, DBG_MASK, "xusb_send[ERR]",
				buf, len);
			/*exit(2);*/
		} else {
			XUSB_DBG(iface, "write to endpoint 0x%x got ENODEV\n",
				ep_out);
			xusb_close(xusb_device);
		}
		return ret;
	}
	if (!ret) {

		XUSB_ERR(iface, "write to endpoint 0x%x short write[%d]: (%d)\n",
			ep_out, retries, ret);
		if (retries++ > MAX_RETRIES)
			return -EFAULT;
		usleep(100);
		goto retry_write;
	}
	if (ret != len) {
		XUSB_ERR(iface, "write to endpoint 0x%x short write: (%d) %s\n",
			ep_out, ret, usb_strerror());
		dump_packet(LOG_ERR, DBG_MASK, "xusb_send[ERR]", buf, len);
		return -EFAULT;
	}
	return ret;
}

int xusb_recv(struct xusb_iface *iface, char *buf, size_t len, int timeout)
{
	struct xusb_device *xusb_device = iface->xusb_device;
	int ep_in = EP_IN(iface);
	int retries = 0;
	int ret;

	if (!xusb_device->impl->handle) {
		XUSB_ERR(iface, "device closed\n");
		return -ENXIO;
	}
	if (!EP_IS_IN(ep_in)) {
		XUSB_ERR(iface, "called with an output endpoint 0x%x\n", ep_in);
		return -EINVAL;
	}
retry_read:
	switch (iface->transfer_type) {
	case XUSB_TT_BULK:
		ret = usb_bulk_read(xusb_device->impl->handle, ep_in, buf, len, timeout);
		break;
	case XUSB_TT_INTERRUPT:
		ret = usb_interrupt_read(xusb_device->impl->handle, ep_in, buf, len, timeout);
		break;
	default:
		ret = -EAFNOSUPPORT;
		break;
	}
	if (ret < 0) {
		XUSB_DBG(iface, "read from endpoint 0x%x failed: (%d) %s\n",
			ep_in, ret, usb_strerror());
		memset(buf, 0, len);
		return ret;
	}
	if (!ret) {
		XUSB_ERR(iface, "read to endpoint 0x%x short read[%d]: (%d)\n",
			ep_in, retries, ret);
		if (retries++ > MAX_RETRIES)
			return -EFAULT;
		usleep(100);
		goto retry_read;
	}
	dump_packet(LOG_DEBUG, DBG_MASK, __func__, buf, ret);
	return ret;
}

/*
 * Serialize calls to usb_find_busses()/usb_find_devices()
 */

static const key_t	SEM_KEY = 0x1a2b3c4d;
static int semid = -1;	/* Failure */

static void xusb_lock_usb()
{
	struct sembuf	sembuf;

	while (semid < 0) {
		/* Maybe it was already created? */
		semid = semget(SEM_KEY, 1, 0);
		if (semid < 0) {
			/* No, let's create ourselves */
			semid = semget(SEM_KEY, 1, IPC_CREAT | IPC_EXCL | 0644);
			if (semid < 0) {
				/* Someone else won the race to create it */
				if (errno != ENOENT)
					ERR("%s: semget() failed: %s\n",
						__func__, strerror(errno));
				/* Retry */
				continue;
			}
			/* Initialize */
			if (semctl(semid, 0, SETVAL, 1) < 0)
				ERR("%s: SETVAL() failed: %s\n",
					__func__, strerror(errno));
		}
	}
	DBG("%d: LOCKING\n", getpid());
	sembuf.sem_num = 0;
	sembuf.sem_op = -1;
	sembuf.sem_flg = SEM_UNDO;
	if (semop(semid, &sembuf, 1) < 0)
		ERR("%s: semop() failed: %s\n", __func__, strerror(errno));
	DBG("%d: LOCKED\n", getpid());
}

static void xusb_unlock_usb()
{
	struct sembuf	sembuf;

	DBG("%d: UNLOCKING\n", getpid());
	sembuf.sem_num = 0;
	sembuf.sem_op = 1;
	sembuf.sem_flg = SEM_UNDO;
	if (semop(semid, &sembuf, 1) < 0)
		ERR("%s: semop() failed: %s\n", __func__, strerror(errno));
	DBG("%d: UNLOCKED\n", getpid());
}

static int		initizalized;

void __attribute__((constructor)) xusb_init(void)
{
	xtalk_parse_options();
	if (!getenv("XUSB_NOLOCK"))
		xusb_lock_usb();
	if (!initizalized) {
		usb_init();
		initizalized = 1;
	}
	usb_find_busses();
	usb_find_devices();
	if (!getenv("XUSB_NOLOCK"))
		xusb_unlock_usb();
}