/***************************************************************************
 *            usb-libusb.c
 *
 *  Wed Jun 16 20:53:01 2004
 *  Copyright  2004  Stanislav Brabec
 *  utx@penguin.cz
 ****************************************************************************/

/*
 *  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 Library 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.
 */

/* FIXME: for debugging only */
#include <stdio.h>

#include "mltusb.h"

/* 4096 max. usb size / 16 bytes per line * 81 characters per line */
//FIXME: char retstring[20736];
char retstring[65536];

char *
hexdump (char *data, int size)
{
  char *ptr = retstring;
  char ascii_string[17];
  int i, first = 1;
  retstring[0] = 0;
  ascii_string[16] = 0;
  for (i = 0; i < size; i++)
    {
      if (!(i % 16))
	{
	  if (first)
	    first = 0;
	  else
	    {
	      ptr += sprintf (ptr, "  '%s'", ascii_string);
	    }
	  ptr += sprintf (ptr, "\n  %08x: ", (unsigned int) i);
	};
      ptr += sprintf (ptr, " %02x", (unsigned char) data[i]);
      ascii_string[i % 16] =
	((data[i] >= 0x20 && data[i] < 0x7f) ? data[i] : '.');
    }
  if (size > 0)
    {
      for (i = 0; i < 15 - (size - 1) % 16; i++)
	{
	  ptr += sprintf (ptr, "   ");
	}
      ascii_string[(size - 1) % 16 + 1] = 0;
      ptr += sprintf (ptr, "  '%s'", ascii_string);
    }
  return retstring;
}

int
myusb_bulk_write (struct usb_dev_handle *handle, int ep, char *bytes,
		  int size, int timeout)
{
  int send_status;
  DBG3 ("TX expecting=%d ", size);
  send_status = usb_bulk_write (handle, ep, bytes, size, timeout);
  DBG3 ("sent=%d (0x%08x) ->%s\n", send_status, send_status,
	hexdump (bytes, send_status));
  return send_status;
}

int
myusb_bulk_read (struct usb_dev_handle *handle, int ep, char *bytes, int size,
		 int timeout)
{
  int get_status;
  DBG3 ("RX expecting=%d ", size);
  get_status = usb_bulk_read (handle, ep, bytes, size, timeout);
  DBG3 ("got=%d (0x%08x) ->%s\n", get_status, get_status,
	hexdump (bytes, get_status));
  return get_status;
}

/* End of debugging functions */
/* =========================================================== */

int
mltusb_compare_id (struct mltusb_device *dev, uint16_t idVendor,
		   uint16_t idProduct)
{
  if (((struct usb_device *) dev)->descriptor.idVendor == idVendor)
    {
      if (((struct usb_device *) dev)->descriptor.idProduct == idProduct)
	{
	  return 1;
	}
    }
  return 0;
}


/**
 * mltrec_find_devices:
 * request: Locate which devices - all cameras or only cameras in
 * Remote Control mote. Either @USB_DEVICE_LOCATE_MLTREC_ALL or
 * @USB_DEVICE_LOCATE_MLTREC_REMOTE.
 * @returns: Pointer to @dev @== @NULL terminated array of
 * @mltrec_device. If your software plans to supports only one
 * camera, you can simply use first item.
 *
 * Locates all Minolta DiMAGE Cameras on all USB busses. It locates
 * also cameras in Mass Storage mode. User must detect them and
 * display warning. Number of cameras is currently limited to 16
 * (expecting that more is probably system bug).
 */
/* FIXME: This function can be done OS independent. */
mltusb_device_array *
mltusb_find_devices (mltusb_locate_request request)
{
  static mltusb_device_array mltrec_devices[17];
  int mltrec_device_count = 0;
  int device_type = 0;
  struct usb_bus *bus;
  struct usb_device *dev;

  usb_init ();
  usb_set_debug (5);

  usb_find_busses ();
  usb_find_devices ();

  for (bus = usb_busses; bus; bus = bus->next)
    {
      for (dev = bus->devices; dev; dev = dev->next)
	{
	  if ((device_type =
	       mltusb_check_device_type ((struct mltusb_device *) dev)) !=
	      MLTUSB_DEVICE_UNKNOWN)
	    {
	      if ((request == MLTUSB_LOCATE_ALL)
		  || (device_type != MLTUSB_DEVICE_MASS_STORAGE))
		{
		  mltrec_devices[mltrec_device_count].usb_device =
		    (struct mltusb_device *) dev;
		  mltrec_devices[mltrec_device_count++].device_type =
		    device_type;
		}
	    }
	  if (mltrec_device_count > 16)
	    return mltrec_devices;
	}
    }
  mltrec_devices[mltrec_device_count].usb_device = NULL;
  return mltrec_devices;
}

void
mltusb_find_devices_free (mltusb_device_array * devices)
{
}

struct mltusb_handle *
mltusb_open (struct mltusb_device *usbdev)
{
  struct mltusb_handle *handle;
  handle = (struct mltusb_handle *) usb_open ((struct usb_device *) usbdev);
  if (handle)
    {
      if ((usb_claim_interface ((struct usb_dev_handle *) handle, 0) < 0))
	{
	  /* FIXME: Error message. */
	  usb_close ((struct usb_dev_handle *) handle);
	  return NULL;
	}
    }
  return handle;
}

/* FIXME: return what? */
int
mltusb_close (struct mltusb_handle *handle)
{
  /* FIXME: What to do, if it fails? */
  usb_release_interface ((struct usb_dev_handle *) handle, 0);
  return usb_close ((struct usb_dev_handle *) handle);
}

int
mltusb_read (struct mltusb_handle *handle, int ep, char *bytes, int size)
{
  return usb_bulk_read ((struct usb_dev_handle *) handle, ep, bytes, size,
			MLTUSB_TIMEOUT);
}

int
mltusb_write (struct mltusb_handle *handle, int ep, char *bytes, int size)
{
  return usb_bulk_write ((struct usb_dev_handle *) handle, ep, bytes, size,
			 MLTUSB_TIMEOUT);
}

/* FIXME: void??? */
void
mltusb_atomic_start (struct mltusb_handle *handle)
{
}

void
mltusb_atomic_stop (struct mltusb_handle *handle)
{
}

