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