1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281/************************************************************************
* smi2021.h *
* *
* USB Driver for SMI2021 - EasyCap *
* **********************************************************************
*
* Copyright 2011-2013 Jon Arne Jørgensen
* <jonjon.arnearne--a.t--gmail.com>
*
* Copyright 2011, 2012 Tony Brown, Michal Demin, Jeffry Johnston
*
* 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, see <http://www.gnu.org/licenses/>.
*
* This driver is heavily influensed by the STK1160 driver.
* Copyright (C) 2012 Ezequiel Garcia
* <elezegarcia--a.t--gmail.com>
*
*/
#ifndef SMI2021_H
#define SMI2021_H
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/i2c.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-event.h>
#include <media/v4l2-ctrls.h>
#include <media/videobuf2-core.h>
#include <media/videobuf2-vmalloc.h>
#include <media/saa7115.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/initval.h>
#define SMI2021_DRIVER_VERSION "0.1"
#define SMI2021_ISOC_TRANSFERS 16
#define SMI2021_ISOC_PACKETS 64
#define SMI2021_ISOC_EP 0x82
/* General USB control setup */
#define SMI2021_USB_REQUEST 0x01
#define SMI2021_USB_INDEX 0x00
#define SMI2021_USB_SNDPIPE 0x00
#define SMI2021_USB_RCVPIPE 0x80
/* General video constants */
#define SMI2021_BYTES_PER_LINE 1440
#define SMI2021_PAL_LINES 576
#define SMI2021_NTSC_LINES 484
/* Timing Reference Codes, see saa7113 datasheet */
#define SMI2021_TRC_EAV 0x10
#define SMI2021_TRC_VBI 0x20
#define SMI2021_TRC_FIELD_2 0x40
#define SMI2021_TRC 0x80
#define _ADAPTED_FROM_USESPACE_CODE
#ifdef DEBUG
#define smi2021_dbg(fmt, args...) \
pr_debug("smi2021::%s: " fmt, __func__, \
##args)
#else
#define smi2021_dbg(fmt, args...)
#endif
#define MIN(a,b) (a<b?a:b)
#define smi2021_info(fmt, args...) \
pr_info("smi2021::%s: " fmt, \
__func__, ##args)
#define smi2021_warn(fmt, args...) \
pr_warn("smi2021::%s: " fmt, \
__func__, ##args)
#define smi2021_err(fmt, args...) \
pr_err("smi2021::%s: " fmt, \
__func__, ##args)
/* Structs passed on USB for device setup */
struct smi2021_set_hw_state {
u8 head;
u8 state;
} __packed;
/* A single videobuf2 frame buffer */
struct smi2021_buf {
/* Common vb2 stuff, must be first */
struct vb2_buffer vb;
struct list_head list;
void *mem;
unsigned int length;
bool active;
};
struct smi2021_vid_input {
char *name;
int type;
};
enum smi2021_sync {
HSYNC,
SYNCZ1,
SYNCZ2,
#ifdef _ADAPTED_FROM_USESPACE_CODE
SYNCAV,
VBLANK,
VACTIVE,
//REMAINDER
//VHSYNC_BLANK1,
VHSYNC_BLANK2,
VHSYNC_BLANK1_OR_HSYNCH
#else
TRC
#endif
};
struct smi2021 {
struct device *dev;
struct usb_device *udev;
struct i2c_adapter i2c_adap;
struct i2c_client i2c_client;
struct v4l2_ctrl_handler ctrl_handler;
struct v4l2_subdev *gm7113c_subdev;
struct v4l2_device v4l2_dev;
struct video_device vdev;
struct vb2_queue vb2q;
struct mutex v4l2_lock;
struct mutex vb2q_lock;
/* List of videobuf2 buffers protected by a lock. */
spinlock_t buf_lock;
struct list_head bufs;
struct smi2021_buf *cur_buf;
int sequence;
/* Frame settings */
int currentFrameHeight;
v4l2_std_id cur_norm;
struct snd_card *snd_card;
struct snd_pcm_substream *pcm_substream;
unsigned int pcm_write_ptr;
unsigned int pcm_complete_samples;
u8 pcm_read_offset;
struct work_struct adev_capture_trigger;
atomic_t adev_capturing;
/* stuff from the userspace impl */
// this sttruct is memset for each new frame
struct
{
enum smi2021_sync sync_state;
bool frameBeingIgnored; // flagged when have started, awaiting a frame start, or missed a buffer fetch
unsigned bytes_remaining_to_fetch;
struct
{
unsigned field0, field1;
} active_line_count;
unsigned fieldNumber;
} parseVideoStateMachine;
struct
{
// how many times we lost hsync
unsigned missedHSync, horizBlanks, vertBlanks;
// how many times we opened the v4l larder and it was bare
unsigned missedV4lBuffers;
// how many full frames we saw
unsigned caughtFrames, ignoredFrames;
// how many copies
unsigned slowCopies, intCopies;
// zero len urbs
unsigned zeroLenURBs;
// urb packet types
unsigned videoPackets, audioPackets, unknownPackets;
// poke beyond count
unsigned tooManyScanlines;
// state counts
unsigned hsync, blank2, blank1, synchz1, synchz2, synchav, blank, active;
// field counts
unsigned SAV_found_field0, SAV_found_field1;
} runtimeStats;
// what vertical timing are we using (tables 4&5 in the 7713 spec)
// subaddress 08 bitmask 0x40
enum {
itu556_525ln60hz=0, itu556_625ln50hz
} fieldSelection;
#ifdef DEBUG
struct {
unsigned totalFrames;
unsigned vblank_found_field0, vblank_found_field1;
} debug;
#endif
// my stuff
/* Device settings */
unsigned int vid_input_count;
const struct smi2021_vid_input *vid_inputs;
int cur_input;
int iso_size;
struct urb *isoc_urbs[SMI2021_ISOC_TRANSFERS];
};
/* Provided by smi2021_bootloader.c */
int smi2021_bootloader_probe(struct usb_interface *intf,
const struct usb_device_id *devid);
void smi2021_bootloader_disconnect(struct usb_interface *intf);
/* Provided by smi2021_main.c */
void smi2021_toggle_audio(struct smi2021 *smi2021, bool enable);
int smi2021_start(struct smi2021 *smi2021);
void smi2021_stop(struct smi2021 *smi2021);
bool smi2021_setSourceSignalFormat(struct smi2021 *thisSMI2021, v4l2_std_id standardId);
/* Provided by smi2021_v4l2.c */
int smi2021_vb2_setup(struct smi2021 *smi2021);
int smi2021_video_register(struct smi2021 *smi2021);
/* Provided by smi2021_audio.c */
int smi2021_snd_register(struct smi2021 *smi2021);
void smi2021_snd_unregister(struct smi2021 *smi2021);
void smi2021_stop_audio(struct smi2021 *smi2021);
void smi2021_audio(struct smi2021 *smi2021, u8 *data, int len);
#endif /* SMI2021_H */