Linux Audio

Check our new training course

Loading...
Note: File does not exist in v5.14.15.
  1/*****************************************************************************
  2*                                                                            *
  3*  easycap.h                                                                 *
  4*                                                                            *
  5*****************************************************************************/
  6/*
  7 *
  8 *  Copyright (C) 2010 R.M. Thomas  <rmthomas@sciolus.org>
  9 *
 10 *
 11 *  This is free software; you can redistribute it and/or modify
 12 *  it under the terms of the GNU General Public License as published by
 13 *  the Free Software Foundation; either version 2 of the License, or
 14 *  (at your option) any later version.
 15 *
 16 *  The software is distributed in the hope that it will be useful,
 17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19 *  GNU General Public License for more details.
 20 *
 21 *  You should have received a copy of the GNU General Public License
 22 *  along with this software; if not, write to the Free Software
 23 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 24 *
 25*/
 26/*****************************************************************************/
 27/*---------------------------------------------------------------------------*/
 28/*
 29 *  THE FOLLOWING PARAMETERS ARE UNDEFINED:
 30 *
 31 *                EASYCAP_DEBUG
 32 *
 33 *  IF REQUIRED THEY MUST BE EXTERNALLY DEFINED, FOR EXAMPLE AS COMPILER
 34 *  OPTIONS.
 35 */
 36/*---------------------------------------------------------------------------*/
 37
 38#ifndef __EASYCAP_H__
 39#define __EASYCAP_H__
 40
 41/*---------------------------------------------------------------------------*/
 42/*
 43 *  THESE ARE NORMALLY DEFINED
 44 */
 45/*---------------------------------------------------------------------------*/
 46#define  PATIENCE  500
 47#define  PERSEVERE
 48/*---------------------------------------------------------------------------*/
 49/*
 50 *  THESE ARE FOR MAINTENANCE ONLY - NORMALLY UNDEFINED:
 51 */
 52/*---------------------------------------------------------------------------*/
 53#undef  EASYCAP_TESTCARD
 54/*---------------------------------------------------------------------------*/
 55#include <linux/kernel.h>
 56#include <linux/errno.h>
 57#include <linux/init.h>
 58#include <linux/slab.h>
 59#include <linux/module.h>
 60#include <linux/kref.h>
 61#include <linux/usb.h>
 62#include <linux/uaccess.h>
 63
 64#include <linux/i2c.h>
 65#include <linux/workqueue.h>
 66#include <linux/poll.h>
 67#include <linux/mm.h>
 68#include <linux/fs.h>
 69#include <linux/delay.h>
 70#include <linux/types.h>
 71
 72#include <linux/vmalloc.h>
 73#include <linux/sound.h>
 74#include <sound/core.h>
 75#include <sound/pcm.h>
 76#include <sound/pcm_params.h>
 77#include <sound/info.h>
 78#include <sound/initval.h>
 79#include <sound/control.h>
 80#include <media/v4l2-dev.h>
 81#include <media/v4l2-device.h>
 82#include <linux/videodev2.h>
 83#include <linux/soundcard.h>
 84
 85/*---------------------------------------------------------------------------*/
 86/*  VENDOR, PRODUCT:  Syntek Semiconductor Co., Ltd
 87 *
 88 *      EITHER        EasyCAP USB 2.0 Video Adapter with Audio, Model No. DC60
 89 *               with input cabling:  AUDIO(L), AUDIO(R), CVBS, S-VIDEO.
 90 *
 91 *          OR        EasyCAP 4CHANNEL USB 2.0 DVR, Model No. EasyCAP002
 92 *               with input cabling:  MICROPHONE, CVBS1, CVBS2, CVBS3, CVBS4.
 93 */
 94/*---------------------------------------------------------------------------*/
 95#define USB_EASYCAP_VENDOR_ID	0x05e1
 96#define USB_EASYCAP_PRODUCT_ID	0x0408
 97
 98#define EASYCAP_DRIVER_VERSION "0.9.01"
 99#define EASYCAP_DRIVER_DESCRIPTION "easycapdc60"
