/*
 * usb_debug_io - libusb program for the PLX NET20DC debug device
 *
 * Copyright(c) 2006 Peter Stuge <peter@stuge.se>
 *
 * 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; version 2 of the License.
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
 */
#include <stdio.h>
#include <stdlib.h>
#include <usb.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>

/* debug port can only send/receive 8 bytes per packet */
#define BLKSIZE 8

static int shutdown;

static void sighandler(int signum) {
  switch(signum) {
    case SIGINT:
    case SIGTERM:
      shutdown=1;
      break;
    default:
      break;
  }
}

void setup_sighandler() {
  struct sigaction sigact;
  sigact.sa_handler=sighandler;
  sigemptyset(&sigact.sa_mask);
  sigact.sa_flags=0;
  shutdown=0;
  sigaction(SIGINT,&sigact,NULL);
  sigaction(SIGTERM,&sigact,NULL);
}


int main(int argc,char **argv) {
  int i,j,ep_in,ep_out,blksize,ret=1;
  unsigned char buf[BLKSIZE];
  struct usb_bus *busses,*bus;
  struct usb_device *dev;
  usb_dev_handle *dh;
  struct timeval tv;

  setup_sighandler();
  usb_init();
  usb_find_busses();
  usb_find_devices();
  busses=usb_get_busses();

  for(bus=busses;bus;bus=bus->next)
    for(dev=bus->devices;dev;dev=dev->next)
      if(dev->descriptor.idVendor==0x0525&&dev->descriptor.idProduct==0x127a)
        goto found;

  fprintf(stderr,"device not found!\n");
  goto xit;

found:

  blksize=BLKSIZE;
  printf("using blksize=%d\n",blksize);
  if(blksize<0) {
    fprintf(stderr,"illegal blksize!\n");
    goto xit;
  }

  dh=usb_open(dev);

  if((i=usb_set_configuration(dh,1))<0) {
    perror("usb_set_configuration");
    goto xit;
  }

tryclaim:
  if((i=usb_claim_interface(dh,0))<0) {
    printf("usb_claim_interface=%d\n",i);
    if(i==-EBUSY) {
      if((i=usb_release_interface(dh,0))==0)
        goto tryclaim;
      perror("usb_release_interface");
    }
    goto xit;
  }

  ep_out=dev->config->interface->altsetting->endpoint[0].bEndpointAddress;
  ep_in=dev->config->interface->altsetting->endpoint[1].bEndpointAddress;
  printf("ep_in 0x%x ep_out 0x%x\n",ep_in,ep_out);

  do {
    i=usb_bulk_read(dh,ep_in,(char *)buf,8,500);
    if(i<0)
      perror("_read");
    if(i<=0)
      continue;
    gettimeofday(&tv,NULL);
    printf("%d.%06d _read=%d",(int)tv.tv_sec,(int)tv.tv_usec,i);
    for(j=0;j<i;++j)
      printf(" %02x",buf[j]);
    printf("\n");
  } while(i>=0 && !shutdown);
  ret=0;

xit:
  usb_release_interface(dh,0);
  usb_close(dh);
  return ret;
}
