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
|
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "ausb.h"
#define CJPPA_USB_VENDOR_ID 0x0c4b
#define CJPPA_USB_DEVICE_ID 0x0100
static struct usb_device *find_cj_usbdev(int num)
{
struct usb_bus *busses, *bus;
struct usb_device *dev;
int found = 0;
busses = usb_get_busses();
for (bus = busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor == CJPPA_USB_VENDOR_ID &&
dev->descriptor.idProduct == CJPPA_USB_DEVICE_ID) {
found++;
if (found == num)
return dev;
}
}
}
return NULL;
}
static void int_cb(struct usbdevfs_urb *uurb, void *data)
{
struct ausb_dev_handle *ah = uurb->usercontext;
unsigned char *buffer = uurb->buffer;
int i;
fprintf(stdout, "int_cb() called, ");
ausb_dump_urb(uurb);
for (i = 0; i < uurb->actual_length; i++)
printf("%02x ", buffer[i]);
if (ausb_submit_urb(ah, uurb)) {
fprintf(stderr, "unable to resubmit urb\n");
ausb_close(ah);
exit(1);
}
fflush(stdout);
}
int main(int argc, char **argv)
{
struct usb_device *dev;
struct ausb_dev_handle *ah;
struct usbdevfs_urb *uurb;
char buffer[280];
ausb_init();
uurb = malloc(sizeof(*uurb));
dev = find_cj_usbdev(1);
if (!dev) {
fprintf(stderr, "unable to find matching usb device\n");
exit(1);
}
ah = ausb_open(dev);
if (!ah) {
fprintf(stderr, "error while opening usb device\n");
exit(1);
}
if (ausb_claim_interface(ah, 0)) {
fprintf(stderr, "unable to claim interface\n");
ausb_close(ah);
exit(1);
}
if (usb_set_configuration(ah->uh, 1)) {
fprintf(stderr, "unable to set configuration 1\n");
ausb_close(ah);
exit(1);
}
ausb_register_callback(ah, USBDEVFS_URB_TYPE_INTERRUPT, &int_cb, ah);
#if 1
ausb_fill_int_urb(uurb, 0x81, buffer, sizeof(buffer));
if (ausb_submit_urb(ah, uurb)) {
fprintf(stderr, "unable to submit urb\n");
ausb_close(ah);
exit(1);
}
while (1) {
sleep(10);
}
#endif
ausb_close(ah);
exit(0);
}
|