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
38 * IF REQUIRED THEY MUST BE EXTERNALLY DEFINED, FOR EXAMPLE AS COMPILER
41 /*---------------------------------------------------------------------------*/
46 /*---------------------------------------------------------------------------*/
48 * THESE ARE NORMALLY DEFINED
50 /*---------------------------------------------------------------------------*/
54 /*---------------------------------------------------------------------------*/
56 * THESE ARE FOR MAINTENANCE ONLY - NORMALLY UNDEFINED:
58 /*---------------------------------------------------------------------------*/
59 #undef EASYCAP_TESTCARD
60 #ifdef CONFIG_EASYCAP_OSS
61 #undef EASYCAP_TESTTONE
62 #endif /* CONFIG_EASYCAP_OSS */
63 /*---------------------------------------------------------------------------*/
64 #include <linux/kernel.h>
65 #include <linux/errno.h>
66 #include <linux/init.h>
67 #include <linux/slab.h>
68 #include <linux/module.h>
69 #include <linux/kref.h>
70 #include <linux/usb.h>
71 #include <linux/uaccess.h>
73 #include <linux/i2c.h>
74 #include <linux/version.h>
75 #include <linux/workqueue.h>
76 #include <linux/poll.h>
79 #include <linux/delay.h>
80 #include <linux/types.h>
82 #ifndef CONFIG_EASYCAP_OSS
83 #include <linux/vmalloc.h>
84 #include <linux/sound.h>
85 #include <sound/core.h>
86 #include <sound/pcm.h>
87 #include <sound/pcm_params.h>
88 #include <sound/info.h>
89 #include <sound/initval.h>
90 #include <sound/control.h>
91 #endif /* !CONFIG_EASYCAP_OSS */
92 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
93 #if defined(EASYCAP_IS_VIDEODEV_CLIENT)
94 #include <media/v4l2-dev.h>
95 #if defined(EASYCAP_NEEDS_V4L2_DEVICE_H)
96 #include <media/v4l2-device.h>
97 #endif /*EASYCAP_NEEDS_V4L2_DEVICE_H*/
98 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
99 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
100 #include <linux/videodev2.h>
101 #include <linux/soundcard.h>
102 #if defined(EASYCAP_NEEDS_USBVIDEO_H)
103 #include <config/video/usbvideo.h>
104 #endif /*EASYCAP_NEEDS_USBVIDEO_H*/
106 #if (!defined(PAGE_SIZE))
107 #error "PAGE_SIZE not defined"
110 /*---------------------------------------------------------------------------*/
111 /* VENDOR, PRODUCT: Syntek Semiconductor Co., Ltd
113 * EITHER EasyCAP USB 2.0 Video Adapter with Audio, Model No. DC60
114 * with input cabling: AUDIO(L), AUDIO(R), CVBS, S-VIDEO.
116 * OR EasyCAP 4CHANNEL USB 2.0 DVR, Model No. EasyCAP002
117 * with input cabling: MICROPHONE, CVBS1, CVBS2, CVBS3, CVBS4.
119 /*---------------------------------------------------------------------------*/
120 #define USB_EASYCAP_VENDOR_ID 0x05e1
121 #define USB_EASYCAP_PRODUCT_ID 0x0408
123 #define EASYCAP_DRIVER_VERSION "0.9.01"
124 #define EASYCAP_DRIVER_DESCRIPTION "easycapdc60"
126 #define USB_SKEL_MINOR_BASE 192
127 #define DONGLE_MANY 8
129 /*---------------------------------------------------------------------------*/
131 * DEFAULT LUMINANCE, CONTRAST, SATURATION AND HUE
133 /*---------------------------------------------------------------------------*/
134 #define SAA_0A_DEFAULT 0x7F
135 #define SAA_0B_DEFAULT 0x3F
136 #define SAA_0C_DEFAULT 0x2F
137 #define SAA_0D_DEFAULT 0x00
138 /*---------------------------------------------------------------------------*/
140 * VIDEO STREAMING PARAMETERS:
141 * USB 2.0 PROVIDES FOR HIGH-BANDWIDTH ENDPOINTS WITH AN UPPER LIMIT
142 * OF 3072 BYTES PER MICROFRAME for wMaxPacketSize.
144 /*---------------------------------------------------------------------------*/
145 #define VIDEO_ISOC_BUFFER_MANY 16
146 #define VIDEO_ISOC_ORDER 3
147 #define VIDEO_ISOC_FRAMESPERDESC ((unsigned int) 1 << VIDEO_ISOC_ORDER)
148 #define USB_2_0_MAXPACKETSIZE 3072
149 #if (USB_2_0_MAXPACKETSIZE > PAGE_SIZE)
150 #error video_isoc_buffer[.] will not be big enough
152 #define VIDEO_JUNK_TOLERATE VIDEO_ISOC_BUFFER_MANY
153 #define VIDEO_LOST_TOLERATE 50
154 /*---------------------------------------------------------------------------*/
158 /*---------------------------------------------------------------------------*/
159 #define FIELD_BUFFER_SIZE (203 * PAGE_SIZE)
160 #define FRAME_BUFFER_SIZE (405 * PAGE_SIZE)
161 #define FIELD_BUFFER_MANY 4
162 #define FRAME_BUFFER_MANY 6
163 /*---------------------------------------------------------------------------*/
165 * AUDIO STREAMING PARAMETERS
167 /*---------------------------------------------------------------------------*/
168 #define AUDIO_ISOC_BUFFER_MANY 16
169 #define AUDIO_ISOC_ORDER 1
170 #define AUDIO_ISOC_FRAMESPERDESC 32
171 #define AUDIO_ISOC_BUFFER_SIZE (PAGE_SIZE << AUDIO_ISOC_ORDER)
172 /*---------------------------------------------------------------------------*/
176 /*---------------------------------------------------------------------------*/
177 #define AUDIO_FRAGMENT_MANY 32
178 #define PAGES_PER_AUDIO_FRAGMENT 4
179 /*---------------------------------------------------------------------------*/
181 * IT IS ESSENTIAL THAT EVEN-NUMBERED STANDARDS ARE 25 FRAMES PER SECOND,
182 * ODD-NUMBERED STANDARDS ARE 30 FRAMES PER SECOND.
183 * THE NUMBERING OF STANDARDS MUST NOT BE CHANGED WITHOUT DUE CARE. NOT
184 * ONLY MUST THE PARAMETER
186 * BE CHANGED TO CORRESPOND TO THE NEW NUMBER OF STANDARDS, BUT ALSO THE
187 * NUMBERING MUST REMAIN AN UNBROKEN ASCENDING SEQUENCE: DUMMY STANDARDS
188 * MAY NEED TO BE ADDED. APPROPRIATE CHANGES WILL ALWAYS BE REQUIRED IN
189 * ROUTINE fillin_formats() AND POSSIBLY ELSEWHERE. BEWARE.
191 /*---------------------------------------------------------------------------*/
202 #define PAL_BGHIN_SLOW 10
203 #define PAL_Nc_SLOW 12
204 #define SECAM_SLOW 14
205 #define NTSC_N_SLOW 16
206 #define NTSC_N_443_SLOW 18
207 #define NTSC_M_SLOW 11
208 #define NTSC_443_SLOW 13
209 #define NTSC_M_JP_SLOW 15
210 #define PAL_60_SLOW 17
211 #define PAL_M_SLOW 19
212 #define STANDARD_MANY 20
213 /*---------------------------------------------------------------------------*/
217 /*---------------------------------------------------------------------------*/
242 #define SETTINGS_MANY (STANDARD_MANY * \
247 /*---------------------------------------------------------------------------*/
249 * STRUCTURE DEFINITIONS
251 /*---------------------------------------------------------------------------*/
252 struct easycap_dongle
{
253 struct easycap
*peasycap
;
254 struct mutex mutex_video
;
255 struct mutex mutex_audio
;
257 /*---------------------------------------------------------------------------*/
259 struct list_head list_head
;
265 /*---------------------------------------------------------------------------*/
267 struct list_head list_head
;
272 /*---------------------------------------------------------------------------*/
273 struct easycap_standard
{
275 struct v4l2_standard v4l2_standard
;
277 struct easycap_format
{
280 struct v4l2_format v4l2_format
;
286 int standard_offset_ok
;
288 int format_offset_ok
;
298 /*---------------------------------------------------------------------------*/
300 * easycap.ilk == 0 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=256
301 * easycap.ilk == 2 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=9
302 * easycap.ilk == 3 => FOUR-CVBS HARDWARE, AUDIO wMaxPacketSize=9
304 /*---------------------------------------------------------------------------*/
306 #define TELLTALE "expectedstring"
311 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
312 #if defined(EASYCAP_IS_VIDEODEV_CLIENT)
313 struct video_device video_device
;
314 #if defined(EASYCAP_NEEDS_V4L2_DEVICE_H)
315 struct v4l2_device v4l2_device
;
316 #endif /*EASYCAP_NEEDS_V4L2_DEVICE_H*/
317 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
318 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
320 unsigned int audio_pages_per_fragment
;
321 unsigned int audio_bytes_per_fragment
;
322 unsigned int audio_buffer_page_many
;
325 #if defined(UPSAMPLE)
332 struct usb_device
*pusb_device
;
333 struct usb_interface
*pusb_interface
;
337 int queued
[FRAME_BUFFER_MANY
];
338 int done
[FRAME_BUFFER_MANY
];
340 wait_queue_head_t wq_video
;
341 wait_queue_head_t wq_audio
;
342 wait_queue_head_t wq_trigger
;
348 struct inputset inputset
[INPUT_MANY
];
356 int lost
[INPUT_MANY
];
359 struct timeval timeval0
;
360 struct timeval timeval1
;
361 struct timeval timeval2
;
362 struct timeval timeval3
;
363 struct timeval timeval6
;
364 struct timeval timeval7
;
365 struct timeval timeval8
;
366 long long int dnbydt
;
369 int video_altsetting_on
;
370 int video_altsetting_off
;
371 int video_endpointnumber
;
372 int video_isoc_maxframesize
;
373 int video_isoc_buffer_size
;
374 int video_isoc_framesperdesc
;
376 int video_isoc_streaming
;
377 int video_isoc_sequence
;
382 struct data_buffer video_isoc_buffer
[VIDEO_ISOC_BUFFER_MANY
];
383 struct data_buffer field_buffer
[FIELD_BUFFER_MANY
]
384 [(FIELD_BUFFER_SIZE
/PAGE_SIZE
)];
385 struct data_buffer frame_buffer
[FRAME_BUFFER_MANY
]
386 [(FRAME_BUFFER_SIZE
/PAGE_SIZE
)];
388 struct list_head urb_video_head
;
389 struct list_head
*purb_video_head
;
395 long long audio_bytes
;
399 /*---------------------------------------------------------------------------*/
403 /*---------------------------------------------------------------------------*/
404 int field_fill
; /* Field buffer being filled by easycap_complete(). */
405 /* Bumped only by easycap_complete(). */
406 int field_page
; /* Page of field buffer page being filled by */
407 /* easycap_complete(). */
408 int field_read
; /* Field buffer to be read by field2frame(). */
409 /* Bumped only by easycap_complete(). */
410 int frame_fill
; /* Frame buffer being filled by field2frame(). */
411 /* Bumped only by easycap_dqbuf() when */
412 /* field2frame() has created a complete frame. */
413 int frame_read
; /* Frame buffer offered to user by DQBUF. */
414 /* Set only by easycap_dqbuf() to trail frame_fill.*/
415 int frame_lock
; /* Flag set to 1 by DQBUF and cleared by QBUF */
416 /*---------------------------------------------------------------------------*/
420 /*---------------------------------------------------------------------------*/
428 int frame_buffer_used
;
429 int frame_buffer_many
;
430 int videofieldamount
;
437 int allocation_video_urb
;
438 int allocation_video_page
;
439 int allocation_video_struct
;
440 int registered_video
;
441 /*---------------------------------------------------------------------------*/
445 /*---------------------------------------------------------------------------*/
446 #ifndef CONFIG_EASYCAP_OSS
447 struct snd_pcm_hardware alsa_hardware
;
448 struct snd_card
*psnd_card
;
449 struct snd_pcm
*psnd_pcm
;
450 struct snd_pcm_substream
*psubstream
;
454 #endif /* !CONFIG_EASYCAP_OSS */
455 /*---------------------------------------------------------------------------*/
459 /*---------------------------------------------------------------------------*/
461 int audio_altsetting_on
;
462 int audio_altsetting_off
;
463 int audio_endpointnumber
;
464 int audio_isoc_maxframesize
;
465 int audio_isoc_buffer_size
;
466 int audio_isoc_framesperdesc
;
468 int audio_isoc_streaming
;
475 struct data_buffer audio_isoc_buffer
[AUDIO_ISOC_BUFFER_MANY
];
477 struct list_head urb_audio_head
;
478 struct list_head
*purb_audio_head
;
479 /*---------------------------------------------------------------------------*/
483 /*---------------------------------------------------------------------------*/
484 int audio_fill
; /* Audio buffer being filled by easycap_complete(). */
485 /* Bumped only by easycap_complete(). */
486 int audio_read
; /* Audio buffer page being read by easycap_read(). */
487 /* Set by easycap_read() to trail audio_fill by */
489 /*---------------------------------------------------------------------------*/
493 /*---------------------------------------------------------------------------*/
495 int audio_buffer_many
;
497 int allocation_audio_urb
;
498 int allocation_audio_page
;
499 int allocation_audio_struct
;
500 int registered_audio
;
502 long long int audio_sample
;
503 long long int audio_niveau
;
504 long long int audio_square
;
506 struct data_buffer audio_buffer
[];
508 /*---------------------------------------------------------------------------*/
510 * VIDEO FUNCTION PROTOTYPES
512 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
513 long easycap_unlocked_ioctl(struct file
*, unsigned int, unsigned long);
514 int easycap_dqbuf(struct easycap
*, int);
515 int submit_video_urbs(struct easycap
*);
516 int kill_video_urbs(struct easycap
*);
517 int field2frame(struct easycap
*);
518 int redaub(struct easycap
*, void *, void *,
519 int, int, __u8
, __u8
, bool);
520 void easycap_testcard(struct easycap
*, int);
521 int fillin_formats(void);
522 int newinput(struct easycap
*, int);
523 int adjust_standard(struct easycap
*, v4l2_std_id
);
524 int adjust_format(struct easycap
*, __u32
, __u32
, __u32
,
526 int adjust_brightness(struct easycap
*, int);
527 int adjust_contrast(struct easycap
*, int);
528 int adjust_saturation(struct easycap
*, int);
529 int adjust_hue(struct easycap
*, int);
530 int adjust_volume(struct easycap
*, int);
531 /*---------------------------------------------------------------------------*/
533 * AUDIO FUNCTION PROTOTYPES
535 /*---------------------------------------------------------------------------*/
536 #ifndef CONFIG_EASYCAP_OSS
537 int easycap_alsa_probe(struct easycap
*);
538 void easycap_alsa_complete(struct urb
*);
539 #else /* CONFIG_EASYCAP_OSS */
540 void easyoss_complete(struct urb
*);
541 #endif /* !CONFIG_EASYCAP_OSS */
543 int easycap_sound_setup(struct easycap
*);
544 int submit_audio_urbs(struct easycap
*);
545 int kill_audio_urbs(struct easycap
*);
546 void easyoss_testtone(struct easycap
*, int);
547 int audio_setup(struct easycap
*);
548 /*---------------------------------------------------------------------------*/
550 * LOW-LEVEL FUNCTION PROTOTYPES
552 /*---------------------------------------------------------------------------*/
553 int audio_gainget(struct usb_device
*);
554 int audio_gainset(struct usb_device
*, __s8
);
556 int set_interface(struct usb_device
*, __u16
);
557 int wakeup_device(struct usb_device
*);
558 int confirm_resolution(struct usb_device
*);
559 int confirm_stream(struct usb_device
*);
561 int setup_stk(struct usb_device
*, bool);
562 int setup_saa(struct usb_device
*, bool);
563 int setup_vt(struct usb_device
*);
564 int check_stk(struct usb_device
*, bool);
565 int check_saa(struct usb_device
*, bool);
566 int ready_saa(struct usb_device
*);
567 int merit_saa(struct usb_device
*);
568 int check_vt(struct usb_device
*);
569 int select_input(struct usb_device
*, int, int);
570 int set_resolution(struct usb_device
*,
571 __u16
, __u16
, __u16
, __u16
);
573 int read_saa(struct usb_device
*, __u16
);
574 int read_stk(struct usb_device
*, __u32
);
575 int write_saa(struct usb_device
*, __u16
, __u16
);
576 int wait_i2c(struct usb_device
*);
577 int write_000(struct usb_device
*, __u16
, __u16
);
578 int start_100(struct usb_device
*);
579 int stop_100(struct usb_device
*);
580 int write_300(struct usb_device
*);
581 int read_vt(struct usb_device
*, __u16
);
582 int write_vt(struct usb_device
*, __u16
, __u16
);
583 int regset(struct usb_device
*, __u16
, __u16
);
584 int regget(struct usb_device
*, __u16
, void *);
585 int isdongle(struct easycap
*);
586 /*---------------------------------------------------------------------------*/
587 struct signed_div_result
{
588 long long int quotient
;
589 unsigned long long int remainder
;
590 } signed_div(long long int, long long int);
593 /*---------------------------------------------------------------------------*/
597 /*---------------------------------------------------------------------------*/
598 #define GET(X, Y, Z) do { \
601 rc = regget(X, Y, Z); \
603 JOT(8, ":-(%i\n", __LINE__); return(rc); \
607 #define SET(X, Y, Z) do { \
609 rc = regset(X, Y, Z); \
611 JOT(8, ":-(%i\n", __LINE__); return(rc); \
614 /*---------------------------------------------------------------------------*/
616 * MACROS SAM(...) AND JOM(...) ALLOW DIAGNOSTIC OUTPUT TO BE TAGGED WITH
617 * THE IDENTITY OF THE DONGLE TO WHICH IT APPLIES, BUT IF INVOKED WHEN THE
618 * POINTER peasycap IS INVALID AN Oops IS LIKELY, AND ITS CAUSE MAY NOT BE
619 * IMMEDIATELY OBVIOUS FROM A CASUAL READING OF THE SOURCE CODE. BEWARE.
621 /*---------------------------------------------------------------------------*/
622 const char *strerror(int err
);
624 #define SAY(format, args...) do { \
625 printk(KERN_DEBUG "easycap:: %s: " \
626 format, __func__, ##args); \
628 #define SAM(format, args...) do { \
629 printk(KERN_DEBUG "easycap::%i%s: " \
630 format, peasycap->isdongle, __func__, ##args);\
633 #ifdef CONFIG_EASYCAP_DEBUG
634 extern int easycap_debug
;
635 #define JOT(n, format, args...) do { \
636 if (n <= easycap_debug) { \
637 printk(KERN_DEBUG "easycap:: %s: " \
638 format, __func__, ##args);\
641 #define JOM(n, format, args...) do { \
642 if (n <= easycap_debug) { \
643 printk(KERN_DEBUG "easycap::%i%s: " \
644 format, peasycap->isdongle, __func__, ##args);\
649 #define JOT(n, format, args...) do {} while (0)
650 #define JOM(n, format, args...) do {} while (0)
651 #endif /* CONFIG_EASYCAP_DEBUG */
653 #define MICROSECONDS(X, Y) \
654 ((1000000*((long long int)(X.tv_sec - Y.tv_sec))) + \
655 (long long int)(X.tv_usec - Y.tv_usec))
657 /*---------------------------------------------------------------------------*/
659 * (unsigned char *)P pointer to next byte pair
660 * (long int *)X pointer to accumulating count
661 * (long int *)Y pointer to accumulating sum
662 * (long long int *)Z pointer to accumulating sum of squares
664 /*---------------------------------------------------------------------------*/
665 #define SUMMER(P, X, Y, Z) do { \
667 unsigned int u0, u1, u2; \
669 p = (unsigned char *)(P); \
670 u0 = (unsigned int) (*p); \
671 u1 = (unsigned int) (*(p + 1)); \
672 u2 = (unsigned int) ((u1 << 8) | u0); \
674 s = -(long int)(0x7FFF & (~u2)); \
676 s = (long int)(0x7FFF & u2); \
677 *((X)) += (long int) 1; \
678 *((Y)) += (long int) s; \
679 *((Z)) += ((long long int)(s) * (long long int)(s)); \
681 /*---------------------------------------------------------------------------*/
683 /*---------------------------------------------------------------------------*/
686 /*---------------------------------------------------------------------------*/
688 extern const struct easycap_standard easycap_standard
[];
689 extern struct easycap_format easycap_format
[];
690 extern struct v4l2_queryctrl easycap_control
[];
691 extern struct usb_driver easycap_usb_driver
;
692 extern struct easycap_dongle easycapdc60_dongle
[];
693 #ifndef CONFIG_EASYCAP_OSS
694 extern struct snd_pcm_ops easycap_alsa_ops
;
695 extern struct snd_pcm_hardware easycap_pcm_hardware
;
696 extern struct snd_card
*psnd_card
;
697 #else /* CONFIG_EASYCAP_OSS */
698 extern struct usb_class_driver easyoss_class
;
699 extern const struct file_operations easyoss_fops
;
700 #endif /* !CONFIG_EASYCAP_OSS */
702 #endif /* !__EASYCAP_H__ */