staging: Move media drivers to staging/media
[pohmelfs.git] / drivers / staging / media / easycap / easycap.h
blob7b256a948c2758246d9983bab460c754d1c971b4
1 /*****************************************************************************
2 * *
3 * easycap.h *
4 * *
5 *****************************************************************************/
6 /*
8 * Copyright (C) 2010 R.M. Thomas <rmthomas@sciolus.org>
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.
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.
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
26 /*****************************************************************************/
27 /*---------------------------------------------------------------------------*/
29 * THE FOLLOWING PARAMETERS ARE UNDEFINED:
31 * EASYCAP_DEBUG
33 * IF REQUIRED THEY MUST BE EXTERNALLY DEFINED, FOR EXAMPLE AS COMPILER
34 * OPTIONS.
36 /*---------------------------------------------------------------------------*/
38 #ifndef __EASYCAP_H__
39 #define __EASYCAP_H__
41 /*---------------------------------------------------------------------------*/
43 * THESE ARE NORMALLY DEFINED
45 /*---------------------------------------------------------------------------*/
46 #define PATIENCE 500
47 #define PERSEVERE
48 /*---------------------------------------------------------------------------*/
50 * THESE ARE FOR MAINTENANCE ONLY - NORMALLY UNDEFINED:
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>
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>
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>
85 /*---------------------------------------------------------------------------*/
86 /* VENDOR, PRODUCT: Syntek Semiconductor Co., Ltd
88 * EITHER EasyCAP USB 2.0 Video Adapter with Audio, Model No. DC60
89 * with input cabling: AUDIO(L), AUDIO(R), CVBS, S-VIDEO.
91 * OR EasyCAP 4CHANNEL USB 2.0 DVR, Model No. EasyCAP002
92 * with input cabling: MICROPHONE, CVBS1, CVBS2, CVBS3, CVBS4.
94 /*---------------------------------------------------------------------------*/
95 #define USB_EASYCAP_VENDOR_ID 0x05e1
96 #define USB_EASYCAP_PRODUCT_ID 0x0408
98 #define EASYCAP_DRIVER_VERSION "0.9.01"
99 #define EASYCAP_DRIVER_DESCRIPTION "easycapdc60"
101 #define USB_SKEL_MINOR_BASE 192
102 #define DONGLE_MANY 8
103 #define INPUT_MANY 6
104 /*---------------------------------------------------------------------------*/
106 * DEFAULT LUMINANCE, CONTRAST, SATURATION AND HUE
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 /*---------------------------------------------------------------------------*/
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.
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 /*---------------------------------------------------------------------------*/
131 * VIDEO BUFFERS
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 /*---------------------------------------------------------------------------*/
140 * AUDIO STREAMING PARAMETERS
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 /*---------------------------------------------------------------------------*/
149 * AUDIO BUFFERS
151 /*---------------------------------------------------------------------------*/
152 #define AUDIO_FRAGMENT_MANY 32
153 #define PAGES_PER_AUDIO_FRAGMENT 4
154 /*---------------------------------------------------------------------------*/
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.
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 /*---------------------------------------------------------------------------*/
190 * ENUMS
192 /*---------------------------------------------------------------------------*/
193 enum {
194 AT_720x576,
195 AT_704x576,
196 AT_640x480,
197 AT_720x480,
198 AT_360x288,
199 AT_320x240,
200 AT_360x240,
201 RESOLUTION_MANY
203 enum {
204 FMT_UYVY,
205 FMT_YUY2,
206 FMT_RGB24,
207 FMT_RGB32,
208 FMT_BGR24,
209 FMT_BGR32,
210 PIXELFORMAT_MANY
212 enum {
213 FIELD_NONE,
214 FIELD_INTERLACED,
215 INTERLACE_MANY
217 #define SETTINGS_MANY (STANDARD_MANY * \
218 RESOLUTION_MANY * \
219 2 * \
220 PIXELFORMAT_MANY * \
221 INTERLACE_MANY)
222 /*---------------------------------------------------------------------------*/
224 * STRUCTURE DEFINITIONS
226 /*---------------------------------------------------------------------------*/
227 struct easycap_dongle {
228 struct easycap *peasycap;
229 struct mutex mutex_video;
230 struct mutex mutex_audio;
232 /*---------------------------------------------------------------------------*/
233 struct data_buffer {
234 struct list_head list_head;
235 void *pgo;
236 void *pto;
237 u16 kount;
238 u16 input;
240 /*---------------------------------------------------------------------------*/
241 struct data_urb {
242 struct list_head list_head;
243 struct urb *purb;
244 int isbuf;
245 int length;
247 /*---------------------------------------------------------------------------*/
248 struct easycap_standard {
249 u16 mask;
250 struct v4l2_standard v4l2_standard;
252 struct easycap_format {
253 u16 mask;
254 char name[128];
255 struct v4l2_format v4l2_format;
257 struct 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;
273 /*---------------------------------------------------------------------------*/
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
279 /*---------------------------------------------------------------------------*/
280 struct easycap {
281 int isdongle;
282 int minor;
284 struct video_device video_device;
285 struct v4l2_device v4l2_device;
287 int status;
288 unsigned int audio_pages_per_fragment;
289 unsigned int audio_bytes_per_fragment;
290 unsigned int audio_buffer_page_many;
292 #define UPSAMPLE
293 #ifdef UPSAMPLE
294 s16 oldaudio;
295 #endif /*UPSAMPLE*/
297 int ilk;
298 bool microphone;
300 struct usb_device *pusb_device;
301 struct usb_interface *pusb_interface;
303 struct kref kref;
305 int queued[FRAME_BUFFER_MANY];
306 int done[FRAME_BUFFER_MANY];
308 wait_queue_head_t wq_video;
309 wait_queue_head_t wq_audio;
310 wait_queue_head_t wq_trigger;
312 int input;
313 int polled;
314 int standard_offset;
315 int format_offset;
316 struct inputset inputset[INPUT_MANY];
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];
327 long long int dnbydt;
329 int video_interface;
330 int video_altsetting_on;
331 int video_altsetting_off;
332 int video_endpointnumber;
333 int video_isoc_maxframesize;
334 int video_isoc_buffer_size;
335 int video_isoc_framesperdesc;
337 int video_isoc_streaming;
338 int video_isoc_sequence;
339 int video_idle;
340 int video_eof;
341 int video_junk;
343 struct data_buffer video_isoc_buffer[VIDEO_ISOC_BUFFER_MANY];
344 struct data_buffer field_buffer[FIELD_BUFFER_MANY]
345 [(FIELD_BUFFER_SIZE/PAGE_SIZE)];
346 struct data_buffer frame_buffer[FRAME_BUFFER_MANY]
347 [(FRAME_BUFFER_SIZE/PAGE_SIZE)];
349 struct list_head urb_video_head;
350 struct list_head *purb_video_head;
352 u8 cache[8];
353 u8 *pcache;
354 int video_mt;
355 int audio_mt;
356 long long audio_bytes;
357 u32 isequence;
359 int vma_many;
360 /*---------------------------------------------------------------------------*/
362 * BUFFER INDICATORS
364 /*---------------------------------------------------------------------------*/
365 int field_fill; /* Field buffer being filled by easycap_complete(). */
366 /* Bumped only by easycap_complete(). */
367 int field_page; /* Page of field buffer page being filled by */
368 /* easycap_complete(). */
369 int field_read; /* Field buffer to be read by field2frame(). */
370 /* Bumped only by easycap_complete(). */
371 int frame_fill; /* Frame buffer being filled by field2frame(). */
372 /* Bumped only by easycap_dqbuf() when */
373 /* field2frame() has created a complete frame. */
374 int frame_read; /* Frame buffer offered to user by DQBUF. */
375 /* Set only by easycap_dqbuf() to trail frame_fill.*/
376 int frame_lock; /* Flag set to 1 by DQBUF and cleared by QBUF */
377 /*---------------------------------------------------------------------------*/
379 * IMAGE PROPERTIES
381 /*---------------------------------------------------------------------------*/
382 u32 pixelformat;
383 int width;
384 int height;
385 int bytesperpixel;
386 bool byteswaporder;
387 bool decimatepixel;
388 bool offerfields;
389 int frame_buffer_used;
390 int frame_buffer_many;
391 int videofieldamount;
393 int brightness;
394 int contrast;
395 int saturation;
396 int hue;
398 int allocation_video_urb;
399 int allocation_video_page;
400 int allocation_video_struct;
401 int registered_video;
402 /*---------------------------------------------------------------------------*/
404 * ALSA
406 /*---------------------------------------------------------------------------*/
407 struct snd_pcm_hardware alsa_hardware;
408 struct snd_card *psnd_card;
409 struct snd_pcm *psnd_pcm;
410 struct snd_pcm_substream *psubstream;
411 int dma_fill;
412 int dma_next;
413 int dma_read;
414 /*---------------------------------------------------------------------------*/
416 * SOUND PROPERTIES
418 /*---------------------------------------------------------------------------*/
419 int audio_interface;
420 int audio_altsetting_on;
421 int audio_altsetting_off;
422 int audio_endpointnumber;
423 int audio_isoc_maxframesize;
424 int audio_isoc_buffer_size;
425 int audio_isoc_framesperdesc;
427 int audio_isoc_streaming;
428 int audio_idle;
429 int audio_eof;
430 int volume;
431 int mute;
432 s8 gain;
434 struct data_buffer audio_isoc_buffer[AUDIO_ISOC_BUFFER_MANY];
436 struct list_head urb_audio_head;
437 struct list_head *purb_audio_head;
438 /*---------------------------------------------------------------------------*/
440 * BUFFER INDICATORS
442 /*---------------------------------------------------------------------------*/
443 int audio_fill; /* Audio buffer being filled by easycap_complete(). */
444 /* Bumped only by easycap_complete(). */
445 int audio_read; /* Audio buffer page being read by easycap_read(). */
446 /* Set by easycap_read() to trail audio_fill by */
447 /* one fragment. */
448 /*---------------------------------------------------------------------------*/
450 * SOUND PROPERTIES
452 /*---------------------------------------------------------------------------*/
454 int audio_buffer_many;
456 int allocation_audio_urb;
457 int allocation_audio_page;
458 int allocation_audio_struct;
459 int registered_audio;
461 long long int audio_sample;
462 long long int audio_niveau;
463 long long int audio_square;
465 struct data_buffer audio_buffer[];
467 /*---------------------------------------------------------------------------*/
469 * VIDEO FUNCTION PROTOTYPES
471 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
472 long easycap_unlocked_ioctl(struct file *, unsigned int, unsigned long);
473 int easycap_dqbuf(struct easycap *, int);
474 int submit_video_urbs(struct easycap *);
475 int kill_video_urbs(struct easycap *);
476 int field2frame(struct easycap *);
477 int redaub(struct easycap *, void *, void *,
478 int, int, u8, u8, bool);
479 void easycap_testcard(struct easycap *, int);
480 int fillin_formats(void);
481 int newinput(struct easycap *, int);
482 int adjust_standard(struct easycap *, v4l2_std_id);
483 int adjust_format(struct easycap *, u32, u32, u32,
484 int, bool);
485 int adjust_brightness(struct easycap *, int);
486 int adjust_contrast(struct easycap *, int);
487 int adjust_saturation(struct easycap *, int);
488 int adjust_hue(struct easycap *, int);
489 int adjust_volume(struct easycap *, int);
490 /*---------------------------------------------------------------------------*/
492 * AUDIO FUNCTION PROTOTYPES
494 /*---------------------------------------------------------------------------*/
495 int easycap_alsa_probe(struct easycap *);
496 void easycap_alsa_complete(struct urb *);
498 int easycap_sound_setup(struct easycap *);
499 int submit_audio_urbs(struct easycap *);
500 int kill_audio_urbs(struct easycap *);
501 void easyoss_testtone(struct easycap *, int);
502 int audio_setup(struct easycap *);
503 /*---------------------------------------------------------------------------*/
505 * LOW-LEVEL FUNCTION PROTOTYPES
507 /*---------------------------------------------------------------------------*/
508 int audio_gainget(struct usb_device *);
509 int audio_gainset(struct usb_device *, s8);
511 int set_interface(struct usb_device *, u16);
512 int wakeup_device(struct usb_device *);
513 int confirm_resolution(struct usb_device *);
514 int confirm_stream(struct usb_device *);
516 int setup_stk(struct usb_device *, bool);
517 int setup_saa(struct usb_device *, bool);
518 int setup_vt(struct usb_device *);
519 int check_stk(struct usb_device *, bool);
520 int check_saa(struct usb_device *, bool);
521 int ready_saa(struct usb_device *);
522 int merit_saa(struct usb_device *);
523 int check_vt(struct usb_device *);
524 int select_input(struct usb_device *, int, int);
525 int set_resolution(struct usb_device *,
526 u16, u16, u16, u16);
528 int read_saa(struct usb_device *, u16);
529 int read_stk(struct usb_device *, u32);
530 int write_saa(struct usb_device *, u16, u16);
531 int write_000(struct usb_device *, u16, u16);
532 int start_100(struct usb_device *);
533 int stop_100(struct usb_device *);
534 int write_300(struct usb_device *);
535 int read_vt(struct usb_device *, u16);
536 int write_vt(struct usb_device *, u16, u16);
537 int isdongle(struct easycap *);
538 /*---------------------------------------------------------------------------*/
541 /*---------------------------------------------------------------------------*/
543 * MACROS SAM(...) AND JOM(...) ALLOW DIAGNOSTIC OUTPUT TO BE TAGGED WITH
544 * THE IDENTITY OF THE DONGLE TO WHICH IT APPLIES, BUT IF INVOKED WHEN THE
545 * POINTER peasycap IS INVALID AN Oops IS LIKELY, AND ITS CAUSE MAY NOT BE
546 * IMMEDIATELY OBVIOUS FROM A CASUAL READING OF THE SOURCE CODE. BEWARE.
548 /*---------------------------------------------------------------------------*/
549 const char *strerror(int err);
551 #define SAY(format, args...) do { \
552 printk(KERN_DEBUG "easycap:: %s: " \
553 format, __func__, ##args); \
554 } while (0)
555 #define SAM(format, args...) do { \
556 printk(KERN_DEBUG "easycap::%i%s: " \
557 format, peasycap->isdongle, __func__, ##args);\
558 } while (0)
560 #ifdef CONFIG_EASYCAP_DEBUG
561 extern int easycap_debug;
562 #define JOT(n, format, args...) do { \
563 if (n <= easycap_debug) { \
564 printk(KERN_DEBUG "easycap:: %s: " \
565 format, __func__, ##args);\
567 } while (0)
568 #define JOM(n, format, args...) do { \
569 if (n <= easycap_debug) { \
570 printk(KERN_DEBUG "easycap::%i%s: " \
571 format, peasycap->isdongle, __func__, ##args);\
573 } while (0)
575 #else
576 #define JOT(n, format, args...) do {} while (0)
577 #define JOM(n, format, args...) do {} while (0)
578 #endif /* CONFIG_EASYCAP_DEBUG */
580 /*---------------------------------------------------------------------------*/
582 /*---------------------------------------------------------------------------*/
583 /* globals
585 /*---------------------------------------------------------------------------*/
587 extern bool easycap_readback;
588 extern const struct easycap_standard easycap_standard[];
589 extern struct easycap_format easycap_format[];
590 extern struct v4l2_queryctrl easycap_control[];
591 extern struct usb_driver easycap_usb_driver;
592 extern struct easycap_dongle easycapdc60_dongle[];
594 #endif /* !__EASYCAP_H__ */