/* 
 * linux_usbif.c - Linux API; usb_control_msg() usb_set_interface()
 *                            usb_set_configuration()
 * Copyright (C) 2003 Takafumi Mizuno <taka-qce@ls-a.jp>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <ctype.h>
#include <sys/ioctl.h>
#if defined(__DragonFly__)
#include <bus/usb/usb.h>
#else
#include <dev/usb/usb.h>
#endif

#include "linux_usbif.h"

/* Linux API rapper */

int usb_control_msg(struct usb_device *dev, unsigned int pipe,
		    u_int8_t request, u_int8_t requesttype,
		    u_int16_t value, u_int16_t index, void *data, u_int16_t size, int timeout)
{
    /* XXX pipe and timeout is dummy */
    struct usb_ctl_request ur;

    ur.ucr_request.bRequest = request;
    ur.ucr_request.bmRequestType = requesttype;
    USETW(ur.ucr_request.wValue, value);
    USETW(ur.ucr_request.wIndex, index);
    USETW(ur.ucr_request.wLength, size);
    ur.ucr_data = data;
    ur.ucr_flags = 0;
    ur.ucr_actlen = 0;

    if (ioctl(dev->fd, USB_DO_REQUEST, &ur) < 0) {
	return -1;
    }
    
    return 0;

}

int usb_set_configuration(struct usb_device *dev, int configuration)
{
    if (ioctl(dev->fd, USB_SET_CONFIG, &configuration) < 0) {
	perror("USB_SET_CONFIG");
	return -1;
    }
    return 0;
}

int usb_set_interface(struct usb_device *dev, int ifnum, int alternate)
{
    /* XXX ifnum is dummy */
    struct usb_alt_interface alt;	/* interface/alternate selection */

    alt.uai_config_index = USB_CURRENT_CONFIG_INDEX;
    alt.uai_interface_index = 0;
    alt.uai_alt_no = alternate;
    if (ioctl(dev->fd, USB_SET_ALTINTERFACE, &alt) < 0) {
	perror("USB_SET_ALTINTERFACE");
	return -1;
    }
    return 0;
}
