/*
 Include file for the ioctl() based usbio library
 
 Copyright (c) 2006 Dimtel, Inc., All Rights Reserved

 $Id: usbio_bulk.h,v 1.1.1.1 2010/10/05 16:42:44 dim Exp $
 $Date: 2010/10/05 16:42:44 $
 $Author: dim $
 $Revision: 1.1.1.1 $
 $Log: usbio_bulk.h,v $
 Revision 1.1.1.1  2010/10/05 16:42:44  dim
 Initial import of iGp12

 Revision 1.2  2010/03/12 19:04:59  dim
 Updates for multiple boards, bug fixes

 Revision 1.1.1.1  2009/06/02 20:26:02  dim
 Initial import

 Revision 1.3  2009/06/02 19:56:51  dim
 Full support for service functions (fw load, 8051 control, EEPROM) added

 Revision 1.2  2006/11/16 21:28:13  dim
 Changes to support EP2/EP6 operation (removed EP4 support)
 Added R/W flag to the MSB of the count

 Revision 1.1.1.1  2006/11/16 19:38:15  dim
 Initial import

*/

#ifndef _USBIO_BULK_H_
#define _USBIO_BULK_H_

#include <pthread.h>
#include <string.h>
#include <usb.h>
#include <sys/time.h>

#define USBIO_VENDOR  0x04b4
#define USBIO_PRODUCT 0x8613

/*
  USBDEVFS hard limit is 16k. Empirically 31*512 produces
  highest write and read rates.
*/
#define USBIO_MAXLEN  15872

#define USBIO_EP_WRITE 2
#define USBIO_EP_RDATA 6

/* MSB defines the R/W direction */
#define USBIO_M_RW 0x80000000

#define USBIO_TIMEOUT 1000

#define USBIO_FW_CHUNK 4096

#define MAX(a, b) ((a) < (b)) ? (b) : (a)
#define MIN(a, b) ((a) >= (b)) ? (b) : (a)

#ifndef deltaT
#define deltaT(tvp1, tvp2) (((long long int)((tvp2)->tv_sec-(tvp1)->tv_sec))*1000000 + \
                              (tvp2)->tv_usec - (tvp1)->tv_usec)
#endif

typedef struct usbio_context
{
  pthread_mutex_t       mutex;
  struct usb_device     *device;
  struct usb_dev_handle *handle;
  int                   fd;
} usbio_context;

usbio_context *usbio_open(int flag);
usbio_context *usbio_open_serial(int flag, char *serial);
usbio_context *usbio_open_full(int flag, char *serial, unsigned short *bcd);
void usbio_close(usbio_context *ctxt, int flag);

/* High-speed R/W routines */
unsigned int usbio_write(usbio_context *ctxt, unsigned int addr,
                         unsigned int data);
unsigned int usbio_block_write(usbio_context *ctxt, unsigned int addr,
                               unsigned int *data, unsigned int count);
unsigned int usbio_block_read(usbio_context *ctxt, unsigned int addr,
                              unsigned int *data, unsigned int count);
unsigned int usbio_mult_write(usbio_context *ctxt, unsigned int *addr,
                              unsigned int *data, int count);
                              
/* Some definitions for convenience */
#define usbio_read(ctxt, addr, data) usbio_block_read(ctxt, addr, data, 1)

/* EP0 (vendor) control functions */
int usbio_if_reset(usbio_context *ctxt);
int usbio_i2c_write(usbio_context *ctxt, char addr, const void *buf, int len);
int usbio_i2c_read(usbio_context *ctxt, char addr, void *buf, int len);
int usbio_usberr_read(usbio_context *ctxt);
int usbio_write_sernum(usbio_context *ctxt, char *sernum);
int usbio_cpu_reset(usbio_context *ctxt, int value);
int usbio_readram(usbio_context *ctxt, int addr, int length, unsigned char **ram);
int usbio_writeram(usbio_context *ctxt, int addr, int length, unsigned char *data);
int usbio_fw_load(usbio_context *ctxt, char *file);

/* Common functions */
int string2bytes(char *buf, char *string, int bufsize);

#endif /* _USBIO_BULK_H_ */
