Exemple #1
0
int xusb_claim_interface(struct xusb *xusb)
{
	const struct usb_device_descriptor	*dev_desc;
	int					ret;

	assert(xusb);
	xusb_open(xusb);	/* If it's not open yet... */
	if (usb_claim_interface(xusb->handle, xusb->interface_num) != 0) {
		ERR("usb_claim_interface %d in '%s': %s\n",
			xusb->interface_num,
			xusb->devpath_tail,
			usb_strerror());
		return 0;
	}
	xusb->is_claimed = 1;
	xusb_fill_strings(xusb);
	dev_desc = &xusb->dev->descriptor;
	DBG("ID=%04X:%04X Manufacturer=[%s] Product=[%s] "
		"SerialNumber=[%s] Interface=[%s]\n",
		dev_desc->idVendor,
		dev_desc->idProduct,
		xusb->iManufacturer,
		xusb->iProduct,
		xusb->iSerialNumber,
		xusb->iInterface);
	if (xtalk_option_use_clear_halt()) {
		DBG("Using clear_halt()\n");
		if (usb_clear_halt(xusb->handle, EP_OUT(xusb)) != 0) {
			ERR("Clearing output endpoint: %s\n", usb_strerror());
			return 0;
		}
		if (usb_clear_halt(xusb->handle, EP_IN(xusb)) != 0) {
			ERR("Clearing input endpoint: %s\n", usb_strerror());
			return 0;
		}
	}
	ret = xusb_flushread(xusb);
	if (ret < 0) {
		ERR("xusb_flushread failed: %d\n", ret);
		return 0;
	}
	return 1;
}
Exemple #2
0
static struct xusb *xusb_new(struct usb_device *dev, const struct xusb_spec *spec)
{
	struct usb_device_descriptor	*dev_desc;
	struct usb_config_descriptor	*config_desc;
	struct usb_interface		*interface;
	struct usb_interface_descriptor	*iface_desc;
	struct usb_endpoint_descriptor	*endpoint;
	size_t				max_packet_size;
	int				i;
	struct xusb			*xusb = NULL;

	/*
	 * Get information from the usb_device
	 */
	if((dev_desc = &dev->descriptor) == NULL) {
		ERR("usb device without a device descriptor\n");
		goto fail;
	}
	if((config_desc = dev->config) == NULL) {
		ERR("usb device without a configuration descriptor\n");
		goto fail;
	}
	interface = &config_desc->interface[spec->my_interface_num];
	iface_desc = interface->altsetting;
	endpoint = iface_desc->endpoint;
	/* Calculate max packet size */
	max_packet_size = PACKET_SIZE;
	for(i = 0; i < iface_desc->bNumEndpoints; i++, endpoint++) {
		DBG("Validating endpoint @ %d (interface %d)\n", i, spec->my_interface_num);
		if(endpoint->bEndpointAddress == spec->my_ep_out || endpoint->bEndpointAddress == spec->my_ep_in) {
			if(endpoint->wMaxPacketSize > PACKET_SIZE) {
				ERR("Endpoint #%d wMaxPacketSize too large (%d)\n", i, endpoint->wMaxPacketSize);
				goto fail;
			}
			if(endpoint->wMaxPacketSize < max_packet_size) {
				max_packet_size = endpoint->wMaxPacketSize;
			}
		}
	}
	/* Fill xusb */
	if((xusb = malloc(sizeof(*xusb))) == NULL) {
		ERR("Out of memory");
		goto fail;
	}
	memset(xusb, 0, sizeof(*xusb));
	xusb->dev = dev;
	xusb->spec = spec;
	sscanf(dev->bus->dirname, "%d", &xusb->bus_num);
	sscanf(dev->filename, "%d", &xusb->device_num);
	snprintf(xusb->devpath_tail, PATH_MAX, "%03d/%03d",
		xusb->bus_num, xusb->device_num);
	xusb->interface_num = spec->my_interface_num;
	xusb->ep_out = spec->my_ep_out;
	xusb->ep_in = spec->my_ep_in;
	xusb->packet_size = max_packet_size;
	xusb->is_usb2 = (max_packet_size == 512);
	if (! xusb_open(xusb)) {
		ERR("Failed opening device: %04X:%04X - %s\n",
			dev_desc->idVendor,
			dev_desc->idProduct,
			xusb->devpath_tail);
		goto fail;
	}
	DBG("%04X:%04X - %s\n",
		dev_desc->idVendor,
		dev_desc->idProduct,
		xusb->devpath_tail);
	return xusb;
fail:
	xusb_destroy(xusb);
	return NULL;
}