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/workqueue.h>
66 #include <linux/poll.h>
69 #include <linux/delay.h>
70 #include <linux/types.h>
72 #ifndef CONFIG_EASYCAP_OSS
73 #include <linux/vmalloc.h>
74 #include <linux/sound.h>
75 #include <sound/core.h>
76 #include <sound/pcm.h>
77 #include <sound/pcm_params.h>
78 #include <sound/info.h>
79 #include <sound/initval.h>
80 #include <sound/control.h>
81 #endif /* !CONFIG_EASYCAP_OSS */
82 #include <media/v4l2-dev.h>
83 #include <media/v4l2-device.h>
84 #include <linux/videodev2.h>
85 #include <linux/soundcard.h>
87 /*---------------------------------------------------------------------------*/
88 /* VENDOR, PRODUCT: Syntek Semiconductor Co., Ltd
90 * EITHER EasyCAP USB 2.0 Video Adapter with Audio, Model No. DC60
91 * with input cabling: AUDIO(L), AUDIO(R), CVBS, S-VIDEO.
93 * OR EasyCAP 4CHANNEL USB 2.0 DVR, Model No. EasyCAP002
94 * with input cabling: MICROPHONE, CVBS1, CVBS2, CVBS3, CVBS4.
96 /*---------------------------------------------------------------------------*/
97 #define USB_EASYCAP_VENDOR_ID 0x05e1
98 #define USB_EASYCAP_PRODUCT_ID 0x0408
100 #define EASYCAP_DRIVER_VERSION "0.9.01"
101 #define EASYCAP_DRIVER_DESCRIPTION "easycapdc60"
103 #define USB_SKEL_MINOR_BASE 192
104 #define DONGLE_MANY 8
106 /*---------------------------------------------------------------------------*/
108 * DEFAULT LUMINANCE, CONTRAST, SATURATION AND HUE
110 /*---------------------------------------------------------------------------*/
111 #define SAA_0A_DEFAULT 0x7F
112 #define SAA_0B_DEFAULT 0x3F
113 #define SAA_0C_DEFAULT 0x2F
114 #define SAA_0D_DEFAULT 0x00
115 /*---------------------------------------------------------------------------*/
117 * VIDEO STREAMING PARAMETERS:
118 * USB 2.0 PROVIDES FOR HIGH-BANDWIDTH ENDPOINTS WITH AN UPPER LIMIT
119 * OF 3072 BYTES PER MICROFRAME for wMaxPacketSize.
121 /*---------------------------------------------------------------------------*/
122 #define VIDEO_ISOC_BUFFER_MANY 16
123 #define VIDEO_ISOC_ORDER 3
124 #define VIDEO_ISOC_FRAMESPERDESC ((unsigned int) 1 << VIDEO_ISOC_ORDER)
125 #define USB_2_0_MAXPACKETSIZE 3072
126 #if (USB_2_0_MAXPACKETSIZE > PAGE_SIZE)
127 #error video_isoc_buffer[.] will not be big enough
129 #define VIDEO_JUNK_TOLERATE VIDEO_ISOC_BUFFER_MANY
130 #define VIDEO_LOST_TOLERATE 50
131 /*---------------------------------------------------------------------------*/
135 /*---------------------------------------------------------------------------*/
136 #define FIELD_BUFFER_SIZE (203 * PAGE_SIZE)
137 #define FRAME_BUFFER_SIZE (405 * PAGE_SIZE)
138 #define FIELD_BUFFER_MANY 4
139 #define FRAME_BUFFER_MANY 6
140 /*---------------------------------------------------------------------------*/
142 * AUDIO STREAMING PARAMETERS
144 /*---------------------------------------------------------------------------*/
145 #define AUDIO_ISOC_BUFFER_MANY 16
146 #define AUDIO_ISOC_ORDER 1
147 #define AUDIO_ISOC_FRAMESPERDESC 32
148 #define AUDIO_ISOC_BUFFER_SIZE (PAGE_SIZE << AUDIO_ISOC_ORDER)
149 /*---------------------------------------------------------------------------*/
153 /*---------------------------------------------------------------------------*/
154 #define AUDIO_FRAGMENT_MANY 32
155 #define PAGES_PER_AUDIO_FRAGMENT 4
156 /*---------------------------------------------------------------------------*/
158 * IT IS ESSENTIAL THAT EVEN-NUMBERED STANDARDS ARE 25 FRAMES PER SECOND,
159 * ODD-NUMBERED STANDARDS ARE 30 FRAMES PER SECOND.
160 * THE NUMBERING OF STANDARDS MUST NOT BE CHANGED WITHOUT DUE CARE. NOT
161 * ONLY MUST THE PARAMETER
163 * BE CHANGED TO CORRESPOND TO THE NEW NUMBER OF STANDARDS, BUT ALSO THE
164 * NUMBERING MUST REMAIN AN UNBROKEN ASCENDING SEQUENCE: DUMMY STANDARDS
165 * MAY NEED TO BE ADDED. APPROPRIATE CHANGES WILL ALWAYS BE REQUIRED IN
166 * ROUTINE fillin_formats() AND POSSIBLY ELSEWHERE. BEWARE.
168 /*---------------------------------------------------------------------------*/
179 #define PAL_BGHIN_SLOW 10
180 #define PAL_Nc_SLOW 12
181 #define SECAM_SLOW 14
182 #define NTSC_N_SLOW 16
183 #define NTSC_N_443_SLOW 18
184 #define NTSC_M_SLOW 11
185 #define NTSC_443_SLOW 13
186 #define NTSC_M_JP_SLOW 15
187 #define PAL_60_SLOW 17
188 #define PAL_M_SLOW 19
189 #define STANDARD_MANY 20
190 /*---------------------------------------------------------------------------*/
194 /*---------------------------------------------------------------------------*/
219 #define SETTINGS_MANY (STANDARD_MANY * \
224 /*---------------------------------------------------------------------------*/
226 * STRUCTURE DEFINITIONS
228 /*---------------------------------------------------------------------------*/
229 struct easycap_dongle {
230 struct easycap *peasycap;
231 struct mutex mutex_video;
232 struct mutex mutex_audio;
234 /*---------------------------------------------------------------------------*/
236 struct list_head list_head;
242 /*---------------------------------------------------------------------------*/
244 struct list_head list_head;
249 /*---------------------------------------------------------------------------*/
250 struct easycap_standard {
252 struct v4l2_standard v4l2_standard;
254 struct easycap_format {
257 struct v4l2_format v4l2_format;
263 int standard_offset_ok;
265 int format_offset_ok;
275 /*---------------------------------------------------------------------------*/
277 * easycap.ilk == 0 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=256
278 * easycap.ilk == 2 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=9
279 * easycap.ilk == 3 => FOUR-CVBS HARDWARE, AUDIO wMaxPacketSize=9
281 /*---------------------------------------------------------------------------*/
286 struct video_device video_device;
287 struct v4l2_device v4l2_device;
290 unsigned int audio_pages_per_fragment;
291 unsigned int audio_bytes_per_fragment;
292 unsigned int audio_buffer_page_many;
302 struct usb_device *pusb_device;
303 struct usb_interface *pusb_interface;
307 int queued[FRAME_BUFFER_MANY];
308 int done[FRAME_BUFFER_MANY];
310 wait_queue_head_t wq_video;
311 wait_queue_head_t wq_audio;
312 wait_queue_head_t wq_trigger;
318 struct inputset inputset[INPUT_MANY];
326 int lost[INPUT_MANY];
329 struct timeval timeval0;
330 struct timeval timeval1;
331 struct timeval timeval2;
332 struct timeval timeval3;
333 struct timeval timeval6;
334 struct timeval timeval7;
335 struct timeval timeval8;
336 long long int dnbydt;
339 int video_altsetting_on;
340 int video_altsetting_off;
341 int video_endpointnumber;
342 int video_isoc_maxframesize;
343 int video_isoc_buffer_size;
344 int video_isoc_framesperdesc;
346 int video_isoc_streaming;
347 int video_isoc_sequence;
352 struct data_buffer video_isoc_buffer[VIDEO_ISOC_BUFFER_MANY];
353 struct data_buffer field_buffer[FIELD_BUFFER_MANY]
354 [(FIELD_BUFFER_SIZE/PAGE_SIZE)];
355 struct data_buffer frame_buffer[FRAME_BUFFER_MANY]
356 [(FRAME_BUFFER_SIZE/PAGE_SIZE)];
358 struct list_head urb_video_head;
359 struct list_head *purb_video_head;
365 long long audio_bytes;
369 /*---------------------------------------------------------------------------*/
373 /*---------------------------------------------------------------------------*/
374 int field_fill; /* Field buffer being filled by easycap_complete(). */
375 /* Bumped only by easycap_complete(). */
376 int field_page; /* Page of field buffer page being filled by */
377 /* easycap_complete(). */
378 int field_read; /* Field buffer to be read by field2frame(). */
379 /* Bumped only by easycap_complete(). */
380 int frame_fill; /* Frame buffer being filled by field2frame(). */
381 /* Bumped only by easycap_dqbuf() when */
382 /* field2frame() has created a complete frame. */
383 int frame_read; /* Frame buffer offered to user by DQBUF. */
384 /* Set only by easycap_dqbuf() to trail frame_fill.*/
385 int frame_lock; /* Flag set to 1 by DQBUF and cleared by QBUF */
386 /*---------------------------------------------------------------------------*/
390 /*---------------------------------------------------------------------------*/
398 int frame_buffer_used;
399 int frame_buffer_many;
400 int videofieldamount;
407 int allocation_video_urb;
408 int allocation_video_page;
409 int allocation_video_struct;
410 int registered_video;
411 /*---------------------------------------------------------------------------*/
415 /*---------------------------------------------------------------------------*/
416 #ifndef CONFIG_EASYCAP_OSS
417 struct snd_pcm_hardware alsa_hardware;
418 struct snd_card *psnd_card;
419 struct snd_pcm *psnd_pcm;
420 struct snd_pcm_substream *psubstream;
424 #endif /* !CONFIG_EASYCAP_OSS */
425 /*---------------------------------------------------------------------------*/
429 /*---------------------------------------------------------------------------*/
431 int audio_altsetting_on;
432 int audio_altsetting_off;
433 int audio_endpointnumber;
434 int audio_isoc_maxframesize;
435 int audio_isoc_buffer_size;
436 int audio_isoc_framesperdesc;
438 int audio_isoc_streaming;
445 struct data_buffer audio_isoc_buffer[AUDIO_ISOC_BUFFER_MANY];
447 struct list_head urb_audio_head;
448 struct list_head *purb_audio_head;
449 /*---------------------------------------------------------------------------*/
453 /*---------------------------------------------------------------------------*/
454 int audio_fill; /* Audio buffer being filled by easycap_complete(). */
455 /* Bumped only by easycap_complete(). */
456 int audio_read; /* Audio buffer page being read by easycap_read(). */
457 /* Set by easycap_read() to trail audio_fill by */
459 /*---------------------------------------------------------------------------*/
463 /*---------------------------------------------------------------------------*/
465 int audio_buffer_many;
467 int allocation_audio_urb;
468 int allocation_audio_page;
469 int allocation_audio_struct;
470 int registered_audio;
472 long long int audio_sample;
473 long long int audio_niveau;
474 long long int audio_square;
476 struct data_buffer audio_buffer[];
478 /*---------------------------------------------------------------------------*/
480 * VIDEO FUNCTION PROTOTYPES
482 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
483 long easycap_unlocked_ioctl(struct file *, unsigned int, unsigned long);
484 int easycap_dqbuf(struct easycap *, int);
485 int submit_video_urbs(struct easycap *);
486 int kill_video_urbs(struct easycap *);
487 int field2frame(struct easycap *);
488 int redaub(struct easycap *, void *, void *,
489 int, int, u8, u8, bool);
490 void easycap_testcard(struct easycap *, int);
491 int fillin_formats(void);
492 int newinput(struct easycap *, int);
493 int adjust_standard(struct easycap *, v4l2_std_id);
494 int adjust_format(struct easycap *, u32, u32, u32,
496 int adjust_brightness(struct easycap *, int);
497 int adjust_contrast(struct easycap *, int);
498 int adjust_saturation(struct easycap *, int);
499 int adjust_hue(struct easycap *, int);
500 int adjust_volume(struct easycap *, int);
501 /*---------------------------------------------------------------------------*/
503 * AUDIO FUNCTION PROTOTYPES
505 /*---------------------------------------------------------------------------*/
506 #ifndef CONFIG_EASYCAP_OSS
507 int easycap_alsa_probe(struct easycap *);
508 void easycap_alsa_complete(struct urb *);
509 #else /* CONFIG_EASYCAP_OSS */
510 void easyoss_complete(struct urb *);
511 #endif /* !CONFIG_EASYCAP_OSS */
513 int easycap_sound_setup(struct easycap *);
514 int submit_audio_urbs(struct easycap *);
515 int kill_audio_urbs(struct easycap *);
516 void easyoss_testtone(struct easycap *, int);
517 int audio_setup(struct easycap *);
518 /*---------------------------------------------------------------------------*/
520 * LOW-LEVEL FUNCTION PROTOTYPES
522 /*---------------------------------------------------------------------------*/
523 int audio_gainget(struct usb_device *);
524 int audio_gainset(struct usb_device *, s8);
526 int set_interface(struct usb_device *, u16);
527 int wakeup_device(struct usb_device *);
528 int confirm_resolution(struct usb_device *);
529 int confirm_stream(struct usb_device *);
531 int setup_stk(struct usb_device *, bool);
532 int setup_saa(struct usb_device *, bool);
533 int setup_vt(struct usb_device *);
534 int check_stk(struct usb_device *, bool);
535 int check_saa(struct usb_device *, bool);
536 int ready_saa(struct usb_device *);
537 int merit_saa(struct usb_device *);
538 int check_vt(struct usb_device *);
539 int select_input(struct usb_device *, int, int);
540 int set_resolution(struct usb_device *,
543 int read_saa(struct usb_device *, u16);
544 int read_stk(struct usb_device *, u32);
545 int write_saa(struct usb_device *, u16, u16);
546 int write_000(struct usb_device *, u16, u16);
547 int start_100(struct usb_device *);
548 int stop_100(struct usb_device *);
549 int write_300(struct usb_device *);
550 int read_vt(struct usb_device *, u16);
551 int write_vt(struct usb_device *, u16, u16);
552 int isdongle(struct easycap *);
553 /*---------------------------------------------------------------------------*/
554 struct signed_div_result {
555 long long int quotient;
556 unsigned long long int remainder;
557 } signed_div(long long int, long long int);
560 /*---------------------------------------------------------------------------*/
562 * MACROS SAM(...) AND JOM(...) ALLOW DIAGNOSTIC OUTPUT TO BE TAGGED WITH
563 * THE IDENTITY OF THE DONGLE TO WHICH IT APPLIES, BUT IF INVOKED WHEN THE
564 * POINTER peasycap IS INVALID AN Oops IS LIKELY, AND ITS CAUSE MAY NOT BE
565 * IMMEDIATELY OBVIOUS FROM A CASUAL READING OF THE SOURCE CODE. BEWARE.
567 /*---------------------------------------------------------------------------*/
568 const char *strerror(int err);
570 #define SAY(format, args...) do { \
571 printk(KERN_DEBUG "easycap:: %s: " \
572 format, __func__, ##args); \
574 #define SAM(format, args...) do { \
575 printk(KERN_DEBUG "easycap::%i%s: " \
576 format, peasycap->isdongle, __func__, ##args);\
579 #ifdef CONFIG_EASYCAP_DEBUG
580 extern int easycap_debug;
581 #define JOT(n, format, args...) do { \
582 if (n <= easycap_debug) { \
583 printk(KERN_DEBUG "easycap:: %s: " \
584 format, __func__, ##args);\
587 #define JOM(n, format, args...) do { \
588 if (n <= easycap_debug) { \
589 printk(KERN_DEBUG "easycap::%i%s: " \
590 format, peasycap->isdongle, __func__, ##args);\
595 #define JOT(n, format, args...) do {} while (0)
596 #define JOM(n, format, args...) do {} while (0)
597 #endif /* CONFIG_EASYCAP_DEBUG */
599 #define MICROSECONDS(X, Y) \
600 ((1000000*((long long int)(X.tv_sec - Y.tv_sec))) + \
601 (long long int)(X.tv_usec - Y.tv_usec))
603 /*---------------------------------------------------------------------------*/
605 * (unsigned char *)P pointer to next byte pair
606 * (long int *)X pointer to accumulating count
607 * (long int *)Y pointer to accumulating sum
608 * (long long int *)Z pointer to accumulating sum of squares
610 /*---------------------------------------------------------------------------*/
611 #define SUMMER(P, X, Y, Z) do { \
613 unsigned int u0, u1, u2; \
615 p = (unsigned char *)(P); \
616 u0 = (unsigned int) (*p); \
617 u1 = (unsigned int) (*(p + 1)); \
618 u2 = (unsigned int) ((u1 << 8) | u0); \
620 s = -(long int)(0x7FFF & (~u2)); \
622 s = (long int)(0x7FFF & u2); \
623 *((X)) += (long int) 1; \
624 *((Y)) += (long int) s; \
625 *((Z)) += ((long long int)(s) * (long long int)(s)); \
627 /*---------------------------------------------------------------------------*/
629 /*---------------------------------------------------------------------------*/
632 /*---------------------------------------------------------------------------*/
634 extern bool easycap_readback;
635 extern const struct easycap_standard easycap_standard[];
636 extern struct easycap_format easycap_format[];
637 extern struct v4l2_queryctrl easycap_control[];
638 extern struct usb_driver easycap_usb_driver;
639 extern struct easycap_dongle easycapdc60_dongle[];
640 #ifdef CONFIG_EASYCAP_OSS
641 extern struct usb_class_driver easyoss_class;
642 #endif /* !CONFIG_EASYCAP_OSS */
644 #endif /* !__EASYCAP_H__ */