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 /*---------------------------------------------------------------------------*/
59 #include <linux/kernel.h>
60 #include <linux/errno.h>
61 #include <linux/init.h>
62 #include <linux/slab.h>
63 #include <linux/module.h>
64 #include <linux/kref.h>
65 #include <linux/usb.h>
66 #include <linux/uaccess.h>
68 #include <linux/i2c.h>
69 #include <linux/version.h>
70 #include <linux/workqueue.h>
71 #include <linux/poll.h>
74 #include <linux/delay.h>
75 #include <linux/types.h>
77 #ifndef CONFIG_EASYCAP_OSS
78 #include <linux/vmalloc.h>
79 #include <linux/sound.h>
80 #include <sound/core.h>
81 #include <sound/pcm.h>
82 #include <sound/pcm_params.h>
83 #include <sound/info.h>
84 #include <sound/initval.h>
85 #include <sound/control.h>
86 #endif /* !CONFIG_EASYCAP_OSS */
87 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
88 #ifdef EASYCAP_IS_VIDEODEV_CLIENT
89 #include <media/v4l2-dev.h>
90 #ifdef EASYCAP_NEEDS_V4L2_DEVICE_H
91 #include <media/v4l2-device.h>
92 #endif /*EASYCAP_NEEDS_V4L2_DEVICE_H*/
93 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
94 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
95 #include <linux/videodev2.h>
96 #include <linux/soundcard.h>
97 #ifdef EASYCAP_NEEDS_USBVIDEO_H
98 #include <config/video/usbvideo.h>
99 #endif /*EASYCAP_NEEDS_USBVIDEO_H*/
102 #error "PAGE_SIZE not defined"
103 #endif /* PAGE_SIZE */
105 /*---------------------------------------------------------------------------*/
106 /* VENDOR, PRODUCT: Syntek Semiconductor Co., Ltd
108 * EITHER EasyCAP USB 2.0 Video Adapter with Audio, Model No. DC60
109 * with input cabling: AUDIO(L), AUDIO(R), CVBS, S-VIDEO.
111 * OR EasyCAP 4CHANNEL USB 2.0 DVR, Model No. EasyCAP002
112 * with input cabling: MICROPHONE, CVBS1, CVBS2, CVBS3, CVBS4.
114 /*---------------------------------------------------------------------------*/
115 #define USB_EASYCAP_VENDOR_ID 0x05e1
116 #define USB_EASYCAP_PRODUCT_ID 0x0408
118 #define EASYCAP_DRIVER_VERSION "0.9.01"
119 #define EASYCAP_DRIVER_DESCRIPTION "easycapdc60"
121 #define USB_SKEL_MINOR_BASE 192
122 #define DONGLE_MANY 8
124 /*---------------------------------------------------------------------------*/
126 * DEFAULT LUMINANCE, CONTRAST, SATURATION AND HUE
128 /*---------------------------------------------------------------------------*/
129 #define SAA_0A_DEFAULT 0x7F
130 #define SAA_0B_DEFAULT 0x3F
131 #define SAA_0C_DEFAULT 0x2F
132 #define SAA_0D_DEFAULT 0x00
133 /*---------------------------------------------------------------------------*/
135 * VIDEO STREAMING PARAMETERS:
136 * USB 2.0 PROVIDES FOR HIGH-BANDWIDTH ENDPOINTS WITH AN UPPER LIMIT
137 * OF 3072 BYTES PER MICROFRAME for wMaxPacketSize.
139 /*---------------------------------------------------------------------------*/
140 #define VIDEO_ISOC_BUFFER_MANY 16
141 #define VIDEO_ISOC_ORDER 3
142 #define VIDEO_ISOC_FRAMESPERDESC ((unsigned int) 1 << VIDEO_ISOC_ORDER)
143 #define USB_2_0_MAXPACKETSIZE 3072
144 #if (USB_2_0_MAXPACKETSIZE > PAGE_SIZE)
145 #error video_isoc_buffer[.] will not be big enough
147 #define VIDEO_JUNK_TOLERATE VIDEO_ISOC_BUFFER_MANY
148 #define VIDEO_LOST_TOLERATE 50
149 /*---------------------------------------------------------------------------*/
153 /*---------------------------------------------------------------------------*/
154 #define FIELD_BUFFER_SIZE (203 * PAGE_SIZE)
155 #define FRAME_BUFFER_SIZE (405 * PAGE_SIZE)
156 #define FIELD_BUFFER_MANY 4
157 #define FRAME_BUFFER_MANY 6
158 /*---------------------------------------------------------------------------*/
160 * AUDIO STREAMING PARAMETERS
162 /*---------------------------------------------------------------------------*/
163 #define AUDIO_ISOC_BUFFER_MANY 16
164 #define AUDIO_ISOC_ORDER 1
165 #define AUDIO_ISOC_FRAMESPERDESC 32
166 #define AUDIO_ISOC_BUFFER_SIZE (PAGE_SIZE << AUDIO_ISOC_ORDER)
167 /*---------------------------------------------------------------------------*/
171 /*---------------------------------------------------------------------------*/
172 #define AUDIO_FRAGMENT_MANY 32
173 #define PAGES_PER_AUDIO_FRAGMENT 4
174 /*---------------------------------------------------------------------------*/
176 * IT IS ESSENTIAL THAT EVEN-NUMBERED STANDARDS ARE 25 FRAMES PER SECOND,
177 * ODD-NUMBERED STANDARDS ARE 30 FRAMES PER SECOND.
178 * THE NUMBERING OF STANDARDS MUST NOT BE CHANGED WITHOUT DUE CARE. NOT
179 * ONLY MUST THE PARAMETER
181 * BE CHANGED TO CORRESPOND TO THE NEW NUMBER OF STANDARDS, BUT ALSO THE
182 * NUMBERING MUST REMAIN AN UNBROKEN ASCENDING SEQUENCE: DUMMY STANDARDS
183 * MAY NEED TO BE ADDED. APPROPRIATE CHANGES WILL ALWAYS BE REQUIRED IN
184 * ROUTINE fillin_formats() AND POSSIBLY ELSEWHERE. BEWARE.
186 /*---------------------------------------------------------------------------*/
197 #define PAL_BGHIN_SLOW 10
198 #define PAL_Nc_SLOW 12
199 #define SECAM_SLOW 14
200 #define NTSC_N_SLOW 16
201 #define NTSC_N_443_SLOW 18
202 #define NTSC_M_SLOW 11
203 #define NTSC_443_SLOW 13
204 #define NTSC_M_JP_SLOW 15
205 #define PAL_60_SLOW 17
206 #define PAL_M_SLOW 19
207 #define STANDARD_MANY 20
208 /*---------------------------------------------------------------------------*/
212 /*---------------------------------------------------------------------------*/
237 #define SETTINGS_MANY (STANDARD_MANY * \
242 /*---------------------------------------------------------------------------*/
244 * STRUCTURE DEFINITIONS
246 /*---------------------------------------------------------------------------*/
247 struct easycap_dongle
{
248 struct easycap
*peasycap
;
249 struct mutex mutex_video
;
250 struct mutex mutex_audio
;
252 /*---------------------------------------------------------------------------*/
254 struct list_head list_head
;
260 /*---------------------------------------------------------------------------*/
262 struct list_head list_head
;
267 /*---------------------------------------------------------------------------*/
268 struct easycap_standard
{
270 struct v4l2_standard v4l2_standard
;
272 struct easycap_format
{
275 struct v4l2_format v4l2_format
;
281 int standard_offset_ok
;
283 int format_offset_ok
;
293 /*---------------------------------------------------------------------------*/
295 * easycap.ilk == 0 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=256
296 * easycap.ilk == 2 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=9
297 * easycap.ilk == 3 => FOUR-CVBS HARDWARE, AUDIO wMaxPacketSize=9
299 /*---------------------------------------------------------------------------*/
301 #define TELLTALE "expectedstring"
306 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
307 #ifdef EASYCAP_IS_VIDEODEV_CLIENT
308 struct video_device video_device
;
309 #ifdef EASYCAP_NEEDS_V4L2_DEVICE_H
310 struct v4l2_device v4l2_device
;
311 #endif /*EASYCAP_NEEDS_V4L2_DEVICE_H*/
312 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
313 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
315 unsigned int audio_pages_per_fragment
;
316 unsigned int audio_bytes_per_fragment
;
317 unsigned int audio_buffer_page_many
;
327 struct usb_device
*pusb_device
;
328 struct usb_interface
*pusb_interface
;
332 int queued
[FRAME_BUFFER_MANY
];
333 int done
[FRAME_BUFFER_MANY
];
335 wait_queue_head_t wq_video
;
336 wait_queue_head_t wq_audio
;
337 wait_queue_head_t wq_trigger
;
343 struct inputset inputset
[INPUT_MANY
];
351 int lost
[INPUT_MANY
];
354 struct timeval timeval0
;
355 struct timeval timeval1
;
356 struct timeval timeval2
;
357 struct timeval timeval3
;
358 struct timeval timeval6
;
359 struct timeval timeval7
;
360 struct timeval timeval8
;
361 long long int dnbydt
;
364 int video_altsetting_on
;
365 int video_altsetting_off
;
366 int video_endpointnumber
;
367 int video_isoc_maxframesize
;
368 int video_isoc_buffer_size
;
369 int video_isoc_framesperdesc
;
371 int video_isoc_streaming
;
372 int video_isoc_sequence
;
377 struct data_buffer video_isoc_buffer
[VIDEO_ISOC_BUFFER_MANY
];
378 struct data_buffer field_buffer
[FIELD_BUFFER_MANY
]
379 [(FIELD_BUFFER_SIZE
/PAGE_SIZE
)];
380 struct data_buffer frame_buffer
[FRAME_BUFFER_MANY
]
381 [(FRAME_BUFFER_SIZE
/PAGE_SIZE
)];
383 struct list_head urb_video_head
;
384 struct list_head
*purb_video_head
;
390 long long audio_bytes
;
394 /*---------------------------------------------------------------------------*/
398 /*---------------------------------------------------------------------------*/
399 int field_fill
; /* Field buffer being filled by easycap_complete(). */
400 /* Bumped only by easycap_complete(). */
401 int field_page
; /* Page of field buffer page being filled by */
402 /* easycap_complete(). */
403 int field_read
; /* Field buffer to be read by field2frame(). */
404 /* Bumped only by easycap_complete(). */
405 int frame_fill
; /* Frame buffer being filled by field2frame(). */
406 /* Bumped only by easycap_dqbuf() when */
407 /* field2frame() has created a complete frame. */
408 int frame_read
; /* Frame buffer offered to user by DQBUF. */
409 /* Set only by easycap_dqbuf() to trail frame_fill.*/
410 int frame_lock
; /* Flag set to 1 by DQBUF and cleared by QBUF */
411 /*---------------------------------------------------------------------------*/
415 /*---------------------------------------------------------------------------*/
423 int frame_buffer_used
;
424 int frame_buffer_many
;
425 int videofieldamount
;
432 int allocation_video_urb
;
433 int allocation_video_page
;
434 int allocation_video_struct
;
435 int registered_video
;
436 /*---------------------------------------------------------------------------*/
440 /*---------------------------------------------------------------------------*/
441 #ifndef CONFIG_EASYCAP_OSS
442 struct snd_pcm_hardware alsa_hardware
;
443 struct snd_card
*psnd_card
;
444 struct snd_pcm
*psnd_pcm
;
445 struct snd_pcm_substream
*psubstream
;
449 #endif /* !CONFIG_EASYCAP_OSS */
450 /*---------------------------------------------------------------------------*/
454 /*---------------------------------------------------------------------------*/
456 int audio_altsetting_on
;
457 int audio_altsetting_off
;
458 int audio_endpointnumber
;
459 int audio_isoc_maxframesize
;
460 int audio_isoc_buffer_size
;
461 int audio_isoc_framesperdesc
;
463 int audio_isoc_streaming
;
470 struct data_buffer audio_isoc_buffer
[AUDIO_ISOC_BUFFER_MANY
];
472 struct list_head urb_audio_head
;
473 struct list_head
*purb_audio_head
;
474 /*---------------------------------------------------------------------------*/
478 /*---------------------------------------------------------------------------*/
479 int audio_fill
; /* Audio buffer being filled by easycap_complete(). */
480 /* Bumped only by easycap_complete(). */
481 int audio_read
; /* Audio buffer page being read by easycap_read(). */
482 /* Set by easycap_read() to trail audio_fill by */
484 /*---------------------------------------------------------------------------*/
488 /*---------------------------------------------------------------------------*/
490 int audio_buffer_many
;
492 int allocation_audio_urb
;
493 int allocation_audio_page
;
494 int allocation_audio_struct
;
495 int registered_audio
;
497 long long int audio_sample
;
498 long long int audio_niveau
;
499 long long int audio_square
;
501 struct data_buffer audio_buffer
[];
503 /*---------------------------------------------------------------------------*/
505 * VIDEO FUNCTION PROTOTYPES
507 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
508 long easycap_unlocked_ioctl(struct file
*, unsigned int, unsigned long);
509 int easycap_dqbuf(struct easycap
*, int);
510 int submit_video_urbs(struct easycap
*);
511 int kill_video_urbs(struct easycap
*);
512 int field2frame(struct easycap
*);
513 int redaub(struct easycap
*, void *, void *,
514 int, int, u8
, u8
, bool);
515 void easycap_testcard(struct easycap
*, int);
516 int fillin_formats(void);
517 int newinput(struct easycap
*, int);
518 int adjust_standard(struct easycap
*, v4l2_std_id
);
519 int adjust_format(struct easycap
*, u32
, u32
, u32
,
521 int adjust_brightness(struct easycap
*, int);
522 int adjust_contrast(struct easycap
*, int);
523 int adjust_saturation(struct easycap
*, int);
524 int adjust_hue(struct easycap
*, int);
525 int adjust_volume(struct easycap
*, int);
526 /*---------------------------------------------------------------------------*/
528 * AUDIO FUNCTION PROTOTYPES
530 /*---------------------------------------------------------------------------*/
531 #ifndef CONFIG_EASYCAP_OSS
532 int easycap_alsa_probe(struct easycap
*);
533 void easycap_alsa_complete(struct urb
*);
534 #else /* CONFIG_EASYCAP_OSS */
535 void easyoss_complete(struct urb
*);
536 #endif /* !CONFIG_EASYCAP_OSS */
538 int easycap_sound_setup(struct easycap
*);
539 int submit_audio_urbs(struct easycap
*);
540 int kill_audio_urbs(struct easycap
*);
541 void easyoss_testtone(struct easycap
*, int);
542 int audio_setup(struct easycap
*);
543 /*---------------------------------------------------------------------------*/
545 * LOW-LEVEL FUNCTION PROTOTYPES
547 /*---------------------------------------------------------------------------*/
548 int audio_gainget(struct usb_device
*);
549 int audio_gainset(struct usb_device
*, s8
);
551 int set_interface(struct usb_device
*, u16
);
552 int wakeup_device(struct usb_device
*);
553 int confirm_resolution(struct usb_device
*);
554 int confirm_stream(struct usb_device
*);
556 int setup_stk(struct usb_device
*, bool);
557 int setup_saa(struct usb_device
*, bool);
558 int setup_vt(struct usb_device
*);
559 int check_stk(struct usb_device
*, bool);
560 int check_saa(struct usb_device
*, bool);
561 int ready_saa(struct usb_device
*);
562 int merit_saa(struct usb_device
*);
563 int check_vt(struct usb_device
*);
564 int select_input(struct usb_device
*, int, int);
565 int set_resolution(struct usb_device
*,
568 int read_saa(struct usb_device
*, u16
);
569 int read_stk(struct usb_device
*, u32
);
570 int write_saa(struct usb_device
*, u16
, u16
);
571 int wait_i2c(struct usb_device
*);
572 int write_000(struct usb_device
*, u16
, u16
);
573 int start_100(struct usb_device
*);
574 int stop_100(struct usb_device
*);
575 int write_300(struct usb_device
*);
576 int read_vt(struct usb_device
*, u16
);
577 int write_vt(struct usb_device
*, u16
, u16
);
578 int regset(struct usb_device
*, u16
, u16
);
579 int regget(struct usb_device
*, u16
, void *);
580 int isdongle(struct easycap
*);
581 /*---------------------------------------------------------------------------*/
582 struct signed_div_result
{
583 long long int quotient
;
584 unsigned long long int remainder
;
585 } signed_div(long long int, long long int);
588 /*---------------------------------------------------------------------------*/
592 /*---------------------------------------------------------------------------*/
593 #define GET(X, Y, Z) do { \
596 __rc = regget(X, Y, Z); \
598 JOT(8, ":-(%i\n", __LINE__); return __rc; \
602 #define SET(X, Y, Z) do { \
604 __rc = regset(X, Y, Z); \
606 JOT(8, ":-(%i\n", __LINE__); return __rc; \
609 /*---------------------------------------------------------------------------*/
611 * MACROS SAM(...) AND JOM(...) ALLOW DIAGNOSTIC OUTPUT TO BE TAGGED WITH
612 * THE IDENTITY OF THE DONGLE TO WHICH IT APPLIES, BUT IF INVOKED WHEN THE
613 * POINTER peasycap IS INVALID AN Oops IS LIKELY, AND ITS CAUSE MAY NOT BE
614 * IMMEDIATELY OBVIOUS FROM A CASUAL READING OF THE SOURCE CODE. BEWARE.
616 /*---------------------------------------------------------------------------*/
617 const char *strerror(int err
);
619 #define SAY(format, args...) do { \
620 printk(KERN_DEBUG "easycap:: %s: " \
621 format, __func__, ##args); \
623 #define SAM(format, args...) do { \
624 printk(KERN_DEBUG "easycap::%i%s: " \
625 format, peasycap->isdongle, __func__, ##args);\
628 #ifdef CONFIG_EASYCAP_DEBUG
629 extern int easycap_debug
;
630 #define JOT(n, format, args...) do { \
631 if (n <= easycap_debug) { \
632 printk(KERN_DEBUG "easycap:: %s: " \
633 format, __func__, ##args);\
636 #define JOM(n, format, args...) do { \
637 if (n <= easycap_debug) { \
638 printk(KERN_DEBUG "easycap::%i%s: " \
639 format, peasycap->isdongle, __func__, ##args);\
644 #define JOT(n, format, args...) do {} while (0)
645 #define JOM(n, format, args...) do {} while (0)
646 #endif /* CONFIG_EASYCAP_DEBUG */
648 #define MICROSECONDS(X, Y) \
649 ((1000000*((long long int)(X.tv_sec - Y.tv_sec))) + \
650 (long long int)(X.tv_usec - Y.tv_usec))
652 /*---------------------------------------------------------------------------*/
654 * (unsigned char *)P pointer to next byte pair
655 * (long int *)X pointer to accumulating count
656 * (long int *)Y pointer to accumulating sum
657 * (long long int *)Z pointer to accumulating sum of squares
659 /*---------------------------------------------------------------------------*/
660 #define SUMMER(P, X, Y, Z) do { \
662 unsigned int u0, u1, u2; \
664 p = (unsigned char *)(P); \
665 u0 = (unsigned int) (*p); \
666 u1 = (unsigned int) (*(p + 1)); \
667 u2 = (unsigned int) ((u1 << 8) | u0); \
669 s = -(long int)(0x7FFF & (~u2)); \
671 s = (long int)(0x7FFF & u2); \
672 *((X)) += (long int) 1; \
673 *((Y)) += (long int) s; \
674 *((Z)) += ((long long int)(s) * (long long int)(s)); \
676 /*---------------------------------------------------------------------------*/
678 /*---------------------------------------------------------------------------*/
681 /*---------------------------------------------------------------------------*/
683 extern const struct easycap_standard easycap_standard
[];
684 extern struct easycap_format easycap_format
[];
685 extern struct v4l2_queryctrl easycap_control
[];
686 extern struct usb_driver easycap_usb_driver
;
687 extern struct easycap_dongle easycapdc60_dongle
[];
688 #ifdef CONFIG_EASYCAP_OSS
689 extern struct usb_class_driver easyoss_class
;
690 #endif /* !CONFIG_EASYCAP_OSS */
692 #endif /* !__EASYCAP_H__ */