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 #include <linux/vmalloc.h>
73 #include <linux/sound.h>
74 #include <sound/core.h>
75 #include <sound/pcm.h>
76 #include <sound/pcm_params.h>
77 #include <sound/info.h>
78 #include <sound/initval.h>
79 #include <sound/control.h>
80 #include <media/v4l2-dev.h>
81 #include <media/v4l2-device.h>
82 #include <linux/videodev2.h>
83 #include <linux/soundcard.h>
85 /*---------------------------------------------------------------------------*/
86 /* VENDOR, PRODUCT: Syntek Semiconductor Co., Ltd
88 * EITHER EasyCAP USB 2.0 Video Adapter with Audio, Model No. DC60
89 * with input cabling: AUDIO(L), AUDIO(R), CVBS, S-VIDEO.
91 * OR EasyCAP 4CHANNEL USB 2.0 DVR, Model No. EasyCAP002
92 * with input cabling: MICROPHONE, CVBS1, CVBS2, CVBS3, CVBS4.
94 /*---------------------------------------------------------------------------*/
95 #define USB_EASYCAP_VENDOR_ID 0x05e1
96 #define USB_EASYCAP_PRODUCT_ID 0x0408
98 #define EASYCAP_DRIVER_VERSION "0.9.01"
99 #define EASYCAP_DRIVER_DESCRIPTION "easycapdc60"
101 #define DONGLE_MANY 8
103 /*---------------------------------------------------------------------------*/
105 * DEFAULT LUMINANCE, CONTRAST, SATURATION AND HUE
107 /*---------------------------------------------------------------------------*/
108 #define SAA_0A_DEFAULT 0x7F
109 #define SAA_0B_DEFAULT 0x3F
110 #define SAA_0C_DEFAULT 0x2F
111 #define SAA_0D_DEFAULT 0x00
112 /*---------------------------------------------------------------------------*/
114 * VIDEO STREAMING PARAMETERS:
115 * USB 2.0 PROVIDES FOR HIGH-BANDWIDTH ENDPOINTS WITH AN UPPER LIMIT
116 * OF 3072 BYTES PER MICROFRAME for wMaxPacketSize.
118 /*---------------------------------------------------------------------------*/
119 #define VIDEO_ISOC_BUFFER_MANY 16
120 #define VIDEO_ISOC_ORDER 3
121 #define VIDEO_ISOC_FRAMESPERDESC ((unsigned int) 1 << VIDEO_ISOC_ORDER)
122 #define USB_2_0_MAXPACKETSIZE 3072
123 #if (USB_2_0_MAXPACKETSIZE > PAGE_SIZE)
124 #error video_isoc_buffer[.] will not be big enough
126 #define VIDEO_JUNK_TOLERATE VIDEO_ISOC_BUFFER_MANY
127 #define VIDEO_LOST_TOLERATE 50
128 /*---------------------------------------------------------------------------*/
132 /*---------------------------------------------------------------------------*/
133 #define FIELD_BUFFER_SIZE (203 * PAGE_SIZE)
134 #define FRAME_BUFFER_SIZE (405 * PAGE_SIZE)
135 #define FIELD_BUFFER_MANY 4
136 #define FRAME_BUFFER_MANY 6
137 /*---------------------------------------------------------------------------*/
139 * AUDIO STREAMING PARAMETERS
141 /*---------------------------------------------------------------------------*/
142 #define AUDIO_ISOC_BUFFER_MANY 16
143 #define AUDIO_ISOC_ORDER 1
144 #define AUDIO_ISOC_FRAMESPERDESC 32
145 #define AUDIO_ISOC_BUFFER_SIZE (PAGE_SIZE << AUDIO_ISOC_ORDER)
146 /*---------------------------------------------------------------------------*/
150 /*---------------------------------------------------------------------------*/
151 #define AUDIO_FRAGMENT_MANY 32
152 #define PAGES_PER_AUDIO_FRAGMENT 4
153 /*---------------------------------------------------------------------------*/
155 * IT IS ESSENTIAL THAT EVEN-NUMBERED STANDARDS ARE 25 FRAMES PER SECOND,
156 * ODD-NUMBERED STANDARDS ARE 30 FRAMES PER SECOND.
157 * THE NUMBERING OF STANDARDS MUST NOT BE CHANGED WITHOUT DUE CARE. NOT
158 * ONLY MUST THE PARAMETER
160 * BE CHANGED TO CORRESPOND TO THE NEW NUMBER OF STANDARDS, BUT ALSO THE
161 * NUMBERING MUST REMAIN AN UNBROKEN ASCENDING SEQUENCE: DUMMY STANDARDS
162 * MAY NEED TO BE ADDED. APPROPRIATE CHANGES WILL ALWAYS BE REQUIRED IN
163 * ROUTINE fillin_formats() AND POSSIBLY ELSEWHERE. BEWARE.
165 /*---------------------------------------------------------------------------*/
176 #define PAL_BGHIN_SLOW 10
177 #define PAL_Nc_SLOW 12
178 #define SECAM_SLOW 14
179 #define NTSC_N_SLOW 16
180 #define NTSC_N_443_SLOW 18
181 #define NTSC_M_SLOW 11
182 #define NTSC_443_SLOW 13
183 #define NTSC_M_JP_SLOW 15
184 #define PAL_60_SLOW 17
185 #define PAL_M_SLOW 19
186 #define STANDARD_MANY 20
187 /*---------------------------------------------------------------------------*/
191 /*---------------------------------------------------------------------------*/
216 #define SETTINGS_MANY (STANDARD_MANY * \
221 /*---------------------------------------------------------------------------*/
223 * STRUCTURE DEFINITIONS
225 /*---------------------------------------------------------------------------*/
226 struct easycap_dongle
{
227 struct easycap
*peasycap
;
228 struct mutex mutex_video
;
229 struct mutex mutex_audio
;
231 /*---------------------------------------------------------------------------*/
233 struct list_head list_head
;
239 /*---------------------------------------------------------------------------*/
241 struct list_head list_head
;
246 /*---------------------------------------------------------------------------*/
247 struct easycap_standard
{
249 struct v4l2_standard v4l2_standard
;
251 struct easycap_format
{
254 struct v4l2_format v4l2_format
;
260 int standard_offset_ok
;
262 int format_offset_ok
;
272 /*---------------------------------------------------------------------------*/
274 * easycap.ilk == 0 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=256
275 * easycap.ilk == 2 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=9
276 * easycap.ilk == 3 => FOUR-CVBS HARDWARE, AUDIO wMaxPacketSize=9
278 /*---------------------------------------------------------------------------*/
283 struct video_device video_device
;
284 struct v4l2_device v4l2_device
;
287 unsigned int audio_pages_per_fragment
;
288 unsigned int audio_bytes_per_fragment
;
289 unsigned int audio_buffer_page_many
;
299 struct usb_device
*pusb_device
;
300 struct usb_interface
*pusb_interface
;
304 int queued
[FRAME_BUFFER_MANY
];
305 int done
[FRAME_BUFFER_MANY
];
307 wait_queue_head_t wq_video
;
308 wait_queue_head_t wq_audio
;
309 wait_queue_head_t wq_trigger
;
315 struct inputset inputset
[INPUT_MANY
];
323 int lost
[INPUT_MANY
];
327 int video_altsetting_on
;
328 int video_altsetting_off
;
329 int video_endpointnumber
;
330 int video_isoc_maxframesize
;
331 int video_isoc_buffer_size
;
332 int video_isoc_framesperdesc
;
334 int video_isoc_streaming
;
335 int video_isoc_sequence
;
340 struct data_buffer video_isoc_buffer
[VIDEO_ISOC_BUFFER_MANY
];
341 struct data_buffer field_buffer
[FIELD_BUFFER_MANY
]
342 [(FIELD_BUFFER_SIZE
/PAGE_SIZE
)];
343 struct data_buffer frame_buffer
[FRAME_BUFFER_MANY
]
344 [(FRAME_BUFFER_SIZE
/PAGE_SIZE
)];
346 struct list_head urb_video_head
;
347 struct list_head
*purb_video_head
;
356 /*---------------------------------------------------------------------------*/
360 /*---------------------------------------------------------------------------*/
361 int field_fill
; /* Field buffer being filled by easycap_complete(). */
362 /* Bumped only by easycap_complete(). */
363 int field_page
; /* Page of field buffer page being filled by */
364 /* easycap_complete(). */
365 int field_read
; /* Field buffer to be read by field2frame(). */
366 /* Bumped only by easycap_complete(). */
367 int frame_fill
; /* Frame buffer being filled by field2frame(). */
368 /* Bumped only by easycap_dqbuf() when */
369 /* field2frame() has created a complete frame. */
370 int frame_read
; /* Frame buffer offered to user by DQBUF. */
371 /* Set only by easycap_dqbuf() to trail frame_fill.*/
372 int frame_lock
; /* Flag set to 1 by DQBUF and cleared by QBUF */
373 /*---------------------------------------------------------------------------*/
377 /*---------------------------------------------------------------------------*/
385 int frame_buffer_used
;
386 int frame_buffer_many
;
387 int videofieldamount
;
394 int allocation_video_urb
;
395 int allocation_video_page
;
396 int allocation_video_struct
;
397 int registered_video
;
398 /*---------------------------------------------------------------------------*/
402 /*---------------------------------------------------------------------------*/
403 struct snd_pcm_hardware alsa_hardware
;
404 struct snd_card
*psnd_card
;
405 struct snd_pcm
*psnd_pcm
;
406 struct snd_pcm_substream
*psubstream
;
410 /*---------------------------------------------------------------------------*/
414 /*---------------------------------------------------------------------------*/
416 int audio_altsetting_on
;
417 int audio_altsetting_off
;
418 int audio_endpointnumber
;
419 int audio_isoc_maxframesize
;
420 int audio_isoc_buffer_size
;
421 int audio_isoc_framesperdesc
;
423 int audio_isoc_streaming
;
430 struct data_buffer audio_isoc_buffer
[AUDIO_ISOC_BUFFER_MANY
];
432 struct list_head urb_audio_head
;
433 struct list_head
*purb_audio_head
;
434 /*---------------------------------------------------------------------------*/
438 /*---------------------------------------------------------------------------*/
439 int audio_fill
; /* Audio buffer being filled by easycap_complete(). */
440 /* Bumped only by easycap_complete(). */
441 int audio_read
; /* Audio buffer page being read by easycap_read(). */
442 /* Set by easycap_read() to trail audio_fill by */
444 /*---------------------------------------------------------------------------*/
448 /*---------------------------------------------------------------------------*/
449 int allocation_audio_urb
;
450 int allocation_audio_page
;
451 int allocation_audio_struct
;
452 int registered_audio
;
454 long long int audio_sample
;
455 long long int audio_niveau
;
456 long long int audio_square
;
458 struct data_buffer audio_buffer
[];
460 /*---------------------------------------------------------------------------*/
462 * VIDEO FUNCTION PROTOTYPES
464 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
465 int easycap_newinput(struct easycap
*, int);
466 void easycap_testcard(struct easycap
*, int);
467 int easycap_isdongle(struct easycap
*);
469 long easycap_unlocked_ioctl(struct file
*, unsigned int, unsigned long);
471 int easycap_video_dqbuf(struct easycap
*, int);
472 int easycap_video_submit_urbs(struct easycap
*);
473 int easycap_video_kill_urbs(struct easycap
*);
474 int easycap_video_fillin_formats(void);
476 int adjust_standard(struct easycap
*, v4l2_std_id
);
477 int adjust_format(struct easycap
*, u32
, u32
, u32
, int, bool);
478 int adjust_brightness(struct easycap
*, int);
479 int adjust_contrast(struct easycap
*, int);
480 int adjust_saturation(struct easycap
*, int);
481 int adjust_hue(struct easycap
*, int);
482 /*---------------------------------------------------------------------------*/
484 * AUDIO FUNCTION PROTOTYPES
486 /*---------------------------------------------------------------------------*/
487 int easycap_alsa_probe(struct easycap
*);
488 int easycap_audio_kill_urbs(struct easycap
*);
489 void easycap_alsa_complete(struct urb
*);
490 /*---------------------------------------------------------------------------*/
492 * LOW-LEVEL FUNCTION PROTOTYPES
494 /*---------------------------------------------------------------------------*/
495 int easycap_audio_gainset(struct usb_device
*, s8
);
496 int easycap_audio_setup(struct easycap
*);
498 int easycap_wakeup_device(struct usb_device
*);
500 int setup_stk(struct usb_device
*, bool);
501 int setup_saa(struct usb_device
*, bool);
502 int ready_saa(struct usb_device
*);
503 int merit_saa(struct usb_device
*);
504 int check_vt(struct usb_device
*);
505 int select_input(struct usb_device
*, int, int);
506 int set_resolution(struct usb_device
*, u16
, u16
, u16
, u16
);
508 int read_saa(struct usb_device
*, u16
);
509 int write_saa(struct usb_device
*, u16
, u16
);
510 int start_100(struct usb_device
*);
511 int stop_100(struct usb_device
*);
512 /*---------------------------------------------------------------------------*/
515 /*---------------------------------------------------------------------------*/
517 * MACROS SAM(...) AND JOM(...) ALLOW DIAGNOSTIC OUTPUT TO BE TAGGED WITH
518 * THE IDENTITY OF THE DONGLE TO WHICH IT APPLIES, BUT IF INVOKED WHEN THE
519 * POINTER peasycap IS INVALID AN Oops IS LIKELY, AND ITS CAUSE MAY NOT BE
520 * IMMEDIATELY OBVIOUS FROM A CASUAL READING OF THE SOURCE CODE. BEWARE.
522 /*---------------------------------------------------------------------------*/
523 const char *strerror(int err
);
525 #define SAY(format, args...) do { \
526 printk(KERN_DEBUG "easycap:: %s: " \
527 format, __func__, ##args); \
529 #define SAM(format, args...) do { \
530 printk(KERN_DEBUG "easycap::%i%s: " \
531 format, peasycap->isdongle, __func__, ##args);\
534 #ifdef CONFIG_EASYCAP_DEBUG
535 extern int easycap_debug
;
536 #define JOT(n, format, args...) do { \
537 if (n <= easycap_debug) { \
538 printk(KERN_DEBUG "easycap:: %s: " \
539 format, __func__, ##args);\
542 #define JOM(n, format, args...) do { \
543 if (n <= easycap_debug) { \
544 printk(KERN_DEBUG "easycap::%i%s: " \
545 format, peasycap->isdongle, __func__, ##args);\
550 #define JOT(n, format, args...) do {} while (0)
551 #define JOM(n, format, args...) do {} while (0)
552 #endif /* CONFIG_EASYCAP_DEBUG */
554 /*---------------------------------------------------------------------------*/
556 /*---------------------------------------------------------------------------*/
559 /*---------------------------------------------------------------------------*/
561 extern bool easycap_readback
;
562 extern const struct easycap_standard easycap_standard
[];
563 extern struct easycap_format easycap_format
[];
564 extern struct v4l2_queryctrl easycap_control
[];
565 extern struct easycap_dongle easycapdc60_dongle
[];
567 #endif /* !__EASYCAP_H__ */