/*
 * wsstest.c:  Tool to test av7110 WSS interface - Rev. 0.1
 *
 * Copyright (C) 2006 Oliver Endriss (o.endriss@gmx.de)
 *
 * 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.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */

#include <stdio.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/videodev2.h>

int main(int argc, char *argv[])
{
	int fh;
	int rc;
	struct v4l2_capability cap;
	struct v4l2_sliced_vbi_cap vbicap;
	struct v4l2_format fmt;
	struct v4l2_sliced_vbi_data data;
	unsigned wss_data;

	if (argc < 3) {
		fprintf(stderr, "Usage: %s <dev> <wss_data>\n", argv[0]);
		return 99;
	}

	// open
	printf("open %s\n", argv[1]);
	fh = open(argv[1], O_WRONLY);
	if (fh < 0) {
		perror("open");
		return 1;
	}

#if 1   // VIDIOC_QUERYCAP
	rc = ioctl(fh, VIDIOC_QUERYCAP, &cap);
	if (rc < 0) {
		perror("VIDIOC_QUERYCAP");
		close(fh);
		return 10;
	}
	printf("VIDIOC_QUERYCAP: drv '%s' crd '%s' bus '%s' v %x cap %08x\n",
		cap.driver, cap.card, cap.bus_info, cap.version, cap.capabilities);
#endif

#if 1	// VIDIOC_G_SLICED_VBI_CAP
	rc =  ioctl(fh, VIDIOC_G_SLICED_VBI_CAP, &vbicap);
	if (rc < 0) {
		perror("VIDIOC_G_SLICED_VBI_CAP");
		close(fh);
		return 11;
	}
	printf("VIDIOC_G_SLICED_VBI_CAP: %x line23 %x\n",
		vbicap.service_set, vbicap.service_lines[0][23]);
#endif

#if 1
	// VIDIOC_S_FMT
	memset(&fmt, 0, sizeof fmt);
	fmt.type = V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
	fmt.fmt.sliced.service_set = V4L2_SLICED_WSS_625;
	printf("VIDIOC_S_FMT\n");
	rc = ioctl(fh, VIDIOC_S_FMT, &fmt);
	if (rc < 0) {
		perror("VIDIOC_S_FMT");
		close(fh);
		return 20;
	}
#endif

#if 1
	// VIDIOC_G_FMT
	fmt.type = V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
	rc = ioctl(fh, VIDIOC_G_FMT, &fmt);
	if (rc < 0) {
		perror("VIDIOC_G_FMT");
		close(fh);
		return 30;
	}
	printf("VIDIOC_G_FMT: %x line23 %x size %d\n",
		fmt.fmt.sliced.service_set, fmt.fmt.sliced.service_lines[0][23],
	       	fmt.fmt.sliced.io_size);
#endif

	// write
	sscanf(argv[2], "%x", &wss_data);
	memset(&data, 0, sizeof data);
	data.id = (wss_data) ? V4L2_SLICED_WSS_625 : 0;
	data.line = 23;
	data.data[0] = wss_data & 0xff;
	data.data[1] = (wss_data >> 8) & 0x3f;
	printf("write WSS %x\n", wss_data);
	rc = write(fh, &data, sizeof data);
	if (rc != sizeof data) {
		perror("write");
		close(fh);
		return 4;
	}

	printf("sleep\n", wss_data);
	sleep(10);

	printf("close -> reset to default mode\n");
	close(fh);
	return 0;
}