100
101#define USB_SKEL_MINOR_BASE     192
102#define DONGLE_MANY 8
103#define INPUT_MANY 6
104/*---------------------------------------------------------------------------*/
105/*
106 *  DEFAULT LUMINANCE, CONTRAST, SATURATION AND HUE
107 */
108/*---------------------------------------------------------------------------*/
109#define SAA_0A_DEFAULT 0x7F
110#define SAA_0B_DEFAULT 0x3F
111#define SAA_0C_DEFAULT 0x2F
112#define SAA_0D_DEFAULT 0x00
113/*---------------------------------------------------------------------------*/
114/*
115 *  VIDEO STREAMING PARAMETERS:
116 *  USB 2.0 PROVIDES FOR HIGH-BANDWIDTH ENDPOINTS WITH AN UPPER LIMIT
117 *  OF 3072 BYTES PER MICROFRAME for wMaxPacketSize.
118 */
119/*---------------------------------------------------------------------------*/
120#define VIDEO_ISOC_BUFFER_MANY 16
121#define VIDEO_ISOC_ORDER 3
122#define VIDEO_ISOC_FRAMESPERDESC ((unsigned int) 1 << VIDEO_ISOC_ORDER)
123#define USB_2_0_MAXPACKETSIZE 3072
124#if (USB_2_0_MAXPACKETSIZE > PAGE_SIZE)
125#error video_isoc_buffer[.] will not be big enough
126#endif
127#define VIDEO_JUNK_TOLERATE VIDEO_ISOC_BUFFER_MANY
128#define VIDEO_LOST_TOLERATE 50
129/*---------------------------------------------------------------------------*/
130/*
131 *  VIDEO BUFFERS
132 */
133/*---------------------------------------------------------------------------*/
134#define FIELD_BUFFER_SIZE (203 * PAGE_SIZE)
135#define FRAME_BUFFER_SIZE (405 * PAGE_SIZE)
136#define FIELD_BUFFER_MANY 4
137#define FRAME_BUFFER_MANY 6
138/*---------------------------------------------------------------------------*/
139/*
140 *  AUDIO STREAMING PARAMETERS
141 */
142/*---------------------------------------------------------------------------*/
143#define AUDIO_ISOC_BUFFER_MANY 16
144#define AUDIO_ISOC_ORDER 1
145#define AUDIO_ISOC_FRAMESPERDESC 32
146#define AUDIO_ISOC_BUFFER_SIZE (PAGE_SIZE << AUDIO_ISOC_ORDER)
147/*---------------------------------------------------------------------------*/
148/*
149 *  AUDIO BUFFERS
150 */
151/*---------------------------------------------------------------------------*/
152#define AUDIO_FRAGMENT_MANY 32
153#define PAGES_PER_AUDIO_FRAGMENT 4
154/*---------------------------------------------------------------------------*/
155/*
156 *  IT IS ESSENTIAL THAT EVEN-NUMBERED STANDARDS ARE 25 FRAMES PER SECOND,
157 *                        ODD-NUMBERED STANDARDS ARE 30 FRAMES PER SECOND.
158 *  THE NUMBERING OF STANDARDS MUST NOT BE CHANGED WITHOUT DUE CARE.  NOT
159 *  ONLY MUST THE PARAMETER
160 *                             STANDARD_MANY
161 *  BE CHANGED TO CORRESPOND TO THE NEW NUMBER OF STANDARDS, BUT ALSO THE
162 *  NUMBERING MUST REMAIN AN UNBROKEN ASCENDING SEQUENCE:  DUMMY STANDARDS
163 *  MAY NEED TO BE ADDED.   APPROPRIATE CHANGES WILL ALWAYS BE REQUIRED IN
164 *  ROUTINE fillin_formats() AND POSSIBLY ELSEWHERE.  BEWARE.
165 */
166/*---------------------------------------------------------------------------*/
167#define  PAL_BGHIN      0
168#define  PAL_Nc         2
169#define  SECAM          4
170#define  NTSC_N         6
171#define  NTSC_N_443     8
172#define  NTSC_M         1
173#define  NTSC_443       3
174#define  NTSC_M_JP      5
175#define  PAL_60         7
176#define  PAL_M          9
177#define  PAL_BGHIN_SLOW    10
178#define  PAL_Nc_SLOW       12
179#define  SECAM_SLOW        14
180#define  NTSC_N_SLOW       16
181#define  NTSC_N_443_SLOW   18
182#define  NTSC_M_SLOW       11
183#define  NTSC_443_SLOW     13
184#define  NTSC_M_JP_SLOW    15
185#define  PAL_60_SLOW       17
186#define  PAL_M_SLOW        19
187#define  STANDARD_MANY 20
188/*---------------------------------------------------------------------------*/
189/*
190 *  ENUMS
191 */
192/*---------------------------------------------------------------------------*/
193enum {
194	AT_720x576,
195	AT_704x576,
196	AT_640x480,
197	AT_720x480,
198	AT_360x288,
199	AT_320x240,
200	AT_360x240,
201	RESOLUTION_MANY
202};
203enum {
204	FMT_UYVY,
205	FMT_YUY2,
206	FMT_RGB24,
207	FMT_RGB32,
208	FMT_BGR24,
209	FMT_BGR32,
210	PIXELFORMAT_MANY
211};
212enum {
213	FIELD_NONE,
214	FIELD_INTERLACED,
215	INTERLACE_MANY
216};
217#define SETTINGS_MANY	(STANDARD_MANY * \
218			RESOLUTION_MANY * \
219			2 * \
220			PIXELFORMAT_MANY * \
221			INTERLACE_MANY)
222/*---------------------------------------------------------------------------*/
223/*
224 *  STRUCTURE DEFINITIONS
225 */
226/*---------------------------------------------------------------------------*/
227struct easycap_dongle {
228	struct easycap *peasycap;
229	struct mutex mutex_video;
230	struct mutex mutex_audio;
231};
232/*---------------------------------------------------------------------------*/
233struct data_buffer {
234	struct list_head list_head;
235	void *pgo;
236	void *pto;
237	u16 kount;
238	u16 input;
239};
240/*---------------------------------------------------------------------------*/
241struct data_urb {
242	struct list_head list_head;
243	struct urb *purb;
244	int isbuf;
245	int length;
246};
247/*---------------------------------------------------------------------------*/
248struct easycap_standard {
249	u16 mask;
250struct v4l2_standard v4l2_standard;
251};
252struct easycap_format {
253	u16 mask;
254	char name[128];
255struct v4l2_format v4l2_format;
256};
257struct inputset {
258	int input;
259	int input_ok;
260	int standard_offset;
261	int standard_offset_ok;
262	int format_offset;
263	int format_offset_ok;
264	int brightness;
265	int brightness_ok;
266	int contrast;
267	int contrast_ok;
268	int saturation;
269	int saturation_ok;
270	int hue;
271	int hue_ok;
272};
273/*---------------------------------------------------------------------------*/
274/*
275 *   easycap.ilk == 0   =>  CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=256
276 *   easycap.ilk == 2   =>  CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=9
277 *   easycap.ilk == 3   =>     FOUR-CVBS HARDWARE, AUDIO wMaxPacketSize=9
278 */
279/*---------------------------------------------------------------------------*/
280struct easycap {
281	int isdongle;
282	int minor;
283
284	struct video_device video_device;
285	struct v4l2_device v4l2_device;
286
287	int status;
288	unsigned int audio_pages_per_fragment;
289	unsigned int audio_bytes_per_fragment;
290	unsigned int audio_buffer_page_many;
291
292#define UPSAMPLE
293#ifdef UPSAMPLE
294	s16 oldaudio;
295#endif /*UPSAMPLE*/
296
297	int ilk;
298	bool microphone;
299
300	struct usb_device *pusb_device;
301	struct usb_interface *pusb_interface;
302
303	struct kref kref;
304
305	int queued[FRAME_BUFFER_MANY];
306	int done[FRAME_BUFFER_MANY];
307
308	wait_queue_head_t wq_video;
309	wait_queue_head_t wq_audio;
310	wait_queue_head_t wq_trigger;
311
312	int input;
313	int polled;
314	int standard_offset;
315	int format_offset;
316	struct inputset inputset[INPUT_MANY];
317
318	bool ntsc;
319	int fps;
320	int usec;
321	int tolerate;
322	int skip;
323	int skipped;
324	int lost[INPUT_MANY];
325	int merit[180];
326
327	struct timeval timeval0;
328	struct timeval timeval1;
329	struct timeval timeval2;
330	struct timeval timeval3;
331	struct timeval timeval6;
332	struct timeval timeval7;
333	struct timeval timeval8;
334	long long int dnbydt;
335
336	int    video_interface;
337	int    video_altsetting_on;
338	int    video_altsetting_off;
339	int    video_endpointnumber;
340	int    video_isoc_maxframesize;
341	int    video_isoc_buffer_size;
342	int    video_isoc_framesperdesc;
343
344	int    video_isoc_streaming;
345	int    video_isoc_sequence;
346	int    video_idle;
347	int    video_eof;
348	int    video_junk;
349
350	struct data_buffer video_isoc_buffer[VIDEO_ISOC_BUFFER_MANY];
351	struct data_buffer field_buffer[FIELD_BUFFER_MANY]
352					[(FIELD_BUFFER_SIZE/PAGE_SIZE)];
353	struct data_buffer frame_buffer[FRAME_BUFFER_MANY]
354					[(FRAME_BUFFER_SIZE/PAGE_SIZE)];
355
356	struct list_head urb_video_head;
357	struct list_head *purb_video_head;
358
359	u8 cache[8];
360	u8 *pcache;
361	int video_mt;
362	int audio_mt;
363	long long audio_bytes;
364	u32 isequence;
365
366	int vma_many;
367/*---------------------------------------------------------------------------*/
368/*
369 *  BUFFER INDICATORS
370 */
371/*---------------------------------------------------------------------------*/
372	int field_fill;	/* Field buffer being filled by easycap_complete().  */
373			/*   Bumped only by easycap_complete().              */
374	int field_page;	/* Page of field buffer page being filled by         */
375			/*   easycap_complete().                             */
376	int field_read;	/* Field buffer to be read by field2frame().         */
377			/*   Bumped only by easycap_complete().              */
378	int frame_fill;	/* Frame buffer being filled by field2frame().       */
379			/*   Bumped only by easycap_dqbuf() when             */
380			/*   field2frame() has created a complete frame.     */
381	int frame_read;	/* Frame buffer offered to user by DQBUF.            */
382			/*   Set only by easycap_dqbuf() to trail frame_fill.*/
383	int frame_lock;	/* Flag set to 1 by DQBUF and cleared by QBUF        */
384/*---------------------------------------------------------------------------*/
385/*
386 *  IMAGE PROPERTIES
387 */
388/*---------------------------------------------------------------------------*/
389	u32                   pixelformat;
390	int                     width;
391	int                     height;
392	int                     bytesperpixel;
393	bool                    byteswaporder;
394	bool                    decimatepixel;
395	bool                    offerfields;
396	int                     frame_buffer_used;
397	int                     frame_buffer_many;
398	int                     videofieldamount;
399
400	int                     brightness;
401	int                     contrast;
402	int                     saturation;
403	int                     hue;
404
405	int allocation_video_urb;
406	int allocation_video_page;
407	int allocation_video_struct;
408	int registered_video;
409/*---------------------------------------------------------------------------*/
410/*
411 *  ALSA
412 */
413/*---------------------------------------------------------------------------*/
414	struct snd_pcm_hardware alsa_hardware;
415	struct snd_card *psnd_card;
416	struct snd_pcm *psnd_pcm;
417	struct snd_pcm_substream *psubstream;
418	int dma_fill;
419	int dma_next;
420	int dma_read;
421/*---------------------------------------------------------------------------*/
422/*
423 *  SOUND PROPERTIES
424 */
425/*---------------------------------------------------------------------------*/
426	int audio_interface;
427	int audio_altsetting_on;
428	int audio_altsetting_off;
429	int audio_endpointnumber;
430	int audio_isoc_maxframesize;
431	int audio_isoc_buffer_size;
432	int audio_isoc_framesperdesc;
433
434	int audio_isoc_streaming;
435	int audio_idle;
436	int audio_eof;
437	int volume;
438	int mute;
439	s8 gain;
440
441	struct data_buffer audio_isoc_buffer[AUDIO_ISOC_BUFFER_MANY];
442
443	struct list_head urb_audio_head;
444	struct list_head *purb_audio_head;
445/*---------------------------------------------------------------------------*/
446/*
447 *  BUFFER INDICATORS
448 */
449/*---------------------------------------------------------------------------*/
450	int audio_fill;	/* Audio buffer being filled by easycap_complete().  */
451			/*   Bumped only by easycap_complete().              */
452	int audio_read;	/* Audio buffer page being read by easycap_read().   */
453			/*   Set by easycap_read() to trail audio_fill by    */
454			/*   one fragment.                                   */
455/*---------------------------------------------------------------------------*/
456/*
457 *  SOUND PROPERTIES
458 */
459/*---------------------------------------------------------------------------*/
460
461	int audio_buffer_many;
462
463	int allocation_audio_urb;
464	int allocation_audio_page;
465	int allocation_audio_struct;
466	int registered_audio;
467
468	long long int audio_sample;
469	long long int audio_niveau;
470	long long int audio_square;
471
472	struct data_buffer audio_buffer[];
473};
474/*---------------------------------------------------------------------------*/
475/*
476 *  VIDEO FUNCTION PROTOTYPES
477 */
478/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
479long easycap_unlocked_ioctl(struct file *, unsigned int, unsigned long);
480int              easycap_dqbuf(struct easycap *, int);
481int              submit_video_urbs(struct easycap *);
482int              kill_video_urbs(struct easycap *);
483int              field2frame(struct easycap *);
484int              redaub(struct easycap *, void *, void *,
485						int, int, u8, u8, bool);
486void             easycap_testcard(struct easycap *, int);
487int              fillin_formats(void);
488int              newinput(struct easycap *, int);
489int              adjust_standard(struct easycap *, v4l2_std_id);
490int              adjust_format(struct easycap *, u32, u32, u32,
491								int, bool);
492int              adjust_brightness(struct easycap *, int);
493int              adjust_contrast(struct easycap *, int);
494int              adjust_saturation(struct easycap *, int);
495int              adjust_hue(struct easycap *, int);
496int              adjust_volume(struct easycap *, int);
497/*---------------------------------------------------------------------------*/
498/*
499 *  AUDIO FUNCTION PROTOTYPES
500 */
501/*---------------------------------------------------------------------------*/
502int		easycap_alsa_probe(struct easycap *);
503void            easycap_alsa_complete(struct urb *);
504
505int              easycap_sound_setup(struct easycap *);
506int              submit_audio_urbs(struct easycap *);
507int              kill_audio_urbs(struct easycap *);
508void             easyoss_testtone(struct easycap *, int);
509int              audio_setup(struct easycap *);
510/*---------------------------------------------------------------------------*/
511/*
512 *  LOW-LEVEL FUNCTION PROTOTYPES
513 */
514/*---------------------------------------------------------------------------*/
515int              audio_gainget(struct usb_device *);
516int              audio_gainset(struct usb_device *, s8);
517
518int              set_interface(struct usb_device *, u16);
519int              wakeup_device(struct usb_device *);
520int              confirm_resolution(struct usb_device *);
521int              confirm_stream(struct usb_device *);
522
523int              setup_stk(struct usb_device *, bool);
524int              setup_saa(struct usb_device *, bool);
525int              setup_vt(struct usb_device *);
526int              check_stk(struct usb_device *, bool);
527int              check_saa(struct usb_device *, bool);
528int              ready_saa(struct usb_device *);
529int              merit_saa(struct usb_device *);
530int              check_vt(struct usb_device *);
531int              select_input(struct usb_device *, int, int);
532int              set_resolution(struct usb_device *,
533						u16, u16, u16, u16);
534
535int              read_saa(struct usb_device *, u16);
536int              read_stk(struct usb_device *, u32);
537int              write_saa(struct usb_device *, u16, u16);
538int              write_000(struct usb_device *, u16, u16);
539int              start_100(struct usb_device *);
540int              stop_100(struct usb_device *);
541int              write_300(struct usb_device *);
542int              read_vt(struct usb_device *, u16);
543int              write_vt(struct usb_device *, u16, u16);
544int		isdongle(struct easycap *);
545/*---------------------------------------------------------------------------*/
546struct signed_div_result {
547	long long int quotient;
548	unsigned long long int remainder;
549} signed_div(long long int, long long int);
550
551
552/*---------------------------------------------------------------------------*/
553/*
554 *  MACROS SAM(...) AND JOM(...) ALLOW DIAGNOSTIC OUTPUT TO BE TAGGED WITH
555 *  THE IDENTITY OF THE DONGLE TO WHICH IT APPLIES, BUT IF INVOKED WHEN THE
556 *  POINTER peasycap IS INVALID AN Oops IS LIKELY, AND ITS CAUSE MAY NOT BE
557 *  IMMEDIATELY OBVIOUS FROM A CASUAL READING OF THE SOURCE CODE.  BEWARE.
558*/
559/*---------------------------------------------------------------------------*/
560const char *strerror(int err);
561
562#define SAY(format, args...) do { \
563	printk(KERN_DEBUG "easycap:: %s: " \
564			format, __func__, ##args); \
565} while (0)
566#define SAM(format, args...) do { \
567	printk(KERN_DEBUG "easycap::%i%s: " \
568			format, peasycap->isdongle, __func__, ##args);\
569} while (0)
570
571#ifdef CONFIG_EASYCAP_DEBUG
572extern int easycap_debug;
573#define JOT(n, format, args...) do { \
574	if (n <= easycap_debug) { \
575		printk(KERN_DEBUG "easycap:: %s: " \
576			format, __func__, ##args);\
577	} \
578} while (0)
579#define JOM(n, format, args...) do { \
580	if (n <= easycap_debug) { \
581		printk(KERN_DEBUG "easycap::%i%s: " \
582			format, peasycap->isdongle, __func__, ##args);\
583	} \
584} while (0)
585
586#else
587#define JOT(n, format, args...) do {} while (0)
588#define JOM(n, format, args...) do {} while (0)
589#endif /* CONFIG_EASYCAP_DEBUG */
590
591/*---------------------------------------------------------------------------*/
592
593/*---------------------------------------------------------------------------*/
594/* globals
595 */
596/*---------------------------------------------------------------------------*/
597
598extern bool easycap_readback;
599extern const struct easycap_standard easycap_standard[];
600extern struct easycap_format easycap_format[];
601extern struct v4l2_queryctrl easycap_control[];
602extern struct usb_driver easycap_usb_driver;
603extern struct easycap_dongle easycapdc60_dongle[];
604
605#endif /* !__EASYCAP_H__  */