gma500: begin the config based split
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / easycap / easycap.h
blob1f94e2389efcbae725594185805f1fa2e43232fb
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/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>
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>
88 #ifndef PAGE_SIZE
89 #error "PAGE_SIZE not defined"
90 #endif /* PAGE_SIZE */
92 /*---------------------------------------------------------------------------*/
93 /* VENDOR, PRODUCT: Syntek Semiconductor Co., Ltd
95 * EITHER EasyCAP USB 2.0 Video Adapter with Audio, Model No. DC60
96 * with input cabling: AUDIO(L), AUDIO(R), CVBS, S-VIDEO.
98 * OR EasyCAP 4CHANNEL USB 2.0 DVR, Model No. EasyCAP002
99 * with input cabling: MICROPHONE, CVBS1, CVBS2, CVBS3, CVBS4.
101 /*---------------------------------------------------------------------------*/
102 #define USB_EASYCAP_VENDOR_ID 0x05e1
103 #define USB_EASYCAP_PRODUCT_ID 0x0408
105 #define EASYCAP_DRIVER_VERSION "0.9.01"
106 #define EASYCAP_DRIVER_DESCRIPTION "easycapdc60"
108 #define USB_SKEL_MINOR_BASE 192
109 #define DONGLE_MANY 8
110 #define INPUT_MANY 6
111 /*---------------------------------------------------------------------------*/
113 * DEFAULT LUMINANCE, CONTRAST, SATURATION AND HUE
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 /*---------------------------------------------------------------------------*/
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.
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 /*---------------------------------------------------------------------------*/
138 * VIDEO BUFFERS
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 /*---------------------------------------------------------------------------*/
147 * AUDIO STREAMING PARAMETERS
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 /*---------------------------------------------------------------------------*/
156 * AUDIO BUFFERS
158 /*---------------------------------------------------------------------------*/
159 #define AUDIO_FRAGMENT_MANY 32
160 #define PAGES_PER_AUDIO_FRAGMENT 4
161 /*---------------------------------------------------------------------------*/
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.
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 /*---------------------------------------------------------------------------*/
197 * ENUMS
199 /*---------------------------------------------------------------------------*/
200 enum {
201 AT_720x576,
202 AT_704x576,
203 AT_640x480,
204 AT_720x480,
205 AT_360x288,
206 AT_320x240,
207 AT_360x240,
208 RESOLUTION_MANY
210 enum {
211 FMT_UYVY,
212 FMT_YUY2,
213 FMT_RGB24,
214 FMT_RGB32,
215 FMT_BGR24,
216 FMT_BGR32,
217 PIXELFORMAT_MANY
219 enum {
220 FIELD_NONE,
221 FIELD_INTERLACED,
222 INTERLACE_MANY
224 #define SETTINGS_MANY (STANDARD_MANY * \
225 RESOLUTION_MANY * \
226 2 * \
227 PIXELFORMAT_MANY * \
228 INTERLACE_MANY)
229 /*---------------------------------------------------------------------------*/
231 * STRUCTURE DEFINITIONS
233 /*---------------------------------------------------------------------------*/
234 struct easycap_dongle {
235 struct easycap *peasycap;
236 struct mutex mutex_video;
237 struct mutex mutex_audio;
239 /*---------------------------------------------------------------------------*/
240 struct data_buffer {
241 struct list_head list_head;
242 void *pgo;
243 void *pto;
244 u16 kount;
245 u16 input;
247 /*---------------------------------------------------------------------------*/
248 struct data_urb {
249 struct list_head list_head;
250 struct urb *purb;
251 int isbuf;
252 int length;
254 /*---------------------------------------------------------------------------*/
255 struct easycap_standard {
256 u16 mask;
257 struct v4l2_standard v4l2_standard;
259 struct easycap_format {
260 u16 mask;
261 char name[128];
262 struct v4l2_format v4l2_format;
264 struct 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;
280 /*---------------------------------------------------------------------------*/
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
286 /*---------------------------------------------------------------------------*/
287 struct easycap {
288 #define TELLTALE "expectedstring"
289 char telltale[16];
290 int isdongle;
291 int minor;
293 struct video_device video_device;
294 struct v4l2_device v4l2_device;
296 int status;
297 unsigned int audio_pages_per_fragment;
298 unsigned int audio_bytes_per_fragment;
299 unsigned int audio_buffer_page_many;
301 #define UPSAMPLE
302 #ifdef UPSAMPLE
303 s16 oldaudio;
304 #endif /*UPSAMPLE*/
306 int ilk;
307 bool microphone;
309 struct usb_device *pusb_device;
310 struct usb_interface *pusb_interface;
312 struct kref kref;
314 int queued[FRAME_BUFFER_MANY];
315 int done[FRAME_BUFFER_MANY];
317 wait_queue_head_t wq_video;
318 wait_queue_head_t wq_audio;
319 wait_queue_head_t wq_trigger;
321 int input;
322 int polled;
323 int standard_offset;
324 int format_offset;
325 struct inputset inputset[INPUT_MANY];
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];
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;
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;
353 int video_isoc_streaming;
354 int video_isoc_sequence;
355 int video_idle;
356 int video_eof;
357 int video_junk;
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)];
365 struct list_head urb_video_head;
366 struct list_head *purb_video_head;
368 u8 cache[8];
369 u8 *pcache;
370 int video_mt;
371 int audio_mt;
372 long long audio_bytes;
373 u32 isequence;
375 int vma_many;
376 /*---------------------------------------------------------------------------*/
378 * BUFFER INDICATORS
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 /*---------------------------------------------------------------------------*/
395 * IMAGE PROPERTIES
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;
409 int brightness;
410 int contrast;
411 int saturation;
412 int hue;
414 int allocation_video_urb;
415 int allocation_video_page;
416 int allocation_video_struct;
417 int registered_video;
418 /*---------------------------------------------------------------------------*/
420 * ALSA
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 /*---------------------------------------------------------------------------*/
434 * SOUND PROPERTIES
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;
445 int audio_isoc_streaming;
446 int audio_idle;
447 int audio_eof;
448 int volume;
449 int mute;
450 s8 gain;
452 struct data_buffer audio_isoc_buffer[AUDIO_ISOC_BUFFER_MANY];
454 struct list_head urb_audio_head;
455 struct list_head *purb_audio_head;
456 /*---------------------------------------------------------------------------*/
458 * BUFFER INDICATORS
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 /*---------------------------------------------------------------------------*/
468 * SOUND PROPERTIES
470 /*---------------------------------------------------------------------------*/
472 int audio_buffer_many;
474 int allocation_audio_urb;
475 int allocation_audio_page;
476 int allocation_audio_struct;
477 int registered_audio;
479 long long int audio_sample;
480 long long int audio_niveau;
481 long long int audio_square;
483 struct data_buffer audio_buffer[];
485 /*---------------------------------------------------------------------------*/
487 * VIDEO FUNCTION PROTOTYPES
489 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
490 long easycap_unlocked_ioctl(struct file *, unsigned int, unsigned long);
491 int easycap_dqbuf(struct easycap *, int);
492 int submit_video_urbs(struct easycap *);
493 int kill_video_urbs(struct easycap *);
494 int field2frame(struct easycap *);
495 int redaub(struct easycap *, void *, void *,
496 int, int, u8, u8, bool);
497 void easycap_testcard(struct easycap *, int);
498 int fillin_formats(void);
499 int newinput(struct easycap *, int);
500 int adjust_standard(struct easycap *, v4l2_std_id);
501 int adjust_format(struct easycap *, u32, u32, u32,
502 int, bool);
503 int adjust_brightness(struct easycap *, int);
504 int adjust_contrast(struct easycap *, int);
505 int adjust_saturation(struct easycap *, int);
506 int adjust_hue(struct easycap *, int);
507 int adjust_volume(struct easycap *, int);
508 /*---------------------------------------------------------------------------*/
510 * AUDIO FUNCTION PROTOTYPES
512 /*---------------------------------------------------------------------------*/
513 #ifndef CONFIG_EASYCAP_OSS
514 int easycap_alsa_probe(struct easycap *);
515 void easycap_alsa_complete(struct urb *);
516 #else /* CONFIG_EASYCAP_OSS */
517 void easyoss_complete(struct urb *);
518 #endif /* !CONFIG_EASYCAP_OSS */
520 int easycap_sound_setup(struct easycap *);
521 int submit_audio_urbs(struct easycap *);
522 int kill_audio_urbs(struct easycap *);
523 void easyoss_testtone(struct easycap *, int);
524 int audio_setup(struct easycap *);
525 /*---------------------------------------------------------------------------*/
527 * LOW-LEVEL FUNCTION PROTOTYPES
529 /*---------------------------------------------------------------------------*/
530 int audio_gainget(struct usb_device *);
531 int audio_gainset(struct usb_device *, s8);
533 int set_interface(struct usb_device *, u16);
534 int wakeup_device(struct usb_device *);
535 int confirm_resolution(struct usb_device *);
536 int confirm_stream(struct usb_device *);
538 int setup_stk(struct usb_device *, bool);
539 int setup_saa(struct usb_device *, bool);
540 int setup_vt(struct usb_device *);
541 int check_stk(struct usb_device *, bool);
542 int check_saa(struct usb_device *, bool);
543 int ready_saa(struct usb_device *);
544 int merit_saa(struct usb_device *);
545 int check_vt(struct usb_device *);
546 int select_input(struct usb_device *, int, int);
547 int set_resolution(struct usb_device *,
548 u16, u16, u16, u16);
550 int read_saa(struct usb_device *, u16);
551 int read_stk(struct usb_device *, u32);
552 int write_saa(struct usb_device *, u16, u16);
553 int write_000(struct usb_device *, u16, u16);
554 int start_100(struct usb_device *);
555 int stop_100(struct usb_device *);
556 int write_300(struct usb_device *);
557 int read_vt(struct usb_device *, u16);
558 int write_vt(struct usb_device *, u16, u16);
559 int isdongle(struct easycap *);
560 /*---------------------------------------------------------------------------*/
561 struct signed_div_result {
562 long long int quotient;
563 unsigned long long int remainder;
564 } signed_div(long long int, long long int);
567 /*---------------------------------------------------------------------------*/
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.
574 /*---------------------------------------------------------------------------*/
575 const char *strerror(int err);
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)
586 #ifdef CONFIG_EASYCAP_DEBUG
587 extern 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);\
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);\
599 } while (0)
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 */
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))
610 /*---------------------------------------------------------------------------*/
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
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 /*---------------------------------------------------------------------------*/
636 /*---------------------------------------------------------------------------*/
637 /* globals
639 /*---------------------------------------------------------------------------*/
641 extern bool easycap_readback;
642 extern const struct easycap_standard easycap_standard[];
643 extern struct easycap_format easycap_format[];
644 extern struct v4l2_queryctrl easycap_control[];
645 extern struct usb_driver easycap_usb_driver;
646 extern struct easycap_dongle easycapdc60_dongle[];
647 #ifdef CONFIG_EASYCAP_OSS
648 extern struct usb_class_driver easyoss_class;
649 #endif /* !CONFIG_EASYCAP_OSS */
651 #endif /* !__EASYCAP_H__ */