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
36 * EASYCAP_NEEDS_UNLOCKED_IOCTL
38 * IF REQUIRED THEY MUST BE EXTERNALLY DEFINED, FOR EXAMPLE AS COMPILER
41 /*---------------------------------------------------------------------------*/
43 #if (!defined(EASYCAP_H))
46 /*---------------------------------------------------------------------------*/
48 * THESE ARE NORMALLY DEFINED
50 /*---------------------------------------------------------------------------*/
54 /*---------------------------------------------------------------------------*/
56 * THESE ARE FOR MAINTENANCE ONLY - NORMALLY UNDEFINED:
58 /*---------------------------------------------------------------------------*/
59 #undef EASYCAP_TESTCARD
60 #undef EASYCAP_TESTTONE
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/smp_lock.h>
71 #include <linux/usb.h>
72 #include <linux/uaccess.h>
74 #include <linux/i2c.h>
75 #include <linux/version.h>
76 #include <linux/workqueue.h>
77 #include <linux/poll.h>
80 #include <linux/delay.h>
81 #include <linux/types.h>
83 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
84 #if defined(EASYCAP_IS_VIDEODEV_CLIENT)
85 #include <media/v4l2-dev.h>
86 #if defined(EASYCAP_NEEDS_V4L2_DEVICE_H)
87 #include <media/v4l2-device.h>
88 #endif /*EASYCAP_NEEDS_V4L2_DEVICE_H*/
89 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
90 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
91 #include <linux/videodev2.h>
92 #include <linux/soundcard.h>
93 #if defined(EASYCAP_NEEDS_USBVIDEO_H)
94 #include <config/video/usbvideo.h>
95 #endif /*EASYCAP_NEEDS_USBVIDEO_H*/
97 #if (!defined(PAGE_SIZE))
98 #error "PAGE_SIZE not defined"
101 #define STRINGIZE_AGAIN(x) #x
102 #define STRINGIZE(x) STRINGIZE_AGAIN(x)
103 /*---------------------------------------------------------------------------*/
104 /* VENDOR, PRODUCT: Syntek Semiconductor Co., Ltd
106 * EITHER EasyCAP USB 2.0 Video Adapter with Audio, Model No. DC60
107 * with input cabling: AUDIO(L), AUDIO(R), CVBS, S-VIDEO.
109 * OR EasyCAP 4CHANNEL USB 2.0 DVR, Model No. EasyCAP002
110 * with input cabling: MICROPHONE, CVBS1, CVBS2, CVBS3, CVBS4.
112 /*---------------------------------------------------------------------------*/
113 #define USB_EASYCAP_VENDOR_ID 0x05e1
114 #define USB_EASYCAP_PRODUCT_ID 0x0408
116 #define EASYCAP_DRIVER_VERSION "0.8.41"
117 #define EASYCAP_DRIVER_DESCRIPTION "easycapdc60"
119 #define USB_SKEL_MINOR_BASE 192
120 #define DONGLE_MANY 8
122 /*---------------------------------------------------------------------------*/
124 * DEFAULT LUMINANCE, CONTRAST, SATURATION AND HUE
126 /*---------------------------------------------------------------------------*/
127 #define SAA_0A_DEFAULT 0x7F
128 #define SAA_0B_DEFAULT 0x3F
129 #define SAA_0C_DEFAULT 0x2F
130 #define SAA_0D_DEFAULT 0x00
131 /*---------------------------------------------------------------------------*/
133 * VIDEO STREAMING PARAMETERS:
134 * USB 2.0 PROVIDES FOR HIGH-BANDWIDTH ENDPOINTS WITH AN UPPER LIMIT
135 * OF 3072 BYTES PER MICROFRAME for wMaxPacketSize.
137 /*---------------------------------------------------------------------------*/
138 #define VIDEO_ISOC_BUFFER_MANY 16
139 #define VIDEO_ISOC_ORDER 3
140 #define VIDEO_ISOC_FRAMESPERDESC ((unsigned int) 1 << VIDEO_ISOC_ORDER)
141 #define USB_2_0_MAXPACKETSIZE 3072
142 #if (USB_2_0_MAXPACKETSIZE > PAGE_SIZE)
143 #error video_isoc_buffer[.] will not be big enough
145 #define VIDEO_JUNK_TOLERATE VIDEO_ISOC_BUFFER_MANY
146 #define VIDEO_LOST_TOLERATE 50
147 /*---------------------------------------------------------------------------*/
151 /*---------------------------------------------------------------------------*/
152 #define FIELD_BUFFER_SIZE (203 * PAGE_SIZE)
153 #define FRAME_BUFFER_SIZE (405 * PAGE_SIZE)
154 #define FIELD_BUFFER_MANY 4
155 #define FRAME_BUFFER_MANY 6
156 /*---------------------------------------------------------------------------*/
158 * AUDIO STREAMING PARAMETERS
160 /*---------------------------------------------------------------------------*/
161 #define AUDIO_ISOC_BUFFER_MANY 16
162 #define AUDIO_ISOC_ORDER 3
163 #define AUDIO_ISOC_BUFFER_SIZE (PAGE_SIZE << AUDIO_ISOC_ORDER)
164 /*---------------------------------------------------------------------------*/
168 /*---------------------------------------------------------------------------*/
169 #define AUDIO_FRAGMENT_MANY 32
170 /*---------------------------------------------------------------------------*/
172 * IT IS ESSENTIAL THAT EVEN-NUMBERED STANDARDS ARE 25 FRAMES PER SECOND,
173 * ODD-NUMBERED STANDARDS ARE 30 FRAMES PER SECOND.
174 * THE NUMBERING OF STANDARDS MUST NOT BE CHANGED WITHOUT DUE CARE. NOT
175 * ONLY MUST THE PARAMETER
177 * BE CHANGED TO CORRESPOND TO THE NEW NUMBER OF STANDARDS, BUT ALSO THE
178 * NUMBERING MUST REMAIN AN UNBROKEN ASCENDING SEQUENCE: DUMMY STANDARDS
179 * MAY NEED TO BE ADDED. APPROPRIATE CHANGES WILL ALWAYS BE REQUIRED IN
180 * ROUTINE fillin_formats() AND POSSIBLY ELSEWHERE. BEWARE.
182 /*---------------------------------------------------------------------------*/
193 #define PAL_BGHIN_SLOW 10
194 #define PAL_Nc_SLOW 12
195 #define SECAM_SLOW 14
196 #define NTSC_N_SLOW 16
197 #define NTSC_N_443_SLOW 18
198 #define NTSC_M_SLOW 11
199 #define NTSC_443_SLOW 13
200 #define NTSC_M_JP_SLOW 15
201 #define PAL_60_SLOW 17
202 #define PAL_M_SLOW 19
203 #define STANDARD_MANY 20
204 /*---------------------------------------------------------------------------*/
208 /*---------------------------------------------------------------------------*/
233 #define SETTINGS_MANY (STANDARD_MANY * \
238 /*---------------------------------------------------------------------------*/
240 * STRUCTURE DEFINITIONS
242 /*---------------------------------------------------------------------------*/
243 struct easycap_dongle
{
244 struct easycap
*peasycap
;
245 struct mutex mutex_video
;
246 struct mutex mutex_audio
;
248 /*---------------------------------------------------------------------------*/
250 struct list_head list_head
;
256 /*---------------------------------------------------------------------------*/
258 struct list_head list_head
;
263 /*---------------------------------------------------------------------------*/
264 struct easycap_standard
{
266 struct v4l2_standard v4l2_standard
;
268 struct easycap_format
{
271 struct v4l2_format v4l2_format
;
277 int standard_offset_ok
;
279 int format_offset_ok
;
289 /*---------------------------------------------------------------------------*/
291 * easycap.ilk == 0 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=256
292 * easycap.ilk == 2 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=9
293 * easycap.ilk == 3 => FOUR-CVBS HARDWARE, AUDIO wMaxPacketSize=9
295 /*---------------------------------------------------------------------------*/
297 #define TELLTALE "expectedstring"
301 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
302 #if defined(EASYCAP_IS_VIDEODEV_CLIENT)
303 struct video_device video_device
;
304 #if defined(EASYCAP_NEEDS_V4L2_DEVICE_H)
305 struct v4l2_device v4l2_device
;
306 #endif /*EASYCAP_NEEDS_V4L2_DEVICE_H*/
307 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
308 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
310 unsigned int audio_pages_per_fragment
;
311 unsigned int audio_bytes_per_fragment
;
312 unsigned int audio_buffer_page_many
;
315 #if defined(UPSAMPLE)
322 struct usb_device
*pusb_device
;
323 struct usb_interface
*pusb_interface
;
327 int queued
[FRAME_BUFFER_MANY
];
328 int done
[FRAME_BUFFER_MANY
];
330 wait_queue_head_t wq_video
;
331 wait_queue_head_t wq_audio
;
337 struct inputset inputset
[INPUT_MANY
];
345 int lost
[INPUT_MANY
];
348 struct timeval timeval0
;
349 struct timeval timeval1
;
350 struct timeval timeval2
;
351 struct timeval timeval3
;
352 struct timeval timeval6
;
353 struct timeval timeval7
;
354 struct timeval timeval8
;
355 long long int dnbydt
;
358 int video_altsetting_on
;
359 int video_altsetting_off
;
360 int video_endpointnumber
;
361 int video_isoc_maxframesize
;
362 int video_isoc_buffer_size
;
363 int video_isoc_framesperdesc
;
365 int video_isoc_streaming
;
366 int video_isoc_sequence
;
371 struct data_buffer video_isoc_buffer
[VIDEO_ISOC_BUFFER_MANY
];
373 field_buffer
[FIELD_BUFFER_MANY
][(FIELD_BUFFER_SIZE
/PAGE_SIZE
)];
375 frame_buffer
[FRAME_BUFFER_MANY
][(FRAME_BUFFER_SIZE
/PAGE_SIZE
)];
377 struct list_head urb_video_head
;
378 struct list_head
*purb_video_head
;
384 long long audio_bytes
;
389 /*---------------------------------------------------------------------------*/
393 /*---------------------------------------------------------------------------*/
394 int field_fill
; /* Field buffer being filled by easycap_complete(). */
395 /* Bumped only by easycap_complete(). */
396 int field_page
; /* Page of field buffer page being filled by */
397 /* easycap_complete(). */
398 int field_read
; /* Field buffer to be read by field2frame(). */
399 /* Bumped only by easycap_complete(). */
400 int frame_fill
; /* Frame buffer being filled by field2frame(). */
401 /* Bumped only by easycap_dqbuf() when */
402 /* field2frame() has created a complete frame. */
403 int frame_read
; /* Frame buffer offered to user by DQBUF. */
404 /* Set only by easycap_dqbuf() to trail frame_fill.*/
405 int frame_lock
; /* Flag set to 1 by DQBUF and cleared by QBUF */
406 /*---------------------------------------------------------------------------*/
410 /*---------------------------------------------------------------------------*/
418 int frame_buffer_used
;
419 int frame_buffer_many
;
420 int videofieldamount
;
427 int allocation_video_urb
;
428 int allocation_video_page
;
429 int allocation_video_struct
;
430 int registered_video
;
431 /*---------------------------------------------------------------------------*/
435 /*---------------------------------------------------------------------------*/
437 int audio_altsetting_on
;
438 int audio_altsetting_off
;
439 int audio_endpointnumber
;
440 int audio_isoc_maxframesize
;
441 int audio_isoc_buffer_size
;
442 int audio_isoc_framesperdesc
;
444 int audio_isoc_streaming
;
450 struct data_buffer audio_isoc_buffer
[AUDIO_ISOC_BUFFER_MANY
];
452 struct list_head urb_audio_head
;
453 struct list_head
*purb_audio_head
;
454 /*---------------------------------------------------------------------------*/
458 /*---------------------------------------------------------------------------*/
459 int audio_fill
; /* Audio buffer being filled by easysnd_complete(). */
460 /* Bumped only by easysnd_complete(). */
461 int audio_read
; /* Audio buffer page being read by easysnd_read(). */
462 /* Set by easysnd_read() to trail audio_fill by */
464 /*---------------------------------------------------------------------------*/
468 /*---------------------------------------------------------------------------*/
470 int audio_buffer_many
;
472 int allocation_audio_urb
;
473 int allocation_audio_page
;
474 int allocation_audio_struct
;
475 int registered_audio
;
477 long long int audio_sample
;
478 long long int audio_niveau
;
479 long long int audio_square
;
481 struct data_buffer audio_buffer
[];
483 /*---------------------------------------------------------------------------*/
485 * VIDEO FUNCTION PROTOTYPES
487 /*---------------------------------------------------------------------------*/
488 void easycap_complete(struct urb
*);
489 int easycap_open(struct inode
*, struct file
*);
490 int easycap_release(struct inode
*, struct file
*);
491 long easycap_ioctl_noinode(struct file
*, unsigned int, \
493 int easycap_ioctl(struct inode
*, struct file
*, unsigned int, \
495 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
496 #if defined(EASYCAP_IS_VIDEODEV_CLIENT)
497 int easycap_open_noinode(struct file
*);
498 int easycap_release_noinode(struct file
*);
499 int videodev_release(struct video_device
*);
500 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
501 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
503 unsigned int easycap_poll(struct file
*, poll_table
*);
504 int easycap_mmap(struct file
*, struct vm_area_struct
*);
505 int easycap_usb_probe(struct usb_interface
*, \
506 const struct usb_device_id
*);
507 void easycap_usb_disconnect(struct usb_interface
*);
508 void easycap_delete(struct kref
*);
510 void easycap_vma_open(struct vm_area_struct
*);
511 void easycap_vma_close(struct vm_area_struct
*);
512 int easycap_vma_fault(struct vm_area_struct
*, struct vm_fault
*);
513 int easycap_dqbuf(struct easycap
*, int);
514 int submit_video_urbs(struct easycap
*);
515 int kill_video_urbs(struct easycap
*);
516 int field2frame(struct easycap
*);
517 int redaub(struct easycap
*, void *, void *, \
518 int, int, __u8
, __u8
, bool);
519 void easycap_testcard(struct easycap
*, int);
520 int fillin_formats(void);
521 int reset(struct easycap
*);
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 void easysnd_complete(struct urb
*);
537 ssize_t
easysnd_read(struct file
*, char __user
*, size_t, loff_t
*);
538 int easysnd_open(struct inode
*, struct file
*);
539 int easysnd_release(struct inode
*, struct file
*);
540 long easysnd_ioctl_noinode(struct file
*, unsigned int, \
542 int easysnd_ioctl(struct inode
*, struct file
*, unsigned int, \
544 unsigned int easysnd_poll(struct file
*, poll_table
*);
545 void easysnd_delete(struct kref
*);
546 int submit_audio_urbs(struct easycap
*);
547 int kill_audio_urbs(struct easycap
*);
548 void easysnd_testtone(struct easycap
*, int);
549 int audio_setup(struct easycap
*);
550 /*---------------------------------------------------------------------------*/
552 * LOW-LEVEL FUNCTION PROTOTYPES
554 /*---------------------------------------------------------------------------*/
555 int audio_gainget(struct usb_device
*);
556 int audio_gainset(struct usb_device
*, __s8
);
558 int set_interface(struct usb_device
*, __u16
);
559 int wakeup_device(struct usb_device
*);
560 int confirm_resolution(struct usb_device
*);
561 int confirm_stream(struct usb_device
*);
563 int setup_stk(struct usb_device
*, bool);
564 int setup_saa(struct usb_device
*, bool);
565 int setup_vt(struct usb_device
*);
566 int check_stk(struct usb_device
*, bool);
567 int check_saa(struct usb_device
*, bool);
568 int ready_saa(struct usb_device
*);
569 int merit_saa(struct usb_device
*);
570 int check_vt(struct usb_device
*);
571 int select_input(struct usb_device
*, int, int);
572 int set_resolution(struct usb_device
*, \
573 __u16
, __u16
, __u16
, __u16
);
575 int read_saa(struct usb_device
*, __u16
);
576 int read_stk(struct usb_device
*, __u32
);
577 int write_saa(struct usb_device
*, __u16
, __u16
);
578 int wait_i2c(struct usb_device
*);
579 int write_000(struct usb_device
*, __u16
, __u16
);
580 int start_100(struct usb_device
*);
581 int stop_100(struct usb_device
*);
582 int write_300(struct usb_device
*);
583 int read_vt(struct usb_device
*, __u16
);
584 int write_vt(struct usb_device
*, __u16
, __u16
);
585 int regset(struct usb_device
*, __u16
, __u16
);
586 int regget(struct usb_device
*, __u16
, void *);
587 int isdongle(struct easycap
*);
588 /*---------------------------------------------------------------------------*/
589 struct signed_div_result
{
590 long long int quotient
;
591 unsigned long long int remainder
;
592 } 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 #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 #if defined(EASYCAP_DEBUG)
632 #define JOT(n, format, args...) do { \
633 if (n <= easycap_debug) { \
634 printk(KERN_DEBUG "easycap:: %s: " \
635 format, __func__, ##args);\
638 #define JOM(n, format, args...) do { \
639 if (n <= easycap_debug) { \
640 printk(KERN_DEBUG "easycap::%i%s: " \
641 format, peasycap->isdongle, __func__, ##args);\
646 #define JOT(n, format, args...) do {} while (0)
647 #define JOM(n, format, args...) do {} while (0)
648 #endif /*EASYCAP_DEBUG*/
650 #define MICROSECONDS(X, Y) \
651 ((1000000*((long long int)(X.tv_sec - Y.tv_sec))) + \
652 (long long int)(X.tv_usec - Y.tv_usec))
654 /*---------------------------------------------------------------------------*/
656 * (unsigned char *)P pointer to next byte pair
657 * (long int *)X pointer to accumulating count
658 * (long int *)Y pointer to accumulating sum
659 * (long long int *)Z pointer to accumulating sum of squares
661 /*---------------------------------------------------------------------------*/
662 #define SUMMER(P, X, Y, Z) do { \
664 unsigned int u0, u1, u2; \
666 p = (unsigned char *)(P); \
667 u0 = (unsigned int) (*p); \
668 u1 = (unsigned int) (*(p + 1)); \
669 u2 = (unsigned int) ((u1 << 8) | u0); \
671 s = -(long int)(0x7FFF & (~u2)); \
673 s = (long int)(0x7FFF & u2); \
674 *((X)) += (long int) 1; \
675 *((Y)) += (long int) s; \
676 *((Z)) += ((long long int)(s) * (long long int)(s)); \
678 /*---------------------------------------------------------------------------*/