1 /*****************************************************************************
5 *****************************************************************************/
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:
33 * IF REQUIRED THEY MUST BE EXTERNALLY DEFINED, FOR EXAMPLE AS COMPILER
36 /*---------------------------------------------------------------------------*/
41 /*---------------------------------------------------------------------------*/
43 * THESE ARE NORMALLY DEFINED
45 /*---------------------------------------------------------------------------*/
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>
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>
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
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
134 #define VIDEO_JUNK_TOLERATE VIDEO_ISOC_BUFFER_MANY
135 #define VIDEO_LOST_TOLERATE 50
136 /*---------------------------------------------------------------------------*/
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 /*---------------------------------------------------------------------------*/
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
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 /*---------------------------------------------------------------------------*/
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 /*---------------------------------------------------------------------------*/
199 /*---------------------------------------------------------------------------*/
224 #define SETTINGS_MANY (STANDARD_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 /*---------------------------------------------------------------------------*/
241 struct list_head list_head
;
247 /*---------------------------------------------------------------------------*/
249 struct list_head list_head
;
254 /*---------------------------------------------------------------------------*/
255 struct easycap_standard
{
257 struct v4l2_standard v4l2_standard
;
259 struct easycap_format
{
262 struct v4l2_format v4l2_format
;
268 int standard_offset_ok
;
270 int format_offset_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 /*---------------------------------------------------------------------------*/
288 #define TELLTALE "expectedstring"
293 struct video_device video_device
;
294 struct v4l2_device v4l2_device
;
297 unsigned int audio_pages_per_fragment
;
298 unsigned int audio_bytes_per_fragment
;
299 unsigned int audio_buffer_page_many
;
309 struct usb_device
*pusb_device
;
310 struct usb_interface
*pusb_interface
;
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
;
325 struct inputset inputset
[INPUT_MANY
];
333 int lost
[INPUT_MANY
];
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
;
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
;
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
;
372 long long audio_bytes
;
376 /*---------------------------------------------------------------------------*/
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 /*---------------------------------------------------------------------------*/
397 /*---------------------------------------------------------------------------*/
405 int frame_buffer_used
;
406 int frame_buffer_many
;
407 int videofieldamount
;
414 int allocation_video_urb
;
415 int allocation_video_page
;
416 int allocation_video_struct
;
417 int registered_video
;
418 /*---------------------------------------------------------------------------*/
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
;
431 #endif /* !CONFIG_EASYCAP_OSS */
432 /*---------------------------------------------------------------------------*/
436 /*---------------------------------------------------------------------------*/
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
;
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 /*---------------------------------------------------------------------------*/
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 */
466 /*---------------------------------------------------------------------------*/
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
,
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
*,
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); \
581 #define SAM(format, args...) do { \
582 printk(KERN_DEBUG "easycap::%i%s: " \
583 format, peasycap->isdongle, __func__, ##args);\
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);\
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);\
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 { \
620 unsigned int u0, u1, u2; \
622 p = (unsigned char *)(P); \
623 u0 = (unsigned int) (*p); \
624 u1 = (unsigned int) (*(p + 1)); \
625 u2 = (unsigned int) ((u1 << 8) | u0); \
627 s = -(long int)(0x7FFF & (~u2)); \
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)); \
634 /*---------------------------------------------------------------------------*/
636 /*---------------------------------------------------------------------------*/
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__ */