/*
 usb.h
*/

#ifndef usb_h
#define usb_h

#define MAX_ENDS    2 // maximum number of endpoints supported
#define STR_LEN   128 // maximum length for string descriptors
#define NAME_LEN   20 // maximum length of device name string

// endpoint array indexes
#define IN  0
#define OUT 1

typedef struct handle_s
{
  int fileswitch;  // from OS_Find, number
  int buffer;      // usb buffer number
  int devicefs;    // structure address
  int usbstream;   // structure address
  int driver;      // structure address, same for IN and OUT
  int buff_id;     // buffer inernal id

} handle_t;

typedef struct usb_endpoint_s
{
  int addr;
  int attr;
  int size;
  handle_t handle;

} usb_endpoint_t;

typedef struct usb_port_s
{
  char device[NAME_LEN];
  int vendor;
  int product_id;
  int version;
  int iMan;
  int iPro;
  int iSer;
  char manufacturer[STR_LEN];
  // note. &product will be 12 bytes higher than &name.
  //       This fact is used by midisupport to validate that
  //       a product string has been provided.
  char name[12];
  char product[STR_LEN];
  char serial_no[STR_LEN];
  int interface;

  int ends; // number of endpoints found
  usb_endpoint_t endpoint[MAX_ENDS];
  int open; // true if port open

} usb_port_t;

typedef struct usb_s
{
  int ports; // number of ports found
  usb_port_t port[MAX_PORTS];

} usb_t;

extern usb_t usb;

int usb_find(void);
_kernel_oserror *usb_open(int port);
_kernel_oserror *usb_close(int port);
void usb_wakeup_rx(int port);
void usb_remove(int port);
void usb_report(int full);

// pluging and unpluging devices
int usb_check_device(char *name);
int usb_dead_device(int driver);
int usb_new_device(int descriptors);
int usb_open_device(void);

#endif
