/* JTAG GNU/Linux Cypress FX2 device I/O

Copyright (C) 2008 Dmitry Teytelman

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; either version 2 of the License, or
(at your option) any later version.

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

*/



#ifndef IOFX2_H
#define IOFX2_H

#include <usb.h>
#include "iobase.h"

#define	USB_VID_FSF			0xfffe	  // Free Software Folks
#define	USB_PID_FSF_EXP_0		0x0000	  // Experimental 0
#define	USB_PID_FSF_EXP_1		0x0001	  // Experimental 1
#define	USB_PID_FSF_USRP		0x0002	  // Universal Software Radio Peripheral
#define	USB_PID_FSF_USRP_reserved	0x0003	  // Universal Software Radio Peripheral
#define	USB_PID_FSF_SSRP		0x0004	  // Simple Software Radio Peripheral
#define	USB_PID_FSF_SSRP_reserved	0x0005	  // Simple Software Radio Peripheral
#define USB_PID_FSF_LBNL_UXO            0x0018    // http://recycle.lbl.gov/~ldoolitt/uxo/
#define USB_PID_FSF_LBNL_LLRF4          0x0019    // http://recycle.lbl.gov/~ldoolitt/llrf4/
#define USB_PID_FSF_LBNL_LLRF41         0x001a    // http://recycle.lbl.gov/~ldoolitt/llrf4/
#define USB_PID_FSF_DIMTEL_LLRF41       0x0020    // Dimtel firmware on LLRF4.2
#define USB_PID_FSF_DIMTEL_LLRF46       0x001b    // Dimtel firmware on LLRF4.6
#define USB_PID_FSF_DIMTEL_IGP12        0x0021    // iGp12

#define	MAX_EP0_PKTSIZE		        64   // max size of EP0 packet on FX2
#define MAX_EP8_WRITESIZE		512 // max size of EP8 bulk transfer

// Vendor bmRequestType masks (from usb_defs.h)

#define USB_DIR_IN			0x80
#define USB_TYPE_VENDOR			(0x02 << 5)

// Vendor requests (from vendor_commands.h)
/*
  Shift out TMS and TDI, capture TDO:
  wValueL: bit 1 TMS, bit 0 TDI
  returns one byte, LSB - TDO
*/
#define USB_REQ_VENDOR_JTAG_RXTX	0x83

/* --------- Write requests ----------- */
/* Shift a block of data out to TDI */
#define USB_REQ_VENDOR_JTAG		0x02
#define JTAG_FLAG_BLOCK			1
#define JTAG_FLAG_INIT			5

class IOFX2 : public IOBase
{
 protected:
  struct usb_dev_handle *udh;
  int total;
  bool error;
 public:
  IOFX2();
  virtual ~IOFX2();
  virtual void dev_open(const char *device);
  virtual bool txrx(bool tms, bool tdi);
  virtual void tx(bool tms, bool tdi);
  virtual void tx_tdi_byte(unsigned char tdi_byte);
  virtual void tx_tdi_block(unsigned char *tdi_buf, int length);
  void flush(void) {};
  void delay(int del);
  virtual inline bool checkError(){return error;}
};


#endif // IOFX2_H
