/*
 * Copyright (c) 2006 Anish Mistry. All rights reserved.
 *
 * 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 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 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 <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>

#include <stdio.h>

#define REP_NUM 3
#define REP_SIZE 6
#define REPORT_ID 0x10

int main(int argc, char *argv[])
{
	int fd, i, ok;
	char x;
	struct usb_ctl_report report;
	char rep[REP_NUM][REP_SIZE] = {{ 0xff, 0x80, 0x80, 0x01, 0x00, 0x00 },
			{ 0xff, 0x80, 0x00, 0x00, 0x30, 0x00 },
			{ 0xff, 0x81, 0x80, 0x00, 0x00, 0x00 }};
	char *dev = argv[1];

	if(argc < 2) {
		printf("usage: %s hid_devname\n", argv[0]);
		return -1;
	}

	fd = open(dev, O_WRONLY);
	if(!fd) {
		printf("Cannot open %s", dev);
		return -1;
	}

	for(i = 0, ok = 1;ok && i < REP_NUM;i++) {
		bzero(report.ucr_data, sizeof(report.ucr_data));
		report.ucr_report = UHID_OUTPUT_REPORT;
		report.ucr_data[0] = REPORT_ID;
		memcpy(report.ucr_data+1, rep[i], sizeof(rep[i]));
		if(ioctl(fd, USB_SET_REPORT, &report) == -1) {
			ok = 0;
			printf("USB_SET_REPORT failed! rep[%d] (%s)\n", i, strerror(errno));
		}
	}

	close(fd);

	if(ok)
		printf("%s converted to HCI mode.\n", dev);

	return 0;
}
