aboutsummaryrefslogtreecommitdiffstats
path: root/at91sam7/host/usb_helper.c
blob: 0830fe4dabb3b65c2b9005f6aa63c1308a64a97a (plain)
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
/* usb_helper - Low-Level USB routines for SimTrace
 *
 * (C) 2006-2010 by Harald Welte <hwelte@hmw-consulting.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 
 *  as published by the Free Software Foundation
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#include <sys/time.h>

#include <sys/types.h>

#include <usb.h>

const char *
hexdump(const void *data, unsigned int len)
{
	static char string[65535];
	unsigned char *d = (unsigned char *) data;
	unsigned int i, left, ofs;

	string[0] = '\0';
	ofs = snprintf(string, sizeof(string)-1, "(%u): ", len);
	
	left = sizeof(string) - ofs;
	for (i = 0; len--; i += 3) {
		if (i >= sizeof(string) -4)
			break;
		snprintf(string+ofs+i, 4, " %02x", *d++);
	}
	string[sizeof(string)-1] = '\0';
	return string;
}

static struct usb_device *find_usb_device (uint16_t vendor_id, uint16_t product_id)
{
	struct usb_bus *bus;

	for (bus = usb_busses; bus; bus = bus->next) {
		struct usb_device *dev;
		for (dev = bus->devices; dev; dev = dev->next) {
			if (dev->descriptor.idVendor == vendor_id &&
			    dev->descriptor.idProduct == product_id)
				return dev;
		}
	}
	return NULL;
}

struct usb_dev_handle *usb_find_open(uint16_t vendor_id, uint16_t product_id)
{
	struct usb_device *dev;
	struct usb_dev_handle *hdl;

	usb_init();
	usb_find_busses();
	usb_find_devices();

	dev = find_usb_device(vendor_id, product_id);
	if (!dev) {
		fprintf(stderr, "Cannot find matching USB Device. "
			"Are you sure it is connected?\n");
		exit(1);
	}

	hdl = usb_open(dev);
	if (!hdl) {
		fprintf(stderr, "Unable to open usb device: %s\n",
			usb_strerror());
		exit(1);
	}

	if (usb_claim_interface(hdl, 0) < 0) {
		fprintf(stderr, "Unable to claim usb interface "
			"1 of device: %s\n", usb_strerror());
		exit(1);
	}

	return hdl;
}