Ticket #57: usb_debug_io.c

File usb_debug_io.c, 3.0 KB (added by stuge, 2 years ago)

revised with license header and small blksize improvement

Line 
1/*
2 * usb_debug_io - libusb program for the PLX NET20DC debug device
3 *
4 * Copyright(c) 2006 Peter Stuge <peter@stuge.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19#include <stdio.h>
20#include <stdlib.h>
21#include <usb.h>
22#include <sys/time.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25#include <errno.h>
26
27/* debug port can only send/receive 8 bytes per packet */
28#define BLKSIZE 8
29
30static int shutdown;
31
32static void sighandler(int signum) {
33  switch(signum) {
34    case SIGINT:
35    case SIGTERM:
36      shutdown=1;
37      break;
38    default:
39      break;
40  }
41}
42
43void setup_sighandler() {
44  struct sigaction sigact;
45  sigact.sa_handler=sighandler;
46  sigemptyset(&sigact.sa_mask);
47  sigact.sa_flags=0;
48  shutdown=0;
49  sigaction(SIGINT,&sigact,NULL);
50  sigaction(SIGTERM,&sigact,NULL);
51}
52
53
54int main(int argc,char **argv) {
55  int i,j,ep_in,ep_out,blksize,ret=1;
56  unsigned char buf[BLKSIZE];
57  struct usb_bus *busses,*bus;
58  struct usb_device *dev;
59  usb_dev_handle *dh;
60  struct timeval tv;
61
62  setup_sighandler();
63  usb_init();
64  usb_find_busses();
65  usb_find_devices();
66  busses=usb_get_busses();
67
68  for(bus=busses;bus;bus=bus->next)
69    for(dev=bus->devices;dev;dev=dev->next)
70      if(dev->descriptor.idVendor==0x0525&&dev->descriptor.idProduct==0x127a)
71        goto found;
72
73  fprintf(stderr,"device not found!\n");
74  goto xit;
75
76found:
77
78  blksize=BLKSIZE;
79  printf("using blksize=%d\n",blksize);
80  if(blksize<0) {
81    fprintf(stderr,"illegal blksize!\n");
82    goto xit;
83  }
84
85  dh=usb_open(dev);
86
87  if((i=usb_set_configuration(dh,1))<0) {
88    perror("usb_set_configuration");
89    goto xit;
90  }
91
92tryclaim:
93  if((i=usb_claim_interface(dh,0))<0) {
94    printf("usb_claim_interface=%d\n",i);
95    if(i==-EBUSY) {
96      if((i=usb_release_interface(dh,0))==0)
97        goto tryclaim;
98      perror("usb_release_interface");
99    }
100    goto xit;
101  }
102
103  ep_out=dev->config->interface->altsetting->endpoint[0].bEndpointAddress;
104  ep_in=dev->config->interface->altsetting->endpoint[1].bEndpointAddress;
105  printf("ep_in 0x%x ep_out 0x%x\n",ep_in,ep_out);
106
107  do {
108    i=usb_bulk_read(dh,ep_in,(char *)buf,8,500);
109    if(i<0)
110      perror("_read");
111    if(i<=0)
112      continue;
113    gettimeofday(&tv,NULL);
114    printf("%d.%06d _read=%d",(int)tv.tv_sec,(int)tv.tv_usec,i);
115    for(j=0;j<i;++j)
116      printf(" %02x",buf[j]);
117    printf("\n");
118  } while(i>=0 && !shutdown);
119  ret=0;
120
121xit:
122  usb_release_interface(dh,0);
123  usb_close(dh);
124  return ret;
125}