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:
32 * EASYCAP_IS_VIDEODEV_CLIENT
33 * EASYCAP_NEEDS_USBVIDEO_H
34 * EASYCAP_NEEDS_V4L2_DEVICE_H
35 * EASYCAP_NEEDS_V4L2_FOPS
37 * IF REQUIRED THEY MUST BE EXTERNALLY DEFINED, FOR EXAMPLE AS COMPILER
40 /*---------------------------------------------------------------------------*/
45 /*---------------------------------------------------------------------------*/
47 * THESE ARE NORMALLY DEFINED
49 /*---------------------------------------------------------------------------*/
52 /*---------------------------------------------------------------------------*/
54 * THESE ARE FOR MAINTENANCE ONLY - NORMALLY UNDEFINED:
56 /*---------------------------------------------------------------------------*/
57 #undef EASYCAP_TESTCARD
58 #ifdef CONFIG_EASYCAP_OSS
59 #undef EASYCAP_TESTTONE
60 #endif /* CONFIG_EASYCAP_OSS */
61 /*---------------------------------------------------------------------------*/
62 #include <linux/kernel.h>
63 #include <linux/errno.h>
64 #include <linux/init.h>
65 #include <linux/slab.h>
66 #include <linux/module.h>
67 #include <linux/kref.h>
68 #include <linux/usb.h>
69 #include <linux/uaccess.h>
71 #include <linux/i2c.h>
72 #include <linux/version.h>
73 #include <linux/workqueue.h>
74 #include <linux/poll.h>
77 #include <linux/delay.h>
78 #include <linux/types.h>
80 #ifndef CONFIG_EASYCAP_OSS
81 #include <linux/vmalloc.h>
82 #include <linux/sound.h>
83 #include <sound/core.h>
84 #include <sound/pcm.h>
85 #include <sound/pcm_params.h>
86 #include <sound/info.h>
87 #include <sound/initval.h>
88 #include <sound/control.h>
89 #endif /* !CONFIG_EASYCAP_OSS */
90 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
91 #ifdef EASYCAP_IS_VIDEODEV_CLIENT
92 #include <media/v4l2-dev.h>
93 #ifdef EASYCAP_NEEDS_V4L2_DEVICE_H
94 #include <media/v4l2-device.h>
95 #endif /*EASYCAP_NEEDS_V4L2_DEVICE_H*/
96 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
97 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
98 #include <linux/videodev2.h>
99 #include <linux/soundcard.h>
100 #ifdef EASYCAP_NEEDS_USBVIDEO_H
101 #include <config/video/usbvideo.h>
102 #endif /*EASYCAP_NEEDS_USBVIDEO_H*/
105 #error "PAGE_SIZE not defined"
106 #endif /* PAGE_SIZE */
108 /*---------------------------------------------------------------------------*/
109 /* VENDOR, PRODUCT: Syntek Semiconductor Co., Ltd
111 * EITHER EasyCAP USB 2.0 Video Adapter with Audio, Model No. DC60
112 * with input cabling: AUDIO(L), AUDIO(R), CVBS, S-VIDEO.
114 * OR EasyCAP 4CHANNEL USB 2.0 DVR, Model No. EasyCAP002
115 * with input cabling: MICROPHONE, CVBS1, CVBS2, CVBS3, CVBS4.
117 /*---------------------------------------------------------------------------*/
118 #define USB_EASYCAP_VENDOR_ID 0x05e1
119 #define USB_EASYCAP_PRODUCT_ID 0x0408
121 #define EASYCAP_DRIVER_VERSION "0.9.01"
122 #define EASYCAP_DRIVER_DESCRIPTION "easycapdc60"
124 #define USB_SKEL_MINOR_BASE 192
125 #define DONGLE_MANY 8
127 /*---------------------------------------------------------------------------*/
129 * DEFAULT LUMINANCE, CONTRAST, SATURATION AND HUE
131 /*---------------------------------------------------------------------------*/
132 #define SAA_0A_DEFAULT 0x7F
133 #define SAA_0B_DEFAULT 0x3F
134 #define SAA_0C_DEFAULT 0x2F
135 #define SAA_0D_DEFAULT 0x00
136 /*---------------------------------------------------------------------------*/
138 * VIDEO STREAMING PARAMETERS:
139 * USB 2.0 PROVIDES FOR HIGH-BANDWIDTH ENDPOINTS WITH AN UPPER LIMIT
140 * OF 3072 BYTES PER MICROFRAME for wMaxPacketSize.
142 /*---------------------------------------------------------------------------*/
143 #define VIDEO_ISOC_BUFFER_MANY 16
144 #define VIDEO_ISOC_ORDER 3
145 #define VIDEO_ISOC_FRAMESPERDESC ((unsigned int) 1 << VIDEO_ISOC_ORDER)
146 #define USB_2_0_MAXPACKETSIZE 3072
147 #if (USB_2_0_MAXPACKETSIZE > PAGE_SIZE)
148 #error video_isoc_buffer[.] will not be big enough
150 #define VIDEO_JUNK_TOLERATE VIDEO_ISOC_BUFFER_MANY
151 #define VIDEO_LOST_TOLERATE 50
152 /*---------------------------------------------------------------------------*/
156 /*---------------------------------------------------------------------------*/
157 #define FIELD_BUFFER_SIZE (203 * PAGE_SIZE)
158 #define FRAME_BUFFER_SIZE (405 * PAGE_SIZE)
159 #define FIELD_BUFFER_MANY 4
160 #define FRAME_BUFFER_MANY 6
161 /*---------------------------------------------------------------------------*/
163 * AUDIO STREAMING PARAMETERS
165 /*---------------------------------------------------------------------------*/
166 #define AUDIO_ISOC_BUFFER_MANY 16
167 #define AUDIO_ISOC_ORDER 1
168 #define AUDIO_ISOC_FRAMESPERDESC 32
169 #define AUDIO_ISOC_BUFFER_SIZE (PAGE_SIZE << AUDIO_ISOC_ORDER)
170 /*---------------------------------------------------------------------------*/
174 /*---------------------------------------------------------------------------*/
175 #define AUDIO_FRAGMENT_MANY 32
176 #define PAGES_PER_AUDIO_FRAGMENT 4
177 /*---------------------------------------------------------------------------*/
179 * IT IS ESSENTIAL THAT EVEN-NUMBERED STANDARDS ARE 25 FRAMES PER SECOND,
180 * ODD-NUMBERED STANDARDS ARE 30 FRAMES PER SECOND.
181 * THE NUMBERING OF STANDARDS MUST NOT BE CHANGED WITHOUT DUE CARE. NOT
182 * ONLY MUST THE PARAMETER
184 * BE CHANGED TO CORRESPOND TO THE NEW NUMBER OF STANDARDS, BUT ALSO THE
185 * NUMBERING MUST REMAIN AN UNBROKEN ASCENDING SEQUENCE: DUMMY STANDARDS
186 * MAY NEED TO BE ADDED. APPROPRIATE CHANGES WILL ALWAYS BE REQUIRED IN
187 * ROUTINE fillin_formats() AND POSSIBLY ELSEWHERE. BEWARE.
189 /*---------------------------------------------------------------------------*/
200 #define PAL_BGHIN_SLOW 10
201 #define PAL_Nc_SLOW 12
202 #define SECAM_SLOW 14
203 #define NTSC_N_SLOW 16
204 #define NTSC_N_443_SLOW 18
205 #define NTSC_M_SLOW 11
206 #define NTSC_443_SLOW 13
207 #define NTSC_M_JP_SLOW 15
208 #define PAL_60_SLOW 17
209 #define PAL_M_SLOW 19
210 #define STANDARD_MANY 20
211 /*---------------------------------------------------------------------------*/
215 /*---------------------------------------------------------------------------*/
240 #define SETTINGS_MANY (STANDARD_MANY * \
245 /*---------------------------------------------------------------------------*/
247 * STRUCTURE DEFINITIONS
249 /*---------------------------------------------------------------------------*/
250 struct easycap_dongle
{
251 struct easycap
*peasycap
;
252 struct mutex mutex_video
;
253 struct mutex mutex_audio
;
255 /*---------------------------------------------------------------------------*/
257 struct list_head list_head
;
263 /*---------------------------------------------------------------------------*/
265 struct list_head list_head
;
270 /*---------------------------------------------------------------------------*/
271 struct easycap_standard
{
273 struct v4l2_standard v4l2_standard
;
275 struct easycap_format
{
278 struct v4l2_format v4l2_format
;
284 int standard_offset_ok
;
286 int format_offset_ok
;
296 /*---------------------------------------------------------------------------*/
298 * easycap.ilk == 0 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=256
299 * easycap.ilk == 2 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=9
300 * easycap.ilk == 3 => FOUR-CVBS HARDWARE, AUDIO wMaxPacketSize=9
302 /*---------------------------------------------------------------------------*/
304 #define TELLTALE "expectedstring"
309 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
310 #ifdef EASYCAP_IS_VIDEODEV_CLIENT
311 struct video_device video_device
;
312 #ifdef EASYCAP_NEEDS_V4L2_DEVICE_H
313 struct v4l2_device v4l2_device
;
314 #endif /*EASYCAP_NEEDS_V4L2_DEVICE_H*/
315 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
316 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
318 unsigned int audio_pages_per_fragment
;
319 unsigned int audio_bytes_per_fragment
;
320 unsigned int audio_buffer_page_many
;
330 struct usb_device
*pusb_device
;
331 struct usb_interface
*pusb_interface
;
335 int queued
[FRAME_BUFFER_MANY
];
336 int done
[FRAME_BUFFER_MANY
];
338 wait_queue_head_t wq_video
;
339 wait_queue_head_t wq_audio
;
340 wait_queue_head_t wq_trigger
;
346 struct inputset inputset
[INPUT_MANY
];
354 int lost
[INPUT_MANY
];
357 struct timeval timeval0
;
358 struct timeval timeval1
;
359 struct timeval timeval2
;
360 struct timeval timeval3
;
361 struct timeval timeval6
;
362 struct timeval timeval7
;
363 struct timeval timeval8
;
364 long long int dnbydt
;
367 int video_altsetting_on
;
368 int video_altsetting_off
;
369 int video_endpointnumber
;
370 int video_isoc_maxframesize
;
371 int video_isoc_buffer_size
;
372 int video_isoc_framesperdesc
;
374 int video_isoc_streaming
;
375 int video_isoc_sequence
;
380 struct data_buffer video_isoc_buffer
[VIDEO_ISOC_BUFFER_MANY
];
381 struct data_buffer field_buffer
[FIELD_BUFFER_MANY
]
382 [(FIELD_BUFFER_SIZE
/PAGE_SIZE
)];
383 struct data_buffer frame_buffer
[FRAME_BUFFER_MANY
]
384 [(FRAME_BUFFER_SIZE
/PAGE_SIZE
)];
386 struct list_head urb_video_head
;
387 struct list_head
*purb_video_head
;
393 long long audio_bytes
;
397 /*---------------------------------------------------------------------------*/
401 /*---------------------------------------------------------------------------*/
402 int field_fill
; /* Field buffer being filled by easycap_complete(). */
403 /* Bumped only by easycap_complete(). */
404 int field_page
; /* Page of field buffer page being filled by */
405 /* easycap_complete(). */
406 int field_read
; /* Field buffer to be read by field2frame(). */
407 /* Bumped only by easycap_complete(). */
408 int frame_fill
; /* Frame buffer being filled by field2frame(). */
409 /* Bumped only by easycap_dqbuf() when */
410 /* field2frame() has created a complete frame. */
411 int frame_read
; /* Frame buffer offered to user by DQBUF. */
412 /* Set only by easycap_dqbuf() to trail frame_fill.*/
413 int frame_lock
; /* Flag set to 1 by DQBUF and cleared by QBUF */
414 /*---------------------------------------------------------------------------*/
418 /*---------------------------------------------------------------------------*/
426 int frame_buffer_used
;
427 int frame_buffer_many
;
428 int videofieldamount
;
435 int allocation_video_urb
;
436 int allocation_video_page
;
437 int allocation_video_struct
;
438 int registered_video
;
439 /*---------------------------------------------------------------------------*/
443 /*---------------------------------------------------------------------------*/
444 #ifndef CONFIG_EASYCAP_OSS
445 struct snd_pcm_hardware alsa_hardware
;
446 struct snd_card
*psnd_card
;
447 struct snd_pcm
*psnd_pcm
;
448 struct snd_pcm_substream
*psubstream
;
452 #endif /* !CONFIG_EASYCAP_OSS */
453 /*---------------------------------------------------------------------------*/
457 /*---------------------------------------------------------------------------*/
459 int audio_altsetting_on
;
460 int audio_altsetting_off
;
461 int audio_endpointnumber
;
462 int audio_isoc_maxframesize
;
463 int audio_isoc_buffer_size
;
464 int audio_isoc_framesperdesc
;
466 int audio_isoc_streaming
;
473 struct data_buffer audio_isoc_buffer
[AUDIO_ISOC_BUFFER_MANY
];
475 struct list_head urb_audio_head
;
476 struct list_head
*purb_audio_head
;
477 /*---------------------------------------------------------------------------*/
481 /*---------------------------------------------------------------------------*/
482 int audio_fill
; /* Audio buffer being filled by easycap_complete(). */
483 /* Bumped only by easycap_complete(). */
484 int audio_read
; /* Audio buffer page being read by easycap_read(). */
485 /* Set by easycap_read() to trail audio_fill by */
487 /*---------------------------------------------------------------------------*/
491 /*---------------------------------------------------------------------------*/
493 int audio_buffer_many
;
495 int allocation_audio_urb
;
496 int allocation_audio_page
;
497 int allocation_audio_struct
;
498 int registered_audio
;
500 long long int audio_sample
;
501 long long int audio_niveau
;
502 long long int audio_square
;
504 struct data_buffer audio_buffer
[];
506 /*---------------------------------------------------------------------------*/
508 * VIDEO FUNCTION PROTOTYPES
510 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
511 long easycap_unlocked_ioctl(struct file
*, unsigned int, unsigned long);
512 int easycap_dqbuf(struct easycap
*, int);
513 int submit_video_urbs(struct easycap
*);
514 int kill_video_urbs(struct easycap
*);
515 int field2frame(struct easycap
*);
516 int redaub(struct easycap
*, void *, void *,
517 int, int, u8
, u8
, bool);
518 void easycap_testcard(struct easycap
*, int);
519 int fillin_formats(void);
520 int newinput(struct easycap
*, int);
521 int adjust_standard(struct easycap
*, v4l2_std_id
);
522 int adjust_format(struct easycap
*, u32
, u32
, u32
,
524 int adjust_brightness(struct easycap
*, int);
525 int adjust_contrast(struct easycap
*, int);
526 int adjust_saturation(struct easycap
*, int);
527 int adjust_hue(struct easycap
*, int);
528 int adjust_volume(struct easycap
*, int);
529 /*---------------------------------------------------------------------------*/
531 * AUDIO FUNCTION PROTOTYPES
533 /*---------------------------------------------------------------------------*/
534 #ifndef CONFIG_EASYCAP_OSS
535 int easycap_alsa_probe(struct easycap
*);
536 void easycap_alsa_complete(struct urb
*);
537 #else /* CONFIG_EASYCAP_OSS */
538 void easyoss_complete(struct urb
*);
539 #endif /* !CONFIG_EASYCAP_OSS */
541 int easycap_sound_setup(struct easycap
*);
542 int submit_audio_urbs(struct easycap
*);
543 int kill_audio_urbs(struct easycap
*);
544 void easyoss_testtone(struct easycap
*, int);
545 int audio_setup(struct easycap
*);
546 /*---------------------------------------------------------------------------*/
548 * LOW-LEVEL FUNCTION PROTOTYPES
550 /*---------------------------------------------------------------------------*/
551 int audio_gainget(struct usb_device
*);
552 int audio_gainset(struct usb_device
*, s8
);
554 int set_interface(struct usb_device
*, u16
);
555 int wakeup_device(struct usb_device
*);
556 int confirm_resolution(struct usb_device
*);
557 int confirm_stream(struct usb_device
*);
559 int setup_stk(struct usb_device
*, bool);
560 int setup_saa(struct usb_device
*, bool);
561 int setup_vt(struct usb_device
*);
562 int check_stk(struct usb_device
*, bool);
563 int check_saa(struct usb_device
*, bool);
564 int ready_saa(struct usb_device
*);
565 int merit_saa(struct usb_device
*);
566 int check_vt(struct usb_device
*);
567 int select_input(struct usb_device
*, int, int);
568 int set_resolution(struct usb_device
*,
571 int read_saa(struct usb_device
*, u16
);
572 int read_stk(struct usb_device
*, u32
);
573 int write_saa(struct usb_device
*, u16
, u16
);
574 int wait_i2c(struct usb_device
*);
575 int write_000(struct usb_device
*, u16
, u16
);
576 int start_100(struct usb_device
*);
577 int stop_100(struct usb_device
*);
578 int write_300(struct usb_device
*);
579 int read_vt(struct usb_device
*, u16
);
580 int write_vt(struct usb_device
*, u16
, u16
);
581 int regset(struct usb_device
*, u16
, u16
);
582 int regget(struct usb_device
*, u16
, void *);
583 int isdongle(struct easycap
*);
584 /*---------------------------------------------------------------------------*/
585 struct signed_div_result
{
586 long long int quotient
;
587 unsigned long long int remainder
;
588 } signed_div(long long int, long long int);
591 /*---------------------------------------------------------------------------*/
595 /*---------------------------------------------------------------------------*/
596 #define GET(X, Y, Z) do { \
599 __rc = regget(X, Y, Z); \
601 JOT(8, ":-(%i\n", __LINE__); return __rc; \
605 #define SET(X, Y, Z) do { \
607 __rc = regset(X, Y, Z); \
609 JOT(8, ":-(%i\n", __LINE__); return __rc; \
612 /*---------------------------------------------------------------------------*/
614 * MACROS SAM(...) AND JOM(...) ALLOW DIAGNOSTIC OUTPUT TO BE TAGGED WITH
615 * THE IDENTITY OF THE DONGLE TO WHICH IT APPLIES, BUT IF INVOKED WHEN THE
616 * POINTER peasycap IS INVALID AN Oops IS LIKELY, AND ITS CAUSE MAY NOT BE
617 * IMMEDIATELY OBVIOUS FROM A CASUAL READING OF THE SOURCE CODE. BEWARE.
619 /*---------------------------------------------------------------------------*/
620 const char *strerror(int err
);
622 #define SAY(format, args...) do { \
623 printk(KERN_DEBUG "easycap:: %s: " \
624 format, __func__, ##args); \
626 #define SAM(format, args...) do { \
627 printk(KERN_DEBUG "easycap::%i%s: " \
628 format, peasycap->isdongle, __func__, ##args);\
631 #ifdef CONFIG_EASYCAP_DEBUG
632 extern int easycap_debug
;
633 #define JOT(n, format, args...) do { \
634 if (n <= easycap_debug) { \
635 printk(KERN_DEBUG "easycap:: %s: " \
636 format, __func__, ##args);\
639 #define JOM(n, format, args...) do { \
640 if (n <= easycap_debug) { \
641 printk(KERN_DEBUG "easycap::%i%s: " \
642 format, peasycap->isdongle, __func__, ##args);\
647 #define JOT(n, format, args...) do {} while (0)
648 #define JOM(n, format, args...) do {} while (0)
649 #endif /* CONFIG_EASYCAP_DEBUG */
651 #define MICROSECONDS(X, Y) \
652 ((1000000*((long long int)(X.tv_sec - Y.tv_sec))) + \
653 (long long int)(X.tv_usec - Y.tv_usec))
655 /*---------------------------------------------------------------------------*/
657 * (unsigned char *)P pointer to next byte pair
658 * (long int *)X pointer to accumulating count
659 * (long int *)Y pointer to accumulating sum
660 * (long long int *)Z pointer to accumulating sum of squares
662 /*---------------------------------------------------------------------------*/
663 #define SUMMER(P, X, Y, Z) do { \
665 unsigned int u0, u1, u2; \
667 p = (unsigned char *)(P); \
668 u0 = (unsigned int) (*p); \
669 u1 = (unsigned int) (*(p + 1)); \
670 u2 = (unsigned int) ((u1 << 8) | u0); \
672 s = -(long int)(0x7FFF & (~u2)); \
674 s = (long int)(0x7FFF & u2); \
675 *((X)) += (long int) 1; \
676 *((Y)) += (long int) s; \
677 *((Z)) += ((long long int)(s) * (long long int)(s)); \
679 /*---------------------------------------------------------------------------*/
681 /*---------------------------------------------------------------------------*/
684 /*---------------------------------------------------------------------------*/
686 extern const struct easycap_standard easycap_standard
[];
687 extern struct easycap_format easycap_format
[];
688 extern struct v4l2_queryctrl easycap_control
[];
689 extern struct usb_driver easycap_usb_driver
;
690 extern struct easycap_dongle easycapdc60_dongle
[];
691 #ifdef CONFIG_EASYCAP_OSS
692 extern struct usb_class_driver easyoss_class
;
693 #endif /* !CONFIG_EASYCAP_OSS */
695 #endif /* !__EASYCAP_H__ */