xref: /openbsd/sys/dev/usb/utvfu.h (revision 94e909e9)
1 /*	$OpenBSD: utvfu.h,v 1.6 2025/01/12 16:39:39 mglocker Exp $ */
2 /*
3  * Copyright (c) 2013 Lubomir Rintel
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer,
11  *    without modification.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * Alternatively, this software may be distributed under the terms of the
16  * GNU General Public License ("GPL").
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*
31  * Fushicai USBTV007 Audio-Video Grabber Driver
32  *
33  * Product web site:
34  * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
35  *
36  * No physical hardware was harmed running Windows during the
37  * reverse-engineering activity
38  */
39 
40 #ifndef _UTVFU_H_
41 #define _UTVFU_H_
42 
43 #include <sys/rwlock.h>
44 #include <sys/queue.h>
45 #include <sys/audioio.h>
46 #include <sys/videoio.h>
47 
48 /* Hardware. */
49 #define UTVFU_VIDEO_ENDP	0x81
50 #define UTVFU_AUDIO_ENDP	0x83
51 #define UTVFU_BASE		0xc000
52 #define UTVFU_REQUEST_REG	12
53 
54 #define UTVFU_DFLT_IFACE_IDX	0
55 #define UTVFU_ALT_IFACE_IDX	1
56 
57 /*
58  * Number of concurrent isochronous urbs submitted.
59  * Higher numbers was seen to overly saturate the USB bus.
60  */
61 #define UTVFU_ISOC_TRANSFERS	3
62 
63 #define UTVFU_CHUNK_SIZE	256
64 #define UTVFU_CHUNK		240
65 
66 #define UTVFU_AUDIO_URBSIZE	20480
67 #define UTVFU_AUDIO_HDRSIZE	4
68 #define UTVFU_AUDIO_BUFFER	65536
69 
70 #define UTVFU_COMPOSITE_INPUT	0
71 #define UTVFU_SVIDEO_INPUT	1
72 
73 /* Chunk header. */
74 #define UTVFU_MAGIC(hdr)	 (hdr & 0xff000000U)
75 #define UTVFU_MAGIC_OK(hdr)	((hdr & 0xff000000U) == 0x88000000U)
76 #define UTVFU_FRAME_ID(hdr)	((hdr & 0x00ff0000U) >> 16)
77 #define UTVFU_ODD(hdr)		((hdr & 0x0000f000U) >> 15)
78 #define UTVFU_CHUNK_NO(hdr)	 (hdr & 0x00000fffU)
79 
80 #define UTVFU_TV_STD		(V4L2_STD_525_60 | V4L2_STD_PAL)
81 
82 /* parameters for supported TV norms */
83 struct utvfu_norm_params {
84 	v4l2_std_id	norm;
85 	int		cap_width;
86 	int		cap_height;
87 	int		frame_len;
88 };
89 
90 #define UTVFU_MAX_BUFFERS	32
91 struct utvfu_mmap {
92 	SIMPLEQ_ENTRY(utvfu_mmap)	q_frames;
93 	uint8_t				*buf;
94 	struct v4l2_buffer		v4l2_buf;
95 };
96 typedef SIMPLEQ_HEAD(, utvfu_mmap)	q_mmap;
97 
98 struct utvfu_frame_buf {
99 	uint		off;
100 	uint		size;
101 	uint16_t	chunks_done;
102 	uint8_t		fid;
103 	uint8_t		last_odd;
104 	uint8_t		*buf;
105 };
106 
107 #define UTVFU_NFRAMES_MAX	40
108 struct utvfu_isoc_xfer {
109 	struct utvfu_softc	*sc;
110 	struct usbd_xfer	*xfer;
111 	uint16_t		size[UTVFU_NFRAMES_MAX];
112 };
113 
114 struct utvfu_vs_iface {
115 	struct usbd_pipe	*pipeh;
116 	uint32_t		psize;
117 	struct utvfu_isoc_xfer	ixfer[UTVFU_ISOC_TRANSFERS];
118 };
119 
120 struct utvfu_as_iface {
121 	struct usbd_pipe	*pipeh;
122 	struct usbd_xfer	*xfer;
123 };
124 
125 struct utvfu_audio_chan {
126 	uint8_t			*start;
127 	uint8_t			*end;
128 	uint8_t			*cur;
129 	int			blksize;
130 	void			*intr_arg;
131 	void			(*intr)(void *);
132 	struct utvfu_as_iface	iface;
133 	struct rwlock		rwlock;
134 };
135 
136 /* Per-device structure. */
137 struct utvfu_softc {
138 	struct device		sc_dev;
139 	struct usbd_device	*sc_udev;
140 	struct usbd_interface	*sc_uifaceh;
141 
142 	/* audio & video device */
143 	struct device		*sc_audiodev;
144 	struct device		*sc_videodev;
145 
146 	int			sc_flags;
147 #define UTVFU_FLAG_MMAP		0x01
148 #define UTVFU_FLAG_AS_RUNNING	0x02
149 
150 	int			sc_normi;
151 	int			sc_nchunks;
152 	int			sc_input;
153 	int			sc_max_frame_sz;
154 	int			sc_nframes;
155 
156 	struct utvfu_vs_iface	sc_iface;
157 	struct utvfu_frame_buf	sc_fb;
158 
159 	struct utvfu_audio_chan	sc_audio;
160 
161 	/* mmap */
162 	struct utvfu_mmap	sc_mmap[UTVFU_MAX_BUFFERS];
163 	uint8_t			*sc_mmap_buffer;
164 	q_mmap			sc_mmap_q;
165 	int			sc_mmap_bufsz;
166 	int			sc_mmap_count;
167 
168 	/* uplayer */
169 	void			*sc_uplayer_arg;
170 	int			*sc_uplayer_fsize;
171 	uint8_t			*sc_uplayer_fbuffer;
172 	void			(*sc_uplayer_intr)(void *);
173 };
174 
175 int	utvfu_max_frame_size(void);
176 int	utvfu_set_regs(struct utvfu_softc *, const uint16_t regs[][2], int);
177 void	utvfu_image_chunk(struct utvfu_softc *, u_char *);
178 int	utvfu_configure_for_norm(struct utvfu_softc *, v4l2_std_id);
179 int	utvfu_start_capture(struct utvfu_softc *);
180 int	utvfu_mmap_queue(struct utvfu_softc *, uint8_t *, int);
181 void	utvfu_read(struct utvfu_softc *, uint8_t *, int);
182 
183 void	utvfu_audio_decode(struct utvfu_softc *, int);
184 int	utvfu_audio_start(struct utvfu_softc *);
185 int	utvfu_audio_stop(struct utvfu_softc *);
186 int	utvfu_audio_start_chip(struct utvfu_softc *);
187 int	utvfu_audio_stop_chip(struct utvfu_softc *);
188 
189 #endif
190